Serializable.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "../Core/Context.h"
  24. #include "../IO/Deserializer.h"
  25. #include "../IO/Log.h"
  26. #include "../Scene/ReplicationState.h"
  27. #include "../Scene/SceneEvents.h"
  28. #include "../Scene/Serializable.h"
  29. #include "../IO/Serializer.h"
  30. #include "../Resource/XMLElement.h"
  31. #include "../DebugNew.h"
  32. namespace Atomic
  33. {
  34. static unsigned RemapAttributeIndex(const Vector<AttributeInfo>* attributes, const AttributeInfo& netAttr, unsigned netAttrIndex)
  35. {
  36. if (!attributes)
  37. return netAttrIndex; // Could not remap
  38. for (unsigned i = 0; i < attributes->Size(); ++i)
  39. {
  40. const AttributeInfo& attr = attributes->At(i);
  41. // Compare either the accessor or offset to avoid name string compare
  42. if (attr.accessor_.Get() && attr.accessor_.Get() == netAttr.accessor_.Get())
  43. return i;
  44. else if (!attr.accessor_.Get() && attr.offset_ == netAttr.offset_)
  45. return i;
  46. }
  47. return netAttrIndex; // Could not remap
  48. }
  49. Serializable::Serializable(Context* context) :
  50. Object(context),
  51. networkState_(0),
  52. instanceDefaultValues_(0),
  53. temporary_(false)
  54. {
  55. }
  56. Serializable::~Serializable()
  57. {
  58. delete networkState_;
  59. networkState_ = 0;
  60. delete instanceDefaultValues_;
  61. instanceDefaultValues_ = 0;
  62. }
  63. void Serializable::OnSetAttribute(const AttributeInfo& attr, const Variant& src)
  64. {
  65. // Check for accessor function mode
  66. if (attr.accessor_)
  67. {
  68. attr.accessor_->Set(this, src);
  69. return;
  70. }
  71. // Calculate the destination address
  72. void* dest = attr.ptr_ ? attr.ptr_ : reinterpret_cast<unsigned char*>(this) + attr.offset_;
  73. switch (attr.type_)
  74. {
  75. case VAR_INT:
  76. // If enum type, use the low 8 bits only
  77. if (attr.enumNames_)
  78. *(reinterpret_cast<unsigned char*>(dest)) = src.GetInt();
  79. else
  80. *(reinterpret_cast<int*>(dest)) = src.GetInt();
  81. break;
  82. case VAR_BOOL:
  83. *(reinterpret_cast<bool*>(dest)) = src.GetBool();
  84. break;
  85. case VAR_FLOAT:
  86. *(reinterpret_cast<float*>(dest)) = src.GetFloat();
  87. break;
  88. case VAR_VECTOR2:
  89. *(reinterpret_cast<Vector2*>(dest)) = src.GetVector2();
  90. break;
  91. case VAR_VECTOR3:
  92. *(reinterpret_cast<Vector3*>(dest)) = src.GetVector3();
  93. break;
  94. case VAR_VECTOR4:
  95. *(reinterpret_cast<Vector4*>(dest)) = src.GetVector4();
  96. break;
  97. case VAR_QUATERNION:
  98. *(reinterpret_cast<Quaternion*>(dest)) = src.GetQuaternion();
  99. break;
  100. case VAR_COLOR:
  101. *(reinterpret_cast<Color*>(dest)) = src.GetColor();
  102. break;
  103. case VAR_STRING:
  104. *(reinterpret_cast<String*>(dest)) = src.GetString();
  105. break;
  106. case VAR_BUFFER:
  107. *(reinterpret_cast<PODVector<unsigned char>*>(dest)) = src.GetBuffer();
  108. break;
  109. case VAR_RESOURCEREF:
  110. *(reinterpret_cast<ResourceRef*>(dest)) = src.GetResourceRef();
  111. break;
  112. case VAR_RESOURCEREFLIST:
  113. *(reinterpret_cast<ResourceRefList*>(dest)) = src.GetResourceRefList();
  114. break;
  115. case VAR_VARIANTVECTOR:
  116. *(reinterpret_cast<VariantVector*>(dest)) = src.GetVariantVector();
  117. break;
  118. case VAR_VARIANTMAP:
  119. *(reinterpret_cast<VariantMap*>(dest)) = src.GetVariantMap();
  120. break;
  121. case VAR_INTRECT:
  122. *(reinterpret_cast<IntRect*>(dest)) = src.GetIntRect();
  123. break;
  124. case VAR_INTVECTOR2:
  125. *(reinterpret_cast<IntVector2*>(dest)) = src.GetIntVector2();
  126. break;
  127. default:
  128. LOGERROR("Unsupported attribute type for OnSetAttribute()");
  129. return;
  130. }
  131. // If it is a network attribute then mark it for next network update
  132. if (attr.mode_ & AM_NET)
  133. MarkNetworkUpdate();
  134. }
  135. void Serializable::OnGetAttribute(const AttributeInfo& attr, Variant& dest) const
  136. {
  137. // Check for accessor function mode
  138. if (attr.accessor_)
  139. {
  140. attr.accessor_->Get(this, dest);
  141. return;
  142. }
  143. // Calculate the source address
  144. const void* src = attr.ptr_ ? attr.ptr_ : reinterpret_cast<const unsigned char*>(this) + attr.offset_;
  145. switch (attr.type_)
  146. {
  147. case VAR_INT:
  148. // If enum type, use the low 8 bits only
  149. if (attr.enumNames_)
  150. dest = *(reinterpret_cast<const unsigned char*>(src));
  151. else
  152. dest = *(reinterpret_cast<const int*>(src));
  153. break;
  154. case VAR_BOOL:
  155. dest = *(reinterpret_cast<const bool*>(src));
  156. break;
  157. case VAR_FLOAT:
  158. dest = *(reinterpret_cast<const float*>(src));
  159. break;
  160. case VAR_VECTOR2:
  161. dest = *(reinterpret_cast<const Vector2*>(src));
  162. break;
  163. case VAR_VECTOR3:
  164. dest = *(reinterpret_cast<const Vector3*>(src));
  165. break;
  166. case VAR_VECTOR4:
  167. dest = *(reinterpret_cast<const Vector4*>(src));
  168. break;
  169. case VAR_QUATERNION:
  170. dest = *(reinterpret_cast<const Quaternion*>(src));
  171. break;
  172. case VAR_COLOR:
  173. dest = *(reinterpret_cast<const Color*>(src));
  174. break;
  175. case VAR_STRING:
  176. dest = *(reinterpret_cast<const String*>(src));
  177. break;
  178. case VAR_BUFFER:
  179. dest = *(reinterpret_cast<const PODVector<unsigned char>*>(src));
  180. break;
  181. case VAR_RESOURCEREF:
  182. dest = *(reinterpret_cast<const ResourceRef*>(src));
  183. break;
  184. case VAR_RESOURCEREFLIST:
  185. dest = *(reinterpret_cast<const ResourceRefList*>(src));
  186. break;
  187. case VAR_VARIANTVECTOR:
  188. dest = *(reinterpret_cast<const VariantVector*>(src));
  189. break;
  190. case VAR_VARIANTMAP:
  191. dest = *(reinterpret_cast<const VariantMap*>(src));
  192. break;
  193. case VAR_INTRECT:
  194. dest = *(reinterpret_cast<const IntRect*>(src));
  195. break;
  196. case VAR_INTVECTOR2:
  197. dest = *(reinterpret_cast<const IntVector2*>(src));
  198. break;
  199. default:
  200. LOGERROR("Unsupported attribute type for OnGetAttribute()");
  201. return;
  202. }
  203. }
  204. const Vector<AttributeInfo>* Serializable::GetAttributes() const
  205. {
  206. return context_->GetAttributes(GetType());
  207. }
  208. const Vector<AttributeInfo>* Serializable::GetNetworkAttributes() const
  209. {
  210. return networkState_ ? networkState_->attributes_ : context_->GetNetworkAttributes(GetType());
  211. }
  212. bool Serializable::Load(Deserializer& source, bool setInstanceDefault)
  213. {
  214. const Vector<AttributeInfo>* attributes = GetAttributes();
  215. if (!attributes)
  216. return true;
  217. for (unsigned i = 0; i < attributes->Size(); ++i)
  218. {
  219. const AttributeInfo& attr = attributes->At(i);
  220. if (!(attr.mode_ & AM_FILE))
  221. continue;
  222. if (source.IsEof())
  223. {
  224. LOGERROR("Could not load " + GetTypeName() + ", stream not open or at end");
  225. return false;
  226. }
  227. Variant varValue = source.ReadVariant(attr.type_);
  228. OnSetAttribute(attr, varValue);
  229. if (setInstanceDefault)
  230. SetInstanceDefault(attr.name_, varValue);
  231. }
  232. return true;
  233. }
  234. bool Serializable::Save(Serializer& dest) const
  235. {
  236. const Vector<AttributeInfo>* attributes = GetAttributes();
  237. if (!attributes)
  238. return true;
  239. Variant value;
  240. for (unsigned i = 0; i < attributes->Size(); ++i)
  241. {
  242. const AttributeInfo& attr = attributes->At(i);
  243. if (!(attr.mode_ & AM_FILE))
  244. continue;
  245. OnGetAttribute(attr, value);
  246. if (!dest.WriteVariantData(value))
  247. {
  248. LOGERROR("Could not save " + GetTypeName() + ", writing to stream failed");
  249. return false;
  250. }
  251. }
  252. return true;
  253. }
  254. bool Serializable::LoadXML(const XMLElement& source, bool setInstanceDefault)
  255. {
  256. if (source.IsNull())
  257. {
  258. LOGERROR("Could not load " + GetTypeName() + ", null source element");
  259. return false;
  260. }
  261. const Vector<AttributeInfo>* attributes = GetAttributes();
  262. if (!attributes)
  263. return true;
  264. XMLElement attrElem = source.GetChild("attribute");
  265. unsigned startIndex = 0;
  266. while (attrElem)
  267. {
  268. String name = attrElem.GetAttribute("name");
  269. unsigned i = startIndex;
  270. unsigned attempts = attributes->Size();
  271. while (attempts)
  272. {
  273. const AttributeInfo& attr = attributes->At(i);
  274. if ((attr.mode_ & AM_FILE) && !attr.name_.Compare(name, true))
  275. {
  276. Variant varValue;
  277. // If enums specified, do enum lookup and int assignment. Otherwise assign the variant directly
  278. if (attr.enumNames_)
  279. {
  280. String value = attrElem.GetAttribute("value");
  281. bool enumFound = false;
  282. int enumValue = 0;
  283. const char** enumPtr = attr.enumNames_;
  284. while (*enumPtr)
  285. {
  286. if (!value.Compare(*enumPtr, false))
  287. {
  288. enumFound = true;
  289. break;
  290. }
  291. ++enumPtr;
  292. ++enumValue;
  293. }
  294. if (enumFound)
  295. varValue = enumValue;
  296. else
  297. LOGWARNING("Unknown enum value " + value + " in attribute " + attr.name_);
  298. }
  299. else
  300. varValue = attrElem.GetVariantValue(attr.type_);
  301. if (!varValue.IsEmpty())
  302. {
  303. OnSetAttribute(attr, varValue);
  304. if (setInstanceDefault)
  305. SetInstanceDefault(attr.name_, varValue);
  306. }
  307. startIndex = (i + 1) % attributes->Size();
  308. break;
  309. }
  310. else
  311. {
  312. i = (i + 1) % attributes->Size();
  313. --attempts;
  314. }
  315. }
  316. if (!attempts)
  317. LOGWARNING("Unknown attribute " + name + " in XML data");
  318. attrElem = attrElem.GetNext("attribute");
  319. }
  320. return true;
  321. }
  322. bool Serializable::SaveXML(XMLElement& dest) const
  323. {
  324. if (dest.IsNull())
  325. {
  326. LOGERROR("Could not save " + GetTypeName() + ", null destination element");
  327. return false;
  328. }
  329. const Vector<AttributeInfo>* attributes = GetAttributes();
  330. if (!attributes)
  331. return true;
  332. Variant value;
  333. for (unsigned i = 0; i < attributes->Size(); ++i)
  334. {
  335. const AttributeInfo& attr = attributes->At(i);
  336. if (!(attr.mode_ & AM_FILE))
  337. continue;
  338. OnGetAttribute(attr, value);
  339. Variant defaultValue(GetAttributeDefault(i));
  340. // In XML serialization default values can be skipped. This will make the file easier to read or edit manually
  341. if (value == defaultValue && !SaveDefaultAttributes())
  342. continue;
  343. XMLElement attrElem = dest.CreateChild("attribute");
  344. attrElem.SetAttribute("name", attr.name_);
  345. // If enums specified, set as an enum string. Otherwise set directly as a Variant
  346. if (attr.enumNames_)
  347. {
  348. int enumValue = value.GetInt();
  349. attrElem.SetAttribute("value", attr.enumNames_[enumValue]);
  350. }
  351. else
  352. attrElem.SetVariantValue(value);
  353. }
  354. return true;
  355. }
  356. bool Serializable::SetAttribute(unsigned index, const Variant& value)
  357. {
  358. const Vector<AttributeInfo>* attributes = GetAttributes();
  359. if (!attributes)
  360. {
  361. LOGERROR(GetTypeName() + " has no attributes");
  362. return false;
  363. }
  364. if (index >= attributes->Size())
  365. {
  366. LOGERROR("Attribute index out of bounds");
  367. return false;
  368. }
  369. const AttributeInfo& attr = attributes->At(index);
  370. // Check that the new value's type matches the attribute type
  371. if (value.GetType() == attr.type_)
  372. {
  373. OnSetAttribute(attr, value);
  374. return true;
  375. }
  376. else
  377. {
  378. LOGERROR("Could not set attribute " + attr.name_ + ": expected type " + Variant::GetTypeName(attr.type_) +
  379. " but got " + value.GetTypeName());
  380. return false;
  381. }
  382. }
  383. bool Serializable::SetAttribute(const String& name, const Variant& value)
  384. {
  385. const Vector<AttributeInfo>* attributes = GetAttributes();
  386. if (!attributes)
  387. {
  388. LOGERROR(GetTypeName() + " has no attributes");
  389. return false;
  390. }
  391. for (Vector<AttributeInfo>::ConstIterator i = attributes->Begin(); i != attributes->End(); ++i)
  392. {
  393. if (!i->name_.Compare(name, true))
  394. {
  395. // Check that the new value's type matches the attribute type
  396. if (value.GetType() == i->type_)
  397. {
  398. OnSetAttribute(*i, value);
  399. return true;
  400. }
  401. else
  402. {
  403. LOGERROR("Could not set attribute " + i->name_ + ": expected type " + Variant::GetTypeName(i->type_)
  404. + " but got " + value.GetTypeName());
  405. return false;
  406. }
  407. }
  408. }
  409. LOGERROR("Could not find attribute " + name + " in " + GetTypeName());
  410. return false;
  411. }
  412. void Serializable::ResetToDefault()
  413. {
  414. const Vector<AttributeInfo>* attributes = GetAttributes();
  415. if (!attributes)
  416. return;
  417. for (unsigned i = 0; i < attributes->Size(); ++i)
  418. {
  419. const AttributeInfo& attr = attributes->At(i);
  420. if (attr.mode_ & (AM_NOEDIT | AM_NODEID | AM_COMPONENTID | AM_NODEIDVECTOR))
  421. continue;
  422. Variant defaultValue = GetInstanceDefault(attr.name_);
  423. if (defaultValue.IsEmpty())
  424. defaultValue = attr.defaultValue_;
  425. OnSetAttribute(attr, defaultValue);
  426. }
  427. }
  428. void Serializable::RemoveInstanceDefault()
  429. {
  430. delete instanceDefaultValues_;
  431. instanceDefaultValues_ = 0;
  432. }
  433. void Serializable::SetTemporary(bool enable)
  434. {
  435. if (enable != temporary_)
  436. {
  437. temporary_ = enable;
  438. using namespace TemporaryChanged;
  439. VariantMap& eventData = GetEventDataMap();
  440. eventData[P_SERIALIZABLE] = this;
  441. SendEvent(E_TEMPORARYCHANGED, eventData);
  442. }
  443. }
  444. void Serializable::SetInterceptNetworkUpdate(const String& attributeName, bool enable)
  445. {
  446. const Vector<AttributeInfo>* attributes = GetNetworkAttributes();
  447. if (!attributes)
  448. return;
  449. AllocateNetworkState();
  450. for (unsigned i = 0; i < attributes->Size(); ++i)
  451. {
  452. const AttributeInfo& attr = attributes->At(i);
  453. if (!attr.name_.Compare(attributeName, true))
  454. {
  455. if (enable)
  456. networkState_->interceptMask_ |= 1ULL << i;
  457. else
  458. networkState_->interceptMask_ &= ~(1ULL << i);
  459. break;
  460. }
  461. }
  462. }
  463. void Serializable::AllocateNetworkState()
  464. {
  465. if (!networkState_)
  466. {
  467. const Vector<AttributeInfo>* networkAttributes = GetNetworkAttributes();
  468. networkState_ = new NetworkState();
  469. networkState_->attributes_ = networkAttributes;
  470. }
  471. }
  472. void Serializable::WriteInitialDeltaUpdate(Serializer& dest, unsigned char timeStamp)
  473. {
  474. if (!networkState_)
  475. {
  476. LOGERROR("WriteInitialDeltaUpdate called without allocated NetworkState");
  477. return;
  478. }
  479. const Vector<AttributeInfo>* attributes = networkState_->attributes_;
  480. if (!attributes)
  481. return;
  482. unsigned numAttributes = attributes->Size();
  483. DirtyBits attributeBits;
  484. // Compare against defaults
  485. for (unsigned i = 0; i < numAttributes; ++i)
  486. {
  487. const AttributeInfo& attr = attributes->At(i);
  488. if (networkState_->currentValues_[i] != attr.defaultValue_)
  489. attributeBits.Set(i);
  490. }
  491. // First write the change bitfield, then attribute data for non-default attributes
  492. dest.WriteUByte(timeStamp);
  493. dest.Write(attributeBits.data_, (numAttributes + 7) >> 3);
  494. for (unsigned i = 0; i < numAttributes; ++i)
  495. {
  496. if (attributeBits.IsSet(i))
  497. dest.WriteVariantData(networkState_->currentValues_[i]);
  498. }
  499. }
  500. void Serializable::WriteDeltaUpdate(Serializer& dest, const DirtyBits& attributeBits, unsigned char timeStamp)
  501. {
  502. if (!networkState_)
  503. {
  504. LOGERROR("WriteDeltaUpdate called without allocated NetworkState");
  505. return;
  506. }
  507. const Vector<AttributeInfo>* attributes = networkState_->attributes_;
  508. if (!attributes)
  509. return;
  510. unsigned numAttributes = attributes->Size();
  511. // First write the change bitfield, then attribute data for changed attributes
  512. // Note: the attribute bits should not contain LATESTDATA attributes
  513. dest.WriteUByte(timeStamp);
  514. dest.Write(attributeBits.data_, (numAttributes + 7) >> 3);
  515. for (unsigned i = 0; i < numAttributes; ++i)
  516. {
  517. if (attributeBits.IsSet(i))
  518. dest.WriteVariantData(networkState_->currentValues_[i]);
  519. }
  520. }
  521. void Serializable::WriteLatestDataUpdate(Serializer& dest, unsigned char timeStamp)
  522. {
  523. if (!networkState_)
  524. {
  525. LOGERROR("WriteLatestDataUpdate called without allocated NetworkState");
  526. return;
  527. }
  528. const Vector<AttributeInfo>* attributes = networkState_->attributes_;
  529. if (!attributes)
  530. return;
  531. unsigned numAttributes = attributes->Size();
  532. dest.WriteUByte(timeStamp);
  533. for (unsigned i = 0; i < numAttributes; ++i)
  534. {
  535. if (attributes->At(i).mode_ & AM_LATESTDATA)
  536. dest.WriteVariantData(networkState_->currentValues_[i]);
  537. }
  538. }
  539. bool Serializable::ReadDeltaUpdate(Deserializer& source)
  540. {
  541. const Vector<AttributeInfo>* attributes = GetNetworkAttributes();
  542. if (!attributes)
  543. return false;
  544. unsigned numAttributes = attributes->Size();
  545. DirtyBits attributeBits;
  546. bool changed = false;
  547. unsigned long long interceptMask = networkState_ ? networkState_->interceptMask_ : 0;
  548. unsigned char timeStamp = source.ReadUByte();
  549. source.Read(attributeBits.data_, (numAttributes + 7) >> 3);
  550. for (unsigned i = 0; i < numAttributes && !source.IsEof(); ++i)
  551. {
  552. if (attributeBits.IsSet(i))
  553. {
  554. const AttributeInfo& attr = attributes->At(i);
  555. if (!(interceptMask & (1ULL << i)))
  556. {
  557. OnSetAttribute(attr, source.ReadVariant(attr.type_));
  558. changed = true;
  559. }
  560. else
  561. {
  562. using namespace InterceptNetworkUpdate;
  563. VariantMap& eventData = GetEventDataMap();
  564. eventData[P_SERIALIZABLE] = this;
  565. eventData[P_TIMESTAMP] = (unsigned)timeStamp;
  566. eventData[P_INDEX] = RemapAttributeIndex(GetAttributes(), attr, i);
  567. eventData[P_NAME] = attr.name_;
  568. eventData[P_VALUE] = source.ReadVariant(attr.type_);
  569. SendEvent(E_INTERCEPTNETWORKUPDATE, eventData);
  570. }
  571. }
  572. }
  573. return changed;
  574. }
  575. bool Serializable::ReadLatestDataUpdate(Deserializer& source)
  576. {
  577. const Vector<AttributeInfo>* attributes = GetNetworkAttributes();
  578. if (!attributes)
  579. return false;
  580. unsigned numAttributes = attributes->Size();
  581. bool changed = false;
  582. unsigned long long interceptMask = networkState_ ? networkState_->interceptMask_ : 0;
  583. unsigned char timeStamp = source.ReadUByte();
  584. for (unsigned i = 0; i < numAttributes && !source.IsEof(); ++i)
  585. {
  586. const AttributeInfo& attr = attributes->At(i);
  587. if (attr.mode_ & AM_LATESTDATA)
  588. {
  589. if (!(interceptMask & (1ULL << i)))
  590. {
  591. OnSetAttribute(attr, source.ReadVariant(attr.type_));
  592. changed = true;
  593. }
  594. else
  595. {
  596. using namespace InterceptNetworkUpdate;
  597. VariantMap& eventData = GetEventDataMap();
  598. eventData[P_SERIALIZABLE] = this;
  599. eventData[P_TIMESTAMP] = (unsigned)timeStamp;
  600. eventData[P_INDEX] = RemapAttributeIndex(GetAttributes(), attr, i);
  601. eventData[P_NAME] = attr.name_;
  602. eventData[P_VALUE] = source.ReadVariant(attr.type_);
  603. SendEvent(E_INTERCEPTNETWORKUPDATE, eventData);
  604. }
  605. }
  606. }
  607. return changed;
  608. }
  609. Variant Serializable::GetAttribute(unsigned index) const
  610. {
  611. Variant ret;
  612. const Vector<AttributeInfo>* attributes = GetAttributes();
  613. if (!attributes)
  614. {
  615. LOGERROR(GetTypeName() + " has no attributes");
  616. return ret;
  617. }
  618. if (index >= attributes->Size())
  619. {
  620. LOGERROR("Attribute index out of bounds");
  621. return ret;
  622. }
  623. OnGetAttribute(attributes->At(index), ret);
  624. return ret;
  625. }
  626. Variant Serializable::GetAttribute(const String& name) const
  627. {
  628. Variant ret;
  629. const Vector<AttributeInfo>* attributes = GetAttributes();
  630. if (!attributes)
  631. {
  632. LOGERROR(GetTypeName() + " has no attributes");
  633. return ret;
  634. }
  635. for (Vector<AttributeInfo>::ConstIterator i = attributes->Begin(); i != attributes->End(); ++i)
  636. {
  637. if (!i->name_.Compare(name, true))
  638. {
  639. OnGetAttribute(*i, ret);
  640. return ret;
  641. }
  642. }
  643. LOGERROR("Could not find attribute " + name + " in " + GetTypeName());
  644. return ret;
  645. }
  646. Variant Serializable::GetAttributeDefault(unsigned index) const
  647. {
  648. const Vector<AttributeInfo>* attributes = GetAttributes();
  649. if (!attributes)
  650. {
  651. LOGERROR(GetTypeName() + " has no attributes");
  652. return Variant::EMPTY;
  653. }
  654. if (index >= attributes->Size())
  655. {
  656. LOGERROR("Attribute index out of bounds");
  657. return Variant::EMPTY;
  658. }
  659. AttributeInfo attr = attributes->At(index);
  660. Variant defaultValue = GetInstanceDefault(attr.name_);
  661. return defaultValue.IsEmpty() ? attr.defaultValue_ : defaultValue;
  662. }
  663. Variant Serializable::GetAttributeDefault(const String& name) const
  664. {
  665. Variant defaultValue = GetInstanceDefault(name);
  666. if (!defaultValue.IsEmpty())
  667. return defaultValue;
  668. const Vector<AttributeInfo>* attributes = GetAttributes();
  669. if (!attributes)
  670. {
  671. LOGERROR(GetTypeName() + " has no attributes");
  672. return Variant::EMPTY;
  673. }
  674. for (Vector<AttributeInfo>::ConstIterator i = attributes->Begin(); i != attributes->End(); ++i)
  675. {
  676. if (!i->name_.Compare(name, true))
  677. return i->defaultValue_;
  678. }
  679. LOGERROR("Could not find attribute " + name + " in " + GetTypeName());
  680. return Variant::EMPTY;
  681. }
  682. unsigned Serializable::GetNumAttributes() const
  683. {
  684. const Vector<AttributeInfo>* attributes = GetAttributes();
  685. return attributes ? attributes->Size() : 0;
  686. }
  687. unsigned Serializable::GetNumNetworkAttributes() const
  688. {
  689. const Vector<AttributeInfo>* attributes = networkState_ ? networkState_->attributes_ :
  690. context_->GetNetworkAttributes(GetType());
  691. return attributes ? attributes->Size() : 0;
  692. }
  693. bool Serializable::GetInterceptNetworkUpdate(const String& attributeName) const
  694. {
  695. const Vector<AttributeInfo>* attributes = GetNetworkAttributes();
  696. if (!attributes)
  697. return false;
  698. unsigned long long interceptMask = networkState_ ? networkState_->interceptMask_ : 0;
  699. for (unsigned i = 0; i < attributes->Size(); ++i)
  700. {
  701. const AttributeInfo& attr = attributes->At(i);
  702. if (!attr.name_.Compare(attributeName, true))
  703. return interceptMask & (1ULL << i) ? true : false;
  704. }
  705. return false;
  706. }
  707. void Serializable::SetInstanceDefault(const String& name, const Variant& defaultValue)
  708. {
  709. // Allocate the instance level default value
  710. if (!instanceDefaultValues_)
  711. instanceDefaultValues_ = new VariantMap();
  712. instanceDefaultValues_->operator[] (name) = defaultValue;
  713. }
  714. Variant Serializable::GetInstanceDefault(const String& name) const
  715. {
  716. if (instanceDefaultValues_)
  717. {
  718. VariantMap::ConstIterator i = instanceDefaultValues_->Find(name);
  719. if (i != instanceDefaultValues_->End())
  720. return i->second_;
  721. }
  722. return Variant::EMPTY;
  723. }
  724. }