Util.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using UnityEngine;
  6. namespace FacilityFlag
  7. {
  8. public static class Util
  9. {
  10. public static bool ToBoolean(BinaryReader br)
  11. {
  12. return br.ReadBoolean();
  13. }
  14. public static byte ToByte(BinaryReader br)
  15. {
  16. return br.ReadByte();
  17. }
  18. public static short ToInt16(BinaryReader br)
  19. {
  20. return br.ReadInt16();
  21. }
  22. public static int ToInt32(BinaryReader br)
  23. {
  24. return br.ReadInt32();
  25. }
  26. public static long ToInt64(BinaryReader br)
  27. {
  28. return br.ReadInt64();
  29. }
  30. public static char ToChar(BinaryReader br)
  31. {
  32. return br.ReadChar();
  33. }
  34. public static decimal ToDecimal(BinaryReader br)
  35. {
  36. return br.ReadDecimal();
  37. }
  38. public static double ToDouble(BinaryReader br)
  39. {
  40. return br.ReadDouble();
  41. }
  42. public static float ToSingle(BinaryReader br)
  43. {
  44. return br.ReadSingle();
  45. }
  46. public static string ToString(BinaryReader br)
  47. {
  48. return br.ReadString();
  49. }
  50. public static ushort ToUInt16(BinaryReader br)
  51. {
  52. return br.ReadUInt16();
  53. }
  54. public static uint ToUInt32(BinaryReader br)
  55. {
  56. return br.ReadUInt32();
  57. }
  58. public static ulong ToUInt64(BinaryReader br)
  59. {
  60. return br.ReadUInt64();
  61. }
  62. public static SimpleExperienceSystem ToSimpleExp(BinaryReader br)
  63. {
  64. SimpleExperienceSystem simpleExperienceSystem = new SimpleExperienceSystem();
  65. simpleExperienceSystem.Deserialize(br, 0);
  66. return simpleExperienceSystem;
  67. }
  68. public static byte[] GetBytes(string value)
  69. {
  70. byte[] bytes = Encoding.UTF8.GetBytes(value);
  71. int value2 = bytes.Length;
  72. byte[] array = Util.CreatePrefix(value2);
  73. return Util.AppendBuffer(new Array[]
  74. {
  75. array,
  76. bytes
  77. });
  78. }
  79. public static byte[] GetBytes(float value)
  80. {
  81. return BitConverter.GetBytes(value);
  82. }
  83. public static byte[] GetBytes(ulong value)
  84. {
  85. return BitConverter.GetBytes(value);
  86. }
  87. public static byte[] GetBytes(uint value)
  88. {
  89. return BitConverter.GetBytes(value);
  90. }
  91. public static byte[] GetBytes(ushort value)
  92. {
  93. return BitConverter.GetBytes(value);
  94. }
  95. public static byte[] GetBytes(long value)
  96. {
  97. return BitConverter.GetBytes(value);
  98. }
  99. public static byte[] GetBytes(double value)
  100. {
  101. return BitConverter.GetBytes(value);
  102. }
  103. public static byte[] GetBytes(short value)
  104. {
  105. return BitConverter.GetBytes(value);
  106. }
  107. public static byte[] GetBytes(char value)
  108. {
  109. return BitConverter.GetBytes(value);
  110. }
  111. public static byte[] GetBytes(bool value)
  112. {
  113. return BitConverter.GetBytes(value);
  114. }
  115. public static byte[] GetBytes(int value)
  116. {
  117. return BitConverter.GetBytes(value);
  118. }
  119. public static byte[] GetBytes(SimpleExperienceSystem simpleExp)
  120. {
  121. byte[] result;
  122. try
  123. {
  124. using (MemoryStream memoryStream = new MemoryStream())
  125. {
  126. using (BinaryWriter binaryWriter = new BinaryWriter(memoryStream))
  127. {
  128. simpleExp.Serialize(binaryWriter);
  129. }
  130. byte[] array = memoryStream.ToArray();
  131. result = array;
  132. }
  133. }
  134. catch
  135. {
  136. result = new byte[0];
  137. }
  138. return result;
  139. }
  140. public static byte[] CreatePrefix(int value)
  141. {
  142. List<byte> prefixBytes = new List<byte>();
  143. byte b = 0;
  144. Action f = null;
  145. f = delegate
  146. {
  147. b = (byte)(value & 127);
  148. value >>= 7;
  149. b |= ((value <= 0) ? 0 : 128);
  150. prefixBytes.Add(b);
  151. if ((b & 128) > 0)
  152. {
  153. f();
  154. }
  155. };
  156. f();
  157. return prefixBytes.ToArray();
  158. }
  159. public static int GetPrefix(byte[] buffer, long readStartPos)
  160. {
  161. Func<int, int> f = null;
  162. byte b = 0;
  163. f = delegate(int callCount)
  164. {
  165. b = buffer[(int)(checked((IntPtr)(unchecked(readStartPos + (long)callCount))))];
  166. int num = (int)(b & 127);
  167. num <<= callCount * 7;
  168. if ((b & 128) == 128)
  169. {
  170. num += f(callCount + 1);
  171. }
  172. return num;
  173. };
  174. return f(0);
  175. }
  176. public static int GetPrefix(Stream stream, long readStartPos, bool isEnableSeek = false)
  177. {
  178. long position = stream.Position;
  179. Func<int, int> f = null;
  180. byte b = 0;
  181. f = delegate(int callCount)
  182. {
  183. b = (byte)stream.ReadByte();
  184. int num = (int)(b & 127);
  185. num <<= callCount * 7;
  186. if ((b & 128) == 128)
  187. {
  188. num += f(callCount + 1);
  189. }
  190. return num;
  191. };
  192. int result = f(0);
  193. if (!isEnableSeek)
  194. {
  195. stream.Seek(position, SeekOrigin.Begin);
  196. }
  197. return result;
  198. }
  199. public static byte[] AppendBuffer(params Array[] bufferArray)
  200. {
  201. try
  202. {
  203. long num = 0L;
  204. for (int i = 0; i < bufferArray.Length; i++)
  205. {
  206. num += ((byte[])bufferArray[i]).LongLength;
  207. }
  208. byte[] array = new byte[num];
  209. int num2 = 0;
  210. for (int j = 0; j < bufferArray.Length; j++)
  211. {
  212. Buffer.BlockCopy(bufferArray[j], 0, array, num2, bufferArray[j].Length);
  213. num2 += bufferArray[j].Length;
  214. }
  215. return array;
  216. }
  217. catch (Exception message)
  218. {
  219. Debug.LogError(message);
  220. }
  221. return null;
  222. }
  223. }
  224. }