Serializable.cpp 17 KB

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