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.

84 lines
3.0KB

  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_server.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, hello!");
  24. //System.IO.File.WriteAllText("InitLogSooner.log", $"Done at " + System.DateTime.Now.ToString());
  25. }
  26. catch (Exception e)
  27. {
  28. API.Utility.Logging.Log($"Failed to initialise log sooner, reason:\n" + e);
  29. System.IO.File.WriteAllText("InitLogSooner.log", e.ToString());
  30. }
  31. }
  32. [Bugfix(name = "InitLogSooner",
  33. target = typeof(CustomLoggerThread),
  34. component = BugfixType.HarmonyPatch, id = 0)]
  35. [HarmonyPatch(typeof(CustomLoggerThread), "CreateGameObject")]
  36. class CustomLoggerThread_CreateGameObject_Patch
  37. {
  38. internal static bool allowed = false;
  39. public static bool Prefix()
  40. {
  41. return allowed;
  42. }
  43. }
  44. [Bugfix(name = "InitLogSooner",
  45. component = BugfixType.HarmonyPatch, id = 0)]
  46. [HarmonyPatch(typeof(CustomLoggerThread), "StartQueue")]
  47. class CustomLoggerThread_StartQueue_Patch
  48. {
  49. internal static volatile bool IsLogStarted = false;
  50. private delegate void Flusher();
  51. public static bool Prefix()
  52. {
  53. // setup thru reflection
  54. FieldInfo quitThreadField = AccessTools.Field(typeof(CustomLoggerThread), "_quitThread");
  55. MethodInfo flushLoggerMethod = AccessTools.Method(typeof(CustomLoggerThread), "FlushLogger");
  56. Flusher flushLogger = (Flusher) Delegate.CreateDelegate(typeof(Action), null, flushLoggerMethod);
  57. MethodInfo forceFlushMethod = AccessTools.Method("CustomLogger:ForceFlush");
  58. Flusher forceFlush = (Flusher) Delegate.CreateDelegate(typeof(Action), null, forceFlushMethod);
  59. Thread.MemoryBarrier();
  60. IsLogStarted = true;
  61. while (!(bool) quitThreadField.GetValue(null))
  62. {
  63. flushLogger();
  64. forceFlush();
  65. Thread.Sleep(millisecondsTimeout);
  66. }
  67. IsLogStarted = false;
  68. return false;
  69. }
  70. }
  71. }
  72. }