Serializable.cpp 13 KB

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