From 7a78e7627f6d92fe3d2cc03f8c783d166e196521 Mon Sep 17 00:00:00 2001 From: Jean Pierre Cimalando Date: Fri, 9 Apr 2021 00:52:17 +0200 Subject: [PATCH] Utility to get the resource path of the plugin bundle --- plugins/editor/CMakeLists.txt | 5 ++- plugins/editor/src/editor/Editor.cpp | 4 +++ plugins/editor/src/editor/VSTGUIHelpers.cpp | 35 +++++++++++++++++++++ plugins/editor/src/editor/VSTGUIHelpers.h | 10 ++++++ plugins/editor/src/editor/VSTGUIHelpers.mm | 26 +++++++++++++++ 5 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 plugins/editor/src/editor/VSTGUIHelpers.cpp create mode 100644 plugins/editor/src/editor/VSTGUIHelpers.h create mode 100644 plugins/editor/src/editor/VSTGUIHelpers.mm diff --git a/plugins/editor/CMakeLists.txt b/plugins/editor/CMakeLists.txt index fa2e3b5d..62522b4e 100644 --- a/plugins/editor/CMakeLists.txt +++ b/plugins/editor/CMakeLists.txt @@ -56,6 +56,8 @@ add_library(sfizz_editor STATIC EXCLUDE_FROM_ALL src/editor/ImageHelpers.cpp src/editor/NativeHelpers.h src/editor/NativeHelpers.cpp + src/editor/VSTGUIHelpers.h + src/editor/VSTGUIHelpers.cpp src/editor/layout/main.hpp src/editor/layout/about.hpp src/editor/utility/vstgui_after.h @@ -74,7 +76,8 @@ if(APPLE) find_library(APPLE_CORESERVICES_LIBRARY "CoreServices") find_library(APPLE_FOUNDATION_LIBRARY "Foundation") target_sources(sfizz_editor PRIVATE - src/editor/NativeHelpers.mm) + src/editor/NativeHelpers.mm + src/editor/VSTGUIHelpers.mm) target_link_libraries(sfizz_editor PRIVATE "${APPLE_APPKIT_LIBRARY}" "${APPLE_CORESERVICES_LIBRARY}" diff --git a/plugins/editor/src/editor/Editor.cpp b/plugins/editor/src/editor/Editor.cpp index 4c9af9e7..85a67f06 100644 --- a/plugins/editor/src/editor/Editor.cpp +++ b/plugins/editor/src/editor/Editor.cpp @@ -13,6 +13,7 @@ #include "DlgAbout.h" #include "ImageHelpers.h" #include "NativeHelpers.h" +#include "VSTGUIHelpers.h" #include "BitArray.h" #include "plugin/MessageUtils.h" #include @@ -274,6 +275,9 @@ void Editor::open(CFrame& frame) { Impl& impl = *impl_; + fprintf(stderr, "[sfizz] The resource path of the bundle is %s\n", + getResourceBasePath().u8string().c_str()); + impl.frame_ = &frame; frame.addView(impl.mainView_.get()); diff --git a/plugins/editor/src/editor/VSTGUIHelpers.cpp b/plugins/editor/src/editor/VSTGUIHelpers.cpp new file mode 100644 index 00000000..2e7d41d7 --- /dev/null +++ b/plugins/editor/src/editor/VSTGUIHelpers.cpp @@ -0,0 +1,35 @@ +// SPDX-License-Identifier: BSD-2-Clause + +// This code is part of the sfizz library and is licensed under a BSD 2-clause +// license. You should have receive a LICENSE.md file along with the code. +// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz + +#include "VSTGUIHelpers.h" +#include "utility/vstgui_before.h" +#include "vstgui/lib/platform/platformfactory.h" +#if defined(_WIN32) +# include "vstgui/lib/platform/win32/win32factory.h" +#elif defined(__APPLE__) +#else +# include "vstgui/lib/platform/linux/linuxfactory.h" +#endif +#include "utility/vstgui_after.h" + +using namespace VSTGUI; + +#if defined(_WIN32) +fs::path getResourceBasePath() +{ + Optional optionalPath = getPlatformFactory().asWin32Factory()->getResourceBasePath(); + if (!optionalPath) + return fs::path(); + return fs::u8path(optionalPath->getString()); +} +#elif defined(__APPLE__) + // implemented in VSTGUIHelpers.mm +#else +fs::path getResourceBasePath() +{ + return fs::u8path(getPlatformFactory().asLinuxFactory()->getResourcePath()); +} +#endif diff --git a/plugins/editor/src/editor/VSTGUIHelpers.h b/plugins/editor/src/editor/VSTGUIHelpers.h new file mode 100644 index 00000000..01c8ef52 --- /dev/null +++ b/plugins/editor/src/editor/VSTGUIHelpers.h @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: BSD-2-Clause + +// This code is part of the sfizz library and is licensed under a BSD 2-clause +// license. You should have receive a LICENSE.md file along with the code. +// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz + +#pragma once +#include + +fs::path getResourceBasePath(); diff --git a/plugins/editor/src/editor/VSTGUIHelpers.mm b/plugins/editor/src/editor/VSTGUIHelpers.mm new file mode 100644 index 00000000..7f753a2b --- /dev/null +++ b/plugins/editor/src/editor/VSTGUIHelpers.mm @@ -0,0 +1,26 @@ +// SPDX-License-Identifier: BSD-2-Clause + +// This code is part of the sfizz library and is licensed under a BSD 2-clause +// license. You should have receive a LICENSE.md file along with the code. +// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz + +#include "VSTGUIHelpers.h" + +#if defined(__APPLE__) +#include "vstgui/lib/platform/mac/macfactory.h" +#import +#import + +fs::path getResourceBasePath() +{ + CFBundleRef bundle = VSTGUI::getPlatformFactory().asMacFactory()->getBundle(); + if (!bundle) + return fs::path(); + + NSURL* url = (__bridge_transfer NSURL*)CFBundleCopyResourcesDirectoryURL(bundle); + if (!url || ![url isFileURL]) + return fs::path(); + + return fs::u8path([[url path] UTF8String]); +} +#endif