-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnbt_interface.hpp
More file actions
149 lines (126 loc) · 4.55 KB
/
nbt_interface.hpp
File metadata and controls
149 lines (126 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* nbt_interface - NBT Lib Interface
Copyright (C) 2025 Dream Helium
This file is part of nbtlib_interface.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
#ifndef NBT_INTERFACE_HPP
#define NBT_INTERFACE_HPP
#ifdef __cplusplus
/* The enum type for recognize */
#ifndef DH_NBT_TYPES
#define DH_NBT_TYPES
typedef enum {
DH_TYPE_INVALID, DH_TYPE_End, DH_TYPE_Byte, DH_TYPE_Short, DH_TYPE_Int, DH_TYPE_Long, DH_TYPE_Float, DH_TYPE_Double, DH_TYPE_Byte_Array, DH_TYPE_String, DH_TYPE_List, DH_TYPE_Compound, DH_TYPE_Int_Array, DH_TYPE_Long_Array} DhNbtType;
#include "libnbt/nbt.h"
#endif
#include <glib.h>
#include <vector>
#include <memory>
class DhNbtInstance
{
public:
/* Create a null instance */
DhNbtInstance()
{
current_nbt = nullptr;
original_nbt = nullptr;
};
DhNbtInstance(const char *filename);
DhNbtInstance(const char* filename, bool temporary_root);
DhNbtInstance(NBT *root, bool temporary_root);
~DhNbtInstance();
DhNbtInstance(gint8 val, const char *key, bool temporary_root);
DhNbtInstance(gint16 val, const char *key, bool temporary_root);
DhNbtInstance(gint32 val, const char *key, bool temporary_root);
DhNbtInstance(gint64 val, const char *key, bool temporary_root);
DhNbtInstance(float val, const char *key, bool temporary_root);
DhNbtInstance(double val, const char *key, bool temporary_root);
DhNbtInstance(const char *val, const char *key, bool temporary_root);
DhNbtInstance(const gint8 *val, int len, const char *key, bool temporary_root);
DhNbtInstance(const gint32 *val, int len, const char *key, bool temporary_root);
DhNbtInstance(const gint64 *val, int len, const char *key, bool temporary_root);
DhNbtInstance(DhNbtType type, const char* key, bool temporary_root);
bool operator ==(DhNbtInstance a)
{
if(current_nbt == a.current_nbt && original_nbt == a.original_nbt)
return true;
else return false;
};
DhNbtInstance dup_current_as_original(bool temporary_root);
NBT *get_original_nbt() { return original_nbt; }
NBT *get_current_nbt() { return current_nbt; }
auto get_tree_struct() { return tree_struct; }
int get_nbt_rc() { return original_nbt_storage.use_count(); }
void set_original_nbt(NBT* nbt)
{
original_nbt = nbt;
original_nbt_storage.reset(nbt, NBT_Free);
}
void set_temp_original_nbt(NBT* nbt)
{
original_nbt = nbt;
original_nbt_storage.reset(nbt, [](NBT*) {});
}
void set_current_nbt(NBT* nbt) { current_nbt = nbt; }
void set_tree_struct(std::vector<NBT*> arr) { tree_struct = arr; }
DhNbtType get_type();
bool is_non_null();
bool prev();
bool next();
bool parent();
int child_value();
bool child();
bool child(const char* key);
bool child(int index);
void goto_root();
bool is_type(DhNbtType type);
const char *get_key();
void set_key(const char* key);
void make_invalid();
bool rm_node(const char* key);
bool rm_node(int index);
void self_free();
gint8 get_byte();
gint16 get_short();
gint32 get_int();
gint64 get_long();
gint64 get_integer();
float get_float();
double get_double();
/* The array type should not be freed unless the memory is freed! */
const gchar *get_string();
const gint8 *get_byte_array(int& len);
const gint32 *get_int_array(int& len);
const gint64 *get_long_array(int& len);
void set_string(const char* str);
bool prepend(DhNbtInstance child);
bool insert_after(DhNbtInstance sibling, DhNbtInstance node);
bool insert_before(DhNbtInstance sibling, DhNbtInstance node);
bool save_to_file(const char *pos);
private:
/* Root NBT storage */
std::shared_ptr<NBT> original_nbt_storage;
/* Real Root NBT */
NBT* original_nbt;
/* The current position of NBT */
NBT* current_nbt;
/* This can be nonexist, based on the implement */
std::vector<NBT*> tree_struct;
};
extern "C"
{
#endif
void* dh_nbt_instance_cpp_new();
void dh_nbt_instance_cpp_free(void* mem);
#ifdef __cplusplus
}
#endif
#endif /* NBT_INTERFACE_HPP */