Skip to content

Commit

Permalink
Wrap json_load_file for UTF-8 support on Windows
Browse files Browse the repository at this point in the history
Faceplates aren't loading if the RACK_USER_DIR has non-ASCII
characters in it. See:
https://community.vcvrack.com/t/rarebreeds-eugene-not-working/20266

Resolves issue #4.
  • Loading branch information
RareBreeds committed Jul 29, 2023
1 parent 09fe998 commit f344603
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/OrbitsConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ OrbitsConfig::OrbitsConfig(std::string path)
m_path = path;
}

static json_t *orbits_json_load_file(const char *path, size_t flags, json_error_t *error)
{
// json_load_file passes the input path to fopen, which doesn't support utf-8 paths
// on Windows. VCV Rack has worked around this by overriding fopen in common.hpp.
FILE* file = std::fopen(path, "rb");
json_t *json = json_loadf(file, flags, error);
if(file)
{
std::fclose(file);
}
return json;
}

float OrbitsConfig::rFindFloatAttribute(std::string &content, std::string attribute, size_t search)
{
search = content.rfind(attribute + "=", search);
Expand All @@ -33,7 +46,7 @@ std::string OrbitsConfig::getSvg(std::string component, int theme)
{
json_error_t error;
std::string path = asset::plugin(pluginInstance, m_path);
json_t *root = json_load_file(path.c_str(), 0, &error);
json_t *root = orbits_json_load_file(path.c_str(), 0, &error);
json_t *themes = json_object_get(root, "themes");
json_t *entry = json_array_get(themes, theme);
json_t *obj = json_object_get(entry, component.c_str());
Expand Down Expand Up @@ -86,7 +99,7 @@ std::string OrbitsConfig::getThemeName(int theme)
{
json_error_t error;
std::string path = asset::plugin(pluginInstance, m_path);
json_t *root = json_load_file(path.c_str(), 0, &error);
json_t *root = orbits_json_load_file(path.c_str(), 0, &error);
json_t *themes = json_object_get(root, "themes");
json_t *entry = json_array_get(themes, theme);
json_t *name = json_object_get(entry, "name");
Expand All @@ -106,7 +119,7 @@ int OrbitsConfig::getDefaultThemeId()
int default_theme_id = 0;
json_error_t error;
std::string path = asset::plugin(pluginInstance, m_path);
json_t *root = json_load_file(path.c_str(), 0, &error);
json_t *root = orbits_json_load_file(path.c_str(), 0, &error);
json_t *def = json_object_get(root, "default");
const char *default_name = json_string_value(def);
json_t *themes = json_object_get(root, "themes");
Expand All @@ -131,7 +144,7 @@ size_t OrbitsConfig::numThemes()
{
json_error_t error;
std::string path = asset::plugin(pluginInstance, m_path);
json_t *root = json_load_file(path.c_str(), 0, &error);
json_t *root = orbits_json_load_file(path.c_str(), 0, &error);
json_t *themes = json_object_get(root, "themes");
size_t count = json_array_size(themes);
json_decref(root);
Expand All @@ -142,7 +155,7 @@ std::array<uint8_t, 3> OrbitsConfig::getColour(std::string component, int theme)
{
json_error_t error;
std::string path = asset::plugin(pluginInstance, m_path);
json_t *root = json_load_file(path.c_str(), 0, &error);
json_t *root = orbits_json_load_file(path.c_str(), 0, &error);
json_t *themes = json_object_get(root, "themes");
json_t *entry = json_array_get(themes, theme);
json_t *obj = json_object_get(entry, component.c_str());
Expand Down

0 comments on commit f344603

Please sign in to comment.