2021-03-18 18:51:13 +00:00
using System ;
using System.Diagnostics ;
2021-05-27 02:00:26 +01:00
using System.IO ;
2021-03-18 18:51:13 +00:00
using System.Linq ;
using System.Reflection ;
using System.Text ;
2021-05-27 02:00:26 +01:00
using CLre_server.API.Config ;
2021-03-18 18:51:13 +00:00
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 ;
2021-08-14 02:26:48 +01:00
public override string Version { get ; } = "21Q3 " + Assembly . GetExecutingAssembly ( ) . GetName ( ) . Version . ToString ( ) ;
2021-03-18 18:51:13 +00:00
internal static Harmony harmonyInstance = null ;
2021-08-14 02:26:48 +01:00
private const string CONFIG_PATH = "CLre_server.json" ;
2021-05-27 02:00:26 +01:00
public static CLreConfig Config = CLreConfig . Default ( ) ;
2021-03-18 18:51:13 +00:00
// called when Cardlife shuts down
public override void OnApplicationQuit ( )
{
2021-08-14 02:26:48 +01:00
Config . ToFile ( CONFIG_PATH ) ;
2021-03-18 18:51:13 +00:00
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 ( ) ;
2021-05-27 01:22:23 +01:00
// API init
API . Synergy . ServerHandshakeEngine . Init ( ) ;
2021-03-18 18:51:13 +00:00
// 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>" ) ;
2021-05-27 01:22:23 +01:00
netData = typeof ( API . Synergy . SerializedCLreHandshake ) ;
NetServerSender . DebugSendMessage ( netData , harmonyInstance ,
NetServerSender . GetLogMethod ( netData ) ) ;
API . Utility . Logging . MetaLog ( "Patched SendMessage<SerializedCLreHandshake>" ) ;
2021-03-18 18:51:13 +00:00
NetServerListener . DebugReceiveMessage ( NetworkDispatcherCode . EACMessageServerToClient ,
NetServerListener . Log ) ;
2021-05-27 01:22:23 +01:00
NetServerListener . DebugReceiveMessage ( NetworkDispatcherCode . SendIsPvEToClient ,
NetServerListener . Log ) ;
NetServerListener . DebugReceiveMessage ( API . Synergy . ServerHandshakeEngine . CLre_HANDSHAKE_NETCODE ,
NetServerListener . Log ) ;
2021-03-18 18:51:13 +00:00
// 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
2021-05-27 02:00:26 +01:00
// try to load config file
2021-08-14 02:26:48 +01:00
Config = CLreConfig . FromFileSafely ( CONFIG_PATH ) ;
2021-05-27 02:00:26 +01:00
// init config-dependent functionality
2021-03-18 18:51:13 +00:00
WebServer . Init ( ) ;
2021-05-27 02:00:26 +01:00
API . Synergy . CLreEnforcer . Init ( ) ;
2021-08-01 19:37:13 +01:00
Tweaks . TerrainModificationExclusionZone . Init ( ) ;
2021-08-14 02:26:48 +01:00
Tweaks . Chat . ChatHandler . Init ( ) ;
API . MainServer . Server . Init ( ) ;
API . MainServer . Moderator . Init ( ) ;
2021-03-18 18:51:13 +00:00
// 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 ( ) ) ;
}
2021-08-01 19:37:13 +01:00
#if DEBUG
2021-03-18 18:51:13 +00:00
public override void OnGUI ( )
{
if ( GUI . Button ( new Rect ( 10 , 10 , 50 , 50 ) , "QUIT" ) )
{
Application . Quit ( ) ; // yeet
}
}
2021-08-01 19:37:13 +01:00
#endif
2021-03-18 18:51:13 +00:00
}
}