packed_data_container.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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_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. uint32_t 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. uint32_t size = _size(p_offset);
  67. int pos = p_iter;
  68. if (pos < 0 || pos >= size)
  69. return Variant();
  70. DVector<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. DVector<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. DVector<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. DVector<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. uint32_t 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 (int 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::MATRIX32:
  183. case Variant::PLANE:
  184. case Variant::QUAT:
  185. case Variant::_AABB:
  186. case Variant::MATRIX3:
  187. case Variant::TRANSFORM:
  188. case Variant::IMAGE:
  189. case Variant::INPUT_EVENT:
  190. case Variant::RAW_ARRAY:
  191. case Variant::INT_ARRAY:
  192. case Variant::REAL_ARRAY:
  193. case Variant::STRING_ARRAY:
  194. case Variant::VECTOR2_ARRAY:
  195. case Variant::VECTOR3_ARRAY:
  196. case Variant::COLOR_ARRAY:
  197. case Variant::NODE_PATH: {
  198. uint32_t pos = tmpdata.size();
  199. int len;
  200. encode_variant(p_data, NULL, len);
  201. tmpdata.resize(tmpdata.size() + len);
  202. encode_variant(p_data, &tmpdata[pos], len);
  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[pos + 0]);
  217. encode_uint32(len, &tmpdata[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[pos + 8 + idx * 12 + 0]);
  231. uint32_t ofs = _pack(E->get().key, tmpdata, string_cache);
  232. encode_uint32(ofs, &tmpdata[pos + 8 + idx * 12 + 4]);
  233. ofs = _pack(d[E->get().key], tmpdata, string_cache);
  234. encode_uint32(ofs, &tmpdata[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[pos + 0]);
  246. encode_uint32(len, &tmpdata[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[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. DVector<uint8_t>::Write w = data.write();
  265. copymem(w.ptr(), tmpdata.ptr(), tmpdata.size());
  266. return OK;
  267. }
  268. void PackedDataContainer::_set_data(const DVector<uint8_t> &p_data) {
  269. data = p_data;
  270. datalen = data.size();
  271. }
  272. DVector<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. ObjectTypeDB::bind_method(_MD("_set_data"), &PackedDataContainer::_set_data);
  286. ObjectTypeDB::bind_method(_MD("_get_data"), &PackedDataContainer::_get_data);
  287. ObjectTypeDB::bind_method(_MD("_iter_init"), &PackedDataContainer::_iter_init);
  288. ObjectTypeDB::bind_method(_MD("_iter_get"), &PackedDataContainer::_iter_get);
  289. ObjectTypeDB::bind_method(_MD("_iter_next"), &PackedDataContainer::_iter_next);
  290. ObjectTypeDB::bind_method(_MD("pack:Error", "value"), &PackedDataContainer::pack);
  291. ObjectTypeDB::bind_method(_MD("size"), &PackedDataContainer::size);
  292. ADD_PROPERTY(PropertyInfo(Variant::RAW_ARRAY, "__data__"), _SCS("_set_data"), _SCS("_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. ObjectTypeDB::bind_method(_MD("size"), &PackedDataContainerRef::size);
  312. ObjectTypeDB::bind_method(_MD("_iter_init"), &PackedDataContainerRef::_iter_init);
  313. ObjectTypeDB::bind_method(_MD("_iter_get"), &PackedDataContainerRef::_iter_get);
  314. ObjectTypeDB::bind_method(_MD("_iter_next"), &PackedDataContainerRef::_iter_next);
  315. ObjectTypeDB::bind_method(_MD("_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. }