packed_data_container.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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/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. ERR_FAIL_COND_V(p_ofs + 4 > (uint32_t)data.size(), Variant());
  93. uint32_t type = decode_uint32(p_buf + p_ofs);
  94. if (type == TYPE_ARRAY || type == TYPE_DICT) {
  95. Ref<PackedDataContainerRef> pdcr = memnew(PackedDataContainerRef);
  96. Ref<PackedDataContainer> pdc = Ref<PackedDataContainer>(const_cast<PackedDataContainer *>(this));
  97. pdcr->from = pdc;
  98. pdcr->offset = p_ofs;
  99. return pdcr;
  100. } else {
  101. Variant v;
  102. Error rerr = decode_variant(v, p_buf + p_ofs, datalen - p_ofs, nullptr, false);
  103. if (rerr != OK) {
  104. err = true;
  105. ERR_FAIL_COND_V_MSG(err != OK, Variant(), "Error when trying to decode Variant.");
  106. }
  107. return v;
  108. }
  109. }
  110. uint32_t PackedDataContainer::_type_at_ofs(uint32_t p_ofs) const {
  111. ERR_FAIL_COND_V(p_ofs + 4 > (uint32_t)data.size(), 0);
  112. const uint8_t *rd = data.ptr();
  113. ERR_FAIL_COND_V(!rd, 0);
  114. const uint8_t *r = &rd[p_ofs];
  115. uint32_t type = decode_uint32(r);
  116. return type;
  117. }
  118. int PackedDataContainer::_size(uint32_t p_ofs) const {
  119. ERR_FAIL_COND_V(p_ofs + 4 > (uint32_t)data.size(), 0);
  120. const uint8_t *rd = data.ptr();
  121. ERR_FAIL_COND_V(!rd, 0);
  122. const uint8_t *r = &rd[p_ofs];
  123. uint32_t type = decode_uint32(r);
  124. if (type == TYPE_ARRAY) {
  125. uint32_t len = decode_uint32(r + 4);
  126. return len;
  127. } else if (type == TYPE_DICT) {
  128. uint32_t len = decode_uint32(r + 4);
  129. return len;
  130. }
  131. return -1;
  132. }
  133. Variant PackedDataContainer::_key_at_ofs(uint32_t p_ofs, const Variant &p_key, bool &err) const {
  134. ERR_FAIL_COND_V(p_ofs + 4 > (uint32_t)data.size(), Variant());
  135. const uint8_t *rd = data.ptr();
  136. if (!rd) {
  137. err = true;
  138. ERR_FAIL_COND_V(!rd, Variant());
  139. }
  140. const uint8_t *r = &rd[p_ofs];
  141. uint32_t type = decode_uint32(r);
  142. if (type == TYPE_ARRAY) {
  143. if (p_key.is_num()) {
  144. int idx = p_key;
  145. int len = decode_uint32(r + 4);
  146. if (idx < 0 || idx >= len) {
  147. err = true;
  148. return Variant();
  149. }
  150. uint32_t ofs = decode_uint32(r + 8 + 4 * idx);
  151. return _get_at_ofs(ofs, rd, err);
  152. } else {
  153. err = true;
  154. return Variant();
  155. }
  156. } else if (type == TYPE_DICT) {
  157. uint32_t hash = p_key.hash();
  158. uint32_t len = decode_uint32(r + 4);
  159. bool found = false;
  160. for (uint32_t i = 0; i < len; i++) {
  161. uint32_t khash = decode_uint32(r + 8 + i * 12 + 0);
  162. if (khash == hash) {
  163. Variant key = _get_at_ofs(decode_uint32(r + 8 + i * 12 + 4), rd, err);
  164. if (err) {
  165. return Variant();
  166. }
  167. if (key == p_key) {
  168. //key matches, return value
  169. return _get_at_ofs(decode_uint32(r + 8 + i * 12 + 8), rd, err);
  170. }
  171. found = true;
  172. } else {
  173. if (found) {
  174. break;
  175. }
  176. }
  177. }
  178. err = true;
  179. return Variant();
  180. } else {
  181. err = true;
  182. return Variant();
  183. }
  184. }
  185. uint32_t PackedDataContainer::_pack(const Variant &p_data, Vector<uint8_t> &tmpdata, HashMap<String, uint32_t> &string_cache) {
  186. switch (p_data.get_type()) {
  187. case Variant::STRING: {
  188. String s = p_data;
  189. if (string_cache.has(s)) {
  190. return string_cache[s];
  191. }
  192. string_cache[s] = tmpdata.size();
  193. [[fallthrough]];
  194. }
  195. case Variant::NIL:
  196. case Variant::BOOL:
  197. case Variant::INT:
  198. case Variant::FLOAT:
  199. case Variant::VECTOR2:
  200. case Variant::RECT2:
  201. case Variant::VECTOR3:
  202. case Variant::TRANSFORM2D:
  203. case Variant::PLANE:
  204. case Variant::QUATERNION:
  205. case Variant::AABB:
  206. case Variant::BASIS:
  207. case Variant::TRANSFORM3D:
  208. case Variant::PACKED_BYTE_ARRAY:
  209. case Variant::PACKED_INT32_ARRAY:
  210. case Variant::PACKED_INT64_ARRAY:
  211. case Variant::PACKED_FLOAT32_ARRAY:
  212. case Variant::PACKED_FLOAT64_ARRAY:
  213. case Variant::PACKED_STRING_ARRAY:
  214. case Variant::PACKED_VECTOR2_ARRAY:
  215. case Variant::PACKED_VECTOR3_ARRAY:
  216. case Variant::PACKED_COLOR_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. Vector<uint8_t> tmpdata;
  281. HashMap<String, uint32_t> string_cache;
  282. _pack(p_data, tmpdata, string_cache);
  283. datalen = tmpdata.size();
  284. data.resize(tmpdata.size());
  285. uint8_t *w = data.ptrw();
  286. memcpy(w, tmpdata.ptr(), tmpdata.size());
  287. return OK;
  288. }
  289. void PackedDataContainer::_set_data(const Vector<uint8_t> &p_data) {
  290. data = p_data;
  291. datalen = data.size();
  292. }
  293. Vector<uint8_t> PackedDataContainer::_get_data() const {
  294. return data;
  295. }
  296. Variant PackedDataContainer::_iter_init(const Array &p_iter) {
  297. return _iter_init_ofs(p_iter, 0);
  298. }
  299. Variant PackedDataContainer::_iter_next(const Array &p_iter) {
  300. return _iter_next_ofs(p_iter, 0);
  301. }
  302. Variant PackedDataContainer::_iter_get(const Variant &p_iter) {
  303. return _iter_get_ofs(p_iter, 0);
  304. }
  305. void PackedDataContainer::_bind_methods() {
  306. ClassDB::bind_method(D_METHOD("_set_data", "data"), &PackedDataContainer::_set_data);
  307. ClassDB::bind_method(D_METHOD("_get_data"), &PackedDataContainer::_get_data);
  308. ClassDB::bind_method(D_METHOD("_iter_init"), &PackedDataContainer::_iter_init);
  309. ClassDB::bind_method(D_METHOD("_iter_get"), &PackedDataContainer::_iter_get);
  310. ClassDB::bind_method(D_METHOD("_iter_next"), &PackedDataContainer::_iter_next);
  311. ClassDB::bind_method(D_METHOD("pack", "value"), &PackedDataContainer::pack);
  312. ClassDB::bind_method(D_METHOD("size"), &PackedDataContainer::size);
  313. ADD_PROPERTY(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "__data__"), "_set_data", "_get_data");
  314. }
  315. //////////////////
  316. Variant PackedDataContainerRef::_iter_init(const Array &p_iter) {
  317. return from->_iter_init_ofs(p_iter, offset);
  318. }
  319. Variant PackedDataContainerRef::_iter_next(const Array &p_iter) {
  320. return from->_iter_next_ofs(p_iter, offset);
  321. }
  322. Variant PackedDataContainerRef::_iter_get(const Variant &p_iter) {
  323. return from->_iter_get_ofs(p_iter, offset);
  324. }
  325. bool PackedDataContainerRef::_is_dictionary() const {
  326. return from->_type_at_ofs(offset) == PackedDataContainer::TYPE_DICT;
  327. }
  328. void PackedDataContainerRef::_bind_methods() {
  329. ClassDB::bind_method(D_METHOD("size"), &PackedDataContainerRef::size);
  330. ClassDB::bind_method(D_METHOD("_iter_init"), &PackedDataContainerRef::_iter_init);
  331. ClassDB::bind_method(D_METHOD("_iter_get"), &PackedDataContainerRef::_iter_get);
  332. ClassDB::bind_method(D_METHOD("_iter_next"), &PackedDataContainerRef::_iter_next);
  333. ClassDB::bind_method(D_METHOD("_is_dictionary"), &PackedDataContainerRef::_is_dictionary);
  334. }
  335. Variant PackedDataContainerRef::getvar(const Variant &p_key, bool *r_valid) const {
  336. bool err = false;
  337. Variant ret = from->_key_at_ofs(offset, p_key, err);
  338. if (r_valid) {
  339. *r_valid = !err;
  340. }
  341. return ret;
  342. }
  343. int PackedDataContainerRef::size() const {
  344. return from->_size(offset);
  345. }