packed_data_container.h 4.0 KB

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