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