/
initfs.cpp
411 lines (372 loc) · 11.9 KB
/
initfs.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
** initfs.cpp
**
**---------------------------------------------------------------------------
** Copyright 1999-2016 Randy Heit
** Copyright 2002-2019 Christoph Oelckers
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
**
** 1. Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
** 3. The name of the author may not be used to endorse or promote products
** derived from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**---------------------------------------------------------------------------
**
**
*/
#include "filesystem.h"
#include "cmdlib.h"
#include "zstring.h"
#include "gamecontrol.h"
#include "gameconfigfile.h"
#include "printf.h"
#include "m_argv.h"
#include "version.h"
#include "sc_man.h"
#include "v_video.h"
#include "v_text.h"
#include "findfile.h"
#include "palutil.h"
#include "startupinfo.h"
#ifndef PATH_MAX
#define PATH_MAX 260
#endif
static const char* validexts[] = { "*.grp", "*.zip", "*.pk3", "*.pk4", "*.7z", "*.pk7", "*.dat", "*.rff" };
//==========================================================================
//
//
//
//==========================================================================
static FString ParseGameInfo(TArray<FString>& pwads, const char* fn, const char* data, int size)
{
FScanner sc;
FString iwad;
int pos = 0;
const char* lastSlash = strrchr(fn, '/');
sc.OpenMem("GAMEINFO", data, size);
sc.SetCMode(true);
while (sc.GetToken())
{
sc.TokenMustBe(TK_Identifier);
FString nextKey = sc.String;
sc.MustGetToken('=');
if (!nextKey.CompareNoCase("GAME"))
{
sc.MustGetString();
iwad = sc.String;
}
else if (!nextKey.CompareNoCase("LOAD"))
{
do
{
sc.MustGetString();
// Try looking for the wad in the same directory as the .wad
// before looking for it in the current directory.
FString checkpath;
if (lastSlash != NULL)
{
checkpath = FString(fn, (lastSlash - fn) + 1);
checkpath += sc.String;
}
else
{
checkpath = sc.String;
}
if (!FileExists(checkpath))
{
pos += D_AddFile(pwads, sc.String, true, pos, GameConfig);
}
else
{
pos += D_AddFile(pwads, checkpath, true, pos, GameConfig);
}
} while (sc.CheckToken(','));
}
else if (!nextKey.CompareNoCase("STARTUPTITLE"))
{
sc.MustGetString();
GameStartupInfo.Name = sc.String;
}
else if (!nextKey.CompareNoCase("STARTUPCOLORS"))
{
sc.MustGetString();
GameStartupInfo.FgColor = V_GetColor(NULL, sc);
sc.MustGetStringName(",");
sc.MustGetString();
GameStartupInfo.BkColor = V_GetColor(NULL, sc);
}
else if (!nextKey.CompareNoCase("MODERN"))
{
sc.MustGetNumber();
GameStartupInfo.modern = sc.Number ? 1 : -1;
}
else
{
// Silently ignore unknown properties
do
{
sc.MustGetAnyToken();
} while (sc.CheckToken(','));
}
}
return iwad;
}
//==========================================================================
//
//
//
//==========================================================================
static FString CheckGameInfo(TArray<FString>& pwads)
{
// scan the list of WADs backwards to find the last one that contains a GAMEINFO lump
for (int i = pwads.Size() - 1; i >= 0; i--)
{
bool isdir = false;
FResourceFile* resfile;
const char* filename = pwads[i];
// Does this exist? If so, is it a directory?
if (!DirEntryExists(pwads[i], &isdir))
{
Printf(TEXTCOLOR_RED "Could not find %s\n", filename);
continue;
}
if (!isdir)
{
FileReader fr;
if (!fr.OpenFile(filename))
{
// Didn't find file
continue;
}
resfile = FResourceFile::OpenResourceFile(filename, fr, true);
}
else
resfile = FResourceFile::OpenDirectory(filename, true);
FName gameinfo = "GAMEINFO.TXT";
if (resfile != NULL)
{
uint32_t cnt = resfile->LumpCount();
for (int i = cnt - 1; i >= 0; i--)
{
FResourceLump* lmp = resfile->GetLump(i);
if (FName(lmp->getName(), true) == gameinfo)
{
// Found one!
FString iwad = ParseGameInfo(pwads, resfile->FileName, (const char*)lmp->Lock(), lmp->LumpSize);
delete resfile;
return iwad;
}
}
delete resfile;
}
}
return "";
}
//==========================================================================
//
//
//
//==========================================================================
FString GetGameFronUserFiles()
{
TArray<FString> Files;
if (userConfig.AddFilesPre) for (auto& file : *userConfig.AddFilesPre)
{
D_AddFile(Files, file, true, -1, GameConfig);
}
if (userConfig.AddFiles)
{
for (auto& file : *userConfig.AddFiles)
{
D_AddFile(Files, file, true, -1, GameConfig);
}
// Finally, if the last entry in the chain is a directory, it's being considered the mod directory, and all GRPs inside need to be loaded, too.
if (userConfig.AddFiles->NumArgs() > 0)
{
auto fn = (*userConfig.AddFiles)[userConfig.AddFiles->NumArgs() - 1];
bool isdir = false;
if (DirEntryExists(fn, &isdir) && isdir)
{
// Insert the GRPs before this entry itself.
FString lastfn;
Files.Pop(lastfn);
for (auto ext : validexts)
{
D_AddDirectory(Files, fn, ext, GameConfig);
}
Files.Push(lastfn);
}
}
}
return CheckGameInfo(Files);
}
//==========================================================================
//
// Deletes unwanted content from the main game files
//
//==========================================================================
static void DeleteStuff(FileSystem &fileSystem, const TArray<FString>& deletelumps, int numgamefiles)
{
// This must account for the game directory being inserted at index 2.
// Deletion may only occur in the main game file, the directory and the add-on, there are no secondary dependencies, i.e. more than two game files.
numgamefiles++;
for (auto str : deletelumps)
{
FString renameTo;
auto ndx = str.IndexOf("*");
if (ndx >= 0)
{
renameTo = FName(str.Mid(ndx + 1)).GetChars();
str.Truncate(ndx);
}
for (uint32_t i = 0; i < fileSystem.GetNumEntries(); i++)
{
int cf = fileSystem.GetFileContainer(i);
auto fname = fileSystem.GetFileFullName(i, false);
if (cf >= 1 && cf <= numgamefiles && !str.CompareNoCase(fname))
{
fileSystem.RenameFile(i, renameTo);
}
}
}
}
//==========================================================================
//
//
//
//==========================================================================
const char* iwad_folders[13] = { "textures/", "hires/", "sounds/", "music/", "maps/" };
const char* iwad_reserved_duke[12] = { ".map", ".con", "menudef", "gldefs", "zscript", "maps/", nullptr };
const char* iwad_reserved_blood[12] = { ".map", ".ini", "menudef", "gldefs", "zscript", "maps/", nullptr };
const char* iwad_reserved_sw[12] = { ".map", "swcustom.txt", "menudef", "gldefs", "zscript", "maps/", nullptr };
const char* iwad_reserved_ex[12] = { ".map", "menudef", "gldefs", "zscript", "maps/", nullptr };
const char** iwad_reserved()
{
return (g_gameType & GAMEFLAG_PSEXHUMED) ? iwad_reserved_ex :
(g_gameType & GAMEFLAG_SW) ? iwad_reserved_sw :
(g_gameType & GAMEFLAG_BLOOD) ? iwad_reserved_blood : iwad_reserved_duke;
}
void InitFileSystem(TArray<GrpEntry>& groups)
{
TArray<int> dependencies;
TArray<FString> Files;
// First comes the engine's own stuff.
FString baseres = progdir + ENGINERES_FILE;
D_AddFile(Files, baseres, true, -1, GameConfig);
bool insertdirectoriesafter = Args->CheckParm("-insertdirafter");
int i = groups.Size()-1;
FString fn;
for (auto &grp : groups)
{
// Add all dependencies, plus the directory of the base dependency.
// Directories of addon content are not added if they differ from the main directory.
// Also, the directory is inserted after the base dependency, allowing the addons to override directory content.
// This can be overridden via command line switch if needed.
if (!grp.FileInfo.loaddirectory && grp.FileName.IsNotEmpty())
{
D_AddFile(Files, grp.FileName, true, -1, GameConfig);
fn = ExtractFilePath(grp.FileName);
if (fn.Len() > 0 && fn.Back() != '/') fn += '/';
}
for (auto& fname : grp.FileInfo.loadfiles)
{
FString newname = fn + fname;
D_AddFile(Files, newname, true, -1, GameConfig);
}
bool insert = (!insertdirectoriesafter && &grp == &groups[0]) || (insertdirectoriesafter && &grp == &groups.Last());
// Add the game's main directory in the proper spot.
if (insert)
{
// Build's original 'file system' loads all GRPs before the first external directory.
// Do this only if explicitly requested because this severely limits the usability of GRP files.
if (insertdirectoriesafter && userConfig.AddFilesPre) for (auto& file : *userConfig.AddFilesPre)
{
D_AddFile(Files, file, true, -1, GameConfig);
}
D_AddFile(Files, fn, true, -1, GameConfig);
}
i--;
}
fileSystem.SetMaxIwadNum(Files.Size() - 1);
D_AddConfigFiles(Files, "Global.Autoload", "*.grp", GameConfig);
long len;
int lastpos = 0;
while (lastpos < LumpFilter.Len() && (len = strcspn(LumpFilter.GetChars() + lastpos, ".")) > 0)
{
auto file = LumpFilter.Left(len + lastpos) + ".Autoload";
D_AddConfigFiles(Files, file, "*.grp", GameConfig);
lastpos += len + 1;
}
if (!insertdirectoriesafter && userConfig.AddFilesPre) for (auto& file : *userConfig.AddFilesPre)
{
D_AddFile(Files, file, true, -1, GameConfig);
}
if (userConfig.AddFiles)
{
for (auto& file : *userConfig.AddFiles)
{
D_AddFile(Files, file, true, -1, GameConfig);
}
// Finally, if the last entry in the chain is a directory, it's being considered the mod directory, and all GRPs inside need to be loaded, too.
if (userConfig.AddFiles->NumArgs() > 0)
{
auto fn = (*userConfig.AddFiles)[userConfig.AddFiles->NumArgs() - 1];
bool isdir = false;
if (DirEntryExists(fn, &isdir) && isdir)
{
// Insert the GRPs before this entry itself.
FString lastfn;
Files.Pop(lastfn);
for (auto ext : validexts)
{
D_AddDirectory(Files, fn, ext, GameConfig);
}
Files.Push(lastfn);
}
}
}
TArray<FString> todelete;
for (auto& g : groups)
{
todelete.Append(g.FileInfo.tobedeleted);
}
todelete.Append(userConfig.toBeDeleted);
LumpFilterInfo lfi;
for (auto p : iwad_folders) lfi.reservedFolders.Push(p);
for (auto p = iwad_reserved(); *p; p++) lfi.requiredPrefixes.Push(*p);
lfi.dotFilter = LumpFilter;
if (g_gameType & (GAMEFLAG_DUKE | GAMEFLAG_NAM | GAMEFLAG_NAPALM | GAMEFLAG_WW2GI | GAMEFLAG_RRALL)) lfi.gameTypeFilter.Push("DukeEngine");
lfi.postprocessFunc = [&]()
{
DeleteStuff(fileSystem, todelete, groups.Size());
};
fileSystem.InitMultipleFiles(Files, false, &lfi);
if (Args->CheckParm("-dumpfs"))
{
FILE* f = fopen("filesystem.dir", "wb");
for (int i = 0; i < fileSystem.GetNumEntries(); i++)
{
auto fd = fileSystem.GetFileAt(i);
fprintf(f, "%.50s %60s %d\n", fd->getName(), fileSystem.GetResourceFileFullName(fileSystem.GetFileContainer(i)), fd->Size());
}
fclose(f);
}
}