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.

223 lines
7.3KB

  1. using System;
  2. using System.Reflection;
  3. using GameNetworkLayer.Shared;
  4. using GameServer;
  5. using HarmonyLib;
  6. using NetworkFramework.Server;
  7. using Svelto.Context;
  8. using User.Server;
  9. namespace CLre_server.API.MainServer
  10. {
  11. public class Server
  12. {
  13. // static
  14. private static Server _instance = null;
  15. public static Server Instance
  16. {
  17. get
  18. {
  19. if (_instance == null) Init();
  20. return _instance;
  21. }
  22. }
  23. internal static void Init()
  24. {
  25. if (_instance == null) _instance = new Server();
  26. }
  27. // instance events
  28. public event EventHandler<StartingEventArgs> InitStart
  29. {
  30. add => MainGameServer_Constructor_Patch.preConstructor += value;
  31. remove => MainGameServer_Constructor_Patch.preConstructor -= value;
  32. }
  33. public event EventHandler<StartedEventArgs> InitComplete
  34. {
  35. add => ServerReadyEngine.serverEngineReady += value;
  36. remove => ServerReadyEngine.serverEngineReady -= value;
  37. }
  38. public event EventHandler<StartedEventArgs> FrameworkReady
  39. {
  40. add => ServerReadyEngine.serverFrameworkReady += value;
  41. remove => ServerReadyEngine.serverFrameworkReady -= value;
  42. }
  43. public event EventHandler<StopEventArgs> FrameworkExit
  44. {
  45. add => ServerReadyEngine.serverFrameworkDestroyed += value;
  46. remove => ServerReadyEngine.serverFrameworkDestroyed -= value;
  47. }
  48. public event EventHandler<StartedEventArgs> Connected
  49. {
  50. add => PhotonNetwork_ConnectUsingSettings_Patch.postConnect += value;
  51. remove => PhotonNetwork_ConnectUsingSettings_Patch.postConnect -= value;
  52. }
  53. public event EventHandler<PlayerConnectArgs> PlayerConnect
  54. {
  55. add => MainGameServer_SetupContainer_Patch.playerConnected += value;
  56. remove => MainGameServer_SetupContainer_Patch.playerConnected -= value;
  57. }
  58. public event EventHandler<PlayerConnectArgs> PlayerDisconnect
  59. {
  60. add => MainGameServer_SetupContainer_Patch.playerDisconnected += value;
  61. remove => MainGameServer_SetupContainer_Patch.playerDisconnected -= value;
  62. }
  63. // properties
  64. public GameServerSettings GameServerSettings
  65. {
  66. get => MainGameServer_SetupMods_Patch._gameServerSettings;
  67. set
  68. {
  69. MainGameServer_SetupMods_Patch._gameServerSettings = value;
  70. Traverse.Create(MainGameServer_Constructor_Patch.mgs).Field<GameServerSettings>("_gameServerSettings").Value = value;
  71. }
  72. }
  73. public AccountIdServerNode[] Players
  74. {
  75. get => _serverDatabaseQueryEngine.GetConnectedAccounts();
  76. }
  77. // fields
  78. private ServerDatabaseQueryEngine _serverDatabaseQueryEngine;
  79. private ServerReadyEngine _serverReadyEngine;
  80. private Server()
  81. {
  82. _serverReadyEngine = new ServerReadyEngine();
  83. _serverDatabaseQueryEngine = new ServerDatabaseQueryEngine();
  84. }
  85. }
  86. [HarmonyPatch]
  87. class MainGameServer_SetupMods_Patch
  88. {
  89. internal static GameServerSettings _gameServerSettings;
  90. [HarmonyPostfix]
  91. public static void AfterMethodCall(GameServerSettings ____gameServerSettings)
  92. {
  93. #if DEBUG
  94. Utility.Logging.MetaLog("Got GameServerSettings");
  95. #endif
  96. _gameServerSettings = ____gameServerSettings;
  97. }
  98. [HarmonyTargetMethod]
  99. public static MethodBase Target()
  100. {
  101. return AccessTools.Method("GameServer.GameFramework.MainGameServer:SetupMods");
  102. }
  103. }
  104. [HarmonyPatch]
  105. class MainGameServer_Constructor_Patch
  106. {
  107. internal static ICompositionRoot mgs = null;
  108. internal static event EventHandler<StartingEventArgs> preConstructor;
  109. internal static event EventHandler<StartingEventArgs> postConstructor;
  110. [HarmonyPrefix]
  111. public static void BeforeMethodCall()
  112. {
  113. if (preConstructor != null) preConstructor(null, default(StartingEventArgs));
  114. }
  115. [HarmonyPostfix]
  116. public static void AfterMethodCall(ICompositionRoot __instance)
  117. {
  118. mgs = __instance;
  119. if (postConstructor != null) postConstructor(__instance, default(StartingEventArgs));
  120. }
  121. [HarmonyTargetMethod]
  122. public static MethodBase Target()
  123. {
  124. return AccessTools.Constructor(AccessTools.TypeByName("GameServer.GameFramework.MainGameServer"));
  125. }
  126. }
  127. [HarmonyPatch(typeof(PhotonNetwork), "ConnectUsingSettings")]
  128. class PhotonNetwork_ConnectUsingSettings_Patch
  129. {
  130. internal static event EventHandler<StartedEventArgs> preConnect;
  131. internal static event EventHandler<StartedEventArgs> postConnect;
  132. [HarmonyPostfix]
  133. public static void AfterMethodCall(string gameVersion)
  134. {
  135. if (postConnect != null) postConnect(null, new StartedEventArgs
  136. {
  137. photonVersion = gameVersion,
  138. photonRegion = PhotonNetwork.CloudRegion,
  139. worldName = MainGameServer_SetupMods_Patch._gameServerSettings.GetWorldName(),
  140. gameGuid = MainGameServer_SetupMods_Patch._gameServerSettings.GetGameGuid(),
  141. });
  142. }
  143. [HarmonyPrefix]
  144. public static void BeforeMethodCall(string gameVersion)
  145. {
  146. if (preConnect != null) preConnect(null, new StartedEventArgs
  147. {
  148. photonVersion = gameVersion,
  149. photonRegion = PhotonNetwork.CloudRegion,
  150. worldName = MainGameServer_SetupMods_Patch._gameServerSettings.GetWorldName(),
  151. gameGuid = MainGameServer_SetupMods_Patch._gameServerSettings.GetGameGuid(),
  152. });
  153. }
  154. }
  155. [HarmonyPatch(typeof(GameServer.GameFramework.MainGameServer), "SetupContainer")]
  156. class MainGameServer_SetupContainer_Patch
  157. {
  158. internal static event EventHandler<PlayerConnectArgs> playerConnected;
  159. internal static event EventHandler<PlayerConnectArgs> playerDisconnected;
  160. private static GameServer.GameFramework.MainGameServer mgs = null;
  161. [HarmonyPostfix]
  162. public static void AfterMethodCall(GameServer.GameFramework.MainGameServer __instance, ref IPlayerConnectedCallbacks ____playerConnectedCallbacks)
  163. {
  164. mgs = __instance;
  165. ____playerConnectedCallbacks.OnPlayerConnected += OnConnect;
  166. ____playerConnectedCallbacks.OnPlayerDisconnected += OnDisconnect;
  167. }
  168. public static void OnConnect(int playerId)
  169. {
  170. Synergy.Clients.RegisterClient(playerId);
  171. if (playerConnected != null) playerConnected(mgs, new PlayerConnectArgs
  172. {
  173. PlayerId = playerId,
  174. });
  175. }
  176. public static void OnDisconnect(int playerId)
  177. {
  178. Synergy.Clients.RemoveClient(playerId);
  179. if (playerDisconnected != null) playerDisconnected(mgs, new PlayerConnectArgs
  180. {
  181. PlayerId = playerId,
  182. });
  183. }
  184. }
  185. }