Serializable.cpp 21 KB

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