Serializable.cpp 28 KB

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