Serializable.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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 = attr.ptr_ ? attr.ptr_ : 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 = attr.ptr_ ? attr.ptr_ : 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. const Vector<AttributeInfo>* Serializable::GetAttributes() const
  184. {
  185. return context_->GetAttributes(GetType());
  186. }
  187. const Vector<AttributeInfo>* Serializable::GetNetworkAttributes() const
  188. {
  189. return networkState_ ? networkState_->attributes_ : context_->GetNetworkAttributes(GetType());
  190. }
  191. bool Serializable::Load(Deserializer& source)
  192. {
  193. const Vector<AttributeInfo>* attributes = GetAttributes();
  194. if (!attributes)
  195. return true;
  196. for (unsigned i = 0; i < attributes->Size(); ++i)
  197. {
  198. const AttributeInfo& attr = attributes->At(i);
  199. if (!(attr.mode_ & AM_FILE))
  200. continue;
  201. if (source.IsEof())
  202. {
  203. LOGERROR("Could not load " + GetTypeName() + ", stream not open or at end");
  204. return false;
  205. }
  206. OnSetAttribute(attr, source.ReadVariant(attr.type_));
  207. }
  208. return true;
  209. }
  210. bool Serializable::Save(Serializer& dest)
  211. {
  212. const Vector<AttributeInfo>* attributes = GetAttributes();
  213. if (!attributes)
  214. return true;
  215. Variant value;
  216. for (unsigned i = 0; i < attributes->Size(); ++i)
  217. {
  218. const AttributeInfo& attr = attributes->At(i);
  219. if (!(attr.mode_ & AM_FILE))
  220. continue;
  221. OnGetAttribute(attr, value);
  222. if (!dest.WriteVariantData(value))
  223. {
  224. LOGERROR("Could not save " + GetTypeName() + ", writing to stream failed");
  225. return false;
  226. }
  227. }
  228. return true;
  229. }
  230. bool Serializable::LoadXML(const XMLElement& source)
  231. {
  232. if (source.IsNull())
  233. {
  234. LOGERROR("Could not load " + GetTypeName() + ", null source element");
  235. return false;
  236. }
  237. const Vector<AttributeInfo>* attributes = GetAttributes();
  238. if (!attributes)
  239. return true;
  240. XMLElement attrElem = source.GetChild("attribute");
  241. unsigned startIndex = 0;
  242. while (attrElem)
  243. {
  244. const char* name = attrElem.GetAttribute("name");
  245. unsigned i = startIndex;
  246. unsigned attempts = attributes->Size();
  247. while (attempts)
  248. {
  249. const AttributeInfo& attr = attributes->At(i);
  250. if ((attr.mode_ & AM_FILE) && !attr.name_.Compare(name, true))
  251. {
  252. // If enums specified, do enum lookup and int assignment. Otherwise assign the variant directly
  253. if (attr.enumNames_)
  254. {
  255. const char* value = attrElem.GetAttribute("value");
  256. bool enumFound = false;
  257. int enumValue = 0;
  258. const char** enumPtr = attr.enumNames_;
  259. while (*enumPtr)
  260. {
  261. if (!String::Compare(*enumPtr, value, false))
  262. {
  263. enumFound = true;
  264. break;
  265. }
  266. ++enumPtr;
  267. ++enumValue;
  268. }
  269. if (enumFound)
  270. OnSetAttribute(attr, Variant(enumValue));
  271. else
  272. LOGWARNING("Unknown enum value " + String(value) + " in attribute " + String(attr.name_));
  273. }
  274. else
  275. OnSetAttribute(attr, attrElem.GetVariantValue(attr.type_));
  276. startIndex = (i + 1) % attributes->Size();
  277. break;
  278. }
  279. else
  280. {
  281. i = (i + 1) % attributes->Size();
  282. --attempts;
  283. }
  284. }
  285. if (!attempts)
  286. LOGWARNING("Unknown attribute " + String(name) + " in XML data");
  287. attrElem = attrElem.GetNext("attribute");
  288. }
  289. return true;
  290. }
  291. bool Serializable::SaveXML(XMLElement& dest)
  292. {
  293. if (dest.IsNull())
  294. {
  295. LOGERROR("Could not save " + GetTypeName() + ", null destination element");
  296. return false;
  297. }
  298. const Vector<AttributeInfo>* attributes = GetAttributes();
  299. if (!attributes)
  300. return true;
  301. Variant value;
  302. for (unsigned i = 0; i < attributes->Size(); ++i)
  303. {
  304. const AttributeInfo& attr = attributes->At(i);
  305. if (!(attr.mode_ & AM_FILE))
  306. continue;
  307. OnGetAttribute(attr, value);
  308. // In XML serialization default values can be skipped. This will make the file easier to read or edit manually
  309. if (value == attr.defaultValue_ && !SaveDefaultAttributes())
  310. continue;
  311. XMLElement attrElem = dest.CreateChild("attribute");
  312. attrElem.SetAttribute("name", attr.name_);
  313. // If enums specified, set as an enum string. Otherwise set directly as a Variant
  314. if (attr.enumNames_)
  315. {
  316. int enumValue = value.GetInt();
  317. attrElem.SetAttribute("value", attr.enumNames_[enumValue]);
  318. }
  319. else
  320. attrElem.SetVariantValue(value);
  321. }
  322. return true;
  323. }
  324. bool Serializable::SetAttribute(unsigned index, const Variant& value)
  325. {
  326. const Vector<AttributeInfo>* attributes = GetAttributes();
  327. if (!attributes)
  328. {
  329. LOGERROR(GetTypeName() + " has no attributes");
  330. return false;
  331. }
  332. if (index >= attributes->Size())
  333. {
  334. LOGERROR("Attribute index out of bounds");
  335. return false;
  336. }
  337. const AttributeInfo& attr = attributes->At(index);
  338. // Check that the new value's type matches the attribute type
  339. if (value.GetType() == attr.type_)
  340. {
  341. OnSetAttribute(attr, value);
  342. return true;
  343. }
  344. else
  345. {
  346. LOGERROR("Could not set attribute " + String(attr.name_) + ": expected type " + Variant::GetTypeName(attr.type_) +
  347. " but got " + value.GetTypeName());
  348. return false;
  349. }
  350. }
  351. bool Serializable::SetAttribute(const String& name, const Variant& value)
  352. {
  353. const Vector<AttributeInfo>* attributes = GetAttributes();
  354. if (!attributes)
  355. {
  356. LOGERROR(GetTypeName() + " has no attributes");
  357. return false;
  358. }
  359. for (Vector<AttributeInfo>::ConstIterator i = attributes->Begin(); i != attributes->End(); ++i)
  360. {
  361. if (!i->name_.Compare(name, true))
  362. {
  363. // Check that the new value's type matches the attribute type
  364. if (value.GetType() == i->type_)
  365. {
  366. OnSetAttribute(*i, value);
  367. return true;
  368. }
  369. else
  370. {
  371. LOGERROR("Could not set attribute " + String(i->name_) + ": expected type " + Variant::GetTypeName(i->type_)
  372. + " but got " + value.GetTypeName());
  373. return false;
  374. }
  375. }
  376. }
  377. LOGERROR("Could not find attribute " + String(name) + " in " + GetTypeName());
  378. return false;
  379. }
  380. void Serializable::AllocateNetworkState()
  381. {
  382. if (!networkState_)
  383. {
  384. const Vector<AttributeInfo>* networkAttributes = GetNetworkAttributes();
  385. networkState_ = new NetworkState();
  386. networkState_->attributes_ = networkAttributes;
  387. }
  388. }
  389. void Serializable::WriteInitialDeltaUpdate(Serializer& dest)
  390. {
  391. if (!networkState_)
  392. {
  393. LOGERROR("WriteInitialDeltaUpdate called without allocated NetworkState");
  394. return;
  395. }
  396. const Vector<AttributeInfo>* attributes = networkState_->attributes_;
  397. if (!attributes)
  398. return;
  399. unsigned numAttributes = attributes->Size();
  400. DirtyBits attributeBits;
  401. // Compare against defaults
  402. for (unsigned i = 0; i < numAttributes; ++i)
  403. {
  404. const AttributeInfo& attr = attributes->At(i);
  405. if (networkState_->currentValues_[i] != attr.defaultValue_)
  406. attributeBits.Set(i);
  407. }
  408. // First write the change bitfield, then attribute data for non-default attributes
  409. dest.Write(attributeBits.data_, (numAttributes + 7) >> 3);
  410. for (unsigned i = 0; i < numAttributes; ++i)
  411. {
  412. if (attributeBits.IsSet(i))
  413. dest.WriteVariantData(networkState_->currentValues_[i]);
  414. }
  415. }
  416. void Serializable::WriteDeltaUpdate(Serializer& dest, const DirtyBits& attributeBits)
  417. {
  418. if (!networkState_)
  419. {
  420. LOGERROR("WriteDeltaUpdate called without allocated NetworkState");
  421. return;
  422. }
  423. const Vector<AttributeInfo>* attributes = networkState_->attributes_;
  424. if (!attributes)
  425. return;
  426. unsigned numAttributes = attributes->Size();
  427. // First write the change bitfield, then attribute data for changed attributes
  428. // Note: the attribute bits should not contain LATESTDATA attributes
  429. dest.Write(attributeBits.data_, (numAttributes + 7) >> 3);
  430. for (unsigned i = 0; i < numAttributes; ++i)
  431. {
  432. if (attributeBits.IsSet(i))
  433. dest.WriteVariantData(networkState_->currentValues_[i]);
  434. }
  435. }
  436. void Serializable::WriteLatestDataUpdate(Serializer& dest)
  437. {
  438. if (!networkState_)
  439. {
  440. LOGERROR("WriteLatestDataUpdate 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. for (unsigned i = 0; i < numAttributes; ++i)
  448. {
  449. if (attributes->At(i).mode_ & AM_LATESTDATA)
  450. dest.WriteVariantData(networkState_->currentValues_[i]);
  451. }
  452. }
  453. void Serializable::ReadDeltaUpdate(Deserializer& source)
  454. {
  455. const Vector<AttributeInfo>* attributes = GetNetworkAttributes();
  456. if (!attributes)
  457. return;
  458. unsigned numAttributes = attributes->Size();
  459. DirtyBits attributeBits;
  460. source.Read(attributeBits.data_, (numAttributes + 7) >> 3);
  461. for (unsigned i = 0; i < numAttributes && !source.IsEof(); ++i)
  462. {
  463. if (attributeBits.IsSet(i))
  464. {
  465. const AttributeInfo& attr = attributes->At(i);
  466. OnSetAttribute(attr, source.ReadVariant(attr.type_));
  467. }
  468. }
  469. }
  470. void Serializable::ReadLatestDataUpdate(Deserializer& source)
  471. {
  472. const Vector<AttributeInfo>* attributes = GetNetworkAttributes();
  473. if (!attributes)
  474. return;
  475. unsigned numAttributes = attributes->Size();
  476. for (unsigned i = 0; i < numAttributes && !source.IsEof(); ++i)
  477. {
  478. const AttributeInfo& attr = attributes->At(i);
  479. if (attr.mode_ & AM_LATESTDATA)
  480. OnSetAttribute(attr, source.ReadVariant(attr.type_));
  481. }
  482. }
  483. Variant Serializable::GetAttribute(unsigned index)
  484. {
  485. Variant ret;
  486. const Vector<AttributeInfo>* attributes = GetAttributes();
  487. if (!attributes)
  488. {
  489. LOGERROR(GetTypeName() + " has no attributes");
  490. return ret;
  491. }
  492. if (index >= attributes->Size())
  493. {
  494. LOGERROR("Attribute index out of bounds");
  495. return ret;
  496. }
  497. OnGetAttribute(attributes->At(index), ret);
  498. return ret;
  499. }
  500. Variant Serializable::GetAttribute(const String& name)
  501. {
  502. Variant ret;
  503. const Vector<AttributeInfo>* attributes = GetAttributes();
  504. if (!attributes)
  505. {
  506. LOGERROR(GetTypeName() + " has no attributes");
  507. return ret;
  508. }
  509. for (Vector<AttributeInfo>::ConstIterator i = attributes->Begin(); i != attributes->End(); ++i)
  510. {
  511. if (!i->name_.Compare(name, true))
  512. {
  513. OnGetAttribute(*i, ret);
  514. return ret;
  515. }
  516. }
  517. LOGERROR("Could not find attribute " + String(name) + " in " + GetTypeName());
  518. return ret;
  519. }
  520. unsigned Serializable::GetNumAttributes() const
  521. {
  522. const Vector<AttributeInfo>* attributes = GetAttributes();
  523. return attributes ? attributes->Size() : 0;
  524. }
  525. unsigned Serializable::GetNumNetworkAttributes() const
  526. {
  527. const Vector<AttributeInfo>* attributes = networkState_ ? networkState_->attributes_ :
  528. context_->GetNetworkAttributes(GetType());
  529. return attributes ? attributes->Size() : 0;
  530. }
  531. }