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.

89 lines
3.3KB

  1. using System;
  2. using System.Diagnostics;
  3. using System.Reflection;
  4. using System.Runtime.CompilerServices;
  5. using System.Threading;
  6. using HarmonyLib;
  7. namespace CLre.Fixes
  8. {
  9. [Bugfix(name = "InitLogSooner",
  10. description = "Start the logger slightly sooner than Cardlife does",
  11. component = BugfixType.Initialiser, id = 0)]
  12. public static class InitLogSooner
  13. {
  14. public static int millisecondsTimeout = 5000;
  15. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  16. public static void Init()
  17. {
  18. try
  19. {
  20. CustomLoggerThread_CreateGameObject_Patch.allowed = true;
  21. CustomLoggerThread.CreateGameObject();
  22. CustomLoggerThread_CreateGameObject_Patch.allowed = false;
  23. API.Utility.Logging.Log("Completed early log init");
  24. //System.IO.File.WriteAllText("InitLogSooner.log", $"Done at " + System.DateTime.Now.ToString());
  25. }
  26. catch (Exception e)
  27. {
  28. API.App.Client.LogInitComplete += (_, __) =>
  29. {
  30. API.Utility.Logging.Log($"Failed to initialise log sooner, reason:\n" + e);
  31. };
  32. System.IO.File.WriteAllText("InitLogSooner.log", e.ToString());
  33. }
  34. }
  35. [Bugfix(name = "InitLogSooner",
  36. target = typeof(CustomLoggerThread),
  37. component = BugfixType.HarmonyPatch, id = 0)]
  38. [HarmonyPatch(typeof(CustomLoggerThread), "CreateGameObject")]
  39. class CustomLoggerThread_CreateGameObject_Patch
  40. {
  41. internal static bool allowed = false;
  42. public static bool Prefix()
  43. {
  44. return allowed;
  45. }
  46. }
  47. [Bugfix(name = "InitLogSooner",
  48. component = BugfixType.HarmonyPatch, id = 0)]
  49. [HarmonyPatch(typeof(CustomLoggerThread), "StartQueue")]
  50. class CustomLoggerThread_StartQueue_Patch
  51. {
  52. internal static volatile bool IsLogStarted = false;
  53. private delegate void Flusher();
  54. public static bool Prefix()
  55. {
  56. // setup thru reflection
  57. FieldInfo quitThreadField = AccessTools.Field(typeof(CustomLoggerThread), "_quitThread");
  58. MethodInfo flushLoggerMethod = AccessTools.Method(typeof(CustomLoggerThread), "FlushLogger");
  59. Flusher flushLogger = API.Utility.Reflection.BuildDelegate<Flusher>(flushLoggerMethod, null);
  60. //Flusher flushLogger = (Flusher) Delegate.CreateDelegate(typeof(Action), null, flushLoggerMethod);
  61. MethodInfo forceFlushMethod = AccessTools.Method("CustomLogger:ForceFlush");
  62. Flusher forceFlush = API.Utility.Reflection.BuildDelegate<Flusher>(forceFlushMethod, null);
  63. //Flusher forceFlush = (Flusher) Delegate.CreateDelegate(typeof(Action), null, forceFlushMethod);
  64. Thread.MemoryBarrier();
  65. IsLogStarted = true;
  66. while (!(bool) quitThreadField.GetValue(null))
  67. {
  68. flushLogger();
  69. forceFlush();
  70. Thread.Sleep(millisecondsTimeout);
  71. }
  72. IsLogStarted = false;
  73. return false;
  74. }
  75. }
  76. }
  77. }