Serializable.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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. for (unsigned i = 0; i < attributes->Size(); ++i)
  214. {
  215. const AttributeInfo& attr = attributes->At(i);
  216. if (!(attr.mode_ & AM_FILE))
  217. continue;
  218. XMLElement attrElem = source.GetChild("attribute");
  219. bool found = false;
  220. while (attrElem)
  221. {
  222. if (attrElem.GetString("name") == attr.name_)
  223. {
  224. // If enums specified, do enum lookup and int assignment. Otherwise assign the variant directly
  225. if (attr.enumNames_)
  226. {
  227. String value = attrElem.GetString("value");
  228. const String* enumPtr = attr.enumNames_;
  229. int enumValue = 0;
  230. bool enumFound = false;
  231. while (enumPtr->Length())
  232. {
  233. if (*enumPtr == value)
  234. {
  235. enumFound = true;
  236. break;
  237. }
  238. ++enumPtr;
  239. ++enumValue;
  240. }
  241. if (enumFound)
  242. OnSetAttribute(attr, Variant(enumValue));
  243. else
  244. LOGWARNING("Unknown enum value " + value + " in attribute " + String(attr.name_));
  245. }
  246. else
  247. OnSetAttribute(attr, attrElem.GetVariant());
  248. found = true;
  249. break;
  250. }
  251. attrElem = attrElem.GetNext("attribute");
  252. }
  253. if (!found)
  254. LOGWARNING("Attribute " + String(attr.name_) + " not found in XML data");
  255. }
  256. loading_ = false;
  257. return true;
  258. }
  259. bool Serializable::SaveXML(XMLElement& dest)
  260. {
  261. if (dest.IsNull())
  262. {
  263. LOGERROR("Could not save " + GetTypeName() + ", null destination element");
  264. return false;
  265. }
  266. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  267. if (!attributes)
  268. return true;
  269. Variant value;
  270. for (unsigned i = 0; i < attributes->Size(); ++i)
  271. {
  272. const AttributeInfo& attr = attributes->At(i);
  273. if (!(attr.mode_ & AM_FILE))
  274. continue;
  275. XMLElement attrElem = dest.CreateChild("attribute");
  276. attrElem.SetString("name", String(attr.name_));
  277. OnGetAttribute(attr, value);
  278. // If enums specified, set as an enum string. Otherwise set directly as a Variant
  279. if (attr.enumNames_)
  280. {
  281. int enumValue = value.GetInt();
  282. attrElem.SetString("type", "Enum");
  283. attrElem.SetString("value", String(attr.enumNames_[enumValue]));
  284. }
  285. else
  286. attrElem.SetVariant(value);
  287. }
  288. return true;
  289. }
  290. bool Serializable::SetAttribute(unsigned index, const Variant& value)
  291. {
  292. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  293. if (!attributes)
  294. {
  295. LOGERROR(GetTypeName() + " has no attributes");
  296. return false;
  297. }
  298. if (index >= attributes->Size())
  299. {
  300. LOGERROR("Attribute index out of bounds");
  301. return false;
  302. }
  303. const AttributeInfo& attr = attributes->At(index);
  304. // Check that the new value's type matches the attribute type
  305. if (value.GetType() == attr.type_)
  306. {
  307. OnSetAttribute(attr, value);
  308. return true;
  309. }
  310. else
  311. {
  312. LOGERROR("Could not set attribute " + String(attr.name_) + ": expected type " + Variant::GetTypeName(attr.type_) +
  313. " but got " + value.GetTypeName());
  314. return false;
  315. }
  316. }
  317. bool Serializable::SetAttribute(const String& name, const Variant& value)
  318. {
  319. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  320. if (!attributes)
  321. {
  322. LOGERROR(GetTypeName() + " has no attributes");
  323. return false;
  324. }
  325. for (Vector<AttributeInfo>::ConstIterator i = attributes->Begin(); i != attributes->End(); ++i)
  326. {
  327. if (i->name_ == name)
  328. {
  329. // Check that the new value's type matches the attribute type
  330. if (value.GetType() == i->type_)
  331. {
  332. OnSetAttribute(*i, value);
  333. return true;
  334. }
  335. else
  336. {
  337. LOGERROR("Could not set attribute " + String(i->name_) + ": expected type " + Variant::GetTypeName(i->type_)
  338. + " but got " + value.GetTypeName());
  339. return false;
  340. }
  341. }
  342. }
  343. LOGERROR("Could not find attribute " + String(name) + " in " + GetTypeName());
  344. return false;
  345. }
  346. void Serializable::WriteInitialDeltaUpdate(Serializer& dest, PODVector<unsigned char>& deltaUpdateBits,
  347. Vector<Variant>& replicationState)
  348. {
  349. const Vector<AttributeInfo>* attributes = GetNetworkAttributes();
  350. if (!attributes)
  351. return;
  352. unsigned numAttributes = attributes->Size();
  353. replicationState.Resize(numAttributes);
  354. deltaUpdateBits.Resize((numAttributes + 7) >> 3);
  355. for (unsigned i = 0; i < deltaUpdateBits.Size(); ++i)
  356. deltaUpdateBits[i] = 0;
  357. // Set initial attribute values and compare against defaults
  358. for (unsigned i = 0; i < numAttributes; ++i)
  359. {
  360. const AttributeInfo& attr = attributes->At(i);
  361. OnGetAttribute(attr, replicationState[i]);
  362. if (replicationState[i] != attr.defaultValue_)
  363. deltaUpdateBits[i >> 3] |= (1 << (i & 7));
  364. }
  365. // First write the change bitfield, then attribute data for non-default attributes
  366. dest.Write(&deltaUpdateBits[0], deltaUpdateBits.Size());
  367. for (unsigned i = 0; i < numAttributes; ++i)
  368. {
  369. const AttributeInfo& attr = attributes->At(i);
  370. if (deltaUpdateBits[i >> 3] & (1 << (i & 7)))
  371. dest.WriteVariantData(replicationState[i]);
  372. }
  373. }
  374. void Serializable::PrepareUpdates(PODVector<unsigned char>& deltaUpdateBits, Vector<Variant>& classCurrentState,
  375. Vector<Variant>& replicationState, bool& deltaUpdate, bool& latestData)
  376. {
  377. deltaUpdate = false;
  378. latestData = false;
  379. const Vector<AttributeInfo>* attributes = GetNetworkAttributes();
  380. if (!attributes)
  381. return;
  382. unsigned numAttributes = attributes->Size();
  383. deltaUpdateBits.Resize((numAttributes + 7) >> 3);
  384. for (unsigned i = 0; i < deltaUpdateBits.Size(); ++i)
  385. deltaUpdateBits[i] = 0;
  386. // If class-specific current state has not been previously used, resize it now
  387. if (classCurrentState.Empty())
  388. classCurrentState.Resize(numAttributes);
  389. for (unsigned i = 0; i < numAttributes; ++i)
  390. {
  391. const AttributeInfo& attr = attributes->At(i);
  392. // Check for attribute change
  393. OnGetAttribute(attr, classCurrentState[i]);
  394. if (classCurrentState[i] != replicationState[i])
  395. {
  396. replicationState[i] = classCurrentState[i];
  397. if (attr.mode_ & AM_LATESTDATA)
  398. latestData = true;
  399. else
  400. {
  401. deltaUpdate = true;
  402. deltaUpdateBits[i >> 3] |= 1 << (i & 7);
  403. }
  404. }
  405. }
  406. }
  407. void Serializable::WriteDeltaUpdate(Serializer& dest, PODVector<unsigned char>& deltaUpdateBits, Vector<Variant>& replicationState)
  408. {
  409. const Vector<AttributeInfo>* attributes = GetNetworkAttributes();
  410. if (!attributes)
  411. return;
  412. unsigned numAttributes = attributes->Size();
  413. // First write the change bitfield, then attribute data for changed attributes
  414. dest.Write(&deltaUpdateBits[0], deltaUpdateBits.Size());
  415. for (unsigned i = 0; i < numAttributes; ++i)
  416. {
  417. if (deltaUpdateBits[i >> 3] & (1 << (i & 7)))
  418. dest.WriteVariantData(replicationState[i]);
  419. }
  420. }
  421. void Serializable::WriteLatestDataUpdate(Serializer& dest, Vector<Variant>& replicationState)
  422. {
  423. const Vector<AttributeInfo>* attributes = GetNetworkAttributes();
  424. if (!attributes)
  425. return;
  426. unsigned numAttributes = attributes->Size();
  427. for (unsigned i = 0; i < numAttributes; ++i)
  428. {
  429. if (attributes->At(i).mode_ & AM_LATESTDATA)
  430. dest.WriteVariantData(replicationState[i]);
  431. }
  432. }
  433. void Serializable::ReadDeltaUpdate(Deserializer& source, PODVector<unsigned char>& deltaUpdateBits)
  434. {
  435. const Vector<AttributeInfo>* attributes = GetNetworkAttributes();
  436. if (!attributes)
  437. return;
  438. unsigned numAttributes = attributes->Size();
  439. deltaUpdateBits.Resize((numAttributes + 7) >> 3);
  440. source.Read(&deltaUpdateBits[0], deltaUpdateBits.Size());
  441. for (unsigned i = 0; i < numAttributes && !source.IsEof(); ++i)
  442. {
  443. const AttributeInfo& attr = attributes->At(i);
  444. if (deltaUpdateBits[i >> 3] & (1 << (i & 7)))
  445. OnSetAttribute(attr, source.ReadVariant(attr.type_));
  446. }
  447. }
  448. void Serializable::ReadLatestDataUpdate(Deserializer& source)
  449. {
  450. const Vector<AttributeInfo>* attributes = GetNetworkAttributes();
  451. if (!attributes)
  452. return;
  453. unsigned numAttributes = attributes->Size();
  454. for (unsigned i = 0; i < numAttributes && !source.IsEof(); ++i)
  455. {
  456. const AttributeInfo& attr = attributes->At(i);
  457. if (attr.mode_ & AM_LATESTDATA)
  458. OnSetAttribute(attr, source.ReadVariant(attr.type_));
  459. }
  460. }
  461. Variant Serializable::GetAttribute(unsigned index)
  462. {
  463. Variant ret;
  464. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  465. if (!attributes || index >= attributes->Size())
  466. return ret;
  467. OnGetAttribute(attributes->At(index), ret);
  468. return ret;
  469. }
  470. Variant Serializable::GetAttribute(const String& name)
  471. {
  472. Variant ret;
  473. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  474. if (!attributes)
  475. {
  476. LOGERROR(GetTypeName() + " has no attributes");
  477. return ret;
  478. }
  479. for (Vector<AttributeInfo>::ConstIterator i = attributes->Begin(); i != attributes->End(); ++i)
  480. {
  481. if (i->name_ == name)
  482. {
  483. OnGetAttribute(*i, ret);
  484. return ret;
  485. }
  486. }
  487. LOGERROR("Could not find attribute " + String(name) + " in " + GetTypeName());
  488. return ret;
  489. }
  490. unsigned Serializable::GetNumAttributes() const
  491. {
  492. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  493. return attributes ? attributes->Size() : 0;
  494. }
  495. unsigned Serializable::GetNumNetworkAttributes() const
  496. {
  497. const Vector<AttributeInfo>* attributes = context_->GetNetworkAttributes(GetType());
  498. return attributes ? attributes->Size() : 0;
  499. }
  500. const Vector<AttributeInfo>* Serializable::GetAttributes() const
  501. {
  502. return context_->GetAttributes(GetType());
  503. }
  504. const Vector<AttributeInfo>* Serializable::GetNetworkAttributes() const
  505. {
  506. return context_->GetNetworkAttributes(GetType());
  507. }