|
| 1 | +using BaseX; |
| 2 | +using HarmonyLib; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Reflection; |
| 6 | + |
| 7 | +namespace NeosModLoader |
| 8 | +{ |
| 9 | + internal static class AssemblyHider |
| 10 | + { |
| 11 | + private static HashSet<Assembly>? neosAssemblies; |
| 12 | + private static HashSet<Assembly>? modAssemblies; |
| 13 | + |
| 14 | + /// <summary> |
| 15 | + /// Patch Neos's type lookup code to not see mod-related types. This is needed, because users can pass |
| 16 | + /// arbitrary strings to TypeHelper.FindType(), which can be used to detect if someone is running mods. |
| 17 | + /// </summary> |
| 18 | + /// <param name="harmony">Our NML harmony instance</param> |
| 19 | + /// <param name="initialAssemblies">Assemblies that were loaded when NML first started</param> |
| 20 | + internal static void PatchNeos(Harmony harmony, HashSet<Assembly> initialAssemblies) |
| 21 | + { |
| 22 | + if (ModLoaderConfiguration.Get().HideModTypes) |
| 23 | + { |
| 24 | + neosAssemblies = GetNeosAssemblies(initialAssemblies); |
| 25 | + 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)); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + private static HashSet<Assembly> GetNeosAssemblies(HashSet<Assembly> initialAssemblies) |
| 33 | + { |
| 34 | + // Remove NML itself, as its types should be hidden but it's guaranteed to be loaded. |
| 35 | + initialAssemblies.Remove(Assembly.GetExecutingAssembly()); |
| 36 | + |
| 37 | + // Remove Harmony, as users who aren't using nml_libs will already have it loaded. |
| 38 | + initialAssemblies.Remove(typeof(Harmony).Assembly); |
| 39 | + |
| 40 | + return initialAssemblies; |
| 41 | + } |
| 42 | + |
| 43 | + private static HashSet<Assembly> GetModAssemblies() |
| 44 | + { |
| 45 | + // start with ALL assemblies |
| 46 | + HashSet<Assembly> assemblies = AppDomain.CurrentDomain.GetAssemblies().ToHashSet(); |
| 47 | + |
| 48 | + // remove assemblies that already existed before NML loaded |
| 49 | + assemblies.ExceptWith(neosAssemblies); |
| 50 | + |
| 51 | + return assemblies; |
| 52 | + } |
| 53 | + |
| 54 | + private static void FindTypePostfix(ref Type? __result) |
| 55 | + { |
| 56 | + if (__result != null && !neosAssemblies!.Contains(__result.Assembly)) |
| 57 | + { |
| 58 | + if (!modAssemblies!.Contains(__result.Assembly)) |
| 59 | + { |
| 60 | + // an assembly was in neither neosAssemblies nor modAssemblies |
| 61 | + // this implies someone late-loaded an assembly after NML, and it was later used in-game |
| 62 | + // this is super weird, and probably shouldn't ever happen... but if it does, I want to know about it. |
| 63 | + 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."); |
| 64 | + } |
| 65 | + else |
| 66 | + { |
| 67 | + Type type = __result; |
| 68 | + Logger.DebugFuncInternal(() => $"Hid type \"{type}\" from Neos"); |
| 69 | + } |
| 70 | + |
| 71 | + // Pretend the type doesn't exist |
| 72 | + __result = null; |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments