Serializable.cpp 25 KB

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