Skip to content

Commit

Permalink
Refactor method targeting. Should cover any exotic dyn var enum inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
Banane9 committed Jun 27, 2022
1 parent 1998b1e commit 8b8fcc0
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 26 deletions.
7 changes: 5 additions & 2 deletions LogixVisualCustomizer/ButtonInputPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ private static void OnGenerateVisualPostfix(LogixNode __instance, Slot root)
[HarmonyTargetMethods]
private static IEnumerable<MethodBase> TargetMethods()
{
return new[] { typeof(BoolInput), typeof(Bool2Input), typeof(Bool3Input), typeof(Bool4Input), impulseInputType }
.Select(t => t.GetMethod("OnGenerateVisual", AccessTools.allDeclared));
return LogixVisualCustomizer.GenerateMethodTargets(
"OnGenerateVisual",
typeof(BoolInput), typeof(Bool2Input),
typeof(Bool3Input), typeof(Bool4Input),
impulseInputType);
}
}
}
8 changes: 4 additions & 4 deletions LogixVisualCustomizer/DisplayPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ private static void OnGenerateVisualPostfix(LogixNode __instance, Slot root)
[HarmonyTargetMethods]
private static IEnumerable<MethodBase> TargetMethods()
{
return AccessTools.GetTypesFromAssembly(typeof(Display_Dummy).Assembly)
.Where(t => t.Namespace == "FrooxEngine.LogiX.Display")
.Select(t => t.GetMethod("OnGenerateVisual", AccessTools.allDeclared))
.Where(m => m != null); // Dummy doesn't have one
return LogixVisualCustomizer.GenerateMethodTargets(
"OnGenerateVisual",
AccessTools.GetTypesFromAssembly(typeof(Display_Dummy).Assembly)
.Where(t => t.Namespace == "FrooxEngine.LogiX.Display"));
}
}
}
2 changes: 1 addition & 1 deletion LogixVisualCustomizer/EnumInputPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ internal static class EnumInputPatch
public static IEnumerable<MethodBase> TargetMethods()
{
return LogixVisualCustomizer.GenerateGenericMethodTargets(
AccessTools.GetTypesFromAssembly(typeof(EnumInput<>).Assembly).Concat(AccessTools.GetTypesFromAssembly(typeof(float4).Assembly)).Where(type => type.IsEnum && !type.IsNested),
LogixVisualCustomizer.NeosEnumTypes,
"OnGenerateVisual",
typeof(EnumInput<>));
}
Expand Down
33 changes: 23 additions & 10 deletions LogixVisualCustomizer/LogixVisualCustomizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,19 +151,18 @@ static LogixVisualCustomizer()
{
var traverse = Traverse.Create(typeof(GenericTypes));

NeosEnumTypes = AccessTools.GetTypesFromAssembly(typeof(EnumInput<>).Assembly)
.Concat(AccessTools.GetTypesFromAssembly(typeof(float4).Assembly))
.Where(type => type.IsEnum && !type.IsNested)
.ToArray();

NeosPrimitiveTypes = traverse.Field<Type[]>("neosPrimitives").Value
.Where(type => type.Name != "String")
.AddItem(typeof(dummy))
.AddItem(typeof(object))
.ToArray();

NeosPrimitiveAndEnumTypes = traverse.Field<Type[]>("neosPrimitivesAndEnums").Value
.Where(type => type.Name != "String")
.AddItem(typeof(dummy))
.AddItem(typeof(object))
.ToArray();

NeosEnumTypes = traverse.Field<Type[]>("commonEnums").Value;
NeosPrimitiveAndEnumTypes = NeosPrimitiveTypes.Concat(NeosEnumTypes).ToArray();

InputBackgroundColorKey.SetSharedDefault(color.White);
InputBorderColorKey.SetSharedDefault(color.White.SetA(0));
Expand All @@ -189,9 +188,23 @@ public static bool ButtonFilter(Button button)

public static IEnumerable<MethodBase> GenerateGenericMethodTargets(IEnumerable<Type> genericTypes, string methodName, params Type[] baseTypes)
{
return genericTypes
.SelectMany(type => baseTypes.Select(baseType => baseType.IsGenericTypeDefinition ? baseType.MakeGenericType(type) : baseType))
.Select(type => type.GetMethod(methodName, AccessTools.all));
return GenerateGenericMethodTargets(genericTypes, methodName, (IEnumerable<Type>)baseTypes);
}

public static IEnumerable<MethodBase> GenerateGenericMethodTargets(IEnumerable<Type> genericTypes, string methodName, IEnumerable<Type> baseTypes)
{
return GenerateMethodTargets(methodName,
genericTypes.SelectMany(type => baseTypes.Select(baseType => baseType.MakeGenericType(type))));
}

public static IEnumerable<MethodBase> GenerateMethodTargets(string methodName, params Type[] baseTypes)
{
return GenerateMethodTargets(methodName, (IEnumerable<Type>)baseTypes);
}

public static IEnumerable<MethodBase> GenerateMethodTargets(string methodName, IEnumerable<Type> baseTypes)
{
return baseTypes.Select(type => type.GetMethod(methodName, AccessTools.all)).Where(m => m != null);
}

public override void OnEngineInit()
Expand Down
23 changes: 14 additions & 9 deletions LogixVisualCustomizer/ModifiableInputNodePatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,20 @@ internal static class ModifiableInputNodePatches
public static IEnumerable<MethodBase> TargetMethods()
{
return LogixVisualCustomizer.GenerateGenericMethodTargets(
LogixVisualCustomizer.NeosPrimitiveTypes,
"OnGenerateVisual",
typeof(DualInputOperator<>),
typeof(IndexOfFirstMatch<>),
typeof(Multiplexer<>),
typeof(Demultiplexer<>),
typeof(ImpulseMultiplexer),
typeof(ImpulseDemultiplexer),
typeof(PickRandom<>));
LogixVisualCustomizer.NeosPrimitiveTypes,
"OnGenerateVisual",
typeof(DualInputOperator<>))
.Concat(LogixVisualCustomizer.GenerateGenericMethodTargets(
LogixVisualCustomizer.NeosPrimitiveAndEnumTypes,
"OnGenerateVisual",
typeof(IndexOfFirstMatch<>),
typeof(Multiplexer<>),
typeof(Demultiplexer<>),
typeof(PickRandom<>)))
.Concat(LogixVisualCustomizer.GenerateMethodTargets(
"OnGenerateVisual",
typeof(ImpulseMultiplexer),
typeof(ImpulseDemultiplexer)));
}

[HarmonyPostfix]
Expand Down

0 comments on commit 8b8fcc0

Please sign in to comment.