|
| 1 | +using System.Globalization; |
| 2 | +using System.Numerics; |
| 3 | +using System.Reflection; |
| 4 | + |
| 5 | +namespace Maple2.File.Ingest.Utils; |
| 6 | + |
| 7 | +public static class GenericHelper { |
| 8 | + public static void SetValue(PropertyInfo prop, object? obj, object? value) { |
| 9 | + if (obj == null && value == null || value == null) return; |
| 10 | + HandleNonIConvertibleTypes(prop, ref value); |
| 11 | + if (value == null) return; |
| 12 | + if (typeof(IConvertible).IsAssignableFrom(prop.PropertyType)) { |
| 13 | + TryParseObject(prop.PropertyType, value, out object? result); |
| 14 | + prop.SetValue(obj, result); |
| 15 | + return; |
| 16 | + } |
| 17 | + prop.SetValue(obj, value); |
| 18 | + } |
| 19 | + |
| 20 | + private static object? HandleNonIConvertibleTypes(PropertyInfo prop, ref object? value) { |
| 21 | + if (value == null) return value; |
| 22 | + // Handle TimeSpan type |
| 23 | + if (prop.PropertyType == typeof(TimeSpan)) { |
| 24 | + TimeSpan.TryParse((string) value, CultureInfo.InvariantCulture, out TimeSpan val); |
| 25 | + value = val; |
| 26 | + } |
| 27 | + // Handle array types (int[], short[], etc.) |
| 28 | + if (prop.PropertyType.IsArray) { |
| 29 | + var elementType = prop.PropertyType.GetElementType(); |
| 30 | + if (elementType == null) return value; |
| 31 | + string[] segments = ((string) value).Split(','); |
| 32 | + Array destinationArray = Array.CreateInstance(elementType, segments.Length); |
| 33 | + for (int i = 0; i < segments.Length; i++) { |
| 34 | + if (TryParseObject(elementType, segments[i].Trim(), out object? parseResult)) { |
| 35 | + destinationArray.SetValue(parseResult ?? default, i); |
| 36 | + }else { |
| 37 | + destinationArray.SetValue(elementType.IsValueType ? Activator.CreateInstance(elementType) : null, i); |
| 38 | + } |
| 39 | + } |
| 40 | + value = destinationArray; |
| 41 | + } |
| 42 | + // Handle Vector3 type |
| 43 | + if (prop.PropertyType == typeof(Vector3)) { |
| 44 | + string[] parts = ((string) value).Split(','); |
| 45 | + bool parseXSuccess = float.TryParse(parts[0], CultureInfo.InvariantCulture, out float x); |
| 46 | + bool parseYSuccess = float.TryParse(parts[1], CultureInfo.InvariantCulture, out float y); |
| 47 | + bool parseZSuccess = float.TryParse(parts[2], CultureInfo.InvariantCulture, out float z); |
| 48 | + if (parts.Length != 3 || parseXSuccess && parseYSuccess && parseZSuccess) { |
| 49 | + value = Vector3.Zero; |
| 50 | + } else { |
| 51 | + value = new Vector3(x, y, z); |
| 52 | + } |
| 53 | + } |
| 54 | + return value; |
| 55 | + } |
| 56 | + |
| 57 | + private static bool TryParseObject(Type? elementType, object? input, out object? result) { |
| 58 | + if (elementType == null || input == null) { |
| 59 | + result = null; |
| 60 | + return false; |
| 61 | + } |
| 62 | + |
| 63 | + string inputString = Convert.ToString(input, CultureInfo.InvariantCulture)!; |
| 64 | + |
| 65 | + // No TryParse method exists for a string, use the result directly. |
| 66 | + if (elementType == typeof(string)) { |
| 67 | + result = inputString; |
| 68 | + return true; |
| 69 | + } |
| 70 | + |
| 71 | + Type[] argTypes = { |
| 72 | + typeof(string), |
| 73 | + typeof(IFormatProvider), |
| 74 | + elementType.MakeByRefType() |
| 75 | + }; |
| 76 | + |
| 77 | + var method = elementType.GetMethod("TryParse", |
| 78 | + BindingFlags.Public | BindingFlags.Static, |
| 79 | + null, argTypes, null); |
| 80 | + if (method != null) { |
| 81 | + object[] args = [inputString, CultureInfo.InvariantCulture, null!]; |
| 82 | + bool success = (bool) method.Invoke(null, args)!; |
| 83 | + result = args[2]; |
| 84 | + return success; |
| 85 | + } |
| 86 | + |
| 87 | + // Fallback without CultureInfo provided, in case the type does not have a CultureInfo overload. |
| 88 | + Type[] simpleArgs = { typeof(string), elementType.MakeByRefType() }; |
| 89 | + method = elementType.GetMethod("TryParse", |
| 90 | + BindingFlags.Public | BindingFlags.Static, |
| 91 | + null, simpleArgs, null); |
| 92 | + if (method != null) { |
| 93 | + object[] args = { inputString, null! }; |
| 94 | + bool success = (bool) method.Invoke(null, args)!; |
| 95 | + result = args[1]; |
| 96 | + return success; |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | + result = null; |
| 101 | + return false; |
| 102 | + } |
| 103 | +} |
0 commit comments