Skip to content

Commit

Permalink
SporeModManager: improve commandline and support multiple files & ids
Browse files Browse the repository at this point in the history
-l/--list-installed -> list-installed
-i/--install        -> install
-u/--update         -> update
-r/--uninstall      -> uninstall
   --help           -> help
  • Loading branch information
Rosalie241 committed Jul 16, 2022
1 parent 279beb9 commit b86d4ca
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 51 deletions.
53 changes: 35 additions & 18 deletions SporeModManager/SporeModManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,17 @@ bool SporeModManager::UpdateMod(std::filesystem::path path)
if (!SporeMod::FindInstalledMod(installedSporeModUniqueName, installedSporeModId))
{
std::cerr << "No mod found with the same unique name" << std::endl
<< "Did you mean -i/--install?" << std::endl;
<< "Did you mean install?" << std::endl;
return false;
}

return UninstallMod(installedSporeModId) && InstallMod(path);
return UninstallMods({ installedSporeModId }) && InstallMod(path);
}

bool SporeModManager::UninstallMod(int id)
bool SporeModManager::UninstallMods(std::vector<int> ids)
{
std::vector<SporeMod::Xml::InstalledSporeMod> installedSporeMods;
std::vector<SporeMod::Xml::InstalledSporeMod> removedSporeMods;
SporeMod::Xml::InstalledSporeMod installedSporeMod;
std::filesystem::path fullInstallPath;

Expand All @@ -141,32 +142,48 @@ bool SporeModManager::UninstallMod(int id)
return false;
}

if (id < 0 || (size_t)id > installedSporeMods.size())
for (const auto& id : ids)
{
std::cerr << "ID must be valid!" << std::endl;
return false;
if (id < 0 || (size_t)id >= installedSporeMods.size() || installedSporeMods.empty())
{
std::cerr << "ID(s) must be valid!" << std::endl;
return false;
}
}

installedSporeMod = installedSporeMods.at(id);

for (const auto& installedFile : installedSporeMod.InstalledFiles)
for (const auto& id : ids)
{
try
installedSporeMod = installedSporeMods.at(id);

for (const auto& installedFile : installedSporeMod.InstalledFiles)
{
fullInstallPath = Path::GetFullInstallPath(installedFile.InstallLocation, installedFile.FileName);
try
{
fullInstallPath = Path::GetFullInstallPath(installedFile.InstallLocation, installedFile.FileName);

std::cout << "-> Removing " << installedFile.FileName.string() << std::endl;

std::filesystem::remove(fullInstallPath);
}
catch (...)
{
std::cerr << "std::filesystem::remove(" << fullInstallPath << "\") Failed!" << std::endl;
return false;
}
}

std::cout << "-> Removing " << installedFile.FileName.string() << std::endl;
removedSporeMods.push_back(installedSporeMod);
}

std::filesystem::remove(fullInstallPath);
}
catch (...)
for (const auto& removedSporeMod : removedSporeMods)
{
auto installedSporeModIter = std::find(installedSporeMods.begin(), installedSporeMods.end(), removedSporeMod);
if (installedSporeModIter != installedSporeMods.end())
{
std::cerr << "std::filesystem::remove(" << fullInstallPath << "\") Failed!" << std::endl;
return false;
installedSporeMods.erase(installedSporeModIter);
}
}

installedSporeMods.erase(installedSporeMods.begin() + id);
if (!SporeMod::Xml::SaveInstalledModList(installedSporeMods))
{
std::cerr << "SporeMod::Xml::SaveInstalledModList() Failed!" << std::endl;
Expand Down
4 changes: 2 additions & 2 deletions SporeModManager/SporeModManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ namespace SporeModManager
bool UpdateMod(std::filesystem::path path);

/// <summary>
/// Uninstalls mod with id
/// Uninstalls mods with ids
/// </summary>
bool UninstallMod(int id);
bool UninstallMods(std::vector<int> ids);
}

#endif // SPOREMODMANAGER_HPP
8 changes: 8 additions & 0 deletions SporeModManager/SporeModManagerHelpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ namespace SporeModManagerHelpers
std::string Description;

std::vector<SporeModFile> InstalledFiles;

bool operator==(const InstalledSporeMod& other) const
{
return Name == other.Name &&
UniqueName == other.UniqueName &&
Description == other.Description &&
InstalledFiles.size() == other.InstalledFiles.size();
}
};

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion SporeModManager/SporeModManagerHelpers/SporeMod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ static bool IsModAlreadyInstalled(std::string uniqueName)
if (installedSporeMod.UniqueName == uniqueName)
{
std::cerr << "A mod with the same unique name (" << installedSporeMod.Name << ") has already been installed" << std::endl
<< "Did you mean -u/--update?" << std::endl;
<< "Did you mean update?" << std::endl;
return true;
}
}
Expand Down
65 changes: 35 additions & 30 deletions SporeModManager/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ static void ShowUsage()
std::cout << "Usage: SporeModManager [OPTION]" << std::endl
<< std::endl
<< "Options:" << std::endl
<< " -l, --list-installed lists installed mods with IDs" << std::endl
<< " -i, --install FILE installs FILE" << std::endl
<< " -u, --update FILE updates mod using FILE" << std::endl
<< " -r, --uninstall ID uninstalls mod with ID" << 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;
}

int main(int argc, char** argv)
Expand All @@ -41,13 +41,12 @@ int main(int argc, char** argv)
}

char* action = argv[1];
if (std::strcmp(action, "--help") == 0)
if (std::strcmp(action, "help") == 0)
{
ShowUsage();
return 0;
}
else if (std::strcmp(action, "-l") == 0 ||
std::strcmp(action, "--list-installed") == 0)
else if (std::strcmp(action, "list-installed") == 0)
{
if (argc != 2)
{
Expand All @@ -58,56 +57,62 @@ int main(int argc, char** argv)
SporeModManager::ListInstalledMods();
return 0;
}
else if (std::strcmp(action, "-i") == 0 ||
std::strcmp(action, "--install") == 0)
else if (std::strcmp(action, "install") == 0)
{
if (argc != 3)
if (argc < 3)
{
ShowUsage();
return 1;
}

if (!SporeModManager::InstallMod(argv[2]))
for (int i = 2; i < argc; i++)
{
return 1;
if (!SporeModManager::InstallMod(argv[i]))
{
return 1;
}
}
}
else if (std::strcmp(action, "-u") == 0 ||
std::strcmp(action, "--update") == 0)
else if (std::strcmp(action, "update") == 0)
{
if (argc != 3)
if (argc < 3)
{
ShowUsage();
return 1;
}

if (!SporeModManager::UpdateMod(argv[2]))
for (int i = 2; i < argc; i++)
{
return 1;
if (!SporeModManager::UpdateMod(argv[i]))
{
return 1;
}
}
}
else if (std::strcmp(action, "-r") == 0 ||
std::strcmp(action, "--uninstall") == 0)
else if (std::strcmp(action, "uninstall") == 0)
{
if (argc != 3)
if (argc < 3)
{
ShowUsage();
return 1;
}

int id = 0;
std::vector<int> ids;

try
for (int i = 2; i < argc; i++)
{
id = std::stoi(argv[2]);
}
catch (...)
{
std::cerr << "ID must be a number!" << std::endl;
return 1;
try
{
ids.push_back(std::stoi(argv[i]));
}
catch (...)
{
std::cerr << "ID must be a number!" << std::endl;
return 1;
}
}

if (!SporeModManager::UninstallMod(id))
if (!SporeModManager::UninstallMods(ids))
{
return 1;
}
Expand Down

0 comments on commit b86d4ca

Please sign in to comment.