Serializer.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. //
  2. // Copyright (c) 2008-2020 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 Urho3D
  26. {
  27. static const float q = 32767.0f;
  28. Serializer::~Serializer() = default;
  29. bool Serializer::WriteInt64(long long value)
  30. {
  31. return Write(&value, sizeof value) == sizeof value;
  32. }
  33. bool Serializer::WriteInt(int value)
  34. {
  35. return Write(&value, sizeof value) == sizeof value;
  36. }
  37. bool Serializer::WriteShort(short value)
  38. {
  39. return Write(&value, sizeof value) == sizeof value;
  40. }
  41. bool Serializer::WriteByte(signed char value)
  42. {
  43. return Write(&value, sizeof value) == sizeof value;
  44. }
  45. bool Serializer::WriteUInt64(unsigned long long value)
  46. {
  47. return Write(&value, sizeof value) == sizeof value;
  48. }
  49. bool Serializer::WriteUInt(unsigned value)
  50. {
  51. return Write(&value, sizeof value) == sizeof value;
  52. }
  53. bool Serializer::WriteUShort(unsigned short value)
  54. {
  55. return Write(&value, sizeof value) == sizeof value;
  56. }
  57. bool Serializer::WriteUByte(unsigned char value)
  58. {
  59. return Write(&value, sizeof value) == sizeof value;
  60. }
  61. bool Serializer::WriteBool(bool value)
  62. {
  63. return WriteUByte((unsigned char)(value ? 1 : 0)) == 1;
  64. }
  65. bool Serializer::WriteFloat(float value)
  66. {
  67. return Write(&value, sizeof value) == sizeof value;
  68. }
  69. bool Serializer::WriteDouble(double value)
  70. {
  71. return Write(&value, sizeof value) == sizeof value;
  72. }
  73. bool Serializer::WriteIntRect(const IntRect& value)
  74. {
  75. return Write(value.Data(), sizeof value) == sizeof value;
  76. }
  77. bool Serializer::WriteIntVector2(const IntVector2& value)
  78. {
  79. return Write(value.Data(), sizeof value) == sizeof value;
  80. }
  81. bool Serializer::WriteIntVector3(const IntVector3& value)
  82. {
  83. return Write(value.Data(), sizeof value) == sizeof value;
  84. }
  85. bool Serializer::WriteRect(const Rect& value)
  86. {
  87. return Write(value.Data(), sizeof value) == sizeof value;
  88. }
  89. bool Serializer::WriteVector2(const Vector2& value)
  90. {
  91. return Write(value.Data(), sizeof value) == sizeof value;
  92. }
  93. bool Serializer::WriteVector3(const Vector3& value)
  94. {
  95. return Write(value.Data(), sizeof value) == sizeof value;
  96. }
  97. bool Serializer::WritePackedVector3(const Vector3& value, float maxAbsCoord)
  98. {
  99. short coords[3];
  100. float v = 32767.0f / maxAbsCoord;
  101. coords[0] = (short)Round(Clamp(value.x_, -maxAbsCoord, maxAbsCoord) * v);
  102. coords[1] = (short)Round(Clamp(value.y_, -maxAbsCoord, maxAbsCoord) * v);
  103. coords[2] = (short)Round(Clamp(value.z_, -maxAbsCoord, maxAbsCoord) * v);
  104. return Write(&coords[0], sizeof coords) == sizeof coords;
  105. }
  106. bool Serializer::WriteVector4(const Vector4& value)
  107. {
  108. return Write(value.Data(), sizeof value) == sizeof value;
  109. }
  110. bool Serializer::WriteQuaternion(const Quaternion& value)
  111. {
  112. return Write(value.Data(), sizeof value) == sizeof value;
  113. }
  114. bool Serializer::WritePackedQuaternion(const Quaternion& value)
  115. {
  116. short coords[4];
  117. Quaternion norm = value.Normalized();
  118. coords[0] = (short)Round(Clamp(norm.w_, -1.0f, 1.0f) * q);
  119. coords[1] = (short)Round(Clamp(norm.x_, -1.0f, 1.0f) * q);
  120. coords[2] = (short)Round(Clamp(norm.y_, -1.0f, 1.0f) * q);
  121. coords[3] = (short)Round(Clamp(norm.z_, -1.0f, 1.0f) * q);
  122. return Write(&coords[0], sizeof coords) == sizeof coords;
  123. }
  124. bool Serializer::WriteMatrix3(const Matrix3& value)
  125. {
  126. return Write(value.Data(), sizeof value) == sizeof value;
  127. }
  128. bool Serializer::WriteMatrix3x4(const Matrix3x4& value)
  129. {
  130. return Write(value.Data(), sizeof value) == sizeof value;
  131. }
  132. bool Serializer::WriteMatrix4(const Matrix4& value)
  133. {
  134. return Write(value.Data(), sizeof value) == sizeof value;
  135. }
  136. bool Serializer::WriteColor(const Color& value)
  137. {
  138. return Write(value.Data(), sizeof value) == sizeof value;
  139. }
  140. bool Serializer::WriteBoundingBox(const BoundingBox& value)
  141. {
  142. bool success = true;
  143. success &= WriteVector3(value.min_);
  144. success &= WriteVector3(value.max_);
  145. return success;
  146. }
  147. bool Serializer::WriteString(const String& value)
  148. {
  149. const char* chars = value.CString();
  150. // Count length to the first zero, because ReadString() does the same
  151. unsigned length = String::CStringLength(chars);
  152. return Write(chars, length + 1) == length + 1;
  153. }
  154. bool Serializer::WriteFileID(const String& value)
  155. {
  156. bool success = true;
  157. unsigned length = Min(value.Length(), 4U);
  158. success &= Write(value.CString(), length) == length;
  159. for (unsigned i = value.Length(); i < 4; ++i)
  160. success &= WriteByte(' ');
  161. return success;
  162. }
  163. bool Serializer::WriteStringHash(const StringHash& value)
  164. {
  165. return WriteUInt(value.Value());
  166. }
  167. bool Serializer::WriteBuffer(const PODVector<unsigned char>& value)
  168. {
  169. bool success = true;
  170. unsigned size = value.Size();
  171. success &= WriteVLE(size);
  172. if (size)
  173. success &= Write(&value[0], size) == size;
  174. return success;
  175. }
  176. bool Serializer::WriteResourceRef(const ResourceRef& value)
  177. {
  178. bool success = true;
  179. success &= WriteStringHash(value.type_);
  180. success &= WriteString(value.name_);
  181. return success;
  182. }
  183. bool Serializer::WriteResourceRefList(const ResourceRefList& value)
  184. {
  185. bool success = true;
  186. success &= WriteStringHash(value.type_);
  187. success &= WriteVLE(value.names_.Size());
  188. for (unsigned i = 0; i < value.names_.Size(); ++i)
  189. success &= WriteString(value.names_[i]);
  190. return success;
  191. }
  192. bool Serializer::WriteVariant(const Variant& value)
  193. {
  194. bool success = true;
  195. VariantType type = value.GetType();
  196. success &= WriteUByte((unsigned char)type);
  197. success &= WriteVariantData(value);
  198. return success;
  199. }
  200. bool Serializer::WriteVariantData(const Variant& value)
  201. {
  202. switch (value.GetType())
  203. {
  204. case VAR_NONE:
  205. return true;
  206. case VAR_INT:
  207. return WriteInt(value.GetInt());
  208. case VAR_INT64:
  209. return WriteInt64(value.GetInt64());
  210. case VAR_BOOL:
  211. return WriteBool(value.GetBool());
  212. case VAR_FLOAT:
  213. return WriteFloat(value.GetFloat());
  214. case VAR_VECTOR2:
  215. return WriteVector2(value.GetVector2());
  216. case VAR_VECTOR3:
  217. return WriteVector3(value.GetVector3());
  218. case VAR_VECTOR4:
  219. return WriteVector4(value.GetVector4());
  220. case VAR_QUATERNION:
  221. return WriteQuaternion(value.GetQuaternion());
  222. case VAR_COLOR:
  223. return WriteColor(value.GetColor());
  224. case VAR_STRING:
  225. return WriteString(value.GetString());
  226. case VAR_BUFFER:
  227. return WriteBuffer(value.GetBuffer());
  228. // Serializing pointers and custom values is not supported. Write null
  229. case VAR_VOIDPTR:
  230. case VAR_PTR:
  231. case VAR_CUSTOM_HEAP:
  232. case VAR_CUSTOM_STACK:
  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 | 0x80u);
  297. data[1] = (unsigned char)(value >> 7u);
  298. return Write(data, 2) == 2;
  299. }
  300. else if (value < 0x200000)
  301. {
  302. data[0] = (unsigned char)(value | 0x80u);
  303. data[1] = (unsigned char)(value >> 7u | 0x80u);
  304. data[2] = (unsigned char)(value >> 14u);
  305. return Write(data, 3) == 3;
  306. }
  307. else
  308. {
  309. data[0] = (unsigned char)(value | 0x80u);
  310. data[1] = (unsigned char)(value >> 7u | 0x80u);
  311. data[2] = (unsigned char)(value >> 14u | 0x80u);
  312. data[3] = (unsigned char)(value >> 21u);
  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. }