Unofficial CardLife revival project, pronounced like "celery"
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AccessToolsWarnings.cs 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using System.Diagnostics;
  3. using System.Reflection;
  4. using HarmonyLib;
  5. #if DEBUG
  6. namespace CLre_server.API.Tools
  7. {
  8. public static class AccessToolsWarnings
  9. {
  10. internal static bool isEnabled = false;
  11. public static void Enable()
  12. {
  13. isEnabled = true;
  14. }
  15. public static void Disable()
  16. {
  17. isEnabled = false;
  18. }
  19. }
  20. [HarmonyPatch(typeof(AccessTools), "TypeByName")]
  21. class AccessTools_TypeByName_Patch
  22. {
  23. [HarmonyPostfix]
  24. public static void AfterMethodCall(Type __result, string name)
  25. {
  26. if (!AccessToolsWarnings.isEnabled) return;
  27. if (__result == null)
  28. {
  29. var method = (new StackTrace()).GetFrame(2).GetMethod();
  30. Utility.Logging.LogWarning($"[{method.DeclaringType.FullName}.{method.Name}] AccessTools.TypeByName(\"{name}\") returned null result");
  31. }
  32. }
  33. }
  34. [HarmonyPatch(typeof(AccessTools), "Method",
  35. new Type[] {typeof(string), typeof(Type[]), typeof(Type[])})]
  36. class AccessTools_Method_Patch
  37. {
  38. [HarmonyPostfix]
  39. public static void AfterMethodCall(MethodInfo __result, string typeColonMethodname)
  40. {
  41. if (!AccessToolsWarnings.isEnabled) return;
  42. if (__result == null)
  43. {
  44. var method = (new StackTrace()).GetFrame(2).GetMethod();
  45. Utility.Logging.LogWarning($"[{method.DeclaringType.FullName}.{method.Name}] AccessTools.Method(\"{typeColonMethodname}\") returned null result");
  46. }
  47. }
  48. }
  49. [HarmonyPatch(typeof(AccessTools), "Method",
  50. new Type[] {typeof(Type), typeof(string), typeof(Type[]), typeof(Type[])})]
  51. class AccessTools_Method2_Patch
  52. {
  53. [HarmonyPostfix]
  54. public static void AfterMethodCall(MethodInfo __result, Type type, string name)
  55. {
  56. if (!AccessToolsWarnings.isEnabled) return;
  57. if (__result == null)
  58. {
  59. var method = (new StackTrace()).GetFrame(2).GetMethod();
  60. Utility.Logging.LogWarning($"[{method.DeclaringType.FullName}.{method.Name}] AccessTools.Method(\"{type}\", \"{name}\") returned null result");
  61. }
  62. }
  63. }
  64. }
  65. #endif