Serializer.cpp 11 KB

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