forked from EasyRPG/Player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilesystem.cpp
392 lines (326 loc) · 11.3 KB
/
filesystem.cpp
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
* This file is part of EasyRPG Player.
*
* EasyRPG Player 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 3 of the License, or
* (at your option) any later version.
*
* EasyRPG Player 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 EasyRPG Player. If not, see <http://www.gnu.org/licenses/>.
*/
#include "filesystem.h"
#include "filesystem_native.h"
#include "filesystem_lzh.h"
#include "filesystem_zip.h"
#include "filesystem_stream.h"
#include "filefinder.h"
#include "utils.h"
#include "output.h"
#include "player.h"
#include <lcf/reader_util.h>
#include <algorithm>
#include <cassert>
#include <utility>
Filesystem::Filesystem(std::string base_path, FilesystemView parent_fs) : base_path(std::move(base_path)) {
this->parent_fs = std::make_unique<FilesystemView>(parent_fs);
tree = DirectoryTree::Create(*this);
};
Filesystem_Stream::InputStream Filesystem::OpenInputStream(StringView name, std::ios_base::openmode m) const {
if (name.empty()) {
return Filesystem_Stream::InputStream();
}
std::streambuf* buf = CreateInputStreambuffer(name, m | std::ios_base::in);
if (!buf) {
return Filesystem_Stream::InputStream();
}
Filesystem_Stream::InputStream is(buf, ToString(name));
return is;
}
Filesystem_Stream::InputStream Filesystem::OpenOrCreateInputStream(StringView name, std::ios_base::openmode m) const {
auto is = OpenInputStream(name, m);
if (!is) {
auto os = OpenOutputStream(name, (std::ios_base::openmode)0);
if (!os) {
return Filesystem_Stream::InputStream();
}
}
return OpenInputStream(name, m);
}
Filesystem_Stream::OutputStream Filesystem::OpenOutputStream(StringView name, std::ios_base::openmode m) const {
if (name.empty()) {
return Filesystem_Stream::OutputStream();
}
std::streambuf* buf = CreateOutputStreambuffer(name, m | std::ios_base::out);
if (!buf) {
return Filesystem_Stream::OutputStream();
}
std::string path;
std::tie(path, std::ignore) = FileFinder::GetPathAndFilename(name);
Filesystem_Stream::OutputStream os(buf, Subtree(path), ToString(name));
return os;
}
void Filesystem::ClearCache(StringView path) const {
tree->ClearCache(path);
}
FilesystemView Filesystem::Create(StringView path) const {
// Determine the proper file system to use
// When the path doesn't exist check if the path contains a file that can
// be handled by another filesystem
if (!IsDirectory(path, true)) {
std::string dir_of_file;
std::string path_prefix;
std::vector<std::string> components = FileFinder::SplitPath(path);
// TODO this should probably move to a static function in the FS classes
// Search for the deepest directory
int i = 0;
for (const auto& comp : components) {
// Do not check stuff that looks like drives, such as C:, ux0: or sd:
// Some systems do not consider them directories
if (i > 0 || (!comp.empty() && comp.back() != ':')) {
if (!IsDirectory(FileFinder::MakePath(dir_of_file, comp), true)) {
break;
}
}
dir_of_file += comp + "/";
++i;
}
if (!dir_of_file.empty()) {
dir_of_file.pop_back();
}
// The next component must be a file
// search for known file extensions and "do magic"
std::string internal_path;
bool handle_internal = false;
for (const auto& comp : lcf::MakeSpan(components).subspan(i)) {
if (handle_internal) {
internal_path += comp + "/";
} else {
path_prefix += comp + "/";
if (FileFinder::IsSupportedArchiveExtension(comp)) {
path_prefix.pop_back();
handle_internal = true;
}
}
}
if (!internal_path.empty()) {
internal_path.pop_back();
}
std::shared_ptr<Filesystem> filesystem = std::make_shared<ZipFilesystem>(path_prefix, Subtree(dir_of_file));
if (!filesystem->IsValid()) {
#if HAVE_LHASA
filesystem = std::make_shared<LzhFilesystem>(path_prefix, Subtree(dir_of_file));
#endif
if (!filesystem->IsValid()) {
return {};
}
}
if (!internal_path.empty()) {
auto fs_view = filesystem->Create(internal_path);
if (!fs_view) {
return {};
}
return fs_view;
}
// This is the root of a new Filesystem
// Check if it only contains a single folder and if yes enter that folder
// This way archives with structure "archive/game_folder" launch the game directly
auto fs_view = filesystem->Subtree("");
if (!fs_view) {
return {};
}
auto entries = fs_view.ListDirectory("");
if (entries->size() == 1 && entries->begin()->second.type == DirectoryTree::FileType::Directory) {
return fs_view.Subtree(entries->begin()->second.name);
}
return fs_view;
} else {
if (!(Exists(path) || !IsDirectory(path, true))) {
return {};
}
// Handle as a normal path in the current filesystem
return Subtree(ToString(path));
}
}
FilesystemView Filesystem::Subtree(std::string sub_path) const {
return FilesystemView(shared_from_this(), sub_path);
}
bool Filesystem::MakeDirectory(StringView, bool) const {
return false;
}
bool Filesystem::IsValid() const {
// FIXME: better way to do this?
return Exists("");
}
std::string Filesystem::FindFile(StringView filename, const Span<const StringView> exts) const {
return tree->FindFile(filename, exts);
}
std::string Filesystem::FindFile(StringView directory, StringView filename, const Span<const StringView> exts) const {
return tree->FindFile(directory, filename, exts);
}
std::string Filesystem::FindFile(const DirectoryTree::Args& args) const {
return tree->FindFile(args);
}
Filesystem_Stream::InputStream Filesystem::OpenFile(StringView filename, const Span<const StringView> exts) const {
return OpenInputStream(tree->FindFile(filename, exts));
}
Filesystem_Stream::InputStream Filesystem::OpenFile(StringView directory, StringView filename, const Span<const StringView> exts) const {
return OpenInputStream(tree->FindFile(directory, filename, exts));
}
Filesystem_Stream::InputStream Filesystem::OpenFile(const DirectoryTree::Args& args) const {
return OpenInputStream(tree->FindFile(args));
}
FilesystemView::FilesystemView(const std::shared_ptr<const Filesystem>& fs, std::string sub_path) :
fs(fs), sub_path(std::move(sub_path)) {
valid = (fs->ListDirectory(this->sub_path) != nullptr);
}
std::string FilesystemView::GetBasePath() const {
assert(fs);
return fs->GetPath();
}
std::string FilesystemView::GetSubPath() const {
assert(fs);
return sub_path;
}
std::string FilesystemView::GetFullPath() const {
assert(fs);
return FileFinder::MakePath(GetBasePath(), GetSubPath());
}
const Filesystem& FilesystemView::GetOwner() const {
assert(fs);
return *fs;
}
void FilesystemView::ClearCache() const {
assert(fs);
fs->ClearCache(GetSubPath());
}
std::string FilesystemView::FindFile(StringView name, const Span<const StringView> exts) const {
assert(fs);
std::string found = fs->FindFile(MakePath(name), exts);
if (!found.empty() && !sub_path.empty()) {
assert(StringView(found).starts_with(sub_path));
// substr calculation must consider if the subpath is / or drive:/
return found.substr(sub_path.size() + (sub_path.back() == '/' ? 0 : 1));
}
return found;
}
std::string FilesystemView::FindFile(StringView dir, StringView name, const Span<const StringView> exts) const {
assert(fs);
std::string found = fs->FindFile(MakePath(dir), name, exts);
if (!found.empty() && !sub_path.empty()) {
assert(StringView(found).starts_with(sub_path));
// substr calculation must consider if the subpath is / or drive:/
return found.substr(sub_path.size() + (sub_path.back() == '/' ? 0 : 1));
}
return found;
}
std::string FilesystemView::FindFile(const DirectoryTree::Args& args) const {
assert(fs);
auto args_cp = args;
std::string path = MakePath(args.path);
args_cp.path = path;
std::string found = fs->FindFile(args_cp);
if (!found.empty() && !sub_path.empty()) {
assert(StringView(found).starts_with(sub_path));
// substr calculation must consider if the subpath is / or drive:/
return found.substr(sub_path.size() + (sub_path.back() == '/' ? 0 : 1));
}
return found;
}
Filesystem_Stream::InputStream FilesystemView::OpenFile(StringView name, const Span<const StringView> exts) const {
assert(fs);
return fs->OpenFile(MakePath(name), exts);
}
Filesystem_Stream::InputStream FilesystemView::OpenFile(StringView dir, StringView name, const Span<const StringView> exts) const {
assert(fs);
return fs->OpenFile(MakePath(dir), name, exts);
}
Filesystem_Stream::InputStream FilesystemView::OpenFile(const DirectoryTree::Args &args) const {
assert(fs);
auto args_cp = args;
std::string path = MakePath(args.path);
args_cp.path = path;
return fs->OpenFile(args_cp);
}
std::string FilesystemView::MakePath(StringView subdir) const {
assert(fs);
return FileFinder::MakePath(sub_path, subdir);
}
bool FilesystemView::IsFile(StringView path) const {
assert(fs);
return fs->IsFile(MakePath(path));
}
bool FilesystemView::IsDirectory(StringView path, bool follow_symlinks) const {
assert(fs);
return fs->IsDirectory(MakePath(path), follow_symlinks);
}
bool FilesystemView::Exists(StringView path) const {
assert(fs);
return fs->Exists(MakePath(path));
}
int64_t FilesystemView::GetFilesize(StringView path) const {
assert(fs);
return fs->GetFilesize(MakePath(path));
}
DirectoryTree::DirectoryListType* FilesystemView::ListDirectory(StringView path) const {
assert(fs);
return fs->ListDirectory(MakePath(path));
}
Filesystem_Stream::InputStream FilesystemView::OpenInputStream(StringView name, std::ios_base::openmode m) const {
assert(fs);
if (name.empty()) {
return Filesystem_Stream::InputStream();
}
return fs->OpenInputStream(MakePath(name), m);
}
Filesystem_Stream::InputStream FilesystemView::OpenOrCreateInputStream(StringView name, std::ios_base::openmode m) const {
assert(fs);
if (name.empty()) {
return Filesystem_Stream::InputStream();
}
return fs->OpenOrCreateInputStream(MakePath(name), m);
}
Filesystem_Stream::OutputStream FilesystemView::OpenOutputStream(StringView name, std::ios_base::openmode m) const {
assert(fs);
if (name.empty()) {
return Filesystem_Stream::OutputStream();
}
return fs->OpenOutputStream(MakePath(name), m);
}
std::streambuf* FilesystemView::CreateInputStreambuffer(StringView path, std::ios_base::openmode mode) const {
assert(fs);
return fs->CreateInputStreambuffer(MakePath(path), mode);
}
std::streambuf* FilesystemView::CreateOutputStreambuffer(StringView path, std::ios_base::openmode mode) const {
assert(fs);
return fs->CreateOutputStreambuffer(MakePath(path), mode);
}
FilesystemView FilesystemView::Create(StringView p) const {
assert(fs);
return fs->Create(MakePath(p));
}
bool FilesystemView::MakeDirectory(StringView dir, bool follow_symlinks) const {
assert(fs);
return fs->MakeDirectory(MakePath(dir), follow_symlinks);
}
bool FilesystemView::IsFeatureSupported(Filesystem::Feature f) const {
assert(fs);
return fs->IsFeatureSupported(f);
}
FilesystemView FilesystemView::Subtree(StringView sub_path) const {
assert(fs);
return FilesystemView(fs, MakePath(sub_path));
}
std::string FilesystemView::Describe() const {
assert(fs);
if (GetSubPath().empty()) {
return fs->Describe();
} else {
return fs->Describe() + " -> " + GetSubPath();
}
}