From 11daacd37bdc68a9ebeb1125f5917379a6b98312 Mon Sep 17 00:00:00 2001 From: frozenreflex Date: Tue, 5 Sep 2023 17:09:53 -0500 Subject: [PATCH] Cleanup --- NEOSPlus/Quantity/QuantityInjector.cs | 83 ++++++++++----------------- 1 file changed, 29 insertions(+), 54 deletions(-) diff --git a/NEOSPlus/Quantity/QuantityInjector.cs b/NEOSPlus/Quantity/QuantityInjector.cs index 967011c..41fbe4b 100644 --- a/NEOSPlus/Quantity/QuantityInjector.cs +++ b/NEOSPlus/Quantity/QuantityInjector.cs @@ -16,11 +16,11 @@ internal static void Inject() { // Get the 'quantities' field using reflection from 'FrooxEngine.GenericTypes' var quantities = - typeof(FrooxEngine.GenericTypes).GetField("quantities", BindingFlags.Static | BindingFlags.NonPublic); + typeof(GenericTypes).GetField("quantities", BindingFlags.Static | BindingFlags.NonPublic); // Get all types in the 'NEOSPlus.Quantity' namespace that are value types var quantityTypes = typeof(QuantityInjector).Assembly.GetTypes() - .Where(type => type.Namespace == "NEOSPlus.Quantity" && type.IsValueType); + .Where(type => type.Namespace == "NEOSPlus.Quantity" && type.IsValueType).ToArray(); // Append all quantity types to the existing array of types var newArray = (quantities.GetValue(null) as Type[]).Concat(quantityTypes).ToArray(); @@ -29,10 +29,7 @@ internal static void Inject() quantities.SetValue(null, newArray); // Log the injected types - foreach (var type in quantityTypes) - { - UniLog.Log($"Injected quantity type: {type.FullName}"); - } + foreach (var type in quantityTypes) UniLog.Log($"Injected quantity type: {type.FullName}"); // Update the quantity cache UpdateQuantityCache(); @@ -43,96 +40,74 @@ internal static void UpdateQuantityCache() { // Get the 'unitCache' and 'unitNameCache' fields using reflection from 'QuantityX.QuantityX' var unitCache = - typeof(QuantityX.QuantityX).GetField("unitCache", BindingFlags.Static | BindingFlags.NonPublic) + typeof(QuantityX.QuantityX).GetField("unitCache", BindingFlags.Static | BindingFlags.NonPublic)! .GetValue(null) as Dictionary>; var unitNameCache = - typeof(QuantityX.QuantityX).GetField("unitNameCache", BindingFlags.Static | BindingFlags.NonPublic) + typeof(QuantityX.QuantityX).GetField("unitNameCache", BindingFlags.Static | BindingFlags.NonPublic)! .GetValue(null) as Dictionary>; // Get all types in the assembly containing the 'QuantityInjector' class - Type[] types = typeof(QuantityInjector).Assembly.GetTypes(); + var types = typeof(QuantityInjector).Assembly.GetTypes(); - foreach (Type type in types) + foreach (var type in types) { // Check if the type is assignable to 'IQuantity' and is a value type if (!typeof(IQuantity).IsAssignableFrom(type) || !type.IsValueType) - { continue; - } // Create an instance of the quantity type - IQuantity quantity = (IQuantity)Activator.CreateInstance(type); + var quantity = (IQuantity)Activator.CreateInstance(type); // Create a list to store associated units - List unitList = new List(); - unitCache.Add(type, unitList); - - bool isQuantitySI = false; + var unitList = new List(); + unitCache!.Add(type, unitList); // Get interfaces implemented by the quantity type - Type[] interfaces = type.GetInterfaces(); + var interfaces = type.GetInterfaces(); // Check if the quantity type implements a generic interface of 'IQuantitySI' - foreach (Type interfaceType in interfaces) - { - if (interfaceType.IsGenericType && interfaceType.GetGenericTypeDefinition() == typeof(IQuantitySI<>)) - { - isQuantitySI = true; - break; - } - } + var isQuantitySi = interfaces.Any(interfaceType => interfaceType.IsGenericType && interfaceType.GetGenericTypeDefinition() == typeof(IQuantitySI<>)); - BindingFlags bindingAttr = BindingFlags.Static | BindingFlags.Public; + const BindingFlags bindingAttr = BindingFlags.Static | BindingFlags.Public; // Create a list to store fields - List fieldLists = new List { type.GetFields(bindingAttr) }; + var fieldLists = new List { type.GetFields(bindingAttr) }; - if (isQuantitySI) + if (isQuantitySi) { // If it's a quantitySI, add common SI units - IQuantitySI quantitySI = (IQuantitySI)quantity; - IUnit[] commonSIUnits = quantitySI.GetCommonSIUnits(); + var quantitySi = (IQuantitySI)quantity; + var commonSiUnits = quantitySi.GetCommonSIUnits(); - foreach (IUnit unit in commonSIUnits) + foreach (var unit in commonSiUnits) { UnitGroup.Common.RegisterUnit(unit); UnitGroup.CommonMetric.RegisterUnit(unit); } // Exclude specific SI units - commonSIUnits = quantitySI.GetExludedSIUnits(); - foreach (IUnit unit2 in commonSIUnits) - { - UnitGroup.Metric.RemoveUnit(unit2); - } + commonSiUnits = quantitySi.GetExludedSIUnits(); + foreach (var unit2 in commonSiUnits) UnitGroup.Metric.RemoveUnit(unit2); - Type siType = typeof(SI<>).MakeGenericType(type); + var siType = typeof(SI<>).MakeGenericType(type); fieldLists.Add(siType.GetFields(bindingAttr)); } - foreach (FieldInfo[] fields in fieldLists) - { - foreach (FieldInfo fieldInfo in fields) - { - if (typeof(IUnit).IsAssignableFrom(fieldInfo.FieldType)) - { - // Get the unit and add it to the unit list - var unit = (IUnit)fieldInfo.GetValue(null); - unitList.Add(unit); - } - } - } + unitList.AddRange(from fields in fieldLists + from fieldInfo in fields + where typeof(IUnit).IsAssignableFrom(fieldInfo.FieldType) + select (IUnit) fieldInfo.GetValue(null)); unitList.Sort(); // Create a dictionary to store unit names - Dictionary unitNameDictionary = new Dictionary(); - unitNameCache.Add(type, unitNameDictionary); + var unitNameDictionary = new Dictionary(); + unitNameCache!.Add(type, unitNameDictionary); - foreach (IUnit unitItem in unitList) + foreach (var unitItem in unitList) { - foreach (string unitName in unitItem.GetUnitNames()) + foreach (var unitName in unitItem.GetUnitNames()) { unitNameDictionary.Add(unitName.Trim(), unitItem); }