Serializable.cpp 31 KB

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