Serializable.cpp 26 KB

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