packed_data_container.cpp 11 KB

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