Serializer.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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. 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.GetData(), sizeof value) == sizeof value;
  67. }
  68. bool Serializer::WriteIntVector2(const IntVector2& value)
  69. {
  70. return Write(value.GetData(), sizeof value) == sizeof value;
  71. }
  72. bool Serializer::WriteRect(const Rect& value)
  73. {
  74. return Write(value.GetData(), sizeof value) == sizeof value;
  75. }
  76. bool Serializer::WriteVector2(const Vector2& value)
  77. {
  78. return Write(value.GetData(), sizeof value) == sizeof value;
  79. }
  80. bool Serializer::WriteVector3(const Vector3& value)
  81. {
  82. return Write(value.GetData(), 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.GetData(), sizeof value) == sizeof value;
  96. }
  97. bool Serializer::WriteQuaternion(const Quaternion& value)
  98. {
  99. return Write(value.GetData(), sizeof value) == sizeof value;
  100. }
  101. bool Serializer::WritePackedQuaternion(const Quaternion& value)
  102. {
  103. short coords[4];
  104. Quaternion norm = value.GetNormalized();
  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 value;
  110. }
  111. bool Serializer::WriteColor(const Color& value)
  112. {
  113. return Write(value.GetData(), sizeof value) == sizeof value;
  114. }
  115. bool Serializer::WriteBoundingBox(const BoundingBox& value)
  116. {
  117. bool success = true;
  118. success &= WriteVector3(value.min_);
  119. success &= WriteVector3(value.max_);
  120. return success;
  121. }
  122. bool Serializer::WriteString(const String& value)
  123. {
  124. return Write(value.CString(), value.Length() + 1) == value.Length() + 1;
  125. }
  126. bool Serializer::WriteID(const String& value)
  127. {
  128. bool success = true;
  129. unsigned length = Min((int)value.Length(), 4);
  130. success &= Write(value.CString(), length) == length;
  131. for (unsigned i = value.Length(); i < 4; ++i)
  132. success &= WriteByte(' ');
  133. return success;
  134. }
  135. bool Serializer::WriteStringHash(const StringHash& value)
  136. {
  137. return WriteUInt(value.GetValue());
  138. }
  139. bool Serializer::WriteShortStringHash(const ShortStringHash& value)
  140. {
  141. return WriteUShort(value.GetValue());
  142. }
  143. bool Serializer::WriteBuffer(const Vector<unsigned char>& value)
  144. {
  145. bool success = true;
  146. unsigned size = value.Size();
  147. success &= WriteVLE(size);
  148. if (size)
  149. success &= Write(&value[0], size) == size;
  150. return success;
  151. }
  152. bool Serializer::WriteResourceRef(const ResourceRef& value)
  153. {
  154. bool success = true;
  155. success &= WriteShortStringHash(value.type_);
  156. success &= WriteStringHash(value.id_);
  157. return success;
  158. }
  159. bool Serializer::WriteResourceRefList(const ResourceRefList& value)
  160. {
  161. bool success = true;
  162. unsigned size = value.ids_.Size() * sizeof(StringHash);
  163. success &= WriteShortStringHash(value.type_);
  164. success &= WriteVLE(value.ids_.Size());
  165. if (size)
  166. success &= Write(&value.ids_[0], size) == size;
  167. return success;
  168. }
  169. bool Serializer::WriteVariant(const Variant& value)
  170. {
  171. bool success = true;
  172. VariantType type = value.GetType();
  173. success &= WriteUByte((unsigned char)type);
  174. success &= WriteVariantData(value);
  175. return success;
  176. }
  177. bool Serializer::WriteVariantData(const Variant& value)
  178. {
  179. switch (value.GetType())
  180. {
  181. case VAR_NONE:
  182. return true;
  183. case VAR_INT:
  184. return WriteInt(value.GetInt());
  185. case VAR_BOOL:
  186. return WriteBool(value.GetBool());
  187. case VAR_FLOAT:
  188. return WriteFloat(value.GetFloat());
  189. case VAR_VECTOR2:
  190. return WriteVector2(value.GetVector2());
  191. case VAR_VECTOR3:
  192. return WriteVector3(value.GetVector3());
  193. case VAR_VECTOR4:
  194. return WriteVector4(value.GetVector4());
  195. case VAR_QUATERNION:
  196. return WriteQuaternion(value.GetQuaternion());
  197. case VAR_COLOR:
  198. return WriteColor(value.GetColor());
  199. case VAR_STRING:
  200. return WriteString(value.GetString());
  201. case VAR_BUFFER:
  202. return WriteBuffer(value.GetBuffer());
  203. case VAR_PTR:
  204. return WriteUInt(0);
  205. case VAR_RESOURCEREF:
  206. return WriteResourceRef(value.GetResourceRef());
  207. case VAR_RESOURCEREFLIST:
  208. return WriteResourceRefList(value.GetResourceRefList());
  209. case VAR_VARIANTVECTOR:
  210. return WriteVariantVector(value.GetVariantVector());
  211. case VAR_VARIANTMAP:
  212. return WriteVariantMap(value.GetVariantMap());
  213. }
  214. return false;
  215. }
  216. bool Serializer::WriteVariantVector(const VariantVector& value)
  217. {
  218. bool success = true;
  219. success &= WriteVLE(value.Size());
  220. for (VariantVector::ConstIterator i = value.Begin(); i != value.End(); ++i)
  221. success &= WriteVariant(*i);
  222. return success;
  223. }
  224. bool Serializer::WriteVariantMap(const VariantMap& value)
  225. {
  226. bool success = true;
  227. success &= WriteVLE(value.Size());
  228. for (VariantMap::ConstIterator i = value.Begin(); i != value.End(); ++i)
  229. {
  230. WriteShortStringHash(i->first_);
  231. WriteVariant(i->second_);
  232. }
  233. return success;
  234. }
  235. bool Serializer::WriteVLE(unsigned value)
  236. {
  237. unsigned char data[4];
  238. if (value < 0x80)
  239. return WriteUByte(value);
  240. else if (value < 0x4000)
  241. {
  242. data[0] = value | 0x80;
  243. data[1] = value >> 7;
  244. return Write(data, 2) == 2;
  245. }
  246. else if (value < 0x200000)
  247. {
  248. data[0] = value | 0x80;
  249. data[1] = (value >> 7) | 0x80;
  250. data[2] = value >> 14;
  251. return Write(data, 3) == 3;
  252. }
  253. else
  254. {
  255. data[0] = value | 0x80;
  256. data[1] = (value >> 7) | 0x80;
  257. data[2] = (value >> 14) | 0x80;
  258. data[3] = (value >> 21);
  259. return Write(data, 4) == 4;
  260. }
  261. }
  262. bool Serializer::WriteLine(const String& value)
  263. {
  264. bool success = true;
  265. success &= Write(value.CString(), value.Length()) == value.Length();
  266. success &= WriteUByte(13);
  267. success &= WriteUByte(10);
  268. return success;
  269. }