Skip to content

Commit

Permalink
SporeModManager: only read and write installed mod list xml once
Browse files Browse the repository at this point in the history
  • Loading branch information
Rosalie241 committed Nov 16, 2023
1 parent 7cb91f3 commit 90c0c82
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 82 deletions.
101 changes: 79 additions & 22 deletions SporeModManager/SporeModManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,44 @@

using namespace SporeModManagerHelpers;

//
// Local Variables
//

static bool l_HasInstalledSporeMods = false;
static std::vector<SporeMod::Xml::InstalledSporeMod> l_InstalledSporeMods;
static bool l_SaveInstalledSporeMods = true;
//
// Helper Functions
//

static bool GetInstalledSporeModList(void)
{
if (!l_HasInstalledSporeMods)
{
if (!SporeMod::Xml::GetInstalledModList(l_InstalledSporeMods))
{
std::cerr << "SporeMod::Xml::GetInstalledMostList() Failed!" << std::endl;
return false;
}
l_HasInstalledSporeMods = true;
}
return true;
}

static bool SaveInstalledSporeModList(void)
{
if (l_SaveInstalledSporeMods)
{
if (!SporeMod::Xml::SaveInstalledModList(l_InstalledSporeMods))
{
std::cerr << "SporeMod::Xml::SaveInstalledModList() Failed!" << std::endl;
return false;
}
}
return true;
}

static bool GetUniqueName(const std::filesystem::path& path, const std::string& extension, std::string& uniqueName)
{
Zip::ZipFile zipFile;
Expand Down Expand Up @@ -71,18 +105,16 @@ static bool GetUniqueName(const std::filesystem::path& path, const std::string&

bool SporeModManager::ListInstalledMods(void)
{
SporeMod::Xml::InstalledSporeMod installedSporeMod;
std::vector<SporeMod::Xml::InstalledSporeMod> installedSporeMods;
SporeMod::Xml::InstalledSporeMod installedSporeMod;

if (!SporeMod::Xml::GetInstalledModList(installedSporeMods))
if (!GetInstalledSporeModList())
{
std::cerr << "SporeMod::Xml::GetInstalledMostList() Failed!" << std::endl;
return false;
}

for (size_t i = 0; i < installedSporeMods.size(); i++)
for (size_t i = 0; i < l_InstalledSporeMods.size(); i++)
{
installedSporeMod = installedSporeMods.at(i);
installedSporeMod = l_InstalledSporeMods.at(i);

std::cout << "[" << i << "] " << installedSporeMod.Name << std::endl;
if (!installedSporeMod.Description.empty())
Expand All @@ -99,6 +131,8 @@ bool SporeModManager::InstallMods(std::vector<std::filesystem::path> paths, bool
std::string uniqueName;
std::vector<std::string> uniqueNames;
std::string extension;
bool returnValue = true;
SporeMod::Xml::InstalledSporeMod installedSporeMod;

// do some basic validation before attempting
// to install the given mods
Expand Down Expand Up @@ -140,27 +174,42 @@ bool SporeModManager::InstallMods(std::vector<std::filesystem::path> paths, bool
}
}

if (!GetInstalledSporeModList())
{
return false;
}

// install given mods
for (const auto& path : paths)
{
installedSporeMod = {};
extension = String::Lowercase(path.extension().string());
if (extension == ".sporemod")
{
if (!SporeMod::InstallSporeMod(path))
if (!SporeMod::InstallSporeMod(path, installedSporeMod, l_InstalledSporeMods))
{
return false;
returnValue = false;
break;
}
l_InstalledSporeMods.push_back(installedSporeMod);
}
else if (extension == ".package")
{
if (!SporeMod::InstallPackage(path))
if (!SporeMod::InstallPackage(path, installedSporeMod, l_InstalledSporeMods))
{
return false;
returnValue = false;
break;
}
l_InstalledSporeMods.push_back(installedSporeMod);
}
}

return true;
if (!SaveInstalledSporeModList())
{
return false;
}

return returnValue;
}

bool SporeModManager::UpdateMods(std::vector<std::filesystem::path> paths, bool requiresInstalled)
Expand All @@ -171,6 +220,11 @@ bool SporeModManager::UpdateMods(std::vector<std::filesystem::path> paths, bool
std::vector<std::string> uniqueNames;
std::string extension;

if (!GetInstalledSporeModList())
{
return false;
}

// do validation before attempting
// to update the given mods
for (size_t i = 0; i < paths.size(); i++)
Expand Down Expand Up @@ -208,7 +262,7 @@ bool SporeModManager::UpdateMods(std::vector<std::filesystem::path> paths, bool
}
uniqueNames.push_back(uniqueName);

if (!SporeMod::FindInstalledMod(uniqueName, installedSporeModId))
if (!SporeMod::FindInstalledMod(uniqueName, installedSporeModId, l_InstalledSporeMods))
{
if (requiresInstalled)
{
Expand All @@ -223,31 +277,35 @@ bool SporeModManager::UpdateMods(std::vector<std::filesystem::path> paths, bool
}
}

// disable saving the installed mod list when uninstalling mods
l_SaveInstalledSporeMods = false;

// apply changes
if (!installedSporeModIds.empty() && !UninstallMods(installedSporeModIds))
{
return false;
}

// save installed mod list when installing mods
l_SaveInstalledSporeMods = true;

return InstallMods(paths, true);
}

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;

if (!SporeMod::Xml::GetInstalledModList(installedSporeMods))
if (!GetInstalledSporeModList())
{
std::cerr << "SporeMod::Xml::GetInstalledModList() Failed!" << std::endl;
return false;
}

for (const auto& id : ids)
{
if (id < 0 || (size_t)id >= installedSporeMods.size() || installedSporeMods.empty())
if (id < 0 || (size_t)id >= l_InstalledSporeMods.size() || l_InstalledSporeMods.empty())
{
std::cerr << "ID(s) must be valid!" << std::endl;
return false;
Expand All @@ -256,7 +314,7 @@ bool SporeModManager::UninstallMods(std::vector<int> ids)

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

for (const auto& installedFile : installedSporeMod.InstalledFiles)
{
Expand All @@ -280,16 +338,15 @@ bool SporeModManager::UninstallMods(std::vector<int> ids)

for (const auto& removedSporeMod : removedSporeMods)
{
auto installedSporeModIter = std::find(installedSporeMods.begin(), installedSporeMods.end(), removedSporeMod);
if (installedSporeModIter != installedSporeMods.end())
auto installedSporeModIter = std::find(l_InstalledSporeMods.begin(), l_InstalledSporeMods.end(), removedSporeMod);
if (installedSporeModIter != l_InstalledSporeMods.end())
{
installedSporeMods.erase(installedSporeModIter);
l_InstalledSporeMods.erase(installedSporeModIter);
}
}

if (!SporeMod::Xml::SaveInstalledModList(installedSporeMods))
if (!SaveInstalledSporeModList())
{
std::cerr << "SporeMod::Xml::SaveInstalledModList() Failed!" << std::endl;
return false;
}

Expand Down
6 changes: 3 additions & 3 deletions SporeModManager/SporeModManagerHelpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,17 +183,17 @@ namespace SporeModManagerHelpers
/// <summary>
/// Tries to find installed mod with uniqueName
/// </summary>
bool FindInstalledMod(std::string uniqueName, int& installedSporeModId);
bool FindInstalledMod(std::string uniqueName, int& installedSporeModId, const std::vector<Xml::InstalledSporeMod>& installedSporeMods);

/// <summary>
/// Installs sporemod file
/// </summary>
bool InstallSporeMod(std::filesystem::path path);
bool InstallSporeMod(std::filesystem::path path, Xml::InstalledSporeMod& installedSporeMod, const std::vector<Xml::InstalledSporeMod>& installedSporeMods);

/// <summary>
/// Installs package file
/// </summary>
bool InstallPackage(std::filesystem::path path);
bool InstallPackage(std::filesystem::path path, Xml::InstalledSporeMod& installedSporeMod, const std::vector<Xml::InstalledSporeMod>& installedSporeMods);
}

namespace Path
Expand Down
69 changes: 12 additions & 57 deletions SporeModManager/SporeModManagerHelpers/SporeMod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,9 @@ using namespace SporeModManagerHelpers;
// Helper Functions
//

static bool IsModAlreadyInstalled(std::string uniqueName)
static bool IsModAlreadyInstalled(std::string uniqueName,
const std::vector<SporeMod::Xml::InstalledSporeMod>& installedSporeMods)
{
std::vector<SporeMod::Xml::InstalledSporeMod> installedSporeMods;

if (!SporeMod::Xml::GetInstalledModList(installedSporeMods))
{
std::cerr << "SporeMod::Xml::GetInstalledModList() Failed!" << std::endl;
return true;
}

for (const auto& installedSporeMod : installedSporeMods)
{
if (installedSporeMod.UniqueName == uniqueName)
Expand All @@ -41,16 +34,9 @@ static bool IsModAlreadyInstalled(std::string uniqueName)
return false;
}

static bool CheckIfOtherModContainsFiles(std::vector<SporeMod::Xml::SporeModFile> sporeModFiles)
static bool CheckIfOtherModContainsFiles(std::vector<SporeMod::Xml::SporeModFile> sporeModFiles,
const std::vector<SporeMod::Xml::InstalledSporeMod>& installedSporeMods)
{
std::vector<SporeMod::Xml::InstalledSporeMod> installedSporeMods;

if (!SporeMod::Xml::GetInstalledModList(installedSporeMods))
{
std::cerr << "SporeMod::Xml::GetInstalledModList() Failed!" << std::endl;
return true;
}

for (const auto& installedSporeMod : installedSporeMods)
{
for (const auto& sporeModFile : sporeModFiles)
Expand All @@ -72,16 +58,9 @@ static bool CheckIfOtherModContainsFiles(std::vector<SporeMod::Xml::SporeModFile
// Exported Functions
//

bool SporeMod::FindInstalledMod(std::string uniqueName, int& installedSporeModId)
bool SporeMod::FindInstalledMod(std::string uniqueName, int& installedSporeModId, const std::vector<Xml::InstalledSporeMod>& installedSporeMods)
{
std::vector<SporeMod::Xml::InstalledSporeMod> installedSporeMods;
SporeMod::Xml::InstalledSporeMod installedSporeMod;

if (!Xml::GetInstalledModList(installedSporeMods))
{
std::cerr << "Xml::GetInstalledModList() Failed!" << std::endl;
return true;
}
Xml::InstalledSporeMod installedSporeMod;

for (size_t i = 0; i < installedSporeMods.size(); i++)
{
Expand All @@ -96,12 +75,11 @@ bool SporeMod::FindInstalledMod(std::string uniqueName, int& installedSporeModId
return false;
}

bool SporeMod::InstallSporeMod(std::filesystem::path path)
bool SporeMod::InstallSporeMod(std::filesystem::path path, Xml::InstalledSporeMod& installedSporeMod, const std::vector<Xml::InstalledSporeMod>& installedSporeMods)
{
Zip::ZipFile zipFile;
std::vector<char> modInfoFileBuffer;
Xml::SporeModInfo sporeModInfo;
Xml::InstalledSporeMod installedSporeMod;
Xml::SporeModInfoComponent component;
size_t componentsSize;
std::optional<int> defaultComponentId;
Expand Down Expand Up @@ -136,7 +114,7 @@ bool SporeMod::InstallSporeMod(std::filesystem::path path)

// check if mod with the same unique name is
// already installed
if (IsModAlreadyInstalled(sporeModInfo.UniqueName))
if (IsModAlreadyInstalled(sporeModInfo.UniqueName, installedSporeMods))
{
Zip::CloseFile(zipFile);
return false;
Expand Down Expand Up @@ -288,7 +266,7 @@ bool SporeMod::InstallSporeMod(std::filesystem::path path)
}

// file collision detection
if (CheckIfOtherModContainsFiles(installedSporeMod.InstalledFiles))
if (CheckIfOtherModContainsFiles(installedSporeMod.InstalledFiles, installedSporeMods))
{
Zip::CloseFile(zipFile);
return false;
Expand Down Expand Up @@ -326,25 +304,12 @@ bool SporeMod::InstallSporeMod(std::filesystem::path path)
}
}

std::vector<Xml::InstalledSporeMod> installedSporeMods;
if (!Xml::GetInstalledModList(installedSporeMods))
{
std::cerr << "Xml::GetInstalledModList() Failed!" << std::endl;
Zip::CloseFile(zipFile);
return false;
}

installedSporeMods.push_back(installedSporeMod);

Xml::SaveInstalledModList(installedSporeMods);

Zip::CloseFile(zipFile);
return true;
}

bool SporeMod::InstallPackage(std::filesystem::path path)
bool SporeMod::InstallPackage(std::filesystem::path path, Xml::InstalledSporeMod& installedSporeMod, const std::vector<Xml::InstalledSporeMod>& installedSporeMods)
{
Xml::InstalledSporeMod installedSporeMod;
Xml::SporeModFile installedModFile;

std::string baseName = path.stem().string();
Expand All @@ -358,13 +323,13 @@ bool SporeMod::InstallPackage(std::filesystem::path path)

// check if mod with the same unique name is
// already installed
if (IsModAlreadyInstalled(baseName))
if (IsModAlreadyInstalled(baseName, installedSporeMods))
{
return false;
}

// file collision detection
if (CheckIfOtherModContainsFiles(installedSporeMod.InstalledFiles))
if (CheckIfOtherModContainsFiles(installedSporeMod.InstalledFiles, installedSporeMods))
{
return false;
}
Expand All @@ -387,16 +352,6 @@ bool SporeMod::InstallPackage(std::filesystem::path path)
}
}

std::vector<Xml::InstalledSporeMod> installedSporeMods;
if (!Xml::GetInstalledModList(installedSporeMods))
{
std::cerr << "Xml::GetInstalledModList() Failed!" << std::endl;
return false;
}

installedSporeMods.push_back(installedSporeMod);

Xml::SaveInstalledModList(installedSporeMods);
return true;
}

0 comments on commit 90c0c82

Please sign in to comment.