| 1 | // Copyright 2017 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "UICommon/CommandLineParse.h" |
| 5 | |
| 6 | #include <list> |
| 7 | #include <optional> |
| 8 | #include <sstream> |
| 9 | #include <string> |
| 10 | #include <tuple> |
| 11 | |
| 12 | #include <OptionParser.h> |
| 13 | |
| 14 | #include "Common/Config/Config.h" |
| 15 | #include "Common/StringUtil.h" |
| 16 | #include "Common/Version.h" |
| 17 | #include "Core/Config/MainSettings.h" |
| 18 | |
| 19 | namespace CommandLineParse |
| 20 | { |
| 21 | class CommandLineConfigLayerLoader final : public Config::ConfigLayerLoader |
| 22 | { |
| 23 | public: |
| 24 | CommandLineConfigLayerLoader(const std::list<std::string>& args, const std::string& video_backend, |
| 25 | const std::string& audio_backend, bool batch, bool debugger) |
| 26 | : ConfigLayerLoader(Config::LayerType::CommandLine) |
| 27 | { |
| 28 | if (!video_backend.empty()) |
| 29 | m_values.emplace_back(Config::MAIN_GFX_BACKEND.GetLocation(), video_backend); |
| 30 | |
| 31 | if (!audio_backend.empty()) |
| 32 | { |
| 33 | m_values.emplace_back(Config::MAIN_DSP_HLE.GetLocation(), |
| 34 | ValueToString(audio_backend == "HLE")); |
| 35 | } |
| 36 | |
| 37 | // Batch mode hides the main window, and render to main hides the render window. To avoid a |
| 38 | // situation where we would have no window at all, disable render to main when using batch mode. |
| 39 | if (batch) |
| 40 | m_values.emplace_back(Config::MAIN_RENDER_TO_MAIN.GetLocation(), ValueToString(false)); |
| 41 | |
| 42 | if (debugger) |
| 43 | m_values.emplace_back(Config::MAIN_ENABLE_DEBUGGING.GetLocation(), ValueToString(true)); |
| 44 | |
| 45 | // Arguments are in the format of <System>.<Section>.<Key>=Value |
| 46 | for (const auto& arg : args) |
| 47 | { |
| 48 | std::istringstream buffer(arg); |
| 49 | std::string system_str, section, key, value; |
| 50 | std::getline(buffer, system_str, '.'); |
| 51 | std::getline(buffer, section, '.'); |
| 52 | std::getline(buffer, key, '='); |
| 53 | std::getline(buffer, value, '='); |
| 54 | std::optional<Config::System> system = Config::GetSystemFromName(system_str); |
| 55 | if (system) |
| 56 | { |
| 57 | m_values.emplace_back(Config::Location{*system, std::move(section), std::move(key)}, |
| 58 | std::move(value)); |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | void Load(Config::Layer* layer) override |
| 64 | { |
| 65 | for (auto& value : m_values) |
| 66 | { |
| 67 | layer->Set(std::get<0>(value), std::get<1>(value)); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | void Save(Config::Layer* layer) override |
| 72 | { |
| 73 | // Save Nothing |
| 74 | } |
| 75 | |
| 76 | private: |
| 77 | std::list<std::tuple<Config::Location, std::string>> m_values; |
| 78 | }; |
| 79 | |
| 80 | std::unique_ptr<optparse::OptionParser> CreateParser(ParserOptions options) |
| 81 | { |
| 82 | auto parser = std::make_unique<optparse::OptionParser>(); |
| 83 | parser->usage("usage: %prog [options]... [FILE]...").version(Common::GetScmRevStr()); |
| 84 | |
| 85 | parser->add_option("-u", "--user").action("store").help("User folder path"); |
| 86 | parser->add_option("-m", "--movie").action("store").help("Play a movie file"); |
| 87 | parser->add_option("-e", "--exec") |
| 88 | .action("append") |
| 89 | .metavar("<file>") |
| 90 | .type("string") |
| 91 | .help("Load the specified file"); |
| 92 | parser->add_option("-n", "--nand_title") |
| 93 | .action("store") |
| 94 | .metavar("<16-character ASCII title ID>") |
| 95 | .type("string") |
| 96 | .help("Launch a NAND title"); |
| 97 | parser->add_option("-C", "--config") |
| 98 | .action("append") |
| 99 | .metavar("<System>.<Section>.<Key>=<Value>") |
| 100 | .type("string") |
| 101 | .help("Set a configuration option"); |
| 102 | parser->add_option("-s", "--save_state") |
| 103 | .action("store") |
| 104 | .metavar("<file>") |
| 105 | .type("string") |
| 106 | .help("Load the initial save state"); |
| 107 | |
| 108 | if (options == ParserOptions::IncludeGUIOptions) |
| 109 | { |
| 110 | parser->add_option("-d", "--debugger") |
| 111 | .action("store_true") |
| 112 | .help("Show the debugger pane and additional View menu options"); |
| 113 | parser->add_option("-l", "--logger").action("store_true").help("Open the logger"); |
| 114 | parser->add_option("-b", "--batch") |
| 115 | .action("store_true") |
| 116 | .help("Run Dolphin without the user interface (Requires --exec or --nand-title)"); |
| 117 | parser->add_option("-c", "--confirm").action("store_true").help("Set Confirm on Stop"); |
| 118 | } |
| 119 | |
| 120 | parser->set_defaults("video_backend", ""); |
| 121 | parser->set_defaults("audio_emulation", ""); |
| 122 | parser->add_option("-v", "--video_backend").action("store").help("Specify a video backend"); |
| 123 | parser->add_option("-a", "--audio_emulation") |
| 124 | .choices({"HLE", "LLE"}) |
| 125 | .help("Choose audio emulation from [%choices]"); |
| 126 | |
| 127 | return parser; |
| 128 | } |
| 129 | |
| 130 | static void AddConfigLayer(const optparse::Values& options) |
| 131 | { |
| 132 | std::list<std::string> config_args; |
| 133 | if (options.is_set_by_user("config")) |
| 134 | config_args = options.all("config"); |
| 135 | |
| 136 | Config::AddLayer(std::make_unique<CommandLineConfigLayerLoader>( |
| 137 | std::move(config_args), static_cast<const char*>(options.get("video_backend")), |
| 138 | static_cast<const char*>(options.get("audio_emulation")), |
| 139 | static_cast<bool>(options.get("batch")), static_cast<bool>(options.get("debugger")))); |
| 140 | } |
| 141 | |
| 142 | optparse::Values& ParseArguments(optparse::OptionParser* parser, int argc, char** argv) |
| 143 | { |
| 144 | optparse::Values& options = parser->parse_args(argc, argv); |
| 145 | AddConfigLayer(options); |
| 146 | return options; |
| 147 | } |
| 148 | |
| 149 | optparse::Values& ParseArguments(optparse::OptionParser* parser, |
| 150 | const std::vector<std::string>& arguments) |
| 151 | { |
| 152 | optparse::Values& options = parser->parse_args(arguments); |
| 153 | AddConfigLayer(options); |
| 154 | return options; |
| 155 | } |
| 156 | } // namespace CommandLineParse |