Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #8797 from iwubcode/save-state-CLI
Core: Add support for booting a save state from command line
  • Loading branch information
delroth committed May 17, 2020
2 parents cea779c + cdf5490 commit 099197b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
19 changes: 17 additions & 2 deletions Source/Core/DolphinNoGUI/MainNoGUI.cpp
Expand Up @@ -156,13 +156,21 @@ int main(int argc, char* argv[])
optparse::Values& options = CommandLineParse::ParseArguments(parser.get(), argc, argv);
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;
bool game_specified = false;
if (options.is_set("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)),
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"))
{
Expand All @@ -178,8 +186,9 @@ int main(int argc, char* argv[])
}
else if (args.size())
{
boot = BootParameters::GenerateFromFile(args.front());
boot = BootParameters::GenerateFromFile(args.front(), save_state_path);
args.erase(args.begin());
game_specified = true;
}
else
{
Expand All @@ -201,6 +210,12 @@ int main(int argc, char* argv[])
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) {
if (state == Core::State::Uninitialized)
s_platform->Stop();
Expand Down
21 changes: 17 additions & 4 deletions Source/Core/DolphinQt/Main.cpp
Expand Up @@ -151,14 +151,20 @@ int main(int argc, char* argv[])
QObject::connect(QAbstractEventDispatcher::instance(), &QAbstractEventDispatcher::aboutToBlock,
&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;
bool game_specified = false;
if (options.is_set("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)),
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"))
Expand All @@ -177,20 +183,27 @@ int main(int argc, char* argv[])
}
else if (!args.empty())
{
boot = BootParameters::GenerateFromFile(args.front());
boot = BootParameters::GenerateFromFile(args.front(), save_state_path);
game_specified = true;
}

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(
nullptr, QObject::tr("Error"),
QObject::tr("Batch mode cannot be used without specifying a game to launch."));
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.
// An error has already been shown by code above, so exit without showing another error.
Expand Down
5 changes: 5 additions & 0 deletions Source/Core/UICommon/CommandLineParse.cpp
Expand Up @@ -94,6 +94,11 @@ std::unique_ptr<optparse::OptionParser> CreateParser(ParserOptions options)
.metavar("<System>.<Section>.<Key>=<Value>")
.type("string")
.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)
{
Expand Down

0 comments on commit 099197b

Please sign in to comment.