Serializer.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. Serializer::~Serializer()
  29. {
  30. }
  31. void Serializer::writeInt(int value)
  32. {
  33. write(&value, sizeof value);
  34. }
  35. void Serializer::writeShort(short value)
  36. {
  37. write(&value, sizeof value);
  38. }
  39. void Serializer::writeByte(signed char value)
  40. {
  41. write(&value, sizeof value);
  42. }
  43. void Serializer::writeUInt(unsigned value)
  44. {
  45. write(&value, sizeof value);
  46. }
  47. void Serializer::writeUShort(unsigned short value)
  48. {
  49. write(&value, sizeof value);
  50. }
  51. void Serializer::writeUByte(unsigned char value)
  52. {
  53. write(&value, sizeof value);
  54. }
  55. void Serializer::writeBool(bool value)
  56. {
  57. if (value)
  58. writeUByte(1);
  59. else
  60. writeUByte(0);
  61. }
  62. void Serializer::writeFloat(float value)
  63. {
  64. write(&value, sizeof value);
  65. }
  66. void Serializer::writeIntRect(const IntRect& value)
  67. {
  68. write(value.getData(), sizeof value);
  69. }
  70. void Serializer::writeIntVector2(const IntVector2& value)
  71. {
  72. write(value.getData(), sizeof value);
  73. }
  74. void Serializer::writeRect(const Rect& value)
  75. {
  76. write(value.getData(), sizeof value);
  77. }
  78. void Serializer::writeVector2(const Vector2& value)
  79. {
  80. write(value.getData(), sizeof value);
  81. }
  82. void Serializer::writeVector3(const Vector3& value)
  83. {
  84. write(value.getData(), sizeof value);
  85. }
  86. void Serializer::writePackedVector3(const Vector3& value, float maxAbsCoord)
  87. {
  88. float v = 32767.0f / maxAbsCoord;
  89. short coords[3];
  90. coords[0] = (short)(clamp(value.mX, -maxAbsCoord, maxAbsCoord) * v + 0.5f);
  91. coords[1] = (short)(clamp(value.mY, -maxAbsCoord, maxAbsCoord) * v + 0.5f);
  92. coords[2] = (short)(clamp(value.mZ, -maxAbsCoord, maxAbsCoord) * v + 0.5f);
  93. write(&coords[0], sizeof coords);
  94. }
  95. void Serializer::writeVector4(const Vector4& value)
  96. {
  97. write(value.getData(), sizeof value);
  98. }
  99. void Serializer::writeQuaternion(const Quaternion& value)
  100. {
  101. write(value.getData(), sizeof value);
  102. }
  103. void Serializer::writePackedQuaternion(const Quaternion& value)
  104. {
  105. static const float q = 32767.0f;
  106. short coords[4];
  107. Quaternion norm = value.getNormalized();
  108. coords[0] = (short)(clamp(norm.mW, -1.0f, 1.0f) * q + 0.5f);
  109. coords[1] = (short)(clamp(norm.mX, -1.0f, 1.0f) * q + 0.5f);
  110. coords[2] = (short)(clamp(norm.mY, -1.0f, 1.0f) * q + 0.5f);
  111. coords[3] = (short)(clamp(norm.mZ, -1.0f, 1.0f) * q + 0.5f);
  112. write(&coords[0], sizeof coords);
  113. }
  114. void Serializer::writeColor(const Color& value)
  115. {
  116. write(value.getData(), sizeof value);
  117. }
  118. void Serializer::writeBoundingBox(const BoundingBox& value)
  119. {
  120. writeVector3(value.mMin);
  121. writeVector3(value.mMax);
  122. }
  123. void Serializer::writeString(const std::string& value)
  124. {
  125. write(value.c_str(), value.length() + 1);
  126. }
  127. void Serializer::writeID(const std::string& value)
  128. {
  129. write(value.c_str(), min((int)value.length(), 4));
  130. for (unsigned i = value.length(); i < 4; ++i)
  131. writeByte(' ');
  132. }
  133. void Serializer::writeStringHash(const StringHash& value)
  134. {
  135. write(value.getData(), sizeof(unsigned));
  136. }
  137. void Serializer::writeShortStringHash(const ShortStringHash& value)
  138. {
  139. write(value.getData(), sizeof(unsigned short));
  140. }
  141. void Serializer::writeVariant(const Variant& value)
  142. {
  143. value.write(*this);
  144. }
  145. void Serializer::writeVariantVector(const VariantVector& value)
  146. {
  147. writeVLE(value.size());
  148. for (VariantVector::const_iterator i = value.begin(); i != value.end(); ++i)
  149. writeVariant(*i);
  150. }
  151. void Serializer::writeVariantMap(const VariantMap& value)
  152. {
  153. writeVLE(value.size());
  154. for (VariantMap::const_iterator i = value.begin(); i != value.end(); ++i)
  155. {
  156. writeShortStringHash(i->first);
  157. writeVariant(i->second);
  158. }
  159. }
  160. void Serializer::writeVLE(unsigned value)
  161. {
  162. if (value < 0x80)
  163. {
  164. writeUByte(value);
  165. }
  166. else if (value < 0x4000)
  167. {
  168. writeUByte(value | 0x80);
  169. writeUByte(value >> 7);
  170. }
  171. else if (value < 0x200000)
  172. {
  173. writeUByte(value | 0x80);
  174. writeUByte((value >> 7) | 0x80);
  175. writeUByte(value >> 14);
  176. }
  177. else
  178. {
  179. writeUByte(value | 0x80);
  180. writeUByte((value >> 7) | 0x80);
  181. writeUByte((value >> 14) | 0x80);
  182. writeUByte(value >> 21);
  183. }
  184. }
  185. void Serializer::writeLine(const std::string& value)
  186. {
  187. write(value.c_str(), value.length());
  188. writeUByte(13);
  189. writeUByte(10);
  190. }