forked from otland/forgottenserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileloader.h
151 lines (122 loc) · 2.87 KB
/
fileloader.h
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
150
151
// Copyright 2022 The Forgotten Server Authors. All rights reserved.
// Use of this source code is governed by the GPL-2.0 License that can be found in the LICENSE file.
#ifndef FS_FILELOADER_H_9B663D19E58D42E6BFACFE5B09D7A05E
#define FS_FILELOADER_H_9B663D19E58D42E6BFACFE5B09D7A05E
#include <limits>
#include <vector>
#include <boost/iostreams/device/mapped_file.hpp>
class PropStream;
namespace OTB {
using MappedFile = boost::iostreams::mapped_file_source;
using ContentIt = MappedFile::iterator;
using Identifier = std::array<char, 4>;
struct Node
{
using ChildrenVector = std::vector<Node>;
ChildrenVector children;
ContentIt propsBegin;
ContentIt propsEnd;
uint8_t type;
enum NodeChar: uint8_t
{
ESCAPE = 0xFD,
START = 0xFE,
END = 0xFF,
};
};
struct LoadError : std::exception {
const char* what() const noexcept override = 0;
};
struct InvalidOTBFormat final : LoadError {
const char* what() const noexcept override {
return "Invalid OTBM file format";
}
};
class Loader {
MappedFile fileContents;
Node root;
std::vector<char> propBuffer;
public:
Loader(const std::string& fileName, const Identifier& acceptedIdentifier);
bool getProps(const Node& node, PropStream& props);
const Node& parseTree();
};
} //namespace OTB
class PropStream
{
public:
void init(const char* a, size_t size) {
p = a;
end = a + size;
}
size_t size() const {
return end - p;
}
template <typename T>
bool read(T& ret) {
if (size() < sizeof(T)) {
return false;
}
memcpy(&ret, p, sizeof(T));
p += sizeof(T);
return true;
}
bool readString(std::string& ret) {
uint16_t strLen;
if (!read<uint16_t>(strLen)) {
return false;
}
if (size() < strLen) {
return false;
}
char* str = new char[strLen + 1];
memcpy(str, p, strLen);
str[strLen] = 0;
ret.assign(str, strLen);
delete[] str;
p += strLen;
return true;
}
bool skip(size_t n) {
if (size() < n) {
return false;
}
p += n;
return true;
}
private:
const char* p = nullptr;
const char* end = nullptr;
};
class PropWriteStream
{
public:
PropWriteStream() = default;
// non-copyable
PropWriteStream(const PropWriteStream&) = delete;
PropWriteStream& operator=(const PropWriteStream&) = delete;
const char* getStream(size_t& size) const {
size = buffer.size();
return buffer.data();
}
void clear() {
buffer.clear();
}
template <typename T>
void write(T add) {
char* addr = reinterpret_cast<char*>(&add);
std::copy(addr, addr + sizeof(T), std::back_inserter(buffer));
}
void writeString(const std::string& str) {
size_t strLength = str.size();
if (strLength > std::numeric_limits<uint16_t>::max()) {
write<uint16_t>(0);
return;
}
write(static_cast<uint16_t>(strLength));
std::copy(str.begin(), str.end(), std::back_inserter(buffer));
}
private:
std::vector<char> buffer;
};
#endif