Utils.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using System.Runtime.InteropServices;
  4. namespace ET
  5. {
  6. public static partial class Utils
  7. {
  8. // Clamp so we don't have to depend on UnityEngine
  9. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  10. public static int Clamp(int value, int min, int max)
  11. {
  12. if (value < min) return min;
  13. if (value > max) return max;
  14. return value;
  15. }
  16. // // encode 8 bits unsigned int
  17. // [MethodImpl(MethodImplOptions.AggressiveInlining)]
  18. // public static int Encode8u(byte[] p, int offset, byte value)
  19. // {
  20. // p[0 + offset] = value;
  21. // return 1;
  22. // }
  23. //
  24. // // decode 8 bits unsigned int
  25. // [MethodImpl(MethodImplOptions.AggressiveInlining)]
  26. // public static int Decode8u(byte[] p, int offset, out byte value)
  27. // {
  28. // value = p[0 + offset];
  29. // return 1;
  30. // }
  31. //
  32. // [MethodImpl(MethodImplOptions.AggressiveInlining)]
  33. // public static int Decode8u(ReadOnlySpan<byte> data,int offset,out byte value)
  34. // {
  35. // value = data[offset];
  36. // return 1;
  37. // }
  38. //
  39. // // encode 16 bits unsigned int (lsb)
  40. // [MethodImpl(MethodImplOptions.AggressiveInlining)]
  41. // public static int Encode16U(byte[] p, int offset, ushort value)
  42. // {
  43. // p[0 + offset] = (byte)(value >> 0);
  44. // p[1 + offset] = (byte)(value >> 8);
  45. // return 2;
  46. // }
  47. //
  48. // // decode 16 bits unsigned int (lsb)
  49. // [MethodImpl(MethodImplOptions.AggressiveInlining)]
  50. // public static int Decode16U(byte[] p, int offset, out ushort value)
  51. // {
  52. // ushort result = 0;
  53. // result |= p[0 + offset];
  54. // result |= (ushort)(p[1 + offset] << 8);
  55. // value = result;
  56. // return 2;
  57. // }
  58. //
  59. // [MethodImpl(MethodImplOptions.AggressiveInlining)]
  60. // public static int Decode16U(ReadOnlySpan<byte> data, int offset, out ushort value)
  61. // {
  62. // value = Unsafe.ReadUnaligned<ushort>(ref MemoryMarshal.GetReference(data.Slice(offset)));
  63. // return 2;
  64. // }
  65. //
  66. // // encode 32 bits unsigned int (lsb)
  67. // [MethodImpl(MethodImplOptions.AggressiveInlining)]
  68. // public static int Encode32U(byte[] p, int offset, uint value)
  69. // {
  70. // p[0 + offset] = (byte)(value >> 0);
  71. // p[1 + offset] = (byte)(value >> 8);
  72. // p[2 + offset] = (byte)(value >> 16);
  73. // p[3 + offset] = (byte)(value >> 24);
  74. // return 4;
  75. // }
  76. //
  77. // // decode 32 bits unsigned int (lsb)
  78. // [MethodImpl(MethodImplOptions.AggressiveInlining)]
  79. // public static int Decode32U(byte[] p, int offset, out uint value)
  80. // {
  81. // uint result = 0;
  82. // result |= p[0 + offset];
  83. // result |= (uint)(p[1 + offset] << 8);
  84. // result |= (uint)(p[2 + offset] << 16);
  85. // result |= (uint)(p[3 + offset] << 24);
  86. // value = result;
  87. // return 4;
  88. // }
  89. //
  90. // [MethodImpl(MethodImplOptions.AggressiveInlining)]
  91. // public static int Decode32U(ReadOnlySpan<byte> data, int offset, out uint value)
  92. // {
  93. // value = Unsafe.ReadUnaligned<uint>(ref MemoryMarshal.GetReference(data.Slice(offset)));
  94. // return 4;
  95. // }
  96. // timediff was a macro in original Kcp. let's inline it if possible.
  97. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  98. public static int TimeDiff(uint later, uint earlier)
  99. {
  100. return (int)(later - earlier);
  101. }
  102. }
  103. }