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

150 lines
5.4 KiB
C#
Raw Normal View History

2025-10-03 00:02:43 -04:00
using System;
using System.Collections.Generic;
2025-10-31 10:02:30 -04:00
using System.Linq;
2025-10-03 00:02:43 -04:00
using SoftCircuits.Collections;
using UnityEngine;
2025-10-23 00:49:44 -04:00
namespace SLSFramework.General
2025-10-03 00:02:43 -04:00
{
public static class DictionaryExtension
{
2025-10-31 10:02:30 -04:00
public static void Invoke(this IDictionary<string, PrioritizedAction> dictionary)
2025-10-03 00:02:43 -04:00
{
foreach (var action in dictionary.Values)
{
action.Invoke();
}
}
2025-10-31 10:02:30 -04:00
public static void Invoke<T>(this IDictionary<string, PrioritizedAction<T>> dictionary, T arg)
2025-10-03 00:02:43 -04:00
{
foreach (var action in dictionary.Values)
{
action.Invoke(arg);
}
}
2025-10-31 10:02:30 -04:00
public static void Invoke<T1, T2>(this IDictionary<string, PrioritizedAction<T1, T2>> dictionary, T1 arg1, T2 arg2)
2025-10-03 00:02:43 -04:00
{
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>
2025-10-23 00:49:44 -04:00
public static void PasteDictionary<T1, T2>(this IDictionary<T1, T2> source, IDictionary<T1, T2> target)
2025-10-03 00:02:43 -04:00
{
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.");
}
}
}
}
2025-10-31 10:02:30 -04:00
public static class CheckAndEffectDictionaryExtension
{
public static List<Func<bool>> GetChecks(this IDictionary<string, PrioritizedCheckAndEffect> dictionary)
{
return dictionary.Values.Select(checkAndEffect => checkAndEffect.check).ToList();
}
public static List<Action> GetEffects(this IDictionary<string, PrioritizedCheckAndEffect> dictionary)
{
return dictionary.Values.Select(checkAndEffect => checkAndEffect.effect).ToList();
}
public static void InvokeIfAnyConditionChecked(this IDictionary<string, PrioritizedCheckAndEffect> dictionary)
{
List<Func<bool>> checks = dictionary.Values.Select(checkAndEffect => checkAndEffect.check).ToList();
List<Action> effects = dictionary.Values.Select(checkAndEffect => checkAndEffect.effect).ToList();
if (checks.Any())
{
effects.ForEach(effect => effect.Invoke());
}
}
}
2025-10-03 00:02:43 -04:00
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);
}
}
}
}