From 2966cd57f74767cf49d33f07781d9e5cf3436d38 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Fri, 30 Apr 2021 14:11:19 -0400 Subject: [PATCH] Move asset::autosavePath to Patch. Add Module::createPatchStorageDir() and remove asset::module(). --- include/asset.hpp | 10 ---------- include/engine/Module.hpp | 5 +++++ include/patch.hpp | 3 +++ src/asset.cpp | 8 -------- src/engine/Module.cpp | 8 ++++++++ src/patch.cpp | 28 +++++++++++++++++----------- 6 files changed, 33 insertions(+), 29 deletions(-) diff --git a/include/asset.hpp b/include/asset.hpp index 92e6aa80..ce4162a7 100644 --- a/include/asset.hpp +++ b/include/asset.hpp @@ -33,15 +33,6 @@ Examples: */ std::string plugin(plugin::Plugin* plugin, std::string filename = ""); -/** Returns the path to an asset in the module patch folder. -The module patch folder is *not* created automatically. Before creating files at these paths, call - system::createDirectories(asset::module(module)) - -Examples: - asset::module(module, "recordings/00.wav") // "/path/to/Rack/user/folder/autosave/modules/1234/recordings/00.wav" -*/ -std::string module(engine::Module* module, const std::string& filename = ""); - // Set these before calling init() to override the default paths extern std::string systemDir; @@ -50,7 +41,6 @@ extern std::string userDir; extern std::string logPath; extern std::string pluginsPath; extern std::string settingsPath; -extern std::string autosavePath; extern std::string templatePath; // Only defined on Mac extern std::string bundlePath; diff --git a/include/engine/Module.hpp b/include/engine/Module.hpp index cd95073f..0acc9575 100644 --- a/include/engine/Module.hpp +++ b/include/engine/Module.hpp @@ -218,6 +218,11 @@ struct Module { bypassRoutes.push_back(br); } + /** Creates and returns the module's patch storage folder path. + Since the folder is created when this is called, do not call it frequently or in an audio/engine thread such as process(). + */ + std::string createPatchStorageDir(); + struct ProcessArgs { /** The current sample rate in Hz. */ float sampleRate; diff --git a/include/patch.hpp b/include/patch.hpp index 7f1edc21..235a610f 100644 --- a/include/patch.hpp +++ b/include/patch.hpp @@ -10,6 +10,9 @@ namespace rack { struct PatchManager { /** The currently loaded patch file path */ std::string path; + /** Path to autosave folder */ + std::string autosavePath; + /** Append to this while loading/saving a patch to display messages to the user after success. */ std::string warningLog; PatchManager(); diff --git a/src/asset.cpp b/src/asset.cpp index b26955a4..42c53bb9 100644 --- a/src/asset.cpp +++ b/src/asset.cpp @@ -120,14 +120,12 @@ void init() { if (settings::devMode) { pluginsPath = system::join(userDir, "plugins"); settingsPath = system::join(userDir, "settings.json"); - autosavePath = system::join(userDir, "autosave"); templatePath = system::join(userDir, "template.vcv"); } else { logPath = system::join(userDir, "log.txt"); pluginsPath = system::join(userDir, "plugins-v" + ABI_VERSION); settingsPath = system::join(userDir, "settings-v" + ABI_VERSION + ".json"); - autosavePath = system::join(userDir, "autosave-v" + ABI_VERSION); templatePath = system::join(userDir, "template-v" + ABI_VERSION + ".vcv"); } } @@ -149,12 +147,6 @@ std::string plugin(plugin::Plugin* plugin, std::string filename) { } -std::string module(engine::Module* module, const std::string& filename) { - assert(module); - return system::join(autosavePath, "modules", std::to_string(module->id), filename); -} - - std::string systemDir; std::string userDir; diff --git a/src/engine/Module.cpp b/src/engine/Module.cpp index e8052290..b8a5c672 100644 --- a/src/engine/Module.cpp +++ b/src/engine/Module.cpp @@ -5,6 +5,7 @@ #include #include #include +#include namespace rack { @@ -81,6 +82,13 @@ void Module::config(int numParams, int numInputs, int numOutputs, int numLights) } +std::string Module::createPatchStorageDir() { + std::string path = system::join(APP->patch->autosavePath, "modules", std::to_string(id)); + system::createDirectories(path); + return path; +} + + void Module::processBypass(const ProcessArgs& args) { for (BypassRoute& bypassRoute : bypassRoutes) { // Route input voltages to output diff --git a/src/patch.cpp b/src/patch.cpp index 54e89b14..aa10bee7 100644 --- a/src/patch.cpp +++ b/src/patch.cpp @@ -23,6 +23,12 @@ static const char PATCH_FILTERS[] = "VCV Rack patch (.vcv):vcv"; PatchManager::PatchManager() { + if (settings::devMode) { + autosavePath = asset::user("autosave"); + } + else { + autosavePath = asset::user("autosave-v" + ABI_VERSION); + } } @@ -85,11 +91,11 @@ void PatchManager::save(std::string path) { cleanAutosave(); // Take screenshot (disabled because there is currently no way to quickly view them on any OS or website.) - // APP->window->screenshot(system::join(asset::autosavePath, "screenshot.png")); + // APP->window->screenshot(system::join(autosavePath, "screenshot.png")); double startTime = system::getTime(); // Set compression level to 1 so that a 500MB/s SSD is almost bottlenecked - system::archiveFolder(path, asset::autosavePath, 1); + system::archiveFolder(path, autosavePath, 1); double endTime = system::getTime(); INFO("Archived patch in %lf seconds", (endTime - startTime)); } @@ -177,7 +183,7 @@ void PatchManager::saveTemplateDialog() { void PatchManager::saveAutosave() { - std::string patchPath = system::join(asset::autosavePath, "patch.json"); + std::string patchPath = system::join(autosavePath, "patch.json"); INFO("Saving autosave %s", patchPath.c_str()); json_t* rootJ = toJson(); if (!rootJ) @@ -185,7 +191,7 @@ void PatchManager::saveAutosave() { DEFER({json_decref(rootJ);}); // Write to temporary path and then rename it to the correct path - system::createDirectories(asset::autosavePath); + system::createDirectories(autosavePath); std::string tmpPath = patchPath + ".tmp"; FILE* file = std::fopen(tmpPath.c_str(), "w"); if (!file) { @@ -202,7 +208,7 @@ void PatchManager::saveAutosave() { void PatchManager::cleanAutosave() { // Remove files and folders in the `autosave/modules` folder that doesn't match a module in the rack. - std::string modulesDir = system::join(asset::autosavePath, "modules"); + std::string modulesDir = system::join(autosavePath, "modules"); if (system::isDirectory(modulesDir)) { for (const std::string& entry : system::getEntries(modulesDir)) { try { @@ -237,17 +243,17 @@ static bool isPatchLegacyV1(std::string path) { void PatchManager::load(std::string path) { INFO("Loading patch %s", path.c_str()); - system::removeRecursively(asset::autosavePath); - system::createDirectories(asset::autosavePath); + system::removeRecursively(autosavePath); + system::createDirectories(autosavePath); if (isPatchLegacyV1(path)) { // Copy the .vcv file directly to "patch.json". - system::copy(path, system::join(asset::autosavePath, "patch.json")); + system::copy(path, system::join(autosavePath, "patch.json")); } else { // Extract the .vcv file as a .tar.zst archive. double startTime = system::getTime(); - system::unarchiveToFolder(path, asset::autosavePath); + system::unarchiveToFolder(path, autosavePath); double endTime = system::getTime(); INFO("Unarchived patch in %lf seconds", (endTime - startTime)); } @@ -287,7 +293,7 @@ void PatchManager::loadTemplateDialog() { bool PatchManager::hasAutosave() { - std::string patchPath = system::join(asset::autosavePath, "patch.json"); + std::string patchPath = system::join(autosavePath, "patch.json"); INFO("Loading autosave %s", patchPath.c_str()); FILE* file = std::fopen(patchPath.c_str(), "r"); if (!file) @@ -298,7 +304,7 @@ bool PatchManager::hasAutosave() { void PatchManager::loadAutosave() { - std::string patchPath = system::join(asset::autosavePath, "patch.json"); + std::string patchPath = system::join(autosavePath, "patch.json"); INFO("Loading autosave %s", patchPath.c_str()); FILE* file = std::fopen(patchPath.c_str(), "r"); if (!file)