Elin Decompiled Documentation EA 23.343.5 Nightly
Loading...
Searching...
No Matches
ElinGameIOPropertyAttribute Class Reference
Inheritance diagram for ElinGameIOPropertyAttribute:
ElinGameIOEventAttribute ElinEventBaseAttribute

Public Member Functions

 ElinGameIOPropertyAttribute (string chunkName)
 
override void Register (PropertyInfo property)
 
- Public Member Functions inherited from ElinEventBaseAttribute
virtual void Register (MethodInfo method)
 
virtual void Register (PropertyInfo property)
 

Properties

string ChunkName [get]
 

Static Private Member Functions

static Func< object > CreateGetterDelegate (MethodInfo method)
 
static Action< object > CreateSetterDelegate (MethodInfo method)
 
static void LoadGameIOProperty (GameIOContext context)
 
static void SaveGameIOProperty (GameIOContext context)
 

Static Private Attributes

static bool _registered
 
static readonly Dictionary< string,(PropertyInfo property, Func< object > getter, Action< object > setter)> _contextVars = new Dictionary<string, (PropertyInfo, Func<object>, Action<object>)>()
 

Detailed Description

Definition at line 10 of file ElinGameIOPropertyAttribute.cs.

Constructor & Destructor Documentation

◆ ElinGameIOPropertyAttribute()

ElinGameIOPropertyAttribute.ElinGameIOPropertyAttribute ( string  chunkName)
inline

Definition at line 18 of file ElinGameIOPropertyAttribute.cs.

19 {
20 ChunkName = chunkName;
21 }

References ChunkName.

Member Function Documentation

◆ CreateGetterDelegate()

static Func< object > ElinGameIOPropertyAttribute.CreateGetterDelegate ( MethodInfo  method)
inlinestaticprivate

Definition at line 42 of file ElinGameIOPropertyAttribute.cs.

43 {
44 return Expression.Lambda<Func<object>>(Expression.Convert(Expression.Call(method), typeof(object)), Array.Empty<ParameterExpression>()).Compile();
45 }

Referenced by Register().

◆ CreateSetterDelegate()

static Action< object > ElinGameIOPropertyAttribute.CreateSetterDelegate ( MethodInfo  method)
inlinestaticprivate

Definition at line 47 of file ElinGameIOPropertyAttribute.cs.

48 {
49 Type parameterType = method.GetParameters()[0].ParameterType;
50 ParameterExpression parameterExpression = Expression.Parameter(typeof(object), "obj");
51 UnaryExpression arg = Expression.Convert(parameterExpression, parameterType);
52 return Expression.Lambda<Action<object>>(Expression.Call(method, arg), new ParameterExpression[1] { parameterExpression }).Compile();
53 }

Referenced by Register().

◆ LoadGameIOProperty()

static void ElinGameIOPropertyAttribute.LoadGameIOProperty ( GameIOContext  context)
inlinestaticprivate

Definition at line 55 of file ElinGameIOPropertyAttribute.cs.

56 {
57 if (!context.Load<Dictionary<string, object>>("context_vars", out var data))
58 {
59 data = new Dictionary<string, object>();
60 }
61 KeyValuePair<string, (PropertyInfo, Func<object>, Action<object>)>[] array = _contextVars.ToArray();
62 foreach (KeyValuePair<string, (PropertyInfo, Func<object>, Action<object>)> keyValuePair in array)
63 {
64 keyValuePair.Deconstruct(out var key, out var value);
65 (PropertyInfo, Func<object>, Action<object>) tuple = value;
66 string text = key;
67 var (propertyInfo, _, action) = tuple;
68 try
69 {
70 string key2 = propertyInfo.DeclaringType.FullName + ":" + text;
71 object valueOrDefault = data.GetValueOrDefault(key2);
72 action(CoerceValue(valueOrDefault, propertyInfo.PropertyType));
73 }
74 catch (Exception arg)
75 {
76 Debug.LogError($"#io failed to populate context var {text}\n{arg}");
77 }
78 }
79 static object CoerceValue(object obj, Type type)
80 {
81 if (obj is JToken jToken)
82 {
83 return jToken.ToObject(type);
84 }
85 if (obj == null)
86 {
87 return type.IsValueType ? Activator.CreateInstance(type) : null;
88 }
89 object result;
90 return DynamicReflection.TryCoerce(obj, type, out result) ? result : obj;
91 }
92 }
static readonly Dictionary< string,(PropertyInfo property, Func< object > getter, Action< object > setter)> _contextVars

References $, _contextVars, and Debug.

Referenced by Register().

◆ Register()

override void ElinGameIOPropertyAttribute.Register ( PropertyInfo  property)
inlinevirtual

Reimplemented from ElinEventBaseAttribute.

Definition at line 23 of file ElinGameIOPropertyAttribute.cs.

24 {
25 if (!_registered)
26 {
27 BaseModManager.SubscribeEvent<GameIOContext>("elin.game.post_load", LoadGameIOProperty);
28 BaseModManager.SubscribeEvent<GameIOContext>("elin.game.post_save", SaveGameIOProperty);
29 _registered = true;
30 }
31 MethodInfo getMethod = property.GetMethod;
32 MethodInfo setMethod = property.SetMethod;
33 if (!(getMethod == null) && !(setMethod == null) && getMethod.IsStatic && setMethod.IsStatic)
34 {
35 Func<object> item = CreateGetterDelegate(getMethod);
36 Action<object> item2 = CreateSetterDelegate(setMethod);
37 Type declaringType = property.DeclaringType;
38 _contextVars[declaringType.FullName + ":" + ChunkName] = (property, item, item2);
39 }
40 }
static void SubscribeEvent(string eventId, Action< object > handler)
static Func< object > CreateGetterDelegate(MethodInfo method)
static void SaveGameIOProperty(GameIOContext context)
static Action< object > CreateSetterDelegate(MethodInfo method)
static void LoadGameIOProperty(GameIOContext context)

References _contextVars, _registered, ChunkName, CreateGetterDelegate(), CreateSetterDelegate(), item, LoadGameIOProperty(), SaveGameIOProperty(), and BaseModManager.SubscribeEvent().

◆ SaveGameIOProperty()

static void ElinGameIOPropertyAttribute.SaveGameIOProperty ( GameIOContext  context)
inlinestaticprivate

Definition at line 94 of file ElinGameIOPropertyAttribute.cs.

95 {
96 Dictionary<string, object> dictionary = new Dictionary<string, object>(StringComparer.Ordinal);
97 KeyValuePair<string, (PropertyInfo, Func<object>, Action<object>)>[] array = _contextVars.ToArray();
98 foreach (KeyValuePair<string, (PropertyInfo, Func<object>, Action<object>)> keyValuePair in array)
99 {
100 keyValuePair.Deconstruct(out var key, out var value);
101 (PropertyInfo, Func<object>, Action<object>) tuple = value;
102 string text = key;
103 var (propertyInfo, func, _) = tuple;
104 try
105 {
106 object value2 = func();
107 string key2 = propertyInfo.DeclaringType.FullName + ":" + text;
108 dictionary[key2] = value2;
109 }
110 catch (Exception arg)
111 {
112 Debug.LogError($"#io failed to save context var {text}\n{arg}");
113 }
114 }
115 context.Save("context_vars", dictionary);
116 }

References $, _contextVars, and Debug.

Referenced by Register().

Member Data Documentation

◆ _contextVars

readonly Dictionary<string, (PropertyInfo property, Func<object> getter, Action<object> setter)> ElinGameIOPropertyAttribute._contextVars = new Dictionary<string, (PropertyInfo, Func<object>, Action<object>)>()
staticprivate

Definition at line 14 of file ElinGameIOPropertyAttribute.cs.

Referenced by LoadGameIOProperty(), Register(), and SaveGameIOProperty().

◆ _registered

bool ElinGameIOPropertyAttribute._registered
staticprivate

Definition at line 12 of file ElinGameIOPropertyAttribute.cs.

Referenced by Register().

Property Documentation

◆ ChunkName

string ElinGameIOPropertyAttribute.ChunkName
get

Definition at line 16 of file ElinGameIOPropertyAttribute.cs.

16{ get; }

Referenced by ElinGameIOPropertyAttribute(), and Register().


The documentation for this class was generated from the following file: