Files
Continentis/Assets/Scripts/SLSUtilities/General/DictionaryExtension.cs
SoulliesOfficial c3b1561375 更新
2026-04-01 12:23:27 -04:00

158 lines
5.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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<string, PrioritizedAction> dictionary)
{
foreach (var action in dictionary.Values)
{
action.Invoke();
}
}
public static void Invoke<T>(this IDictionary<string, PrioritizedAction<T>> dictionary, T arg)
{
foreach (var action in dictionary.Values)
{
action.Invoke(arg);
}
}
public static void Invoke<T1, T2>(this IDictionary<string, PrioritizedAction<T1, T2>> dictionary, T1 arg1, T2 arg2)
{
foreach (var action in dictionary.Values)
{
action.Invoke(arg1, arg2);
}
}
public static void Invoke<T1, T2, T3>(this IDictionary<string, PrioritizedAction<T1, T2, T3>> dictionary, T1 arg1, T2 arg2, T3 arg3)
{
foreach (var action in dictionary.Values)
{
action.Invoke(arg1, arg2, arg3);
}
}
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 IDictionary<T1, T2> source, IDictionary<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 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());
}
}
}
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);
}
}
}
}