Serializable.cpp 18 KB

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