Browse Source

Added Traverse.GetValue warning hook

ManlyMarco 5 years ago
parent
commit
16dbd1e3e5

+ 1 - 0
BepInEx.Preloader/BepInEx.Preloader.csproj

@@ -49,6 +49,7 @@
     <Compile Include="Patching\AssemblyPatcher.cs" />
     <Compile Include="Entrypoint.cs" />
     <Compile Include="Patching\PatcherPlugin.cs" />
+    <Compile Include="RuntimeFixes\HarmonyFixes.cs" />
     <Compile Include="RuntimeFixes\TraceFix.cs" />
     <Compile Include="Preloader.cs" />
     <Compile Include="Logger\PreloaderLogWriter.cs" />

+ 2 - 0
BepInEx.Preloader/Preloader.cs

@@ -51,6 +51,8 @@ namespace BepInEx.Preloader
 
 				Logger.Sources.Add(TraceLogSource.CreateSource());
 
+				HarmonyFixes.Apply();
+
 				PreloaderLog = new PreloaderConsoleListener(ConfigPreloaderCOutLogging.Value);
 				Logger.Listeners.Add(PreloaderLog);
 

+ 28 - 0
BepInEx.Preloader/RuntimeFixes/HarmonyFixes.cs

@@ -0,0 +1,28 @@
+using System;
+using System.Diagnostics;
+using HarmonyLib;
+
+namespace BepInEx.Preloader.RuntimeFixes
+{
+	internal static class HarmonyFixes
+	{
+		public static void Apply()
+		{
+			try
+			{
+				var harmony = new HarmonyLib.Harmony("BepInEx.Preloader.RuntimeFixes.HarmonyFixes");
+				harmony.Patch(AccessTools.Method(typeof(Traverse), nameof(Traverse.GetValue), new Type[0]), null, new HarmonyMethod(typeof(HarmonyFixes), nameof(GetValue)));
+			}
+			catch (Exception e)
+			{
+				Logging.Logger.LogError(e);
+			}
+		}
+
+		private static void GetValue(Traverse __instance)
+		{
+			if (!__instance.FieldExists() && !__instance.MethodExists() && !__instance.TypeExists())
+				Logging.Logger.LogWarning("Traverse.GetValue was called while not pointing at an existing Field, Property, Method or Type. The return value can be unexpected.\n" + new StackTrace());
+		}
+	}
+}