forked from nekiro/TFS-1.5-Downgrades
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfileloader.h
163 lines (134 loc) · 3.44 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
152
153
154
155
156
157
158
159
160
161
162
163
/**
* The Forgotten Server - a free and open-source MMORPG server emulator
* Copyright (C) 2019 Mark Samman <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef FS_FILELOADER_H
#define FS_FILELOADER_H
#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;
}
ret.assign(p, strLen);
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 // FS_FILELOADER_H