Serializable.cpp 26 KB

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