Skip to content

Commit

Permalink
SporeModManager: add -a/--auto argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Rosalie241 committed Oct 29, 2023
1 parent c000cdf commit cfd1663
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 30 deletions.
5 changes: 5 additions & 0 deletions SporeModManager/SporeModManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@

using namespace SporeModManagerHelpers;

void SporeModManager::SetAutoMode(bool value)
{
UI::SetAutoMode(value);
}

bool SporeModManager::ListInstalledMods(void)
{
std::vector<SporeMod::Xml::InstalledSporeMod> installedSporeMods;
Expand Down
5 changes: 5 additions & 0 deletions SporeModManager/SporeModManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@

namespace SporeModManager
{
/// <summary>
/// Sets Automatic Mode
/// </summary>
void SetAutoMode(bool value);

/// <summary>
/// Lists Installed mods
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions SporeModManager/SporeModManagerHelpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,11 @@ namespace SporeModManagerHelpers

namespace UI
{
/// <summary>
/// Sets automatic mode
/// </summary>
void SetAutoMode(bool value);

/// <summary>
/// Asks user for an integer
/// </summary>
Expand Down
36 changes: 36 additions & 0 deletions SporeModManager/SporeModManagerHelpers/UI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> defaultNumber, int min, int max)
{
std::string inputLine;
Expand All @@ -22,6 +37,13 @@ void UI::AskUserInput(std::string text, int& number, std::optional<int> 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())
{
Expand Down Expand Up @@ -58,6 +80,13 @@ void UI::AskUserInput(std::string text, char delimiter, std::vector<int>& number

std::cout << text;

if (l_AutoMode)
{
std::cout << std::endl;
numbers = defaultNumbers;
return;
}

std::getline(std::cin, inputLine);
if (inputLine.empty())
{
Expand Down Expand Up @@ -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())
Expand Down
96 changes: 66 additions & 30 deletions SporeModManager/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,61 @@
#include "SporeModManager.hpp"
#include "SporeModManagerHelpers.hpp"

#include <algorithm>
#include <iostream>
#include <cstring>

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<arg_str_type>& 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
Expand All @@ -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<arg_str_type> 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;
Expand All @@ -72,53 +108,53 @@ 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;
}

std::vector<int> 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 (...)
{
Expand Down

0 comments on commit cfd1663

Please sign in to comment.