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.

134 lines
4.8KB

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