|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- using System;
- using System.Reflection;
- using GameNetworkLayer.Shared;
- using GameServer;
- using HarmonyLib;
- using NetworkFramework.Server;
- using Svelto.Context;
- using User.Server;
-
- namespace CLre_server.API.MainServer
- {
- public class Server
- {
- // static
-
- private static Server _instance = null;
-
- public static Server Instance
- {
- get
- {
- if (_instance == null) Init();
- return _instance;
- }
- }
-
- internal static void Init()
- {
- if (_instance == null) _instance = new Server();
- }
-
- // instance events
- public event EventHandler<StartingEventArgs> InitStart
- {
- add => MainGameServer_Constructor_Patch.preConstructor += value;
- remove => MainGameServer_Constructor_Patch.preConstructor -= value;
- }
-
- public event EventHandler<StartedEventArgs> InitComplete
- {
- add => ServerReadyEngine.serverEngineReady += value;
- remove => ServerReadyEngine.serverEngineReady -= value;
- }
-
- public event EventHandler<StartedEventArgs> FrameworkReady
- {
- add => ServerReadyEngine.serverFrameworkReady += value;
- remove => ServerReadyEngine.serverFrameworkReady -= value;
- }
-
- public event EventHandler<StopEventArgs> FrameworkExit
- {
- add => ServerReadyEngine.serverFrameworkDestroyed += value;
- remove => ServerReadyEngine.serverFrameworkDestroyed -= value;
- }
-
- public event EventHandler<StartedEventArgs> Connected
- {
- add => PhotonNetwork_ConnectUsingSettings_Patch.postConnect += value;
- remove => PhotonNetwork_ConnectUsingSettings_Patch.postConnect -= value;
- }
-
- public event EventHandler<PlayerConnectArgs> PlayerConnect
- {
- add => MainGameServer_SetupContainer_Patch.playerConnected += value;
- remove => MainGameServer_SetupContainer_Patch.playerConnected -= value;
- }
-
- public event EventHandler<PlayerConnectArgs> PlayerDisconnect
- {
- add => MainGameServer_SetupContainer_Patch.playerDisconnected += value;
- remove => MainGameServer_SetupContainer_Patch.playerDisconnected -= value;
- }
-
- // properties
-
- public GameServerSettings GameServerSettings
- {
- get => MainGameServer_SetupMods_Patch._gameServerSettings;
-
- set
- {
- MainGameServer_SetupMods_Patch._gameServerSettings = value;
- Traverse.Create(MainGameServer_Constructor_Patch.mgs).Field<GameServerSettings>("_gameServerSettings").Value = value;
- }
- }
-
- public AccountIdServerNode[] Players
- {
- get => _serverDatabaseQueryEngine.GetConnectedAccounts();
- }
-
- // fields
-
- private ServerDatabaseQueryEngine _serverDatabaseQueryEngine;
- private ServerReadyEngine _serverReadyEngine;
-
- private Server()
- {
- _serverReadyEngine = new ServerReadyEngine();
- _serverDatabaseQueryEngine = new ServerDatabaseQueryEngine();
- }
- }
-
- [HarmonyPatch]
- class MainGameServer_SetupMods_Patch
- {
- internal static GameServerSettings _gameServerSettings;
-
- [HarmonyPostfix]
- public static void AfterMethodCall(GameServerSettings ____gameServerSettings)
- {
- #if DEBUG
- Utility.Logging.MetaLog("Got GameServerSettings");
- #endif
- _gameServerSettings = ____gameServerSettings;
- }
-
- [HarmonyTargetMethod]
- public static MethodBase Target()
- {
- return AccessTools.Method("GameServer.GameFramework.MainGameServer:SetupMods");
- }
- }
-
- [HarmonyPatch]
- class MainGameServer_Constructor_Patch
- {
-
- internal static ICompositionRoot mgs = null;
-
- internal static event EventHandler<StartingEventArgs> preConstructor;
-
- internal static event EventHandler<StartingEventArgs> postConstructor;
-
- [HarmonyPrefix]
- public static void BeforeMethodCall()
- {
- if (preConstructor != null) preConstructor(null, default(StartingEventArgs));
- }
-
- [HarmonyPostfix]
- public static void AfterMethodCall(ICompositionRoot __instance)
- {
- mgs = __instance;
- if (postConstructor != null) postConstructor(__instance, default(StartingEventArgs));
- }
-
- [HarmonyTargetMethod]
- public static MethodBase Target()
- {
- return AccessTools.Constructor(AccessTools.TypeByName("GameServer.GameFramework.MainGameServer"));
- }
- }
-
- [HarmonyPatch(typeof(PhotonNetwork), "ConnectUsingSettings")]
- class PhotonNetwork_ConnectUsingSettings_Patch
- {
- internal static event EventHandler<StartedEventArgs> preConnect;
-
- internal static event EventHandler<StartedEventArgs> postConnect;
-
- [HarmonyPostfix]
- public static void AfterMethodCall(string gameVersion)
- {
- if (postConnect != null) postConnect(null, new StartedEventArgs
- {
- photonVersion = gameVersion,
- photonRegion = PhotonNetwork.CloudRegion,
- worldName = MainGameServer_SetupMods_Patch._gameServerSettings.GetWorldName(),
- gameGuid = MainGameServer_SetupMods_Patch._gameServerSettings.GetGameGuid(),
- });
- }
-
- [HarmonyPrefix]
- public static void BeforeMethodCall(string gameVersion)
- {
- if (preConnect != null) preConnect(null, new StartedEventArgs
- {
- photonVersion = gameVersion,
- photonRegion = PhotonNetwork.CloudRegion,
- worldName = MainGameServer_SetupMods_Patch._gameServerSettings.GetWorldName(),
- gameGuid = MainGameServer_SetupMods_Patch._gameServerSettings.GetGameGuid(),
- });
- }
- }
-
- [HarmonyPatch(typeof(GameServer.GameFramework.MainGameServer), "SetupContainer")]
- class MainGameServer_SetupContainer_Patch
- {
- internal static event EventHandler<PlayerConnectArgs> playerConnected;
-
- internal static event EventHandler<PlayerConnectArgs> playerDisconnected;
-
- private static GameServer.GameFramework.MainGameServer mgs = null;
-
- [HarmonyPostfix]
- public static void AfterMethodCall(GameServer.GameFramework.MainGameServer __instance, ref IPlayerConnectedCallbacks ____playerConnectedCallbacks)
- {
- mgs = __instance;
- ____playerConnectedCallbacks.OnPlayerConnected += OnConnect;
- ____playerConnectedCallbacks.OnPlayerDisconnected += OnDisconnect;
- }
-
- public static void OnConnect(int playerId)
- {
- Synergy.Clients.RegisterClient(playerId);
- if (playerConnected != null) playerConnected(mgs, new PlayerConnectArgs
- {
- PlayerId = playerId,
- });
- }
-
- public static void OnDisconnect(int playerId)
- {
- Synergy.Clients.RemoveClient(playerId);
- if (playerDisconnected != null) playerDisconnected(mgs, new PlayerConnectArgs
- {
- PlayerId = playerId,
- });
- }
- }
- }
|