Improve NetClientListener debugging

This commit is contained in:
NGnius (Graham) 2021-03-18 14:50:37 -04:00
parent 00589d1c28
commit 5dd67a88f4
3 changed files with 63 additions and 23 deletions

View file

@ -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<short, FasterList<NetReceiveMessageCallback>> callbacks = new FasterDictionary<short,FasterList<NetReceiveMessageCallback>>();
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<NetReceiveMessageCallback> handlers))
{
handlers.Add(callback);
}
else
{
FasterList<NetReceiveMessageCallback> newHandlers = new FasterList<NetReceiveMessageCallback>(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<NetReceiveMessageCallback> 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]

View file

@ -62,6 +62,9 @@ namespace CLre
NetClientSender.GetLogMethod(netData));
API.Utility.Logging.MetaLog("Patched SendMessage<Shared.Inventory.HandheldEquipmentRequest>");
NetClientListener.DebugReceiveMessage(NetworkDispatcherCode.EACMessageServerToClient,
NetClientListener.Log);
// API debug and testing
API.App.Client.InitComplete += (_, __) =>
{

View file

@ -78,11 +78,7 @@ namespace CLre.Fixes
return;
}
object wcevOriginal = queryWCEV(toolId, baseGroup);
float cooldownLeft =
Traverse.Create(wcevOriginal).Field("weaponCooldownComponent").Property<float>("cooldownLeft").Value;
bool isInCooldown =
Traverse.Create(wcevOriginal).Field("weaponCooldownComponent").Property<bool>("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<float>("cooldownLeft")
.Value = cooldownLeft;
Traverse.Create(wcev)
.Field("weaponCooldownComponent")
.Property<bool>("isInCooldown").Value = isInCooldown;
}*/
}
[HarmonyTargetMethod]
@ -156,7 +135,9 @@ namespace CLre.Fixes
Traverse.Create(wcevOriginal).Field("weaponCooldownComponent").Property<bool>("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;
}