Serializable.cpp 14 KB

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