Serializable.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Context.h"
  25. #include "Deserializer.h"
  26. #include "Log.h"
  27. #include "ReplicationState.h"
  28. #include "Serializable.h"
  29. #include "Serializer.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. }
  104. }
  105. void Serializable::OnGetAttribute(const AttributeInfo& attr, Variant& dest)
  106. {
  107. // Check for accessor function mode
  108. if (attr.accessor_)
  109. {
  110. attr.accessor_->Get(this, dest);
  111. return;
  112. }
  113. // Calculate the source address
  114. void* src = reinterpret_cast<unsigned char*>(this) + attr.offset_;
  115. switch (attr.type_)
  116. {
  117. case VAR_INT:
  118. // If enum type, use the low 8 bits only
  119. if (attr.enumNames_)
  120. dest = *(reinterpret_cast<const unsigned char*>(src));
  121. else
  122. dest = *(reinterpret_cast<const int*>(src));
  123. break;
  124. case VAR_BOOL:
  125. dest = *(reinterpret_cast<const bool*>(src));
  126. break;
  127. case VAR_FLOAT:
  128. dest = *(reinterpret_cast<const float*>(src));
  129. break;
  130. case VAR_VECTOR2:
  131. dest = *(reinterpret_cast<const Vector2*>(src));
  132. break;
  133. case VAR_VECTOR3:
  134. dest = *(reinterpret_cast<const Vector3*>(src));
  135. break;
  136. case VAR_VECTOR4:
  137. dest = *(reinterpret_cast<const Vector4*>(src));
  138. break;
  139. case VAR_QUATERNION:
  140. dest = *(reinterpret_cast<const Quaternion*>(src));
  141. break;
  142. case VAR_COLOR:
  143. dest = *(reinterpret_cast<const Color*>(src));
  144. break;
  145. case VAR_STRING:
  146. dest = *(reinterpret_cast<const String*>(src));
  147. break;
  148. case VAR_BUFFER:
  149. dest = *(reinterpret_cast<const PODVector<unsigned char>*>(src));
  150. break;
  151. case VAR_RESOURCEREF:
  152. dest = *(reinterpret_cast<const ResourceRef*>(src));
  153. break;
  154. case VAR_RESOURCEREFLIST:
  155. dest = *(reinterpret_cast<const ResourceRefList*>(src));
  156. break;
  157. case VAR_VARIANTVECTOR:
  158. dest = *(reinterpret_cast<const VariantVector*>(src));
  159. break;
  160. case VAR_VARIANTMAP:
  161. dest = *(reinterpret_cast<const VariantMap*>(src));
  162. break;
  163. }
  164. }
  165. bool Serializable::Load(Deserializer& source)
  166. {
  167. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  168. if (!attributes)
  169. return true;
  170. for (unsigned i = 0; i < attributes->Size(); ++i)
  171. {
  172. const AttributeInfo& attr = attributes->At(i);
  173. if (!(attr.mode_ & AM_FILE))
  174. continue;
  175. if (source.IsEof())
  176. {
  177. LOGERROR("Could not load " + GetTypeName() + ", stream not open or at end");
  178. return false;
  179. }
  180. OnSetAttribute(attr, source.ReadVariant(attr.type_));
  181. }
  182. return true;
  183. }
  184. bool Serializable::Save(Serializer& dest)
  185. {
  186. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  187. if (!attributes)
  188. return true;
  189. Variant value;
  190. for (unsigned i = 0; i < attributes->Size(); ++i)
  191. {
  192. const AttributeInfo& attr = attributes->At(i);
  193. if (!(attr.mode_ & AM_FILE))
  194. continue;
  195. OnGetAttribute(attr, value);
  196. if (!dest.WriteVariantData(value))
  197. {
  198. LOGERROR("Could not save " + GetTypeName() + ", writing to stream failed");
  199. return false;
  200. }
  201. }
  202. return true;
  203. }
  204. bool Serializable::LoadXML(const XMLElement& source)
  205. {
  206. if (source.IsNull())
  207. {
  208. LOGERROR("Could not load " + GetTypeName() + ", null source element");
  209. return false;
  210. }
  211. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  212. if (!attributes)
  213. return true;
  214. XMLElement attrElem = source.GetChild("attribute");
  215. unsigned startIndex = 0;
  216. while (attrElem)
  217. {
  218. const char* name = attrElem.GetAttribute("name");
  219. unsigned i = startIndex;
  220. unsigned attempts = attributes->Size();
  221. while (attempts)
  222. {
  223. const AttributeInfo& attr = attributes->At(i);
  224. if ((attr.mode_ & AM_FILE) && !String::Compare(attr.name_, name, true))
  225. {
  226. // If enums specified, do enum lookup and int assignment. Otherwise assign the variant directly
  227. if (attr.enumNames_)
  228. {
  229. const char* value = attrElem.GetAttribute("value");
  230. const char** enumPtr = attr.enumNames_;
  231. int enumValue = 0;
  232. bool enumFound = false;
  233. while (*enumPtr)
  234. {
  235. if (!String::Compare(*enumPtr, value, true))
  236. {
  237. enumFound = true;
  238. break;
  239. }
  240. ++enumPtr;
  241. ++enumValue;
  242. }
  243. if (enumFound)
  244. OnSetAttribute(attr, Variant(enumValue));
  245. else
  246. LOGWARNING("Unknown enum value " + String(value) + " in attribute " + String(attr.name_));
  247. }
  248. else
  249. OnSetAttribute(attr, attrElem.GetVariantValue(attr.type_));
  250. startIndex = (i + 1) % attributes->Size();
  251. break;
  252. }
  253. else
  254. {
  255. i = (i + 1) % attributes->Size();
  256. --attempts;
  257. }
  258. }
  259. if (!attempts)
  260. LOGWARNING("Unknown attribute " + String(name) + " in XML data");
  261. attrElem = attrElem.GetNext("attribute");
  262. }
  263. return true;
  264. }
  265. bool Serializable::SaveXML(XMLElement& dest)
  266. {
  267. if (dest.IsNull())
  268. {
  269. LOGERROR("Could not save " + GetTypeName() + ", null destination element");
  270. return false;
  271. }
  272. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  273. if (!attributes)
  274. return true;
  275. Variant value;
  276. for (unsigned i = 0; i < attributes->Size(); ++i)
  277. {
  278. const AttributeInfo& attr = attributes->At(i);
  279. if (!(attr.mode_ & AM_FILE))
  280. continue;
  281. XMLElement attrElem = dest.CreateChild("attribute");
  282. attrElem.SetAttribute("name", attr.name_);
  283. OnGetAttribute(attr, value);
  284. // If enums specified, set as an enum string. Otherwise set directly as a Variant
  285. if (attr.enumNames_)
  286. {
  287. int enumValue = value.GetInt();
  288. attrElem.SetAttribute("value", attr.enumNames_[enumValue]);
  289. }
  290. else
  291. attrElem.SetVariantValue(value);
  292. }
  293. return true;
  294. }
  295. bool Serializable::SetAttribute(unsigned index, const Variant& value)
  296. {
  297. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  298. if (!attributes)
  299. {
  300. LOGERROR(GetTypeName() + " has no attributes");
  301. return false;
  302. }
  303. if (index >= attributes->Size())
  304. {
  305. LOGERROR("Attribute index out of bounds");
  306. return false;
  307. }
  308. const AttributeInfo& attr = attributes->At(index);
  309. // Check that the new value's type matches the attribute type
  310. if (value.GetType() == attr.type_)
  311. {
  312. OnSetAttribute(attr, value);
  313. return true;
  314. }
  315. else
  316. {
  317. LOGERROR("Could not set attribute " + String(attr.name_) + ": expected type " + Variant::GetTypeName(attr.type_) +
  318. " but got " + value.GetTypeName());
  319. return false;
  320. }
  321. }
  322. bool Serializable::SetAttribute(const String& name, const Variant& value)
  323. {
  324. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  325. if (!attributes)
  326. {
  327. LOGERROR(GetTypeName() + " has no attributes");
  328. return false;
  329. }
  330. for (Vector<AttributeInfo>::ConstIterator i = attributes->Begin(); i != attributes->End(); ++i)
  331. {
  332. if (!String::Compare(i->name_, name.CString(), true))
  333. {
  334. // Check that the new value's type matches the attribute type
  335. if (value.GetType() == i->type_)
  336. {
  337. OnSetAttribute(*i, value);
  338. return true;
  339. }
  340. else
  341. {
  342. LOGERROR("Could not set attribute " + String(i->name_) + ": expected type " + Variant::GetTypeName(i->type_)
  343. + " but got " + value.GetTypeName());
  344. return false;
  345. }
  346. }
  347. }
  348. LOGERROR("Could not find attribute " + String(name) + " in " + GetTypeName());
  349. return false;
  350. }
  351. void Serializable::AllocateNetworkState()
  352. {
  353. if (!networkState_)
  354. {
  355. networkState_ = new NetworkState();
  356. networkState_->attributes_ = context_->GetNetworkAttributes(GetType());
  357. }
  358. }
  359. void Serializable::WriteInitialDeltaUpdate(Serializer& dest)
  360. {
  361. if (!networkState_)
  362. {
  363. LOGERROR("WriteInitialDeltaUpdate called without allocated NetworkState");
  364. return;
  365. }
  366. const Vector<AttributeInfo>* attributes = networkState_->attributes_;
  367. if (!attributes)
  368. return;
  369. unsigned numAttributes = attributes->Size();
  370. DirtyBits attributeBits;
  371. // Compare against defaults
  372. for (unsigned i = 0; i < numAttributes; ++i)
  373. {
  374. const AttributeInfo& attr = attributes->At(i);
  375. if (networkState_->currentValues_[i] != attr.defaultValue_)
  376. attributeBits.Set(i);
  377. }
  378. // First write the change bitfield, then attribute data for non-default attributes
  379. dest.Write(attributeBits.data_, (numAttributes + 7) >> 3);
  380. for (unsigned i = 0; i < numAttributes; ++i)
  381. {
  382. if (attributeBits.IsSet(i))
  383. dest.WriteVariantData(networkState_->currentValues_[i]);
  384. }
  385. }
  386. void Serializable::WriteDeltaUpdate(Serializer& dest, const DirtyBits& attributeBits)
  387. {
  388. if (!networkState_)
  389. {
  390. LOGERROR("WriteDeltaUpdate called without allocated NetworkState");
  391. return;
  392. }
  393. const Vector<AttributeInfo>* attributes = networkState_->attributes_;
  394. if (!attributes)
  395. return;
  396. unsigned numAttributes = attributes->Size();
  397. // First write the change bitfield, then attribute data for changed attributes
  398. // Note: the attribute bits should not contain LATESTDATA attributes
  399. dest.Write(attributeBits.data_, (numAttributes + 7) >> 3);
  400. for (unsigned i = 0; i < numAttributes; ++i)
  401. {
  402. if (attributeBits.IsSet(i))
  403. dest.WriteVariantData(networkState_->currentValues_[i]);
  404. }
  405. }
  406. void Serializable::WriteLatestDataUpdate(Serializer& dest)
  407. {
  408. if (!networkState_)
  409. {
  410. LOGERROR("WriteLatestDataUpdate called without allocated NetworkState");
  411. return;
  412. }
  413. const Vector<AttributeInfo>* attributes = networkState_->attributes_;
  414. if (!attributes)
  415. return;
  416. unsigned numAttributes = attributes->Size();
  417. for (unsigned i = 0; i < numAttributes; ++i)
  418. {
  419. if (attributes->At(i).mode_ & AM_LATESTDATA)
  420. dest.WriteVariantData(networkState_->currentValues_[i]);
  421. }
  422. }
  423. void Serializable::ReadDeltaUpdate(Deserializer& source)
  424. {
  425. const Vector<AttributeInfo>* attributes = GetNetworkAttributes();
  426. if (!attributes)
  427. return;
  428. unsigned numAttributes = attributes->Size();
  429. DirtyBits attributeBits;
  430. source.Read(attributeBits.data_, (numAttributes + 7) >> 3);
  431. for (unsigned i = 0; i < numAttributes && !source.IsEof(); ++i)
  432. {
  433. if (attributeBits.IsSet(i))
  434. {
  435. const AttributeInfo& attr = attributes->At(i);
  436. OnSetAttribute(attr, source.ReadVariant(attr.type_));
  437. }
  438. }
  439. }
  440. void Serializable::ReadLatestDataUpdate(Deserializer& source)
  441. {
  442. const Vector<AttributeInfo>* attributes = GetNetworkAttributes();
  443. if (!attributes)
  444. return;
  445. unsigned numAttributes = attributes->Size();
  446. for (unsigned i = 0; i < numAttributes && !source.IsEof(); ++i)
  447. {
  448. const AttributeInfo& attr = attributes->At(i);
  449. if (attr.mode_ & AM_LATESTDATA)
  450. OnSetAttribute(attr, source.ReadVariant(attr.type_));
  451. }
  452. }
  453. Variant Serializable::GetAttribute(unsigned index)
  454. {
  455. Variant ret;
  456. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  457. if (!attributes || index >= attributes->Size())
  458. return ret;
  459. OnGetAttribute(attributes->At(index), ret);
  460. return ret;
  461. }
  462. Variant Serializable::GetAttribute(const String& name)
  463. {
  464. Variant ret;
  465. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  466. if (!attributes)
  467. {
  468. LOGERROR(GetTypeName() + " has no attributes");
  469. return ret;
  470. }
  471. for (Vector<AttributeInfo>::ConstIterator i = attributes->Begin(); i != attributes->End(); ++i)
  472. {
  473. if (!String::Compare(i->name_, name.CString(), true))
  474. {
  475. OnGetAttribute(*i, ret);
  476. return ret;
  477. }
  478. }
  479. LOGERROR("Could not find attribute " + String(name) + " in " + GetTypeName());
  480. return ret;
  481. }
  482. unsigned Serializable::GetNumAttributes() const
  483. {
  484. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  485. return attributes ? attributes->Size() : 0;
  486. }
  487. unsigned Serializable::GetNumNetworkAttributes() const
  488. {
  489. const Vector<AttributeInfo>* attributes = networkState_ ? networkState_->attributes_ :
  490. context_->GetNetworkAttributes(GetType());
  491. return attributes ? attributes->Size() : 0;
  492. }
  493. const Vector<AttributeInfo>* Serializable::GetAttributes() const
  494. {
  495. return context_->GetAttributes(GetType());
  496. }
  497. const Vector<AttributeInfo>* Serializable::GetNetworkAttributes() const
  498. {
  499. return networkState_ ? networkState_->attributes_ : context_->GetNetworkAttributes(GetType());
  500. }
  501. }