Serializer.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../IO/Serializer.h"
  24. #include "../DebugNew.h"
  25. namespace Atomic
  26. {
  27. static const float q = 32767.0f;
  28. Serializer::~Serializer()
  29. {
  30. }
  31. bool Serializer::WriteInt(int value)
  32. {
  33. return Write(&value, sizeof value) == sizeof value;
  34. }
  35. bool Serializer::WriteShort(short value)
  36. {
  37. return Write(&value, sizeof value) == sizeof value;
  38. }
  39. bool Serializer::WriteByte(signed char value)
  40. {
  41. return Write(&value, sizeof value) == sizeof value;
  42. }
  43. bool Serializer::WriteUInt(unsigned value)
  44. {
  45. return Write(&value, sizeof value) == sizeof value;
  46. }
  47. bool Serializer::WriteUShort(unsigned short value)
  48. {
  49. return Write(&value, sizeof value) == sizeof value;
  50. }
  51. bool Serializer::WriteUByte(unsigned char value)
  52. {
  53. return Write(&value, sizeof value) == sizeof value;
  54. }
  55. bool Serializer::WriteBool(bool value)
  56. {
  57. return WriteUByte((unsigned char)(value ? 1 : 0)) == 1;
  58. }
  59. bool Serializer::WriteFloat(float value)
  60. {
  61. return Write(&value, sizeof value) == sizeof value;
  62. }
  63. bool Serializer::WriteDouble(double value)
  64. {
  65. return Write(&value, sizeof value) == sizeof value;
  66. }
  67. bool Serializer::WriteIntRect(const IntRect& value)
  68. {
  69. return Write(value.Data(), sizeof value) == sizeof value;
  70. }
  71. bool Serializer::WriteIntVector2(const IntVector2& value)
  72. {
  73. return Write(value.Data(), sizeof value) == sizeof value;
  74. }
  75. bool Serializer::WriteRect(const Rect& value)
  76. {
  77. return Write(value.Data(), sizeof value) == sizeof value;
  78. }
  79. bool Serializer::WriteVector2(const Vector2& value)
  80. {
  81. return Write(value.Data(), sizeof value) == sizeof value;
  82. }
  83. bool Serializer::WriteVector3(const Vector3& value)
  84. {
  85. return Write(value.Data(), sizeof value) == sizeof value;
  86. }
  87. bool Serializer::WritePackedVector3(const Vector3& value, float maxAbsCoord)
  88. {
  89. short coords[3];
  90. float v = 32767.0f / maxAbsCoord;
  91. coords[0] = (short)(Clamp(value.x_, -maxAbsCoord, maxAbsCoord) * v + 0.5f);
  92. coords[1] = (short)(Clamp(value.y_, -maxAbsCoord, maxAbsCoord) * v + 0.5f);
  93. coords[2] = (short)(Clamp(value.z_, -maxAbsCoord, maxAbsCoord) * v + 0.5f);
  94. return Write(&coords[0], sizeof coords) == sizeof coords;
  95. }
  96. bool Serializer::WriteVector4(const Vector4& value)
  97. {
  98. return Write(value.Data(), sizeof value) == sizeof value;
  99. }
  100. bool Serializer::WriteQuaternion(const Quaternion& value)
  101. {
  102. return Write(value.Data(), sizeof value) == sizeof value;
  103. }
  104. bool Serializer::WritePackedQuaternion(const Quaternion& value)
  105. {
  106. short coords[4];
  107. Quaternion norm = value.Normalized();
  108. coords[0] = (short)(Clamp(norm.w_, -1.0f, 1.0f) * q + 0.5f);
  109. coords[1] = (short)(Clamp(norm.x_, -1.0f, 1.0f) * q + 0.5f);
  110. coords[2] = (short)(Clamp(norm.y_, -1.0f, 1.0f) * q + 0.5f);
  111. coords[3] = (short)(Clamp(norm.z_, -1.0f, 1.0f) * q + 0.5f);
  112. return Write(&coords[0], sizeof coords) == sizeof coords;
  113. }
  114. bool Serializer::WriteMatrix3(const Matrix3& value)
  115. {
  116. return Write(value.Data(), sizeof value) == sizeof value;
  117. }
  118. bool Serializer::WriteMatrix3x4(const Matrix3x4& value)
  119. {
  120. return Write(value.Data(), sizeof value) == sizeof value;
  121. }
  122. bool Serializer::WriteMatrix4(const Matrix4& value)
  123. {
  124. return Write(value.Data(), sizeof value) == sizeof value;
  125. }
  126. bool Serializer::WriteColor(const Color& value)
  127. {
  128. return Write(value.Data(), sizeof value) == sizeof value;
  129. }
  130. bool Serializer::WriteBoundingBox(const BoundingBox& value)
  131. {
  132. bool success = true;
  133. success &= WriteVector3(value.min_);
  134. success &= WriteVector3(value.max_);
  135. return success;
  136. }
  137. bool Serializer::WriteString(const String& value)
  138. {
  139. const char* chars = value.CString();
  140. // Count length to the first zero, because ReadString() does the same
  141. unsigned length = String::CStringLength(chars);
  142. return Write(chars, length + 1) == length + 1;
  143. }
  144. bool Serializer::WriteFileID(const String& value)
  145. {
  146. bool success = true;
  147. unsigned length = (unsigned)Min((int)value.Length(), 4);
  148. success &= Write(value.CString(), length) == length;
  149. for (unsigned i = value.Length(); i < 4; ++i)
  150. success &= WriteByte(' ');
  151. return success;
  152. }
  153. bool Serializer::WriteStringHash(const StringHash& value)
  154. {
  155. return WriteUInt(value.Value());
  156. }
  157. bool Serializer::WriteBuffer(const PODVector<unsigned char>& value)
  158. {
  159. bool success = true;
  160. unsigned size = value.Size();
  161. success &= WriteVLE(size);
  162. if (size)
  163. success &= Write(&value[0], size) == size;
  164. return success;
  165. }
  166. bool Serializer::WriteResourceRef(const ResourceRef& value)
  167. {
  168. bool success = true;
  169. success &= WriteStringHash(value.type_);
  170. success &= WriteString(value.name_);
  171. return success;
  172. }
  173. bool Serializer::WriteResourceRefList(const ResourceRefList& value)
  174. {
  175. bool success = true;
  176. success &= WriteStringHash(value.type_);
  177. success &= WriteVLE(value.names_.Size());
  178. for (unsigned i = 0; i < value.names_.Size(); ++i)
  179. success &= WriteString(value.names_[i]);
  180. return success;
  181. }
  182. bool Serializer::WriteVariant(const Variant& value)
  183. {
  184. bool success = true;
  185. VariantType type = value.GetType();
  186. success &= WriteUByte((unsigned char)type);
  187. success &= WriteVariantData(value);
  188. return success;
  189. }
  190. bool Serializer::WriteVariantData(const Variant& value)
  191. {
  192. switch (value.GetType())
  193. {
  194. case VAR_NONE:
  195. return true;
  196. case VAR_INT:
  197. return WriteInt(value.GetInt());
  198. case VAR_BOOL:
  199. return WriteBool(value.GetBool());
  200. case VAR_FLOAT:
  201. return WriteFloat(value.GetFloat());
  202. case VAR_VECTOR2:
  203. return WriteVector2(value.GetVector2());
  204. case VAR_VECTOR3:
  205. return WriteVector3(value.GetVector3());
  206. case VAR_VECTOR4:
  207. return WriteVector4(value.GetVector4());
  208. case VAR_QUATERNION:
  209. return WriteQuaternion(value.GetQuaternion());
  210. case VAR_COLOR:
  211. return WriteColor(value.GetColor());
  212. case VAR_STRING:
  213. return WriteString(value.GetString());
  214. case VAR_BUFFER:
  215. return WriteBuffer(value.GetBuffer());
  216. // Serializing pointers is not supported. Write null
  217. case VAR_VOIDPTR:
  218. case VAR_PTR:
  219. return WriteUInt(0);
  220. case VAR_RESOURCEREF:
  221. return WriteResourceRef(value.GetResourceRef());
  222. case VAR_RESOURCEREFLIST:
  223. return WriteResourceRefList(value.GetResourceRefList());
  224. case VAR_VARIANTVECTOR:
  225. return WriteVariantVector(value.GetVariantVector());
  226. case VAR_VARIANTMAP:
  227. return WriteVariantMap(value.GetVariantMap());
  228. case VAR_INTRECT:
  229. return WriteIntRect(value.GetIntRect());
  230. case VAR_INTVECTOR2:
  231. return WriteIntVector2(value.GetIntVector2());
  232. case VAR_MATRIX3:
  233. return WriteMatrix3(value.GetMatrix3());
  234. case VAR_MATRIX3X4:
  235. return WriteMatrix3x4(value.GetMatrix3x4());
  236. case VAR_MATRIX4:
  237. return WriteMatrix4(value.GetMatrix4());
  238. case VAR_DOUBLE:
  239. return WriteDouble(value.GetDouble());
  240. default:
  241. return false;
  242. }
  243. }
  244. bool Serializer::WriteVariantVector(const VariantVector& value)
  245. {
  246. bool success = true;
  247. success &= WriteVLE(value.Size());
  248. for (VariantVector::ConstIterator i = value.Begin(); i != value.End(); ++i)
  249. success &= WriteVariant(*i);
  250. return success;
  251. }
  252. bool Serializer::WriteVariantMap(const VariantMap& value)
  253. {
  254. bool success = true;
  255. success &= WriteVLE(value.Size());
  256. for (VariantMap::ConstIterator i = value.Begin(); i != value.End(); ++i)
  257. {
  258. WriteStringHash(i->first_);
  259. WriteVariant(i->second_);
  260. }
  261. return success;
  262. }
  263. bool Serializer::WriteVLE(unsigned value)
  264. {
  265. unsigned char data[4];
  266. if (value < 0x80)
  267. return WriteUByte((unsigned char)value);
  268. else if (value < 0x4000)
  269. {
  270. data[0] = (unsigned char)(value | 0x80);
  271. data[1] = (unsigned char)(value >> 7);
  272. return Write(data, 2) == 2;
  273. }
  274. else if (value < 0x200000)
  275. {
  276. data[0] = (unsigned char)(value | 0x80);
  277. data[1] = (unsigned char)((value >> 7) | 0x80);
  278. data[2] = (unsigned char)(value >> 14);
  279. return Write(data, 3) == 3;
  280. }
  281. else
  282. {
  283. data[0] = (unsigned char)(value | 0x80);
  284. data[1] = (unsigned char)((value >> 7) | 0x80);
  285. data[2] = (unsigned char)((value >> 14) | 0x80);
  286. data[3] = (unsigned char)(value >> 21);
  287. return Write(data, 4) == 4;
  288. }
  289. }
  290. bool Serializer::WriteNetID(unsigned value)
  291. {
  292. return Write(&value, 3) == 3;
  293. }
  294. bool Serializer::WriteLine(const String& value)
  295. {
  296. bool success = true;
  297. success &= Write(value.CString(), value.Length()) == value.Length();
  298. success &= WriteUByte(13);
  299. success &= WriteUByte(10);
  300. return success;
  301. }
  302. }