Serializable.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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 "Serializable.h"
  28. #include "Serializer.h"
  29. #include "XMLElement.h"
  30. #include "DebugNew.h"
  31. OBJECTTYPESTATIC(Serializable);
  32. Serializable::Serializable(Context* context) :
  33. Object(context),
  34. loading_(false)
  35. {
  36. }
  37. Serializable::~Serializable()
  38. {
  39. }
  40. void Serializable::OnSetAttribute(const AttributeInfo& attr, const Variant& src)
  41. {
  42. // Check for accessor function mode
  43. if (attr.accessor_)
  44. {
  45. attr.accessor_->Set(this, src);
  46. return;
  47. }
  48. // Calculate the destination address
  49. void* dest = reinterpret_cast<unsigned char*>(this) + attr.offset_;
  50. switch (attr.type_)
  51. {
  52. case VAR_INT:
  53. // If enum type, use the low 8 bits only (assume full value to be initialized)
  54. if (attr.enumNames_)
  55. *(reinterpret_cast<unsigned char*>(dest)) = src.GetInt();
  56. else
  57. *(reinterpret_cast<int*>(dest)) = src.GetInt();
  58. break;
  59. case VAR_BOOL:
  60. *(reinterpret_cast<bool*>(dest)) = src.GetBool();
  61. break;
  62. case VAR_FLOAT:
  63. *(reinterpret_cast<float*>(dest)) = src.GetFloat();
  64. break;
  65. case VAR_VECTOR2:
  66. *(reinterpret_cast<Vector2*>(dest)) = src.GetVector2();
  67. break;
  68. case VAR_VECTOR3:
  69. *(reinterpret_cast<Vector3*>(dest)) = src.GetVector3();
  70. break;
  71. case VAR_VECTOR4:
  72. *(reinterpret_cast<Vector4*>(dest)) = src.GetVector4();
  73. break;
  74. case VAR_QUATERNION:
  75. *(reinterpret_cast<Quaternion*>(dest)) = src.GetQuaternion();
  76. break;
  77. case VAR_COLOR:
  78. *(reinterpret_cast<Color*>(dest)) = src.GetColor();
  79. break;
  80. case VAR_STRING:
  81. *(reinterpret_cast<String*>(dest)) = src.GetString();
  82. break;
  83. case VAR_BUFFER:
  84. *(reinterpret_cast<PODVector<unsigned char>*>(dest)) = src.GetBuffer();
  85. break;
  86. case VAR_RESOURCEREF:
  87. *(reinterpret_cast<ResourceRef*>(dest)) = src.GetResourceRef();
  88. break;
  89. case VAR_RESOURCEREFLIST:
  90. *(reinterpret_cast<ResourceRefList*>(dest)) = src.GetResourceRefList();
  91. break;
  92. case VAR_VARIANTVECTOR:
  93. *(reinterpret_cast<VariantVector*>(dest)) = src.GetVariantVector();
  94. break;
  95. case VAR_VARIANTMAP:
  96. *(reinterpret_cast<VariantMap*>(dest)) = src.GetVariantMap();
  97. break;
  98. }
  99. }
  100. void Serializable::OnGetAttribute(const AttributeInfo& attr, Variant& dest)
  101. {
  102. // Check for accessor function mode
  103. if (attr.accessor_)
  104. {
  105. attr.accessor_->Get(this, dest);
  106. return;
  107. }
  108. // Calculate the source address
  109. void* src = reinterpret_cast<unsigned char*>(this) + attr.offset_;
  110. switch (attr.type_)
  111. {
  112. case VAR_INT:
  113. // If enum type, use the low 8 bits only
  114. if (attr.enumNames_)
  115. dest = *(reinterpret_cast<const unsigned char*>(src));
  116. else
  117. dest = *(reinterpret_cast<const int*>(src));
  118. break;
  119. case VAR_BOOL:
  120. dest = *(reinterpret_cast<const bool*>(src));
  121. break;
  122. case VAR_FLOAT:
  123. dest = *(reinterpret_cast<const float*>(src));
  124. break;
  125. case VAR_VECTOR2:
  126. dest = *(reinterpret_cast<const Vector2*>(src));
  127. break;
  128. case VAR_VECTOR3:
  129. dest = *(reinterpret_cast<const Vector3*>(src));
  130. break;
  131. case VAR_VECTOR4:
  132. dest = *(reinterpret_cast<const Vector4*>(src));
  133. break;
  134. case VAR_QUATERNION:
  135. dest = *(reinterpret_cast<const Quaternion*>(src));
  136. break;
  137. case VAR_COLOR:
  138. dest = *(reinterpret_cast<const Color*>(src));
  139. break;
  140. case VAR_STRING:
  141. dest = *(reinterpret_cast<const String*>(src));
  142. break;
  143. case VAR_BUFFER:
  144. dest = *(reinterpret_cast<const PODVector<unsigned char>*>(src));
  145. break;
  146. case VAR_RESOURCEREF:
  147. dest = *(reinterpret_cast<const ResourceRef*>(src));
  148. break;
  149. case VAR_RESOURCEREFLIST:
  150. dest = *(reinterpret_cast<const ResourceRefList*>(src));
  151. break;
  152. case VAR_VARIANTVECTOR:
  153. dest = *(reinterpret_cast<const VariantVector*>(src));
  154. break;
  155. case VAR_VARIANTMAP:
  156. dest = *(reinterpret_cast<const VariantMap*>(src));
  157. break;
  158. }
  159. }
  160. bool Serializable::Load(Deserializer& source)
  161. {
  162. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  163. if (!attributes)
  164. return true;
  165. loading_ = true;
  166. for (unsigned i = 0; i < attributes->Size(); ++i)
  167. {
  168. const AttributeInfo& attr = attributes->At(i);
  169. if (!(attr.mode_ & AM_FILE))
  170. continue;
  171. if (source.IsEof())
  172. {
  173. LOGERROR("Could not load " + GetTypeName() + ", stream not open or at end");
  174. loading_ = false;
  175. return false;
  176. }
  177. OnSetAttribute(attr, source.ReadVariant(attr.type_));
  178. }
  179. loading_ = false;
  180. return true;
  181. }
  182. bool Serializable::Save(Serializer& dest)
  183. {
  184. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  185. if (!attributes)
  186. return true;
  187. Variant value;
  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. OnGetAttribute(attr, value);
  194. if (!dest.WriteVariantData(value))
  195. {
  196. LOGERROR("Could not save " + GetTypeName() + ", writing to stream failed");
  197. return false;
  198. }
  199. }
  200. return true;
  201. }
  202. bool Serializable::LoadXML(const XMLElement& source)
  203. {
  204. if (source.IsNull())
  205. {
  206. LOGERROR("Could not load " + GetTypeName() + ", null source element");
  207. return false;
  208. }
  209. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  210. if (!attributes)
  211. return true;
  212. loading_ = true;
  213. XMLElement attrElem = source.GetChild("attribute");
  214. unsigned startIndex = 0;
  215. while (attrElem)
  216. {
  217. String name = attrElem.GetString("name");
  218. unsigned i = startIndex;
  219. unsigned attempts = attributes->Size();
  220. while (attempts)
  221. {
  222. const AttributeInfo& attr = attributes->At(i);
  223. if ((attr.mode_ & AM_FILE) && attr.name_ == name)
  224. {
  225. // If enums specified, do enum lookup and int assignment. Otherwise assign the variant directly
  226. if (attr.enumNames_)
  227. {
  228. String value = attrElem.GetString("value");
  229. const String* enumPtr = attr.enumNames_;
  230. int enumValue = 0;
  231. bool enumFound = false;
  232. while (enumPtr->Length())
  233. {
  234. if (*enumPtr == value)
  235. {
  236. enumFound = true;
  237. break;
  238. }
  239. ++enumPtr;
  240. ++enumValue;
  241. }
  242. if (enumFound)
  243. OnSetAttribute(attr, Variant(enumValue));
  244. else
  245. LOGWARNING("Unknown enum value " + value + " in attribute " + String(attr.name_));
  246. }
  247. else
  248. OnSetAttribute(attr, attrElem.GetVariantValue(attr.type_));
  249. startIndex = (i + 1) % attributes->Size();
  250. break;
  251. }
  252. else
  253. {
  254. i = (i + 1) % attributes->Size();
  255. --attempts;
  256. }
  257. }
  258. if (!attempts)
  259. LOGWARNING("Unknown attribute " + name + " in XML data");
  260. attrElem = attrElem.GetNext("attribute");
  261. }
  262. loading_ = false;
  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.SetString("name", String(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.SetString("value", String(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 (i->name_ == name)
  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::WriteInitialDeltaUpdate(Serializer& dest, PODVector<unsigned char>& deltaUpdateBits,
  352. Vector<Variant>& replicationState)
  353. {
  354. const Vector<AttributeInfo>* attributes = GetNetworkAttributes();
  355. if (!attributes)
  356. return;
  357. unsigned numAttributes = attributes->Size();
  358. replicationState.Resize(numAttributes);
  359. deltaUpdateBits.Resize((numAttributes + 7) >> 3);
  360. for (unsigned i = 0; i < deltaUpdateBits.Size(); ++i)
  361. deltaUpdateBits[i] = 0;
  362. // Set initial attribute values and compare against defaults
  363. for (unsigned i = 0; i < numAttributes; ++i)
  364. {
  365. const AttributeInfo& attr = attributes->At(i);
  366. OnGetAttribute(attr, replicationState[i]);
  367. if (replicationState[i] != attr.defaultValue_)
  368. deltaUpdateBits[i >> 3] |= (1 << (i & 7));
  369. }
  370. // First write the change bitfield, then attribute data for non-default attributes
  371. dest.Write(&deltaUpdateBits[0], deltaUpdateBits.Size());
  372. for (unsigned i = 0; i < numAttributes; ++i)
  373. {
  374. const AttributeInfo& attr = attributes->At(i);
  375. if (deltaUpdateBits[i >> 3] & (1 << (i & 7)))
  376. dest.WriteVariantData(replicationState[i]);
  377. }
  378. }
  379. void Serializable::PrepareUpdates(PODVector<unsigned char>& deltaUpdateBits, Vector<Variant>& classCurrentState,
  380. Vector<Variant>& replicationState, bool& deltaUpdate, bool& latestData)
  381. {
  382. deltaUpdate = false;
  383. latestData = false;
  384. const Vector<AttributeInfo>* attributes = GetNetworkAttributes();
  385. if (!attributes)
  386. return;
  387. unsigned numAttributes = attributes->Size();
  388. deltaUpdateBits.Resize((numAttributes + 7) >> 3);
  389. for (unsigned i = 0; i < deltaUpdateBits.Size(); ++i)
  390. deltaUpdateBits[i] = 0;
  391. // If class-specific current state has not been previously used, resize it now
  392. if (classCurrentState.Empty())
  393. classCurrentState.Resize(numAttributes);
  394. for (unsigned i = 0; i < numAttributes; ++i)
  395. {
  396. const AttributeInfo& attr = attributes->At(i);
  397. // Check for attribute change
  398. OnGetAttribute(attr, classCurrentState[i]);
  399. if (classCurrentState[i] != replicationState[i])
  400. {
  401. replicationState[i] = classCurrentState[i];
  402. if (attr.mode_ & AM_LATESTDATA)
  403. latestData = true;
  404. else
  405. {
  406. deltaUpdate = true;
  407. deltaUpdateBits[i >> 3] |= 1 << (i & 7);
  408. }
  409. }
  410. }
  411. }
  412. void Serializable::WriteDeltaUpdate(Serializer& dest, PODVector<unsigned char>& deltaUpdateBits, Vector<Variant>& replicationState)
  413. {
  414. const Vector<AttributeInfo>* attributes = GetNetworkAttributes();
  415. if (!attributes)
  416. return;
  417. unsigned numAttributes = attributes->Size();
  418. // First write the change bitfield, then attribute data for changed attributes
  419. dest.Write(&deltaUpdateBits[0], deltaUpdateBits.Size());
  420. for (unsigned i = 0; i < numAttributes; ++i)
  421. {
  422. if (deltaUpdateBits[i >> 3] & (1 << (i & 7)))
  423. dest.WriteVariantData(replicationState[i]);
  424. }
  425. }
  426. void Serializable::WriteLatestDataUpdate(Serializer& dest, Vector<Variant>& replicationState)
  427. {
  428. const Vector<AttributeInfo>* attributes = GetNetworkAttributes();
  429. if (!attributes)
  430. return;
  431. unsigned numAttributes = attributes->Size();
  432. for (unsigned i = 0; i < numAttributes; ++i)
  433. {
  434. if (attributes->At(i).mode_ & AM_LATESTDATA)
  435. dest.WriteVariantData(replicationState[i]);
  436. }
  437. }
  438. void Serializable::ReadDeltaUpdate(Deserializer& source, PODVector<unsigned char>& deltaUpdateBits)
  439. {
  440. const Vector<AttributeInfo>* attributes = GetNetworkAttributes();
  441. if (!attributes)
  442. return;
  443. unsigned numAttributes = attributes->Size();
  444. deltaUpdateBits.Resize((numAttributes + 7) >> 3);
  445. source.Read(&deltaUpdateBits[0], deltaUpdateBits.Size());
  446. for (unsigned i = 0; i < numAttributes && !source.IsEof(); ++i)
  447. {
  448. const AttributeInfo& attr = attributes->At(i);
  449. if (deltaUpdateBits[i >> 3] & (1 << (i & 7)))
  450. OnSetAttribute(attr, source.ReadVariant(attr.type_));
  451. }
  452. }
  453. void Serializable::ReadLatestDataUpdate(Deserializer& source)
  454. {
  455. const Vector<AttributeInfo>* attributes = GetNetworkAttributes();
  456. if (!attributes)
  457. return;
  458. unsigned numAttributes = attributes->Size();
  459. for (unsigned i = 0; i < numAttributes && !source.IsEof(); ++i)
  460. {
  461. const AttributeInfo& attr = attributes->At(i);
  462. if (attr.mode_ & AM_LATESTDATA)
  463. OnSetAttribute(attr, source.ReadVariant(attr.type_));
  464. }
  465. }
  466. Variant Serializable::GetAttribute(unsigned index)
  467. {
  468. Variant ret;
  469. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  470. if (!attributes || index >= attributes->Size())
  471. return ret;
  472. OnGetAttribute(attributes->At(index), ret);
  473. return ret;
  474. }
  475. Variant Serializable::GetAttribute(const String& name)
  476. {
  477. Variant ret;
  478. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  479. if (!attributes)
  480. {
  481. LOGERROR(GetTypeName() + " has no attributes");
  482. return ret;
  483. }
  484. for (Vector<AttributeInfo>::ConstIterator i = attributes->Begin(); i != attributes->End(); ++i)
  485. {
  486. if (i->name_ == name)
  487. {
  488. OnGetAttribute(*i, ret);
  489. return ret;
  490. }
  491. }
  492. LOGERROR("Could not find attribute " + String(name) + " in " + GetTypeName());
  493. return ret;
  494. }
  495. unsigned Serializable::GetNumAttributes() const
  496. {
  497. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  498. return attributes ? attributes->Size() : 0;
  499. }
  500. unsigned Serializable::GetNumNetworkAttributes() const
  501. {
  502. const Vector<AttributeInfo>* attributes = context_->GetNetworkAttributes(GetType());
  503. return attributes ? attributes->Size() : 0;
  504. }
  505. const Vector<AttributeInfo>* Serializable::GetAttributes() const
  506. {
  507. return context_->GetAttributes(GetType());
  508. }
  509. const Vector<AttributeInfo>* Serializable::GetNetworkAttributes() const
  510. {
  511. return context_->GetNetworkAttributes(GetType());
  512. }