Debian Codegraph @

similar/misc/args.cpp

raw
1 /*
2 * This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>.
3 * It is copyright by its individual contributors, as recorded in the
4 * project's Git history. See COPYING.txt at the top level for license
5 * terms and a link to the Git history.
6 */
7
8 /*
9 *
10 * Functions for accessing arguments.
11 *
12 */
13
14 #include <string>
15 #include <vector>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <SDL_stdinc.h>
19 #include "physfsx.h"
20 #include "args.h"
21 #include "u_mem.h"
22 #include "strutil.h"
23 #include "digi.h"
24 #include "game.h"
25 #include "console.h"
26 #include "mission.h"
27 #if DXX_USE_SDLMIXER
28 #include "digi_mixer.h"
29 #endif
30 #if DXX_USE_UDP
31 #include "net_udp.h"
32 #endif
33
34 #include "partial_range.h"
35
36 namespace dcx {
37 CArg CGameArg;
38
39 namespace {
40
41 constexpr std::integral_constant<std::size_t, 1000> MAX_ARGS{};
42 typedef std::vector<std::string> Arglist;
43
44 class ini_entry
45 {
46 public:
47 const std::string filename;
48 ini_entry(std::string &&f) :
49 filename(std::move(f))
50 {
51 }
52 };
53
54 typedef std::vector<ini_entry> Inilist;
55
56 class argument_exception
57 {
58 public:
59 const std::string arg;
60 argument_exception(std::string &&a) :
61 arg(std::move(a))
62 {
63 }
64 };
65
66 class missing_parameter : public argument_exception
67 {
68 public:
69 using argument_exception::argument_exception;
70 };
71
72 class unhandled_argument : public argument_exception
73 {
74 public:
75 using argument_exception::argument_exception;
76 };
77
78 class conversion_failure : public argument_exception
79 {
80 public:
81 const std::string value;
82 conversion_failure(std::string &&a, std::string &&v) :
83 argument_exception(std::move(a)), value(std::move(v))
84 {
85 }
86 };
87
88 class nesting_depth_exceeded
89 {
90 };
91
92 [[nodiscard]]
93 static Arglist BuildArglistFromIni(const char *filename)
94 {
95 Arglist Args;
96 if (auto f = PHYSFSX_openReadBuffered(filename).first)
97 {
98 for (PHYSFSX_gets_line_t<1024> line; Args.size() < MAX_ARGS && PHYSFSX_fgets(line, f);)
99 {
100 auto &separator = " \t";
101 for(char *token = strtok(line, separator); token != NULL; token = strtok(NULL, separator))
102 {
103 if (*token == ';')
104 break;
105 Args.push_back(token);
106 }
107 }
108 }
109 return Args;
110 }
111
112 static void advance_arg_iterator(Arglist::iterator &pp, Arglist::const_iterator end)
113 {
114 auto arg{pp};
115 if (++pp == end)
116 throw missing_parameter(std::move(*arg));
117 }
118
119 static std::string &&arg_string(Arglist::iterator &pp, Arglist::const_iterator end)
120 {
121 advance_arg_iterator(pp, std::move(end));
122 return std::move(*pp);
123 }
124
125 static long arg_integer(Arglist::iterator &pp, Arglist::const_iterator end)
126 {
127 const auto arg{pp};
128 advance_arg_iterator(pp, std::move(end));
129 auto &&value{std::move(*pp)};
130 char *p;
131 const auto i{strtol(value.c_str(), &p, 10)};
132 if (*p)
133 throw conversion_failure(std::move(*arg), std::move(value));
134 return i;
135 }
136
137 template<typename E> E arg_enum(Arglist::iterator &pp, Arglist::const_iterator end)
138 {
139 return static_cast<E>(arg_integer(pp, std::move(end)));
140 }
141
142 #if DXX_USE_UDP
143 static void arg_port_number(Arglist::iterator &pp, Arglist::const_iterator end, uint16_t &out, bool allow_privileged)
144 {
145 auto port = arg_integer(pp, end);
146 if (static_cast<uint16_t>(port) == port && (allow_privileged || port >= 1024))
147 out = port;
148 }
149 #endif
150
151 static void InitGameArg()
152 {
153 CGameArg.SysMaxFPS = MAXIMUM_FPS;
154 CGameArg.SysRenderZoomAdjustment = 0;
155 #if DXX_USE_UDP
156 CGameArg.MplUdpHostAddr = UDP_MANUAL_ADDR_DEFAULT;
157 #if DXX_USE_TRACKER
158 CGameArg.MplTrackerAddr = TRACKER_ADDR_DEFAULT;
159 CGameArg.MplTrackerPort = TRACKER_PORT_DEFAULT;
160 #endif
161 #endif
162 CGameArg.DbgVerbose = CON_NORMAL;
163 CGameArg.DbgBpp = 32;
164 #if DXX_USE_OGL
165 CGameArg.OglSyncMethod = OGL_SYNC_METHOD_DEFAULT;
166 CGameArg.OglSyncWait = OGL_SYNC_WAIT_DEFAULT;
167 #if DXX_USE_STEREOSCOPIC_RENDER
168 CGameArg.OglStereo = false;
169 #endif
170 CGameArg.DbgGlIntensity4Ok = true;
171 CGameArg.DbgGlLuminance4Alpha4Ok = true;
172 CGameArg.DbgGlRGBA2Ok = true;
173 CGameArg.DbgGlReadPixelsOk = true;
174 CGameArg.DbgGlGetTexLevelParamOk = true;
175 #endif
176 }
177
178 }
179
180 }
181
182 namespace dsx {
183
184 Arg GameArg;
185
186 namespace {
187
188 static void InitGameArg()
189 {
190 #if DXX_BUILD_DESCENT == 2
191 GameArg.SndDigiSampleRate = sound_sample_rate::_22k;
192 #endif
193 ::dcx::InitGameArg();
194 }
195
196 static void ReadCmdArgs(Inilist &ini, Arglist &&Args);
197
198 static void ReadIniArgs(Inilist &ini)
199 {
200 ReadCmdArgs(ini, BuildArglistFromIni(ini.back().filename.c_str()));
201 ini.pop_back();
202 }
203
204 static void ReadCmdArgs(Inilist &ini, Arglist &&Args)
205 {
206 for (Arglist::iterator pp = Args.begin(), end = Args.end(); pp != end; ++pp)
207 {
208 const char *p = pp->c_str();
209 // System Options
210
211 if (!d_stricmp(p, "-help") || !d_stricmp(p, "-h") || !d_stricmp(p, "-?") || !d_stricmp(p, "?"))
212 CGameArg.SysShowCmdHelp = true;
213 else if (!d_stricmp(p, "-nonicefps"))
214 CGameArg.SysNoNiceFPS = true;
215 else if (!d_stricmp(p, "-maxfps"))
216 CGameArg.SysMaxFPS = arg_integer(pp, end);
217 else if (!d_stricmp(p, "-render-zoom"))
218 CGameArg.SysRenderZoomAdjustment = arg_integer(pp, end);
219 else if (!d_stricmp(p, "-hogdir"))
220 CGameArg.SysHogDir = arg_string(pp, end);
221 #if PHYSFS_VER_MAJOR >= 2
222 else if (!d_stricmp(p, "-add-missions-dir"))
223 CGameArg.SysMissionDir = arg_string(pp, end);
224 #endif
225 else if (!d_stricmp(p, "-nohogdir"))
226 {
227 /* No effect if no DXX_SHAREPATH. Ignore it so that players can
228 * pass it via a cross-platform ini.
229 */
230 #if DXX_USE_SHAREPATH
231 CGameArg.SysNoHogDir = true;
232 #endif
233 }
234 else if (!d_stricmp(p, "-use_players_dir"))
235 CGameArg.SysUsePlayersDir = static_cast<int8_t>(- (sizeof(PLAYER_DIRECTORY_TEXT) - 1));
236 else if (!d_stricmp(p, "-lowmem"))
237 CGameArg.SysLowMem = true;
238 else if (!d_stricmp(p, "-pilot"))
239 CGameArg.SysPilot = arg_string(pp, end);
240 else if (!d_stricmp(p, "-record-demo-format"))
241 CGameArg.SysRecordDemoNameTemplate = arg_string(pp, end);
242 else if (!d_stricmp(p, "-auto-record-demo"))
243 CGameArg.SysAutoRecordDemo = true;
244 else if (!d_stricmp(p, "-window"))
245 CGameArg.SysWindow = true;
246 else if (!d_stricmp(p, "-noborders"))
247 CGameArg.SysNoBorders = true;
248 else if (!d_stricmp(p, "-notitles"))
249 CGameArg.SysNoTitles = true;
250 #if DXX_BUILD_DESCENT == 2
251 else if (!d_stricmp(p, "-nomovies"))
252 GameArg.SysNoMovies = 1;
253 #endif
254 else if (!d_stricmp(p, "-autodemo"))
255 CGameArg.SysAutoDemo = true;
256
257 // Control Options
258
259 else if (!d_stricmp(p, "-nocursor"))
260 CGameArg.CtlNoCursor = true;
261 else if (!d_stricmp(p, "-nomouse"))
262 CGameArg.CtlNoMouse = true;
263 else if (!d_stricmp(p, "-nojoystick"))
264 {
265 #if DXX_MAX_JOYSTICKS
266 CGameArg.CtlNoJoystick = 1;
267 #endif
268 }
269 else if (!d_stricmp(p, "-nostickykeys"))
270 CGameArg.CtlNoStickyKeys = true;
271
272 // Sound Options
273
274 else if (!d_stricmp(p, "-nosound"))
275 CGameArg.SndNoSound = 1;
276 else if (!d_stricmp(p, "-nomusic"))
277 CGameArg.SndNoMusic = true;
278 #if DXX_BUILD_DESCENT == 2
279 else if (!d_stricmp(p, "-sound11k"))
280 GameArg.SndDigiSampleRate = sound_sample_rate::_11k;
281 #endif
282 else if (!d_stricmp(p, "-nosdlmixer"))
283 {
284 #if DXX_USE_SDLMIXER
285 CGameArg.SndDisableSdlMixer = true;
286 #endif
287 }
288 else if (!strncmp(p, "-sdlmixer-resampler=", 20))
289 {
290 #if DXX_USE_SDLMIXER
291 #if DXX_FEATURE_EXTERNAL_RESAMPLER_SDL_NATIVE
292 if (!strcmp(&p[20], "sdl-native"))
293 CGameArg.SndMixerMethod = digi_mixer_method::sdl_native;
294 else
295 #endif
296 #if DXX_FEATURE_INTERNAL_RESAMPLER_EMULATE_SDL1
297 if (!strcmp(&p[20], "emulate-sdl1"))
298 CGameArg.SndMixerMethod = digi_mixer_method::emulate_sdl1;
299 else
300 #endif
301 #if DXX_FEATURE_INTERNAL_RESAMPLER_EMULATE_SOUNDBLASTER16
302 if (!strcmp(&p[20], "emulate-soundblaster16"))
303 CGameArg.SndMixerMethod = digi_mixer_method::emulate_soundblaster16;
304 else
305 #endif
306 throw unhandled_argument(std::move(*pp));
307 #endif
308 }
309
310 // Graphics Options
311
312 else if (!d_stricmp(p, "-lowresfont"))
313 CGameArg.GfxSkipHiresFNT = true;
314 #if DXX_BUILD_DESCENT == 2
315 else if (!d_stricmp(p, "-lowresgraphics"))
316 GameArg.GfxSkipHiresGFX = 1;
317 else if (!d_stricmp(p, "-lowresmovies"))
318 GameArg.GfxSkipHiresMovie = 1;
319 #endif
320 #if DXX_USE_OGL
321 // OpenGL Options
322
323 else if (!d_stricmp(p, "-gl_fixedfont"))
324 CGameArg.OglFixedFont = true;
325 else if (!d_stricmp(p, "-gl_syncmethod"))
326 CGameArg.OglSyncMethod = arg_enum<SyncGLMethod>(pp, end);
327 else if (!d_stricmp(p, "-gl_syncwait"))
328 CGameArg.OglSyncWait = arg_integer(pp, end);
329 else if (!d_stricmp(p, "-gl_darkedges"))
330 CGameArg.OglDarkEdges = true;
331 #if DXX_USE_STEREOSCOPIC_RENDER
332 else if (!d_stricmp(p, "-gl_stereo"))
333 CGameArg.OglStereo = true;
334 else if (!d_stricmp(p, "-gl_stereoview"))
335 CGameArg.OglStereoView = arg_integer(pp, end);
336 #endif
337 #endif
338
339 // Multiplayer Options
340
341 #if DXX_USE_UDP
342 else if (!d_stricmp(p, "-udp_hostaddr"))
343 CGameArg.MplUdpHostAddr = arg_string(pp, end);
344 else if (!d_stricmp(p, "-udp_hostport"))
345 /* Peers use -udp_myport to change, so peer cannot set a
346 * privileged port.
347 */
348 arg_port_number(pp, end, CGameArg.MplUdpHostPort, false);
349 else if (!d_stricmp(p, "-udp_myport"))
350 {
351 arg_port_number(pp, end, CGameArg.MplUdpMyPort, false);
352 }
353 else if (!d_stricmp(p, "-no-tracker"))
354 {
355 /* Always recognized. No-op if tracker support compiled
356 * out. */
357 #if DXX_USE_TRACKER
358 CGameArg.MplTrackerAddr.clear();
359 #endif
360 }
361 #if DXX_USE_TRACKER
362 else if (!d_stricmp(p, "-tracker_hostaddr"))
363 {
364 CGameArg.MplTrackerAddr = arg_string(pp, end);
365 }
366 else if (!d_stricmp(p, "-tracker_hostport"))
367 arg_port_number(pp, end, CGameArg.MplTrackerPort, true);
368 #endif
369 #endif
370
371 #if DXX_BUILD_DESCENT == 1
372 else if (!d_stricmp(p, "-nobm"))
373 GameArg.EdiNoBm = 1;
374 #elif DXX_BUILD_DESCENT == 2
375 #if DXX_USE_EDITOR
376 // Editor Options
377
378 else if (!d_stricmp(p, "-autoload"))
379 GameArg.EdiAutoLoad = arg_string(pp, end);
380 else if (!d_stricmp(p, "-macdata"))
381 GameArg.EdiMacData = 1;
382 else if (!d_stricmp(p, "-hoarddata"))
383 GameArg.EdiSaveHoardData = 1;
384 #endif
385 #endif
386
387 // Debug Options
388
389 else if (!d_stricmp(p, "-debug"))
390 {
391 if (CGameArg.DbgVerbose < CON_DEBUG)
392 CGameArg.DbgVerbose = CON_DEBUG;
393 }
394 else if (!d_stricmp(p, "-verbose"))
395 {
396 if (CGameArg.DbgVerbose < CON_VERBOSE)
397 CGameArg.DbgVerbose = CON_VERBOSE;
398 }
399
400 else if (!d_stricmp(p, "-no-grab"))
401 CGameArg.DbgForbidConsoleGrab = true;
402 else if (!d_stricmp(p, "-safelog"))
403 CGameArg.DbgSafelog = true;
404 else if (!d_stricmp(p, "-norun"))
405 CGameArg.DbgNoRun = true;
406 else if (!d_stricmp(p, "-renderstats"))
407 CGameArg.DbgRenderStats = true;
408 else if (!d_stricmp(p, "-text"))
409 CGameArg.DbgAltTex = arg_string(pp, end);
410 else if (!d_stricmp(p, "-showmeminfo"))
411 CGameArg.DbgShowMemInfo = 1;
412 else if (!d_stricmp(p, "-nodoublebuffer"))
413 CGameArg.DbgNoDoubleBuffer = true;
414 else if (!d_stricmp(p, "-bigpig"))
415 CGameArg.DbgNoCompressPigBitmap = true;
416 else if (!d_stricmp(p, "-16bpp"))
417 CGameArg.DbgBpp = 16;
418
419 #if DXX_USE_OGL
420 else if (!d_stricmp(p, "-gl_oldtexmerge"))
421 CGameArg.DbgUseOldTextureMerge = true;
422 else if (!d_stricmp(p, "-gl_intensity4_ok"))
423 CGameArg.DbgGlIntensity4Ok = arg_integer(pp, end);
424 else if (!d_stricmp(p, "-gl_luminance4_alpha4_ok"))
425 CGameArg.DbgGlLuminance4Alpha4Ok = arg_integer(pp, end);
426 else if (!d_stricmp(p, "-gl_rgba2_ok"))
427 CGameArg.DbgGlRGBA2Ok = arg_integer(pp, end);
428 else if (!d_stricmp(p, "-gl_readpixels_ok"))
429 CGameArg.DbgGlReadPixelsOk = arg_integer(pp, end);
430 else if (!d_stricmp(p, "-gl_gettexlevelparam_ok"))
431 CGameArg.DbgGlGetTexLevelParamOk = arg_integer(pp, end);
432 #else
433 else if (!d_stricmp(p, "-tmap"))
434 CGameArg.DbgTexMap = arg_string(pp, end);
435 else if (!d_stricmp(p, "-hwsurface"))
436 CGameArg.DbgSdlHWSurface = true;
437 else if (!d_stricmp(p, "-asyncblit"))
438 CGameArg.DbgSdlASyncBlit = true;
439 #endif
440 else if (!d_stricmp(p, "-ini"))
441 {
442 ini.emplace_back(arg_string(pp, end));
443 if (ini.size() > 10)
444 throw nesting_depth_exceeded();
445 ReadIniArgs(ini);
446 }
447 #if defined(__APPLE__) && defined(__MACH__)
448 else if (!strncmp(p, "-psn", 4))
449 {
450 //do nothing/gobble it up
451 }
452 #endif
453 else
454 throw unhandled_argument(std::move(*pp));
455 }
456 }
457
458 }
459
460 }
461
462 namespace dcx {
463
464 namespace {
465
466 static void PostProcessGameArg()
467 {
468 if (CGameArg.SysMaxFPS < MINIMUM_FPS)
469 CGameArg.SysMaxFPS = MINIMUM_FPS;
470 else if (CGameArg.SysMaxFPS > MAXIMUM_FPS)
471 CGameArg.SysMaxFPS = MAXIMUM_FPS;
472 #if PHYSFS_VER_MAJOR >= 2
473 if (!CGameArg.SysMissionDir.empty())
474 PHYSFS_mount(CGameArg.SysMissionDir.c_str(), MISSION_DIR, 1);
475 #endif
476
477 #if SDL_MAJOR_VERSION == 1
478 static char sdl_disable_lock_keys[] = "SDL_DISABLE_LOCK_KEYS=0";
479 if (CGameArg.CtlNoStickyKeys) // Must happen before SDL_Init!
480 sdl_disable_lock_keys[sizeof(sdl_disable_lock_keys) - 2] = '1';
481 SDL_putenv(sdl_disable_lock_keys);
482 #endif
483 }
484
485 static std::string ConstructIniStackExplanation(const Inilist &ini)
486 {
487 Inilist::const_reverse_iterator i = ini.rbegin(), e = ini.rend();
488 if (i == e)
489 return " while processing <command line>";
490 std::string result;
491 result.reserve(ini.size() * 128);
492 result += " while processing \"";
493 for (;;)
494 {
495 result += i->filename;
496 if (++ i == e)
497 return result += "\"";
498 result += "\"\n included from \"";
499 }
500 }
501
502 }
503
504 }
505
506 namespace dsx {
507
508 bool InitArgs(std::span<char *> argv)
509 {
510 InitGameArg();
511
512 Inilist ini;
513 try {
514 {
515 assert(ini.empty());
516 #if DXX_BUILD_DESCENT == 1
517 const auto INI_FILENAME = "d1x.ini";
518 #elif DXX_BUILD_DESCENT == 2
519 const auto INI_FILENAME = "d2x.ini";
520 #endif
521 ini.emplace_back(INI_FILENAME);
522 ReadIniArgs(ini);
523 }
524 ReadCmdArgs(ini, {argv.begin(), argv.end()});
525 PostProcessGameArg();
526 return true;
527 } catch(const missing_parameter& e) {
528 UserError("Missing parameter for argument \"%s\"%s", e.arg.c_str(), ConstructIniStackExplanation(ini).c_str());
529 } catch(const unhandled_argument& e) {
530 UserError("Unhandled argument \"%s\"%s", e.arg.c_str(), ConstructIniStackExplanation(ini).c_str());
531 } catch(const conversion_failure& e) {
532 UserError("Failed to convert argument \"%s\" parameter \"%s\"%s", e.arg.c_str(), e.value.c_str(), ConstructIniStackExplanation(ini).c_str());
533 } catch(const nesting_depth_exceeded &) {
534 UserError("Nesting depth exceeded%s", ConstructIniStackExplanation(ini).c_str());
535 }
536 return false;
537 }
538
539 }