Skip to content

Commit 82df0d5

Browse files
committed
Merge pull request OpenXcom#540 from cfailde/options
Load options.cfg with -user parameter
2 parents a579b93 + 925cd52 commit 82df0d5

2 files changed

Lines changed: 110 additions & 100 deletions

File tree

src/Engine/Options.cpp

Lines changed: 106 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -269,45 +269,41 @@ void loadArgs(int argc, char** args)
269269
std::transform(argname.begin(), argname.end(), argname.begin(), ::tolower);
270270
if (argc > i + 1)
271271
{
272-
if (argname == "data")
272+
if (argname == "data")
273273
{
274274
_dataFolder = CrossPlatform::endPath(args[i+1]);
275275
}
276276
else if (argname == "user")
277277
{
278278
_userFolder = CrossPlatform::endPath(args[i+1]);
279279
}
280-
else
281-
{
282-
// case insensitive lookup of the argument
283-
bool found = false;
284-
for(std::map<std::string, std::string>::iterator it = _options.begin(); it != _options.end(); ++it)
285-
{
286-
std::string option = it->first;
287-
std::transform(option.begin(), option.end(), option.begin(), ::tolower);
288-
if (option == argname)
289-
{
290-
//save this command line option for now, we will apply it later
291-
_commandLineOptions[it->first]= args[i+1];
292-
found = true;
293-
break;
294-
}
295-
}
296-
if(!found)
297-
{
298-
Log(LOG_WARNING) << "Unknown option: " << argname;
299-
}
300-
}
301-
}
302-
else
303-
{
304-
Log(LOG_WARNING) << "Unknown option: " << argname;
305-
}
306-
}
307-
}
308-
if (_userFolder != "")
309-
{
310-
load();
280+
else
281+
{
282+
// case insensitive lookup of the argument
283+
bool found = false;
284+
for(std::map<std::string, std::string>::iterator it = _options.begin(); it != _options.end(); ++it)
285+
{
286+
std::string option = it->first;
287+
std::transform(option.begin(), option.end(), option.begin(), ::tolower);
288+
if (option == argname)
289+
{
290+
//save this command line option for now, we will apply it later
291+
_commandLineOptions[it->first]= args[i+1];
292+
found = true;
293+
break;
294+
}
295+
}
296+
if(!found)
297+
{
298+
Log(LOG_WARNING) << "Unknown option: " << argname;
299+
}
300+
}
301+
}
302+
else
303+
{
304+
Log(LOG_WARNING) << "Unknown option: " << argname;
305+
}
306+
}
311307
}
312308
}
313309

@@ -363,14 +359,9 @@ bool init(int argc, char** args)
363359
if (showHelp(argc, args))
364360
return false;
365361
createDefault();
366-
loadArgs(argc, args);
367-
if (_dataFolder == "")
368-
{
369-
_dataList = CrossPlatform::findDataFolders();
370-
// Missing data folder is handled in StartState
371-
}
372-
if (_userFolder == "")
373-
setUserFolder();
362+
loadArgs(argc, args);
363+
setFolders();
364+
updateOptions();
374365

375366
std::string s = getUserFolder();
376367
s += "openxcom.log";
@@ -393,15 +384,88 @@ bool init(int argc, char** args)
393384
Log(LOG_INFO) << "User folder is: " << _userFolder;
394385
Log(LOG_INFO) << "Config folder is: " << _configFolder;
395386
Log(LOG_INFO) << "Options loaded successfully.";
387+
return true;
388+
}
389+
390+
/**
391+
* Sets up the game's Data folder where the data files
392+
* are loaded from and the User folder and Config
393+
* folder where settings and saves are stored in.
394+
*/
395+
void setFolders()
396+
{
397+
if (_dataFolder == "")
398+
{
399+
_dataList = CrossPlatform::findDataFolders();
400+
// Missing data folder is handled in StartState
401+
}
402+
if (_userFolder == "")
403+
{
404+
std::vector<std::string> user = CrossPlatform::findUserFolders();
405+
_configFolder = CrossPlatform::findConfigFolder();
406+
407+
// Look for an existing user folder
408+
for (std::vector<std::string>::iterator i = user.begin(); i != user.end(); ++i)
409+
{
410+
if (CrossPlatform::folderExists(*i))
411+
{
412+
_userFolder = *i;
413+
break;
414+
}
415+
}
416+
417+
// Set up folders
418+
if (_userFolder == "")
419+
{
420+
for (std::vector<std::string>::iterator i = user.begin(); i != user.end(); ++i)
421+
{
422+
if (CrossPlatform::createFolder(*i))
423+
{
424+
_userFolder = *i;
425+
break;
426+
}
427+
}
428+
}
429+
}
430+
431+
if (_configFolder == "")
432+
{
433+
_configFolder = _userFolder;
434+
}
435+
}
436+
437+
/**
438+
* Updates the game's options with those in the configuation
439+
* file, if it exists yet, and any supplied on the command line.
440+
*/
441+
void updateOptions()
442+
{
443+
// Load existing options
444+
if (CrossPlatform::folderExists(_configFolder))
445+
{
446+
try
447+
{
448+
load();
449+
}
450+
catch (YAML::Exception &e)
451+
{
452+
Log(LOG_ERROR) << e.what();
453+
}
454+
}
455+
// Create config folder and save options
456+
else
457+
{
458+
CrossPlatform::createFolder(_configFolder);
459+
save();
460+
}
396461

397462
// now apply options set on the command line, overriding defaults and those loaded from config file
398463
for(std::map<std::string, std::string>::const_iterator it = _commandLineOptions.begin(); it != _commandLineOptions.end(); ++it)
399464
{
400465
_options[it->first] = it->second;
401466
}
402-
return true;
403467
}
404-
468+
405469
/**
406470
* Loads options from a YAML file.
407471
* @param filename YAML filename.
@@ -509,62 +573,6 @@ std::string getUserFolder()
509573
return _userFolder;
510574
}
511575

512-
/**
513-
* Sets up the game's User folder where settings
514-
* and saves are stored in.
515-
*/
516-
void setUserFolder()
517-
{
518-
std::vector<std::string> user = CrossPlatform::findUserFolders();
519-
_configFolder = CrossPlatform::findConfigFolder();
520-
521-
// Look for an existing user folder
522-
for (std::vector<std::string>::iterator i = user.begin(); i != user.end(); ++i)
523-
{
524-
if (CrossPlatform::folderExists(*i))
525-
{
526-
_userFolder = *i;
527-
break;
528-
}
529-
}
530-
531-
// Set up folders
532-
if (_userFolder == "")
533-
{
534-
for (std::vector<std::string>::iterator i = user.begin(); i != user.end(); ++i)
535-
{
536-
if (CrossPlatform::createFolder(*i))
537-
{
538-
_userFolder = *i;
539-
break;
540-
}
541-
}
542-
}
543-
if (_configFolder == "")
544-
{
545-
_configFolder = _userFolder;
546-
}
547-
548-
// Load existing options
549-
if (CrossPlatform::folderExists(_configFolder))
550-
{
551-
try
552-
{
553-
load();
554-
}
555-
catch (YAML::Exception &e)
556-
{
557-
Log(LOG_ERROR) << e.what();
558-
}
559-
}
560-
// Create config folder and save options
561-
else
562-
{
563-
CrossPlatform::createFolder(_configFolder);
564-
save();
565-
}
566-
}
567-
568576
/**
569577
* Returns an option in string format.
570578
* @param id Option ID.

src/Engine/Options.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ namespace Options
5757
std::vector<std::string> *getDataList();
5858
/// Gets the game's user folder.
5959
std::string getUserFolder();
60-
/// Sets the game's user folder.
61-
void setUserFolder();
60+
/// Sets the game's data, user and config folders.
61+
void setFolders();
62+
/// Update game options from config file and command line.
63+
void updateOptions();
6264
/// Gets a string option.
6365
std::string getString(const std::string& id);
6466
/// Gets an integer option.

0 commit comments

Comments
 (0)