Serializable.cpp 22 KB

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