Try and preserve the wideness of std::filesystem::path more (#660)

As noted in #567, a filesystem path that contains non-ascii will break a
lot if using a clang toolchain.

Luckily, fmtlib has a lossy utf8 convert when you use it to print a path
(after including `fmt/std.h`). The vast majority of this diff is just
removing `.string()` from paths inside loggings calls.

There are some callsites I _didn't_ touch, mainly the options, because
it would be an ABI break to change those to be wide strings and I cbf
looking into settings upgrades. There are also some spots (avs mountpath
remapping, for example) where the path is guaranteed to be ascii, so I
didn't modify them.

ImGui doesn't appear to easily support wide strings (I mean, surely it
does, but I'm not gonna look too far into it) so I mostly just left
those alone too, with a few spots modified to re-use fmtlib's lossy
utf8.

Some of the changes are basically never gonna be hit IRL, like who would
put a file with a non-ascii _extension_ along with their modules? But
the diff is (I hope) pretty easy to validate as OK.

Testing has been somewhat minimal, I fired up the GCC build of spice2x
in a dodgy folder name, got mojibake (running via wine in linux so take
that as you will), ran the unmodified clang spice and crashed the same
way the reporter did. After modification, I get the exact same mojibake
so I assume if the terminal enjoys utf8 it'll display OK.

Claude (only used for review) thinks the commit is fine but is annoyed
that I use `fmt::detail` in the appdata censoring, which is part of the
private API; personally I don't care because it's pretty stable.
This commit is contained in:
Will
2026-04-28 13:07:15 +10:00
committed by GitHub
parent 678e11eade
commit 37218e7fe0
18 changed files with 119 additions and 136 deletions
+14 -37
View File
@@ -82,14 +82,14 @@ bool fileutils::verify_header_pe(const std::filesystem::path &file_path) {
if (!valid) {
log_fatal("fileutils",
"{} (32 bit) can't be loaded using spice64.exe - please use spice.exe for this game.",
file_path.string());
file_path);
}
#else
valid = dll_file_header->Machine == IMAGE_FILE_MACHINE_I386;
if (!valid) {
log_fatal("fileutils",
"{} (64 bit) can't be loaded using spice.exe - please use spice64.exe for this game.",
file_path.string());
file_path);
}
#endif
}
@@ -156,9 +156,9 @@ bool fileutils::dir_create_log(const std::string_view &module, const std::filesy
auto ret = std::filesystem::create_directory(dir_path, err);
if (err) {
log_warning(module, "failed to create directory '{}': {}", dir_path.string(), err.message());
log_warning(module, "failed to create directory '{}': {}", dir_path, err.message());
} else if (ret) {
log_misc(module, "created directory '{}'", dir_path.string());
log_misc(module, "created directory '{}'", dir_path);
}
return ret && !err;
@@ -178,39 +178,14 @@ bool fileutils::dir_create_recursive_log(const std::string_view &module, const s
auto ret = std::filesystem::create_directories(dir_path, err);
if (err) {
log_warning(module, "failed to create directory (recursive) '{}': {}", dir_path.string(), err.message());
log_warning(module, "failed to create directory (recursive) '{}': {}", dir_path, err.message());
} else if (ret) {
log_misc(module, "created directory (recursive) '{}'", dir_path.string());
log_misc(module, "created directory (recursive) '{}'", dir_path);
}
return ret && !err;
}
void fileutils::dir_scan(const std::string &path, std::vector<std::string> &vec, bool recursive) {
// check directory
if (std::filesystem::exists(path) && std::filesystem::is_directory(path)) {
if (recursive) {
for (const auto &entry : std::filesystem::recursive_directory_iterator(path)) {
if (!std::filesystem::is_directory(entry)) {
auto path = entry.path().string();
vec.emplace_back(std::move(path));
}
}
} else {
for (const auto &entry : std::filesystem::directory_iterator(path)) {
if (!std::filesystem::is_directory(entry)) {
auto path = entry.path().string();
vec.emplace_back(std::move(path));
}
}
}
}
// determinism
std::sort(vec.begin(), vec.end());
}
bool fileutils::text_write(const std::filesystem::path &file_path, std::string text) {
std::ofstream out(file_path, std::ios::out | std::ios::binary);
if (out) {
@@ -296,22 +271,24 @@ std::filesystem::path fileutils::get_config_file_path(const std::string module,
bool fileutils::write_config_file(const std::string_view &module, const std::filesystem::path path, std::string text) {
// attempt to undo %appdata% expansion to hide user name
const auto appdata = std::filesystem::path(_wgetenv(L"APPDATA")).string();
auto censored = path.string();
const auto appdata = std::filesystem::path(_wgetenv(L"APPDATA")).wstring();
auto censored = path.wstring();
const auto substr_offset = censored.find(appdata);
if (substr_offset != std::string::npos) {
censored.replace(substr_offset, appdata.length(), "%appdata%");
censored.replace(substr_offset, appdata.length(), L"%appdata%");
}
auto censored_display = fmt::detail::to_utf8<wchar_t>(censored, fmt::detail::to_utf8_error_policy::replace);
// create directory path up to where the config file lives
if (!path.parent_path().empty() && !std::filesystem::exists(path.parent_path())) {
log_misc(module, "creating directory path to config file: {}", censored);
log_misc(module, "creating directory path to config file: {}", censored_display);
if (!fileutils::dir_create_recursive(path.parent_path())) {
return false;
}
}
// save file
log_info(module, "saving config file: {}", censored);
log_info(module, "saving config file: {}", censored_display);
return fileutils::text_write(path, text);
}
}
-1
View File
@@ -26,7 +26,6 @@ namespace fileutils {
bool dir_create_log(const std::string_view &module, const std::filesystem::path &dir_path);
bool dir_create_recursive(const std::filesystem::path &dir_path);
bool dir_create_recursive_log(const std::string_view &module, const std::filesystem::path &dir_path);
void dir_scan(const std::string &path, std::vector<std::string> &vec, bool recursive);
// IO
bool text_write(const std::filesystem::path &file_path, std::string text);
+21 -20
View File
@@ -1,5 +1,6 @@
#include "libutils.h"
#include <cstring>
#include <windows.h>
#include <psapi.h>
#include <shlwapi.h>
@@ -38,7 +39,7 @@ std::filesystem::path libutils::module_file_name(HMODULE module) {
return std::filesystem::path(std::move(buf));
}
static inline void load_library_fail(const std::string &file_name, bool fatal) {
static inline void load_library_fail(const std::filesystem::path &file_name, bool fatal) {
std::string info_str { fmt::format(
"DLL failed to load - this is a common error. Please carefully read ALL of the following steps for a fix:\n"
" 1. Confirm if the file ({}) exists on the disk and check the file permissions.\n"
@@ -76,9 +77,9 @@ HMODULE libutils::load_library(const std::filesystem::path &path, bool fatal) {
HMODULE module = LoadLibraryW(path.c_str());
if (!module) {
log_warning("libutils", "'{}' couldn't be loaded: {}", path.string(), get_last_error_string());
log_warning("libutils", "'{}' couldn't be loaded: {}", path, get_last_error_string());
dependencies::walk(path);
load_library_fail(path.filename().string(), fatal);
load_library_fail(path.filename(), fatal);
}
return module;
@@ -236,7 +237,7 @@ intptr_t libutils::rva2offset(const std::filesystem::path &path, intptr_t rva) {
HANDLE dll_mapping = CreateFileMappingW(dll_file, nullptr, PAGE_READONLY, 0, 0, nullptr);
if (!dll_mapping) {
CloseHandle(dll_file);
log_warning("libutils", "could not create file mapping for {}", path.string());
log_warning("libutils", "could not create file mapping for {}", path);
return -1;
}
@@ -245,7 +246,7 @@ intptr_t libutils::rva2offset(const std::filesystem::path &path, intptr_t rva) {
if (!dll_file_base) {
CloseHandle(dll_file);
CloseHandle(dll_mapping);
log_warning("libutils", "could not map view of file for {}", path.string());
log_warning("libutils", "could not map view of file for {}", path);
return -1;
}
@@ -306,7 +307,7 @@ intptr_t libutils::offset2rva(const std::filesystem::path &path, intptr_t offset
// create file mapping
HANDLE dll_mapping = CreateFileMappingW(dll_file, nullptr, PAGE_READONLY, 0, 0, nullptr);
if (!dll_mapping) {
log_warning("libutils", "could not create file mapping for {}: {}", path.string(), get_last_error_string());
log_warning("libutils", "could not create file mapping for {}: {}", path, get_last_error_string());
CloseHandle(dll_file);
return -1;
}
@@ -314,7 +315,7 @@ intptr_t libutils::offset2rva(const std::filesystem::path &path, intptr_t offset
// map view of file
LPVOID dll_file_base = MapViewOfFile(dll_mapping, FILE_MAP_READ, 0, 0, 0);
if (!dll_file_base) {
log_warning("libutils", "could not map view of file for {}: {}", path.string(), get_last_error_string());
log_warning("libutils", "could not map view of file for {}: {}", path, get_last_error_string());
CloseHandle(dll_file);
CloseHandle(dll_mapping);
return -1;
@@ -344,9 +345,9 @@ void libutils::check_duplicate_dlls() {
for (const auto &file : std::filesystem::directory_iterator(MODULE_PATH)) {
const auto &filename = file.path().filename();
const auto extension = strtolower(filename.extension().string());
const auto extension = filename.extension().wstring();
if (extension == ".dll" &&
if (wcsicmp(extension.c_str(), L".dll") == 0 &&
fileutils::file_exists(spice_bin_path / filename)) {
log_warning(
"libutils",
@@ -361,9 +362,9 @@ void libutils::check_duplicate_dlls() {
"this has unintended consequences and may crash your game!\n"
"resolve the conflict by deleting the stale copy of the DLL\n"
"-------------------------------------------------------------------\n\n\n",
filename.string(),
(spice_bin_path / filename).string(),
(MODULE_PATH / filename).string());
filename,
(spice_bin_path / filename),
(MODULE_PATH / filename));
}
}
}
@@ -384,22 +385,22 @@ void libutils::warn_if_dll_exists(const std::string &file_name) {
void libutils::print_dll_info(std::filesystem::path filename) {
DWORD handle;
const auto size = GetFileVersionInfoSizeA(filename.string().c_str(), &handle);
const auto size = GetFileVersionInfoSizeW(filename.wstring().c_str(), &handle);
if (size == 0) {
log_debug(
"libutils",
"GetFileVersionInfoSizeA failed for {}: {}",
filename.filename().string(),
filename.filename(),
get_last_error_string());
return;
}
auto data = util::make_unique_plain<VOID>(size);
if (!GetFileVersionInfoA(filename.string().c_str(), handle, size, data.get())) {
if (!GetFileVersionInfoW(filename.wstring().c_str(), handle, size, data.get())) {
log_debug(
"libutils",
"GetFileVersionInfoA failed for {}: {}",
filename.filename().string(),
filename.filename(),
get_last_error_string());
return;
}
@@ -414,7 +415,7 @@ void libutils::print_dll_info(std::filesystem::path filename) {
log_debug(
"libutils",
"VerQueryValueA failed for {}: {}",
filename.filename().string(),
filename.filename(),
get_last_error_string());
return;
}
@@ -422,7 +423,7 @@ void libutils::print_dll_info(std::filesystem::path filename) {
log_debug(
"libutils",
"VerQueryValueA returned invalid results for {}",
filename.filename().string());
filename.filename());
return;
}
@@ -457,7 +458,7 @@ void libutils::print_dll_info(std::filesystem::path filename) {
"libutils",
"VerQueryValueA({}) failed for {}: {}",
subBlock,
filename.filename().string(),
filename.filename(),
get_last_error_string());
return "";
};
@@ -469,7 +470,7 @@ void libutils::print_dll_info(std::filesystem::path filename) {
log_info(
"libutils",
"DLL info for {}: CompanyName = {}, ProductName = {}, Version = {}",
filename.filename().string(),
filename.filename(),
company_name.empty() ? "?" : company_name,
product_name.empty() ? "?" : product_name,
version_str.empty() ? "?" : version_str);
+2 -1
View File
@@ -10,6 +10,7 @@
#include "external/fmt/include/fmt/format.h"
#include "external/fmt/include/fmt/compile.h"
#include "external/fmt/include/fmt/std.h"
#include "launcher/launcher.h"
#include "launcher/logger.h"
@@ -75,7 +76,7 @@ void show_popup_for_fatal_error(std::string message);
// misc log
#define LOG_FORMAT(level, module, fmt_str, ...) fmt::format(FMT_COMPILE("{}" fmt_str "\n"), \
fmt_log { std::time(nullptr), level, module }, ## __VA_ARGS__)
#define LOG_FORMAT_POPUP(module, fmt_str, ...) fmt::format(FMT_COMPILE("{}: " fmt_str "\n"), module, ## __VA_ARGS__)
#define log_misc(module, format_str, ...) logger::push( \
+3 -3
View File
@@ -275,7 +275,7 @@ intptr_t replace_pattern(HMODULE module, const std::string &signature,
bool get_pe_identifier(const std::filesystem::path& dll_path, uint32_t* time_date_stamp, uint32_t* address_of_entry_point) {
std::ifstream file(dll_path, std::ios::binary);
if (!file) {
log_warning("sigscan", "Failed to open file: {}", dll_path.string().c_str());
log_warning("sigscan", "Failed to open file: {}", dll_path);
return false;
}
@@ -283,7 +283,7 @@ bool get_pe_identifier(const std::filesystem::path& dll_path, uint32_t* time_dat
IMAGE_DOS_HEADER dos_header;
file.read(reinterpret_cast<char*>(&dos_header), sizeof(dos_header));
if (dos_header.e_magic != IMAGE_DOS_SIGNATURE) {
log_warning("sigscan", "Invalid DOS signature: {}", dll_path.string().c_str());
log_warning("sigscan", "Invalid DOS signature: {}", dll_path);
return false;
}
@@ -294,7 +294,7 @@ bool get_pe_identifier(const std::filesystem::path& dll_path, uint32_t* time_dat
IMAGE_NT_HEADERS nt_headers;
file.read(reinterpret_cast<char*>(&nt_headers), sizeof(nt_headers));
if (nt_headers.Signature != IMAGE_NT_SIGNATURE) {
log_warning("sigscan", "Invalid NT signature: {}", dll_path.string().c_str());
log_warning("sigscan", "Invalid NT signature: {}", dll_path);
return false;
}