packed_data_container.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. }
  39. if (err) {
  40. return Object::getvar(p_key, r_valid);
  41. }
  42. return ret;
  43. }
  44. int PackedDataContainer::size() const {
  45. return _size(0);
  46. }
  47. Variant PackedDataContainer::_iter_init_ofs(const Array &p_iter, uint32_t p_offset) {
  48. Array ref = p_iter;
  49. uint32_t size = _size(p_offset);
  50. if (size == 0 || ref.size() != 1) {
  51. return false;
  52. } else {
  53. ref[0] = 0;
  54. return true;
  55. }
  56. }
  57. Variant PackedDataContainer::_iter_next_ofs(const Array &p_iter, uint32_t p_offset) {
  58. Array ref = p_iter;
  59. int size = _size(p_offset);
  60. if (ref.size() != 1) {
  61. return false;
  62. }
  63. int pos = ref[0];
  64. if (pos < 0 || pos >= size) {
  65. return false;
  66. }
  67. pos += 1;
  68. ref[0] = pos;
  69. return pos != size;
  70. }
  71. Variant PackedDataContainer::_iter_get_ofs(const Variant &p_iter, uint32_t p_offset) {
  72. int size = _size(p_offset);
  73. int pos = p_iter;
  74. if (pos < 0 || pos >= size) {
  75. return Variant();
  76. }
  77. const uint8_t *rd = data.ptr();
  78. const uint8_t *r = &rd[p_offset];
  79. uint32_t type = decode_uint32(r);
  80. bool err = false;
  81. if (type == TYPE_ARRAY) {
  82. uint32_t vpos = decode_uint32(rd + p_offset + 8 + pos * 4);
  83. return _get_at_ofs(vpos, rd, err);
  84. } else if (type == TYPE_DICT) {
  85. uint32_t vpos = decode_uint32(rd + p_offset + 8 + pos * 12 + 4);
  86. return _get_at_ofs(vpos, rd, err);
  87. } else {
  88. ERR_FAIL_V(Variant());
  89. }
  90. }
  91. Variant PackedDataContainer::_get_at_ofs(uint32_t p_ofs, const uint8_t *p_buf, bool &err) const {
  92. uint32_t type = decode_uint32(p_buf + p_ofs);
  93. if (type == TYPE_ARRAY || type == TYPE_DICT) {
  94. Ref<PackedDataContainerRef> pdcr = memnew(PackedDataContainerRef);
  95. Ref<PackedDataContainer> pdc = Ref<PackedDataContainer>((PackedDataContainer *)this);
  96. pdcr->from = pdc;
  97. pdcr->offset = p_ofs;
  98. return pdcr;
  99. } else {
  100. Variant v;
  101. Error rerr = decode_variant(v, p_buf + p_ofs, datalen - p_ofs, nullptr, false);
  102. if (rerr != OK) {
  103. err = true;
  104. ERR_FAIL_COND_V_MSG(err != OK, Variant(), "Error when trying to decode Variant.");
  105. }
  106. return v;
  107. }
  108. }
  109. uint32_t PackedDataContainer::_type_at_ofs(uint32_t p_ofs) const {
  110. const uint8_t *rd = data.ptr();
  111. ERR_FAIL_COND_V(!rd, 0);
  112. const uint8_t *r = &rd[p_ofs];
  113. uint32_t type = decode_uint32(r);
  114. return type;
  115. }
  116. int PackedDataContainer::_size(uint32_t p_ofs) const {
  117. const uint8_t *rd = data.ptr();
  118. ERR_FAIL_COND_V(!rd, 0);
  119. const uint8_t *r = &rd[p_ofs];
  120. uint32_t type = decode_uint32(r);
  121. if (type == TYPE_ARRAY) {
  122. uint32_t len = decode_uint32(r + 4);
  123. return len;
  124. } else if (type == TYPE_DICT) {
  125. uint32_t len = decode_uint32(r + 4);
  126. return len;
  127. }
  128. return -1;
  129. }
  130. Variant PackedDataContainer::_key_at_ofs(uint32_t p_ofs, const Variant &p_key, bool &err) const {
  131. const uint8_t *rd = data.ptr();
  132. if (!rd) {
  133. err = true;
  134. ERR_FAIL_COND_V(!rd, Variant());
  135. }
  136. const uint8_t *r = &rd[p_ofs];
  137. uint32_t type = decode_uint32(r);
  138. if (type == TYPE_ARRAY) {
  139. if (p_key.is_num()) {
  140. int idx = p_key;
  141. int len = decode_uint32(r + 4);
  142. if (idx < 0 || idx >= len) {
  143. err = true;
  144. return Variant();
  145. }
  146. uint32_t ofs = decode_uint32(r + 8 + 4 * idx);
  147. return _get_at_ofs(ofs, rd, err);
  148. } else {
  149. err = true;
  150. return Variant();
  151. }
  152. } else if (type == TYPE_DICT) {
  153. uint32_t hash = p_key.hash();
  154. uint32_t len = decode_uint32(r + 4);
  155. bool found = false;
  156. for (uint32_t i = 0; i < len; i++) {
  157. uint32_t khash = decode_uint32(r + 8 + i * 12 + 0);
  158. if (khash == hash) {
  159. Variant key = _get_at_ofs(decode_uint32(r + 8 + i * 12 + 4), rd, err);
  160. if (err) {
  161. return Variant();
  162. }
  163. if (key == p_key) {
  164. //key matches, return value
  165. return _get_at_ofs(decode_uint32(r + 8 + i * 12 + 8), rd, err);
  166. }
  167. found = true;
  168. } else {
  169. if (found) {
  170. break;
  171. }
  172. }
  173. }
  174. err = true;
  175. return Variant();
  176. } else {
  177. err = true;
  178. return Variant();
  179. }
  180. }
  181. uint32_t PackedDataContainer::_pack(const Variant &p_data, Vector<uint8_t> &tmpdata, Map<String, uint32_t> &string_cache) {
  182. switch (p_data.get_type()) {
  183. case Variant::STRING: {
  184. String s = p_data;
  185. if (string_cache.has(s)) {
  186. return string_cache[s];
  187. }
  188. string_cache[s] = tmpdata.size();
  189. [[fallthrough]];
  190. }
  191. case Variant::NIL:
  192. case Variant::BOOL:
  193. case Variant::INT:
  194. case Variant::FLOAT:
  195. case Variant::VECTOR2:
  196. case Variant::RECT2:
  197. case Variant::VECTOR3:
  198. case Variant::TRANSFORM2D:
  199. case Variant::PLANE:
  200. case Variant::QUAT:
  201. case Variant::AABB:
  202. case Variant::BASIS:
  203. case Variant::TRANSFORM3D:
  204. case Variant::PACKED_BYTE_ARRAY:
  205. case Variant::PACKED_INT32_ARRAY:
  206. case Variant::PACKED_INT64_ARRAY:
  207. case Variant::PACKED_FLOAT32_ARRAY:
  208. case Variant::PACKED_FLOAT64_ARRAY:
  209. case Variant::PACKED_STRING_ARRAY:
  210. case Variant::PACKED_VECTOR2_ARRAY:
  211. case Variant::PACKED_VECTOR3_ARRAY:
  212. case Variant::PACKED_COLOR_ARRAY:
  213. case Variant::STRING_NAME:
  214. case Variant::NODE_PATH: {
  215. uint32_t pos = tmpdata.size();
  216. int len;
  217. encode_variant(p_data, nullptr, len, false);
  218. tmpdata.resize(tmpdata.size() + len);
  219. encode_variant(p_data, &tmpdata.write[pos], len, false);
  220. return pos;
  221. } break;
  222. // misc types
  223. case Variant::RID:
  224. case Variant::OBJECT: {
  225. return _pack(Variant(), tmpdata, string_cache);
  226. } break;
  227. case Variant::DICTIONARY: {
  228. Dictionary d = p_data;
  229. //size is known, use sort
  230. uint32_t pos = tmpdata.size();
  231. int len = d.size();
  232. tmpdata.resize(tmpdata.size() + len * 12 + 8);
  233. encode_uint32(TYPE_DICT, &tmpdata.write[pos + 0]);
  234. encode_uint32(len, &tmpdata.write[pos + 4]);
  235. List<Variant> keys;
  236. d.get_key_list(&keys);
  237. List<DictKey> sortk;
  238. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  239. DictKey dk;
  240. dk.hash = E->get().hash();
  241. dk.key = E->get();
  242. sortk.push_back(dk);
  243. }
  244. sortk.sort();
  245. int idx = 0;
  246. for (List<DictKey>::Element *E = sortk.front(); E; E = E->next()) {
  247. encode_uint32(E->get().hash, &tmpdata.write[pos + 8 + idx * 12 + 0]);
  248. uint32_t ofs = _pack(E->get().key, tmpdata, string_cache);
  249. encode_uint32(ofs, &tmpdata.write[pos + 8 + idx * 12 + 4]);
  250. ofs = _pack(d[E->get().key], tmpdata, string_cache);
  251. encode_uint32(ofs, &tmpdata.write[pos + 8 + idx * 12 + 8]);
  252. idx++;
  253. }
  254. return pos;
  255. } break;
  256. case Variant::ARRAY: {
  257. Array a = p_data;
  258. //size is known, use sort
  259. uint32_t pos = tmpdata.size();
  260. int len = a.size();
  261. tmpdata.resize(tmpdata.size() + len * 4 + 8);
  262. encode_uint32(TYPE_ARRAY, &tmpdata.write[pos + 0]);
  263. encode_uint32(len, &tmpdata.write[pos + 4]);
  264. for (int i = 0; i < len; i++) {
  265. uint32_t ofs = _pack(a[i], tmpdata, string_cache);
  266. encode_uint32(ofs, &tmpdata.write[pos + 8 + i * 4]);
  267. }
  268. return pos;
  269. } break;
  270. default: {
  271. }
  272. }
  273. return OK;
  274. }
  275. Error PackedDataContainer::pack(const Variant &p_data) {
  276. Vector<uint8_t> tmpdata;
  277. Map<String, uint32_t> string_cache;
  278. _pack(p_data, tmpdata, string_cache);
  279. datalen = tmpdata.size();
  280. data.resize(tmpdata.size());
  281. uint8_t *w = data.ptrw();
  282. memcpy(w, tmpdata.ptr(), tmpdata.size());
  283. return OK;
  284. }
  285. void PackedDataContainer::_set_data(const Vector<uint8_t> &p_data) {
  286. data = p_data;
  287. datalen = data.size();
  288. }
  289. Vector<uint8_t> PackedDataContainer::_get_data() const {
  290. return data;
  291. }
  292. Variant PackedDataContainer::_iter_init(const Array &p_iter) {
  293. return _iter_init_ofs(p_iter, 0);
  294. }
  295. Variant PackedDataContainer::_iter_next(const Array &p_iter) {
  296. return _iter_next_ofs(p_iter, 0);
  297. }
  298. Variant PackedDataContainer::_iter_get(const Variant &p_iter) {
  299. return _iter_get_ofs(p_iter, 0);
  300. }
  301. void PackedDataContainer::_bind_methods() {
  302. ClassDB::bind_method(D_METHOD("_set_data"), &PackedDataContainer::_set_data);
  303. ClassDB::bind_method(D_METHOD("_get_data"), &PackedDataContainer::_get_data);
  304. ClassDB::bind_method(D_METHOD("_iter_init"), &PackedDataContainer::_iter_init);
  305. ClassDB::bind_method(D_METHOD("_iter_get"), &PackedDataContainer::_iter_get);
  306. ClassDB::bind_method(D_METHOD("_iter_next"), &PackedDataContainer::_iter_next);
  307. ClassDB::bind_method(D_METHOD("pack", "value"), &PackedDataContainer::pack);
  308. ClassDB::bind_method(D_METHOD("size"), &PackedDataContainer::size);
  309. ADD_PROPERTY(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "__data__"), "_set_data", "_get_data");
  310. }
  311. //////////////////
  312. Variant PackedDataContainerRef::_iter_init(const Array &p_iter) {
  313. return from->_iter_init_ofs(p_iter, offset);
  314. }
  315. Variant PackedDataContainerRef::_iter_next(const Array &p_iter) {
  316. return from->_iter_next_ofs(p_iter, offset);
  317. }
  318. Variant PackedDataContainerRef::_iter_get(const Variant &p_iter) {
  319. return from->_iter_get_ofs(p_iter, offset);
  320. }
  321. bool PackedDataContainerRef::_is_dictionary() const {
  322. return from->_type_at_ofs(offset) == PackedDataContainer::TYPE_DICT;
  323. }
  324. void PackedDataContainerRef::_bind_methods() {
  325. ClassDB::bind_method(D_METHOD("size"), &PackedDataContainerRef::size);
  326. ClassDB::bind_method(D_METHOD("_iter_init"), &PackedDataContainerRef::_iter_init);
  327. ClassDB::bind_method(D_METHOD("_iter_get"), &PackedDataContainerRef::_iter_get);
  328. ClassDB::bind_method(D_METHOD("_iter_next"), &PackedDataContainerRef::_iter_next);
  329. ClassDB::bind_method(D_METHOD("_is_dictionary"), &PackedDataContainerRef::_is_dictionary);
  330. }
  331. Variant PackedDataContainerRef::getvar(const Variant &p_key, bool *r_valid) const {
  332. bool err = false;
  333. Variant ret = from->_key_at_ofs(offset, p_key, err);
  334. if (r_valid) {
  335. *r_valid = !err;
  336. }
  337. return ret;
  338. }
  339. int PackedDataContainerRef::size() const {
  340. return from->_size(offset);
  341. }