-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.c
More file actions
406 lines (343 loc) · 10.4 KB
/
options.c
File metadata and controls
406 lines (343 loc) · 10.4 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
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
/*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*
* Eventually this should include all configuration stuff,
* for now there's few options which indicate 3do/pc flavors.
*/
#include "options.h"
#include "port.h"
#include "libs/graphics/gfx_common.h"
#include "file.h"
#include "config.h"
#include "libs/compiler.h"
#include "libs/uio.h"
#include "libs/strlib.h"
#include "libs/log.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <locale.h>
int optWhichMusic;
int optWhichCoarseScan;
int optWhichMenu;
int optWhichFonts;
int optWhichIntro;
int optWhichShield;
int optSmoothScroll;
int optMeleeScale;
BOOLEAN optSubtitles;
BOOLEAN optStereoSFX;
uio_DirHandle *contentDir;
uio_DirHandle *configDir;
uio_DirHandle *saveDir;
uio_DirHandle *meleeDir;
char baseContentPath[PATH_MAX];
uio_DirList *availableAddons;
extern uio_Repository *repository;
extern uio_DirHandle *rootDir;
INPUT_TEMPLATE input_templates[6];
static const char *findFileInDirs (const char *locs[], int numLocs,
const char *file);
static void mountContentDir (uio_Repository *repository,
const char *contentPath, const char **addons);
static void mountDirZips (uio_MountHandle *contentHandle,
uio_DirHandle *dirHandle);
// Looks for a file 'file' in all 'numLocs' locations from 'locs'.
// returns the first element from locs where 'file' is found.
// If there is no matching location, NULL will be returned and
// errno will be set to 'ENOENT'.
// Entries from 'locs' that together with 'file' are longer than
// PATH_MAX will be ignored, except for a warning given to stderr.
static const char *
findFileInDirs (const char *locs[], int numLocs, const char *file)
{
int locI;
char path[PATH_MAX];
size_t fileLen;
fileLen = strlen (file);
for (locI = 0; locI < numLocs; locI++)
{
size_t locLen;
const char *loc;
bool needSlash;
loc = locs[locI];
locLen = strlen (loc);
needSlash = (locLen != 0 && loc[locLen - 1] != '/');
if (locLen + (needSlash ? 1 : 0) + fileLen + 1 >= sizeof path)
{
// This dir plus the file name is too long.
log_add (log_Warning, "Warning: path '%s' is ignored"
" because it is too long.", loc);
continue;
}
snprintf (path, sizeof path, "%s%s%s", loc, needSlash ? "/" : "",
file);
if (fileExists (path))
return loc;
}
// No matching location was found.
errno = ENOENT;
return NULL;
}
void
prepareContentDir (const char *contentDirName, const char **addons)
{
const char *testFile = "version";
const char *loc;
if (contentDirName == NULL)
{
// Try the default content locations.
const char *locs[] = {
CONTENTDIR, /* defined in config.h */
""
"content",
"../../content" /* For running from MSVC */
};
loc = findFileInDirs (locs, sizeof locs / sizeof locs[0], testFile);
}
else
{
// Only use the explicitely supplied content dir.
loc = findFileInDirs (&contentDirName, 1, testFile);
}
if (loc == NULL)
{
log_add (log_Fatal, "Fatal error: Could not find content.");
exit (EXIT_FAILURE);
}
if (expandPath (baseContentPath, sizeof baseContentPath, loc, EP_ALL_SYSTEM) == -1)
{
log_add (log_Fatal, "Fatal error: Could not expand path to content "
"directory: %s", strerror (errno));
exit (EXIT_FAILURE);
}
log_add (log_Debug, "Using '%s' as base content dir.", baseContentPath);
mountContentDir (repository, baseContentPath, addons);
}
void
prepareConfigDir (const char *configDirName) {
char buf[PATH_MAX];
static uio_AutoMount *autoMount[] = { NULL };
uio_MountHandle *contentHandle;
if (configDirName == NULL)
{
configDirName = getenv("UQM_CONFIG_DIR");
if (configDirName == NULL)
configDirName = CONFIGDIR;
}
if (expandPath (buf, PATH_MAX - 13, configDirName, EP_ALL_SYSTEM)
== -1)
{
// Doesn't have to be fatal, but might mess up things when saving
// config files.
log_add (log_Fatal, "Fatal error: Invalid path to config files.");
exit (EXIT_FAILURE);
}
configDirName = buf;
log_add (log_Debug, "Using config dir '%s'", configDirName);
// Set the environment variable UQM_CONFIG_DIR so UQM_MELEE_DIR
// and UQM_SAVE_DIR can refer to it.
setenv("UQM_CONFIG_DIR", configDirName, 1);
// Create the path upto the config dir, if not already existing.
if (mkdirhier (configDirName) == -1)
exit (EXIT_FAILURE);
contentHandle = uio_mountDir (repository, "/",
uio_FSTYPE_STDIO, NULL, NULL, configDirName, autoMount,
uio_MOUNT_TOP, NULL);
if (contentHandle == NULL)
{
log_add (log_Fatal, "Fatal error: Could not mount config dir: %s",
strerror (errno));
exit (EXIT_FAILURE);
}
configDir = uio_openDir (repository, "/", 0);
if (configDir == NULL)
{
log_add (log_Fatal, "Fatal error: Could not open config dir: %s",
strerror (errno));
exit (EXIT_FAILURE);
}
}
void
prepareSaveDir (void) {
char buf[PATH_MAX];
const char *saveDirName;
saveDirName = getenv("UQM_SAVE_DIR");
if (saveDirName == NULL)
saveDirName = SAVEDIR;
if (expandPath (buf, PATH_MAX - 13, saveDirName, EP_ALL_SYSTEM) == -1)
{
// Doesn't have to be fatal, but might mess up things when saving
// config files.
log_add (log_Fatal, "Fatal error: Invalid path to config files.");
exit (EXIT_FAILURE);
}
saveDirName = buf;
setenv("UQM_SAVE_DIR", saveDirName, 1);
// Create the path upto the save dir, if not already existing.
if (mkdirhier (saveDirName) == -1)
exit (EXIT_FAILURE);
log_add (log_Debug, "Saved games are kept in %s.", saveDirName);
saveDir = uio_openDirRelative (configDir, "save", 0);
// TODO: this doesn't work if the save dir is not
// "save" in the config dir.
if (saveDir == NULL)
{
log_add (log_Fatal, "Fatal error: Could not open save dir: %s",
strerror (errno));
exit (EXIT_FAILURE);
}
}
void
prepareMeleeDir (void) {
char buf[PATH_MAX];
const char *meleeDirName;
meleeDirName = getenv("UQM_MELEE_DIR");
if (meleeDirName == NULL)
meleeDirName = MELEEDIR;
if (expandPath (buf, PATH_MAX - 13, meleeDirName, EP_ALL_SYSTEM) == -1)
{
// Doesn't have to be fatal, but might mess up things when saving
// config files.
log_add (log_Fatal, "Fatal error: Invalid path to config files.");
exit (EXIT_FAILURE);
}
meleeDirName = buf;
setenv("UQM_MELEE_DIR", meleeDirName, 1);
// Create the path upto the save dir, if not already existing.
if (mkdirhier (meleeDirName) == -1)
exit (EXIT_FAILURE);
meleeDir = uio_openDirRelative (configDir, "teams", 0);
// TODO: this doesn't work if the save dir is not
// "teams" in the config dir.
if (meleeDir == NULL)
{
log_add (log_Fatal, "Fatal error: Could not open melee teams dir: %s",
strerror (errno));
exit (EXIT_FAILURE);
}
}
static void
mountContentDir (uio_Repository *repository, const char *contentPath,
const char **addons) {
uio_MountHandle *contentHandle;
uio_DirHandle *packagesDir, *addonsDir, *addonDir;
static uio_AutoMount *autoMount[] = { NULL };
availableAddons = NULL;
contentHandle = uio_mountDir (repository, "/",
uio_FSTYPE_STDIO, NULL, NULL, contentPath, autoMount,
uio_MOUNT_TOP | uio_MOUNT_RDONLY, NULL);
if (contentHandle == NULL)
{
log_add (log_Fatal, "Fatal error: Could not mount content dir: %s",
strerror (errno));
exit (EXIT_FAILURE);
}
contentDir = uio_openDir (repository, "/", 0);
if (contentDir == NULL)
{
log_add (log_Fatal, "Fatal error: Could not open content dir: %s",
strerror (errno));
exit (EXIT_FAILURE);
}
packagesDir = uio_openDir (repository, "/packages", 0);
if (packagesDir == NULL)
{ // No packages dir means no packages to load.
if (addons[0] != NULL)
{ // addons were specified, but there's no /packages dir,
// let alone a /packages/addons dir.
log_add (log_Error, "Warning: There's no 'packages/addons' "
"directory in the 'content' directory;\n\t'--addon' "
"options are ignored.");
}
return;
}
mountDirZips (contentHandle, packagesDir);
// NB: note the difference between addonsDir and addonDir.
// the former is the dir 'packages/addons', the latter a directory
// in that dir.
addonsDir = uio_openDirRelative (packagesDir, "addons", 0);
if (addonsDir == NULL)
{ // No addon dir found.
log_add (log_Error, "Warning: There's no 'packages/addons' "
"directory in the 'content' directory;\n\t'--addon' "
"options are ignored.");
uio_closeDir (packagesDir);
return;
}
uio_closeDir (packagesDir);
availableAddons = uio_getDirList (addonsDir, "", "", match_MATCH_PREFIX);
if (availableAddons != NULL)
{
int i, count;
count = availableAddons->numNames;
if (count != 1)
{
log_add (log_Info, "%d available addon packs.", count);
}
else
{
log_add (log_Info, "1 available addon pack.");
}
for (i = 0; i < count; i++)
{
log_add (log_Info, " %d. %s", i+1,
availableAddons->names[i]);
}
}
else
{
log_add (log_Info, "0 available addon packs.");
}
for (; *addons != NULL; addons++)
{
addonDir = uio_openDirRelative (addonsDir, *addons, 0);
if (addonDir == NULL)
{
log_add (log_Error, "Warning: directory 'packages/addons/%s' "
"not found; addon skipped.", *addons);
continue;
}
mountDirZips (contentHandle, addonDir);
uio_closeDir (addonDir);
}
uio_closeDir (addonsDir);
}
static void
mountDirZips (uio_MountHandle *contentHandle, uio_DirHandle *dirHandle)
{
static uio_AutoMount *autoMount[] = { NULL };
uio_DirList *dirList;
dirList = uio_getDirList (dirHandle, "", ".(zip|uqm)$",
match_MATCH_REGEX);
if (dirList != NULL)
{
int i;
for (i = 0; i < dirList->numNames; i++)
{
if (uio_mountDir (repository, "/", uio_FSTYPE_ZIP,
dirHandle, dirList->names[i], "/", autoMount,
uio_MOUNT_BELOW | uio_MOUNT_RDONLY,
contentHandle) == NULL)
{
log_add (log_Error, "Warning: Could not mount '%s': %s.",
dirList->names[i], strerror (errno));
}
}
}
uio_DirList_free (dirList);
}