using System; using System.Diagnostics; using System.Reflection; using HarmonyLib; #if DEBUG namespace CLre_server.API.Tools { public static class AccessToolsWarnings { internal static bool isEnabled = false; public static void Enable() { isEnabled = true; } public static void Disable() { isEnabled = false; } } [HarmonyPatch(typeof(AccessTools), "TypeByName")] class AccessTools_TypeByName_Patch { [HarmonyPostfix] public static void AfterMethodCall(Type __result, string name) { if (!AccessToolsWarnings.isEnabled) return; if (__result == null) { var method = (new StackTrace()).GetFrame(2).GetMethod(); Utility.Logging.LogWarning($"[{method.DeclaringType.FullName}.{method.Name}] AccessTools.TypeByName(\"{name}\") returned null result"); } } } [HarmonyPatch(typeof(AccessTools), "Method", new Type[] {typeof(string), typeof(Type[]), typeof(Type[])})] class AccessTools_Method_Patch { [HarmonyPostfix] public static void AfterMethodCall(MethodInfo __result, string typeColonMethodname) { if (!AccessToolsWarnings.isEnabled) return; if (__result == null) { var method = (new StackTrace()).GetFrame(2).GetMethod(); Utility.Logging.LogWarning($"[{method.DeclaringType.FullName}.{method.Name}] AccessTools.Method(\"{typeColonMethodname}\") returned null result"); } } } [HarmonyPatch(typeof(AccessTools), "Method", new Type[] {typeof(Type), typeof(string), typeof(Type[]), typeof(Type[])})] class AccessTools_Method2_Patch { [HarmonyPostfix] public static void AfterMethodCall(MethodInfo __result, Type type, string name) { if (!AccessToolsWarnings.isEnabled) return; if (__result == null) { var method = (new StackTrace()).GetFrame(2).GetMethod(); Utility.Logging.LogWarning($"[{method.DeclaringType.FullName}.{method.Name}] AccessTools.Method(\"{type}\", \"{name}\") returned null result"); } } } } #endif