forked from electronicarts/CnC_Generals_Zero_Hour
-
Notifications
You must be signed in to change notification settings - Fork 237
Expand file tree
/
Copy pathStdLocalFileSystem.cpp
More file actions
348 lines (289 loc) · 9.93 KB
/
Copy pathStdLocalFileSystem.cpp
File metadata and controls
348 lines (289 loc) · 9.93 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
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
/*
** Command & Conquer Generals Zero Hour(tm)
** Copyright 2025 Electronic Arts Inc.
**
** 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 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 General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
////////////////////////////////////////////////////////////////////////////////
// //
// (c) 2001-2003 Electronic Arts Inc. //
// //
////////////////////////////////////////////////////////////////////////////////
///////// StdLocalFileSystem.cpp /////////////////////////
// Stephan Vedder, April 2025
////////////////////////////////////////////////////////////
#include "Common/AsciiString.h"
#include "Common/GameMemory.h"
#include "Common/PerfTimer.h"
#include "StdDevice/Common/StdLocalFileSystem.h"
#include "StdDevice/Common/StdLocalFile.h"
#include <filesystem>
StdLocalFileSystem::StdLocalFileSystem() : LocalFileSystem()
{
}
StdLocalFileSystem::~StdLocalFileSystem() {
}
//DECLARE_PERF_TIMER(StdLocalFileSystem_openFile)
static std::filesystem::path fixFilenameFromWindowsPath(const Char *filename, Int access)
{
std::string fixedFilename(filename);
#ifndef _WIN32
// Replace backslashes with forward slashes on unix
std::replace(fixedFilename.begin(), fixedFilename.end(), '\\', '/');
#endif
// Convert the filename to a std::filesystem::path and pass that
std::filesystem::path path(std::move(fixedFilename));
#ifndef _WIN32
// check if the file exists to see if fixup is required
// if it's not found try to match disregarding case sensitivity
// For cases where a write is happening, we should check if the parent path exists, if so, let it through, since the file may not exist yet.
std::error_code ec;
if (!std::filesystem::exists(path, ec) &&
((!(access & File::WRITE)) || ((access & File::WRITE) && !std::filesystem::exists(path.parent_path(), ec))))
{
// Traverse path to try and match case-insensitively
std::filesystem::path parent = path.parent_path();
std::filesystem::path pathFixed;
std::filesystem::path pathCurrent;
for (auto& p : path)
{
std::filesystem::path pathFixedPart;
if (pathCurrent.empty())
{
// Load the first part of the path
pathFixed /= p;
pathCurrent /= p;
continue;
}
if (std::filesystem::exists(pathCurrent / p, ec))
{
pathFixedPart = p;
}
else if (std::filesystem::exists(pathFixed / p, ec))
{
pathFixedPart = p;
}
else
{
// Check if the subpath exists using case-insensitive comparison
for (auto& entry : std::filesystem::directory_iterator(pathFixed, ec))
{
if (strcasecmp(entry.path().filename().string().c_str(), p.string().c_str()) == 0)
{
pathFixedPart = entry.path().filename();
break;
}
}
}
if (pathFixedPart.empty())
{
// Required to allow creation of new files
if (!(access & File::WRITE))
{
DEBUG_LOG(("StdLocalFileSystem::fixFilenameFromWindowsPath - Error finding file %s", filename.string().c_str()));
DEBUG_LOG(("StdLocalFileSystem::fixFilenameFromWindowsPath - Got so far %s", pathCurrent.string().c_str()));
return std::filesystem::path();
}
// Use the last known good path
pathFixedPart = p;
}
// Copy of the current path to mirror the current depth
pathFixed /= pathFixedPart;
pathCurrent /= p;
}
path = pathFixed;
}
#endif
return path;
}
File * StdLocalFileSystem::openFile(const Char *filename, Int access, size_t bufferSize)
{
//USE_PERF_TIMER(StdLocalFileSystem_openFile)
// sanity check
if (strlen(filename) <= 0) {
return nullptr;
}
std::filesystem::path path = fixFilenameFromWindowsPath(filename, access);
if (path.empty()) {
return nullptr;
}
if (access & File::WRITE) {
// if opening the file for writing, we need to make sure the directory is there
// before we try to create the file.
std::filesystem::path dir = path.parent_path();
std::error_code ec;
if (!std::filesystem::exists(dir, ec) || ec) {
if(!std::filesystem::create_directories(dir, ec) || ec) {
DEBUG_LOG(("StdLocalFileSystem::openFile - Error creating directory %s", dir.string().c_str()));
return nullptr;
}
}
}
StdLocalFile *file = newInstance( StdLocalFile );
if (file->open(path.string().c_str(), access, bufferSize) == FALSE) {
deleteInstance(file);
file = nullptr;
} else {
file->deleteOnClose();
}
// this will also need to play nice with the STREAMING type that I added, if we ever enable this
// srj sez: this speeds up INI loading, but makes BIG files unusable.
// don't enable it without further tweaking.
//
// unless you like running really slowly.
// if (!(access&File::WRITE)) {
// // Return a ramfile.
// RAMFile *ramFile = newInstance( RAMFile );
// if (ramFile->open(file)) {
// file->close(); // is deleteonclose, so should delete.
// ramFile->deleteOnClose();
// return ramFile;
// } else {
// ramFile->close();
// deleteInstance(ramFile);
// }
// }
return file;
}
void StdLocalFileSystem::update()
{
}
void StdLocalFileSystem::init()
{
}
void StdLocalFileSystem::reset()
{
}
//DECLARE_PERF_TIMER(StdLocalFileSystem_doesFileExist)
Bool StdLocalFileSystem::doesFileExist(const Char *filename) const
{
std::filesystem::path path = fixFilenameFromWindowsPath(filename, 0);
if(path.empty()) {
return FALSE;
}
std::error_code ec;
return std::filesystem::exists(path, ec);
}
void StdLocalFileSystem::getFileListInDirectory(const AsciiString& currentDirectory, const AsciiString& originalDirectory, const AsciiString& searchName, FilenameList & filenameList, Bool searchSubdirectories) const
{
AsciiString asciisearch;
asciisearch = originalDirectory;
asciisearch.concat(currentDirectory);
auto searchExt = std::filesystem::path(searchName.str()).extension();
if (asciisearch.isEmpty()) {
asciisearch = ".";
}
std::string fixedDirectory(asciisearch.str());
#ifndef _WIN32
// Replace backslashes with forward slashes on unix
std::replace(fixedDirectory.begin(), fixedDirectory.end(), '\\', '/');
#endif
Bool done = FALSE;
std::error_code ec;
auto iter = std::filesystem::directory_iterator(fixedDirectory.c_str(), ec);
// The default iterator constructor creates an end iterator
done = iter == std::filesystem::directory_iterator();
if (ec) {
DEBUG_LOG(("StdLocalFileSystem::getFileListInDirectory - Error opening directory %s", fixedDirectory.c_str()));
return;
}
while (!done) {
std::string filenameStr = iter->path().filename().string();
if (!iter->is_directory() && iter->path().extension() == searchExt &&
(strcmp(filenameStr.c_str(), ".") != 0 && strcmp(filenameStr.c_str(), "..") != 0)) {
// if we haven't already, add this filename to the list.
// a stl set should only allow one copy of each filename
AsciiString newFilename = iter->path().string().c_str();
if (filenameList.find(newFilename) == filenameList.end()) {
filenameList.insert(newFilename);
}
}
iter++;
done = iter == std::filesystem::directory_iterator();
}
if (searchSubdirectories) {
auto iter = std::filesystem::directory_iterator(fixedDirectory, ec);
if (ec) {
DEBUG_LOG(("StdLocalFileSystem::getFileListInDirectory - Error opening subdirectory %s", fixedDirectory.c_str()));
return;
}
// The default iterator constructor creates an end iterator
done = iter == std::filesystem::directory_iterator();
while (!done) {
std::string filenameStr = iter->path().filename().string();
if(iter->is_directory() &&
(strcmp(filenameStr.c_str(), ".") != 0 && strcmp(filenameStr.c_str(), "..") != 0)) {
AsciiString tempsearchstr(filenameStr.c_str());
// recursively add files in subdirectories if required.
getFileListInDirectory(tempsearchstr, originalDirectory, searchName, filenameList, searchSubdirectories);
}
iter++;
done = iter == std::filesystem::directory_iterator();
}
}
}
Bool StdLocalFileSystem::getFileInfo(const AsciiString& filename, FileInfo *fileInfo) const
{
std::filesystem::path path = fixFilenameFromWindowsPath(filename.str(), 0);
if(path.empty()) {
return FALSE;
}
std::error_code ec;
auto file_size = std::filesystem::file_size(path, ec);
if (ec)
{
return FALSE;
}
auto write_time = std::filesystem::last_write_time(path, ec);
if (ec)
{
return FALSE;
}
// TODO: fix this to be win compatible (time since 1601)
auto time = write_time.time_since_epoch().count();
fileInfo->timestampHigh = time >> 32;
fileInfo->timestampLow = time & UINT32_MAX;
fileInfo->sizeHigh = file_size >> 32;
fileInfo->sizeLow = file_size & UINT32_MAX;
return TRUE;
}
Bool StdLocalFileSystem::createDirectory(AsciiString directory)
{
bool result = FALSE;
std::string fixedDirectory(directory.str());
#ifndef _WIN32
// Replace backslashes with forward slashes on unix
std::replace(fixedDirectory.begin(), fixedDirectory.end(), '\\', '/');
#endif
if ((!fixedDirectory.empty()) && (fixedDirectory.length() < _MAX_DIR)) {
// Convert to host path
std::filesystem::path path(std::move(fixedDirectory));
std::error_code ec;
result = std::filesystem::create_directory(path, ec);
if (ec) {
result = FALSE;
}
}
return result;
}
AsciiString StdLocalFileSystem::normalizePath(const AsciiString& filePath) const
{
std::string nonNormalized(filePath.str());
#ifndef _WIN32
// Replace backslashes with forward slashes on non-Windows platforms
std::replace(unNormalized.begin(), unNormalized.end(), '\\', '/');
#endif
std::filesystem::path pathNonNormalized(nonNormalized);
return AsciiString(pathNonNormalized.lexically_normal().string().c_str());
}