Core: Add support for specifying a command line option to boot the game into a save-state

This commit is contained in:
iwubcode 2020-05-06 22:10:30 -05:00
parent 74b2410d7e
commit cdf5490d56
3 changed files with 39 additions and 6 deletions

View File

@ -156,13 +156,21 @@ int main(int argc, char* argv[])
optparse::Values& options = CommandLineParse::ParseArguments(parser.get(), argc, argv); optparse::Values& options = CommandLineParse::ParseArguments(parser.get(), argc, argv);
std::vector<std::string> args = parser->args(); std::vector<std::string> args = parser->args();
std::optional<std::string> save_state_path;
if (options.is_set("save_state"))
{
save_state_path = static_cast<const char*>(options.get("save_state"));
}
std::unique_ptr<BootParameters> boot; std::unique_ptr<BootParameters> boot;
bool game_specified = false;
if (options.is_set("exec")) if (options.is_set("exec"))
{ {
const std::list<std::string> paths_list = options.all("exec"); const std::list<std::string> paths_list = options.all("exec");
const std::vector<std::string> paths{std::make_move_iterator(std::begin(paths_list)), const std::vector<std::string> paths{std::make_move_iterator(std::begin(paths_list)),
std::make_move_iterator(std::end(paths_list))}; std::make_move_iterator(std::end(paths_list))};
boot = BootParameters::GenerateFromFile(paths); boot = BootParameters::GenerateFromFile(paths, save_state_path);
game_specified = true;
} }
else if (options.is_set("nand_title")) else if (options.is_set("nand_title"))
{ {
@ -178,8 +186,9 @@ int main(int argc, char* argv[])
} }
else if (args.size()) else if (args.size())
{ {
boot = BootParameters::GenerateFromFile(args.front()); boot = BootParameters::GenerateFromFile(args.front(), save_state_path);
args.erase(args.begin()); args.erase(args.begin());
game_specified = true;
} }
else else
{ {
@ -201,6 +210,12 @@ int main(int argc, char* argv[])
return 1; return 1;
} }
if (save_state_path && !game_specified)
{
fprintf(stderr, "A save state cannot be loaded without specifying a game to launch.\n");
return 1;
}
Core::SetOnStateChangedCallback([](Core::State state) { Core::SetOnStateChangedCallback([](Core::State state) {
if (state == Core::State::Uninitialized) if (state == Core::State::Uninitialized)
s_platform->Stop(); s_platform->Stop();

View File

@ -151,6 +151,12 @@ int main(int argc, char* argv[])
QObject::connect(QAbstractEventDispatcher::instance(), &QAbstractEventDispatcher::aboutToBlock, QObject::connect(QAbstractEventDispatcher::instance(), &QAbstractEventDispatcher::aboutToBlock,
&app, &Core::HostDispatchJobs); &app, &Core::HostDispatchJobs);
std::optional<std::string> save_state_path;
if (options.is_set("save_state"))
{
save_state_path = static_cast<const char*>(options.get("save_state"));
}
std::unique_ptr<BootParameters> boot; std::unique_ptr<BootParameters> boot;
bool game_specified = false; bool game_specified = false;
if (options.is_set("exec")) if (options.is_set("exec"))
@ -158,7 +164,7 @@ int main(int argc, char* argv[])
const std::list<std::string> paths_list = options.all("exec"); const std::list<std::string> paths_list = options.all("exec");
const std::vector<std::string> paths{std::make_move_iterator(std::begin(paths_list)), const std::vector<std::string> paths{std::make_move_iterator(std::begin(paths_list)),
std::make_move_iterator(std::end(paths_list))}; std::make_move_iterator(std::end(paths_list))};
boot = BootParameters::GenerateFromFile(paths); boot = BootParameters::GenerateFromFile(paths, save_state_path);
game_specified = true; game_specified = true;
} }
else if (options.is_set("nand_title")) else if (options.is_set("nand_title"))
@ -177,20 +183,27 @@ int main(int argc, char* argv[])
} }
else if (!args.empty()) else if (!args.empty())
{ {
boot = BootParameters::GenerateFromFile(args.front()); boot = BootParameters::GenerateFromFile(args.front(), save_state_path);
game_specified = true; game_specified = true;
} }
int retval; int retval;
if (Settings::Instance().IsBatchModeEnabled() && !game_specified) if (save_state_path && !game_specified)
{
ModalMessageBox::critical(
nullptr, QObject::tr("Error"),
QObject::tr("A save state cannot be loaded without specifying a game to launch."));
retval = 1;
}
else if (Settings::Instance().IsBatchModeEnabled() && !game_specified)
{ {
ModalMessageBox::critical( ModalMessageBox::critical(
nullptr, QObject::tr("Error"), nullptr, QObject::tr("Error"),
QObject::tr("Batch mode cannot be used without specifying a game to launch.")); QObject::tr("Batch mode cannot be used without specifying a game to launch."));
retval = 1; retval = 1;
} }
else if (Settings::Instance().IsBatchModeEnabled() && !boot) else if (!boot && (Settings::Instance().IsBatchModeEnabled() || save_state_path))
{ {
// A game to launch was specified, but it was invalid. // A game to launch was specified, but it was invalid.
// An error has already been shown by code above, so exit without showing another error. // An error has already been shown by code above, so exit without showing another error.

View File

@ -94,6 +94,11 @@ std::unique_ptr<optparse::OptionParser> CreateParser(ParserOptions options)
.metavar("<System>.<Section>.<Key>=<Value>") .metavar("<System>.<Section>.<Key>=<Value>")
.type("string") .type("string")
.help("Set a configuration option"); .help("Set a configuration option");
parser->add_option("-s", "--save_state")
.action("store")
.metavar("<file>")
.type("string")
.help("Load the initial save state");
if (options == ParserOptions::IncludeGUIOptions) if (options == ParserOptions::IncludeGUIOptions)
{ {