mirror of
https://github.com/MirrorNetworking/Mirror.git
synced 2024-11-18 02:50:32 +00:00
Weaver: GetHashCode replaced with string.GetStableHashCode extension that is less weird and can be used in HLAPI too (without compiling with /unsafe flag)
This commit is contained in:
parent
86c482cf60
commit
07ab30f366
19
Mirror/Weaver/Extensions.cs
Normal file
19
Mirror/Weaver/Extensions.cs
Normal file
@ -0,0 +1,19 @@
|
||||
namespace Mirror.Weaver
|
||||
{
|
||||
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 HLAPI
|
||||
public static int GetStableHashCode(this string text)
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
int hash = 23;
|
||||
foreach (char c in text)
|
||||
hash = hash * 31 + c;
|
||||
return hash;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -66,6 +66,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="AssemblyInfo.cs" />
|
||||
<Compile Include="Extensions.cs" />
|
||||
<Compile Include="MessageClassProcessor.cs" />
|
||||
<Compile Include="MonoBehaviourProcessor.cs" />
|
||||
<Compile Include="Program.cs" />
|
||||
|
@ -240,7 +240,7 @@ void GenerateConstants()
|
||||
{
|
||||
FieldReference cmdConstant = Weaver.ResolveField(m_td, "kCmd" + md.Name);
|
||||
|
||||
int cmdHash = GetHashCode(m_td.Name + ":Cmd:" + md.Name);
|
||||
int cmdHash = (m_td.Name + ":Cmd:" + md.Name).GetStableHashCode();
|
||||
cctorWorker.Append(cctorWorker.Create(OpCodes.Ldc_I4, cmdHash));
|
||||
cctorWorker.Append(cctorWorker.Create(OpCodes.Stsfld, cmdConstant));
|
||||
//Weaver.DLog(m_td, " Constant " + m_td.Name + ":Cmd:" + md.Name);
|
||||
@ -254,7 +254,7 @@ void GenerateConstants()
|
||||
{
|
||||
FieldReference rpcConstant = Weaver.ResolveField(m_td, "kRpc" + md.Name);
|
||||
|
||||
int rpcHash = GetHashCode(m_td.Name + ":Rpc:" + md.Name);
|
||||
int rpcHash = (m_td.Name + ":Rpc:" + md.Name).GetStableHashCode();
|
||||
cctorWorker.Append(cctorWorker.Create(OpCodes.Ldc_I4, rpcHash));
|
||||
cctorWorker.Append(cctorWorker.Create(OpCodes.Stsfld, rpcConstant));
|
||||
//Weaver.DLog(m_td, " Constant " + m_td.Name + ":Rpc:" + md.Name);
|
||||
@ -268,7 +268,7 @@ void GenerateConstants()
|
||||
{
|
||||
FieldReference targetRpcConstant = Weaver.ResolveField(m_td, "kTargetRpc" + md.Name);
|
||||
|
||||
int targetRpcHash = GetHashCode(m_td.Name + ":TargetRpc:" + md.Name);
|
||||
int targetRpcHash = (m_td.Name + ":TargetRpc:" + md.Name).GetStableHashCode();
|
||||
cctorWorker.Append(cctorWorker.Create(OpCodes.Ldc_I4, targetRpcHash));
|
||||
cctorWorker.Append(cctorWorker.Create(OpCodes.Stsfld, targetRpcConstant));
|
||||
//Weaver.DLog(m_td, " Constant " + m_td.Name + ":Rpc:" + md.Name);
|
||||
@ -282,7 +282,7 @@ void GenerateConstants()
|
||||
{
|
||||
FieldReference eventConstant = Weaver.ResolveField(m_td, "kEvent" + ed.Name);
|
||||
|
||||
int eventHash = GetHashCode(m_td.Name + ":Event:" + ed.Name);
|
||||
int eventHash = (m_td.Name + ":Event:" + ed.Name).GetStableHashCode();
|
||||
cctorWorker.Append(cctorWorker.Create(OpCodes.Ldc_I4, eventHash));
|
||||
cctorWorker.Append(cctorWorker.Create(OpCodes.Stsfld, eventConstant));
|
||||
//Weaver.DLog(m_td, " Constant " + m_td.Name + ":Event:" + ed.Name);
|
||||
@ -1908,30 +1908,6 @@ void ProcessSyncVars()
|
||||
Weaver.SetNumSyncVars(m_td.FullName, numSyncVars);
|
||||
}
|
||||
|
||||
// Copy of Mono string.GetHashCode(), so that we generate same hashes regardless of runtime (mono/MS .NET)
|
||||
private static int GetHashCode(string s)
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
int length = s.Length;
|
||||
fixed(char* c = s)
|
||||
{
|
||||
char* cc = c;
|
||||
char* end = cc + length - 1;
|
||||
int h = 0;
|
||||
for (; cc < end; cc += 2)
|
||||
{
|
||||
h = (h << 5) - h + *cc;
|
||||
h = (h << 5) - h + cc[1];
|
||||
}
|
||||
++end;
|
||||
if (cc < end)
|
||||
h = (h << 5) - h + *cc;
|
||||
return h;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool HasMethod(string name)
|
||||
{
|
||||
foreach (var method in m_td.Methods)
|
||||
|
Loading…
Reference in New Issue
Block a user