com.wallstop-studios.unity-helpers 2.0.0-rc61 → 2.0.0-rc62

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,6 +3,7 @@
3
3
  using System;
4
4
  using System.Collections;
5
5
  using System.Collections.Generic;
6
+ using System.Linq;
6
7
  using System.Reflection;
7
8
  using System.Reflection.Emit;
8
9
  using System.Runtime.CompilerServices;
@@ -16,6 +17,30 @@
16
17
  private static readonly Dictionary<Type, Func<IList>> ListCreators = new();
17
18
  private static readonly Dictionary<Type, Func<int, IList>> ListWithCapacityCreators = new();
18
19
 
20
+ public static Dictionary<string, PropertyInfo> LoadStaticPropertiesForType<T>()
21
+ {
22
+ Type type = typeof(T);
23
+ return type.GetProperties(BindingFlags.Static | BindingFlags.Public)
24
+ .Where(property => property.PropertyType == type)
25
+ .ToDictionary(
26
+ property => property.Name,
27
+ property => property,
28
+ StringComparer.OrdinalIgnoreCase
29
+ );
30
+ }
31
+
32
+ public static Dictionary<string, FieldInfo> LoadStaticFieldsForType<T>()
33
+ {
34
+ Type type = typeof(T);
35
+ return type.GetFields(BindingFlags.Static | BindingFlags.Public)
36
+ .Where(field => field.FieldType == type)
37
+ .ToDictionary(
38
+ field => field.Name,
39
+ field => field,
40
+ StringComparer.OrdinalIgnoreCase
41
+ );
42
+ }
43
+
19
44
  [MethodImpl(MethodImplOptions.AggressiveInlining)]
20
45
  public static Array CreateArray(Type type, int length)
21
46
  {
@@ -43,11 +68,11 @@
43
68
 
44
69
  public static Func<object, object> GetFieldGetter(FieldInfo field)
45
70
  {
46
- #if WEB_GL
71
+ #if UNITY_WEBGL && !UNITY_EDITOR
47
72
  return field.GetValue;
48
73
  #else
49
74
  DynamicMethod dynamicMethod = new(
50
- $"Get{field.DeclaringType.Name}{field.Name}",
75
+ $"Get{field.DeclaringType.Name}_{field.Name}",
51
76
  typeof(object),
52
77
  new[] { typeof(object) },
53
78
  field.DeclaringType,
@@ -75,6 +100,43 @@
75
100
  #endif
76
101
  }
77
102
 
103
+ public static Func<object, object> GetPropertyGetter(PropertyInfo property)
104
+ {
105
+ #if UNITY_WEBGL && !UNITY_EDITOR
106
+ return property.GetValue;
107
+ #else
108
+ MethodInfo getMethod = property.GetGetMethod(true);
109
+
110
+ DynamicMethod dynamicMethod = new(
111
+ $"Get{property.DeclaringType.Name}_{property.Name}",
112
+ typeof(object),
113
+ new[] { typeof(object) },
114
+ property.DeclaringType,
115
+ true
116
+ );
117
+
118
+ ILGenerator il = dynamicMethod.GetILGenerator();
119
+ il.Emit(OpCodes.Ldarg_0);
120
+ il.Emit(
121
+ property.DeclaringType.IsValueType ? OpCodes.Unbox : OpCodes.Castclass,
122
+ property.DeclaringType
123
+ );
124
+ il.Emit(
125
+ property.DeclaringType.IsValueType ? OpCodes.Call : OpCodes.Callvirt,
126
+ getMethod
127
+ );
128
+
129
+ if (property.PropertyType.IsValueType)
130
+ {
131
+ il.Emit(OpCodes.Box, property.PropertyType);
132
+ }
133
+
134
+ il.Emit(OpCodes.Ret);
135
+
136
+ return (Func<object, object>)dynamicMethod.CreateDelegate(typeof(Func<object, object>));
137
+ #endif
138
+ }
139
+
78
140
  public static Func<object> GetStaticFieldGetter(FieldInfo field)
79
141
  {
80
142
  if (!field.IsStatic)
@@ -82,11 +144,11 @@
82
144
  throw new ArgumentException(nameof(field));
83
145
  }
84
146
 
85
- #if WEB_GL
147
+ #if UNITY_WEBGL && !UNITY_EDITOR
86
148
  return () => field.GetValue(null);
87
149
  #else
88
150
  DynamicMethod dynamicMethod = new(
89
- $"Get{field.DeclaringType.Name}{field.Name}",
151
+ $"Get{field.DeclaringType.Name}_{field.Name}",
90
152
  typeof(object),
91
153
  Type.EmptyTypes, // No parameters for static fields
92
154
  field.DeclaringType,
@@ -112,7 +174,7 @@
112
174
 
113
175
  public static Func<TInstance, TValue> GetFieldGetter<TInstance, TValue>(FieldInfo field)
114
176
  {
115
- #if WEB_GL
177
+ #if UNITY_WEBGL && !UNITY_EDITOR
116
178
  return Getter;
117
179
  TValue Getter(TInstance instance)
118
180
  {
@@ -120,7 +182,7 @@
120
182
  }
121
183
  #else
122
184
  DynamicMethod dynamicMethod = new(
123
- $"GetGeneric{field.DeclaringType.Name}{field.Name}",
185
+ $"GetGeneric{field.DeclaringType.Name}_{field.Name}",
124
186
  typeof(TValue),
125
187
  new[] { typeof(TInstance) },
126
188
  field.DeclaringType,
@@ -180,6 +242,58 @@
180
242
  #endif
181
243
  }
182
244
 
245
+ public static Func<TValue> GetStaticPropertyGetter<TValue>(PropertyInfo property)
246
+ {
247
+ MethodInfo getMethod = property.GetGetMethod(true);
248
+
249
+ #if UNITY_WEBGL && !UNITY_EDITOR
250
+
251
+ return Getter;
252
+ TValue Getter()
253
+ {
254
+ // Use null for instance, null for indexer args for static properties
255
+ return (TValue)property.GetValue(null, null);
256
+ }
257
+ #endif
258
+
259
+ DynamicMethod dynamicMethod = new(
260
+ $"GetStatic_{property.DeclaringType.Name}_{property.Name}",
261
+ typeof(TValue),
262
+ Type.EmptyTypes,
263
+ property.DeclaringType,
264
+ true
265
+ );
266
+
267
+ ILGenerator il = dynamicMethod.GetILGenerator();
268
+
269
+ il.Emit(OpCodes.Call, getMethod);
270
+
271
+ Type actualType = property.PropertyType;
272
+ Type targetType = typeof(TValue);
273
+
274
+ if (actualType != targetType)
275
+ {
276
+ if (actualType.IsValueType)
277
+ {
278
+ il.Emit(OpCodes.Box, actualType);
279
+ if (targetType != typeof(object))
280
+ {
281
+ il.Emit(OpCodes.Castclass, targetType);
282
+ }
283
+ }
284
+ else
285
+ {
286
+ il.Emit(
287
+ targetType.IsValueType ? OpCodes.Unbox_Any : OpCodes.Castclass,
288
+ targetType
289
+ );
290
+ }
291
+ }
292
+
293
+ il.Emit(OpCodes.Ret);
294
+ return (Func<TValue>)dynamicMethod.CreateDelegate(typeof(Func<TValue>));
295
+ }
296
+
183
297
  public static Func<TValue> GetStaticFieldGetter<TValue>(FieldInfo field)
184
298
  {
185
299
  if (!field.IsStatic)
@@ -187,7 +301,7 @@
187
301
  throw new ArgumentException(nameof(field));
188
302
  }
189
303
 
190
- #if WEB_GL
304
+ #if UNITY_WEBGL && !UNITY_EDITOR
191
305
  return Getter;
192
306
  TValue Getter()
193
307
  {
@@ -195,40 +309,40 @@
195
309
  }
196
310
  #else
197
311
  DynamicMethod dynamicMethod = new(
198
- $"GetGenericStatic{field.DeclaringType.Name}{field.Name}",
312
+ $"GetStatic_{field.DeclaringType.Name}_{field.Name}",
199
313
  typeof(TValue),
200
- Type.EmptyTypes, // no parameters needed for static fields
314
+ Type.EmptyTypes,
201
315
  field.DeclaringType,
202
316
  true
203
317
  );
204
318
 
205
319
  ILGenerator il = dynamicMethod.GetILGenerator();
206
320
 
207
- // Load the static field.
208
321
  il.Emit(OpCodes.Ldsfld, field);
209
322
 
210
- // Handle conversion from the field type to TValue.
211
- if (field.FieldType.IsValueType)
212
- {
213
- if (!typeof(TValue).IsValueType)
214
- {
215
- il.Emit(OpCodes.Box, field.FieldType);
216
- }
217
- }
218
- else
323
+ Type actualType = field.FieldType;
324
+ Type targetType = typeof(TValue);
325
+
326
+ if (actualType != targetType)
219
327
  {
220
- if (typeof(TValue).IsValueType)
328
+ if (actualType.IsValueType)
221
329
  {
222
- il.Emit(OpCodes.Unbox_Any, typeof(TValue));
330
+ il.Emit(OpCodes.Box, actualType);
331
+ if (targetType != typeof(object))
332
+ {
333
+ il.Emit(OpCodes.Castclass, targetType);
334
+ }
223
335
  }
224
- else if (typeof(TValue) != field.FieldType)
336
+ else
225
337
  {
226
- il.Emit(OpCodes.Castclass, typeof(TValue));
338
+ il.Emit(
339
+ targetType.IsValueType ? OpCodes.Unbox_Any : OpCodes.Castclass,
340
+ targetType
341
+ );
227
342
  }
228
343
  }
229
344
 
230
345
  il.Emit(OpCodes.Ret);
231
-
232
346
  return (Func<TValue>)dynamicMethod.CreateDelegate(typeof(Func<TValue>));
233
347
  #endif
234
348
  }
@@ -237,7 +351,7 @@
237
351
  FieldInfo field
238
352
  )
239
353
  {
240
- #if WEB_GL
354
+ #if UNITY_WEBGL && !UNITY_EDITOR
241
355
  return Setter;
242
356
  void Setter(ref TInstance instance, TValue newValue)
243
357
  {
@@ -250,7 +364,7 @@
250
364
  Type valueType = field.FieldType;
251
365
 
252
366
  DynamicMethod dynamicMethod = new(
253
- $"SetFieldGeneric{field.DeclaringType.Name}{field.Name}",
367
+ $"SetFieldGeneric{field.DeclaringType.Name}_{field.Name}",
254
368
  MethodAttributes.Public | MethodAttributes.Static,
255
369
  CallingConventions.Standard,
256
370
  typeof(void),
@@ -281,7 +395,7 @@
281
395
  {
282
396
  throw new ArgumentException(nameof(field));
283
397
  }
284
- #if WEB_GL
398
+ #if UNITY_WEBGL && !UNITY_EDITOR
285
399
  return Setter;
286
400
  void Setter(TValue newValue)
287
401
  {
@@ -289,7 +403,7 @@
289
403
  }
290
404
  #else
291
405
  DynamicMethod dynamicMethod = new(
292
- $"SetFieldGenericStatic{field.DeclaringType.Name}{field.Name}",
406
+ $"SetFieldGenericStatic{field.DeclaringType.Name}_{field.Name}",
293
407
  typeof(void),
294
408
  new[] { typeof(TValue) },
295
409
  field.Module,
@@ -307,11 +421,11 @@
307
421
 
308
422
  public static Action<object, object> GetFieldSetter(FieldInfo field)
309
423
  {
310
- #if WEB_GL
424
+ #if UNITY_WEBGL && !UNITY_EDITOR
311
425
  return field.SetValue;
312
426
  #else
313
427
  DynamicMethod dynamicMethod = new(
314
- $"SetField{field.DeclaringType.Name}{field.Name}",
428
+ $"SetField{field.DeclaringType.Name}_{field.Name}",
315
429
  null,
316
430
  new[] { typeof(object), typeof(object) },
317
431
  field.DeclaringType.Module,
@@ -344,11 +458,11 @@
344
458
  {
345
459
  throw new ArgumentException(nameof(field));
346
460
  }
347
- #if WEB_GL
461
+ #if UNITY_WEBGL && !UNITY_EDITOR
348
462
  return value => field.SetValue(null, value);
349
463
  #else
350
464
  DynamicMethod dynamicMethod = new(
351
- $"SetFieldStatic{field.DeclaringType.Name}{field.Name}",
465
+ $"SetFieldStatic{field.DeclaringType.Name}_{field.Name}",
352
466
  null,
353
467
  new[] { typeof(object) },
354
468
  field.DeclaringType.Module,
@@ -374,7 +488,7 @@
374
488
 
375
489
  public static Func<int, Array> GetArrayCreator(Type elementType)
376
490
  {
377
- #if WEB_GL
491
+ #if UNITY_WEBGL && !UNITY_EDITOR
378
492
  return size => Array.CreateInstance(elementType, size);
379
493
  #else
380
494
  DynamicMethod dynamicMethod = new(
@@ -395,7 +509,7 @@
395
509
  public static Func<IList> GetListCreator(Type elementType)
396
510
  {
397
511
  Type listType = typeof(List<>).MakeGenericType(elementType);
398
- #if WEB_GL
512
+ #if UNITY_WEBGL && !UNITY_EDITOR
399
513
  return () => (IList)Activator.CreateInstance(listType);
400
514
  #else
401
515
  DynamicMethod dynamicMethod = new(
@@ -423,7 +537,7 @@
423
537
  public static Func<int, IList> GetListWithCapacityCreator(Type elementType)
424
538
  {
425
539
  Type listType = typeof(List<>).MakeGenericType(elementType);
426
- #if WEB_GL
540
+ #if UNITY_WEBGL && !UNITY_EDITOR
427
541
  return _ => (IList)Activator.CreateInstance(listType);
428
542
  #else
429
543
  DynamicMethod dynamicMethod = new(
@@ -17,14 +17,14 @@
17
17
  string assetPath = AssetDatabase.GetAssetPath(texture);
18
18
  if (string.IsNullOrEmpty(assetPath))
19
19
  {
20
- texture.LogError("Failed to get asset path.");
20
+ texture.LogError($"Failed to get asset path.");
21
21
  return;
22
22
  }
23
23
 
24
24
  TextureImporter tImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter;
25
25
  if (tImporter == null)
26
26
  {
27
- texture.LogError("Failed to get texture importer.");
27
+ texture.LogError($"Failed to get texture importer.");
28
28
  return;
29
29
  }
30
30
 
@@ -61,20 +61,18 @@
61
61
  {
62
62
  if (numSegments <= 2)
63
63
  {
64
- this.LogWarn("Invalid number of segments {0}.", numSegments);
64
+ this.LogWarn($"Invalid number of segments {numSegments}.");
65
65
  }
66
66
 
67
67
  if (updateRateSeconds <= 0)
68
68
  {
69
- this.LogWarn("Invalid update rate {0}.", updateRateSeconds);
69
+ this.LogWarn($"Invalid update rate {updateRateSeconds}.");
70
70
  }
71
71
 
72
72
  if (maxLineWidth < minLineWidth)
73
73
  {
74
74
  this.LogWarn(
75
- "MaxLineWidth {0} smaller than MinLineWidth {1}.",
76
- maxLineWidth,
77
- minLineWidth
75
+ $"MaxLineWidth {maxLineWidth} smaller than MinLineWidth {minLineWidth}."
78
76
  );
79
77
  }
80
78
  }