Skip to content
This repository has been archived by the owner on Nov 9, 2023. It is now read-only.

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Frozenreflex committed Sep 5, 2023
1 parent a58e6fb commit 11daacd
Showing 1 changed file with 29 additions and 54 deletions.
83 changes: 29 additions & 54 deletions NEOSPlus/Quantity/QuantityInjector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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<Type, List<IUnit>>;

var unitNameCache =
typeof(QuantityX.QuantityX).GetField("unitNameCache", BindingFlags.Static | BindingFlags.NonPublic)
typeof(QuantityX.QuantityX).GetField("unitNameCache", BindingFlags.Static | BindingFlags.NonPublic)!
.GetValue(null) as Dictionary<Type, Dictionary<string, IUnit>>;

// 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<IUnit> unitList = new List<IUnit>();
unitCache.Add(type, unitList);

bool isQuantitySI = false;
var unitList = new List<IUnit>();
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<FieldInfo[]> fieldLists = new List<FieldInfo[]> { type.GetFields(bindingAttr) };
var fieldLists = new List<FieldInfo[]> { 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<string, IUnit> unitNameDictionary = new Dictionary<string, IUnit>();
unitNameCache.Add(type, unitNameDictionary);
var unitNameDictionary = new Dictionary<string, IUnit>();
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);
}
Expand Down

0 comments on commit 11daacd

Please sign in to comment.