Files
Cielonos/Assets/Scripts/SLSUtilities/General/DictionaryExtension.cs
SoulliesOfficial 50ee502684 完善
2026-02-13 09:22:11 -05:00

201 lines
6.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using SoftCircuits.Collections;
using UnityEngine;
namespace SLSUtilities.General
{
public static class DictionaryExtension
{
/// <summary>
/// 修改字典中指定键的值,如果键不存在则添加该键值对。
/// </summary>
public static void ModifyOrAdd(this IDictionary<string, float> dictionary, string key, float delta, float defaultValue = 0)
{
if (!dictionary.TryAdd(key, defaultValue + delta))
{
dictionary[key] += delta;
}
}
/// <summary>
/// 从字典中获取指定键的原始值。
/// </summary>
public static float GetRawValue(this Dictionary<string, float> dictionary, string key, float defaultValue = 0)
{
return dictionary.GetValueOrDefault(key, defaultValue);
}
/// <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 void Paste<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 DictionaryExtensionForEvents
{
#region
public static void Invoke(this IDictionary<string, PrioritizedAction> dictionary)
{
foreach (string key in dictionary.Keys.ToList())
{
var pAction = dictionary[key];
pAction.Invoke();
if (pAction.ShouldRemove)
{
dictionary.Remove(key);
}
}
}
public static void Invoke<T>(this IDictionary<string, PrioritizedAction<T>> dictionary, T arg)
{
foreach (string key in dictionary.Keys.ToList())
{
var pAction = dictionary[key];
pAction.Invoke(arg);
if (pAction.ShouldRemove)
{
dictionary.Remove(key);
}
}
}
public static void Invoke<T0, T1>(this IDictionary<string, PrioritizedAction<T0, T1>> dictionary, T0 arg0, T1 arg1)
{
foreach (string key in dictionary.Keys.ToList())
{
var pAction = dictionary[key];
pAction.Invoke(arg0, arg1);
if (pAction.ShouldRemove)
{
dictionary.Remove(key);
}
}
}
public static void Invoke<T0, T1, T2>(this IDictionary<string, PrioritizedAction<T0, T1, T2>> dictionary, T0 arg0, T1 arg1, T2 arg2)
{
foreach (string key in dictionary.Keys.ToList())
{
var pAction = dictionary[key];
pAction.Invoke(arg0, arg1, arg2);
if (pAction.ShouldRemove)
{
dictionary.Remove(key);
}
}
}
#endregion
#region
public static Dictionary<string, TR> Invoke<TR>(this IDictionary<string, PrioritizedFunc<TR>> dictionary)
{
Dictionary<string, TR> results = new Dictionary<string, TR>();
foreach (string key in dictionary.Keys.ToList())
{
var pFunc = dictionary[key];
results[key] = pFunc.Invoke();
if (pFunc.ShouldRemove)
{
dictionary.Remove(key);
}
}
return results;
}
public static Dictionary<string, TR> Invoke<T, TR>(this IDictionary<string, PrioritizedFunc<T, TR>> dictionary, T arg)
{
Dictionary<string, TR> results = new Dictionary<string, TR>();
foreach (string key in dictionary.Keys.ToList())
{
var pFunc = dictionary[key];
results[key] = pFunc.Invoke(arg);
if (pFunc.ShouldRemove)
{
dictionary.Remove(key);
}
}
return results;
}
public static Dictionary<string, TR> Invoke<T0, T1, TR>(this IDictionary<string, PrioritizedFunc<T0, T1, TR>> dictionary, T0 arg0, T1 arg1)
{
Dictionary<string, TR> results = new Dictionary<string, TR>();
foreach (string key in dictionary.Keys.ToList())
{
var pFunc = dictionary[key];
results[key] = pFunc.Invoke(arg0, arg1);
if (pFunc.ShouldRemove)
{
dictionary.Remove(key);
}
}
return results;
}
public static Dictionary<string, TR> Invoke<T0, T1, T2, TR>(this IDictionary<string, PrioritizedFunc<T0, T1, T2, TR>> dictionary, T0 arg0, T1 arg1, T2 arg2)
{
Dictionary<string, TR> results = new Dictionary<string, TR>();
foreach (string key in dictionary.Keys.ToList())
{
var pFunc = dictionary[key];
results[key] = pFunc.Invoke(arg0, arg1, arg2);
if (pFunc.ShouldRemove)
{
dictionary.Remove(key);
}
}
return results;
}
#endregion
}
}