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.

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