packed_data_container.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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_string_names.h"
  32. #include "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);
  95. if (rerr != OK) {
  96. err = true;
  97. ERR_FAIL_COND_V(err != OK, 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. const uint8_t *r = &rd[p_ofs];
  111. uint32_t type = decode_uint32(r);
  112. if (type == TYPE_ARRAY) {
  113. uint32_t len = decode_uint32(r + 4);
  114. return len;
  115. } else if (type == TYPE_DICT) {
  116. uint32_t len = decode_uint32(r + 4);
  117. return len;
  118. };
  119. return -1;
  120. };
  121. Variant PackedDataContainer::_key_at_ofs(uint32_t p_ofs, const Variant &p_key, bool &err) const {
  122. PoolVector<uint8_t>::Read rd = data.read();
  123. const uint8_t *r = &rd[p_ofs];
  124. uint32_t type = decode_uint32(r);
  125. if (type == TYPE_ARRAY) {
  126. if (p_key.is_num()) {
  127. int idx = p_key;
  128. int len = decode_uint32(r + 4);
  129. if (idx < 0 || idx >= len) {
  130. err = true;
  131. return Variant();
  132. }
  133. uint32_t ofs = decode_uint32(r + 8 + 4 * idx);
  134. return _get_at_ofs(ofs, rd.ptr(), err);
  135. } else {
  136. err = true;
  137. return Variant();
  138. }
  139. } else if (type == TYPE_DICT) {
  140. uint32_t hash = p_key.hash();
  141. uint32_t len = decode_uint32(r + 4);
  142. bool found = false;
  143. for (uint32_t i = 0; i < len; i++) {
  144. uint32_t khash = decode_uint32(r + 8 + i * 12 + 0);
  145. if (khash == hash) {
  146. Variant key = _get_at_ofs(decode_uint32(r + 8 + i * 12 + 4), rd.ptr(), err);
  147. if (err)
  148. return Variant();
  149. if (key == p_key) {
  150. //key matches, return value
  151. return _get_at_ofs(decode_uint32(r + 8 + i * 12 + 8), rd.ptr(), err);
  152. }
  153. found = true;
  154. } else {
  155. if (found)
  156. break;
  157. }
  158. }
  159. err = true;
  160. return Variant();
  161. } else {
  162. err = true;
  163. return Variant();
  164. }
  165. }
  166. uint32_t PackedDataContainer::_pack(const Variant &p_data, Vector<uint8_t> &tmpdata, Map<String, uint32_t> &string_cache) {
  167. switch (p_data.get_type()) {
  168. case Variant::STRING: {
  169. String s = p_data;
  170. if (string_cache.has(s)) {
  171. return string_cache[s];
  172. }
  173. string_cache[s] = tmpdata.size();
  174. }; //fallthrough
  175. case Variant::NIL:
  176. case Variant::BOOL:
  177. case Variant::INT:
  178. case Variant::REAL:
  179. case Variant::VECTOR2:
  180. case Variant::RECT2:
  181. case Variant::VECTOR3:
  182. case Variant::TRANSFORM2D:
  183. case Variant::PLANE:
  184. case Variant::QUAT:
  185. case Variant::RECT3:
  186. case Variant::BASIS:
  187. case Variant::TRANSFORM:
  188. case Variant::POOL_BYTE_ARRAY:
  189. case Variant::POOL_INT_ARRAY:
  190. case Variant::POOL_REAL_ARRAY:
  191. case Variant::POOL_STRING_ARRAY:
  192. case Variant::POOL_VECTOR2_ARRAY:
  193. case Variant::POOL_VECTOR3_ARRAY:
  194. case Variant::POOL_COLOR_ARRAY:
  195. case Variant::NODE_PATH: {
  196. uint32_t pos = tmpdata.size();
  197. int len;
  198. encode_variant(p_data, NULL, len);
  199. tmpdata.resize(tmpdata.size() + len);
  200. encode_variant(p_data, &tmpdata[pos], len);
  201. return pos;
  202. } break;
  203. // misc types
  204. case Variant::_RID:
  205. case Variant::OBJECT: {
  206. return _pack(Variant(), tmpdata, string_cache);
  207. } break;
  208. case Variant::DICTIONARY: {
  209. Dictionary d = p_data;
  210. //size is known, use sort
  211. uint32_t pos = tmpdata.size();
  212. int len = d.size();
  213. tmpdata.resize(tmpdata.size() + len * 12 + 8);
  214. encode_uint32(TYPE_DICT, &tmpdata[pos + 0]);
  215. encode_uint32(len, &tmpdata[pos + 4]);
  216. List<Variant> keys;
  217. d.get_key_list(&keys);
  218. List<DictKey> sortk;
  219. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  220. DictKey dk;
  221. dk.hash = E->get().hash();
  222. dk.key = E->get();
  223. sortk.push_back(dk);
  224. }
  225. sortk.sort();
  226. int idx = 0;
  227. for (List<DictKey>::Element *E = sortk.front(); E; E = E->next()) {
  228. encode_uint32(E->get().hash, &tmpdata[pos + 8 + idx * 12 + 0]);
  229. uint32_t ofs = _pack(E->get().key, tmpdata, string_cache);
  230. encode_uint32(ofs, &tmpdata[pos + 8 + idx * 12 + 4]);
  231. ofs = _pack(d[E->get().key], tmpdata, string_cache);
  232. encode_uint32(ofs, &tmpdata[pos + 8 + idx * 12 + 8]);
  233. idx++;
  234. }
  235. return pos;
  236. } break;
  237. case Variant::ARRAY: {
  238. Array a = p_data;
  239. //size is known, use sort
  240. uint32_t pos = tmpdata.size();
  241. int len = a.size();
  242. tmpdata.resize(tmpdata.size() + len * 4 + 8);
  243. encode_uint32(TYPE_ARRAY, &tmpdata[pos + 0]);
  244. encode_uint32(len, &tmpdata[pos + 4]);
  245. for (int i = 0; i < len; i++) {
  246. uint32_t ofs = _pack(a[i], tmpdata, string_cache);
  247. encode_uint32(ofs, &tmpdata[pos + 8 + i * 4]);
  248. }
  249. return pos;
  250. } break;
  251. default: {}
  252. }
  253. return OK;
  254. }
  255. Error PackedDataContainer::pack(const Variant &p_data) {
  256. Vector<uint8_t> tmpdata;
  257. Map<String, uint32_t> string_cache;
  258. _pack(p_data, tmpdata, string_cache);
  259. datalen = tmpdata.size();
  260. data.resize(tmpdata.size());
  261. PoolVector<uint8_t>::Write w = data.write();
  262. copymem(w.ptr(), tmpdata.ptr(), tmpdata.size());
  263. return OK;
  264. }
  265. void PackedDataContainer::_set_data(const PoolVector<uint8_t> &p_data) {
  266. data = p_data;
  267. datalen = data.size();
  268. }
  269. PoolVector<uint8_t> PackedDataContainer::_get_data() const {
  270. return data;
  271. }
  272. Variant PackedDataContainer::_iter_init(const Array &p_iter) {
  273. return _iter_init_ofs(p_iter, 0);
  274. }
  275. Variant PackedDataContainer::_iter_next(const Array &p_iter) {
  276. return _iter_next_ofs(p_iter, 0);
  277. }
  278. Variant PackedDataContainer::_iter_get(const Variant &p_iter) {
  279. return _iter_get_ofs(p_iter, 0);
  280. }
  281. void PackedDataContainer::_bind_methods() {
  282. ClassDB::bind_method(D_METHOD("_set_data"), &PackedDataContainer::_set_data);
  283. ClassDB::bind_method(D_METHOD("_get_data"), &PackedDataContainer::_get_data);
  284. ClassDB::bind_method(D_METHOD("_iter_init"), &PackedDataContainer::_iter_init);
  285. ClassDB::bind_method(D_METHOD("_iter_get"), &PackedDataContainer::_iter_get);
  286. ClassDB::bind_method(D_METHOD("_iter_next"), &PackedDataContainer::_iter_next);
  287. ClassDB::bind_method(D_METHOD("pack", "value"), &PackedDataContainer::pack);
  288. ClassDB::bind_method(D_METHOD("size"), &PackedDataContainer::size);
  289. ADD_PROPERTY(PropertyInfo(Variant::POOL_BYTE_ARRAY, "__data__"), "_set_data", "_get_data");
  290. }
  291. PackedDataContainer::PackedDataContainer() {
  292. datalen = 0;
  293. }
  294. //////////////////
  295. Variant PackedDataContainerRef::_iter_init(const Array &p_iter) {
  296. return from->_iter_init_ofs(p_iter, offset);
  297. }
  298. Variant PackedDataContainerRef::_iter_next(const Array &p_iter) {
  299. return from->_iter_next_ofs(p_iter, offset);
  300. }
  301. Variant PackedDataContainerRef::_iter_get(const Variant &p_iter) {
  302. return from->_iter_get_ofs(p_iter, offset);
  303. }
  304. bool PackedDataContainerRef::_is_dictionary() const {
  305. return from->_type_at_ofs(offset) == PackedDataContainer::TYPE_DICT;
  306. };
  307. void PackedDataContainerRef::_bind_methods() {
  308. ClassDB::bind_method(D_METHOD("size"), &PackedDataContainerRef::size);
  309. ClassDB::bind_method(D_METHOD("_iter_init"), &PackedDataContainerRef::_iter_init);
  310. ClassDB::bind_method(D_METHOD("_iter_get"), &PackedDataContainerRef::_iter_get);
  311. ClassDB::bind_method(D_METHOD("_iter_next"), &PackedDataContainerRef::_iter_next);
  312. ClassDB::bind_method(D_METHOD("_is_dictionary"), &PackedDataContainerRef::_is_dictionary);
  313. }
  314. Variant PackedDataContainerRef::getvar(const Variant &p_key, bool *r_valid) const {
  315. bool err = false;
  316. Variant ret = from->_key_at_ofs(offset, p_key, err);
  317. if (r_valid)
  318. *r_valid = !err;
  319. return ret;
  320. }
  321. int PackedDataContainerRef::size() const {
  322. return from->_size(offset);
  323. };
  324. PackedDataContainerRef::PackedDataContainerRef() {
  325. }