Skip to content
This repository was archived by the owner on Feb 28, 2024. It is now read-only.

Commit 6c98b01

Browse files
committed
Hide mod types from two more type-searching methods
It turns out #67 did not cover all cases. In this PR, `WorkerManager.IsValidGenericType` and `WorkerManager.GetType` are now both handled. I've made the code a little more modular as well now that three distinct methods are being hooked in very similar ways.
1 parent 390afce commit 6c98b01

File tree

2 files changed

+57
-12
lines changed

2 files changed

+57
-12
lines changed

NeosModLoader/AssemblyHider.cs

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using BaseX;
2+
using FrooxEngine;
23
using HarmonyLib;
34
using System;
45
using System.Collections.Generic;
@@ -23,9 +24,21 @@ internal static void PatchNeos(Harmony harmony, HashSet<Assembly> initialAssembl
2324
{
2425
neosAssemblies = GetNeosAssemblies(initialAssemblies);
2526
modAssemblies = GetModAssemblies();
26-
MethodInfo target = AccessTools.DeclaredMethod(typeof(TypeHelper), nameof(TypeHelper.FindType));
27-
MethodInfo patch = AccessTools.DeclaredMethod(typeof(AssemblyHider), nameof(FindTypePostfix));
28-
harmony.Patch(target, postfix: new HarmonyMethod(patch));
27+
28+
// TypeHelper.FindType explicitly does a type search
29+
MethodInfo findTypeTarget = AccessTools.DeclaredMethod(typeof(TypeHelper), nameof(TypeHelper.FindType));
30+
MethodInfo findTypePatch = AccessTools.DeclaredMethod(typeof(AssemblyHider), nameof(FindTypePostfix));
31+
harmony.Patch(findTypeTarget, postfix: new HarmonyMethod(findTypePatch));
32+
33+
// WorkerManager.IsValidGenericType checks a type for validity, and if it returns `true` it reveals that the type exists
34+
MethodInfo isValidGenericTypeTarget = AccessTools.DeclaredMethod(typeof(WorkerManager), nameof(WorkerManager.IsValidGenericType));
35+
MethodInfo isValidGenericTypePatch = AccessTools.DeclaredMethod(typeof(AssemblyHider), nameof(IsValidTypePostfix));
36+
harmony.Patch(isValidGenericTypeTarget, postfix: new HarmonyMethod(isValidGenericTypePatch));
37+
38+
// WorkerManager.GetType uses FindType, but upon failure fails back to doing a (strangely) exhausitive reflection-based search for the type
39+
MethodInfo getTypeTarget = AccessTools.DeclaredMethod(typeof(WorkerManager), nameof(WorkerManager.GetType));
40+
MethodInfo getTypePatch = AccessTools.DeclaredMethod(typeof(AssemblyHider), nameof(FindTypePostfix));
41+
harmony.Patch(getTypeTarget, postfix: new HarmonyMethod(getTypePatch));
2942
}
3043
}
3144

@@ -51,24 +64,56 @@ private static HashSet<Assembly> GetModAssemblies()
5164
return assemblies;
5265
}
5366

54-
private static void FindTypePostfix(ref Type? __result)
67+
private static bool IsModType(Type type)
5568
{
56-
if (__result != null && !neosAssemblies!.Contains(__result.Assembly))
69+
if (neosAssemblies!.Contains(type.Assembly))
70+
{
71+
// the type belongs to a Neos assembly
72+
return false; // don't hide the type
73+
}
74+
else
5775
{
58-
if (!modAssemblies!.Contains(__result.Assembly))
76+
if (modAssemblies!.Contains(type.Assembly))
77+
{
78+
// known type from a mod assembly
79+
Logger.DebugInternal($"Hid type \"{type}\" from Neos");
80+
return true; // hide the type
81+
}
82+
else
5983
{
6084
// an assembly was in neither neosAssemblies nor modAssemblies
6185
// this implies someone late-loaded an assembly after NML, and it was later used in-game
6286
// this is super weird, and probably shouldn't ever happen... but if it does, I want to know about it.
6387
// since this is an edge case users may want to handle in different ways, the HideLateTypes nml config option allows them to choose.
6488
bool hideLate = ModLoaderConfiguration.Get().HideLateTypes;
65-
Logger.WarnInternal($"The \"{__result}\" type does not appear to part of Neos or a mod. It is unclear whether it should be hidden or not. due to the HideLateTypes config option being {hideLate} it will be {(hideLate ? "Hidden" : "Shown")}");
66-
if (hideLate) __result = null;
89+
Logger.WarnInternal($"The \"{type}\" type does not appear to part of Neos or a mod. It is unclear whether it should be hidden or not. Due to the HideLateTypes config option being {hideLate} it will be {(hideLate ? "Hidden" : "Shown")}");
90+
return hideLate; // hide the type only if hideLate == true
6791
}
68-
else
92+
}
93+
}
94+
95+
// postfix for a method that searches for a type, and returns a reference to it if found (TypeHelper.FindType and WorkerManager.GetType)
96+
private static void FindTypePostfix(ref Type? __result)
97+
{
98+
if (__result != null)
99+
{
100+
// we only need to think about types if the method actually returned a non-null result
101+
if (IsModType(__result))
102+
{
103+
__result = null;
104+
}
105+
}
106+
}
107+
108+
// postfix for a method that validates a type (WorkerManager.IsValidGenericType)
109+
private static void IsValidTypePostfix(ref bool __result, Type type)
110+
{
111+
if (__result == true)
112+
{
113+
// we only need to think about types if the method actually returned a true result
114+
if (IsModType(type))
69115
{
70-
Logger.DebugInternal($"Hid type \"{__result}\" from Neos");
71-
__result = null; // Pretend the type doesn't exist
116+
__result = false;
72117
}
73118
}
74119
}

NeosModLoader/ModLoader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace NeosModLoader
1010
{
1111
public class ModLoader
1212
{
13-
internal const string VERSION_CONSTANT = "1.12.4";
13+
internal const string VERSION_CONSTANT = "1.12.5";
1414
/// <summary>
1515
/// NeosModLoader's version
1616
/// </summary>

0 commit comments

Comments
 (0)