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