diff --git a/SporeModManager/SporeModManager.cpp b/SporeModManager/SporeModManager.cpp index d061d69..e09dd10 100644 --- a/SporeModManager/SporeModManager.cpp +++ b/SporeModManager/SporeModManager.cpp @@ -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 ids) { std::vector installedSporeMods; + std::vector removedSporeMods; SporeMod::Xml::InstalledSporeMod installedSporeMod; std::filesystem::path fullInstallPath; @@ -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; diff --git a/SporeModManager/SporeModManager.hpp b/SporeModManager/SporeModManager.hpp index bee4e55..c976915 100644 --- a/SporeModManager/SporeModManager.hpp +++ b/SporeModManager/SporeModManager.hpp @@ -30,9 +30,9 @@ namespace SporeModManager bool UpdateMod(std::filesystem::path path); /// - /// Uninstalls mod with id + /// Uninstalls mods with ids /// - bool UninstallMod(int id); + bool UninstallMods(std::vector ids); } #endif // SPOREMODMANAGER_HPP \ No newline at end of file diff --git a/SporeModManager/SporeModManagerHelpers.hpp b/SporeModManager/SporeModManagerHelpers.hpp index 07c4353..88aa7cc 100644 --- a/SporeModManager/SporeModManagerHelpers.hpp +++ b/SporeModManager/SporeModManagerHelpers.hpp @@ -71,6 +71,14 @@ namespace SporeModManagerHelpers std::string Description; std::vector InstalledFiles; + + bool operator==(const InstalledSporeMod& other) const + { + return Name == other.Name && + UniqueName == other.UniqueName && + Description == other.Description && + InstalledFiles.size() == other.InstalledFiles.size(); + } }; /// diff --git a/SporeModManager/SporeModManagerHelpers/SporeMod.cpp b/SporeModManager/SporeModManagerHelpers/SporeMod.cpp index 0feb4bb..dd5ed6a 100644 --- a/SporeModManager/SporeModManagerHelpers/SporeMod.cpp +++ b/SporeModManager/SporeModManagerHelpers/SporeMod.cpp @@ -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; } } diff --git a/SporeModManager/main.cpp b/SporeModManager/main.cpp index 785328c..20b78ad 100644 --- a/SporeModManager/main.cpp +++ b/SporeModManager/main.cpp @@ -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) @@ -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) { @@ -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 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; }