Serializable.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 "Context.h"
  25. #include "Deserializer.h"
  26. #include "Log.h"
  27. #include "Serializable.h"
  28. #include "Serializer.h"
  29. #include "StringUtils.h"
  30. #include "XMLElement.h"
  31. #include <cstring>
  32. #include "DebugNew.h"
  33. OBJECTTYPESTATIC(Serializable);
  34. Serializable::Serializable(Context* context) :
  35. Object(context),
  36. inSerialization_(false),
  37. inNetwork_(false)
  38. {
  39. }
  40. Serializable::~Serializable()
  41. {
  42. }
  43. void Serializable::OnSetAttribute(const AttributeInfo& attr, const Variant& value)
  44. {
  45. // Check for ID-based attribute handling (negative offset); can not be supported in the base implementation
  46. if (attr.offset_ >= ID_ATTRIBUTE_BASE)
  47. return;
  48. // Calculate the destination address
  49. void* dest = reinterpret_cast<unsigned char*>(this) + attr.offset_;
  50. switch (attr.type_)
  51. {
  52. case VAR_INT:
  53. // If enum type, use the low 8 bits only (assume full value to be initialized)
  54. if (attr.enumNames_)
  55. *(reinterpret_cast<unsigned char*>(dest)) = value.GetInt();
  56. else
  57. *(reinterpret_cast<int*>(dest)) = value.GetInt();
  58. break;
  59. case VAR_BOOL:
  60. *(reinterpret_cast<bool*>(dest)) = value.GetBool();
  61. break;
  62. case VAR_FLOAT:
  63. *(reinterpret_cast<float*>(dest)) = value.GetFloat();
  64. break;
  65. case VAR_VECTOR2:
  66. *(reinterpret_cast<Vector2*>(dest)) = value.GetVector2();
  67. break;
  68. case VAR_VECTOR3:
  69. *(reinterpret_cast<Vector3*>(dest)) = value.GetVector3();
  70. break;
  71. case VAR_VECTOR4:
  72. *(reinterpret_cast<Vector4*>(dest)) = value.GetVector4();
  73. break;
  74. case VAR_QUATERNION:
  75. *(reinterpret_cast<Quaternion*>(dest)) = value.GetQuaternion();
  76. break;
  77. case VAR_COLOR:
  78. *(reinterpret_cast<Color*>(dest)) = value.GetColor();
  79. break;
  80. case VAR_STRING:
  81. *(reinterpret_cast<std::string*>(dest)) = value.GetString();
  82. break;
  83. case VAR_BUFFER:
  84. *(reinterpret_cast<std::vector<unsigned char>*>(dest)) = value.GetBuffer();
  85. break;
  86. case VAR_PTR:
  87. *(reinterpret_cast<void**>(dest)) = value.GetPtr();
  88. break;
  89. case VAR_RESOURCEREF:
  90. *(reinterpret_cast<ResourceRef*>(dest)) = value.GetResourceRef();
  91. break;
  92. case VAR_RESOURCEREFLIST:
  93. *(reinterpret_cast<ResourceRefList*>(dest)) = value.GetResourceRefList();
  94. break;
  95. case VAR_VARIANTVECTOR:
  96. *(reinterpret_cast<VariantVector*>(dest)) = value.GetVariantVector();
  97. break;
  98. case VAR_VARIANTMAP:
  99. *(reinterpret_cast<VariantMap*>(dest)) = value.GetVariantMap();
  100. break;
  101. }
  102. }
  103. Variant Serializable::OnGetAttribute(const AttributeInfo& attr)
  104. {
  105. // Check for ID-based attribute handling (negative offset); can not be supported in the base implementation
  106. if (attr.offset_ >= ID_ATTRIBUTE_BASE)
  107. return Variant();
  108. // Calculate the source address
  109. void* src = reinterpret_cast<unsigned char*>(this) + attr.offset_;
  110. switch (attr.type_)
  111. {
  112. case VAR_INT:
  113. // If enum type, use the low 8 bits only
  114. if (attr.enumNames_)
  115. return Variant(*(reinterpret_cast<const unsigned char*>(src)));
  116. else
  117. return Variant(*(reinterpret_cast<const int*>(src)));
  118. case VAR_BOOL:
  119. return Variant(*(reinterpret_cast<const bool*>(src)));
  120. case VAR_FLOAT:
  121. return Variant(*(reinterpret_cast<const float*>(src)));
  122. case VAR_VECTOR2:
  123. return Variant(*(reinterpret_cast<const Vector2*>(src)));
  124. case VAR_VECTOR3:
  125. return Variant(*(reinterpret_cast<const Vector3*>(src)));
  126. case VAR_VECTOR4:
  127. return Variant(*(reinterpret_cast<const Vector4*>(src)));
  128. case VAR_QUATERNION:
  129. return Variant(*(reinterpret_cast<const Quaternion*>(src)));
  130. case VAR_COLOR:
  131. return Variant(*(reinterpret_cast<const Color*>(src)));
  132. case VAR_STRING:
  133. return Variant(*(reinterpret_cast<const std::string*>(src)));
  134. case VAR_BUFFER:
  135. return Variant(*(reinterpret_cast<const std::vector<unsigned char>*>(src)));
  136. case VAR_PTR:
  137. return Variant(*(reinterpret_cast<void**>(src)));
  138. case VAR_RESOURCEREF:
  139. return Variant(*(reinterpret_cast<const ResourceRef*>(src)));
  140. case VAR_RESOURCEREFLIST:
  141. return Variant(*(reinterpret_cast<const ResourceRefList*>(src)));
  142. case VAR_VARIANTVECTOR:
  143. return Variant(*(reinterpret_cast<const VariantVector*>(src)));
  144. case VAR_VARIANTMAP:
  145. return Variant(*(reinterpret_cast<const VariantMap*>(src)));
  146. }
  147. return Variant();
  148. }
  149. bool Serializable::Load(Deserializer& source)
  150. {
  151. const std::vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  152. if (!attributes)
  153. return true;
  154. inSerialization_ = true;
  155. // Read attributes as Variants with predefined types from the attribute info
  156. for (unsigned i = 0; i < attributes->size(); ++i)
  157. {
  158. const AttributeInfo& attr = attributes->at(i);
  159. if (!(attr.mode_ & AM_SERIALIZATION))
  160. continue;
  161. if (source.IsEof())
  162. {
  163. LOGERROR("Could not load " + ToLower(GetTypeNameStr()) + ", stream not open or at end");
  164. inSerialization_ = false;
  165. return false;
  166. }
  167. SetAttribute(i, source.ReadVariant(attr.type_));
  168. }
  169. inSerialization_ = false;
  170. return true;
  171. }
  172. bool Serializable::Save(Serializer& dest)
  173. {
  174. const std::vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  175. if (!attributes)
  176. return true;
  177. inSerialization_ = true;
  178. // Then write attributes as Variants without type
  179. for (unsigned i = 0; i < attributes->size(); ++i)
  180. {
  181. const AttributeInfo& attr = attributes->at(i);
  182. if (!(attr.mode_ & AM_SERIALIZATION))
  183. continue;
  184. if (!dest.WriteVariantData(GetAttribute(i)))
  185. {
  186. LOGERROR("Could not save " + ToLower(GetTypeNameStr()) + ", writing to stream failed");
  187. inSerialization_ = false;
  188. return false;
  189. }
  190. }
  191. inSerialization_ = false;
  192. return true;
  193. }
  194. bool Serializable::LoadXML(const XMLElement& source)
  195. {
  196. if (source.IsNull())
  197. {
  198. LOGERROR("Could not load " + ToLower(GetTypeNameStr()) + ", null source element");
  199. return false;
  200. }
  201. const std::vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  202. if (!attributes)
  203. return true;
  204. inSerialization_ = true;
  205. for (unsigned i = 0; i < attributes->size(); ++i)
  206. {
  207. const AttributeInfo& attr = attributes->at(i);
  208. if (!(attr.mode_ & AM_SERIALIZATION))
  209. continue;
  210. // We could assume fixed order. However, do name-based lookup instead for more robustness
  211. XMLElement attrElem = source.GetChildElement("attribute");
  212. bool found = false;
  213. while (attrElem)
  214. {
  215. if (!strcmp(attrElem.GetString("name").c_str(), attr.name_))
  216. {
  217. // If enums specified, do enum lookup and int assignment. Otherwise assign the variant directly
  218. if (attr.enumNames_)
  219. {
  220. std::string value = attrElem.GetString("value");
  221. const char** enumPtr = attr.enumNames_;
  222. int enumValue = 0;
  223. bool enumFound = false;
  224. while (*enumPtr)
  225. {
  226. if (!strcmp(value.c_str(), *enumPtr))
  227. {
  228. enumFound = true;
  229. break;
  230. }
  231. ++enumPtr;
  232. ++enumValue;
  233. }
  234. if (enumFound)
  235. SetAttribute(i, Variant(enumValue));
  236. else
  237. LOGWARNING("Unknown enum value " + value + " in attribute " + std::string(attr.name_));
  238. }
  239. else
  240. SetAttribute(i, attrElem.GetVariant());
  241. found = true;
  242. break;
  243. }
  244. attrElem = attrElem.GetNextElement("attribute");
  245. }
  246. if (!found)
  247. LOGWARNING("Attribute " + std::string(attr.name_) + " not found in XML data");
  248. }
  249. inSerialization_ = false;
  250. return true;
  251. }
  252. bool Serializable::SaveXML(XMLElement& dest)
  253. {
  254. if (dest.IsNull())
  255. {
  256. LOGERROR("Could not save " + ToLower(GetTypeNameStr()) + ", null destination element");
  257. return false;
  258. }
  259. const std::vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  260. if (!attributes)
  261. return true;
  262. inSerialization_ = true;
  263. for (unsigned i = 0; i < attributes->size(); ++i)
  264. {
  265. const AttributeInfo& attr = attributes->at(i);
  266. if (!(attr.mode_ & AM_SERIALIZATION))
  267. continue;
  268. XMLElement attrElem = dest.CreateChildElement("attribute");
  269. attrElem.SetString("name", std::string(attr.name_));
  270. // If enums specified, set as an enum string. Otherwise set directly as a Variant
  271. if (attr.enumNames_)
  272. {
  273. int enumValue = GetAttribute(i).GetInt();
  274. attrElem.SetString("type", "Enum");
  275. attrElem.SetString("value", std::string(attr.enumNames_[enumValue]));
  276. }
  277. else
  278. attrElem.SetVariant(GetAttribute(i));
  279. }
  280. inSerialization_ = false;
  281. return true;
  282. }
  283. void Serializable::SetAttribute(unsigned index, const Variant& value)
  284. {
  285. const std::vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  286. if ((!attributes) || (index >= attributes->size()))
  287. return;
  288. const AttributeInfo& attr = attributes->at(index);
  289. // Check that the new value's type matches the attribute type
  290. if (value.GetType() == attr.type_)
  291. OnSetAttribute(attr, value);
  292. }
  293. void Serializable::SetAttribute(const char* name, const Variant& value)
  294. {
  295. const std::vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  296. if (!attributes)
  297. return;
  298. for (std::vector<AttributeInfo>::const_iterator i = attributes->begin(); i != attributes->end(); ++i)
  299. {
  300. if (!strcmp(i->name_, name))
  301. {
  302. // Check that the new value's type matches the attribute type
  303. if (value.GetType() == i->type_)
  304. OnSetAttribute(*i, value);
  305. return;
  306. }
  307. }
  308. }
  309. Variant Serializable::GetAttribute(unsigned index)
  310. {
  311. const std::vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  312. if ((!attributes) || (index >= attributes->size()))
  313. return Variant();
  314. return OnGetAttribute(attributes->at(index));
  315. }
  316. Variant Serializable::GetAttribute(const char* name)
  317. {
  318. const std::vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  319. if (!attributes)
  320. return Variant();
  321. for (std::vector<AttributeInfo>::const_iterator i = attributes->begin(); i != attributes->end(); ++i)
  322. {
  323. if (!strcmp(i->name_, name))
  324. return OnGetAttribute(*i);
  325. }
  326. return Variant();
  327. }
  328. unsigned Serializable::GetNumAttributes() const
  329. {
  330. const std::vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  331. return attributes ? attributes->size() : 0;
  332. }
  333. const std::vector<AttributeInfo>* Serializable::GetAttributes() const
  334. {
  335. return context_->GetAttributes(GetType());
  336. }