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.

206 lines
6.8KB

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