using System; using System.Collections.Generic; using System.Linq; using SoftCircuits.Collections; using UnityEngine; namespace SLSFramework.General { public static class DictionaryExtension { public static void Invoke(this IDictionary dictionary) { foreach (var action in dictionary.Values) { action.Invoke(); } } public static void Invoke(this IDictionary> dictionary, T arg) { foreach (var action in dictionary.Values) { action.Invoke(arg); } } public static void Invoke(this IDictionary> dictionary, T1 arg1, T2 arg2) { foreach (var action in dictionary.Values) { action.Invoke(arg1, arg2); } } public static void Invoke(this IDictionary> dictionary, T1 arg1, T2 arg2, T3 arg3) { foreach (var action in dictionary.Values) { action.Invoke(arg1, arg2, arg3); } } public static void ModifyOrAdd(this IDictionary dictionary, string key, float delta) { if (!dictionary.TryAdd(key, delta)) { dictionary[key] += delta; } } /// /// 从字典中获取指定键的值,并将其四舍五入为整数返回。 /// public static int GetRoundValue(this Dictionary dictionary, string key, int defaultValue = 0) { return dictionary.TryGetValue(key, out float value) ? Mathf.RoundToInt(value) : defaultValue; } /// /// 从字典中获取指定键的值,并将其向下取整为整数返回。 /// public static int GetFloorValue(this Dictionary dictionary, string key, int defaultValue = 0) { return dictionary.TryGetValue(key, out float value) ? Mathf.FloorToInt(value) : defaultValue; } /// /// 从字典中获取指定键的原始值。 /// public static float GetValue(this Dictionary dictionary, string key, float defaultValue = 0) { return dictionary.GetValueOrDefault(key, defaultValue); } /// /// 将源字典中的所有键值对粘贴到目标字典中,避免重复键。 /// public static void PasteDictionary(this IDictionary source, IDictionary 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 CheckAndEffectDictionaryExtension { public static List> GetChecks(this IDictionary dictionary) { return dictionary.Values.Select(checkAndEffect => checkAndEffect.check).ToList(); } public static List GetEffects(this IDictionary dictionary) { return dictionary.Values.Select(checkAndEffect => checkAndEffect.effect).ToList(); } public static void InvokeIfAnyConditionChecked(this IDictionary dictionary) { List> checks = dictionary.Values.Select(checkAndEffect => checkAndEffect.check).ToList(); List effects = dictionary.Values.Select(checkAndEffect => checkAndEffect.effect).ToList(); if (checks.Any()) { effects.ForEach(effect => effect.Invoke()); } } } public static class OrderedDictionaryExtension { /// /// 根据优先级将元素插入OrderedDictionary中对应的位置,保持字典按优先级从高到低排序。 /// public static void InsertByPriority(this OrderedDictionary 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 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); } /// /// 将字典中的元素根据优先级重新排序,保持字典按优先级从高到低排序。 /// public static void Sort(this OrderedDictionary dictionary) where T : IPrioritized { List> sortedItems = new List>(dictionary); sortedItems.Sort((pairA, pairB) => pairA.Value.CompareTo(pairB.Value)); dictionary.Clear(); foreach (KeyValuePair item in sortedItems) { dictionary.Add(item.Key, item.Value); } } } }