packed_data_container.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*************************************************************************/
  2. /* packed_data_container.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef PACKED_DATA_CONTAINER_H
  30. #define PACKED_DATA_CONTAINER_H
  31. #include "resource.h"
  32. class PackedDataContainer : public Resource {
  33. OBJ_TYPE(PackedDataContainer,Resource);
  34. enum {
  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. DVector<uint8_t> data;
  44. int datalen;
  45. uint32_t _pack(const Variant& p_data,Vector<uint8_t>& tmpdata,Map<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 DVector<uint8_t>& p_data);
  59. DVector<uint8_t> _get_data() const;
  60. static void _bind_methods();
  61. public:
  62. virtual Variant getvar(const Variant& p_key, bool *r_valid=NULL) const;
  63. Error pack(const Variant& p_data);
  64. int size() const;
  65. PackedDataContainer();
  66. };
  67. class PackedDataContainerRef : public Reference {
  68. OBJ_TYPE(PackedDataContainerRef,Reference);
  69. friend class PackedDataContainer;
  70. uint32_t offset;
  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. bool _is_dictionary() const;
  79. int size() const;
  80. virtual Variant getvar(const Variant& p_key, bool *r_valid=NULL) const;
  81. PackedDataContainerRef();
  82. };
  83. #endif // PACKED_DATA_CONTAINER_H