CLre/CLre_server/API/MainServer/Server.cs

204 lines
6.7 KiB
C#
Raw Normal View History

using System;
using System.Reflection;
2021-05-27 01:22:23 +01:00
using GameNetworkLayer.Shared;
using GameServer;
using HarmonyLib;
2021-05-27 01:22:23 +01:00
using NetworkFramework.Server;
using Svelto.Context;
namespace CLre_server.API.MainServer
{
public class Server
{
// static
private static Server _instance = null;
public static Server Instance
{
get
{
if (_instance == null) _instance = new Server();
return _instance;
}
}
// 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;
}
2021-05-27 01:22:23 +01:00
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;
}
}
private Server()
{
new ServerReadyEngine();
}
}
[HarmonyPatch]
class MainGameServer_SetupMods_Patch
{
internal static GameServerSettings _gameServerSettings;
[HarmonyPostfix]
public static void AfterMethodCall(GameServerSettings ____gameServerSettings)
{
2021-05-27 01:22:23 +01:00
#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(),
});
}
}
2021-05-27 01:22:23 +01:00
[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)
{
if (playerConnected != null) playerConnected(mgs, new PlayerConnectArgs
{
PlayerId = playerId,
});
}
public static void OnDisconnect(int playerId)
{
if (playerDisconnected != null) playerDisconnected(mgs, new PlayerConnectArgs
{
PlayerId = playerId,
});
}
}
}