LRMTools.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.IO.Compression;
  6. using System.Text;
  7. using UnityEngine;
  8. namespace LightReflectiveMirror
  9. {
  10. public static class LRMTools
  11. {
  12. public static void WriteByte(this byte[] data, ref int position, byte value)
  13. {
  14. data[position] = value;
  15. position += 1;
  16. }
  17. public static byte ReadByte(this byte[] data, ref int position)
  18. {
  19. byte value = data[position];
  20. position += 1;
  21. return value;
  22. }
  23. public static void WriteBool(this byte[] data, ref int position, bool value)
  24. {
  25. unsafe
  26. {
  27. fixed (byte* dataPtr = &data[position])
  28. {
  29. bool* valuePtr = (bool*)dataPtr;
  30. *valuePtr = value;
  31. position += 1;
  32. }
  33. }
  34. }
  35. public static bool ReadBool(this byte[] data, ref int position)
  36. {
  37. bool value = BitConverter.ToBoolean(data, position);
  38. position += 1;
  39. return value;
  40. }
  41. public static void WriteString(this byte[] data, ref int position, string value)
  42. {
  43. if (string.IsNullOrWhiteSpace(value))
  44. {
  45. data.WriteInt(ref position, 0);
  46. }
  47. else
  48. {
  49. data.WriteInt(ref position, value.Length);
  50. for (int i = 0; i < value.Length; i++)
  51. data.WriteChar(ref position, value[i]);
  52. }
  53. }
  54. public static string ReadString(this byte[] data, ref int position)
  55. {
  56. string value = default;
  57. int stringSize = data.ReadInt(ref position);
  58. for (int i = 0; i < stringSize; i++)
  59. value += data.ReadChar(ref position);
  60. return value;
  61. }
  62. public static void WriteBytes(this byte[] data, ref int position, byte[] value)
  63. {
  64. data.WriteInt(ref position, value.Length);
  65. for (int i = 0; i < value.Length; i++)
  66. data.WriteByte(ref position, value[i]);
  67. }
  68. public static byte[] ReadBytes(this byte[] data, ref int position)
  69. {
  70. int byteSize = data.ReadInt(ref position);
  71. byte[] value = new byte[byteSize];
  72. for (int i = 0; i < byteSize; i++)
  73. value[i] = data.ReadByte(ref position);
  74. return value;
  75. }
  76. public static void WriteChar(this byte[] data, ref int position, char value)
  77. {
  78. unsafe
  79. {
  80. fixed (byte* dataPtr = &data[position])
  81. {
  82. char* valuePtr = (char*)dataPtr;
  83. *valuePtr = value;
  84. position += 2;
  85. }
  86. }
  87. }
  88. public static char ReadChar(this byte[] data, ref int position)
  89. {
  90. char value = BitConverter.ToChar(data, position);
  91. position += 2;
  92. return value;
  93. }
  94. public static void WriteInt(this byte[] data, ref int position, int value)
  95. {
  96. unsafe
  97. {
  98. fixed (byte* dataPtr = &data[position])
  99. {
  100. int* valuePtr = (int*)dataPtr;
  101. *valuePtr = value;
  102. position += 4;
  103. }
  104. }
  105. }
  106. public static int ReadInt(this byte[] data, ref int position)
  107. {
  108. int value = BitConverter.ToInt32(data, position);
  109. position += 4;
  110. return value;
  111. }
  112. }
  113. internal static class CompressorExtensions
  114. {
  115. /// <summary>
  116. /// Decompresses the string.
  117. /// </summary>
  118. /// <param name="compressedText">The compressed text.</param>
  119. /// <returns></returns>
  120. public static string Decompress(this string compressedText)
  121. {
  122. byte[] gZipBuffer = Convert.FromBase64String(compressedText);
  123. using (var memoryStream = new MemoryStream())
  124. {
  125. int dataLength = BitConverter.ToInt32(gZipBuffer, 0);
  126. memoryStream.Write(gZipBuffer, 4, gZipBuffer.Length - 4);
  127. var buffer = new byte[dataLength];
  128. memoryStream.Position = 0;
  129. using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
  130. {
  131. gZipStream.Read(buffer, 0, buffer.Length);
  132. }
  133. return Encoding.UTF8.GetString(buffer);
  134. }
  135. }
  136. }
  137. internal static class JsonUtilityHelper
  138. {
  139. public static bool IsJsonArray(string json)
  140. {
  141. return json.StartsWith("[") && json.EndsWith("]");
  142. }
  143. public static T[] FromJson<T>(string json)
  144. {
  145. if (!IsJsonArray(json))
  146. {
  147. throw new System.FormatException("The input json string is not a Json Array");
  148. }
  149. json = "{\"Items\":" + json + "}";
  150. JsonWrapper<T> wrapper = JsonUtility.FromJson<JsonWrapper<T>>(json);
  151. return wrapper.Items;
  152. }
  153. public static string ToJson<T>(T[] array)
  154. {
  155. JsonWrapper<T> wrapper = new JsonWrapper<T>();
  156. wrapper.Items = array;
  157. return JsonUtility.ToJson(wrapper);
  158. }
  159. public static string ToJson<T>(T[] array, bool prettyPrint)
  160. {
  161. JsonWrapper<T> wrapper = new JsonWrapper<T>();
  162. wrapper.Items = array;
  163. return JsonUtility.ToJson(wrapper, prettyPrint);
  164. }
  165. [Serializable]
  166. private class JsonWrapper<T>
  167. {
  168. public T[] Items;
  169. }
  170. }
  171. }