Serializable.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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. bool CompareAndCopyVariant(const Variant& src, Variant& dest)
  32. {
  33. if (dest != src)
  34. {
  35. dest = src;
  36. return true;
  37. }
  38. else
  39. return false;
  40. }
  41. OBJECTTYPESTATIC(Serializable);
  42. Serializable::Serializable(Context* context) :
  43. Object(context),
  44. inSerialization_(false)
  45. {
  46. }
  47. Serializable::~Serializable()
  48. {
  49. }
  50. void Serializable::OnSetAttribute(const AttributeInfo& attr, const Variant& value)
  51. {
  52. // Check for accessor function mode
  53. if (attr.accessor_)
  54. {
  55. attr.accessor_->Set(this, value);
  56. return;
  57. }
  58. // Calculate the destination address
  59. void* dest = reinterpret_cast<unsigned char*>(this) + attr.offset_;
  60. switch (attr.type_)
  61. {
  62. case VAR_INT:
  63. // If enum type, use the low 8 bits only (assume full value to be initialized)
  64. if (attr.enumNames_)
  65. *(reinterpret_cast<unsigned char*>(dest)) = value.GetInt();
  66. else
  67. *(reinterpret_cast<int*>(dest)) = value.GetInt();
  68. break;
  69. case VAR_BOOL:
  70. *(reinterpret_cast<bool*>(dest)) = value.GetBool();
  71. break;
  72. case VAR_FLOAT:
  73. *(reinterpret_cast<float*>(dest)) = value.GetFloat();
  74. break;
  75. case VAR_VECTOR2:
  76. *(reinterpret_cast<Vector2*>(dest)) = value.GetVector2();
  77. break;
  78. case VAR_VECTOR3:
  79. *(reinterpret_cast<Vector3*>(dest)) = value.GetVector3();
  80. break;
  81. case VAR_VECTOR4:
  82. *(reinterpret_cast<Vector4*>(dest)) = value.GetVector4();
  83. break;
  84. case VAR_QUATERNION:
  85. *(reinterpret_cast<Quaternion*>(dest)) = value.GetQuaternion();
  86. break;
  87. case VAR_COLOR:
  88. *(reinterpret_cast<Color*>(dest)) = value.GetColor();
  89. break;
  90. case VAR_STRING:
  91. *(reinterpret_cast<String*>(dest)) = value.GetString();
  92. break;
  93. case VAR_BUFFER:
  94. *(reinterpret_cast<PODVector<unsigned char>*>(dest)) = value.GetBuffer();
  95. break;
  96. case VAR_RESOURCEREF:
  97. *(reinterpret_cast<ResourceRef*>(dest)) = value.GetResourceRef();
  98. break;
  99. case VAR_RESOURCEREFLIST:
  100. *(reinterpret_cast<ResourceRefList*>(dest)) = value.GetResourceRefList();
  101. break;
  102. case VAR_VARIANTVECTOR:
  103. *(reinterpret_cast<VariantVector*>(dest)) = value.GetVariantVector();
  104. break;
  105. case VAR_VARIANTMAP:
  106. *(reinterpret_cast<VariantMap*>(dest)) = value.GetVariantMap();
  107. break;
  108. }
  109. }
  110. Variant Serializable::OnGetAttribute(const AttributeInfo& attr)
  111. {
  112. // Check for accessor function mode
  113. if (attr.accessor_)
  114. return attr.accessor_->Get(this);
  115. // Calculate the source address
  116. void* src = reinterpret_cast<unsigned char*>(this) + attr.offset_;
  117. switch (attr.type_)
  118. {
  119. case VAR_INT:
  120. // If enum type, use the low 8 bits only
  121. if (attr.enumNames_)
  122. return Variant(*(reinterpret_cast<const unsigned char*>(src)));
  123. else
  124. return Variant(*(reinterpret_cast<const int*>(src)));
  125. case VAR_BOOL:
  126. return Variant(*(reinterpret_cast<const bool*>(src)));
  127. case VAR_FLOAT:
  128. return Variant(*(reinterpret_cast<const float*>(src)));
  129. case VAR_VECTOR2:
  130. return Variant(*(reinterpret_cast<const Vector2*>(src)));
  131. case VAR_VECTOR3:
  132. return Variant(*(reinterpret_cast<const Vector3*>(src)));
  133. case VAR_VECTOR4:
  134. return Variant(*(reinterpret_cast<const Vector4*>(src)));
  135. case VAR_QUATERNION:
  136. return Variant(*(reinterpret_cast<const Quaternion*>(src)));
  137. case VAR_COLOR:
  138. return Variant(*(reinterpret_cast<const Color*>(src)));
  139. case VAR_STRING:
  140. return Variant(*(reinterpret_cast<const String*>(src)));
  141. case VAR_BUFFER:
  142. return Variant(*(reinterpret_cast<const PODVector<unsigned char>*>(src)));
  143. case VAR_RESOURCEREF:
  144. return Variant(*(reinterpret_cast<const ResourceRef*>(src)));
  145. case VAR_RESOURCEREFLIST:
  146. return Variant(*(reinterpret_cast<const ResourceRefList*>(src)));
  147. case VAR_VARIANTVECTOR:
  148. return Variant(*(reinterpret_cast<const VariantVector*>(src)));
  149. case VAR_VARIANTMAP:
  150. return Variant(*(reinterpret_cast<const VariantMap*>(src)));
  151. }
  152. return Variant();
  153. }
  154. bool Serializable::Load(Deserializer& source)
  155. {
  156. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  157. if (!attributes)
  158. return true;
  159. inSerialization_ = true;
  160. for (unsigned i = 0; i < attributes->Size(); ++i)
  161. {
  162. const AttributeInfo& attr = attributes->At(i);
  163. if (!(attr.mode_ & AM_FILE))
  164. continue;
  165. if (source.IsEof())
  166. {
  167. LOGERROR("Could not load " + GetTypeName() + ", stream not open or at end");
  168. inSerialization_ = false;
  169. return false;
  170. }
  171. OnSetAttribute(attr, source.ReadVariant(attr.type_));
  172. }
  173. inSerialization_ = false;
  174. return true;
  175. }
  176. bool Serializable::Save(Serializer& dest)
  177. {
  178. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  179. if (!attributes)
  180. return true;
  181. inSerialization_ = true;
  182. for (unsigned i = 0; i < attributes->Size(); ++i)
  183. {
  184. const AttributeInfo& attr = attributes->At(i);
  185. if (!(attr.mode_ & AM_FILE))
  186. continue;
  187. if (!dest.WriteVariantData(OnGetAttribute(attr)))
  188. {
  189. LOGERROR("Could not save " + GetTypeName() + ", writing to stream failed");
  190. inSerialization_ = false;
  191. return false;
  192. }
  193. }
  194. inSerialization_ = false;
  195. return true;
  196. }
  197. bool Serializable::LoadXML(const XMLElement& source)
  198. {
  199. if (source.IsNull())
  200. {
  201. LOGERROR("Could not load " + GetTypeName() + ", null source element");
  202. return false;
  203. }
  204. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  205. if (!attributes)
  206. return true;
  207. inSerialization_ = true;
  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. XMLElement attrElem = source.GetChild("attribute");
  214. bool found = false;
  215. while (attrElem)
  216. {
  217. if (attrElem.GetString("name") == attr.name_)
  218. {
  219. // If enums specified, do enum lookup and int assignment. Otherwise assign the variant directly
  220. if (attr.enumNames_)
  221. {
  222. String value = attrElem.GetString("value");
  223. const String* enumPtr = attr.enumNames_;
  224. int enumValue = 0;
  225. bool enumFound = false;
  226. while (enumPtr->Length())
  227. {
  228. if (*enumPtr == value)
  229. {
  230. enumFound = true;
  231. break;
  232. }
  233. ++enumPtr;
  234. ++enumValue;
  235. }
  236. if (enumFound)
  237. OnSetAttribute(attr, Variant(enumValue));
  238. else
  239. LOGWARNING("Unknown enum value " + value + " in attribute " + String(attr.name_));
  240. }
  241. else
  242. OnSetAttribute(attr, attrElem.GetVariant());
  243. found = true;
  244. break;
  245. }
  246. attrElem = attrElem.GetNext("attribute");
  247. }
  248. if (!found)
  249. LOGWARNING("Attribute " + String(attr.name_) + " not found in XML data");
  250. }
  251. inSerialization_ = false;
  252. return true;
  253. }
  254. bool Serializable::SaveXML(XMLElement& dest)
  255. {
  256. if (dest.IsNull())
  257. {
  258. LOGERROR("Could not save " + GetTypeName() + ", null destination element");
  259. return false;
  260. }
  261. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  262. if (!attributes)
  263. return true;
  264. inSerialization_ = true;
  265. for (unsigned i = 0; i < attributes->Size(); ++i)
  266. {
  267. const AttributeInfo& attr = attributes->At(i);
  268. if (!(attr.mode_ & AM_FILE))
  269. continue;
  270. XMLElement attrElem = dest.CreateChild("attribute");
  271. attrElem.SetString("name", String(attr.name_));
  272. // If enums specified, set as an enum string. Otherwise set directly as a Variant
  273. if (attr.enumNames_)
  274. {
  275. int enumValue = OnGetAttribute(attr).GetInt();
  276. attrElem.SetString("type", "Enum");
  277. attrElem.SetString("value", String(attr.enumNames_[enumValue]));
  278. }
  279. else
  280. attrElem.SetVariant(OnGetAttribute(attr));
  281. }
  282. inSerialization_ = false;
  283. return true;
  284. }
  285. bool Serializable::SetAttribute(unsigned index, const Variant& value)
  286. {
  287. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  288. if (!attributes)
  289. {
  290. LOGERROR(GetTypeName() + " has no attributes");
  291. return false;
  292. }
  293. if (index >= attributes->Size())
  294. {
  295. LOGERROR("Attribute index out of bounds");
  296. return false;
  297. }
  298. const AttributeInfo& attr = attributes->At(index);
  299. // Check that the new value's type matches the attribute type
  300. if (value.GetType() == attr.type_)
  301. {
  302. OnSetAttribute(attr, value);
  303. return true;
  304. }
  305. else
  306. {
  307. LOGERROR("Could not set attribute " + String(attr.name_) + ": expected type " + Variant::GetTypeName(attr.type_) +
  308. " but got " + value.GetTypeName());
  309. return false;
  310. }
  311. }
  312. bool Serializable::SetAttribute(const String& name, const Variant& value)
  313. {
  314. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  315. if (!attributes)
  316. {
  317. LOGERROR(GetTypeName() + " has no attributes");
  318. return false;
  319. }
  320. for (Vector<AttributeInfo>::ConstIterator i = attributes->Begin(); i != attributes->End(); ++i)
  321. {
  322. if (i->name_ == name)
  323. {
  324. // Check that the new value's type matches the attribute type
  325. if (value.GetType() == i->type_)
  326. {
  327. OnSetAttribute(*i, value);
  328. return true;
  329. }
  330. else
  331. {
  332. LOGERROR("Could not set attribute " + String(i->name_) + ": expected type " + Variant::GetTypeName(i->type_)
  333. + " but got " + value.GetTypeName());
  334. return false;
  335. }
  336. }
  337. }
  338. LOGERROR("Could not find attribute " + String(name) + " in " + GetTypeName());
  339. return false;
  340. }
  341. void Serializable::WriteInitialDeltaUpdate(Serializer& dest, PODVector<unsigned char>& deltaUpdateBits, Vector<Variant>& lastSentState)
  342. {
  343. const Vector<AttributeInfo>* attributes = GetNetworkAttributes();
  344. if (!attributes)
  345. return;
  346. unsigned numAttributes = attributes->Size();
  347. lastSentState.Resize(numAttributes);
  348. deltaUpdateBits.Resize((numAttributes + 7) >> 3);
  349. for (unsigned i = 0; i < deltaUpdateBits.Size(); ++i)
  350. deltaUpdateBits[i] = 0;
  351. // Set initial values to the last sent state and compare against defaults
  352. for (unsigned i = 0; i < numAttributes; ++i)
  353. {
  354. const AttributeInfo& attr = attributes->At(i);
  355. lastSentState[i] = OnGetAttribute(attr);
  356. if (lastSentState[i] != attr.defaultValue_)
  357. deltaUpdateBits[i >> 3] |= (1 << (i & 7));
  358. }
  359. // First write the change bitfield, then attribute data for non-default attributes
  360. dest.Write(&deltaUpdateBits[0], deltaUpdateBits.Size());
  361. for (unsigned i = 0; i < numAttributes; ++i)
  362. {
  363. const AttributeInfo& attr = attributes->At(i);
  364. if (deltaUpdateBits[i >> 3] & (1 << (i & 7)))
  365. dest.WriteVariantData(lastSentState[i]);
  366. }
  367. }
  368. void Serializable::PrepareUpdates(PODVector<unsigned char>& deltaUpdateBits, Vector<Variant>& lastSentState, bool& deltaUpdate,
  369. bool& latestData)
  370. {
  371. deltaUpdate = false;
  372. latestData = false;
  373. const Vector<AttributeInfo>* attributes = GetNetworkAttributes();
  374. if (!attributes)
  375. return;
  376. unsigned numAttributes = attributes->Size();
  377. deltaUpdateBits.Resize((numAttributes + 7) >> 3);
  378. for (unsigned i = 0; i < deltaUpdateBits.Size(); ++i)
  379. deltaUpdateBits[i] = 0;
  380. for (unsigned i = 0; i < numAttributes; ++i)
  381. {
  382. const AttributeInfo& attr = attributes->At(i);
  383. // Check for attribute change
  384. if (CompareAndCopyVariant(OnGetAttribute(attr), lastSentState[i]))
  385. {
  386. if (attr.mode_ & AM_LATESTDATA)
  387. latestData = true;
  388. else
  389. {
  390. deltaUpdate = true;
  391. deltaUpdateBits[i >> 3] |= 1 << (i & 7);
  392. }
  393. }
  394. }
  395. }
  396. void Serializable::WriteDeltaUpdate(Serializer& dest, PODVector<unsigned char>& deltaUpdateBits, Vector<Variant>& lastSentState)
  397. {
  398. const Vector<AttributeInfo>* attributes = GetNetworkAttributes();
  399. if (!attributes)
  400. return;
  401. unsigned numAttributes = attributes->Size();
  402. // First write the change bitfield, then attribute data for changed attributes
  403. dest.Write(&deltaUpdateBits[0], deltaUpdateBits.Size());
  404. for (unsigned i = 0; i < numAttributes; ++i)
  405. {
  406. const AttributeInfo& attr = attributes->At(i);
  407. if (deltaUpdateBits[i >> 3] & (1 << (i & 7)))
  408. dest.WriteVariantData(lastSentState[i]);
  409. }
  410. }
  411. void Serializable::WriteLatestDataUpdate(Serializer& dest, Vector<Variant>& lastSentState)
  412. {
  413. const Vector<AttributeInfo>* attributes = GetNetworkAttributes();
  414. if (!attributes)
  415. return;
  416. unsigned numAttributes = attributes->Size();
  417. for (unsigned i = 0; i < numAttributes; ++i)
  418. {
  419. const AttributeInfo& attr = attributes->At(i);
  420. if (attr.mode_ & AM_LATESTDATA)
  421. dest.WriteVariantData(lastSentState[i]);
  422. }
  423. }
  424. void Serializable::ReadDeltaUpdate(Deserializer& source, PODVector<unsigned char>& deltaUpdateBits)
  425. {
  426. const Vector<AttributeInfo>* attributes = GetNetworkAttributes();
  427. if (!attributes)
  428. return;
  429. unsigned numAttributes = attributes->Size();
  430. deltaUpdateBits.Resize((numAttributes + 7) >> 3);
  431. source.Read(&deltaUpdateBits[0], deltaUpdateBits.Size());
  432. for (unsigned i = 0; i < numAttributes && !source.IsEof(); ++i)
  433. {
  434. const AttributeInfo& attr = attributes->At(i);
  435. if (deltaUpdateBits[i >> 3] & (1 << (i & 7)))
  436. OnSetAttribute(attr, source.ReadVariant(attr.type_));
  437. }
  438. }
  439. void Serializable::ReadLatestDataUpdate(Deserializer& source)
  440. {
  441. const Vector<AttributeInfo>* attributes = GetNetworkAttributes();
  442. if (!attributes)
  443. return;
  444. unsigned numAttributes = attributes->Size();
  445. for (unsigned i = 0; i < numAttributes && !source.IsEof(); ++i)
  446. {
  447. const AttributeInfo& attr = attributes->At(i);
  448. if (attr.mode_ & AM_LATESTDATA)
  449. OnSetAttribute(attr, source.ReadVariant(attr.type_));
  450. }
  451. }
  452. Variant Serializable::GetAttribute(unsigned index)
  453. {
  454. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  455. if (!attributes || index >= attributes->Size())
  456. return Variant();
  457. return OnGetAttribute(attributes->At(index));
  458. }
  459. Variant Serializable::GetAttribute(const String& name)
  460. {
  461. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  462. if (!attributes)
  463. {
  464. LOGERROR(GetTypeName() + " has no attributes");
  465. return Variant();
  466. }
  467. for (Vector<AttributeInfo>::ConstIterator i = attributes->Begin(); i != attributes->End(); ++i)
  468. {
  469. if (i->name_ == name)
  470. return OnGetAttribute(*i);
  471. }
  472. LOGERROR("Could not find attribute " + String(name) + " in " + GetTypeName());
  473. return Variant();
  474. }
  475. unsigned Serializable::GetNumAttributes() const
  476. {
  477. const Vector<AttributeInfo>* attributes = context_->GetAttributes(GetType());
  478. return attributes ? attributes->Size() : 0;
  479. }
  480. unsigned Serializable::GetNumNetworkAttributes() const
  481. {
  482. const Vector<AttributeInfo>* attributes = context_->GetNetworkAttributes(GetType());
  483. return attributes ? attributes->Size() : 0;
  484. }
  485. const Vector<AttributeInfo>* Serializable::GetAttributes() const
  486. {
  487. return context_->GetAttributes(GetType());
  488. }
  489. const Vector<AttributeInfo>* Serializable::GetNetworkAttributes() const
  490. {
  491. return context_->GetNetworkAttributes(GetType());
  492. }