Skip to content

Commit

Permalink
SporeModManager: support unicode in path of configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
Rosalie241 committed Aug 4, 2024
1 parent 591cf63 commit fa27a7f
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 19 deletions.
12 changes: 6 additions & 6 deletions SporeModManager/SporeModManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ bool SporeModManager::InstallMods(std::vector<std::filesystem::path> paths, bool

if (!std::filesystem::is_regular_file(path))
{
std::cerr << "Error: " << path << " is not a regular file or doesn't exist!" << std::endl;
std::wcerr << L"Error: " << path << L" is not a regular file or doesn't exist!" << std::endl;
close_zipfiles();
return false;
}
Expand Down Expand Up @@ -222,9 +222,9 @@ bool SporeModManager::InstallMods(std::vector<std::filesystem::path> paths, bool
const bool skipInstall = skipInstalled && hasInstalled;
if (hasUniqueName || skipInstall)
{
std::cout << "Skipping " << path << (hasUniqueName ?
" as it's already being installed!" :
" as it's already installed!") << std::endl;
std::wcout << L"Skipping " << path << (hasUniqueName ?
L" as it's already being installed!" :
L" as it's already installed!") << std::endl;
paths.erase(paths.begin() + i);
l_ZipFiles.erase(l_ZipFiles.begin() + i);
l_SporeModInfos.erase(l_SporeModInfos.begin() + i);
Expand Down Expand Up @@ -329,7 +329,7 @@ bool SporeModManager::UpdateMods(std::vector<std::filesystem::path> paths, bool

if (!std::filesystem::is_regular_file(path))
{
std::cerr << "Error: " << path << " is not a regular file or doesn't exist!" << std::endl;
std::wcerr << L"Error: " << path << L" is not a regular file or doesn't exist!" << std::endl;
return false;
}

Expand All @@ -352,7 +352,7 @@ bool SporeModManager::UpdateMods(std::vector<std::filesystem::path> paths, bool
// ensure we only have unique mod names
if (std::find(uniqueNames.begin(), uniqueNames.end(), sporeModInfo.UniqueName) != uniqueNames.end())
{
std::cout << "Skipping " << path << " as it's already being installed!" << std::endl;
std::wcout << L"Skipping " << path << L" as it's already being installed!" << std::endl;
paths.erase(paths.begin() + i);
l_ZipFiles.erase(l_ZipFiles.begin() + i);
l_SporeModInfos.erase(l_SporeModInfos.begin() + i);
Expand Down
2 changes: 1 addition & 1 deletion SporeModManager/SporeModManagerHelpers/Download.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ bool Download::DownloadFile(std::string url, std::filesystem::path path)
{
curl_easy_cleanup(curl);
dlclose(libcurl);
std::cerr << "Error: failed to open " << path << std::endl;
std::wcerr << L"Error: failed to open " << path << std::endl;
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions SporeModManager/SporeModManagerHelpers/FileVersion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ bool FileVersion::ParseFile(std::filesystem::path path, FileVersionInfo& fileVer
fileStream.open(path, std::ios_base::in | std::ios_base::binary);
if (!fileStream.is_open())
{
std::cerr << "Error: failed to open " << path << std::endl;
std::wcerr << L"Error: failed to open " << path << std::endl;
return false;
}

Expand All @@ -143,7 +143,7 @@ bool FileVersion::ParseFile(std::filesystem::path path, FileVersionInfo& fileVer

if (fileStream.fail())
{
std::cerr << "Error: failed to read data from " << path << std::endl;
std::wcerr << L"Error: failed to read data from " << path << std::endl;
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion SporeModManager/SporeModManagerHelpers/Path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ bool Path::CheckIfPathsExist(void)
{
if (!std::filesystem::is_directory(path))
{
std::cerr << "Error: " << path << " is not a directory or doesn't exist!" << std::endl;
std::wcerr << L"Error: " << path << L" is not a directory or doesn't exist!" << std::endl;
return false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion SporeModManager/SporeModManagerHelpers/SporeMod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ bool SporeMod::InstallSporeMod(Zip::ZipFile zipFile, const Xml::InstalledSporeMo

if (UI::GetVerboseMode())
{
std::cout << "--> Installing " << installedFile.FileName << " to " << installPath << std::endl;
std::wcout << L"--> Installing " << installedFile.FileName << L" to " << installPath << std::endl;
}

if (!Zip::ExtractFile(zipFile, sourcePath, installPath))
Expand Down
76 changes: 69 additions & 7 deletions SporeModManager/SporeModManagerHelpers/SporeModXml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,68 @@ using namespace SporeModManagerHelpers;
// Helper Functions
//

// tinyxml2 doesn't support unicode paths for windows,
// it only accepts char* paths, so to support unicode
// we have our own wrappers to open a file using wchar_t
// and then pass that file pointer to tinyxml2

#ifdef _WIN32
#define tinyxml2_mode_str(str) L##str
#else
#define tinyxml2_mode_str(str) str
#endif // _WIN32

#ifdef _WIN32
static FILE* tinyxml2_fopen(const std::filesystem::path& path, const wchar_t* mode)
#else
static FILE* tinyxml2_fopen(const std::filesystem::path& path, const char* mode)
#endif // _WIN32
{
#ifdef _WIN32
FILE* file = nullptr;
errno_t err = _wfopen_s(&file, path.wstring().c_str(), mode);
if (err)
{
return nullptr;
}
return file;
#else
return fopen(path.string().c_str(), mode);
#endif
}

static tinyxml2::XMLError tinyxml2_load_file(tinyxml2::XMLDocument& xmlDocument, const std::filesystem::path& path)
{
tinyxml2::XMLError error;
FILE* fileStream;

fileStream = tinyxml2_fopen(path, tinyxml2_mode_str("rb"));
if (fileStream == nullptr)
{
return tinyxml2::XML_ERROR_FILE_COULD_NOT_BE_OPENED;
}

error = xmlDocument.LoadFile(fileStream);
fclose(fileStream);
return error;
}

static tinyxml2::XMLError tinyxml2_write_file(tinyxml2::XMLDocument& xmlDocument, const std::filesystem::path& path)
{
tinyxml2::XMLError error;
FILE* fileStream;

fileStream = tinyxml2_fopen(path, tinyxml2_mode_str("w"));
if (fileStream == nullptr)
{
return tinyxml2::XML_ERROR_FILE_COULD_NOT_BE_OPENED;
}

error = xmlDocument.SaveFile(fileStream);
fclose(fileStream);
return error;
}

static std::string install_location_to_string(SporeMod::InstallLocation installLocation)
{
switch (installLocation)
Expand Down Expand Up @@ -397,7 +459,7 @@ bool SporeMod::Xml::GetDirectories(std::filesystem::path& coreLibsPath, std::fil
}
}

error = xmlDocument.LoadFile(configFilePath.string().c_str());
error = tinyxml2_load_file(xmlDocument, configFilePath);
if (error != tinyxml2::XMLError::XML_SUCCESS)
{
std::cerr << "Error: failed to load XML file: " << xmlDocument.ErrorName() << std::endl;
Expand Down Expand Up @@ -467,7 +529,7 @@ bool SporeMod::Xml::SaveDirectories(std::filesystem::path coreLibsPath, std::fil

configFilePath = Path::GetConfigFilePath();

error = xmlDocument.LoadFile(configFilePath.string().c_str());
error = tinyxml2_load_file(xmlDocument, configFilePath);
if (error != tinyxml2::XMLError::XML_SUCCESS)
{ // config file doesn't exist
rootXmlElement = xmlDocument.NewElement("SporeModManager");
Expand All @@ -479,7 +541,7 @@ bool SporeMod::Xml::SaveDirectories(std::filesystem::path coreLibsPath, std::fil
xmlElement->InsertNewChildElement("GalacticAdventuresDataDirectory")->SetText(galacticAdventuresDataPath.string().c_str());
xmlElement->InsertNewChildElement("CoreSporeDataDirectory")->SetText(coreSporeDataPath.string().c_str());

xmlDocument.SaveFile(configFilePath.string().c_str());
tinyxml2_write_file(xmlDocument, configFilePath);
return true;
}

Expand Down Expand Up @@ -524,7 +586,7 @@ bool SporeMod::Xml::SaveDirectories(std::filesystem::path coreLibsPath, std::fil
xmlElement = xmlElement->NextSiblingElement();
}

xmlDocument.SaveFile(configFilePath.string().c_str());
tinyxml2_write_file(xmlDocument, configFilePath);
return true;
}

Expand All @@ -544,7 +606,7 @@ bool SporeMod::Xml::GetInstalledModList(std::vector<InstalledSporeMod>& installe
return true;
}

error = xmlDocument.LoadFile(configFilePath.string().c_str());
error = tinyxml2_load_file(xmlDocument, configFilePath);
if (error != tinyxml2::XMLError::XML_SUCCESS)
{
std::cerr << "Error: failed to load XML file: " << xmlDocument.ErrorName() << std::endl;
Expand Down Expand Up @@ -605,7 +667,7 @@ bool SporeMod::Xml::SaveInstalledModList(const std::vector<InstalledSporeMod>& i

configFilePath = Path::GetConfigFilePath();

error = xmlDocument.LoadFile(configFilePath.string().c_str());
error = tinyxml2_load_file(xmlDocument, configFilePath);
if (error != tinyxml2::XMLError::XML_SUCCESS)
{
std::cerr << "Error: failed to load XML file: " << xmlDocument.ErrorName() << std::endl;
Expand Down Expand Up @@ -645,7 +707,7 @@ bool SporeMod::Xml::SaveInstalledModList(const std::vector<InstalledSporeMod>& i
}
}

xmlDocument.SaveFile(configFilePath.string().c_str());
tinyxml2_write_file(xmlDocument, configFilePath);
return true;
}

2 changes: 1 addition & 1 deletion SporeModManager/SporeModManagerHelpers/Zip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ bool Zip::OpenFile(ZipFile& zipFile, std::filesystem::path path)
zipFile = unzOpen2_64((const void*)&path, &filefuncs);
if (zipFile == nullptr)
{
std::cerr << "Error: unzOpen2_64(" << path << ") Failed!" << std::endl;
std::wcerr << L"Error: unzOpen2_64(" << path << L") Failed!" << std::endl;
}
return zipFile != nullptr;
}
Expand Down

0 comments on commit fa27a7f

Please sign in to comment.