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