Serializer.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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::WriteShortStringHash(const ShortStringHash& value)
  156. {
  157. return WriteUShort(value.Value());
  158. }
  159. bool Serializer::WriteBuffer(const PODVector<unsigned char>& value)
  160. {
  161. bool success = true;
  162. unsigned size = value.Size();
  163. success &= WriteVLE(size);
  164. if (size)
  165. success &= Write(&value[0], size) == size;
  166. return success;
  167. }
  168. bool Serializer::WriteResourceRef(const ResourceRef& value)
  169. {
  170. bool success = true;
  171. success &= WriteShortStringHash(value.type_);
  172. success &= WriteString(value.name_);
  173. return success;
  174. }
  175. bool Serializer::WriteResourceRefList(const ResourceRefList& value)
  176. {
  177. bool success = true;
  178. unsigned size = value.names_.Size() * sizeof(StringHash);
  179. success &= WriteShortStringHash(value.type_);
  180. success &= WriteVLE(value.names_.Size());
  181. for (unsigned i = 0; i < value.names_.Size(); ++i)
  182. success &= WriteString(value.names_[i]);
  183. return success;
  184. }
  185. bool Serializer::WriteVariant(const Variant& value)
  186. {
  187. bool success = true;
  188. VariantType type = value.GetType();
  189. success &= WriteUByte((unsigned char)type);
  190. success &= WriteVariantData(value);
  191. return success;
  192. }
  193. bool Serializer::WriteVariantData(const Variant& value)
  194. {
  195. switch (value.GetType())
  196. {
  197. case VAR_NONE:
  198. return true;
  199. case VAR_INT:
  200. return WriteInt(value.GetInt());
  201. case VAR_BOOL:
  202. return WriteBool(value.GetBool());
  203. case VAR_FLOAT:
  204. return WriteFloat(value.GetFloat());
  205. case VAR_VECTOR2:
  206. return WriteVector2(value.GetVector2());
  207. case VAR_VECTOR3:
  208. return WriteVector3(value.GetVector3());
  209. case VAR_VECTOR4:
  210. return WriteVector4(value.GetVector4());
  211. case VAR_QUATERNION:
  212. return WriteQuaternion(value.GetQuaternion());
  213. case VAR_COLOR:
  214. return WriteColor(value.GetColor());
  215. case VAR_STRING:
  216. return WriteString(value.GetString());
  217. case VAR_BUFFER:
  218. return WriteBuffer(value.GetBuffer());
  219. // Serializing pointers is not supported. Write null
  220. case VAR_VOIDPTR:
  221. case VAR_PTR:
  222. return WriteUInt(0);
  223. case VAR_RESOURCEREF:
  224. return WriteResourceRef(value.GetResourceRef());
  225. case VAR_RESOURCEREFLIST:
  226. return WriteResourceRefList(value.GetResourceRefList());
  227. case VAR_VARIANTVECTOR:
  228. return WriteVariantVector(value.GetVariantVector());
  229. case VAR_VARIANTMAP:
  230. return WriteVariantMap(value.GetVariantMap());
  231. case VAR_INTRECT:
  232. return WriteIntRect(value.GetIntRect());
  233. case VAR_INTVECTOR2:
  234. return WriteIntVector2(value.GetIntVector2());
  235. case VAR_MATRIX3:
  236. return WriteMatrix3(value.GetMatrix3());
  237. case VAR_MATRIX3X4:
  238. return WriteMatrix3x4(value.GetMatrix3x4());
  239. case VAR_MATRIX4:
  240. return WriteMatrix4(value.GetMatrix4());
  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. WriteShortStringHash(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. }