Skip to content

Commit

Permalink
Merge pull request #1210 from geode-sdk/file-serialize-utf16
Browse files Browse the repository at this point in the history
Implement UTF conversion for matjson::Serialize<std::filesystem::path>
  • Loading branch information
Fleeym authored Jan 7, 2025
2 parents 17107a2 + 42ece10 commit 591a9b7
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions loader/include/Geode/utils/file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,37 @@

#include <matjson.hpp>
#include <Geode/DefaultInclude.hpp>
#include <Geode/utils/string.hpp>
#include <filesystem>
#include <string>
#include <unordered_set>

template <>
struct matjson::Serialize<std::filesystem::path> {
static geode::Result<std::filesystem::path, std::string> fromJson(Value const& value)
{
GEODE_UNWRAP_INTO(auto str, value.asString());
static geode::Result<std::filesystem::path> fromJson(matjson::Value const& value) {
GEODE_UNWRAP_INTO(const std::string str, value.asString());

#ifdef GEODE_IS_WINDOWS
// On Windows, paths are stored as utf16, and matjson uses utf8 internally
// This is not an issue until paths actually use unicode characters
// So we do this conversion to make sure it stores the characters correctly
return geode::Ok(
std::filesystem::path(geode::utils::string::utf8ToWide(str)).make_preferred()
);
#else
return geode::Ok(std::filesystem::path(str).make_preferred());
#endif
}

static Value toJson(std::filesystem::path const& value)
{
return Value(value.string());
static matjson::Value toJson(std::filesystem::path const& value) {
#ifdef GEODE_IS_WINDOWS
// On Windows, paths are stored as utf16, and matjson uses utf8 internally
// This is not an issue until paths actually use unicode characters
// So we do this conversion to make sure it stores the characters correctly
return matjson::Value(geode::utils::string::wideToUtf8(value.wstring()));
#else
return matjson::Value(value.string());
#endif
}
};

Expand Down

0 comments on commit 591a9b7

Please sign in to comment.