1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-27 22:49:25 +02:00

fix Chinese encoding problem in GBK

This commit is contained in:
kdmcser
2025-04-19 13:30:31 +08:00
parent 975d9aedc9
commit ea3947df4f
3 changed files with 31 additions and 8 deletions

View File

@@ -14,6 +14,8 @@
#include "../ExceptionsCommon.h"
#include <boost/locale/encoding_utf.hpp>
VCMI_LIB_NAMESPACE_BEGIN
CFilesystemLoader::CFilesystemLoader(std::string _mountPoint, boost::filesystem::path baseDirectory, size_t depth, bool initial):
@@ -91,20 +93,21 @@ bool CFilesystemLoader::createResource(const std::string & requestedFilename, bo
}
filename = filename.substr(mountPoint.size());
std::wstring filePath = boost::locale::conv::utf_to_utf<wchar_t>(filename);
if (!update)
{
// create folders if not exists
boost::filesystem::path p((baseDirectory / filename).c_str());
boost::filesystem::path p((baseDirectory / filePath).c_str());
boost::filesystem::create_directories(p.parent_path());
// create file, if not exists
std::ofstream file((baseDirectory / filename).c_str(), std::ofstream::binary);
std::ofstream file((baseDirectory / filePath).c_str(), std::ofstream::binary);
if (!file.is_open())
return false;
}
fileList[resID] = filename;
fileList[resID] = filePath;
return true;
}
@@ -173,19 +176,26 @@ std::unordered_map<ResourcePath, boost::filesystem::path> CFilesystemLoader::lis
filename = it->path().filename();
std::string resName;
#ifdef VCMI_WINDOWS
std::string filenameUtf8 = boost::locale::conv::utf_to_utf<char>(filename.native());
#else
std::string filenameUtf8 = filename.string();
#endif
if (boost::filesystem::path::preferred_separator != '/')
{
// resource names are using UNIX slashes (/)
resName.reserve(resName.size() + filename.native().size());
resName = mountPoint;
for (const char c : filename.string())
for (const char c : filenameUtf8)
if (c != boost::filesystem::path::preferred_separator)
resName.push_back(c);
else
resName.push_back('/');
}
else
resName = mountPoint + filename.string();
resName = mountPoint + filenameUtf8;
fileList[ResourcePath(resName, type)] = std::move(filename);
}

View File

@@ -27,6 +27,8 @@
#include "../IGameSettings.h"
#include "../CConfigHandler.h"
#include <boost/locale/encoding_utf.hpp>
VCMI_LIB_NAMESPACE_BEGIN
CMapInfo::CMapInfo()
@@ -40,13 +42,23 @@ CMapInfo::~CMapInfo()
vstd::clear_pointer(scenarioOptionsOfSave);
}
std::string CMapInfo::getFullFileURI(const ResourcePath & file) const
{
auto path = boost::filesystem::canonical(*CResourceHandler::get()->getResourceName(file));
#ifdef VCMI_WINDOWS
return boost::locale::conv::utf_to_utf<char>(path.native());
#else
return path.string();
#endif
}
void CMapInfo::mapInit(const std::string & fname)
{
fileURI = fname;
CMapService mapService;
ResourcePath resource = ResourcePath(fname, EResType::MAP);
originalFileURI = resource.getOriginalName();
fullFileURI = boost::filesystem::canonical(*CResourceHandler::get()->getResourceName(resource)).string();
fullFileURI = getFullFileURI(resource);
mapHeader = mapService.loadMapHeader(resource);
lastWrite = boost::filesystem::last_write_time(*CResourceHandler::get()->getResourceName(resource));
date = TextOperations::getFormattedDateTimeLocal(lastWrite);
@@ -62,7 +74,7 @@ void CMapInfo::saveInit(const ResourcePath & file)
lf >> *(mapHeader) >> scenarioOptionsOfSave;
fileURI = file.getName();
originalFileURI = file.getOriginalName();
fullFileURI = boost::filesystem::canonical(*CResourceHandler::get()->getResourceName(file)).string();
fullFileURI = getFullFileURI(file);
countPlayers();
lastWrite = boost::filesystem::last_write_time(*CResourceHandler::get()->getResourceName(file));
date = TextOperations::getFormattedDateTimeLocal(lastWrite);
@@ -76,7 +88,7 @@ void CMapInfo::campaignInit()
{
ResourcePath resource = ResourcePath(fileURI, EResType::CAMPAIGN);
originalFileURI = resource.getOriginalName();
fullFileURI = boost::filesystem::canonical(*CResourceHandler::get()->getResourceName(resource)).string();
fullFileURI = getFullFileURI(resource);
campaign = CampaignHandler::getHeader(fileURI);
lastWrite = boost::filesystem::last_write_time(*CResourceHandler::get()->getResourceName(resource));
date = TextOperations::getFormattedDateTimeLocal(lastWrite);

View File

@@ -48,6 +48,7 @@ public:
CMapInfo &operator=(CMapInfo &&other) = delete;
CMapInfo &operator=(const CMapInfo &other) = delete;
std::string CMapInfo::getFullFileURI(const ResourcePath& file) const;
void mapInit(const std::string & fname);
void saveInit(const ResourcePath & file);
void campaignInit();