packed_data_container.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*************************************************************************/
  2. /* packed_data_container.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "packed_data_container.h"
  31. #include "core/core_string_names.h"
  32. #include "core/io/marshalls.h"
  33. Variant PackedDataContainer::getvar(const Variant &p_key, bool *r_valid) const {
  34. bool err = false;
  35. Variant ret = _key_at_ofs(0, p_key, err);
  36. if (r_valid)
  37. *r_valid = !err;
  38. return ret;
  39. }
  40. int PackedDataContainer::size() const {
  41. return _size(0);
  42. };
  43. Variant PackedDataContainer::_iter_init_ofs(const Array &p_iter, uint32_t p_offset) {
  44. Array ref = p_iter;
  45. uint32_t size = _size(p_offset);
  46. if (size == 0 || ref.size() != 1)
  47. return false;
  48. else {
  49. ref[0] = 0;
  50. return true;
  51. }
  52. }
  53. Variant PackedDataContainer::_iter_next_ofs(const Array &p_iter, uint32_t p_offset) {
  54. Array ref = p_iter;
  55. int size = _size(p_offset);
  56. if (ref.size() != 1)
  57. return false;
  58. int pos = ref[0];
  59. if (pos < 0 || pos >= size)
  60. return false;
  61. pos += 1;
  62. ref[0] = pos;
  63. return pos != size;
  64. }
  65. Variant PackedDataContainer::_iter_get_ofs(const Variant &p_iter, uint32_t p_offset) {
  66. int size = _size(p_offset);
  67. int pos = p_iter;
  68. if (pos < 0 || pos >= size)
  69. return Variant();
  70. PoolVector<uint8_t>::Read rd = data.read();
  71. const uint8_t *r = &rd[p_offset];
  72. uint32_t type = decode_uint32(r);
  73. bool err = false;
  74. if (type == TYPE_ARRAY) {
  75. uint32_t vpos = decode_uint32(rd.ptr() + p_offset + 8 + pos * 4);
  76. return _get_at_ofs(vpos, rd.ptr(), err);
  77. } else if (type == TYPE_DICT) {
  78. uint32_t vpos = decode_uint32(rd.ptr() + p_offset + 8 + pos * 12 + 4);
  79. return _get_at_ofs(vpos, rd.ptr(), err);
  80. } else {
  81. ERR_FAIL_V(Variant());
  82. }
  83. }
  84. Variant PackedDataContainer::_get_at_ofs(uint32_t p_ofs, const uint8_t *p_buf, bool &err) const {
  85. uint32_t type = decode_uint32(p_buf + p_ofs);
  86. if (type == TYPE_ARRAY || type == TYPE_DICT) {
  87. Ref<PackedDataContainerRef> pdcr = memnew(PackedDataContainerRef);
  88. Ref<PackedDataContainer> pdc = Ref<PackedDataContainer>((PackedDataContainer *)this);
  89. pdcr->from = pdc;
  90. pdcr->offset = p_ofs;
  91. return pdcr;
  92. } else {
  93. Variant v;
  94. Error rerr = decode_variant(v, p_buf + p_ofs, datalen - p_ofs, NULL, false);
  95. if (rerr != OK) {
  96. err = true;
  97. ERR_FAIL_COND_V_MSG(err != OK, Variant(), "Error when trying to decode Variant.");
  98. }
  99. return v;
  100. }
  101. }
  102. uint32_t PackedDataContainer::_type_at_ofs(uint32_t p_ofs) const {
  103. PoolVector<uint8_t>::Read rd = data.read();
  104. const uint8_t *r = &rd[p_ofs];
  105. uint32_t type = decode_uint32(r);
  106. return type;
  107. };
  108. int PackedDataContainer::_size(uint32_t p_ofs) const {
  109. PoolVector<uint8_t>::Read rd = data.read();
  110. ERR_FAIL_COND_V(!rd.ptr(), 0);
  111. const uint8_t *r = &rd[p_ofs];
  112. uint32_t type = decode_uint32(r);
  113. if (type == TYPE_ARRAY) {
  114. uint32_t len = decode_uint32(r + 4);
  115. return len;
  116. } else if (type == TYPE_DICT) {
  117. uint32_t len = decode_uint32(r + 4);
  118. return len;
  119. };
  120. return -1;
  121. };
  122. Variant PackedDataContainer::_key_at_ofs(uint32_t p_ofs, const Variant &p_key, bool &err) const {
  123. PoolVector<uint8_t>::Read rd = data.read();
  124. const uint8_t *r = &rd[p_ofs];
  125. uint32_t type = decode_uint32(r);
  126. if (type == TYPE_ARRAY) {
  127. if (p_key.is_num()) {
  128. int idx = p_key;
  129. int len = decode_uint32(r + 4);
  130. if (idx < 0 || idx >= len) {
  131. err = true;
  132. return Variant();
  133. }
  134. uint32_t ofs = decode_uint32(r + 8 + 4 * idx);
  135. return _get_at_ofs(ofs, rd.ptr(), err);
  136. } else {
  137. err = true;
  138. return Variant();
  139. }
  140. } else if (type == TYPE_DICT) {
  141. uint32_t hash = p_key.hash();
  142. uint32_t len = decode_uint32(r + 4);
  143. bool found = false;
  144. for (uint32_t i = 0; i < len; i++) {
  145. uint32_t khash = decode_uint32(r + 8 + i * 12 + 0);
  146. if (khash == hash) {
  147. Variant key = _get_at_ofs(decode_uint32(r + 8 + i * 12 + 4), rd.ptr(), err);
  148. if (err)
  149. return Variant();
  150. if (key == p_key) {
  151. //key matches, return value
  152. return _get_at_ofs(decode_uint32(r + 8 + i * 12 + 8), rd.ptr(), err);
  153. }
  154. found = true;
  155. } else {
  156. if (found)
  157. break;
  158. }
  159. }
  160. err = true;
  161. return Variant();
  162. } else {
  163. err = true;
  164. return Variant();
  165. }
  166. }
  167. uint32_t PackedDataContainer::_pack(const Variant &p_data, Vector<uint8_t> &tmpdata, Map<String, uint32_t> &string_cache) {
  168. switch (p_data.get_type()) {
  169. case Variant::STRING: {
  170. String s = p_data;
  171. if (string_cache.has(s)) {
  172. return string_cache[s];
  173. }
  174. string_cache[s] = tmpdata.size();
  175. FALLTHROUGH;
  176. };
  177. case Variant::NIL:
  178. case Variant::BOOL:
  179. case Variant::INT:
  180. case Variant::REAL:
  181. case Variant::VECTOR2:
  182. case Variant::RECT2:
  183. case Variant::VECTOR3:
  184. case Variant::TRANSFORM2D:
  185. case Variant::PLANE:
  186. case Variant::QUAT:
  187. case Variant::AABB:
  188. case Variant::BASIS:
  189. case Variant::TRANSFORM:
  190. case Variant::POOL_BYTE_ARRAY:
  191. case Variant::POOL_INT_ARRAY:
  192. case Variant::POOL_REAL_ARRAY:
  193. case Variant::POOL_STRING_ARRAY:
  194. case Variant::POOL_VECTOR2_ARRAY:
  195. case Variant::POOL_VECTOR3_ARRAY:
  196. case Variant::POOL_COLOR_ARRAY:
  197. case Variant::NODE_PATH: {
  198. uint32_t pos = tmpdata.size();
  199. int len;
  200. encode_variant(p_data, NULL, len, false);
  201. tmpdata.resize(tmpdata.size() + len);
  202. encode_variant(p_data, &tmpdata.write[pos], len, false);
  203. return pos;
  204. } break;
  205. // misc types
  206. case Variant::_RID:
  207. case Variant::OBJECT: {
  208. return _pack(Variant(), tmpdata, string_cache);
  209. } break;
  210. case Variant::DICTIONARY: {
  211. Dictionary d = p_data;
  212. //size is known, use sort
  213. uint32_t pos = tmpdata.size();
  214. int len = d.size();
  215. tmpdata.resize(tmpdata.size() + len * 12 + 8);
  216. encode_uint32(TYPE_DICT, &tmpdata.write[pos + 0]);
  217. encode_uint32(len, &tmpdata.write[pos + 4]);
  218. List<Variant> keys;
  219. d.get_key_list(&keys);
  220. List<DictKey> sortk;
  221. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  222. DictKey dk;
  223. dk.hash = E->get().hash();
  224. dk.key = E->get();
  225. sortk.push_back(dk);
  226. }
  227. sortk.sort();
  228. int idx = 0;
  229. for (List<DictKey>::Element *E = sortk.front(); E; E = E->next()) {
  230. encode_uint32(E->get().hash, &tmpdata.write[pos + 8 + idx * 12 + 0]);
  231. uint32_t ofs = _pack(E->get().key, tmpdata, string_cache);
  232. encode_uint32(ofs, &tmpdata.write[pos + 8 + idx * 12 + 4]);
  233. ofs = _pack(d[E->get().key], tmpdata, string_cache);
  234. encode_uint32(ofs, &tmpdata.write[pos + 8 + idx * 12 + 8]);
  235. idx++;
  236. }
  237. return pos;
  238. } break;
  239. case Variant::ARRAY: {
  240. Array a = p_data;
  241. //size is known, use sort
  242. uint32_t pos = tmpdata.size();
  243. int len = a.size();
  244. tmpdata.resize(tmpdata.size() + len * 4 + 8);
  245. encode_uint32(TYPE_ARRAY, &tmpdata.write[pos + 0]);
  246. encode_uint32(len, &tmpdata.write[pos + 4]);
  247. for (int i = 0; i < len; i++) {
  248. uint32_t ofs = _pack(a[i], tmpdata, string_cache);
  249. encode_uint32(ofs, &tmpdata.write[pos + 8 + i * 4]);
  250. }
  251. return pos;
  252. } break;
  253. default: {
  254. }
  255. }
  256. return OK;
  257. }
  258. Error PackedDataContainer::pack(const Variant &p_data) {
  259. Vector<uint8_t> tmpdata;
  260. Map<String, uint32_t> string_cache;
  261. _pack(p_data, tmpdata, string_cache);
  262. datalen = tmpdata.size();
  263. data.resize(tmpdata.size());
  264. PoolVector<uint8_t>::Write w = data.write();
  265. copymem(w.ptr(), tmpdata.ptr(), tmpdata.size());
  266. return OK;
  267. }
  268. void PackedDataContainer::_set_data(const PoolVector<uint8_t> &p_data) {
  269. data = p_data;
  270. datalen = data.size();
  271. }
  272. PoolVector<uint8_t> PackedDataContainer::_get_data() const {
  273. return data;
  274. }
  275. Variant PackedDataContainer::_iter_init(const Array &p_iter) {
  276. return _iter_init_ofs(p_iter, 0);
  277. }
  278. Variant PackedDataContainer::_iter_next(const Array &p_iter) {
  279. return _iter_next_ofs(p_iter, 0);
  280. }
  281. Variant PackedDataContainer::_iter_get(const Variant &p_iter) {
  282. return _iter_get_ofs(p_iter, 0);
  283. }
  284. void PackedDataContainer::_bind_methods() {
  285. ClassDB::bind_method(D_METHOD("_set_data"), &PackedDataContainer::_set_data);
  286. ClassDB::bind_method(D_METHOD("_get_data"), &PackedDataContainer::_get_data);
  287. ClassDB::bind_method(D_METHOD("_iter_init"), &PackedDataContainer::_iter_init);
  288. ClassDB::bind_method(D_METHOD("_iter_get"), &PackedDataContainer::_iter_get);
  289. ClassDB::bind_method(D_METHOD("_iter_next"), &PackedDataContainer::_iter_next);
  290. ClassDB::bind_method(D_METHOD("pack", "value"), &PackedDataContainer::pack);
  291. ClassDB::bind_method(D_METHOD("size"), &PackedDataContainer::size);
  292. ADD_PROPERTY(PropertyInfo(Variant::POOL_BYTE_ARRAY, "__data__"), "_set_data", "_get_data");
  293. }
  294. PackedDataContainer::PackedDataContainer() {
  295. datalen = 0;
  296. }
  297. //////////////////
  298. Variant PackedDataContainerRef::_iter_init(const Array &p_iter) {
  299. return from->_iter_init_ofs(p_iter, offset);
  300. }
  301. Variant PackedDataContainerRef::_iter_next(const Array &p_iter) {
  302. return from->_iter_next_ofs(p_iter, offset);
  303. }
  304. Variant PackedDataContainerRef::_iter_get(const Variant &p_iter) {
  305. return from->_iter_get_ofs(p_iter, offset);
  306. }
  307. bool PackedDataContainerRef::_is_dictionary() const {
  308. return from->_type_at_ofs(offset) == PackedDataContainer::TYPE_DICT;
  309. };
  310. void PackedDataContainerRef::_bind_methods() {
  311. ClassDB::bind_method(D_METHOD("size"), &PackedDataContainerRef::size);
  312. ClassDB::bind_method(D_METHOD("_iter_init"), &PackedDataContainerRef::_iter_init);
  313. ClassDB::bind_method(D_METHOD("_iter_get"), &PackedDataContainerRef::_iter_get);
  314. ClassDB::bind_method(D_METHOD("_iter_next"), &PackedDataContainerRef::_iter_next);
  315. ClassDB::bind_method(D_METHOD("_is_dictionary"), &PackedDataContainerRef::_is_dictionary);
  316. }
  317. Variant PackedDataContainerRef::getvar(const Variant &p_key, bool *r_valid) const {
  318. bool err = false;
  319. Variant ret = from->_key_at_ofs(offset, p_key, err);
  320. if (r_valid)
  321. *r_valid = !err;
  322. return ret;
  323. }
  324. int PackedDataContainerRef::size() const {
  325. return from->_size(offset);
  326. };
  327. PackedDataContainerRef::PackedDataContainerRef() {
  328. }