Fix encoding error in input profile filenames

- Controller profile filenames now encode unicode characters correctly
- Removed dependency on boost::filesystem. There is still an indirect dependency on it from another boost module it seems
- Refactored some code to use FileStream instead of ifstream/ofstream
This commit is contained in:
Exzap 2022-10-12 14:18:24 +02:00
parent 8b3f36ad50
commit f65dbe8437
4 changed files with 44 additions and 29 deletions

View File

@ -277,10 +277,8 @@ bool GameProfile::Load(uint64_t title_id)
void GameProfile::Save(uint64_t title_id) void GameProfile::Save(uint64_t title_id)
{ {
auto gameProfileDir = ActiveSettings::GetConfigPath("gameProfiles"); auto gameProfileDir = ActiveSettings::GetConfigPath("gameProfiles");
if (std::error_code ex_ec; !fs::exists(gameProfileDir, ex_ec) && !ex_ec) { if (std::error_code ex_ec; !fs::exists(gameProfileDir, ex_ec))
std::error_code cr_ec; fs::create_directories(gameProfileDir, ex_ec);
fs::create_directories(gameProfileDir, cr_ec);
}
auto gameProfilePath = gameProfileDir / fmt::format("{:016x}.ini", title_id); auto gameProfilePath = gameProfileDir / fmt::format("{:016x}.ini", title_id);
FileStream* fs = FileStream::createFile2(gameProfilePath); FileStream* fs = FileStream::createFile2(gameProfilePath);
if (!fs) if (!fs)
@ -309,16 +307,11 @@ void GameProfile::Save(uint64_t title_id)
fs->writeLine(""); fs->writeLine("");
fs->writeLine("[Graphics]"); fs->writeLine("[Graphics]");
//WRITE_OPTIONAL_ENTRY(gpuBufferCacheAccuracy);
WRITE_ENTRY(accurateShaderMul); WRITE_ENTRY(accurateShaderMul);
WRITE_OPTIONAL_ENTRY(precompiledShaders); WRITE_OPTIONAL_ENTRY(precompiledShaders);
WRITE_OPTIONAL_ENTRY(graphics_api); WRITE_OPTIONAL_ENTRY(graphics_api);
fs->writeLine(""); fs->writeLine("");
/*stream_writeLine(stream_gameProfile, "[Audio]");
WRITE_ENTRY(disableAudio);
stream_writeLine(stream_gameProfile, "");*/
fs->writeLine("[Controller]"); fs->writeLine("[Controller]");
for (int i = 0; i < 8; ++i) for (int i = 0; i < 8; ++i)
{ {

View File

@ -69,7 +69,6 @@
#include <boost/nowide/convert.hpp> #include <boost/nowide/convert.hpp>
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <boost/filesystem.hpp>
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <glm/gtc/quaternion.hpp> #include <glm/gtc/quaternion.hpp>

View File

@ -92,12 +92,12 @@ bool InputManager::load(size_t player_index, std::string_view filename)
try try
{ {
std::ifstream file(file_path); auto xmlData = FileStream::LoadIntoMemory(file_path);
if (!file.is_open()) if (!xmlData || xmlData->empty())
return false; return false;
pugi::xml_document doc; pugi::xml_document doc;
if (!doc.load(file)) if (!doc.load_buffer(xmlData->data(), xmlData->size()))
return false; return false;
const pugi::xml_node root = doc.document_element(); const pugi::xml_node root = doc.document_element();
@ -216,12 +216,15 @@ bool InputManager::migrate_config(const fs::path& file_path)
{ {
try try
{ {
std::ifstream file(file_path); auto xmlData = FileStream::LoadIntoMemory(file_path);
if (!file.is_open()) if (!xmlData || xmlData->empty())
return false; return false;
std::string iniDataStr((const char*)xmlData->data(), xmlData->size());
std::stringstream iniData(iniDataStr);
boost::property_tree::ptree m_data; boost::property_tree::ptree m_data;
read_ini(file, m_data); read_ini(iniData, m_data);
const auto emulate_string = m_data.get<std::string>("General.emulate"); const auto emulate_string = m_data.get<std::string>("General.emulate");
const auto api_string = m_data.get<std::string>("General.api"); const auto api_string = m_data.get<std::string>("General.api");
@ -455,7 +458,7 @@ bool InputManager::save(size_t player_index, std::string_view filename)
if (is_default_file) if (is_default_file)
file_path /= fmt::format("controller{}", player_index); file_path /= fmt::format("controller{}", player_index);
else else
file_path /= filename; file_path /= _utf8ToPath(filename);
file_path.replace_extension(".xml"); // force .xml extension file_path.replace_extension(".xml"); // force .xml extension
@ -540,15 +543,15 @@ bool InputManager::save(size_t player_index, std::string_view filename)
} }
} }
} }
FileStream* fs = FileStream::createFile2(file_path);
if (!fs)
std::ofstream file(file_path, std::ios::out | std::ios::trunc); return false;
if (file.is_open()) std::stringstream xmlData;
{ doc.save(xmlData);
doc.save(file); std::string xmlStr = xmlData.str();
return true; fs->writeData(xmlStr.data(), xmlStr.size());
} delete fs;
return false; return true;
} }
bool InputManager::is_gameprofile_set(size_t player_index) const bool InputManager::is_gameprofile_set(size_t player_index) const
@ -792,7 +795,7 @@ std::vector<std::string> InputManager::get_profiles()
const auto& p = entry.path(); const auto& p = entry.path();
if (p.has_extension() && (p.extension() == ".xml" || p.extension() == ".txt")) if (p.has_extension() && (p.extension() == ".xml" || p.extension() == ".txt"))
{ {
auto stem = p.filename().stem().string(); auto stem = _pathToUtf8(p.filename().stem());
if (is_valid_profilename(stem)) if (is_valid_profilename(stem))
{ {
tmp.emplace(stem); tmp.emplace(stem);
@ -808,7 +811,7 @@ std::vector<std::string> InputManager::get_profiles()
bool InputManager::is_valid_profilename(const std::string& name) bool InputManager::is_valid_profilename(const std::string& name)
{ {
if (!boost::filesystem::windows_name(name)) if (!IsValidFilename(name))
return false; return false;
// dont allow default profile names // dont allow default profile names

View File

@ -231,6 +231,26 @@ inline uint64 MakeU64(uint32 high, uint32 low)
return ((uint64)high << 32) | ((uint64)low); return ((uint64)high << 32) | ((uint64)low);
} }
static bool IsValidFilename(std::string_view sv)
{
for (auto& it : sv)
{
uint8 c = (uint8)it;
if (c < 0x20)
return false;
if (c == '.' || c == '#' || c == '/' || c == '\\' ||
c == '<' || c == '>' || c == '|' || c == ':' ||
c == '\"')
return false;
}
if (!sv.empty())
{
if (sv.back() == ' ' || sv.back() == '.')
return false;
}
return true;
}
// MAJOR; MINOR // MAJOR; MINOR
std::pair<DWORD, DWORD> GetWindowsVersion(); std::pair<DWORD, DWORD> GetWindowsVersion();
bool IsWindows81OrGreater(); bool IsWindows81OrGreater();