HLAPI: GetStableHashCode added too

This commit is contained in:
vis2k 2018-12-30 16:24:28 +01:00
parent 07ab30f366
commit 1b33f7d633
3 changed files with 38 additions and 0 deletions

View File

@ -0,0 +1,20 @@
namespace Mirror
{
public static class Extensions
{
// string.GetHashCode is not guaranteed to be the same on all machines, but
// we need one that is the same on all machines. simple and stupid:
// IMPORTANT: needs to be same one as in Weaver
public static int GetStableHashCode(this string text)
{
unchecked
{
int hash = 23;
foreach (char c in text)
hash = hash * 31 + c;
return hash;
}
}
}
}

View File

@ -65,6 +65,7 @@
<Compile Include="ClientScene.cs" />
<Compile Include="CustomAttributes.cs" />
<Compile Include="DotNetCompatibility.cs" />
<Compile Include="Extensions.cs" />
<Compile Include="LocalConnections.cs" />
<Compile Include="LogFilter.cs" />
<Compile Include="Messages.cs" />

View File

@ -123,4 +123,21 @@ public static bool UnpackMessage(byte[] message, out ushort msgType, out byte[]
return true;
}
}
public static class Utils
{
// string.GetHashCode is not guaranteed to be the same on all machines, but
// we need one that is the same on all machines. simple and stupid:
// IMPORTANT: needs to be the same one as in Weaver
public static int GetStableHashCode(string text)
{
unchecked
{
int hash = 23;
foreach (char c in text)
hash = hash * 31 + c;
return hash;
}
}
}
}