Serializable.cpp 19 KB

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