Serializer.cpp 9.0 KB

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