Serializable.cpp 19 KB

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