From 8c86f5f81eafb7e2083bc483f63eb9bed216798a Mon Sep 17 00:00:00 2001 From: Inas-07 <42310741+Inas-07@users.noreply.github.com> Date: Thu, 9 May 2024 20:39:57 +0800 Subject: [PATCH 1/2] State sync is not performed on re-joining lobby --- FloLib/Networks/Inject/Inject_OnRecallDone.cs | 32 +++++++++++++++++++ .../Replications/ReplicatorHandshake.cs | 25 ++++++++++++++- .../Replications/StateReplicator.Static.cs | 3 ++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 FloLib/Networks/Inject/Inject_OnRecallDone.cs diff --git a/FloLib/Networks/Inject/Inject_OnRecallDone.cs b/FloLib/Networks/Inject/Inject_OnRecallDone.cs new file mode 100644 index 0000000..e1ab8a7 --- /dev/null +++ b/FloLib/Networks/Inject/Inject_OnRecallDone.cs @@ -0,0 +1,32 @@ +using HarmonyLib; +using SNetwork; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FloLib.Networks.Inject +{ + namespace FloLib.Networks.Inject + { + // Token: 0x02000038 RID: 56 + [HarmonyPatch] + internal class Inject_OnRecallDone + { + // Token: 0x14000007 RID: 7 + // (add) Token: 0x06000145 RID: 325 RVA: 0x000059A4 File Offset: 0x00003BA4 + // (remove) Token: 0x06000146 RID: 326 RVA: 0x000059D8 File Offset: 0x00003BD8 + public static event Action OnRecallDone; + + // Token: 0x06000147 RID: 327 RVA: 0x00005A0B File Offset: 0x00003C0B + [HarmonyPostfix] + [HarmonyPatch(typeof(SNet_SyncManager), nameof(SNet_SyncManager.OnRecallDone))] + private static void Post_OnRecallDone(SNet_SyncManager __instance) + { + OnRecallDone?.Invoke(); + } + } + } + +} diff --git a/FloLib/Networks/Replications/ReplicatorHandshake.cs b/FloLib/Networks/Replications/ReplicatorHandshake.cs index f07ab01..c829268 100644 --- a/FloLib/Networks/Replications/ReplicatorHandshake.cs +++ b/FloLib/Networks/Replications/ReplicatorHandshake.cs @@ -1,8 +1,10 @@ -using GTFO.API; +using FloLib.Networks.Inject.FloLib.Networks.Inject; +using GTFO.API; using SNetwork; using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; @@ -29,6 +31,27 @@ private ReplicatorHandshake(string eventName) { EventName = eventName; NetworkAPI.RegisterEvent(eventName, OnSyncAction); + Inject_OnRecallDone.OnRecallDone += delegate () + { + Logger.Warn("ReplicatorHandshake: client sending sync request"); + ClientSyncRequest(); + }; + } + + private void ClientSyncRequest() + { + if (SNet.IsMaster) + { + return; + } + foreach(uint replicatorID in _Lookup.Keys) + { + NetworkAPI.InvokeEvent(this.EventName, new ReplicatorHandshake.Packet + { + replicatorID = replicatorID, + action = PacketAction.SyncRequest + }, SNet.Master, SNet_ChannelType.GameOrderCritical); + } } public void Reset() diff --git a/FloLib/Networks/Replications/StateReplicator.Static.cs b/FloLib/Networks/Replications/StateReplicator.Static.cs index e7b16c4..da6db5d 100644 --- a/FloLib/Networks/Replications/StateReplicator.Static.cs +++ b/FloLib/Networks/Replications/StateReplicator.Static.cs @@ -1,4 +1,5 @@ using FloLib.Networks.Inject; +using FloLib.Networks.Inject.FloLib.Networks.Inject; using FloLib.Networks.Replications; using GTFO.API; using SNetwork; @@ -45,6 +46,8 @@ static StateReplicator() _H_SetStateEvent = StatePayloads.CreateEvent(StateSizeType, HostSetStateEventName, HostSetStateEventCallback); _H_SetRecallStateEvent = StatePayloads.CreateEvent(StateSizeType, HostSetRecallStateEventName, HostSetRecallStateEventCallback); _Handshake = ReplicatorHandshake.Create($"{Name}-{HashName}"); + + _Handshake.OnClientSyncRequested += ClientSyncRequested; Inject_SNet_Capture.OnBufferCapture += BufferStored; From 688e35d9fa87788febb50422c20452035685495c Mon Sep 17 00:00:00 2001 From: Yi Zhou <42310741+Inas-07@users.noreply.github.com> Date: Tue, 15 Oct 2024 09:42:28 +0800 Subject: [PATCH 2/2] Replace direct reflection with accesstools reflection --- FloLib/Automation.cs | 20 +++++++++++++------ .../Replications/StateReplicator.Static.cs | 3 ++- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/FloLib/Automation.cs b/FloLib/Automation.cs index 115ba69..d776784 100644 --- a/FloLib/Automation.cs +++ b/FloLib/Automation.cs @@ -2,6 +2,7 @@ using FloLib.Attributes; using FloLib.Events; using GTFO.API; +using HarmonyLib; using Il2CppInterop.Runtime.Injection; using System; using System.Collections.Generic; @@ -78,7 +79,7 @@ public static void RegisterTypes() var targetAssem = new StackFrame(1).GetMethod()?.GetType()?.Assembly ?? null; if (targetAssem == null) throw new NullReferenceException("Caller Assembly was null"); - + RegisterTypes(targetAssem); } @@ -112,9 +113,11 @@ public static void RegisterTypes(Assembly target) private static void InjectAll(Assembly assem) { - var types = assem - .GetTypes()? - .Where(x=>Attribute.IsDefined(x, typeof(AutoInjectAttribute))) ?? Enumerable.Empty(); + //var types = assem + // .GetTypes()? + // .Where(x=>Attribute.IsDefined(x, typeof(AutoInjectAttribute))) ?? Enumerable.Empty(); + var types = AccessTools.GetTypesFromAssembly(assem)? + .Where(x => Attribute.IsDefined(x, typeof(AutoInjectAttribute))) ?? Enumerable.Empty(); foreach (var type in types) { @@ -136,13 +139,18 @@ private static void InjectAll(Assembly assem) private static void AddAutoInvokes(Assembly assem) { - var methods = assem - .GetTypes()? + var methods = AccessTools.GetTypesFromAssembly(assem)? .SelectMany(x => x.GetMethods(ALL))? .Where(x => x.IsStatic && Attribute.IsDefined(x, typeof(AutoInvokeAttribute))) ?? Enumerable.Empty(); + //var methods = assem + // .GetTypes()? + // .SelectMany(x => x.GetMethods(ALL))? + // .Where(x => x.IsStatic && Attribute.IsDefined(x, typeof(AutoInvokeAttribute))) ?? Enumerable.Empty(); + foreach (var method in methods) { + var attribute = (AutoInvokeAttribute)Attribute.GetCustomAttribute(method, typeof(AutoInvokeAttribute)); var args = attribute.Arguments; var when = attribute.When; diff --git a/FloLib/Networks/Replications/StateReplicator.Static.cs b/FloLib/Networks/Replications/StateReplicator.Static.cs index da6db5d..c71141a 100644 --- a/FloLib/Networks/Replications/StateReplicator.Static.cs +++ b/FloLib/Networks/Replications/StateReplicator.Static.cs @@ -36,7 +36,8 @@ static StateReplicator() StateSize = Marshal.SizeOf(typeof(S)); StateSizeType = StatePayloads.GetSizeType(StateSize); - using var md5 = MD5.Create(); + using var md5 = MD5.Create(); // unused + HashName = UName.GetHash(typeof(S).FullName); ClientRequestEventName = $"SRs{Name}-{HashName}"; HostSetStateEventName = $"SRr{Name}-{HashName}";