This commit is contained in:
2025-07-27 16:45:42 +08:00
parent 5d1181ef97
commit 502edf03a0
12 changed files with 291 additions and 51 deletions

View File

@@ -0,0 +1,52 @@
using System.Collections.Generic;
using Verse;
namespace WulaFallenEmpire
{
public static class EventContext
{
private static Dictionary<string, object> variables = new Dictionary<string, object>();
public static void SetVariable(string name, object value)
{
if (variables.ContainsKey(name))
{
variables[name] = value;
}
else
{
variables.Add(name, value);
}
Log.Message($"[EventContext] Set variable '{name}' to '{value}'.");
}
public static T GetVariable<T>(string name, T defaultValue = default)
{
if (variables.TryGetValue(name, out object value))
{
if (value is T typedValue)
{
return typedValue;
}
// Try to convert, e.g., from int to float
try
{
return (T)System.Convert.ChangeType(value, typeof(T));
}
catch (System.Exception)
{
Log.Warning($"[EventContext] Variable '{name}' is of type {value.GetType()} but could not be converted to {typeof(T)}.");
return defaultValue;
}
}
Log.Warning($"[EventContext] Variable '{name}' not found. Returning default value.");
return defaultValue;
}
public static void Clear()
{
variables.Clear();
Log.Message("[EventContext] All variables cleared.");
}
}
}