From b470ca8a3699ca06c6ba7221612e2aad8c39c86a Mon Sep 17 00:00:00 2001 From: Garrett <52760019+Casimir255@users.noreply.github.com> Date: Fri, 25 Aug 2023 14:19:50 -0500 Subject: [PATCH] Updated seamless --- Components/PlayersWindowComponent.cs | 5 ++++ Components/ServerSwitcherComponent.cs | 39 ++++++++++++++++++++++++++- Seamless.cs | 4 --- SeamlessClient.csproj | 1 + Utilities/PatchUtils.cs | 22 +-------------- Utilities/UtilExtensions.cs | 15 +++++++++++ 6 files changed, 60 insertions(+), 26 deletions(-) create mode 100644 Utilities/UtilExtensions.cs diff --git a/Components/PlayersWindowComponent.cs b/Components/PlayersWindowComponent.cs index 4d96241..fe90eba 100644 --- a/Components/PlayersWindowComponent.cs +++ b/Components/PlayersWindowComponent.cs @@ -16,6 +16,9 @@ namespace SeamlessClient.OnlinePlayersWindow public class PlayersWindowComponent : ComponentBase { + + + public override void Patch(Harmony patcher) { patcher.CreateClassProcessor(typeof(OnlineNexusPlayersWindow)).Patch(); @@ -30,6 +33,8 @@ namespace SeamlessClient.OnlinePlayersWindow public static void ApplyRecievedPlayers(List servers, int CurrentServer) { Seamless.TryShow($"Recieved {CurrentServer} - {servers.Count}"); + + } diff --git a/Components/ServerSwitcherComponent.cs b/Components/ServerSwitcherComponent.cs index 27f787b..098e630 100644 --- a/Components/ServerSwitcherComponent.cs +++ b/Components/ServerSwitcherComponent.cs @@ -33,6 +33,12 @@ namespace SeamlessClient.ServerSwitching { public class ServerSwitcherComponent : ComponentBase { + public static ConstructorInfo ClientConstructor; + public static ConstructorInfo SyncLayerConstructor; + public static ConstructorInfo TransportLayerConstructor; + + public static PropertyInfo MySessionLayer; + private static FieldInfo RemoteAdminSettings; private static FieldInfo AdminSettings; private static MethodInfo UnloadProceduralWorldGenerator; @@ -63,6 +69,13 @@ namespace SeamlessClient.ServerSwitching public override void Patch(Harmony patcher) { + MySessionLayer = PatchUtils.GetProperty(typeof(MySession), "SyncLayer"); + + ClientConstructor = PatchUtils.GetConstructor(PatchUtils.ClientType, new[] { typeof(MyGameServerItem), PatchUtils.SyncLayerType }); + SyncLayerConstructor = PatchUtils.GetConstructor(PatchUtils.SyncLayerType, new[] { PatchUtils.MyTransportLayerType }); + TransportLayerConstructor = PatchUtils.GetConstructor(PatchUtils.MyTransportLayerType, new[] { typeof(int) }); + + RemoteAdminSettings = PatchUtils.GetField(typeof(MySession), "m_remoteAdminSettings"); AdminSettings = PatchUtils.GetField(typeof(MySession), "m_adminSettings"); VirtualClients = PatchUtils.GetField(typeof(MySession), "VirtualClients"); @@ -124,7 +137,7 @@ namespace SeamlessClient.ServerSwitching UnloadServer(); SetNewMultiplayerClient(); - + }); } @@ -174,17 +187,41 @@ namespace SeamlessClient.ServerSwitching private void SetNewMultiplayerClient() { OnJoinEvent += ServerSwitcherComponent_OnJoinEvent; + + MySandboxGame.Static.SessionCompatHelper.FixSessionComponentObjectBuilders(TargetWorld.Checkpoint, TargetWorld.Sector); + + + // Create constructors + var LayerInstance = TransportLayerConstructor.Invoke(new object[] { 2 }); + var SyncInstance = SyncLayerConstructor.Invoke(new object[] { LayerInstance }); + var instance = ClientConstructor.Invoke(new object[] { TargetServer, SyncInstance }); + + + MyMultiplayer.Static = UtilExtensions.CastToReflected(instance, PatchUtils.ClientType); + MyMultiplayer.Static.ExperimentalMode = true; + + // Set the new SyncLayer to the MySession.Static.SyncLayer + MySessionLayer.SetValue(MySession.Static, MyMultiplayer.Static.SyncLayer); + + Seamless.TryShow("Successfully set MyMultiplayer.Static"); + + + Sync.Clients.SetLocalSteamId(Sync.MyId, false, MyGameService.UserName); + Sync.Players.RegisterEvents(); } private void ServerSwitcherComponent_OnJoinEvent(object sender, JoinResultMsg e) { OnJoinEvent -= ServerSwitcherComponent_OnJoinEvent; + + if (e.JoinResult != JoinResult.OK) { Seamless.TryShow("Failed to join the target server!"); return; } + Seamless.TryShow("Starting new MP Client!"); /* On Server Successfull Join * diff --git a/Seamless.cs b/Seamless.cs index fd7f883..d18a4cc 100644 --- a/Seamless.cs +++ b/Seamless.cs @@ -113,7 +113,6 @@ namespace SeamlessClient { //Ignore anything except dedicated server - Seamless.TryShow($"From Server {fromServer} - {sender}"); if (!fromServer || sender == 0) return; @@ -122,15 +121,12 @@ namespace SeamlessClient return; //Get Nexus Version - Seamless.TryShow($"NexusVersion {msg.NexusVersion}"); if (!string.IsNullOrEmpty(msg.NexusVersion)) NexusVersion = Version.Parse(msg.NexusVersion); switch (msg.MessageType) { case ClientMessageType.FirstJoin: - - Seamless.TryShow("Sending First Join!"); ClientMessage response = new ClientMessage(SeamlessVersion.ToString()); MyAPIGateway.Multiplayer?.SendMessageToServer(SeamlessClientNetId, MessageUtils.Serialize(response)); diff --git a/SeamlessClient.csproj b/SeamlessClient.csproj index 9cd9f2c..31cbc4f 100644 --- a/SeamlessClient.csproj +++ b/SeamlessClient.csproj @@ -98,6 +98,7 @@ + diff --git a/Utilities/PatchUtils.cs b/Utilities/PatchUtils.cs index 7f47d7a..bc89781 100644 --- a/Utilities/PatchUtils.cs +++ b/Utilities/PatchUtils.cs @@ -50,19 +50,10 @@ namespace SeamlessClient.Components /* Static Contructors */ - public static ConstructorInfo ClientConstructor { get; private set; } - public static ConstructorInfo SyncLayerConstructor { get; private set; } - public static ConstructorInfo TransportLayerConstructor { get; private set; } public static ConstructorInfo MySessionConstructor { get; private set; } public static ConstructorInfo MyMultiplayerClientBaseConstructor { get; private set; } - /* Static FieldInfos and PropertyInfos */ - public static PropertyInfo MySessionLayer { get; private set; } - - public static FieldInfo AdminSettings { get; private set; } - public static FieldInfo MPlayerGpsCollection { get; private set; } - /* Static MethodInfos */ public static MethodInfo LoadPlayerInternal { get; private set; } @@ -78,24 +69,13 @@ namespace SeamlessClient.Components public static MethodInfo UnloadProceduralWorldGenerator; - - public override void Patch(Harmony patcher) { - ClientConstructor = GetConstructor(ClientType, new[] { typeof(MyGameServerItem), SyncLayerType }); - SyncLayerConstructor = GetConstructor(SyncLayerType, new[] { MyTransportLayerType }); - TransportLayerConstructor = GetConstructor(MyTransportLayerType, new[] { typeof(int) }); + MySessionConstructor = GetConstructor(MySessionType, new[] { typeof(MySyncLayer), typeof(bool) }); MyMultiplayerClientBaseConstructor = GetConstructor(MyMultiplayerClientBase, new[] { typeof(MySyncLayer) }); - /* Get Fields and Properties */ - MySessionLayer = GetProperty(typeof(MySession), "SyncLayer"); - AdminSettings = GetField(typeof(MySession), "m_adminSettings"); - - MPlayerGpsCollection = GetField(typeof(MyPlayerCollection), "m_players"); - - /* Get Methods */ diff --git a/Utilities/UtilExtensions.cs b/Utilities/UtilExtensions.cs new file mode 100644 index 0000000..dfaec32 --- /dev/null +++ b/Utilities/UtilExtensions.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SeamlessClient.Utilities +{ + public static class UtilExtensions + { + public static dynamic CastToReflected(this object o, Type type) => Convert.ChangeType(o, type); + + + } +}