Serializable.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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. inSerialization_(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. inSerialization_ = 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. inSerialization_ = false;
  175. return false;
  176. }
  177. OnSetAttribute(attr, source.ReadVariant(attr.type_));
  178. }
  179. inSerialization_ = 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. inSerialization_ = true;
  189. for (unsigned i = 0; i < attributes->Size(); ++i)
  190. {
  191. const AttributeInfo& attr = attributes->At(i);
  192. if (!(attr.mode_ & AM_FILE))
  193. continue;
  194. OnGetAttribute(attr, value);
  195. if (!dest.WriteVariantData(value))
  196. {
  197. LOGERROR("Could not save " + GetTypeName() + ", writing to stream failed");
  198. inSerialization_ = false;
  199. return false;
  200. }
  201. }
  202. inSerialization_ = false;
  203. return true;
  204. }
  205. bool Serializable::LoadXML(const XMLElement& source)
  206. {
  207. if (source.IsNull())
  208. {
  209. LOGERROR("Could not load " + GetTypeName() + ", null source element");
  210. return false;
  211. }
  212. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  213. if (!attributes)
  214. return true;
  215. inSerialization_ = true;
  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. XMLElement attrElem = source.GetChild("attribute");
  222. bool found = false;
  223. while (attrElem)
  224. {
  225. if (attrElem.GetString("name") == attr.name_)
  226. {
  227. // If enums specified, do enum lookup and int assignment. Otherwise assign the variant directly
  228. if (attr.enumNames_)
  229. {
  230. String value = attrElem.GetString("value");
  231. const String* enumPtr = attr.enumNames_;
  232. int enumValue = 0;
  233. bool enumFound = false;
  234. while (enumPtr->Length())
  235. {
  236. if (*enumPtr == value)
  237. {
  238. enumFound = true;
  239. break;
  240. }
  241. ++enumPtr;
  242. ++enumValue;
  243. }
  244. if (enumFound)
  245. OnSetAttribute(attr, Variant(enumValue));
  246. else
  247. LOGWARNING("Unknown enum value " + value + " in attribute " + String(attr.name_));
  248. }
  249. else
  250. OnSetAttribute(attr, attrElem.GetVariant());
  251. found = true;
  252. break;
  253. }
  254. attrElem = attrElem.GetNext("attribute");
  255. }
  256. if (!found)
  257. LOGWARNING("Attribute " + String(attr.name_) + " not found in XML data");
  258. }
  259. inSerialization_ = false;
  260. return true;
  261. }
  262. bool Serializable::SaveXML(XMLElement& dest)
  263. {
  264. if (dest.IsNull())
  265. {
  266. LOGERROR("Could not save " + GetTypeName() + ", null destination element");
  267. return false;
  268. }
  269. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  270. if (!attributes)
  271. return true;
  272. Variant value;
  273. inSerialization_ = true;
  274. for (unsigned i = 0; i < attributes->Size(); ++i)
  275. {
  276. const AttributeInfo& attr = attributes->At(i);
  277. if (!(attr.mode_ & AM_FILE))
  278. continue;
  279. XMLElement attrElem = dest.CreateChild("attribute");
  280. attrElem.SetString("name", String(attr.name_));
  281. OnGetAttribute(attr, value);
  282. // If enums specified, set as an enum string. Otherwise set directly as a Variant
  283. if (attr.enumNames_)
  284. {
  285. int enumValue = value.GetInt();
  286. attrElem.SetString("type", "Enum");
  287. attrElem.SetString("value", String(attr.enumNames_[enumValue]));
  288. }
  289. else
  290. attrElem.SetVariant(value);
  291. }
  292. inSerialization_ = false;
  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. // If class-specific current state has not been previously used, resize it now
  389. if (classCurrentState.Empty())
  390. classCurrentState.Resize(numAttributes);
  391. deltaUpdateBits.Resize((numAttributes + 7) >> 3);
  392. for (unsigned i = 0; i < deltaUpdateBits.Size(); ++i)
  393. deltaUpdateBits[i] = 0;
  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. const AttributeInfo& attr = attributes->At(i);
  423. if (deltaUpdateBits[i >> 3] & (1 << (i & 7)))
  424. dest.WriteVariantData(replicationState[i]);
  425. }
  426. }
  427. void Serializable::WriteLatestDataUpdate(Serializer& dest, Vector<Variant>& replicationState)
  428. {
  429. const Vector<AttributeInfo>* attributes = GetNetworkAttributes();
  430. if (!attributes)
  431. return;
  432. unsigned numAttributes = attributes->Size();
  433. for (unsigned i = 0; i < numAttributes; ++i)
  434. {
  435. const AttributeInfo& attr = attributes->At(i);
  436. if (attr.mode_ & AM_LATESTDATA)
  437. dest.WriteVariantData(replicationState[i]);
  438. }
  439. }
  440. void Serializable::ReadDeltaUpdate(Deserializer& source, PODVector<unsigned char>& deltaUpdateBits)
  441. {
  442. const Vector<AttributeInfo>* attributes = GetNetworkAttributes();
  443. if (!attributes)
  444. return;
  445. unsigned numAttributes = attributes->Size();
  446. deltaUpdateBits.Resize((numAttributes + 7) >> 3);
  447. source.Read(&deltaUpdateBits[0], deltaUpdateBits.Size());
  448. for (unsigned i = 0; i < numAttributes && !source.IsEof(); ++i)
  449. {
  450. const AttributeInfo& attr = attributes->At(i);
  451. if (deltaUpdateBits[i >> 3] & (1 << (i & 7)))
  452. OnSetAttribute(attr, source.ReadVariant(attr.type_));
  453. }
  454. }
  455. void Serializable::ReadLatestDataUpdate(Deserializer& source)
  456. {
  457. const Vector<AttributeInfo>* attributes = GetNetworkAttributes();
  458. if (!attributes)
  459. return;
  460. unsigned numAttributes = attributes->Size();
  461. for (unsigned i = 0; i < numAttributes && !source.IsEof(); ++i)
  462. {
  463. const AttributeInfo& attr = attributes->At(i);
  464. if (attr.mode_ & AM_LATESTDATA)
  465. OnSetAttribute(attr, source.ReadVariant(attr.type_));
  466. }
  467. }
  468. Variant Serializable::GetAttribute(unsigned index)
  469. {
  470. Variant ret;
  471. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  472. if (!attributes || index >= attributes->Size())
  473. return ret;
  474. OnGetAttribute(attributes->At(index), ret);
  475. return ret;
  476. }
  477. Variant Serializable::GetAttribute(const String& name)
  478. {
  479. Variant ret;
  480. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  481. if (!attributes)
  482. {
  483. LOGERROR(GetTypeName() + " has no attributes");
  484. return ret;
  485. }
  486. for (Vector<AttributeInfo>::ConstIterator i = attributes->Begin(); i != attributes->End(); ++i)
  487. {
  488. if (i->name_ == name)
  489. {
  490. OnGetAttribute(*i, ret);
  491. return ret;
  492. }
  493. }
  494. LOGERROR("Could not find attribute " + String(name) + " in " + GetTypeName());
  495. return ret;
  496. }
  497. unsigned Serializable::GetNumAttributes() const
  498. {
  499. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  500. return attributes ? attributes->Size() : 0;
  501. }
  502. unsigned Serializable::GetNumNetworkAttributes() const
  503. {
  504. const Vector<AttributeInfo>* attributes = context_->GetNetworkAttributes(GetType());
  505. return attributes ? attributes->Size() : 0;
  506. }
  507. const Vector<AttributeInfo>* Serializable::GetAttributes() const
  508. {
  509. return context_->GetAttributes(GetType());
  510. }
  511. const Vector<AttributeInfo>* Serializable::GetNetworkAttributes() const
  512. {
  513. return context_->GetNetworkAttributes(GetType());
  514. }