Serializer.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. //
  2. // Copyright (c) 2008-2017 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::WriteInt64(long long value)
  32. {
  33. return Write(&value, sizeof value) == sizeof value;
  34. }
  35. bool Serializer::WriteInt(int value)
  36. {
  37. return Write(&value, sizeof value) == sizeof value;
  38. }
  39. bool Serializer::WriteShort(short value)
  40. {
  41. return Write(&value, sizeof value) == sizeof value;
  42. }
  43. bool Serializer::WriteByte(signed char value)
  44. {
  45. return Write(&value, sizeof value) == sizeof value;
  46. }
  47. bool Serializer::WriteUInt64(unsigned long long value)
  48. {
  49. return Write(&value, sizeof value) == sizeof value;
  50. }
  51. bool Serializer::WriteUInt(unsigned value)
  52. {
  53. return Write(&value, sizeof value) == sizeof value;
  54. }
  55. bool Serializer::WriteUShort(unsigned short value)
  56. {
  57. return Write(&value, sizeof value) == sizeof value;
  58. }
  59. bool Serializer::WriteUByte(unsigned char value)
  60. {
  61. return Write(&value, sizeof value) == sizeof value;
  62. }
  63. bool Serializer::WriteBool(bool value)
  64. {
  65. return WriteUByte((unsigned char)(value ? 1 : 0)) == 1;
  66. }
  67. bool Serializer::WriteFloat(float value)
  68. {
  69. return Write(&value, sizeof value) == sizeof value;
  70. }
  71. bool Serializer::WriteDouble(double value)
  72. {
  73. return Write(&value, sizeof value) == sizeof value;
  74. }
  75. bool Serializer::WriteIntRect(const IntRect& value)
  76. {
  77. return Write(value.Data(), sizeof value) == sizeof value;
  78. }
  79. bool Serializer::WriteIntVector2(const IntVector2& value)
  80. {
  81. return Write(value.Data(), sizeof value) == sizeof value;
  82. }
  83. bool Serializer::WriteIntVector3(const IntVector3& value)
  84. {
  85. return Write(value.Data(), sizeof value) == sizeof value;
  86. }
  87. bool Serializer::WriteRect(const Rect& value)
  88. {
  89. return Write(value.Data(), sizeof value) == sizeof value;
  90. }
  91. bool Serializer::WriteVector2(const Vector2& value)
  92. {
  93. return Write(value.Data(), sizeof value) == sizeof value;
  94. }
  95. bool Serializer::WriteVector3(const Vector3& value)
  96. {
  97. return Write(value.Data(), sizeof value) == sizeof value;
  98. }
  99. bool Serializer::WritePackedVector3(const Vector3& value, float maxAbsCoord)
  100. {
  101. short coords[3];
  102. float v = 32767.0f / maxAbsCoord;
  103. coords[0] = (short)(Clamp(value.x_, -maxAbsCoord, maxAbsCoord) * v + 0.5f);
  104. coords[1] = (short)(Clamp(value.y_, -maxAbsCoord, maxAbsCoord) * v + 0.5f);
  105. coords[2] = (short)(Clamp(value.z_, -maxAbsCoord, maxAbsCoord) * v + 0.5f);
  106. return Write(&coords[0], sizeof coords) == sizeof coords;
  107. }
  108. bool Serializer::WriteVector4(const Vector4& value)
  109. {
  110. return Write(value.Data(), sizeof value) == sizeof value;
  111. }
  112. bool Serializer::WriteQuaternion(const Quaternion& value)
  113. {
  114. return Write(value.Data(), sizeof value) == sizeof value;
  115. }
  116. bool Serializer::WritePackedQuaternion(const Quaternion& value)
  117. {
  118. short coords[4];
  119. Quaternion norm = value.Normalized();
  120. coords[0] = (short)(Clamp(norm.w_, -1.0f, 1.0f) * q + 0.5f);
  121. coords[1] = (short)(Clamp(norm.x_, -1.0f, 1.0f) * q + 0.5f);
  122. coords[2] = (short)(Clamp(norm.y_, -1.0f, 1.0f) * q + 0.5f);
  123. coords[3] = (short)(Clamp(norm.z_, -1.0f, 1.0f) * q + 0.5f);
  124. return Write(&coords[0], sizeof coords) == sizeof coords;
  125. }
  126. bool Serializer::WriteMatrix3(const Matrix3& value)
  127. {
  128. return Write(value.Data(), sizeof value) == sizeof value;
  129. }
  130. bool Serializer::WriteMatrix3x4(const Matrix3x4& value)
  131. {
  132. return Write(value.Data(), sizeof value) == sizeof value;
  133. }
  134. bool Serializer::WriteMatrix4(const Matrix4& value)
  135. {
  136. return Write(value.Data(), sizeof value) == sizeof value;
  137. }
  138. bool Serializer::WriteColor(const Color& value)
  139. {
  140. return Write(value.Data(), sizeof value) == sizeof value;
  141. }
  142. bool Serializer::WriteBoundingBox(const BoundingBox& value)
  143. {
  144. bool success = true;
  145. success &= WriteVector3(value.min_);
  146. success &= WriteVector3(value.max_);
  147. return success;
  148. }
  149. bool Serializer::WriteString(const String& value)
  150. {
  151. const char* chars = value.CString();
  152. // Count length to the first zero, because ReadString() does the same
  153. unsigned length = String::CStringLength(chars);
  154. return Write(chars, length + 1) == length + 1;
  155. }
  156. bool Serializer::WriteFileID(const String& value)
  157. {
  158. bool success = true;
  159. unsigned length = Min(value.Length(), 4U);
  160. success &= Write(value.CString(), length) == length;
  161. for (unsigned i = value.Length(); i < 4; ++i)
  162. success &= WriteByte(' ');
  163. return success;
  164. }
  165. bool Serializer::WriteStringHash(const StringHash& value)
  166. {
  167. return WriteUInt(value.Value());
  168. }
  169. bool Serializer::WriteBuffer(const PODVector<unsigned char>& value)
  170. {
  171. bool success = true;
  172. unsigned size = value.Size();
  173. success &= WriteVLE(size);
  174. if (size)
  175. success &= Write(&value[0], size) == size;
  176. return success;
  177. }
  178. bool Serializer::WriteResourceRef(const ResourceRef& value)
  179. {
  180. bool success = true;
  181. success &= WriteStringHash(value.type_);
  182. success &= WriteString(value.name_);
  183. return success;
  184. }
  185. bool Serializer::WriteResourceRefList(const ResourceRefList& value)
  186. {
  187. bool success = true;
  188. success &= WriteStringHash(value.type_);
  189. success &= WriteVLE(value.names_.Size());
  190. for (unsigned i = 0; i < value.names_.Size(); ++i)
  191. success &= WriteString(value.names_[i]);
  192. return success;
  193. }
  194. bool Serializer::WriteVariant(const Variant& value)
  195. {
  196. bool success = true;
  197. VariantType type = value.GetType();
  198. success &= WriteUByte((unsigned char)type);
  199. success &= WriteVariantData(value);
  200. return success;
  201. }
  202. bool Serializer::WriteVariantData(const Variant& value)
  203. {
  204. switch (value.GetType())
  205. {
  206. case VAR_NONE:
  207. return true;
  208. case VAR_INT:
  209. return WriteInt(value.GetInt());
  210. case VAR_INT64:
  211. return WriteInt64(value.GetInt64());
  212. case VAR_BOOL:
  213. return WriteBool(value.GetBool());
  214. case VAR_FLOAT:
  215. return WriteFloat(value.GetFloat());
  216. case VAR_VECTOR2:
  217. return WriteVector2(value.GetVector2());
  218. case VAR_VECTOR3:
  219. return WriteVector3(value.GetVector3());
  220. case VAR_VECTOR4:
  221. return WriteVector4(value.GetVector4());
  222. case VAR_QUATERNION:
  223. return WriteQuaternion(value.GetQuaternion());
  224. case VAR_COLOR:
  225. return WriteColor(value.GetColor());
  226. case VAR_STRING:
  227. return WriteString(value.GetString());
  228. case VAR_BUFFER:
  229. return WriteBuffer(value.GetBuffer());
  230. // Serializing pointers is not supported. Write null
  231. case VAR_VOIDPTR:
  232. case VAR_PTR:
  233. return WriteUInt(0);
  234. case VAR_RESOURCEREF:
  235. return WriteResourceRef(value.GetResourceRef());
  236. case VAR_RESOURCEREFLIST:
  237. return WriteResourceRefList(value.GetResourceRefList());
  238. case VAR_VARIANTVECTOR:
  239. return WriteVariantVector(value.GetVariantVector());
  240. case VAR_STRINGVECTOR:
  241. return WriteStringVector(value.GetStringVector());
  242. case VAR_VARIANTMAP:
  243. return WriteVariantMap(value.GetVariantMap());
  244. case VAR_INTRECT:
  245. return WriteIntRect(value.GetIntRect());
  246. case VAR_INTVECTOR2:
  247. return WriteIntVector2(value.GetIntVector2());
  248. case VAR_INTVECTOR3:
  249. return WriteIntVector3(value.GetIntVector3());
  250. case VAR_MATRIX3:
  251. return WriteMatrix3(value.GetMatrix3());
  252. case VAR_MATRIX3X4:
  253. return WriteMatrix3x4(value.GetMatrix3x4());
  254. case VAR_MATRIX4:
  255. return WriteMatrix4(value.GetMatrix4());
  256. case VAR_DOUBLE:
  257. return WriteDouble(value.GetDouble());
  258. default:
  259. return false;
  260. }
  261. }
  262. bool Serializer::WriteVariantVector(const VariantVector& value)
  263. {
  264. bool success = true;
  265. success &= WriteVLE(value.Size());
  266. for (VariantVector::ConstIterator i = value.Begin(); i != value.End(); ++i)
  267. success &= WriteVariant(*i);
  268. return success;
  269. }
  270. bool Serializer::WriteStringVector(const StringVector& value)
  271. {
  272. bool success = true;
  273. success &= WriteVLE(value.Size());
  274. for (StringVector::ConstIterator i = value.Begin(); i != value.End(); ++i)
  275. success &= WriteString(*i);
  276. return success;
  277. }
  278. bool Serializer::WriteVariantMap(const VariantMap& value)
  279. {
  280. bool success = true;
  281. success &= WriteVLE(value.Size());
  282. for (VariantMap::ConstIterator i = value.Begin(); i != value.End(); ++i)
  283. {
  284. WriteStringHash(i->first_);
  285. WriteVariant(i->second_);
  286. }
  287. return success;
  288. }
  289. bool Serializer::WriteVLE(unsigned value)
  290. {
  291. unsigned char data[4];
  292. if (value < 0x80)
  293. return WriteUByte((unsigned char)value);
  294. else if (value < 0x4000)
  295. {
  296. data[0] = (unsigned char)(value | 0x80);
  297. data[1] = (unsigned char)(value >> 7);
  298. return Write(data, 2) == 2;
  299. }
  300. else if (value < 0x200000)
  301. {
  302. data[0] = (unsigned char)(value | 0x80);
  303. data[1] = (unsigned char)((value >> 7) | 0x80);
  304. data[2] = (unsigned char)(value >> 14);
  305. return Write(data, 3) == 3;
  306. }
  307. else
  308. {
  309. data[0] = (unsigned char)(value | 0x80);
  310. data[1] = (unsigned char)((value >> 7) | 0x80);
  311. data[2] = (unsigned char)((value >> 14) | 0x80);
  312. data[3] = (unsigned char)(value >> 21);
  313. return Write(data, 4) == 4;
  314. }
  315. }
  316. bool Serializer::WriteNetID(unsigned value)
  317. {
  318. return Write(&value, 3) == 3;
  319. }
  320. bool Serializer::WriteLine(const String& value)
  321. {
  322. bool success = true;
  323. success &= Write(value.CString(), value.Length()) == value.Length();
  324. success &= WriteUByte(13);
  325. success &= WriteUByte(10);
  326. return success;
  327. }
  328. }