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.

147 lines
4.6KB

  1. using System;
  2. using System.Reflection;
  3. using GameServer;
  4. using HarmonyLib;
  5. using Svelto.Context;
  6. namespace CLre_server.API.MainServer
  7. {
  8. public class Server
  9. {
  10. // static
  11. private static Server _instance = null;
  12. public static Server Instance
  13. {
  14. get
  15. {
  16. if (_instance == null) _instance = new Server();
  17. return _instance;
  18. }
  19. }
  20. // instance events
  21. public event EventHandler<StartingEventArgs> InitStart
  22. {
  23. add => MainGameServer_Constructor_Patch.preConstructor += value;
  24. remove => MainGameServer_Constructor_Patch.preConstructor -= value;
  25. }
  26. public event EventHandler<StartedEventArgs> InitComplete
  27. {
  28. add => ServerReadyEngine.serverEngineReady += value;
  29. remove => ServerReadyEngine.serverEngineReady -= value;
  30. }
  31. public event EventHandler<StartedEventArgs> FrameworkReady
  32. {
  33. add => ServerReadyEngine.serverFrameworkReady += value;
  34. remove => ServerReadyEngine.serverFrameworkReady -= value;
  35. }
  36. public event EventHandler<StopEventArgs> FrameworkExit
  37. {
  38. add => ServerReadyEngine.serverFrameworkDestroyed += value;
  39. remove => ServerReadyEngine.serverFrameworkDestroyed -= value;
  40. }
  41. // properties
  42. public GameServerSettings GameServerSettings
  43. {
  44. get => MainGameServer_SetupMods_Patch._gameServerSettings;
  45. set
  46. {
  47. MainGameServer_SetupMods_Patch._gameServerSettings = value;
  48. Traverse.Create(MainGameServer_Constructor_Patch.mgs).Field<GameServerSettings>("_gameServerSettings").Value = value;
  49. }
  50. }
  51. private Server()
  52. {
  53. new ServerReadyEngine();
  54. }
  55. }
  56. [HarmonyPatch]
  57. class MainGameServer_SetupMods_Patch
  58. {
  59. internal static GameServerSettings _gameServerSettings;
  60. [HarmonyPostfix]
  61. public static void AfterMethodCall(GameServerSettings ____gameServerSettings)
  62. {
  63. _gameServerSettings = ____gameServerSettings;
  64. }
  65. [HarmonyTargetMethod]
  66. public static MethodBase Target()
  67. {
  68. return AccessTools.Method("GameServer.GameFramework.MainGameServer:SetupMods");
  69. }
  70. }
  71. [HarmonyPatch]
  72. class MainGameServer_Constructor_Patch
  73. {
  74. internal static ICompositionRoot mgs = null;
  75. internal static event EventHandler<StartingEventArgs> preConstructor;
  76. internal static event EventHandler<StartingEventArgs> postConstructor;
  77. [HarmonyPrefix]
  78. public static void BeforeMethodCall()
  79. {
  80. if (preConstructor != null) preConstructor(null, default(StartingEventArgs));
  81. }
  82. [HarmonyPostfix]
  83. public static void AfterMethodCall(ICompositionRoot __instance)
  84. {
  85. mgs = __instance;
  86. if (postConstructor != null) postConstructor(__instance, default(StartingEventArgs));
  87. }
  88. [HarmonyTargetMethod]
  89. public static MethodBase Target()
  90. {
  91. return AccessTools.Constructor(AccessTools.TypeByName("GameServer.GameFramework.MainGameServer"));
  92. }
  93. }
  94. [HarmonyPatch(typeof(PhotonNetwork), "ConnectUsingSettings")]
  95. class PhotonNetwork_ConnectUsingSettings_Patch
  96. {
  97. internal static event EventHandler<StartedEventArgs> preConnect;
  98. internal static event EventHandler<StartedEventArgs> postConnect;
  99. [HarmonyPostfix]
  100. public static void AfterMethodCall(string gameVersion)
  101. {
  102. if (postConnect != null) postConnect(null, new StartedEventArgs
  103. {
  104. photonVersion = gameVersion,
  105. photonRegion = PhotonNetwork.CloudRegion,
  106. worldName = MainGameServer_SetupMods_Patch._gameServerSettings.GetWorldName(),
  107. gameGuid = MainGameServer_SetupMods_Patch._gameServerSettings.GetGameGuid(),
  108. });
  109. }
  110. [HarmonyPrefix]
  111. public static void BeforeMethodCall(string gameVersion)
  112. {
  113. if (preConnect != null) preConnect(null, new StartedEventArgs
  114. {
  115. photonVersion = gameVersion,
  116. photonRegion = PhotonNetwork.CloudRegion,
  117. worldName = MainGameServer_SetupMods_Patch._gameServerSettings.GetWorldName(),
  118. gameGuid = MainGameServer_SetupMods_Patch._gameServerSettings.GetGameGuid(),
  119. });
  120. }
  121. }
  122. }