Serializer.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "BoundingBox.h"
  25. #include "Serializer.h"
  26. #include "Variant.h"
  27. #include "DebugNew.h"
  28. namespace Urho3D
  29. {
  30. static const float q = 32767.0f;
  31. Serializer::~Serializer()
  32. {
  33. }
  34. bool Serializer::WriteInt(int value)
  35. {
  36. return Write(&value, sizeof value) == sizeof value;
  37. }
  38. bool Serializer::WriteShort(short value)
  39. {
  40. return Write(&value, sizeof value) == sizeof value;
  41. }
  42. bool Serializer::WriteByte(signed char value)
  43. {
  44. return Write(&value, sizeof value) == sizeof value;
  45. }
  46. bool Serializer::WriteUInt(unsigned value)
  47. {
  48. return Write(&value, sizeof value) == sizeof value;
  49. }
  50. bool Serializer::WriteUShort(unsigned short value)
  51. {
  52. return Write(&value, sizeof value) == sizeof value;
  53. }
  54. bool Serializer::WriteUByte(unsigned char value)
  55. {
  56. return Write(&value, sizeof value) == sizeof value;
  57. }
  58. bool Serializer::WriteBool(bool value)
  59. {
  60. return WriteUByte(value ? 1 : 0) == 1;
  61. }
  62. bool Serializer::WriteFloat(float value)
  63. {
  64. return Write(&value, sizeof value) == sizeof value;
  65. }
  66. bool Serializer::WriteIntRect(const IntRect& value)
  67. {
  68. return Write(value.Data(), sizeof value) == sizeof value;
  69. }
  70. bool Serializer::WriteIntVector2(const IntVector2& value)
  71. {
  72. return Write(value.Data(), sizeof value) == sizeof value;
  73. }
  74. bool Serializer::WriteRect(const Rect& value)
  75. {
  76. return Write(value.Data(), sizeof value) == sizeof value;
  77. }
  78. bool Serializer::WriteVector2(const Vector2& value)
  79. {
  80. return Write(value.Data(), sizeof value) == sizeof value;
  81. }
  82. bool Serializer::WriteVector3(const Vector3& value)
  83. {
  84. return Write(value.Data(), sizeof value) == sizeof value;
  85. }
  86. bool Serializer::WritePackedVector3(const Vector3& value, float maxAbsCoord)
  87. {
  88. short coords[3];
  89. float v = 32767.0f / maxAbsCoord;
  90. coords[0] = (short)(Clamp(value.x_, -maxAbsCoord, maxAbsCoord) * v + 0.5f);
  91. coords[1] = (short)(Clamp(value.y_, -maxAbsCoord, maxAbsCoord) * v + 0.5f);
  92. coords[2] = (short)(Clamp(value.z_, -maxAbsCoord, maxAbsCoord) * v + 0.5f);
  93. return Write(&coords[0], sizeof coords) == sizeof coords;
  94. }
  95. bool Serializer::WriteVector4(const Vector4& value)
  96. {
  97. return Write(value.Data(), sizeof value) == sizeof value;
  98. }
  99. bool Serializer::WriteQuaternion(const Quaternion& value)
  100. {
  101. return Write(value.Data(), sizeof value) == sizeof value;
  102. }
  103. bool Serializer::WritePackedQuaternion(const Quaternion& value)
  104. {
  105. short coords[4];
  106. Quaternion norm = value.Normalized();
  107. coords[0] = (short)(Clamp(norm.w_, -1.0f, 1.0f) * q + 0.5f);
  108. coords[1] = (short)(Clamp(norm.x_, -1.0f, 1.0f) * q + 0.5f);
  109. coords[2] = (short)(Clamp(norm.y_, -1.0f, 1.0f) * q + 0.5f);
  110. coords[3] = (short)(Clamp(norm.z_, -1.0f, 1.0f) * q + 0.5f);
  111. return Write(&coords[0], sizeof coords) == sizeof value;
  112. }
  113. bool Serializer::WriteColor(const Color& value)
  114. {
  115. return Write(value.Data(), sizeof value) == sizeof value;
  116. }
  117. bool Serializer::WriteBoundingBox(const BoundingBox& value)
  118. {
  119. bool success = true;
  120. success &= WriteVector3(value.min_);
  121. success &= WriteVector3(value.max_);
  122. return success;
  123. }
  124. bool Serializer::WriteString(const String& value)
  125. {
  126. const char* chars = value.CString();
  127. // Count length to the first zero, because ReadString() does the same
  128. unsigned length = strlen(chars);
  129. return Write(chars, length + 1) == length + 1;
  130. }
  131. bool Serializer::WriteFileID(const String& value)
  132. {
  133. bool success = true;
  134. unsigned length = Min((int)value.Length(), 4);
  135. success &= Write(value.CString(), length) == length;
  136. for (unsigned i = value.Length(); i < 4; ++i)
  137. success &= WriteByte(' ');
  138. return success;
  139. }
  140. bool Serializer::WriteStringHash(const StringHash& value)
  141. {
  142. return WriteUInt(value.Value());
  143. }
  144. bool Serializer::WriteShortStringHash(const ShortStringHash& value)
  145. {
  146. return WriteUShort(value.Value());
  147. }
  148. bool Serializer::WriteBuffer(const PODVector<unsigned char>& value)
  149. {
  150. bool success = true;
  151. unsigned size = value.Size();
  152. success &= WriteVLE(size);
  153. if (size)
  154. success &= Write(&value[0], size) == size;
  155. return success;
  156. }
  157. bool Serializer::WriteResourceRef(const ResourceRef& value)
  158. {
  159. bool success = true;
  160. success &= WriteShortStringHash(value.type_);
  161. success &= WriteStringHash(value.id_);
  162. return success;
  163. }
  164. bool Serializer::WriteResourceRefList(const ResourceRefList& value)
  165. {
  166. bool success = true;
  167. unsigned size = value.ids_.Size() * sizeof(StringHash);
  168. success &= WriteShortStringHash(value.type_);
  169. success &= WriteVLE(value.ids_.Size());
  170. if (size)
  171. success &= Write(&value.ids_[0], size) == size;
  172. return success;
  173. }
  174. bool Serializer::WriteVariant(const Variant& value)
  175. {
  176. bool success = true;
  177. VariantType type = value.GetType();
  178. success &= WriteUByte((unsigned char)type);
  179. success &= WriteVariantData(value);
  180. return success;
  181. }
  182. bool Serializer::WriteVariantData(const Variant& value)
  183. {
  184. switch (value.GetType())
  185. {
  186. case VAR_NONE:
  187. return true;
  188. case VAR_INT:
  189. return WriteInt(value.GetInt());
  190. case VAR_BOOL:
  191. return WriteBool(value.GetBool());
  192. case VAR_FLOAT:
  193. return WriteFloat(value.GetFloat());
  194. case VAR_VECTOR2:
  195. return WriteVector2(value.GetVector2());
  196. case VAR_VECTOR3:
  197. return WriteVector3(value.GetVector3());
  198. case VAR_VECTOR4:
  199. return WriteVector4(value.GetVector4());
  200. case VAR_QUATERNION:
  201. return WriteQuaternion(value.GetQuaternion());
  202. case VAR_COLOR:
  203. return WriteColor(value.GetColor());
  204. case VAR_STRING:
  205. return WriteString(value.GetString());
  206. case VAR_BUFFER:
  207. return WriteBuffer(value.GetBuffer());
  208. case VAR_PTR:
  209. return WriteUInt(0);
  210. case VAR_RESOURCEREF:
  211. return WriteResourceRef(value.GetResourceRef());
  212. case VAR_RESOURCEREFLIST:
  213. return WriteResourceRefList(value.GetResourceRefList());
  214. case VAR_VARIANTVECTOR:
  215. return WriteVariantVector(value.GetVariantVector());
  216. case VAR_VARIANTMAP:
  217. return WriteVariantMap(value.GetVariantMap());
  218. case VAR_INTRECT:
  219. return WriteIntRect(value.GetIntRect());
  220. case VAR_INTVECTOR2:
  221. return WriteIntVector2(value.GetIntVector2());
  222. }
  223. return false;
  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. }