Serializer.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. //
  2. // Copyright (c) 2008-2013 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::WriteColor(const Color& value)
  113. {
  114. return Write(value.Data(), sizeof value) == sizeof value;
  115. }
  116. bool Serializer::WriteBoundingBox(const BoundingBox& value)
  117. {
  118. bool success = true;
  119. success &= WriteVector3(value.min_);
  120. success &= WriteVector3(value.max_);
  121. return success;
  122. }
  123. bool Serializer::WriteString(const String& value)
  124. {
  125. const char* chars = value.CString();
  126. // Count length to the first zero, because ReadString() does the same
  127. unsigned length = String::CStringLength(chars);
  128. return Write(chars, length + 1) == length + 1;
  129. }
  130. bool Serializer::WriteFileID(const String& value)
  131. {
  132. bool success = true;
  133. unsigned length = Min((int)value.Length(), 4);
  134. success &= Write(value.CString(), length) == length;
  135. for (unsigned i = value.Length(); i < 4; ++i)
  136. success &= WriteByte(' ');
  137. return success;
  138. }
  139. bool Serializer::WriteStringHash(const StringHash& value)
  140. {
  141. return WriteUInt(value.Value());
  142. }
  143. bool Serializer::WriteShortStringHash(const ShortStringHash& value)
  144. {
  145. return WriteUShort(value.Value());
  146. }
  147. bool Serializer::WriteBuffer(const PODVector<unsigned char>& value)
  148. {
  149. bool success = true;
  150. unsigned size = value.Size();
  151. success &= WriteVLE(size);
  152. if (size)
  153. success &= Write(&value[0], size) == size;
  154. return success;
  155. }
  156. bool Serializer::WriteResourceRef(const ResourceRef& value)
  157. {
  158. bool success = true;
  159. success &= WriteShortStringHash(value.type_);
  160. success &= WriteString(value.name_);
  161. return success;
  162. }
  163. bool Serializer::WriteResourceRefList(const ResourceRefList& value)
  164. {
  165. bool success = true;
  166. unsigned size = value.names_.Size() * sizeof(StringHash);
  167. success &= WriteShortStringHash(value.type_);
  168. success &= WriteVLE(value.names_.Size());
  169. for (unsigned i = 0; i < value.names_.Size(); ++i)
  170. success &= WriteString(value.names_[i]);
  171. return success;
  172. }
  173. bool Serializer::WriteVariant(const Variant& value)
  174. {
  175. bool success = true;
  176. VariantType type = value.GetType();
  177. success &= WriteUByte((unsigned char)type);
  178. success &= WriteVariantData(value);
  179. return success;
  180. }
  181. bool Serializer::WriteVariantData(const Variant& value)
  182. {
  183. switch (value.GetType())
  184. {
  185. case VAR_NONE:
  186. return true;
  187. case VAR_INT:
  188. return WriteInt(value.GetInt());
  189. case VAR_BOOL:
  190. return WriteBool(value.GetBool());
  191. case VAR_FLOAT:
  192. return WriteFloat(value.GetFloat());
  193. case VAR_VECTOR2:
  194. return WriteVector2(value.GetVector2());
  195. case VAR_VECTOR3:
  196. return WriteVector3(value.GetVector3());
  197. case VAR_VECTOR4:
  198. return WriteVector4(value.GetVector4());
  199. case VAR_QUATERNION:
  200. return WriteQuaternion(value.GetQuaternion());
  201. case VAR_COLOR:
  202. return WriteColor(value.GetColor());
  203. case VAR_STRING:
  204. return WriteString(value.GetString());
  205. case VAR_BUFFER:
  206. return WriteBuffer(value.GetBuffer());
  207. case VAR_PTR:
  208. return WriteUInt(0);
  209. case VAR_RESOURCEREF:
  210. return WriteResourceRef(value.GetResourceRef());
  211. case VAR_RESOURCEREFLIST:
  212. return WriteResourceRefList(value.GetResourceRefList());
  213. case VAR_VARIANTVECTOR:
  214. return WriteVariantVector(value.GetVariantVector());
  215. case VAR_VARIANTMAP:
  216. return WriteVariantMap(value.GetVariantMap());
  217. case VAR_INTRECT:
  218. return WriteIntRect(value.GetIntRect());
  219. case VAR_INTVECTOR2:
  220. return WriteIntVector2(value.GetIntVector2());
  221. default:
  222. return false;
  223. }
  224. }
  225. bool Serializer::WriteVariantVector(const VariantVector& value)
  226. {
  227. bool success = true;
  228. success &= WriteVLE(value.Size());
  229. for (VariantVector::ConstIterator i = value.Begin(); i != value.End(); ++i)
  230. success &= WriteVariant(*i);
  231. return success;
  232. }
  233. bool Serializer::WriteVariantMap(const VariantMap& value)
  234. {
  235. bool success = true;
  236. success &= WriteVLE(value.Size());
  237. for (VariantMap::ConstIterator i = value.Begin(); i != value.End(); ++i)
  238. {
  239. WriteShortStringHash(i->first_);
  240. WriteVariant(i->second_);
  241. }
  242. return success;
  243. }
  244. bool Serializer::WriteVLE(unsigned value)
  245. {
  246. unsigned char data[4];
  247. if (value < 0x80)
  248. return WriteUByte(value);
  249. else if (value < 0x4000)
  250. {
  251. data[0] = value | 0x80;
  252. data[1] = value >> 7;
  253. return Write(data, 2) == 2;
  254. }
  255. else if (value < 0x200000)
  256. {
  257. data[0] = value | 0x80;
  258. data[1] = (value >> 7) | 0x80;
  259. data[2] = value >> 14;
  260. return Write(data, 3) == 3;
  261. }
  262. else
  263. {
  264. data[0] = value | 0x80;
  265. data[1] = (value >> 7) | 0x80;
  266. data[2] = (value >> 14) | 0x80;
  267. data[3] = (value >> 21);
  268. return Write(data, 4) == 4;
  269. }
  270. }
  271. bool Serializer::WriteNetID(unsigned value)
  272. {
  273. return Write(&value, 3) == 3;
  274. }
  275. bool Serializer::WriteLine(const String& value)
  276. {
  277. bool success = true;
  278. success &= Write(value.CString(), value.Length()) == value.Length();
  279. success &= WriteUByte(13);
  280. success &= WriteUByte(10);
  281. return success;
  282. }
  283. }