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 ;
public override string Version { get ; } = Assembly . GetExecutingAssembly ( ) . GetName ( ) . Version . ToString ( ) ;
internal static Harmony harmonyInstance = null ;
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 ( )
{
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
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
2021-03-18 18:51:13 +00:00
WebServer . Init ( ) ;
2021-05-27 02:00:26 +01:00
API . Synergy . CLreEnforcer . 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 ( ) ) ;
}
public override void OnGUI ( )
{
if ( GUI . Button ( new Rect ( 10 , 10 , 50 , 50 ) , "QUIT" ) )
{
Application . Quit ( ) ; // yeet
}
}
}
}