diff --git a/CLre/API/Tools/NetClientListener.cs b/CLre/API/Tools/NetClientListener.cs index cd3feca..a326fe2 100644 --- a/CLre/API/Tools/NetClientListener.cs +++ b/CLre/API/Tools/NetClientListener.cs @@ -1,6 +1,9 @@ using System.Reflection; +using System.Text; using GameNetworkLayer.Shared; using HarmonyLib; +using Svelto.DataStructures; +using Svelto.DataStructures.Experimental; namespace CLre.API.Tools { @@ -8,6 +11,10 @@ namespace CLre.API.Tools { internal static bool isEnabled = false; + private static FasterDictionary> callbacks = new FasterDictionary>(); + + public delegate void NetReceiveMessageCallback(NetworkDispatcherCode code, byte[] data, int playerId); + public static void Enable() { isEnabled = true; @@ -17,6 +24,52 @@ namespace CLre.API.Tools { isEnabled = false; } + + public static void DebugReceiveMessage(NetworkDispatcherCode code, NetReceiveMessageCallback callback) + { + short key = (short)code; + if (callbacks.TryGetValue(key, out FasterList handlers)) + { + handlers.Add(callback); + } + else + { + FasterList newHandlers = new FasterList(new [] {callback}); + callbacks.Add(key, newHandlers); + } + } + + internal static bool RunDebugCallbacks(NetworkDispatcherCode code, byte[] data, int playerId) + { + short key = (short)code; + if (callbacks.TryGetValue(key, out FasterList handlers)) + { + foreach (NetReceiveMessageCallback callback in handlers) + { + callback(code, data, playerId); + } + + return true; + } + else + { + return false; + } + } + + public static void Log(NetworkDispatcherCode code, byte[] data, int playerId) + { + StringBuilder sb = new StringBuilder("Received "); + sb.Append(code.ToString()); + sb.Append(" for player #"); + sb.Append(playerId); + sb.Append(": 0x"); + foreach (byte b in data) + { + sb.Append(b.ToString("X")); + } + Utility.Logging.Log(sb.ToString()); + } } [HarmonyPatch] @@ -35,7 +88,8 @@ namespace CLre.API.Tools Utility.Logging.LogWarning("Network message data was deserialized as null"); return; } - Utility.Logging.Log($"Received network message for player {playerId} (code: {code.ToString()}, len: {data.Length})"); + bool isHandled = NetClientListener.RunDebugCallbacks(code, data, playerId); + if (!isHandled) Utility.Logging.Log($"Received network message for player {playerId} (code: {code.ToString()}, len: {data.Length})"); } [HarmonyTargetMethod] diff --git a/CLre/CLre.cs b/CLre/CLre.cs index 1e4a10e..47b618b 100644 --- a/CLre/CLre.cs +++ b/CLre/CLre.cs @@ -62,6 +62,9 @@ namespace CLre NetClientSender.GetLogMethod(netData)); API.Utility.Logging.MetaLog("Patched SendMessage"); + NetClientListener.DebugReceiveMessage(NetworkDispatcherCode.EACMessageServerToClient, + NetClientListener.Log); + // API debug and testing API.App.Client.InitComplete += (_, __) => { diff --git a/CLre/Fixes/CooldownCrossSlotSync.cs b/CLre/Fixes/CooldownCrossSlotSync.cs index 0045a18..a25c08a 100644 --- a/CLre/Fixes/CooldownCrossSlotSync.cs +++ b/CLre/Fixes/CooldownCrossSlotSync.cs @@ -78,11 +78,7 @@ namespace CLre.Fixes return; } - object wcevOriginal = queryWCEV(toolId, baseGroup); - float cooldownLeft = - Traverse.Create(wcevOriginal).Field("weaponCooldownComponent").Property("cooldownLeft").Value; - bool isInCooldown = - Traverse.Create(wcevOriginal).Field("weaponCooldownComponent").Property("isInCooldown").Value; + //object wcevOriginal = queryWCEV(toolId, baseGroup); //API.Utility.Logging.MetaLog($"Cooling down? {isInCooldown} for {cooldownLeft}s"); // build functions for querying all Game.Handhelds.WeaponCooldownEntityView objects Type protoRaw = @@ -109,23 +105,6 @@ namespace CLre.Fixes isRunningTick = true; cooldownTickEverything(characterId, cwcevExists, queryCWCEV, queryWCEV, queryAllWCEV, indexer, baseGroup).Run(); } - /*object[] indexParams = {0}; - for (int index = 0; index < count; index++) - { -#if DEBUG - API.Utility.Logging.MetaLog("Syncing cooldown with another weapon"); -#endif - indexParams[0] = index; - object wcev = indexer.GetValue(collectionStruct, indexParams); - Traverse.Create(wcev) - .Field("weaponCooldownComponent") - .Property("cooldownLeft") - .Value = cooldownLeft; - - Traverse.Create(wcev) - .Field("weaponCooldownComponent") - .Property("isInCooldown").Value = isInCooldown; - }*/ } [HarmonyTargetMethod] @@ -156,7 +135,9 @@ namespace CLre.Fixes Traverse.Create(wcevOriginal).Field("weaponCooldownComponent").Property("isInCooldown").Value; object[] indexParams = {0}; // iterate over other handhelds and sync their cooldowns to the held item +#if DEBUG API.Utility.Logging.MetaDebugLog($"Syncing {count} weapon cooldowns with the held item {toolId}"); +#endif for (int index = 0; index < count; index++) { indexParams[0] = index; @@ -175,7 +156,9 @@ namespace CLre.Fixes yield return null; } // cleanup +#if DEBUG API.Utility.Logging.MetaDebugLog("Custom cooldown ticks complete"); +#endif isRunningTick = false; yield return null; }