Skip to content

Commit

Permalink
SporeModManager: add Path::GetConfigFilePath()
Browse files Browse the repository at this point in the history
  • Loading branch information
Rosalie241 committed Jul 19, 2022
1 parent a76076d commit 5c1f1c7
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 29 deletions.
13 changes: 4 additions & 9 deletions SporeModManager/SporeModManagerHelpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,14 @@ namespace SporeModManagerHelpers
std::filesystem::path GetFullInstallPath(SporeMod::InstallLocation installLocation, std::filesystem::path path);

/// <summary>
/// Returns mod libs path
/// Returns full path from the executable (without the executable filename)
/// </summary>
std::filesystem::path GetModLibsPath(void);
std::filesystem::path GetCurrentExecutablePath(void);

/// <summary>
/// Returns Galactic Adventures data path
/// Returns full path to the config file
/// </summary>
std::filesystem::path GetGalacticAdventuresDataPath(void);

/// <summary>
/// Returns Core Spore data path
/// </summary>
std::filesystem::path GetCoreSporeDataPath(void);
std::filesystem::path GetConfigFilePath(void);
}

namespace UI
Expand Down
100 changes: 87 additions & 13 deletions SporeModManager/SporeModManagerHelpers/Path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

#include <iostream>

#ifdef _WIN32
#include <Windows.h>
#endif // _WIN32

using namespace SporeModManagerHelpers;

//
Expand All @@ -21,6 +25,39 @@ std::filesystem::path l_ModLibsPath;
std::filesystem::path l_GalacticAdventuresDataPath;
std::filesystem::path l_CoreSporeDataPath;

//
// Helper Functions
//

std::filesystem::path MakeAbsolutePath(std::filesystem::path& path)
{
std::filesystem::path currentExecutablePath;
std::filesystem::path fullPath;

if (path.is_absolute())
{
return path;
}

currentExecutablePath = Path::GetCurrentExecutablePath();
fullPath = currentExecutablePath;
fullPath += "\\";
fullPath += path;

try
{
fullPath = std::filesystem::canonical(fullPath);
}
catch (...)
{
// just return the original path,
// because the directory probably doesn't exist
return path;
}

return fullPath;
}

//
// Exported Functions
//
Expand All @@ -32,6 +69,10 @@ bool Path::CheckIfPathsExist(void)
std::cerr << "SporeMod::Xml::GetDirectories() Failed!" << std::endl;
}

l_ModLibsPath = MakeAbsolutePath(l_ModLibsPath);
l_GalacticAdventuresDataPath = MakeAbsolutePath(l_GalacticAdventuresDataPath);
l_CoreSporeDataPath = MakeAbsolutePath(l_CoreSporeDataPath);

for (const auto& path : { l_ModLibsPath, l_GalacticAdventuresDataPath, l_CoreSporeDataPath })
{
if (!std::filesystem::is_directory(path))
Expand All @@ -45,6 +86,32 @@ bool Path::CheckIfPathsExist(void)
return true;
}

std::filesystem::path Path::GetCurrentExecutablePath(void)
{
#ifdef _WIN32
static std::filesystem::path currentExecutablePath;
wchar_t currentExecutablePathBuf[MAX_PATH];

if (!currentExecutablePath.empty())
{
return currentExecutablePath;
}

if (GetModuleFileNameW(nullptr, currentExecutablePathBuf, MAX_PATH) == 0)
{
std::cerr << "GetModuleFileNameW() Failed!" << std::endl;
return currentExecutablePath;
}

currentExecutablePath = currentExecutablePathBuf;
currentExecutablePath = currentExecutablePath.parent_path();

return currentExecutablePath;
#else // _WIN32
return std::filesystem::current_path()
#endif // _WIN32
}

std::filesystem::path Path::GetFullInstallPath(SporeMod::InstallLocation installLocation, std::filesystem::path path)
{
std::filesystem::path fullPath;
Expand All @@ -53,13 +120,13 @@ std::filesystem::path Path::GetFullInstallPath(SporeMod::InstallLocation install
{
default:
case SporeMod::InstallLocation::ModLibs:
fullPath = Path::GetModLibsPath();
fullPath = l_ModLibsPath;
break;
case SporeMod::InstallLocation::GalacticAdventuresData:
fullPath = Path::GetGalacticAdventuresDataPath();
fullPath = l_GalacticAdventuresDataPath;
break;
case SporeMod::InstallLocation::CoreSporeData:
fullPath = Path::GetCoreSporeDataPath();
fullPath = l_CoreSporeDataPath;
break;
}

Expand All @@ -69,18 +136,25 @@ std::filesystem::path Path::GetFullInstallPath(SporeMod::InstallLocation install
return fullPath;
}

std::filesystem::path Path::GetModLibsPath(void)
std::filesystem::path Path::GetConfigFilePath(void)
{
return l_ModLibsPath;
}
std::filesystem::path configFileName;
static std::filesystem::path configFilePath;

std::filesystem::path Path::GetGalacticAdventuresDataPath(void)
{
return l_GalacticAdventuresDataPath;
}
configFileName = "SporeModManager.xml";

std::filesystem::path Path::GetCoreSporeDataPath(void)
{
return l_CoreSporeDataPath;
if (!configFilePath.empty())
{
return configFilePath;
}

configFilePath = Path::GetCurrentExecutablePath();
if (!configFilePath.empty())
{
configFilePath += "\\";
}
configFilePath += configFileName;

return configFilePath;
}

27 changes: 20 additions & 7 deletions SporeModManager/SporeModManagerHelpers/SporeModXml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "SporeModManagerHelpers.hpp"

#include <iostream>
#include <algorithm>
#include <tinyxml2.h>

using namespace SporeModManagerHelpers;
Expand Down Expand Up @@ -308,13 +309,16 @@ bool SporeMod::Xml::ParseSporeModInfo(char* buffer, size_t bufferSize, SporeModI

bool SporeMod::Xml::GetDirectories(std::filesystem::path& modLibsPath, std::filesystem::path& galacticAdventuresDataPath, std::filesystem::path& coreSporeDataPath)
{
std::filesystem::path configFilePath;
tinyxml2::XMLDocument xmlDocument;
tinyxml2::XMLElement* xmlElement;
tinyxml2::XMLElement* childXmlElement;
tinyxml2::XMLError error;
std::string xmlElementName;

if (!std::filesystem::is_regular_file("SporeModManager.xml"))
configFilePath = Path::GetConfigFilePath();

if (!std::filesystem::is_regular_file(configFilePath))
{
if (!Xml::SaveDirectories("..\\ModLibs", "..\\..\\DataEP1", "..\\..\\Data"))
{
Expand All @@ -323,7 +327,7 @@ bool SporeMod::Xml::GetDirectories(std::filesystem::path& modLibsPath, std::file
}
}

error = xmlDocument.LoadFile("SporeModManager.xml");
error = xmlDocument.LoadFile(configFilePath.string().c_str());
if (error != tinyxml2::XMLError::XML_SUCCESS)
{
std::cerr << "XmlDocument.LoadFile() Failed!" << std::endl;
Expand Down Expand Up @@ -372,10 +376,13 @@ bool SporeMod::Xml::GetDirectories(std::filesystem::path& modLibsPath, std::file

bool SporeMod::Xml::SaveDirectories(std::filesystem::path modLibsPath, std::filesystem::path galacticAdventuresDataPath, std::filesystem::path coreSporeDataPath)
{
std::filesystem::path configFilePath;
tinyxml2::XMLDocument xmlDocument;
tinyxml2::XMLElement* rootXmlElement;
tinyxml2::XMLElement* directoriesElement;

configFilePath = Path::GetConfigFilePath();

rootXmlElement = xmlDocument.NewElement("SporeModManager");
xmlDocument.InsertFirstChild(rootXmlElement);

Expand All @@ -384,24 +391,27 @@ bool SporeMod::Xml::SaveDirectories(std::filesystem::path modLibsPath, std::file
directoriesElement->InsertNewChildElement("GalacticAdventuresDataDirectory")->SetText(galacticAdventuresDataPath.string().c_str());
directoriesElement->InsertNewChildElement("CoreSporeDataDirectory")->SetText(coreSporeDataPath.string().c_str());

xmlDocument.SaveFile("SporeModManager.xml");
xmlDocument.SaveFile(configFilePath.string().c_str());
return true;
}

bool SporeMod::Xml::GetInstalledModList(std::vector<InstalledSporeMod>& installedSporeModList)
{
std::filesystem::path configFilePath;
tinyxml2::XMLDocument xmlDocument;
tinyxml2::XMLElement* xmlElement;
tinyxml2::XMLElement* childXmlElement;
tinyxml2::XMLError error;
std::string xmlElementName;

if (!std::filesystem::is_regular_file("SporeModManager.xml"))
configFilePath = Path::GetConfigFilePath();

if (!std::filesystem::is_regular_file(configFilePath))
{
return true;
}

error = xmlDocument.LoadFile("SporeModManager.xml");
error = xmlDocument.LoadFile(configFilePath.string().c_str());
if (error != tinyxml2::XMLError::XML_SUCCESS)
{
std::cerr << "XmlDocument.LoadFile() Failed!" << std::endl;
Expand Down Expand Up @@ -450,6 +460,7 @@ bool SporeMod::Xml::GetInstalledModList(std::vector<InstalledSporeMod>& installe

bool SporeMod::Xml::SaveInstalledModList(std::vector<InstalledSporeMod> installedSporeModList)
{
std::filesystem::path configFilePath;
tinyxml2::XMLDocument xmlDocument;
tinyxml2::XMLElement* rootXmlElement;
tinyxml2::XMLElement* installedSporeModsElement;
Expand All @@ -459,7 +470,9 @@ bool SporeMod::Xml::SaveInstalledModList(std::vector<InstalledSporeMod> installe
tinyxml2::XMLError error;
std::string xmlElementName;

error = xmlDocument.LoadFile("SporeModManager.xml");
configFilePath = Path::GetConfigFilePath();

error = xmlDocument.LoadFile(configFilePath.string().c_str());
if (error != tinyxml2::XMLError::XML_SUCCESS)
{
std::cerr << "XmlDocument.LoadFile() Failed!" << std::endl;
Expand Down Expand Up @@ -499,7 +512,7 @@ bool SporeMod::Xml::SaveInstalledModList(std::vector<InstalledSporeMod> installe
}
}

xmlDocument.SaveFile("SporeModManager.xml");
xmlDocument.SaveFile(configFilePath.string().c_str());
return true;
}

0 comments on commit 5c1f1c7

Please sign in to comment.