Serializer.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 "BoundingBox.h"
  24. #include "Serializer.h"
  25. #include "Variant.h"
  26. #include "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 value;
  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. unsigned size = value.names_.Size() * sizeof(StringHash);
  175. success &= WriteStringHash(value.type_);
  176. success &= WriteVLE(value.names_.Size());
  177. for (unsigned i = 0; i < value.names_.Size(); ++i)
  178. success &= WriteString(value.names_[i]);
  179. return success;
  180. }
  181. bool Serializer::WriteVariant(const Variant& value)
  182. {
  183. bool success = true;
  184. VariantType type = value.GetType();
  185. success &= WriteUByte((unsigned char)type);
  186. success &= WriteVariantData(value);
  187. return success;
  188. }
  189. bool Serializer::WriteVariantData(const Variant& value)
  190. {
  191. switch (value.GetType())
  192. {
  193. case VAR_NONE:
  194. return true;
  195. case VAR_INT:
  196. return WriteInt(value.GetInt());
  197. case VAR_BOOL:
  198. return WriteBool(value.GetBool());
  199. case VAR_FLOAT:
  200. return WriteFloat(value.GetFloat());
  201. case VAR_VECTOR2:
  202. return WriteVector2(value.GetVector2());
  203. case VAR_VECTOR3:
  204. return WriteVector3(value.GetVector3());
  205. case VAR_VECTOR4:
  206. return WriteVector4(value.GetVector4());
  207. case VAR_QUATERNION:
  208. return WriteQuaternion(value.GetQuaternion());
  209. case VAR_COLOR:
  210. return WriteColor(value.GetColor());
  211. case VAR_STRING:
  212. return WriteString(value.GetString());
  213. case VAR_BUFFER:
  214. return WriteBuffer(value.GetBuffer());
  215. // Serializing pointers is not supported. Write null
  216. case VAR_VOIDPTR:
  217. case VAR_PTR:
  218. return WriteUInt(0);
  219. case VAR_RESOURCEREF:
  220. return WriteResourceRef(value.GetResourceRef());
  221. case VAR_RESOURCEREFLIST:
  222. return WriteResourceRefList(value.GetResourceRefList());
  223. case VAR_VARIANTVECTOR:
  224. return WriteVariantVector(value.GetVariantVector());
  225. case VAR_VARIANTMAP:
  226. return WriteVariantMap(value.GetVariantMap());
  227. case VAR_INTRECT:
  228. return WriteIntRect(value.GetIntRect());
  229. case VAR_INTVECTOR2:
  230. return WriteIntVector2(value.GetIntVector2());
  231. case VAR_MATRIX3:
  232. return WriteMatrix3(value.GetMatrix3());
  233. case VAR_MATRIX3X4:
  234. return WriteMatrix3x4(value.GetMatrix3x4());
  235. case VAR_MATRIX4:
  236. return WriteMatrix4(value.GetMatrix4());
  237. default:
  238. return false;
  239. }
  240. }
  241. bool Serializer::WriteVariantVector(const VariantVector& value)
  242. {
  243. bool success = true;
  244. success &= WriteVLE(value.Size());
  245. for (VariantVector::ConstIterator i = value.Begin(); i != value.End(); ++i)
  246. success &= WriteVariant(*i);
  247. return success;
  248. }
  249. bool Serializer::WriteVariantMap(const VariantMap& value)
  250. {
  251. bool success = true;
  252. success &= WriteVLE(value.Size());
  253. for (VariantMap::ConstIterator i = value.Begin(); i != value.End(); ++i)
  254. {
  255. WriteStringHash(i->first_);
  256. WriteVariant(i->second_);
  257. }
  258. return success;
  259. }
  260. bool Serializer::WriteVLE(unsigned value)
  261. {
  262. unsigned char data[4];
  263. if (value < 0x80)
  264. return WriteUByte(value);
  265. else if (value < 0x4000)
  266. {
  267. data[0] = value | 0x80;
  268. data[1] = value >> 7;
  269. return Write(data, 2) == 2;
  270. }
  271. else if (value < 0x200000)
  272. {
  273. data[0] = value | 0x80;
  274. data[1] = (value >> 7) | 0x80;
  275. data[2] = value >> 14;
  276. return Write(data, 3) == 3;
  277. }
  278. else
  279. {
  280. data[0] = value | 0x80;
  281. data[1] = (value >> 7) | 0x80;
  282. data[2] = (value >> 14) | 0x80;
  283. data[3] = (value >> 21);
  284. return Write(data, 4) == 4;
  285. }
  286. }
  287. bool Serializer::WriteNetID(unsigned value)
  288. {
  289. return Write(&value, 3) == 3;
  290. }
  291. bool Serializer::WriteLine(const String& value)
  292. {
  293. bool success = true;
  294. success &= Write(value.CString(), value.Length()) == value.Length();
  295. success &= WriteUByte(13);
  296. success &= WriteUByte(10);
  297. return success;
  298. }
  299. }