From 583530032f5f9a2537840619cba2c2f7cb2da88c Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Mon, 3 May 2021 13:55:19 -0400 Subject: [PATCH] Move asset::pluginsPath, templatePath, and settingsPath to appropriate namespaces. --- include/asset.hpp | 4 ---- include/logger.hpp | 2 +- include/patch.hpp | 4 ++++ include/plugin.hpp | 2 ++ include/settings.hpp | 5 +++++ src/app/Scene.cpp | 2 +- src/asset.cpp | 15 --------------- src/library.cpp | 2 +- src/logger.cpp | 12 ++++++------ src/patch.cpp | 11 +++++++---- src/plugin.cpp | 18 +++++++++++++----- src/settings.cpp | 14 ++++++++++++++ standalone/main.cpp | 7 ++++--- 13 files changed, 58 insertions(+), 40 deletions(-) diff --git a/include/asset.hpp b/include/asset.hpp index 9bd9dedd..61f616a6 100644 --- a/include/asset.hpp +++ b/include/asset.hpp @@ -40,10 +40,6 @@ extern std::string userDir; // Only defined on Mac extern std::string bundlePath; -extern std::string pluginsPath; -extern std::string settingsPath; -extern std::string templatePath; - } // namespace asset } // namespace rack diff --git a/include/logger.hpp b/include/logger.hpp index 82bf4b1f..0286ed8c 100644 --- a/include/logger.hpp +++ b/include/logger.hpp @@ -23,7 +23,7 @@ namespace rack { namespace logger { -extern std::string path; +extern std::string logPath; enum Level { diff --git a/include/patch.hpp b/include/patch.hpp index 235a610f..e8d8a2dd 100644 --- a/include/patch.hpp +++ b/include/patch.hpp @@ -12,6 +12,10 @@ struct PatchManager { std::string path; /** Path to autosave folder */ std::string autosavePath; + /** Path to user template patch */ + std::string templatePath; + /** Path to factory template patch */ + std::string factoryTemplatePath; /** Append to this while loading/saving a patch to display messages to the user after success. */ std::string warningLog; diff --git a/include/plugin.hpp b/include/plugin.hpp index 77c426e3..668d60e3 100644 --- a/include/plugin.hpp +++ b/include/plugin.hpp @@ -28,6 +28,8 @@ bool isSlugValid(const std::string& slug); std::string normalizeSlug(const std::string& slug); +/** Path to plugins installation folder */ +extern std::string pluginsPath; extern std::vector plugins; diff --git a/include/settings.hpp b/include/settings.hpp index 5c179ffe..cc084d0a 100644 --- a/include/settings.hpp +++ b/include/settings.hpp @@ -19,6 +19,10 @@ namespace rack { namespace settings { +/** Path to settings.json */ +extern std::string settingsPath; + + // Runtime state, not serialized. extern bool devMode; @@ -86,6 +90,7 @@ struct ModuleUsage { extern std::map> moduleUsages; ModuleUsage* getModuleUsage(const std::string& pluginSlug, const std::string& moduleSlug); +void init(); json_t* toJson(); void fromJson(json_t* rootJ); void save(const std::string& path); diff --git a/src/app/Scene.cpp b/src/app/Scene.cpp index a32395ee..f04f93d7 100644 --- a/src/app/Scene.cpp +++ b/src/app/Scene.cpp @@ -83,7 +83,7 @@ void Scene::step() { if (time - lastAutosaveTime >= settings::autosaveInterval) { lastAutosaveTime = time; APP->patch->saveAutosave(); - settings::save(asset::settingsPath); + settings::save(settings::settingsPath); } } diff --git a/src/asset.cpp b/src/asset.cpp index fe97966a..3a839b53 100644 --- a/src/asset.cpp +++ b/src/asset.cpp @@ -115,18 +115,6 @@ void init() { initUserDir(); system::createDirectory(userDir); - - // Set paths - if (settings::devMode) { - pluginsPath = system::join(userDir, "plugins"); - settingsPath = system::join(userDir, "settings.json"); - templatePath = system::join(userDir, "template.vcv"); - } - else { - pluginsPath = system::join(userDir, "plugins-v" + ABI_VERSION); - settingsPath = system::join(userDir, "settings-v" + ABI_VERSION + ".json"); - templatePath = system::join(userDir, "template-v" + ABI_VERSION + ".vcv"); - } } @@ -149,9 +137,6 @@ std::string plugin(plugin::Plugin* plugin, std::string filename) { std::string systemDir; std::string userDir; -std::string pluginsPath; -std::string settingsPath; -std::string templatePath; std::string bundlePath; diff --git a/src/library.cpp b/src/library.cpp index 07dadcad..8890c99c 100644 --- a/src/library.cpp +++ b/src/library.cpp @@ -317,7 +317,7 @@ void syncUpdate(const std::string& slug) { // Get file path std::string packageFilename = slug + "-" + update.version + "-" + APP_ARCH + ".vcvplugin"; - std::string packagePath = system::join(asset::pluginsPath, packageFilename); + std::string packagePath = system::join(plugin::pluginsPath, packageFilename); // Download plugin package if (!network::requestDownload(downloadUrl, packagePath, &updateProgress, getTokenCookies())) { diff --git a/src/logger.cpp b/src/logger.cpp index 2fd0b058..84d5734d 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -11,7 +11,7 @@ namespace rack { namespace logger { -std::string path; +std::string logPath; static FILE* outputFile = NULL; static std::mutex mutex; @@ -25,11 +25,11 @@ void init() { outputFile = stderr; } else { - path = asset::user("log.txt"); + logPath = asset::user("log.txt"); - outputFile = std::fopen(path.c_str(), "w"); + outputFile = std::fopen(logPath.c_str(), "w"); if (!outputFile) { - std::fprintf(stderr, "Could not open log at %s\n", path.c_str()); + std::fprintf(stderr, "Could not open log at %s\n", logPath.c_str()); } } @@ -99,11 +99,11 @@ static bool fileEndsWith(FILE* file, std::string str) { } bool isTruncated() { - if (path.empty()) + if (logPath.empty()) return false; // Open existing log file - FILE* file = std::fopen(path.c_str(), "r"); + FILE* file = std::fopen(logPath.c_str(), "r"); if (!file) return false; DEFER({std::fclose(file);}); diff --git a/src/patch.cpp b/src/patch.cpp index aa10bee7..c3c35b36 100644 --- a/src/patch.cpp +++ b/src/patch.cpp @@ -25,10 +25,13 @@ static const char PATCH_FILTERS[] = "VCV Rack patch (.vcv):vcv"; PatchManager::PatchManager() { if (settings::devMode) { autosavePath = asset::user("autosave"); + templatePath = asset::user("template.vcv"); } else { autosavePath = asset::user("autosave-v" + ABI_VERSION); + templatePath = asset::user("template-v" + ABI_VERSION + ".vcv"); } + factoryTemplatePath = asset::system("template.vcv"); } @@ -172,7 +175,7 @@ void PatchManager::saveTemplateDialog() { return; try { - save(asset::templatePath); + save(templatePath); } catch (Exception& e) { std::string message = string::f("Could not save template patch: %s", e.what()); @@ -264,12 +267,12 @@ void PatchManager::load(std::string path) { void PatchManager::loadTemplate() { try { - load(asset::templatePath); + load(templatePath); } catch (Exception& e) { - // Do nothing because it's okay for the user template to not exist. + // Try loading the system template patch try { - load(asset::system("template.vcv")); + load(factoryTemplatePath); } catch (Exception& e) { std::string message = string::f("Could not load system template patch, clearing rack: %s", e.what()); diff --git a/src/plugin.cpp b/src/plugin.cpp index 0bcfb7c9..bdde93b4 100644 --- a/src/plugin.cpp +++ b/src/plugin.cpp @@ -213,19 +213,26 @@ void init() { // Load Core loadPlugin(""); + if (settings::devMode) { + pluginsPath = asset::user("plugins"); + } + else { + pluginsPath = asset::user("plugins-v" + ABI_VERSION); + } + // Get user plugins directory - system::createDirectory(asset::pluginsPath); + system::createDirectory(pluginsPath); // Extract packages and load plugins - extractPackages(asset::pluginsPath); - loadPlugins(asset::pluginsPath); + extractPackages(pluginsPath); + loadPlugins(pluginsPath); // If Fundamental wasn't loaded, copy the bundled Fundamental package and load it std::string fundamentalSrc = asset::system("Fundamental.vcvplugin"); - std::string fundamentalDir = system::join(asset::pluginsPath, "Fundamental"); + std::string fundamentalDir = system::join(pluginsPath, "Fundamental"); if (!settings::devMode && !getPlugin("Fundamental") && system::isFile(fundamentalSrc)) { INFO("Extracting bundled Fundamental package"); - system::unarchiveToFolder(fundamentalSrc.c_str(), asset::pluginsPath.c_str()); + system::unarchiveToFolder(fundamentalSrc.c_str(), pluginsPath.c_str()); loadPlugin(fundamentalDir); } } @@ -342,6 +349,7 @@ std::string normalizeSlug(const std::string& slug) { } +std::string pluginsPath; std::vector plugins; diff --git a/src/settings.cpp b/src/settings.cpp index 5ecfd1b0..9cc78221 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -8,12 +8,16 @@ #include #include #include +#include namespace rack { namespace settings { +std::string settingsPath; + + bool devMode = false; bool headless = false; std::string token; @@ -59,6 +63,16 @@ std::map> moduleWhitelist = {}; std::map> moduleUsages = {}; +void init() { + if (devMode) { + settingsPath = asset::user("settings.json"); + } + else { + settingsPath = asset::user("settings-v" + ABI_VERSION + ".json"); + } +} + + ModuleUsage* getModuleUsage(const std::string& pluginSlug, const std::string& moduleSlug) { auto it1 = moduleUsages.find(pluginSlug); if (it1 == moduleUsages.end()) diff --git a/standalone/main.cpp b/standalone/main.cpp index d4cae580..b855e138 100644 --- a/standalone/main.cpp +++ b/standalone/main.cpp @@ -49,7 +49,7 @@ static void fatalSignalHandler(int sig) { // This might fail because we might not be in the main thread. // But oh well, we're crashing anyway. - std::string text = APP_NAME + " has crashed. See " + logger::path + " for details."; + std::string text = APP_NAME + " has crashed. See " + logger::logPath + " for details."; osdialog_message(OSDIALOG_ERROR, OSDIALOG_OK, text.c_str()); abort(); @@ -144,8 +144,9 @@ int main(int argc, char* argv[]) { #endif // Load settings + settings::init(); try { - settings::load(asset::settingsPath); + settings::load(settings::settingsPath); } catch (Exception& e) { std::string msg = e.what(); @@ -240,7 +241,7 @@ int main(int argc, char* argv[]) { delete APP; contextSet(NULL); if (!settings::headless) { - settings::save(asset::settingsPath); + settings::save(settings::settingsPath); } // Destroy environment