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.

128 lines
4.6KB

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