2using System.Collections.Generic;
4using System.Linq.Expressions;
5using System.Reflection;
8[AttributeUsage(AttributeTargets.Property)]
13 private static readonly Dictionary<string, (PropertyInfo property, Func<object> getter, Action<object> setter)>
_contextVars =
new Dictionary<
string, (PropertyInfo, Func<object>, Action<object>)>();
22 public override void Register(PropertyInfo property)
30 MethodInfo getMethod =
property.GetMethod;
31 MethodInfo setMethod =
property.SetMethod;
32 if (!(getMethod ==
null) && !(setMethod ==
null) && setMethod.IsStatic)
34 Func<object>
item = getMethod.CreateDelegate<Func<object>>();
36 Type declaringType =
property.DeclaringType;
43 Type parameterType = method.GetParameters()[0].ParameterType;
44 ParameterExpression parameterExpression = Expression.Parameter(typeof(
object),
"obj");
45 UnaryExpression arg = Expression.Convert(parameterExpression, parameterType);
46 return Expression.Lambda<Action<object>>(Expression.Call(method, arg),
new ParameterExpression[1] { parameterExpression }).Compile();
51 if (!context.Load<Dictionary<string, object>>(
"context_vars", out var data))
53 data =
new Dictionary<string, object>();
55 KeyValuePair<string, (PropertyInfo, Func<object>, Action<object>)>[] array =
_contextVars.ToArray();
56 foreach (KeyValuePair<
string, (PropertyInfo, Func<object>, Action<object>)> keyValuePair in array)
58 keyValuePair.Deconstruct(out var key, out var value);
59 (PropertyInfo, Func<object>, Action<object>) tuple = value;
61 var (propertyInfo, _, action) = tuple;
64 string key2 = propertyInfo.DeclaringType.FullName +
":" + text;
65 object valueOrDefault = data.GetValueOrDefault(key2);
66 action(valueOrDefault);
70 Debug.LogError(
$"#io failed to populate context var {text}\n{arg}");
78 Dictionary<string, object> dictionary =
new Dictionary<string, object>(StringComparer.Ordinal);
79 KeyValuePair<string, (PropertyInfo, Func<object>, Action<object>)>[] array =
_contextVars.ToArray();
80 foreach (KeyValuePair<
string, (PropertyInfo, Func<object>, Action<object>)> keyValuePair in array)
82 keyValuePair.Deconstruct(out var key, out var value);
83 (PropertyInfo, Func<object>, Action<object>) tuple = value;
85 var (propertyInfo, func, _) = tuple;
88 object value2 = func();
89 string key2 = propertyInfo.DeclaringType.FullName +
":" + text;
90 dictionary[key2] = value2;
94 Debug.LogError(
$"#io failed to save context var {text}\n{arg}");
98 context.Save(
"context_vars", dictionary);
static void SubscribeEvent(string eventId, Action< object > handler)
override void Register(PropertyInfo property)
static readonly Dictionary< string,(PropertyInfo property, Func< object > getter, Action< object > setter)> _contextVars
static void SaveGameIOProperty(GameIOContext context)
static Action< object > CreateSetterDelegate(MethodInfo method)
static void LoadGameIOProperty(GameIOContext context)
ElinGameIOPropertyAttribute(string chunkName)