packed_data_container.cpp 12 KB

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