R_SerializationHelper.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System;
  5. using System.Text;
  6. static public class R_SerializationHelper
  7. {
  8. // Serilization
  9. // ====================================================================================================================================
  10. static public void SerializeString (List<byte> bytes, string v) { bytes.AddRange(BitConverter.GetBytes(v.Length)); bytes.AddRange(Encoding.ASCII.GetBytes(v)); }
  11. static public void SerializeFloat (List<byte> bytes, float v) { bytes.AddRange(BitConverter.GetBytes(v)); }
  12. static public void SerializeInt (List<byte> bytes, int v) { bytes.AddRange(BitConverter.GetBytes(v)); }
  13. static public void SerializeBool (List<byte> bytes, bool v) { bytes.Add(v ? (byte)1 : (byte)0); }
  14. static public void SerializeVector2 (List<byte> bytes, Vector2 v) { bytes.AddRange(BitConverter.GetBytes(v.x)); bytes.AddRange(BitConverter.GetBytes(v.y)); }
  15. static public void SerializeVector3 (List<byte> bytes, Vector3 v) { bytes.AddRange(BitConverter.GetBytes(v.x)); bytes.AddRange(BitConverter.GetBytes(v.y)); bytes.AddRange(BitConverter.GetBytes(v.z)); }
  16. static public void SerializeVector4 (List<byte> bytes, Vector4 v) { bytes.AddRange(BitConverter.GetBytes(v.x)); bytes.AddRange(BitConverter.GetBytes(v.y)); bytes.AddRange(BitConverter.GetBytes(v.z)); bytes.AddRange(BitConverter.GetBytes(v.w)); }
  17. static public void SerializeQuaternion (List<byte> bytes, Quaternion v) { bytes.AddRange(BitConverter.GetBytes(v.x)); bytes.AddRange(BitConverter.GetBytes(v.y)); bytes.AddRange(BitConverter.GetBytes(v.z)); bytes.AddRange(BitConverter.GetBytes(v.w)); }
  18. static public void SerializeTransform (List<byte> bytes, Transform t) { SerializeVector3(bytes, t.position); SerializeVector4(bytes, new Vector4(t.rotation.x, t.rotation.y, t.rotation.z, t.rotation.w)); SerializeVector3(bytes, t.localScale); }
  19. static public void SerializeColor (List<byte> bytes, Color color) { bytes.AddRange(BitConverter.GetBytes(color.r)); bytes.AddRange(BitConverter.GetBytes(color.g)); bytes.AddRange(BitConverter.GetBytes(color.b)); bytes.AddRange(BitConverter.GetBytes(color.a)); }
  20. static public void SerializeIntArray(List<byte> bytes, int[] array) { SerializeInt(bytes, array.Length); for (int i = 0; i < array.Length; i++) SerializeInt(bytes, array[i]); }
  21. static public void SerializeFloatArray(List<byte> bytes, float[] array) { SerializeInt(bytes, array.Length); for (int i = 0; i < array.Length; i++) SerializeFloat(bytes, array[i]); }
  22. static public void SerializeBoolArray(List<byte> bytes, bool[] array) { SerializeInt(bytes, array.Length); for (int i = 0; i < array.Length; i++) SerializeBool(bytes, array[i]); }
  23. static public void SerializeVector2Array(List<byte> bytes, Vector2[] array) { SerializeInt(bytes, array.Length); for (int i = 0; i < array.Length; i++) SerializeVector2(bytes, array[i]); }
  24. static public void SerializeVector3Array(List<byte> bytes, Vector3[] array) { if (array == null) { return; } SerializeInt(bytes, array.Length); for (int i = 0; i < array.Length; i++) SerializeVector3(bytes, array[i]); }
  25. static public void SerializeVector4Array(List<byte> bytes, Vector4[] array) { SerializeInt(bytes, array.Length); for (int i = 0; i < array.Length; i++) SerializeVector4(bytes, array[i]); }
  26. static public void SerializeTransformArray(List<byte> bytes, Transform[] array) { SerializeInt(bytes, array.Length); for (int i = 0; i < array.Length; i++) SerializeTransform(bytes, array[i]); }
  27. static public void SerializeAnimationCurve (List<byte> bytes, AnimationCurve curve) { SerializeInt(bytes, curve.length); for (int i = 0; i < curve.length; i++) { SerializeVector2(bytes, new Vector2(curve.keys[i].time, curve.keys[i].value)); } }
  28. static public void SerializeAnimationCurveExact (List<byte> bytes, AnimationCurve curve)
  29. {
  30. SerializeInt(bytes, curve.length);
  31. for (int i = 0; i < curve.length; i++)
  32. {
  33. Keyframe key = curve.keys[i];
  34. SerializeFloat(bytes, key.time);
  35. SerializeFloat(bytes, key.value);
  36. SerializeFloat(bytes, key.inTangent);
  37. SerializeFloat(bytes, key.outTangent);
  38. SerializeInt(bytes, key.tangentMode);
  39. }
  40. }
  41. static public void Serialize2DFloatArray(List <byte> bytes, float[,] array)
  42. {
  43. int yLength = array.GetLength(0);
  44. int xLength = array.GetLength(1);
  45. bytes.AddRange(BitConverter.GetBytes(yLength));
  46. bytes.AddRange(BitConverter.GetBytes(xLength));
  47. for (int y = 0; y < yLength; y++)
  48. {
  49. for (int x = 0; x < xLength; x++)
  50. {
  51. bytes.AddRange(BitConverter.GetBytes(array[y,x]));
  52. }
  53. }
  54. }
  55. static public void Serialize2DIntArrayToBytes(List<byte> bytes, int[,] array)
  56. {
  57. int yLength = array.GetLength(0);
  58. int xLength = array.GetLength(1);
  59. bytes.AddRange(BitConverter.GetBytes(yLength));
  60. bytes.AddRange(BitConverter.GetBytes(xLength));
  61. for (int y = 0; y < yLength; y++)
  62. {
  63. for (int x = 0; x < xLength; x++)
  64. {
  65. bytes.Add((byte)array[y,x]);
  66. }
  67. }
  68. }
  69. // Deserialization
  70. // ====================================================================================================================================
  71. static public string DeserializeString(byte[] bytes, ref int index)
  72. {
  73. int length = BitConverter.ToInt32(bytes, index); index += 4;
  74. string v = Encoding.ASCII.GetString(bytes, index, length);
  75. index += length;
  76. return v;
  77. }
  78. static public float DeserializeFloat(byte[] bytes, ref int index)
  79. {
  80. float v = BitConverter.ToSingle(bytes, index); index += 4;
  81. return v;
  82. }
  83. static public int DeserializeInt(byte[] bytes, ref int index)
  84. {
  85. int v = BitConverter.ToInt32(bytes, index); index += 4;
  86. return v;
  87. }
  88. static public bool DeserializeBool(byte[] bytes, ref int index) { return (bytes[index++] == 1 ? true : false); }
  89. static public Vector2 DeserializeVector2(byte[] bytes, ref int index)
  90. {
  91. Vector2 v;
  92. v.x = BitConverter.ToSingle(bytes, index); index += 4;
  93. v.y = BitConverter.ToSingle(bytes, index); index += 4;
  94. return v;
  95. }
  96. static public Vector3 DeserializeVector3(byte[] bytes, ref int index)
  97. {
  98. Vector3 v;
  99. v.x = BitConverter.ToSingle(bytes, index); index += 4;
  100. v.y = BitConverter.ToSingle(bytes, index); index += 4;
  101. v.z = BitConverter.ToSingle(bytes, index); index += 4;
  102. return v;
  103. }
  104. static public Vector4 DeserializeVector4(byte[] bytes, ref int index)
  105. {
  106. Vector4 v;
  107. v.x = BitConverter.ToSingle(bytes, index); index += 4;
  108. v.y = BitConverter.ToSingle(bytes, index); index += 4;
  109. v.z = BitConverter.ToSingle(bytes, index); index += 4;
  110. v.w = BitConverter.ToSingle(bytes, index); index += 4;
  111. return v;
  112. }
  113. static public Quaternion DeserializeQuaternion(byte[] bytes, ref int index)
  114. {
  115. Quaternion v;
  116. v.x = BitConverter.ToSingle(bytes, index); index += 4;
  117. v.y = BitConverter.ToSingle(bytes, index); index += 4;
  118. v.z = BitConverter.ToSingle(bytes, index); index += 4;
  119. v.w = BitConverter.ToSingle(bytes, index); index += 4;
  120. return v;
  121. }
  122. static public Color DeserializeColor(byte[] bytes, ref int index)
  123. {
  124. Color color;
  125. color.r = BitConverter.ToSingle(bytes, index); index += 4;
  126. color.g = BitConverter.ToSingle(bytes, index); index += 4;
  127. color.b = BitConverter.ToSingle(bytes, index); index += 4;
  128. color.a = BitConverter.ToSingle(bytes, index); index += 4;
  129. return color;
  130. }
  131. static public void DeserializeTransform(byte[] bytes, ref int index, Transform t)
  132. {
  133. t.position = DeserializeVector3(bytes, ref index);
  134. Vector4 rotation = DeserializeVector4(bytes, ref index);
  135. t.rotation = new Quaternion(rotation.x, rotation.y, rotation.z, rotation.w);
  136. t.localScale = DeserializeVector3(bytes, ref index);
  137. }
  138. static public void DeserializeAnimationCurve(byte[] bytes, ref int index, AnimationCurve curve)
  139. {
  140. Vector2 v2;
  141. int length = DeserializeInt(bytes, ref index);
  142. for (int i = 0; i < length; i++) { v2 = DeserializeVector2(bytes, ref index); curve.AddKey(v2.x, v2.y); }
  143. }
  144. static public void DeserializeAnimationCurve2(byte[] bytes, ref int index, AnimationCurve curve)
  145. {
  146. Vector2 v2;
  147. int length = DeserializeInt(bytes, ref index);
  148. for (int i = 0; i < length; i++) { v2 = DeserializeVector2(bytes, ref index); curve.AddKey(new Keyframe(v2.x, v2.y, Mathf.Infinity, Mathf.Infinity)); }
  149. }
  150. static public Keyframe[] DeserializeAnimationCurveExact(byte[] bytes, ref int index)
  151. {
  152. int length = DeserializeInt(bytes, ref index);
  153. Keyframe[] keys = new Keyframe[length];
  154. for (int i = 0; i < length; i++)
  155. {
  156. float time = DeserializeFloat(bytes, ref index);
  157. float value = DeserializeFloat(bytes, ref index);
  158. float inTangent = DeserializeFloat(bytes, ref index);
  159. float outTangent = DeserializeFloat(bytes, ref index);
  160. keys[i] = new Keyframe(time, value, inTangent, outTangent);
  161. keys[i].tangentMode = DeserializeInt(bytes, ref index);
  162. }
  163. return keys;
  164. }
  165. static public int[] DeserializeIntArray(byte[] bytes, ref int index)
  166. {
  167. int length = DeserializeInt(bytes, ref index);
  168. int[] array = new int[length];
  169. for (int i = 0; i < length; i++) array[i] = DeserializeInt(bytes, ref index);
  170. return array;
  171. }
  172. static public float[] DeserializeFloatArray(byte[] bytes, ref int index)
  173. {
  174. int length = DeserializeInt(bytes, ref index);
  175. float[] array = new float[length];
  176. for (int i = 0; i < length; i++) array[i] = DeserializeFloat(bytes, ref index);
  177. return array;
  178. }
  179. static public bool[] DeserializeBoolArray(byte[] bytes, ref int index)
  180. {
  181. int length = DeserializeInt(bytes, ref index);
  182. bool[] array = new bool[length];
  183. for (int i = 0; i < length; i++) array[i] = DeserializeBool(bytes, ref index);
  184. return array;
  185. }
  186. static public Vector2[] DeserializeVector2Array(byte[] bytes, ref int index)
  187. {
  188. int length = DeserializeInt(bytes, ref index);
  189. Vector2[] array = new Vector2[length];
  190. for (int i = 0; i < length; i++) array[i] = DeserializeVector2(bytes, ref index);
  191. return array;
  192. }
  193. static public Vector3[] DeserializeVector3Array(byte[] bytes, ref int index)
  194. {
  195. int length = DeserializeInt(bytes, ref index);
  196. Vector3[] array = new Vector3[length];
  197. for (int i = 0; i < length; i++) array[i] = DeserializeVector3(bytes, ref index);
  198. return array;
  199. }
  200. static public Vector4[] DeserializeVector4Array(byte[] bytes, ref int index)
  201. {
  202. int length = DeserializeInt(bytes, ref index);
  203. Vector4[] array = new Vector4[length];
  204. for (int i = 0; i < length; i++) array[i] = DeserializeVector4(bytes, ref index);
  205. return array;
  206. }
  207. static public Transform[] DeserializeTransformArray(byte[] bytes, ref int index)
  208. {
  209. int length = DeserializeInt(bytes, ref index);
  210. Transform[] array = new Transform[length];
  211. for (int i = 0; i < length; i++) DeserializeTransform(bytes, ref index, array[i]);
  212. return array;
  213. }
  214. static public float[,] Deserialize2DFloatArray(byte[] bytes, ref int index)
  215. {
  216. int yLength = BitConverter.ToInt32(bytes, index); index += 4;
  217. int xLength = BitConverter.ToInt32(bytes, index); index += 4;
  218. float[,] v = new float[yLength, xLength];
  219. for (int y = 0; y < yLength; y++)
  220. {
  221. for (int x = 0; x < xLength; x++)
  222. {
  223. v[y, x] = BitConverter.ToSingle(bytes, index); index += 4;
  224. }
  225. }
  226. return v;
  227. }
  228. static public int[,] Deserialize2DByteArrayToInt(byte[] bytes, ref int index)
  229. {
  230. int yLength = BitConverter.ToInt32(bytes, index); index += 4;
  231. int xLength = BitConverter.ToInt32(bytes, index); index += 4;
  232. int[,] v = new int[yLength, xLength];
  233. for (int y = 0; y < yLength; y++)
  234. {
  235. for (int x = 0; x < xLength; x++)
  236. {
  237. v[y, x] = bytes[index++];
  238. }
  239. }
  240. return v;
  241. }
  242. }