diff --git a/src/FontCache.cc b/src/FontCache.cc index 1d2546f8..29599e32 100644 --- a/src/FontCache.cc +++ b/src/FontCache.cc @@ -90,7 +90,7 @@ FontCache::FontCache() // If we've got a bundled fonts.conf, initialize fontconfig with our own config // by overriding the built-in fontconfig path. // For system installs and dev environments, we leave this alone - fs::path fontdir(fs::path(PlatformUtils::resourcesPath()) / "fonts"); + fs::path fontdir(PlatformUtils::resourcePath("fonts")); if (fs::is_regular_file(fontdir / "fonts.conf")) { PlatformUtils::setenv("FONTCONFIG_PATH", boosty::stringy(boosty::absolute(fontdir)).c_str(), 0); } @@ -103,7 +103,7 @@ FontCache::FontCache() } // Add the built-in fonts & config - fs::path builtinfontpath = fs::path(PlatformUtils::resourcesPath()) / "fonts"; + fs::path builtinfontpath(PlatformUtils::resourcePath("fonts")); if (fs::is_directory(builtinfontpath)) { FcConfigParseAndLoad(this->config, reinterpret_cast(boosty::stringy(builtinfontpath).c_str()), false); add_font_dir(boosty::stringy(boosty::canonical(builtinfontpath))); diff --git a/src/PlatformUtils.cc b/src/PlatformUtils.cc index a7f6c61a..97b237ab 100644 --- a/src/PlatformUtils.cc +++ b/src/PlatformUtils.cc @@ -1,7 +1,6 @@ #include #include "PlatformUtils.h" -#include "boosty.h" #include #ifdef USE_SCINTILLA_EDITOR #include @@ -97,7 +96,7 @@ bool PlatformUtils::createBackupPath() } // This is the built-in read-only resources path -std::string PlatformUtils::resourcesPath() +std::string PlatformUtils::resourceBasePath() { fs::path resourcedir(applicationPath()); fs::path tmpdir; @@ -130,6 +129,21 @@ std::string PlatformUtils::resourcesPath() return boosty::stringy(boosty::canonical(resourcedir)); } +fs::path PlatformUtils::resourcePath(const std::string &resource) +{ + fs::path base(resourceBasePath()); + if (!fs::is_directory(base)) { + return fs::path(); + } + + fs::path resource_dir = base / resource; + if (!fs::is_directory(resource_dir)) { + return fs::path(); + } + + return resource_dir; +} + int PlatformUtils::setenv(const char *name, const char *value, int overwrite) { #if defined(WIN32) diff --git a/src/PlatformUtils.h b/src/PlatformUtils.h index ca28134c..1dd7feac 100644 --- a/src/PlatformUtils.h +++ b/src/PlatformUtils.h @@ -2,13 +2,16 @@ #include +#include "boosty.h" + namespace PlatformUtils { void registerApplicationPath(const std::string &applicationpath); std::string applicationPath(); std::string documentsPath(); - std::string resourcesPath(); + std::string resourceBasePath(); + fs::path resourcePath(const std::string& resource); std::string userLibraryPath(); bool createUserLibraryPath(); std::string backupPath(); diff --git a/src/UIUtils.cc b/src/UIUtils.cc index 58241ef6..3e8c8c01 100644 --- a/src/UIUtils.cc +++ b/src/UIUtils.cc @@ -89,8 +89,8 @@ QStringList UIUtils::exampleCategories() QFileInfoList UIUtils::exampleFiles(const QString &category) { - QDir dir(QString::fromStdString(PlatformUtils::resourcesPath())); - if (!dir.cd("examples") || !dir.cd(category)) { + QDir dir(QString::fromStdString(PlatformUtils::resourcePath("examples").string())); + if (!dir.cd(category)) { return QFileInfoList(); } diff --git a/src/openscad.cc b/src/openscad.cc index b65ed057..b3515eaf 100644 --- a/src/openscad.cc +++ b/src/openscad.cc @@ -164,7 +164,7 @@ void localization_init() { #ifdef LOCALE_PREFIX std::string locale_path(LOCALE_PREFIX); #else - fs::path po_dir = get_resource_dir("locale"); + fs::path po_dir(PlatformUtils::resourcePath("locale")); std::string locale_path(po_dir.string()); #endif if (fs::is_directory(locale_path)) { @@ -279,7 +279,7 @@ int cmdline(const char *deps_output_file, const std::string &filename, Camera &c const std::string application_path = boosty::stringy(boosty::absolute(boost::filesystem::path(argv[0]).parent_path())); #endif PlatformUtils::registerApplicationPath(application_path); - parser_init(PlatformUtils::applicationPath()); + parser_init(); localization_init(); Tree tree; @@ -578,7 +578,7 @@ int gui(vector &inputFiles, const fs::path &original_path, int argc, cha const QString &app_path = app.applicationDirPath(); PlatformUtils::registerApplicationPath(app_path.toLocal8Bit().constData()); - parser_init(PlatformUtils::applicationPath()); + parser_init(); QSettings settings; if (settings.value("advanced/localization", true).toBool()) { diff --git a/src/parsersettings.cc b/src/parsersettings.cc index b47beccc..6e3f06bb 100644 --- a/src/parsersettings.cc +++ b/src/parsersettings.cc @@ -7,7 +7,6 @@ namespace fs = boost::filesystem; -static std::string applicationdir; std::vector librarypath; static void add_librarydir(const std::string &libdir) @@ -15,47 +14,6 @@ static void add_librarydir(const std::string &libdir) librarypath.push_back(libdir); } -fs::path get_resource_dir(const std::string &resource_folder) -{ - if (!fs::is_directory(applicationdir)) { - return fs::path(); - } - - fs::path basepath(applicationdir); - - fs::path paths[] = { -#if __APPLE__ - // Application layout when installed on MacOS - basepath.parent_path().parent_path() / "Contents" / "Resources", -#endif -#ifdef __unix__ - // Different unix installation layouts are possible, this - // tries to capture the most obvious cases. - basepath.parent_path() / "share" / "openscad", - basepath.parent_path().parent_path() / "share" / "openscad", - fs::path("..") / "..", -#endif -#ifdef OPENSCAD_TESTING - // Used when running the test cases from source code layout. - fs::path(".."), -#endif - // Try to fall back to path relative to the executable and - // relative to the current working directory. - basepath, - fs::path("."), - fs::path(), // end of list marker - }; - - for (int a = 0;!paths[a].empty();a++) { - fs::path resource_dir = paths[a] / resource_folder; - if (fs::is_directory(resource_dir)) { - return resource_dir; - } - } - - return fs::path(); -} - /*! Searces for the given file in library paths and returns the full path if found. Returns an empty path if file cannot be found or filename is a directory. @@ -131,9 +89,8 @@ fs::path find_valid_path(const fs::path &sourcepath, return fs::path(); } -void parser_init(const std::string &applicationpath) +void parser_init() { - applicationdir = applicationpath; // Add paths from OPENSCADPATH before adding built-in paths const char *openscadpaths = getenv("OPENSCADPATH"); if (openscadpaths) { @@ -149,5 +106,5 @@ void parser_init(const std::string &applicationpath) add_librarydir(PlatformUtils::userLibraryPath()); #endif - add_librarydir(boosty::absolute(fs::path(PlatformUtils::resourcesPath()) / "libraries").string()); + add_librarydir(boosty::absolute(PlatformUtils::resourcePath("libraries")).string()); } diff --git a/src/parsersettings.h b/src/parsersettings.h index 8ae7730b..1b8dd53b 100644 --- a/src/parsersettings.h +++ b/src/parsersettings.h @@ -8,22 +8,9 @@ namespace fs = boost::filesystem; extern int parser_error_pos; /** - * Initialize application an library path. - * - * @param applicationpath path of the application binary, this is usually - * derived from the Qt application object. If Qt is disabled, argv[0] is used. + * Initialize library path. */ -void parser_init(const std::string &applicationpath); - -/** - * Return a path to specific resources relative to the application binary. - * This is used to find resources bundled with the application, e.g. the - * translation files for the gettext library. - * - * @param folder subfolder for the resources (e.g. "po"). - * @return the resource path. - */ -fs::path get_resource_dir(const std::string &resource_folder); +void parser_init(); fs::path search_libs(const fs::path &localpath); fs::path find_valid_path(const fs::path &sourcepath, diff --git a/tests/cgalcachetest.cc b/tests/cgalcachetest.cc index 0d434cac..663dc48c 100644 --- a/tests/cgalcachetest.cc +++ b/tests/cgalcachetest.cc @@ -52,6 +52,7 @@ namespace fs = boost::filesystem; #include namespace po = boost::program_options; #include "boosty.h" +#include "PlatformUtils.h" std::string commandline_commands; std::string currentdir; @@ -116,7 +117,8 @@ int main(int argc, char **argv) currentdir = boosty::stringy(fs::current_path()); - parser_init(boosty::stringy(fs::path(argv[0]).branch_path())); + PlatformUtils::registerApplicationPath(boosty::stringy(fs::path(argv[0]).branch_path())); + parser_init(); ModuleContext top_ctx; top_ctx.registerBuiltin(); diff --git a/tests/csgtexttest.cc b/tests/csgtexttest.cc index 95912f63..0787088d 100644 --- a/tests/csgtexttest.cc +++ b/tests/csgtexttest.cc @@ -48,6 +48,7 @@ #include namespace fs = boost::filesystem; #include "boosty.h" +#include "PlatformUtils.h" std::string commandline_commands; std::string currentdir; @@ -77,7 +78,8 @@ int main(int argc, char **argv) currentdir = boosty::stringy( fs::current_path() ); - parser_init(boosty::stringy(fs::path(argv[0]).branch_path())); + PlatformUtils::registerApplicationPath(boosty::stringy(fs::path(argv[0]).branch_path())); + parser_init(); ModuleContext top_ctx; top_ctx.registerBuiltin(); diff --git a/tests/modulecachetest.cc b/tests/modulecachetest.cc index d996cd84..c5c46fc0 100644 --- a/tests/modulecachetest.cc +++ b/tests/modulecachetest.cc @@ -46,6 +46,7 @@ #include namespace fs = boost::filesystem; #include "boosty.h" +#include "PlatformUtils.h" std::string commandline_commands; std::string currentdir; @@ -73,7 +74,8 @@ int main(int argc, char **argv) currentdir = boosty::stringy( fs::current_path() ); - parser_init(boosty::stringy(fs::path(argv[0]).branch_path())); + PlatformUtils::registerApplicationPath(boosty::stringy(fs::path(argv[0]).branch_path())); + parser_init(); ModuleContext top_ctx; top_ctx.registerBuiltin();