Extensions.cs 1.6 KB

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