Serializable.cpp 22 KB

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