Skip to content

Commit 162c052

Browse files
authored
fix: Use GetStableHashCode16 instead of truncation (#4102)
1 parent df4fed3 commit 162c052

File tree

6 files changed

+11
-11
lines changed

6 files changed

+11
-11
lines changed

Assets/Mirror/Core/NetworkBehaviour.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ protected void InitSyncObject(SyncObject syncObject)
364364
}
365365

366366
// pass full function name to avoid ClassA.Func <-> ClassB.Func collisions
367-
protected void SendCommandInternal(string functionFullName, int functionHashCode, NetworkWriter writer, int channelId, bool requiresAuthority = true)
367+
protected void SendCommandInternal(string functionFullName, ushort functionHashCode, NetworkWriter writer, int channelId, bool requiresAuthority = true)
368368
{
369369
// this was in Weaver before
370370
// NOTE: we could remove this later to allow calling Cmds on Server
@@ -418,7 +418,7 @@ protected void SendCommandInternal(string functionFullName, int functionHashCode
418418
netId = netId,
419419
componentIndex = ComponentIndex,
420420
// type+func so Inventory.RpcUse != Equipment.RpcUse
421-
functionHash = (ushort)functionHashCode,
421+
functionHash = functionHashCode,
422422
// segment to avoid reader allocations
423423
payload = writer.ToArraySegment()
424424
};
@@ -434,7 +434,7 @@ protected void SendCommandInternal(string functionFullName, int functionHashCode
434434
}
435435

436436
// pass full function name to avoid ClassA.Func <-> ClassB.Func collisions
437-
protected void SendRPCInternal(string functionFullName, int functionHashCode, NetworkWriter writer, int channelId, bool includeOwner)
437+
protected void SendRPCInternal(string functionFullName, ushort functionHashCode, NetworkWriter writer, int channelId, bool includeOwner)
438438
{
439439
// this was in Weaver before
440440
if (!NetworkServer.active)
@@ -456,7 +456,7 @@ protected void SendRPCInternal(string functionFullName, int functionHashCode, Ne
456456
netId = netId,
457457
componentIndex = ComponentIndex,
458458
// type+func so Inventory.RpcUse != Equipment.RpcUse
459-
functionHash = (ushort)functionHashCode,
459+
functionHash = functionHashCode,
460460
// segment to avoid reader allocations
461461
payload = writer.ToArraySegment()
462462
};
@@ -488,7 +488,7 @@ protected void SendRPCInternal(string functionFullName, int functionHashCode, Ne
488488
}
489489

490490
// pass full function name to avoid ClassA.Func <-> ClassB.Func collisions
491-
protected void SendTargetRPCInternal(NetworkConnection conn, string functionFullName, int functionHashCode, NetworkWriter writer, int channelId)
491+
protected void SendTargetRPCInternal(NetworkConnection conn, string functionFullName, ushort functionHashCode, NetworkWriter writer, int channelId)
492492
{
493493
if (!NetworkServer.active)
494494
{
@@ -527,7 +527,7 @@ protected void SendTargetRPCInternal(NetworkConnection conn, string functionFull
527527
netId = netId,
528528
componentIndex = ComponentIndex,
529529
// type+func so Inventory.RpcUse != Equipment.RpcUse
530-
functionHash = (ushort)functionHashCode,
530+
functionHash = functionHashCode,
531531
// segment to avoid reader allocations
532532
payload = writer.ToArraySegment()
533533
};

Assets/Mirror/Core/RemoteCalls.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ static bool CheckIfDelegateExists(Type componentType, RemoteCallType remoteCallT
7070
internal static ushort RegisterDelegate(Type componentType, string functionFullName, RemoteCallType remoteCallType, RemoteCallDelegate func, bool cmdRequiresAuthority = true)
7171
{
7272
// type+func so Inventory.RpcUse != Equipment.RpcUse
73-
ushort hash = (ushort)(functionFullName.GetStableHashCode() & 0xFFFF);
73+
ushort hash = functionFullName.GetStableHashCode16();
7474

7575
if (CheckIfDelegateExists(componentType, remoteCallType, func, hash))
7676
return hash;

Assets/Mirror/Editor/Weaver/Processors/CommandProcessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public static MethodDefinition ProcessCommandCall(WeaverTypes weaverTypes, Write
128128
// otherwise each GetStableHash call requires O(N) complexity.
129129
// noticeable for long function names:
130130
// https://github.com/MirrorNetworking/Mirror/issues/3375
131-
worker.Emit(OpCodes.Ldc_I4, md.FullName.GetStableHashCode());
131+
worker.Emit(OpCodes.Ldc_I4, md.FullName.GetStableHashCode16());
132132
// writer
133133
worker.Emit(OpCodes.Ldloc_0);
134134
worker.Emit(OpCodes.Ldc_I4, channel);

Assets/Mirror/Editor/Weaver/Processors/RpcProcessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public static MethodDefinition ProcessRpcCall(WeaverTypes weaverTypes, Writers w
8686
// otherwise each GetStableHash call requires O(N) complexity.
8787
// noticeable for long function names:
8888
// https://github.com/MirrorNetworking/Mirror/issues/3375
89-
worker.Emit(OpCodes.Ldc_I4, md.FullName.GetStableHashCode());
89+
worker.Emit(OpCodes.Ldc_I4, md.FullName.GetStableHashCode16());
9090
// writer
9191
worker.Emit(OpCodes.Ldloc_0);
9292
worker.Emit(OpCodes.Ldc_I4, channel);

Assets/Mirror/Editor/Weaver/Processors/TargetRpcProcessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public static MethodDefinition ProcessTargetRpcCall(WeaverTypes weaverTypes, Wri
143143
// otherwise each GetStableHash call requires O(N) complexity.
144144
// noticeable for long function names:
145145
// https://github.com/MirrorNetworking/Mirror/issues/3375
146-
worker.Emit(OpCodes.Ldc_I4, md.FullName.GetStableHashCode());
146+
worker.Emit(OpCodes.Ldc_I4, md.FullName.GetStableHashCode16());
147147
// writer
148148
worker.Emit(OpCodes.Ldloc_0);
149149
worker.Emit(OpCodes.Ldc_I4, targetRpcAttr.GetField("channel", 0));

Assets/Mirror/Tests/Editor/NetworkBehaviour/NetworkBehaviourTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public void GetDelegate()
201201
false);
202202

203203
// get handler
204-
ushort cmdHash = (ushort)nameof(NetworkBehaviourDelegateComponent.Delegate).GetStableHashCode();
204+
ushort cmdHash = nameof(NetworkBehaviourDelegateComponent.Delegate).GetStableHashCode16();
205205
RemoteCallDelegate func = RemoteProcedureCalls.GetDelegate(cmdHash);
206206
RemoteCallDelegate expected = NetworkBehaviourDelegateComponent.Delegate;
207207
Assert.That(func, Is.EqualTo(expected));

0 commit comments

Comments
 (0)