forked from EasyRPG/Player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilefinder.h
359 lines (316 loc) · 9.9 KB
/
filefinder.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
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
/*
* 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/>.
*/
#ifndef EP_FILEFINDER_H
#define EP_FILEFINDER_H
// Headers
#include "system.h"
#include "filesystem_stream.h"
#include "filesystem.h"
#include "string_view.h"
#include "directory_tree.h"
#include <string>
#include <cstdio>
#include <ios>
#include <istream>
#include <unordered_map>
#include <vector>
/**
* FileFinder contains helper methods for finding case
* insensitive files paths.
*/
namespace FileFinder {
/**
* Quits FileFinder.
*/
void Quit();
/** @return A filesystem handle for arbitrary file access inside the host filesystem */
FilesystemView Root();
/** @return A filesystem handle for file access inside the game directory */
FilesystemView Game();
/**
* Sets the game filesystem.
*
* @param filesystem Game filesystem to use.
*/
void SetGameFilesystem(FilesystemView filesystem);
/**
* A filesystem handle for file access inside the save directory.
* Do not expect that this is the same as the game filesystem.
* The path is based on Main_Data::SavePath but could be redirected when
* the path is not writable because the filesystem does not support it.
*
* @return Save filesystem handle
*/
FilesystemView Save();
/**
* Sets the save filesystem.
*
* @param filesystem Save filesystem to use.
*/
void SetSaveFilesystem(FilesystemView filesystem);
/**
* Finds an image file in the current RPG Maker game.
*
* @param dir directory to check.
* @param name image file name to check.
* @return path to file.
*/
std::string FindImage(StringView dir, StringView name);
/**
* Finds a music file in the current RPG Maker game.
*
* @param name the music path and name.
* @return path to file.
*/
std::string FindMusic(StringView name);
/**
* Finds a sound file in the current RPG Maker game.
*
* @param name the sound path and name.
* @return path to file.
*/
std::string FindSound(StringView name);
/**
* Finds a font file.
* Searches through the current RPG Maker game and the RTP directories.
*
* @param name the font name.
* @return path to file.
*/
std::string FindFont(StringView name);
/**
* Finds a text file in the current RPG Maker game.
*
* @param name the text path and name.
* @return path to file.
*/
std::string FindText(StringView name);
/**
* Finds an image file and opens a file handle to it.
* Searches through the current RPG Maker game and the RTP directories.
*
* @param dir directory to check.
* @param name image file name to check.
* @return read handle on success or invalid handle if not found
*/
Filesystem_Stream::InputStream OpenImage(StringView dir, StringView name);
/**
* Finds a music file and opens a file handle to it.
* Searches through the Music folder of the current RPG Maker game and
* the RTP directories.
*
* @param name the music path and name.
* @return read handle on success or invalid handle if not found
*/
Filesystem_Stream::InputStream OpenMusic(StringView name);
/**
* Finds a sound file and opens a file handle to it.
* Searches through the Sound folder of the current RPG Maker game and
* the RTP directories.
*
* @param name the sound path and name.
* @return read handle on success or invalid handle if not found
*/
Filesystem_Stream::InputStream OpenSound(StringView name);
/**
* Finds a font file and opens a file handle to it.
* Searches through the Font folder of the current RPG Maker game.
*
* @param name the font path and name.
* @return read handle on success or invalid handle if not found
*/
Filesystem_Stream::InputStream OpenFont(StringView name);
/**
* Finds a text file and opens a file handle to it.
* Searches through the Text folder of the current RPG Maker game.
* Will also search through the directory save files are written to as a fallback,
* as it needs to account for files written by the game as well.
*
* @param name the text path and name.
* @return read handle on success or invalid handle if not found
*/
Filesystem_Stream::InputStream OpenText(StringView name);
/**
* Writes data to a txt file.
* If the file exists, it will be overwritten.
*
* @param name the text file path and name
* @param data the content of the text file to be written
*/
void WriteText(StringView name, StringView data);
/**
* Appends name to directory.
*
* @param dir base directory.
* @param name file name to be appended to dir.
* @return combined path
*/
std::string MakePath(StringView dir, StringView name);
/**
* Creates a path out of multiple components
*
* @param path Path to join
*/
template<typename T>
std::string MakePath(lcf::Span<T> components);
/**
* Converts a path to the canonical equivalent.
* This generates a path that does not contain ".." or "." directories.
*
* @param path Path to normalize
* @param initial_deepness How deep the passed path is relative to the game root
* @return canonical path
*/
std::string MakeCanonical(StringView path, int initial_deepness = -1);
/**
* Splits a path in it's components.
*
* @param path Path to split
* @return Vector containing path components
*/
std::vector<std::string> SplitPath(StringView path);
/**
* Splits a path into path and filename.
*
* @param path Path to split
* @return Pair containing dir and name
*/
std::pair<std::string, std::string> GetPathAndFilename(StringView path);
/**
* Converts all path delimiters to the platform delimiters.
* \ on Windows, / on others.
*
* @param path to convert
*/
void ConvertPathDelimiters(std::string& path);
/**
* Returns the part of "path_in" that is inside "path_to".
* e.g. Input: /h/e/game, /h/e/game/Music/a.wav; Output: Music/a.wav
*
* @param path_to Path to a primary folder of path_in
* @param path_in Absolute path to the file, must start with path_to
*
* @return The part of path_in that is inside path_to. path_in when the path is not in path_to
*/
std::string GetPathInsidePath(StringView path_to, StringView path_in);
/**
* Return the part of "path_in" that is inside the current games directory.
*
* @see GetPathInsidePath
* @param path_in An absolute path inside the game directory
* @return The part of path_in that is inside the game directory, path_in when it's not in the directory
*/
std::string GetPathInsideGamePath(StringView path_in);
/**
* Checks whether a passed path ends with a supported extension for an archive, e.g. ".zip"
*
* @param path path to check
* @return true when the path ends on an archive extension
*/
bool IsSupportedArchiveExtension(std::string path);
/**
* @param p fs Tree to check
* @return Whether the tree contains a valid RPG2k(3) or EasyRPG project
*/
bool IsValidProject(const FilesystemView& fs);
/**
* @param p fs Tree to check
* @return Whether the tree contains a valid RPG2k(3) project
*/
bool IsRPG2kProject(const FilesystemView& fs);
/**
* @param p fs Tree to check
* @return Whether the tree contains a valid EasyRPG project
*/
bool IsEasyRpgProject(const FilesystemView& fs);
/**
* Determines if the directory in question represents an RPG2k project with non-standard
* database, map tree, or map file names.
*
* @param fs The directory tree in question
* @return true if this is likely an RPG2k project; false otherwise
*/
bool IsRPG2kProjectWithRenames(const FilesystemView& fs);
/**
* Determines if the directory contains a single file/directory ending in ".easyrpg" for use in the
* autostart feature.
*
* @param fs The directory tree to check. Is replaced with a view to the game for autorun.
* @return true when autorun is possible, fs contains the new view; when false fs is not modified
*/
bool OpenViewToEasyRpgFile(FilesystemView& fs);
/**
* Checks whether the save directory contains any savegame with name
* SaveXX.lsd (XX from 00 to 15).
*
* @return If directory tree contains a savegame
*/
bool HasSavegame();
/** @returns Amount of savegames in the save directory */
int GetSavegames();
/**
* Known file sizes
*/
enum KnownFileSize {
OFFICIAL_HARMONY_DLL = 473600,
};
/**
* Checks whether the game is created with RPG2k >= 1.50 or RPG2k3 >= 1.05.
*
* @return true if RPG2k >= 1.50 or RPG2k3 >= 1.05, otherwise false.
*/
bool IsMajorUpdatedTree();
/** RPG_RT.exe file size thresholds
*
* 2k v1.51 (Japanese) : 746496
* 2k v1.50 (Japanese) : 745984
* -- threshold (2k) -- : 735000
* 2k v1.10 (Japanese) : 726016
*
* 2k3 v1.09a (Japanese) : 950784
* 2k3 v1.06 (Japanese) : 949248
* 2k3 v1.05 (Japanese) : unknown
* -- threshold (2k3) -- : 927000
* 2k3 v1.04 (Japanese) : 913408
*/
enum RpgrtMajorUpdateThreshold {
RPG2K = 735000,
RPG2K3 = 927000,
};
/**
* Utility function for getting a complete filesystem path.
*
* @param fs Filesystem to use
* @return complete path
*/
std::string GetFullFilesystemPath(FilesystemView fs);
/**
* Utility function for debug printing of a filesystem path.
*
* @param fs Filesystem to use
*/
void DumpFilesystem(FilesystemView fs);
} // namespace FileFinder
template<typename T>
std::string FileFinder::MakePath(lcf::Span<T> components) {
std::string path;
for (auto& c: components) {
path = MakePath(path, c);
}
return path;
}
#endif