Serializable.cpp 14 KB

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