ReplicationUtils.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  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 "ReplicationUtils.h"
  25. #include <cstring>
  26. void checkBool(bool value, Deserializer& baseRevision, unsigned char& bits, unsigned char bit)
  27. {
  28. // If no base data, set bit always
  29. if (baseRevision.isEof())
  30. bits |= bit;
  31. else
  32. {
  33. if (value != baseRevision.readBool())
  34. bits |= bit;
  35. }
  36. }
  37. void checkBool(bool value, bool defaultValue, Deserializer& baseRevision, unsigned char& bits, unsigned char bit)
  38. {
  39. // If no base data, compare to default value
  40. if (baseRevision.isEof())
  41. {
  42. if (value != defaultValue)
  43. bits |= bit;
  44. }
  45. else
  46. {
  47. if (value != baseRevision.readBool())
  48. bits |= bit;
  49. }
  50. }
  51. void checkInt(int value, Deserializer& baseRevision, unsigned char& bits, unsigned char bit)
  52. {
  53. // If no base data, set bit always
  54. if (baseRevision.isEof())
  55. bits |= bit;
  56. else
  57. {
  58. if (value != baseRevision.readInt())
  59. bits |= bit;
  60. }
  61. }
  62. void checkInt(int value, int defaultValue, Deserializer& baseRevision, unsigned char& bits, unsigned char bit)
  63. {
  64. // If no base data, compare to default value
  65. if (baseRevision.isEof())
  66. {
  67. if (value != defaultValue)
  68. bits |= bit;
  69. }
  70. else
  71. {
  72. if (value != baseRevision.readInt())
  73. bits |= bit;
  74. }
  75. }
  76. void checkUByte(unsigned char value, Deserializer& baseRevision, unsigned char& bits, unsigned char bit)
  77. {
  78. // If no base data, set bit always
  79. if (baseRevision.isEof())
  80. bits |= bit;
  81. else
  82. {
  83. if (value != baseRevision.readUByte())
  84. bits |= bit;
  85. }
  86. }
  87. void checkUByte(unsigned char value, unsigned char defaultValue, Deserializer& baseRevision, unsigned char& bits, unsigned char bit)
  88. {
  89. // If no base data, compare to default value
  90. if (baseRevision.isEof())
  91. {
  92. if (value != defaultValue)
  93. bits |= bit;
  94. }
  95. else
  96. {
  97. if (value != baseRevision.readUByte())
  98. bits |= bit;
  99. }
  100. }
  101. void checkUShort(unsigned short value, Deserializer& baseRevision, unsigned char& bits, unsigned char bit)
  102. {
  103. // If no base data, set bit always
  104. if (baseRevision.isEof())
  105. bits |= bit;
  106. else
  107. {
  108. if (value != baseRevision.readUShort())
  109. bits |= bit;
  110. }
  111. }
  112. void checkUShort(unsigned short value, unsigned short defaultValue, Deserializer& baseRevision, unsigned char& bits, unsigned char bit)
  113. {
  114. // If no base data, compare to default value
  115. if (baseRevision.isEof())
  116. {
  117. if (value != defaultValue)
  118. bits |= bit;
  119. }
  120. else
  121. {
  122. if (value != baseRevision.readUShort())
  123. bits |= bit;
  124. }
  125. }
  126. void checkUInt(unsigned value, Deserializer& baseRevision, unsigned char& bits, unsigned char bit)
  127. {
  128. // If no base data, set bit always
  129. if (baseRevision.isEof())
  130. bits |= bit;
  131. else
  132. {
  133. if (value != baseRevision.readUInt())
  134. bits |= bit;
  135. }
  136. }
  137. void checkUInt(unsigned value, unsigned defaultValue, Deserializer& baseRevision, unsigned char& bits, unsigned char bit)
  138. {
  139. // If no base data, compare to default value
  140. if (baseRevision.isEof())
  141. {
  142. if (value != defaultValue)
  143. bits |= bit;
  144. }
  145. else
  146. {
  147. if (value != baseRevision.readUInt())
  148. bits |= bit;
  149. }
  150. }
  151. void checkFloat(float value, Deserializer& baseRevision, unsigned char& bits, unsigned char bit)
  152. {
  153. // If no base data, set bit always
  154. if (baseRevision.isEof())
  155. bits |= bit;
  156. else
  157. {
  158. if (value != baseRevision.readFloat())
  159. bits |= bit;
  160. }
  161. }
  162. void checkFloat(float value, float defaultValue, Deserializer& baseRevision, unsigned char& bits, unsigned char bit)
  163. {
  164. // If no base data, compare to default value
  165. if (baseRevision.isEof())
  166. {
  167. if (value != defaultValue)
  168. bits |= bit;
  169. }
  170. else
  171. {
  172. if (value != baseRevision.readFloat())
  173. bits |= bit;
  174. }
  175. }
  176. void checkVector3(const Vector3& value, Deserializer& baseRevision, unsigned char& bits, unsigned char bit)
  177. {
  178. // If no base data, set bit always
  179. if (baseRevision.isEof())
  180. bits |= bit;
  181. else
  182. {
  183. if (value != baseRevision.readVector3())
  184. bits |= bit;
  185. }
  186. }
  187. void checkVector3(const Vector3& value, const Vector3& defaultValue, Deserializer& baseRevision, unsigned char& bits, unsigned char bit)
  188. {
  189. // If no base data, compare to default value
  190. if (baseRevision.isEof())
  191. {
  192. if (value != defaultValue)
  193. bits |= bit;
  194. }
  195. else
  196. {
  197. if (value != baseRevision.readVector3())
  198. bits |= bit;
  199. }
  200. }
  201. void checkQuaternion(const Quaternion& value, Deserializer& baseRevision, unsigned char& bits, unsigned char bit)
  202. {
  203. // If no base data, set bit always
  204. if (baseRevision.isEof())
  205. bits |= bit;
  206. else
  207. {
  208. if (value != baseRevision.readQuaternion())
  209. bits |= bit;
  210. }
  211. }
  212. void checkQuaternion(const Quaternion& value, const Quaternion& defaultValue, Deserializer& baseRevision, unsigned char& bits, unsigned char bit)
  213. {
  214. // If no base data, compare to default value
  215. if (baseRevision.isEof())
  216. {
  217. if (value != defaultValue)
  218. bits |= bit;
  219. }
  220. else
  221. {
  222. if (value != baseRevision.readQuaternion())
  223. bits |= bit;
  224. }
  225. }
  226. void checkColor(const Color& value, Deserializer& baseRevision, unsigned char& bits, unsigned char bit)
  227. {
  228. // If no base data, set bit always
  229. if (baseRevision.isEof())
  230. bits |= bit;
  231. else
  232. {
  233. if (value != baseRevision.readColor())
  234. bits |= bit;
  235. }
  236. }
  237. void checkColor(const Color& value, const Color& defaultValue, Deserializer& baseRevision, unsigned char& bits, unsigned char bit)
  238. {
  239. // If no base data, compare to default value
  240. if (baseRevision.isEof())
  241. {
  242. if (value != defaultValue)
  243. bits |= bit;
  244. }
  245. else
  246. {
  247. if (value != baseRevision.readColor())
  248. bits |= bit;
  249. }
  250. }
  251. void checkString(const std::string& value, Deserializer& baseRevision, unsigned char& bits, unsigned char bit)
  252. {
  253. // If no base data, set bit always
  254. if (baseRevision.isEof())
  255. bits |= bit;
  256. else
  257. {
  258. if (value != baseRevision.readString())
  259. bits |= bit;
  260. }
  261. }
  262. void checkString(const std::string& value, const std::string& defaultValue, Deserializer& baseRevision, unsigned char& bits, unsigned char bit)
  263. {
  264. // If no base data, compare to default value
  265. if (baseRevision.isEof())
  266. {
  267. if (value != defaultValue)
  268. bits |= bit;
  269. }
  270. else
  271. {
  272. if (value != baseRevision.readString())
  273. bits |= bit;
  274. }
  275. }
  276. void checkStringHash(StringHash value, Deserializer& baseRevision, unsigned char& bits, unsigned char bit)
  277. {
  278. // If no base data, set bit always
  279. if (baseRevision.isEof())
  280. bits |= bit;
  281. else
  282. {
  283. if (value != baseRevision.readStringHash())
  284. bits |= bit;
  285. }
  286. }
  287. void checkStringHash(StringHash value, StringHash defaultValue, Deserializer& baseRevision, unsigned char& bits, unsigned char bit)
  288. {
  289. // If no base data, compare to default value
  290. if (baseRevision.isEof())
  291. {
  292. if (value != defaultValue)
  293. bits |= bit;
  294. }
  295. else
  296. {
  297. if (value != baseRevision.readStringHash())
  298. bits |= bit;
  299. }
  300. }
  301. void checkShortStringHash(ShortStringHash value, Deserializer& baseRevision, unsigned char& bits, unsigned char bit)
  302. {
  303. // If no base data, set bit always
  304. if (baseRevision.isEof())
  305. bits |= bit;
  306. else
  307. {
  308. if (value != baseRevision.readShortStringHash())
  309. bits |= bit;
  310. }
  311. }
  312. void checkShortStringHash(ShortStringHash value, ShortStringHash defaultValue, Deserializer& baseRevision, unsigned char& bits, unsigned char bit)
  313. {
  314. // If no base data, compare to default value
  315. if (baseRevision.isEof())
  316. {
  317. if (value != defaultValue)
  318. bits |= bit;
  319. }
  320. else
  321. {
  322. if (value != baseRevision.readShortStringHash())
  323. bits |= bit;
  324. }
  325. }
  326. void checkComponentRef(const ComponentRef& value, Deserializer& baseRevision, unsigned char& bits, unsigned char bit)
  327. {
  328. // If no base data, set bit always
  329. if (baseRevision.isEof())
  330. bits |= bit;
  331. else
  332. {
  333. ComponentRef temp;
  334. temp.readPacked(baseRevision);
  335. if (value != temp)
  336. bits |= bit;
  337. }
  338. }
  339. void checkComponentRef(const ComponentRef& value, const ComponentRef& defaultValue, Deserializer& baseRevision, unsigned char& bits, unsigned char bit)
  340. {
  341. // If no base data, compare to default value
  342. if (baseRevision.isEof())
  343. {
  344. if (value != defaultValue)
  345. bits |= bit;
  346. }
  347. else
  348. {
  349. ComponentRef temp;
  350. temp.readPacked(baseRevision);
  351. if (value != temp)
  352. bits |= bit;
  353. }
  354. }
  355. void checkVLE(unsigned value, Deserializer& baseRevision, unsigned char& bits, unsigned char bit)
  356. {
  357. // If no base data, set bit always
  358. if (baseRevision.isEof())
  359. bits |= bit;
  360. else
  361. {
  362. if (value != baseRevision.readVLE())
  363. bits |= bit;
  364. }
  365. }
  366. void checkVLE(unsigned value, unsigned defaultValue, Deserializer& baseRevision, unsigned char& bits, unsigned char bit)
  367. {
  368. // If no base data, compare to default value
  369. if (baseRevision.isEof())
  370. {
  371. if (value != defaultValue)
  372. bits |= bit;
  373. }
  374. else
  375. {
  376. if (value != baseRevision.readVLE())
  377. bits |= bit;
  378. }
  379. }
  380. void checkBuffer(const VectorBuffer& value, Deserializer& baseRevision, unsigned char& bits, unsigned char bit)
  381. {
  382. // If no base data, set bit always
  383. if (baseRevision.isEof())
  384. bits |= bit;
  385. else
  386. {
  387. static VectorBuffer baseBuffer;
  388. unsigned baseBufferSize = baseRevision.readVLE();
  389. baseBuffer.setData(baseRevision, baseBufferSize);
  390. if ((baseBufferSize != value.getSize()) || (memcmp(value.getData(), baseBuffer.getData(), value.getSize())))
  391. bits |= bit;
  392. }
  393. }
  394. void writeBoolDelta(bool value, Serializer& dest, Serializer& destRevision, unsigned char condition)
  395. {
  396. destRevision.writeBool(value);
  397. if (condition)
  398. dest.writeBool(value);
  399. }
  400. void writeIntDelta(int value, Serializer& dest, Serializer& destRevision, unsigned char condition)
  401. {
  402. destRevision.writeInt(value);
  403. if (condition)
  404. dest.writeInt(value);
  405. }
  406. void writeUByteDelta(unsigned char value, Serializer& dest, Serializer& destRevision, unsigned char condition)
  407. {
  408. destRevision.writeUByte(value);
  409. if (condition)
  410. dest.writeUByte(value);
  411. }
  412. void writeUShortDelta(unsigned short value, Serializer& dest, Serializer& destRevision, unsigned char condition)
  413. {
  414. destRevision.writeUShort(value);
  415. if (condition)
  416. dest.writeUShort(value);
  417. }
  418. void writeUIntDelta(unsigned value, Serializer& dest, Serializer& destRevision, unsigned char condition)
  419. {
  420. destRevision.writeUInt(value);
  421. if (condition)
  422. dest.writeUInt(value);
  423. }
  424. void writeFloatDelta(float value, Serializer& dest, Serializer& destRevision, unsigned char condition)
  425. {
  426. destRevision.writeFloat(value);
  427. if (condition)
  428. dest.writeFloat(value);
  429. }
  430. void writeVector3Delta(const Vector3& value, Serializer& dest, Serializer& destRevision, unsigned char condition)
  431. {
  432. destRevision.writeVector3(value);
  433. if (condition)
  434. dest.writeVector3(value);
  435. }
  436. void writePackedVector3Delta(const Vector3& value, float maxAbsCoord, Serializer& dest, Serializer& destRevision, unsigned char condition)
  437. {
  438. // Use full resolution in the internal value, but pack to half size for the network stream
  439. destRevision.writeVector3(value);
  440. if (condition)
  441. dest.writePackedVector3(value, maxAbsCoord);
  442. }
  443. void writeQuaternionDelta(const Quaternion& value, Serializer& dest, Serializer& destRevision, unsigned char condition)
  444. {
  445. destRevision.writeQuaternion(value);
  446. if (condition)
  447. dest.writeQuaternion(value);
  448. }
  449. void writePackedQuaternionDelta(const Quaternion& value, Serializer& dest, Serializer& destRevision, unsigned char condition)
  450. {
  451. // Use full resolution in the internal value, but pack to half size for the network stream
  452. destRevision.writeQuaternion(value);
  453. if (condition)
  454. dest.writePackedQuaternion(value);
  455. }
  456. void writeColorDelta(const Color& value, Serializer& dest, Serializer& destRevision, unsigned char condition)
  457. {
  458. destRevision.writeColor(value);
  459. if (condition)
  460. dest.writeColor(value);
  461. }
  462. void writeStringDelta(const std::string& value, Serializer& dest, Serializer& destRevision, unsigned char condition)
  463. {
  464. destRevision.writeString(value);
  465. if (condition)
  466. dest.writeString(value);
  467. }
  468. void writeStringHashDelta(StringHash value, Serializer& dest, Serializer& destRevision, unsigned char condition)
  469. {
  470. destRevision.writeStringHash(value);
  471. if (condition)
  472. dest.writeStringHash(value);
  473. }
  474. void writeShortStringHashDelta(ShortStringHash value, Serializer& dest, Serializer& destRevision, unsigned char condition)
  475. {
  476. destRevision.writeShortStringHash(value);
  477. if (condition)
  478. dest.writeShortStringHash(value);
  479. }
  480. void writeComponentRefDelta(const ComponentRef& value, Serializer& dest, Serializer& destRevision, unsigned char condition)
  481. {
  482. value.writePacked(destRevision);
  483. if (condition)
  484. value.writePacked(dest);
  485. }
  486. void writeVLEDelta(unsigned value, Serializer& dest, Serializer& destRevision, unsigned char condition)
  487. {
  488. destRevision.writeVLE(value);
  489. if (condition)
  490. dest.writeVLE(value);
  491. }
  492. void writeBufferDelta(const VectorBuffer& value, Serializer& dest, Serializer& destRevision, unsigned char condition)
  493. {
  494. destRevision.writeVLE(value.getSize());
  495. destRevision.write(value.getData(), value.getSize());
  496. if (condition)
  497. {
  498. dest.writeVLE(value.getSize());
  499. dest.write(value.getData(), value.getSize());
  500. }
  501. }
  502. bool readBoolDelta(bool& value, Deserializer& source, unsigned char condition)
  503. {
  504. if (condition)
  505. value = source.readBool();
  506. return condition != 0;
  507. }
  508. bool readIntDelta(int& value, Deserializer& source, unsigned char condition)
  509. {
  510. if (condition)
  511. value = source.readInt();
  512. return condition != 0;
  513. }
  514. bool readUByteDelta(unsigned char& value, Deserializer& source, unsigned char condition)
  515. {
  516. if (condition)
  517. value = source.readUByte();
  518. return condition != 0;
  519. }
  520. bool readUShortDelta(unsigned short& value, Deserializer& source, unsigned char condition)
  521. {
  522. if (condition)
  523. value = source.readUShort();
  524. return condition != 0;
  525. }
  526. bool readUIntDelta(unsigned& value, Deserializer& source, unsigned char condition)
  527. {
  528. if (condition)
  529. value = source.readUInt();
  530. return condition != 0;
  531. }
  532. bool readFloatDelta(float& value, Deserializer& source, unsigned char condition)
  533. {
  534. if (condition)
  535. value = source.readFloat();
  536. return condition != 0;
  537. }
  538. bool readVector3Delta(Vector3& value, Deserializer& source, unsigned char condition)
  539. {
  540. if (condition)
  541. value = source.readVector3();
  542. return condition != 0;
  543. }
  544. bool readPackedVector3Delta(Vector3& value, float maxAbsCoord, Deserializer& source, unsigned char condition)
  545. {
  546. if (condition)
  547. value = source.readPackedVector3(maxAbsCoord);
  548. return condition != 0;
  549. }
  550. bool readQuaternionDelta(Quaternion& value, Deserializer& source, unsigned char condition)
  551. {
  552. if (condition)
  553. value = source.readQuaternion();
  554. return condition != 0;
  555. }
  556. bool readPackedQuaternionDelta(Quaternion& value, Deserializer& source, unsigned char condition)
  557. {
  558. if (condition)
  559. value = source.readPackedQuaternion();
  560. return condition != 0;
  561. }
  562. bool readColorDelta(Color& value, Deserializer& source, unsigned char condition)
  563. {
  564. if (condition)
  565. value = source.readColor();
  566. return condition != 0;
  567. }
  568. bool readStringDelta(std::string& value, Deserializer& source, unsigned char condition)
  569. {
  570. if (condition)
  571. value = source.readString();
  572. return condition != 0;
  573. }
  574. bool readStringHashDelta(StringHash& value, Deserializer& source, unsigned char condition)
  575. {
  576. if (condition)
  577. value = source.readStringHash();
  578. return condition != 0;
  579. }
  580. bool readShortStringHashDelta(ShortStringHash& value, Deserializer& source, unsigned char condition)
  581. {
  582. if (condition)
  583. value = source.readShortStringHash();
  584. return condition != 0;
  585. }
  586. bool readComponentRefDelta(ComponentRef& value, Deserializer& source, unsigned char condition)
  587. {
  588. if (condition)
  589. value.readPacked(source);
  590. return condition != 0;
  591. }
  592. bool readVLEDelta(unsigned& value, Deserializer& source, unsigned char condition)
  593. {
  594. if (condition)
  595. value = source.readVLE();
  596. return condition != 0;
  597. }
  598. bool readBufferDelta(VectorBuffer& value, Deserializer& source, unsigned char condition)
  599. {
  600. if (condition)
  601. {
  602. unsigned bufferSize = source.readVLE();
  603. value.setData(source, bufferSize);
  604. }
  605. return condition != 0;
  606. }