diff --git a/SporeModManager/SporeModManager.cpp b/SporeModManager/SporeModManager.cpp index ab6707f..845648f 100644 --- a/SporeModManager/SporeModManager.cpp +++ b/SporeModManager/SporeModManager.cpp @@ -15,6 +15,11 @@ using namespace SporeModManagerHelpers; +void SporeModManager::SetAutoMode(bool value) +{ + UI::SetAutoMode(value); +} + bool SporeModManager::ListInstalledMods(void) { std::vector installedSporeMods; diff --git a/SporeModManager/SporeModManager.hpp b/SporeModManager/SporeModManager.hpp index c5093c0..40560b3 100644 --- a/SporeModManager/SporeModManager.hpp +++ b/SporeModManager/SporeModManager.hpp @@ -15,6 +15,11 @@ namespace SporeModManager { + /// + /// Sets Automatic Mode + /// + void SetAutoMode(bool value); + /// /// Lists Installed mods /// diff --git a/SporeModManager/SporeModManagerHelpers.hpp b/SporeModManager/SporeModManagerHelpers.hpp index 9b82901..417089b 100644 --- a/SporeModManager/SporeModManagerHelpers.hpp +++ b/SporeModManager/SporeModManagerHelpers.hpp @@ -231,6 +231,11 @@ namespace SporeModManagerHelpers namespace UI { + /// + /// Sets automatic mode + /// + void SetAutoMode(bool value); + /// /// Asks user for an integer /// diff --git a/SporeModManager/SporeModManagerHelpers/UI.cpp b/SporeModManager/SporeModManagerHelpers/UI.cpp index 721779c..1c165ce 100644 --- a/SporeModManager/SporeModManagerHelpers/UI.cpp +++ b/SporeModManager/SporeModManagerHelpers/UI.cpp @@ -14,6 +14,21 @@ using namespace SporeModManagerHelpers; +// +// Local Variables +// + +static bool l_AutoMode = false; + +// +// Exported Functions +// + +void UI::SetAutoMode(bool value) +{ + l_AutoMode = value; +} + void UI::AskUserInput(std::string text, int& number, std::optional defaultNumber, int min, int max) { std::string inputLine; @@ -22,6 +37,13 @@ void UI::AskUserInput(std::string text, int& number, std::optional defaultN { std::cout << text; + if (l_AutoMode) + { + std::cout << std::endl; + number = defaultNumber.has_value() ? defaultNumber.value() : 0; + return; + } + std::getline(std::cin, inputLine); if (inputLine.empty() && defaultNumber.has_value()) { @@ -58,6 +80,13 @@ void UI::AskUserInput(std::string text, char delimiter, std::vector& number std::cout << text; + if (l_AutoMode) + { + std::cout << std::endl; + numbers = defaultNumbers; + return; + } + std::getline(std::cin, inputLine); if (inputLine.empty()) { @@ -103,6 +132,13 @@ void UI::AskUserInput(std::string text, bool& boolValue, bool defaultValue) { std::cout << text; + if (l_AutoMode) + { + std::cout << std::endl; + boolValue = true; + return; + } + std::getline(std::cin, inputLine); inputLine = String::Lowercase(inputLine); if (inputLine.empty()) diff --git a/SporeModManager/main.cpp b/SporeModManager/main.cpp index aa85417..8a7d680 100644 --- a/SporeModManager/main.cpp +++ b/SporeModManager/main.cpp @@ -10,24 +10,61 @@ #include "SporeModManager.hpp" #include "SporeModManagerHelpers.hpp" +#include #include #include using namespace SporeModManagerHelpers; +// +// Local Defines +// + +#ifdef _WIN32 +#define arg_str(str) L##str +#define arg_str_type std::wstring +#else +#define arg_str(str) str +#define arg_str_type std::string +#endif // _WIN32 + +// +// Local Functions +// + static void ShowUsage() { - std::cout << "Usage: SporeModManager [OPTION]" << std::endl + std::cout << "SporeModManager is a commandline mod manager for Spore" << std::endl << std::endl - << "Options:" << std::endl + << "Usage: SporeModManager [OPTION] [COMMAND]" << std::endl + << std::endl + << "Commands:" << std::endl << " list-installed lists installed mod(s) with id(s)" << std::endl << " install file(s) installs file(s)" << std::endl << " update file(s) updates mod(s) using file(s)" << std::endl << " uninstall id(s) uninstalls mod with id(s)" << std::endl << std::endl - << " help display this help and exit" << std::endl; + << " help display this help and exit" << std::endl + << std::endl + << "Options: " << std::endl + << " -a, --auto disables user input during installation of mods" << std::endl + << std::endl; +} + +static bool HasArgument(std::vector& args, arg_str_type arg) +{ + auto argumentIter = std::find(args.begin(), args.end(), arg); + if (argumentIter != args.end()) + { + args.erase(argumentIter); + } + return argumentIter != args.end(); } +// +// Exported Functions +// + #ifdef _WIN32 int wmain(int argc, wchar_t** argv) #else @@ -45,25 +82,24 @@ int main(int argc, char** argv) return 1; } -#ifdef _WIN32 - // we know that wstring -> string conversion - // will possibly lose data, but that doesn't - // matter for the first argument, so we can - // safely disable the warning -#pragma warning(disable : 4244) - std::wstring waction = std::wstring(argv[1]); - std::string action = std::string(waction.begin(), waction.end()); -#else - std::string action = std::string(argv[1]); -#endif // _WIN32 - if (action == "help") + std::vector args(argv, argv + argc); + + // parse options + if (HasArgument(args, arg_str("-a")) || HasArgument(args, arg_str("--auto"))) + { + SporeModManager::SetAutoMode(true); + } + + // parse commands + arg_str_type action = args.at(1); + if (action == arg_str("help")) { ShowUsage(); return 0; } - else if (action == "list-installed") + else if (action == arg_str("list-installed")) { - if (argc != 2) + if (args.size() != 2) { ShowUsage(); return 1; @@ -72,41 +108,41 @@ int main(int argc, char** argv) SporeModManager::ListInstalledMods(); return 0; } - else if (action == "install") + else if (action == arg_str("install")) { - if (argc < 3) + if (args.size() < 3) { ShowUsage(); return 1; } - - for (int i = 2; i < argc; i++) + + for (int i = 2; i < args.size(); i++) { - if (!SporeModManager::InstallMod(argv[i])) + if (!SporeModManager::InstallMod(args[i])) { return 1; } } } - else if (action == "update") + else if (action == arg_str("update")) { - if (argc < 3) + if (args.size() < 3) { ShowUsage(); return 1; } - for (int i = 2; i < argc; i++) + for (int i = 2; i < args.size(); i++) { - if (!SporeModManager::UpdateMod(argv[i])) + if (!SporeModManager::UpdateMod(args[i])) { return 1; } } } - else if (action == "uninstall") + else if (action == arg_str("uninstall")) { - if (argc < 3) + if (args.size() < 3) { ShowUsage(); return 1; @@ -114,11 +150,11 @@ int main(int argc, char** argv) std::vector ids; - for (int i = 2; i < argc; i++) + for (int i = 2; i < args.size(); i++) { try { - ids.push_back(std::stoi(argv[i])); + ids.push_back(std::stoi(args[i])); } catch (...) {