Files
Continentis/Assets/Scripts/ScriptExtensions/General/DictionaryExtension.cs

142 lines
4.8 KiB
C#
Raw Normal View History

2025-10-03 00:02:43 -04:00
using System;
using System.Collections.Generic;
using Continentis.MainGame;
using SoftCircuits.Collections;
using UnityEngine;
using UnityEngine.Events;
namespace SoulliesFramework.General
{
public static class DictionaryExtension
{
public static void Invoke(this IDictionary<string, EventUnit> dictionary)
{
foreach (var action in dictionary.Values)
{
action.Invoke();
}
}
public static void Invoke<T>(this IDictionary<string, EventUnit<T>> dictionary, T arg)
{
foreach (var action in dictionary.Values)
{
action.Invoke(arg);
}
}
public static void Invoke<T1, T2>(this IDictionary<string, EventUnit<T1, T2>> dictionary, T1 arg1, T2 arg2)
{
foreach (var action in dictionary.Values)
{
action.Invoke(arg1, arg2);
}
}
public static void ModifyOrAdd(this IDictionary<string, float> dictionary, string key, float delta)
{
if (!dictionary.TryAdd(key, delta))
{
dictionary[key] += delta;
}
}
/// <summary>
/// 从字典中获取指定键的值,并将其四舍五入为整数返回。
/// </summary>
public static int GetRoundValue(this Dictionary<string, float> dictionary, string key, int defaultValue = 0)
{
return dictionary.TryGetValue(key, out float value) ? Mathf.RoundToInt(value) : defaultValue;
}
/// <summary>
/// 从字典中获取指定键的值,并将其向下取整为整数返回。
/// </summary>
public static int GetFloorValue(this Dictionary<string, float> dictionary, string key, int defaultValue = 0)
{
return dictionary.TryGetValue(key, out float value) ? Mathf.FloorToInt(value) : defaultValue;
}
/// <summary>
/// 从字典中获取指定键的原始值。
/// </summary>
public static float GetValue(this Dictionary<string, float> dictionary, string key, float defaultValue = 0)
{
return dictionary.GetValueOrDefault(key, defaultValue);
}
/// <summary>
/// 将源字典中的所有键值对粘贴到目标字典中,避免重复键。
/// </summary>
public static void PasteDictionary<T1, T2>(this Dictionary<T1, T2> source, Dictionary<T1, T2> target)
{
foreach (var pair in source)
{
if (!target.ContainsKey(pair.Key))
{
target[pair.Key] = pair.Value;
}
else
{
Debug.LogWarning($"Attribute \"{pair.Key}\" already exists. Skipping duplicate.");
}
}
}
}
public static class OrderedDictionaryExtension
{
/// <summary>
/// 根据优先级将元素插入OrderedDictionary中对应的位置保持字典按优先级从高到低排序。
/// </summary>
public static void InsertByPriority<T>(this OrderedDictionary<string, T> dictionary, string key, T value) where T : IPrioritized
{
if (dictionary.ContainsKey(key))
{
Debug.LogWarning($"Key '{key}' already exists in the dictionary. Insertion skipped.");
return;
}
foreach (KeyValuePair<string, T> pair in dictionary)
{
if (value.Priority > pair.Value.Priority)
{
int index = dictionary.IndexOf(pair.Key);
dictionary.Insert(index, key, value);
return;
}
}
dictionary.Add(key, value);
}
/// <summary>
/// 将字典中的元素根据优先级重新排序,保持字典按优先级从高到低排序。
/// </summary>
public static void Sort<T>(this OrderedDictionary<string, T> dictionary) where T : IPrioritized
{
List<KeyValuePair<string,T>> sortedItems = new List<KeyValuePair<string, T>>(dictionary);
sortedItems.Sort((pairA, pairB) => pairA.Value.CompareTo(pairB.Value));
dictionary.Clear();
foreach (KeyValuePair<string, T> item in sortedItems)
{
dictionary.Add(item.Key, item.Value);
}
}
}
/// <summary>
/// 实现该接口的类可以根据优先级进行比较和排序。
/// 数字越大优先级越高。
/// </summary>
public interface IPrioritized : IComparable<IPrioritized>
{
int Priority { get; }
int IComparable<IPrioritized>.CompareTo(IPrioritized other)
{
return other.Priority.CompareTo(Priority);
}
}
}