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