Serializable.cpp 14 KB

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