Serializer.cpp 9.8 KB

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