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.

117 lines
4.3KB

  1. using System;
  2. using System.Diagnostics;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using CLre_server.API.Tools;
  7. using CLre_server.WebStatus;
  8. using GameNetworkLayer.Shared;
  9. using HarmonyLib;
  10. using Svelto.ECS;
  11. using UnityEngine;
  12. namespace CLre_server
  13. {
  14. public class CLre : IllusionPlugin.IEnhancedPlugin // the Illusion Plugin Architecture (IPA) will ignore classes that don't implement IPlugin
  15. {
  16. public override string Name { get; } = Assembly.GetExecutingAssembly().GetName().Name;
  17. public override string Version { get; } = Assembly.GetExecutingAssembly().GetName().Version.ToString();
  18. internal static Harmony harmonyInstance = null;
  19. // called when Cardlife shuts down
  20. public override void OnApplicationQuit()
  21. {
  22. WebServer.Deinit();
  23. harmonyInstance.UnpatchAll();
  24. }
  25. // called when Cardlife starts up
  26. public override void OnApplicationStart()
  27. {
  28. #if DEBUG
  29. FileLog.Reset();
  30. Harmony.DEBUG = true;
  31. // enable CLre debug functionality
  32. AccessToolsWarnings.Enable();
  33. NetServerListener.Enable();
  34. #endif
  35. // init all Harmony patches in project
  36. harmonyInstance = new Harmony(Name);
  37. harmonyInstance.PatchAll();
  38. // patches for bugs
  39. Fixes.InitLogSooner.Init();
  40. // API init
  41. API.Synergy.ServerHandshakeEngine.Init();
  42. // misc
  43. LogIPAPlugins(); // log plugins again so they show up in the log, and not just stdout
  44. Fixes.BugfixAttributeUtility.LogBugfixes(); // log bugfixes that are applied
  45. #if DEBUG
  46. // test CLre debug functionality
  47. Type netData = AccessTools.TypeByName("Game.Handhelds.DrawingStateMessage");
  48. NetServerSender.DebugSendMessage(netData, harmonyInstance,
  49. NetServerSender.GetLogMethod(netData));
  50. API.Utility.Logging.MetaLog("Patched SendMessage<Game.Handhelds.DrawingStateMessage>");
  51. netData = AccessTools.TypeByName("Shared.Inventory.HandheldEquipmentRequest");
  52. NetServerSender.DebugSendMessage(netData, harmonyInstance,
  53. NetServerSender.GetLogMethod(netData));
  54. API.Utility.Logging.MetaLog("Patched SendMessage<Shared.Inventory.HandheldEquipmentRequest>");
  55. netData = typeof(API.Synergy.SerializedCLreHandshake);
  56. NetServerSender.DebugSendMessage(netData, harmonyInstance,
  57. NetServerSender.GetLogMethod(netData));
  58. API.Utility.Logging.MetaLog("Patched SendMessage<SerializedCLreHandshake>");
  59. NetServerListener.DebugReceiveMessage(NetworkDispatcherCode.EACMessageServerToClient,
  60. NetServerListener.Log);
  61. NetServerListener.DebugReceiveMessage(NetworkDispatcherCode.SendIsPvEToClient,
  62. NetServerListener.Log);
  63. NetServerListener.DebugReceiveMessage(API.Synergy.ServerHandshakeEngine.CLre_HANDSHAKE_NETCODE,
  64. NetServerListener.Log);
  65. // API debug and testing
  66. API.MainServer.Server.Instance.FrameworkReady += (_, __) => API.Utility.Logging.MetaLog("(!) Server framework ready for business");
  67. API.MainServer.Server.Instance.FrameworkExit += (_, __) => API.Utility.Logging.MetaLog("(!) Server framework shutting down"); // this seems to never happen
  68. API.MainServer.Server.Instance.InitStart += (_, __) => API.Utility.Logging.MetaLog("(!) Server initialising");
  69. API.MainServer.Server.Instance.InitComplete += (_, __) => API.Utility.Logging.MetaLog("(!) Server successfully initialised");
  70. #endif
  71. WebServer.Init();
  72. // Log info
  73. API.Utility.Logging.MetaLog($"{Name} init complete.");
  74. }
  75. private static void LogIPAPlugins()
  76. {
  77. StringBuilder sb = new StringBuilder();
  78. sb.AppendFormat("Running on Unity {0}\n", Application.unityVersion);
  79. sb.AppendFormat("Running on CardLife Server {0} (aka {1})\n", Game.Utilities.VersionReader.GetVersion(), Application.version);
  80. sb.AppendFormat("-----------------------------\n");
  81. sb.AppendFormat("Loading plugins from {0} and found {1}\n", System.IO.Path.Combine(Environment.CurrentDirectory, "Plugins"), IllusionInjector.PluginManager.Plugins.Count());
  82. sb.AppendFormat("-----------------------------\n");
  83. foreach (IllusionPlugin.IPlugin plugin in IllusionInjector.PluginManager.Plugins)
  84. {
  85. sb.AppendFormat(" {0}: {1}\n", plugin.Name, plugin.Version);
  86. }
  87. sb.AppendFormat("-----------------------------\n");
  88. API.Utility.Logging.Log(sb.ToString());
  89. }
  90. public override void OnGUI()
  91. {
  92. if (GUI.Button(new Rect(10, 10, 50, 50), "QUIT"))
  93. {
  94. Application.Quit(); // yeet
  95. }
  96. }
  97. }
  98. }