From 1b33f7d6338fb7665cae84567fc9f2f9f879bd5b Mon Sep 17 00:00:00 2001 From: vis2k Date: Sun, 30 Dec 2018 16:24:28 +0100 Subject: [PATCH] HLAPI: GetStableHashCode added too --- Mirror/Runtime/Extensions.cs | 20 ++++++++++++++++++++ Mirror/Runtime/Mirror.Runtime.csproj | 1 + Mirror/Runtime/UNetwork.cs | 17 +++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 Mirror/Runtime/Extensions.cs diff --git a/Mirror/Runtime/Extensions.cs b/Mirror/Runtime/Extensions.cs new file mode 100644 index 000000000..cf090d7e6 --- /dev/null +++ b/Mirror/Runtime/Extensions.cs @@ -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; + } + } + } +} \ No newline at end of file diff --git a/Mirror/Runtime/Mirror.Runtime.csproj b/Mirror/Runtime/Mirror.Runtime.csproj index e7a8d0ba5..671cb2b60 100644 --- a/Mirror/Runtime/Mirror.Runtime.csproj +++ b/Mirror/Runtime/Mirror.Runtime.csproj @@ -65,6 +65,7 @@ + diff --git a/Mirror/Runtime/UNetwork.cs b/Mirror/Runtime/UNetwork.cs index af3d607d6..484903d90 100644 --- a/Mirror/Runtime/UNetwork.cs +++ b/Mirror/Runtime/UNetwork.cs @@ -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; + } + } + } }