Mirror Networking
Extensions.cs
1using System;
2using System.Collections.Generic;
3using System.Runtime.CompilerServices;
4
5namespace Mirror
6{
7 public static class Extensions
8 {
9 // string.GetHashCode is not guaranteed to be the same on all machines, but
10 // we need one that is the same on all machines. simple and stupid:
11 public static int GetStableHashCode(this string text)
12 {
13 unchecked
14 {
15 int hash = 23;
16 foreach (char c in text)
17 hash = hash * 31 + c;
18 return hash;
19 }
20 }
21
22 // previously in DotnetCompatibility.cs
23 // leftover from the UNET days. supposedly for windows store?
24 internal static string GetMethodName(this Delegate func)
25 {
26#if NETFX_CORE
27 return func.GetMethodInfo().Name;
28#else
29 return func.Method.Name;
30#endif
31 }
32
33 // helper function to copy to List<T>
34 // C# only provides CopyTo(T[])
35 [MethodImpl(MethodImplOptions.AggressiveInlining)]
36 public static void CopyTo<T>(this IEnumerable<T> source, List<T> destination)
37 {
38 // foreach allocates. use AddRange.
39 destination.AddRange(source);
40 }
41
42#if !UNITY_2021_OR_NEWER
43 // Unity 2020 and earlier doesn't have Queue.TryDequeue which we need for batching.
44 public static bool TryDequeue<T>(this Queue<T> source, out T element)
45 {
46 if (source.Count > 0)
47 {
48 element = source.Dequeue();
49 return true;
50 }
51
52 element = default;
53 return false;
54 }
55#endif
56 }
57}