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.

79 lines
2.7KB

  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. public static class InitLogSooner
  10. {
  11. public static int millisecondsTimeout = 5000;
  12. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  13. public static void Init()
  14. {
  15. try
  16. {
  17. CustomLoggerThread_CreateGameObject_Patch.allowed = true;
  18. CustomLoggerThread.CreateGameObject();
  19. CustomLoggerThread_CreateGameObject_Patch.allowed = false;
  20. API.Utility.Logging.Log($"Completed early log init");
  21. //System.IO.File.WriteAllText("InitLogSooner.log", $"Done at " + System.DateTime.Now.ToString());
  22. }
  23. catch (Exception e)
  24. {
  25. API.App.Client.LogInitComplete += (_, __) =>
  26. {
  27. API.Utility.Logging.Log($"Failed to initialise log sooner, reason:\n" + e);
  28. };
  29. System.IO.File.WriteAllText("InitLogSooner.log", e.ToString());
  30. }
  31. }
  32. [HarmonyPatch(typeof(CustomLoggerThread), "CreateGameObject")]
  33. class CustomLoggerThread_CreateGameObject_Patch
  34. {
  35. internal static bool allowed = false;
  36. public static bool Prefix()
  37. {
  38. return allowed;
  39. }
  40. }
  41. [HarmonyPatch(typeof(CustomLoggerThread), "StartQueue")]
  42. class CustomLoggerThread_StartQueue_Patch
  43. {
  44. internal static volatile bool IsLogStarted = false;
  45. private delegate void Flusher();
  46. public static bool Prefix()
  47. {
  48. // setup thru reflection
  49. FieldInfo quitThreadField = AccessTools.Field(typeof(CustomLoggerThread), "_quitThread");
  50. MethodInfo flushLoggerMethod = AccessTools.Method(typeof(CustomLoggerThread), "FlushLogger");
  51. Flusher flushLogger = (Flusher) Delegate.CreateDelegate(typeof(Action), null, flushLoggerMethod);
  52. MethodInfo forceFlushMethod = AccessTools.Method("CustomLogger:ForceFlush");
  53. Flusher forceFlush = (Flusher) Delegate.CreateDelegate(typeof(Action), null, forceFlushMethod);
  54. Thread.MemoryBarrier();
  55. IsLogStarted = true;
  56. while (!(bool) quitThreadField.GetValue(null))
  57. {
  58. flushLogger();
  59. forceFlush();
  60. Thread.Sleep(millisecondsTimeout);
  61. }
  62. IsLogStarted = false;
  63. return false;
  64. }
  65. }
  66. }
  67. }