Serializer.cpp 9.9 KB

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