-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathClassFieldHolder.cs
272 lines (264 loc) · 8.6 KB
/
ClassFieldHolder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
using HarmonyLib;
using NeosModLoader;
using FrooxEngine;
using FrooxEngine.UIX;
using BaseX;
using CodeX;
using System;
using System.Reflection;
using Newtonsoft.Json;
using UnityEngine;
using MinAttribute = UnityEngine.Rendering.PostProcessing.MinAttribute;
using UnityEngine.Rendering;
using UnityEngine.Rendering.PostProcessing;
namespace PhotonicFreedom;
public class ClassFieldHolder
{
public Type componentType { get; private set; }
public object? Instance;
public ClassFieldHolder(Type type, object? instance = null)
{
componentType = type;
Instance = instance;
List<FieldHolder> fields = new List<FieldHolder>();
foreach (FieldInfo field in componentType.GetFields())
{
if (Instance != null)
{
fields.Add(new FieldHolder(field, Instance));
}
else
{
fields.Add(new FieldHolder(field));
}
}
List<PropertyHolder> properties = new List<PropertyHolder>();
foreach (PropertyInfo property in componentType.GetProperties())
{
if (Instance != null)
{
properties.Add(new PropertyHolder(property, Instance));
}
else
{
properties.Add(new PropertyHolder(property));
}
}
Properties = properties.ToArray();
Fields = fields.ToArray();
}
public class FieldHolder
{
public FieldInfo FieldInfo { get; private set; }
public FieldHolder(FieldInfo fieldInfo)
{
FieldInfo = fieldInfo;
}
public FieldHolder(FieldInfo fieldInfo, object obj) : this(fieldInfo)
{
_defaultValue = fieldInfo.GetValue(obj);
Instance = obj;
}
public object? Instance;
public string Name => FieldInfo.Name;
public string PrettifiedName => Char.ToUpper(Name[0]) + Name.Substring(1);
public Type ReflectedType => FieldInfo.ReflectedType;
public Type BaseFieldType
{
get => FieldInfo.FieldType;
}
public Type RealType
{
get => IsParameter ? BaseFieldType.BaseType.GetGenericArguments()[0] : FieldInfo.FieldType;
}
public bool IsParameter
{
get => typeof(ParameterOverride).IsAssignableFrom(BaseFieldType);
}
public object[] Attributes
{
get => FieldInfo.GetCustomAttributes(false);
}
public bool HasAttribute<T>() where T : Attribute
{
return FieldInfo.GetCustomAttributes(typeof(T), false).Length > 0;
}
public T GetAttribute<T>() where T : Attribute
{
return (T)FieldInfo.GetCustomAttributes(typeof(T), false)[0];
}
public bool HasRangeAttribute
{
get => HasAttribute<UnityEngine.RangeAttribute>();
}
public bool HasTooltipAttribute
{
get => HasAttribute<TooltipAttribute>();
}
public bool HasMinAttribute
{
get => HasAttribute<MinAttribute>();
}
public bool HasMaxAttribute
{
get => HasAttribute<MaxAttribute>();
}
public float Min
{
get => HasRangeAttribute ? GetAttribute<UnityEngine.RangeAttribute>().min : HasMinAttribute ? GetAttribute<MinAttribute>().min : 0f;
}
public float Max
{
get => HasRangeAttribute ? GetAttribute<UnityEngine.RangeAttribute>().max : HasMaxAttribute ? GetAttribute<MaxAttribute>().max : 100f;
}
public string Tooltip
{
get => HasTooltipAttribute ? GetAttribute<TooltipAttribute>().tooltip : "";
}
public object DefaultValue
{
get
{
object field = _defaultValue ?? FieldInfo.GetValue(Activator.CreateInstance(FieldInfo.ReflectedType));
return IsParameter ? field.GetType().GetField("value").GetValue(field) : field ?? Activator.CreateInstance(RealType);
}
}
public void SetDefaultValue(object value)
{
_defaultValue = value;
}
public object GetValue(object instance)
{
object field = FieldInfo.GetValue(instance);
return IsParameter ? field.GetType().GetField("value").GetValue(field) : field;
}
public void SetRealValue(object instance, object value)
{
if (IsParameter)
{
object param = Activator.CreateInstance(BaseFieldType);
param.GetType().GetField("value").SetValue(param, value);
FieldInfo.SetValue(instance, param);
}
else
{
FieldInfo.SetValue(instance, value);
}
}
public object FieldValue
{
get => Instance != null ? GetValue(Instance) : DefaultValue;
set => SetRealValue(Instance ?? Activator.CreateInstance(FieldInfo.ReflectedType), value);
}
private object? _defaultValue;
}
public class PropertyHolder
{
public PropertyInfo PropertyInfo { get; private set; }
public PropertyHolder(PropertyInfo propertyInfo)
{
PropertyInfo = propertyInfo;
}
public PropertyHolder(PropertyInfo propertyInfo, object obj) : this(propertyInfo)
{
_defaultValue = propertyInfo.GetValue(obj);
Instance = obj;
}
public object? Instance;
public string Name => PropertyInfo.Name;
public string PrettifiedName => Char.ToUpper(Name[0]) + Name.Substring(1);
public Type ReflectedType => PropertyInfo.ReflectedType;
public Type BasePropertyType
{
get => PropertyInfo.PropertyType;
}
public object[] Attributes
{
get => PropertyInfo.GetCustomAttributes(false);
}
public bool HasAttribute<T>() where T : Attribute
{
return PropertyInfo.GetCustomAttributes(typeof(T), false).Length > 0;
}
public T GetAttribute<T>() where T : Attribute
{
return (T)PropertyInfo.GetCustomAttributes(typeof(T), false)[0];
}
public object DefaultValue
{
get
{
object field = _defaultValue ?? PropertyInfo.GetValue(Activator.CreateInstance(PropertyInfo.ReflectedType));
return field ?? Activator.CreateInstance(BasePropertyType);
}
}
public void SetDefaultValue(object value)
{
_defaultValue = value;
}
public object GetValue(object instance)
{
object field = PropertyInfo.GetValue(instance);
return field;
}
public void SetRealValue(object instance, object value)
{
if (!PropertyInfo.CanWrite)
return;
PropertyInfo.SetValue(instance, value);
}
public object PropertyValue
{
get => Instance != null ? GetValue(Instance) : DefaultValue;
set => SetRealValue(Instance ?? Activator.CreateInstance(PropertyInfo.ReflectedType), value);
}
private object? _defaultValue;
}
public FieldHolder[] Fields;
public PropertyHolder[] Properties;
}
public class ClassValueSerializer
{
public string componentType { get; private set; }
private Dictionary<string, object> defaultFieldValues = new Dictionary<string, object>();
public Dictionary<string, object> ObjectValues
{
get => defaultFieldValues;
}
public ClassValueSerializer(Type type, object o)
{
componentType = type.Name;
foreach (ClassFieldHolder.FieldHolder field in new ClassFieldHolder(type, o).Fields)
{
UniLog.Log(field.Name);
defaultFieldValues[field.Name] = field.DefaultValue;
}
}
}
public struct FieldValuePair<T> : IFieldValuePair
{
public string fieldName { get; private set; }
public T fieldValue { get; }
[JsonIgnore]
public object? BoxedValue => fieldValue;
public FieldValuePair(string fieldName, T fieldValue)
{
this.fieldName = fieldName;
this.fieldValue = fieldValue;
}
}
public interface IFieldValuePair
{
string fieldName { get; }
object? BoxedValue { get; }
}
public struct ClassSerializer
{
public string componentType;
public List<IFieldValuePair> defaultFieldValues;
public ClassSerializer(Type t)
{
componentType = t.FullName;
defaultFieldValues = new List<IFieldValuePair>();
}
}