Serializable.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 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 "ReplicationState.h"
  28. #include "Serializable.h"
  29. #include "Serializer.h"
  30. #include "XMLElement.h"
  31. #include "DebugNew.h"
  32. OBJECTTYPESTATIC(Serializable);
  33. Serializable::Serializable(Context* context) :
  34. Object(context),
  35. loading_(false)
  36. {
  37. }
  38. Serializable::~Serializable()
  39. {
  40. }
  41. void Serializable::OnSetAttribute(const AttributeInfo& attr, const Variant& src)
  42. {
  43. // Check for accessor function mode
  44. if (attr.accessor_)
  45. {
  46. attr.accessor_->Set(this, src);
  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)) = src.GetInt();
  57. else
  58. *(reinterpret_cast<int*>(dest)) = src.GetInt();
  59. break;
  60. case VAR_BOOL:
  61. *(reinterpret_cast<bool*>(dest)) = src.GetBool();
  62. break;
  63. case VAR_FLOAT:
  64. *(reinterpret_cast<float*>(dest)) = src.GetFloat();
  65. break;
  66. case VAR_VECTOR2:
  67. *(reinterpret_cast<Vector2*>(dest)) = src.GetVector2();
  68. break;
  69. case VAR_VECTOR3:
  70. *(reinterpret_cast<Vector3*>(dest)) = src.GetVector3();
  71. break;
  72. case VAR_VECTOR4:
  73. *(reinterpret_cast<Vector4*>(dest)) = src.GetVector4();
  74. break;
  75. case VAR_QUATERNION:
  76. *(reinterpret_cast<Quaternion*>(dest)) = src.GetQuaternion();
  77. break;
  78. case VAR_COLOR:
  79. *(reinterpret_cast<Color*>(dest)) = src.GetColor();
  80. break;
  81. case VAR_STRING:
  82. *(reinterpret_cast<String*>(dest)) = src.GetString();
  83. break;
  84. case VAR_BUFFER:
  85. *(reinterpret_cast<PODVector<unsigned char>*>(dest)) = src.GetBuffer();
  86. break;
  87. case VAR_RESOURCEREF:
  88. *(reinterpret_cast<ResourceRef*>(dest)) = src.GetResourceRef();
  89. break;
  90. case VAR_RESOURCEREFLIST:
  91. *(reinterpret_cast<ResourceRefList*>(dest)) = src.GetResourceRefList();
  92. break;
  93. case VAR_VARIANTVECTOR:
  94. *(reinterpret_cast<VariantVector*>(dest)) = src.GetVariantVector();
  95. break;
  96. case VAR_VARIANTMAP:
  97. *(reinterpret_cast<VariantMap*>(dest)) = src.GetVariantMap();
  98. break;
  99. }
  100. }
  101. void Serializable::OnGetAttribute(const AttributeInfo& attr, Variant& dest)
  102. {
  103. // Check for accessor function mode
  104. if (attr.accessor_)
  105. {
  106. attr.accessor_->Get(this, dest);
  107. return;
  108. }
  109. // Calculate the source address
  110. void* src = reinterpret_cast<unsigned char*>(this) + attr.offset_;
  111. switch (attr.type_)
  112. {
  113. case VAR_INT:
  114. // If enum type, use the low 8 bits only
  115. if (attr.enumNames_)
  116. dest = *(reinterpret_cast<const unsigned char*>(src));
  117. else
  118. dest = *(reinterpret_cast<const int*>(src));
  119. break;
  120. case VAR_BOOL:
  121. dest = *(reinterpret_cast<const bool*>(src));
  122. break;
  123. case VAR_FLOAT:
  124. dest = *(reinterpret_cast<const float*>(src));
  125. break;
  126. case VAR_VECTOR2:
  127. dest = *(reinterpret_cast<const Vector2*>(src));
  128. break;
  129. case VAR_VECTOR3:
  130. dest = *(reinterpret_cast<const Vector3*>(src));
  131. break;
  132. case VAR_VECTOR4:
  133. dest = *(reinterpret_cast<const Vector4*>(src));
  134. break;
  135. case VAR_QUATERNION:
  136. dest = *(reinterpret_cast<const Quaternion*>(src));
  137. break;
  138. case VAR_COLOR:
  139. dest = *(reinterpret_cast<const Color*>(src));
  140. break;
  141. case VAR_STRING:
  142. dest = *(reinterpret_cast<const String*>(src));
  143. break;
  144. case VAR_BUFFER:
  145. dest = *(reinterpret_cast<const PODVector<unsigned char>*>(src));
  146. break;
  147. case VAR_RESOURCEREF:
  148. dest = *(reinterpret_cast<const ResourceRef*>(src));
  149. break;
  150. case VAR_RESOURCEREFLIST:
  151. dest = *(reinterpret_cast<const ResourceRefList*>(src));
  152. break;
  153. case VAR_VARIANTVECTOR:
  154. dest = *(reinterpret_cast<const VariantVector*>(src));
  155. break;
  156. case VAR_VARIANTMAP:
  157. dest = *(reinterpret_cast<const VariantMap*>(src));
  158. break;
  159. }
  160. }
  161. bool Serializable::Load(Deserializer& source)
  162. {
  163. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  164. if (!attributes)
  165. return true;
  166. loading_ = true;
  167. for (unsigned i = 0; i < attributes->Size(); ++i)
  168. {
  169. const AttributeInfo& attr = attributes->At(i);
  170. if (!(attr.mode_ & AM_FILE))
  171. continue;
  172. if (source.IsEof())
  173. {
  174. LOGERROR("Could not load " + GetTypeName() + ", stream not open or at end");
  175. loading_ = false;
  176. return false;
  177. }
  178. OnSetAttribute(attr, source.ReadVariant(attr.type_));
  179. }
  180. loading_ = false;
  181. return true;
  182. }
  183. bool Serializable::Save(Serializer& dest)
  184. {
  185. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  186. if (!attributes)
  187. return true;
  188. Variant value;
  189. for (unsigned i = 0; i < attributes->Size(); ++i)
  190. {
  191. const AttributeInfo& attr = attributes->At(i);
  192. if (!(attr.mode_ & AM_FILE))
  193. continue;
  194. OnGetAttribute(attr, value);
  195. if (!dest.WriteVariantData(value))
  196. {
  197. LOGERROR("Could not save " + GetTypeName() + ", writing to stream failed");
  198. return false;
  199. }
  200. }
  201. return true;
  202. }
  203. bool Serializable::LoadXML(const XMLElement& source)
  204. {
  205. if (source.IsNull())
  206. {
  207. LOGERROR("Could not load " + GetTypeName() + ", null source element");
  208. return false;
  209. }
  210. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  211. if (!attributes)
  212. return true;
  213. loading_ = true;
  214. XMLElement attrElem = source.GetChild("attribute");
  215. unsigned startIndex = 0;
  216. while (attrElem)
  217. {
  218. const char* name = attrElem.GetAttribute("name");
  219. unsigned i = startIndex;
  220. unsigned attempts = attributes->Size();
  221. while (attempts)
  222. {
  223. const AttributeInfo& attr = attributes->At(i);
  224. if ((attr.mode_ & AM_FILE) && attr.name_ == name)
  225. {
  226. // If enums specified, do enum lookup and int assignment. Otherwise assign the variant directly
  227. if (attr.enumNames_)
  228. {
  229. const char* value = attrElem.GetAttribute("value");
  230. const String* enumPtr = attr.enumNames_;
  231. int enumValue = 0;
  232. bool enumFound = false;
  233. while (enumPtr->Length())
  234. {
  235. if (*enumPtr == value)
  236. {
  237. enumFound = true;
  238. break;
  239. }
  240. ++enumPtr;
  241. ++enumValue;
  242. }
  243. if (enumFound)
  244. OnSetAttribute(attr, Variant(enumValue));
  245. else
  246. LOGWARNING("Unknown enum value " + String(value) + " in attribute " + String(attr.name_));
  247. }
  248. else
  249. OnSetAttribute(attr, attrElem.GetVariantValue(attr.type_));
  250. startIndex = (i + 1) % attributes->Size();
  251. break;
  252. }
  253. else
  254. {
  255. i = (i + 1) % attributes->Size();
  256. --attempts;
  257. }
  258. }
  259. if (!attempts)
  260. LOGWARNING("Unknown attribute " + String(name) + " in XML data");
  261. attrElem = attrElem.GetNext("attribute");
  262. }
  263. loading_ = false;
  264. return true;
  265. }
  266. bool Serializable::SaveXML(XMLElement& dest)
  267. {
  268. if (dest.IsNull())
  269. {
  270. LOGERROR("Could not save " + GetTypeName() + ", null destination element");
  271. return false;
  272. }
  273. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  274. if (!attributes)
  275. return true;
  276. Variant value;
  277. for (unsigned i = 0; i < attributes->Size(); ++i)
  278. {
  279. const AttributeInfo& attr = attributes->At(i);
  280. if (!(attr.mode_ & AM_FILE))
  281. continue;
  282. XMLElement attrElem = dest.CreateChild("attribute");
  283. attrElem.SetAttribute("name", attr.name_.CString());
  284. OnGetAttribute(attr, value);
  285. // If enums specified, set as an enum string. Otherwise set directly as a Variant
  286. if (attr.enumNames_)
  287. {
  288. int enumValue = value.GetInt();
  289. attrElem.SetAttribute("value", attr.enumNames_[enumValue].CString());
  290. }
  291. else
  292. attrElem.SetVariantValue(value);
  293. }
  294. return true;
  295. }
  296. bool Serializable::SetAttribute(unsigned index, const Variant& value)
  297. {
  298. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  299. if (!attributes)
  300. {
  301. LOGERROR(GetTypeName() + " has no attributes");
  302. return false;
  303. }
  304. if (index >= attributes->Size())
  305. {
  306. LOGERROR("Attribute index out of bounds");
  307. return false;
  308. }
  309. const AttributeInfo& attr = attributes->At(index);
  310. // Check that the new value's type matches the attribute type
  311. if (value.GetType() == attr.type_)
  312. {
  313. OnSetAttribute(attr, value);
  314. return true;
  315. }
  316. else
  317. {
  318. LOGERROR("Could not set attribute " + String(attr.name_) + ": expected type " + Variant::GetTypeName(attr.type_) +
  319. " but got " + value.GetTypeName());
  320. return false;
  321. }
  322. }
  323. bool Serializable::SetAttribute(const String& name, const Variant& value)
  324. {
  325. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  326. if (!attributes)
  327. {
  328. LOGERROR(GetTypeName() + " has no attributes");
  329. return false;
  330. }
  331. for (Vector<AttributeInfo>::ConstIterator i = attributes->Begin(); i != attributes->End(); ++i)
  332. {
  333. if (i->name_ == name)
  334. {
  335. // Check that the new value's type matches the attribute type
  336. if (value.GetType() == i->type_)
  337. {
  338. OnSetAttribute(*i, value);
  339. return true;
  340. }
  341. else
  342. {
  343. LOGERROR("Could not set attribute " + String(i->name_) + ": expected type " + Variant::GetTypeName(i->type_)
  344. + " but got " + value.GetTypeName());
  345. return false;
  346. }
  347. }
  348. }
  349. LOGERROR("Could not find attribute " + String(name) + " in " + GetTypeName());
  350. return false;
  351. }
  352. void Serializable::WriteInitialDeltaUpdate(Serializer& dest)
  353. {
  354. const Vector<AttributeInfo>* attributes = GetNetworkAttributes();
  355. if (!attributes)
  356. return;
  357. unsigned numAttributes = attributes->Size();
  358. DirtyBits attributeBits;
  359. // Compare against defaults
  360. for (unsigned i = 0; i < numAttributes; ++i)
  361. {
  362. const AttributeInfo& attr = attributes->At(i);
  363. if (currentState_[i] != attr.defaultValue_)
  364. attributeBits.Set(i);
  365. }
  366. // First write the change bitfield, then attribute data for non-default attributes
  367. dest.Write(attributeBits.data_, (numAttributes + 7) >> 3);
  368. for (unsigned i = 0; i < numAttributes; ++i)
  369. {
  370. if (attributeBits.IsSet(i))
  371. dest.WriteVariantData(currentState_[i]);
  372. }
  373. }
  374. void Serializable::WriteDeltaUpdate(Serializer& dest, const DirtyBits& attributeBits)
  375. {
  376. const Vector<AttributeInfo>* attributes = GetNetworkAttributes();
  377. if (!attributes)
  378. return;
  379. unsigned numAttributes = attributes->Size();
  380. // First write the change bitfield, then attribute data for changed attributes
  381. // Note: the attribute bits should not contain LATESTDATA attributes
  382. dest.Write(attributeBits.data_, (numAttributes + 7) >> 3);
  383. for (unsigned i = 0; i < numAttributes; ++i)
  384. {
  385. if (attributeBits.IsSet(i))
  386. dest.WriteVariantData(currentState_[i]);
  387. }
  388. }
  389. void Serializable::WriteLatestDataUpdate(Serializer& dest)
  390. {
  391. const Vector<AttributeInfo>* attributes = GetNetworkAttributes();
  392. if (!attributes)
  393. return;
  394. unsigned numAttributes = attributes->Size();
  395. for (unsigned i = 0; i < numAttributes; ++i)
  396. {
  397. if (attributes->At(i).mode_ & AM_LATESTDATA)
  398. dest.WriteVariantData(currentState_[i]);
  399. }
  400. }
  401. void Serializable::ReadDeltaUpdate(Deserializer& source)
  402. {
  403. const Vector<AttributeInfo>* attributes = GetNetworkAttributes();
  404. if (!attributes)
  405. return;
  406. unsigned numAttributes = attributes->Size();
  407. DirtyBits attributeBits;
  408. source.Read(attributeBits.data_, (numAttributes + 7) >> 3);
  409. for (unsigned i = 0; i < numAttributes && !source.IsEof(); ++i)
  410. {
  411. if (attributeBits.IsSet(i))
  412. {
  413. const AttributeInfo& attr = attributes->At(i);
  414. OnSetAttribute(attr, source.ReadVariant(attr.type_));
  415. }
  416. }
  417. }
  418. void Serializable::ReadLatestDataUpdate(Deserializer& source)
  419. {
  420. const Vector<AttributeInfo>* attributes = GetNetworkAttributes();
  421. if (!attributes)
  422. return;
  423. unsigned numAttributes = attributes->Size();
  424. for (unsigned i = 0; i < numAttributes && !source.IsEof(); ++i)
  425. {
  426. const AttributeInfo& attr = attributes->At(i);
  427. if (attr.mode_ & AM_LATESTDATA)
  428. OnSetAttribute(attr, source.ReadVariant(attr.type_));
  429. }
  430. }
  431. Variant Serializable::GetAttribute(unsigned index)
  432. {
  433. Variant ret;
  434. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  435. if (!attributes || index >= attributes->Size())
  436. return ret;
  437. OnGetAttribute(attributes->At(index), ret);
  438. return ret;
  439. }
  440. Variant Serializable::GetAttribute(const String& name)
  441. {
  442. Variant ret;
  443. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  444. if (!attributes)
  445. {
  446. LOGERROR(GetTypeName() + " has no attributes");
  447. return ret;
  448. }
  449. for (Vector<AttributeInfo>::ConstIterator i = attributes->Begin(); i != attributes->End(); ++i)
  450. {
  451. if (i->name_ == name)
  452. {
  453. OnGetAttribute(*i, ret);
  454. return ret;
  455. }
  456. }
  457. LOGERROR("Could not find attribute " + String(name) + " in " + GetTypeName());
  458. return ret;
  459. }
  460. unsigned Serializable::GetNumAttributes() const
  461. {
  462. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  463. return attributes ? attributes->Size() : 0;
  464. }
  465. unsigned Serializable::GetNumNetworkAttributes() const
  466. {
  467. const Vector<AttributeInfo>* attributes = context_->GetNetworkAttributes(GetType());
  468. return attributes ? attributes->Size() : 0;
  469. }
  470. const Vector<AttributeInfo>* Serializable::GetAttributes() const
  471. {
  472. return context_->GetAttributes(GetType());
  473. }
  474. const Vector<AttributeInfo>* Serializable::GetNetworkAttributes() const
  475. {
  476. return context_->GetNetworkAttributes(GetType());
  477. }