diff --git a/Mirror/Weaver/Extensions.cs b/Mirror/Weaver/Extensions.cs new file mode 100644 index 000000000..d14fb5987 --- /dev/null +++ b/Mirror/Weaver/Extensions.cs @@ -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; + } + } + } +} \ No newline at end of file diff --git a/Mirror/Weaver/Mirror.Weaver.csproj b/Mirror/Weaver/Mirror.Weaver.csproj index 3a2875ff6..6aa67d1a0 100644 --- a/Mirror/Weaver/Mirror.Weaver.csproj +++ b/Mirror/Weaver/Mirror.Weaver.csproj @@ -66,6 +66,7 @@ + diff --git a/Mirror/Weaver/UNetBehaviourProcessor.cs b/Mirror/Weaver/UNetBehaviourProcessor.cs index d3071fab1..5d38a8fae 100644 --- a/Mirror/Weaver/UNetBehaviourProcessor.cs +++ b/Mirror/Weaver/UNetBehaviourProcessor.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)