Serializable.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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. XMLElement attrElem = dest.CreateChild("attribute");
  300. attrElem.SetAttribute("name", attr.name_);
  301. OnGetAttribute(attr, value);
  302. // If enums specified, set as an enum string. Otherwise set directly as a Variant
  303. if (attr.enumNames_)
  304. {
  305. int enumValue = value.GetInt();
  306. attrElem.SetAttribute("value", attr.enumNames_[enumValue]);
  307. }
  308. else
  309. attrElem.SetVariantValue(value);
  310. }
  311. return true;
  312. }
  313. bool Serializable::SetAttribute(unsigned index, const Variant& value)
  314. {
  315. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  316. if (!attributes)
  317. {
  318. LOGERROR(GetTypeName() + " has no attributes");
  319. return false;
  320. }
  321. if (index >= attributes->Size())
  322. {
  323. LOGERROR("Attribute index out of bounds");
  324. return false;
  325. }
  326. const AttributeInfo& attr = attributes->At(index);
  327. // Check that the new value's type matches the attribute type
  328. if (value.GetType() == attr.type_)
  329. {
  330. OnSetAttribute(attr, value);
  331. return true;
  332. }
  333. else
  334. {
  335. LOGERROR("Could not set attribute " + String(attr.name_) + ": expected type " + Variant::GetTypeName(attr.type_) +
  336. " but got " + value.GetTypeName());
  337. return false;
  338. }
  339. }
  340. bool Serializable::SetAttribute(const String& name, const Variant& value)
  341. {
  342. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  343. if (!attributes)
  344. {
  345. LOGERROR(GetTypeName() + " has no attributes");
  346. return false;
  347. }
  348. for (Vector<AttributeInfo>::ConstIterator i = attributes->Begin(); i != attributes->End(); ++i)
  349. {
  350. if (!String::Compare(i->name_, name.CString(), true))
  351. {
  352. // Check that the new value's type matches the attribute type
  353. if (value.GetType() == i->type_)
  354. {
  355. OnSetAttribute(*i, value);
  356. return true;
  357. }
  358. else
  359. {
  360. LOGERROR("Could not set attribute " + String(i->name_) + ": expected type " + Variant::GetTypeName(i->type_)
  361. + " but got " + value.GetTypeName());
  362. return false;
  363. }
  364. }
  365. }
  366. LOGERROR("Could not find attribute " + String(name) + " in " + GetTypeName());
  367. return false;
  368. }
  369. void Serializable::AllocateNetworkState()
  370. {
  371. if (!networkState_)
  372. {
  373. networkState_ = new NetworkState();
  374. networkState_->attributes_ = context_->GetNetworkAttributes(GetType());
  375. }
  376. }
  377. void Serializable::WriteInitialDeltaUpdate(Serializer& dest)
  378. {
  379. if (!networkState_)
  380. {
  381. LOGERROR("WriteInitialDeltaUpdate called without allocated NetworkState");
  382. return;
  383. }
  384. const Vector<AttributeInfo>* attributes = networkState_->attributes_;
  385. if (!attributes)
  386. return;
  387. unsigned numAttributes = attributes->Size();
  388. DirtyBits attributeBits;
  389. // Compare against defaults
  390. for (unsigned i = 0; i < numAttributes; ++i)
  391. {
  392. const AttributeInfo& attr = attributes->At(i);
  393. if (networkState_->currentValues_[i] != attr.defaultValue_)
  394. attributeBits.Set(i);
  395. }
  396. // First write the change bitfield, then attribute data for non-default attributes
  397. dest.Write(attributeBits.data_, (numAttributes + 7) >> 3);
  398. for (unsigned i = 0; i < numAttributes; ++i)
  399. {
  400. if (attributeBits.IsSet(i))
  401. dest.WriteVariantData(networkState_->currentValues_[i]);
  402. }
  403. }
  404. void Serializable::WriteDeltaUpdate(Serializer& dest, const DirtyBits& attributeBits)
  405. {
  406. if (!networkState_)
  407. {
  408. LOGERROR("WriteDeltaUpdate called without allocated NetworkState");
  409. return;
  410. }
  411. const Vector<AttributeInfo>* attributes = networkState_->attributes_;
  412. if (!attributes)
  413. return;
  414. unsigned numAttributes = attributes->Size();
  415. // First write the change bitfield, then attribute data for changed attributes
  416. // Note: the attribute bits should not contain LATESTDATA attributes
  417. dest.Write(attributeBits.data_, (numAttributes + 7) >> 3);
  418. for (unsigned i = 0; i < numAttributes; ++i)
  419. {
  420. if (attributeBits.IsSet(i))
  421. dest.WriteVariantData(networkState_->currentValues_[i]);
  422. }
  423. }
  424. void Serializable::WriteLatestDataUpdate(Serializer& dest)
  425. {
  426. if (!networkState_)
  427. {
  428. LOGERROR("WriteLatestDataUpdate called without allocated NetworkState");
  429. return;
  430. }
  431. const Vector<AttributeInfo>* attributes = networkState_->attributes_;
  432. if (!attributes)
  433. return;
  434. unsigned numAttributes = attributes->Size();
  435. for (unsigned i = 0; i < numAttributes; ++i)
  436. {
  437. if (attributes->At(i).mode_ & AM_LATESTDATA)
  438. dest.WriteVariantData(networkState_->currentValues_[i]);
  439. }
  440. }
  441. void Serializable::ReadDeltaUpdate(Deserializer& source)
  442. {
  443. const Vector<AttributeInfo>* attributes = GetNetworkAttributes();
  444. if (!attributes)
  445. return;
  446. unsigned numAttributes = attributes->Size();
  447. DirtyBits attributeBits;
  448. source.Read(attributeBits.data_, (numAttributes + 7) >> 3);
  449. for (unsigned i = 0; i < numAttributes && !source.IsEof(); ++i)
  450. {
  451. if (attributeBits.IsSet(i))
  452. {
  453. const AttributeInfo& attr = attributes->At(i);
  454. OnSetAttribute(attr, source.ReadVariant(attr.type_));
  455. }
  456. }
  457. }
  458. void Serializable::ReadLatestDataUpdate(Deserializer& source)
  459. {
  460. const Vector<AttributeInfo>* attributes = GetNetworkAttributes();
  461. if (!attributes)
  462. return;
  463. unsigned numAttributes = attributes->Size();
  464. for (unsigned i = 0; i < numAttributes && !source.IsEof(); ++i)
  465. {
  466. const AttributeInfo& attr = attributes->At(i);
  467. if (attr.mode_ & AM_LATESTDATA)
  468. OnSetAttribute(attr, source.ReadVariant(attr.type_));
  469. }
  470. }
  471. Variant Serializable::GetAttribute(unsigned index)
  472. {
  473. Variant ret;
  474. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  475. if (!attributes)
  476. {
  477. LOGERROR(GetTypeName() + " has no attributes");
  478. return ret;
  479. }
  480. if (index >= attributes->Size())
  481. {
  482. LOGERROR("Attribute index out of bounds");
  483. return ret;
  484. }
  485. OnGetAttribute(attributes->At(index), ret);
  486. return ret;
  487. }
  488. Variant Serializable::GetAttribute(const String& name)
  489. {
  490. Variant ret;
  491. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  492. if (!attributes)
  493. {
  494. LOGERROR(GetTypeName() + " has no attributes");
  495. return ret;
  496. }
  497. for (Vector<AttributeInfo>::ConstIterator i = attributes->Begin(); i != attributes->End(); ++i)
  498. {
  499. if (!String::Compare(i->name_, name.CString(), true))
  500. {
  501. OnGetAttribute(*i, ret);
  502. return ret;
  503. }
  504. }
  505. LOGERROR("Could not find attribute " + String(name) + " in " + GetTypeName());
  506. return ret;
  507. }
  508. unsigned Serializable::GetNumAttributes() const
  509. {
  510. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  511. return attributes ? attributes->Size() : 0;
  512. }
  513. unsigned Serializable::GetNumNetworkAttributes() const
  514. {
  515. const Vector<AttributeInfo>* attributes = networkState_ ? networkState_->attributes_ :
  516. context_->GetNetworkAttributes(GetType());
  517. return attributes ? attributes->Size() : 0;
  518. }
  519. const Vector<AttributeInfo>* Serializable::GetAttributes() const
  520. {
  521. return context_->GetAttributes(GetType());
  522. }
  523. const Vector<AttributeInfo>* Serializable::GetNetworkAttributes() const
  524. {
  525. return networkState_ ? networkState_->attributes_ : context_->GetNetworkAttributes(GetType());
  526. }
  527. }