CLre/CLre_server/CLre_server.cs

141 lines
4.9 KiB
C#

using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using CLre_server.API.Config;
using CLre_server.API.Tools;
using CLre_server.WebStatus;
using GameNetworkLayer.Shared;
using HarmonyLib;
using Svelto.ECS;
using UnityEngine;
namespace CLre_server
{
public class CLre : IllusionPlugin.IEnhancedPlugin // the Illusion Plugin Architecture (IPA) will ignore classes that don't implement IPlugin
{
public override string Name { get; } = Assembly.GetExecutingAssembly().GetName().Name;
public override string Version { get; } = Assembly.GetExecutingAssembly().GetName().Version.ToString();
internal static Harmony harmonyInstance = null;
public static CLreConfig Config = CLreConfig.Default();
// called when Cardlife shuts down
public override void OnApplicationQuit()
{
WebServer.Deinit();
harmonyInstance.UnpatchAll();
}
// called when Cardlife starts up
public override void OnApplicationStart()
{
#if DEBUG
FileLog.Reset();
Harmony.DEBUG = true;
// enable CLre debug functionality
AccessToolsWarnings.Enable();
NetServerListener.Enable();
#endif
// init all Harmony patches in project
harmonyInstance = new Harmony(Name);
harmonyInstance.PatchAll();
// patches for bugs
Fixes.InitLogSooner.Init();
// API init
API.Synergy.ServerHandshakeEngine.Init();
// misc
LogIPAPlugins(); // log plugins again so they show up in the log, and not just stdout
Fixes.BugfixAttributeUtility.LogBugfixes(); // log bugfixes that are applied
#if DEBUG
// test CLre debug functionality
Type netData = AccessTools.TypeByName("Game.Handhelds.DrawingStateMessage");
NetServerSender.DebugSendMessage(netData, harmonyInstance,
NetServerSender.GetLogMethod(netData));
API.Utility.Logging.MetaLog("Patched SendMessage<Game.Handhelds.DrawingStateMessage>");
netData = AccessTools.TypeByName("Shared.Inventory.HandheldEquipmentRequest");
NetServerSender.DebugSendMessage(netData, harmonyInstance,
NetServerSender.GetLogMethod(netData));
API.Utility.Logging.MetaLog("Patched SendMessage<Shared.Inventory.HandheldEquipmentRequest>");
netData = typeof(API.Synergy.SerializedCLreHandshake);
NetServerSender.DebugSendMessage(netData, harmonyInstance,
NetServerSender.GetLogMethod(netData));
API.Utility.Logging.MetaLog("Patched SendMessage<SerializedCLreHandshake>");
NetServerListener.DebugReceiveMessage(NetworkDispatcherCode.EACMessageServerToClient,
NetServerListener.Log);
NetServerListener.DebugReceiveMessage(NetworkDispatcherCode.SendIsPvEToClient,
NetServerListener.Log);
NetServerListener.DebugReceiveMessage(API.Synergy.ServerHandshakeEngine.CLre_HANDSHAKE_NETCODE,
NetServerListener.Log);
// API debug and testing
API.MainServer.Server.Instance.FrameworkReady += (_, __) => API.Utility.Logging.MetaLog("(!) Server framework ready for business");
API.MainServer.Server.Instance.FrameworkExit += (_, __) => API.Utility.Logging.MetaLog("(!) Server framework shutting down"); // this seems to never happen
API.MainServer.Server.Instance.InitStart += (_, __) => API.Utility.Logging.MetaLog("(!) Server initialising");
API.MainServer.Server.Instance.InitComplete += (_, __) => API.Utility.Logging.MetaLog("(!) Server successfully initialised");
#endif
// try to load config file
try
{
string s = File.ReadAllText("CLre_server.json");
Config = CLreConfig.FromString(s);
}
catch (Exception e)
{
API.Utility.Logging.LogWarning($"Failed to load CLre_server.json: {e}\n{e.StackTrace}");
try
{
File.WriteAllText("CLre_server.json", Config.ToString());
}
catch (Exception e2)
{
API.Utility.Logging.LogWarning($"Failed to write CLre_server.json: {e2}\n{e2.StackTrace}");
}
}
// init config-dependent functionality
WebServer.Init();
API.Synergy.CLreEnforcer.Init();
// Log info
API.Utility.Logging.MetaLog($"{Name} init complete.");
}
private static void LogIPAPlugins()
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("Running on Unity {0}\n", Application.unityVersion);
sb.AppendFormat("Running on CardLife Server {0} (aka {1})\n", Game.Utilities.VersionReader.GetVersion(), Application.version);
sb.AppendFormat("-----------------------------\n");
sb.AppendFormat("Loading plugins from {0} and found {1}\n", System.IO.Path.Combine(Environment.CurrentDirectory, "Plugins"), IllusionInjector.PluginManager.Plugins.Count());
sb.AppendFormat("-----------------------------\n");
foreach (IllusionPlugin.IPlugin plugin in IllusionInjector.PluginManager.Plugins)
{
sb.AppendFormat(" {0}: {1}\n", plugin.Name, plugin.Version);
}
sb.AppendFormat("-----------------------------\n");
API.Utility.Logging.Log(sb.ToString());
}
public override void OnGUI()
{
if (GUI.Button(new Rect(10, 10, 50, 50), "QUIT"))
{
Application.Quit(); // yeet
}
}
}
}