From 7c3a35b56d7d86732abcabead0aef6ff923e9b22 Mon Sep 17 00:00:00 2001 From: Jean Pierre Cimalando Date: Sat, 24 Oct 2020 05:31:54 +0200 Subject: [PATCH] Allow LV2 to find resources under Windows --- lv2/sfizz_ui.cpp | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/lv2/sfizz_ui.cpp b/lv2/sfizz_ui.cpp index 70ff76e1..42866eb7 100644 --- a/lv2/sfizz_ui.cpp +++ b/lv2/sfizz_ui.cpp @@ -118,6 +118,26 @@ private: void uiTouch(EditId id, bool t); }; +#if defined(_WIN32) +static bool fixBundlePath(std::string& path) +{ + // go up some directories until reaching the *.lv2 directory + bool valid = false; + while (!valid && !path.empty()) { + if (path.back() == '\\' || path.back() == '/') + path.pop_back(); + else if (path.size() > 4 && !memcmp(".lv2", path.data() + path.size() - 4, 4)) + valid = true; + else { + path.pop_back(); + while (!path.empty() && path.back() != '\\' && path.back() != '/') + path.pop_back(); + } + } + return valid; +} +#endif + static LV2UI_Handle instantiate(const LV2UI_Descriptor *descriptor, const char *plugin_uri, @@ -178,8 +198,16 @@ instantiate(const LV2UI_Descriptor *descriptor, // name, and appending "Contents/Resources" (not overridable) // * on Windows, the folder is set programmatically // * on macOS, resource files are looked up using CFBundle APIs + #if defined(_WIN32) - IWin32PlatformFrame::setResourceBasePath((std::string(bundle_path) + "\\Contents\\Resources\\").c_str()); + // some hosts give us the DLL path instead of the bundle path, + // so we have to work around that. + std::string realBundlePath { bundle_path }; + if (!fixBundlePath(realBundlePath)) + return nullptr; + + std::string resourcePath = realBundlePath + "\\Contents\\Resources\\"; + IWin32PlatformFrame::setResourceBasePath(resourcePath.c_str()); #endif // makes labels refresh correctly