packed_data_container.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**************************************************************************/
  2. /* packed_data_container.h */
  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. #ifndef PACKED_DATA_CONTAINER_H
  31. #define PACKED_DATA_CONTAINER_H
  32. #include "core/io/resource.h"
  33. class PackedDataContainer : public Resource {
  34. GDCLASS(PackedDataContainer, Resource);
  35. enum : uint32_t {
  36. TYPE_DICT = 0xFFFFFFFF,
  37. TYPE_ARRAY = 0xFFFFFFFE,
  38. };
  39. struct DictKey {
  40. uint32_t hash;
  41. Variant key;
  42. bool operator<(const DictKey &p_key) const { return hash < p_key.hash; }
  43. };
  44. Vector<uint8_t> data;
  45. int datalen = 0;
  46. uint32_t _pack(const Variant &p_data, Vector<uint8_t> &tmpdata, HashMap<String, uint32_t> &string_cache);
  47. Variant _iter_init_ofs(const Array &p_iter, uint32_t p_offset);
  48. Variant _iter_next_ofs(const Array &p_iter, uint32_t p_offset);
  49. Variant _iter_get_ofs(const Variant &p_iter, uint32_t p_offset);
  50. Variant _iter_init(const Array &p_iter);
  51. Variant _iter_next(const Array &p_iter);
  52. Variant _iter_get(const Variant &p_iter);
  53. friend class PackedDataContainerRef;
  54. Variant _key_at_ofs(uint32_t p_ofs, const Variant &p_key, bool &err) const;
  55. Variant _get_at_ofs(uint32_t p_ofs, const uint8_t *p_buf, bool &err) const;
  56. uint32_t _type_at_ofs(uint32_t p_ofs) const;
  57. int _size(uint32_t p_ofs) const;
  58. protected:
  59. void _set_data(const Vector<uint8_t> &p_data);
  60. Vector<uint8_t> _get_data() const;
  61. static void _bind_methods();
  62. public:
  63. virtual Variant getvar(const Variant &p_key, bool *r_valid = nullptr) const override;
  64. Error pack(const Variant &p_data);
  65. int size() const;
  66. PackedDataContainer() {}
  67. };
  68. class PackedDataContainerRef : public RefCounted {
  69. GDCLASS(PackedDataContainerRef, RefCounted);
  70. friend class PackedDataContainer;
  71. uint32_t offset = 0;
  72. Ref<PackedDataContainer> from;
  73. protected:
  74. static void _bind_methods();
  75. public:
  76. Variant _iter_init(const Array &p_iter);
  77. Variant _iter_next(const Array &p_iter);
  78. Variant _iter_get(const Variant &p_iter);
  79. int size() const;
  80. virtual Variant getvar(const Variant &p_key, bool *r_valid = nullptr) const override;
  81. PackedDataContainerRef() {}
  82. };
  83. #endif // PACKED_DATA_CONTAINER_H