From 53cf78eb72ddfbc76112105e7878134d6928a2fd Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Fri, 4 Sep 2020 10:45:46 -0400 Subject: [PATCH] Add rough code to load/save patch to new autosave directory. --- src/asset.cpp | 4 +-- src/patch.cpp | 87 ++++++++++++++++++++++++--------------------- standalone/main.cpp | 2 +- 3 files changed, 50 insertions(+), 43 deletions(-) diff --git a/src/asset.cpp b/src/asset.cpp index 27a0f37e..d2c2906b 100644 --- a/src/asset.cpp +++ b/src/asset.cpp @@ -122,14 +122,14 @@ void init() { if (settings::devMode) { pluginsPath = userDir + "/plugins"; settingsPath = userDir + "/settings.json"; - autosavePath = userDir + "/autosave.vcv"; + autosavePath = userDir + "/autosave"; templatePath = userDir + "/template.vcv"; } else { logPath = userDir + "/log.txt"; pluginsPath = userDir + "/plugins-v" + ABI_VERSION; settingsPath = userDir + "/settings-v" + ABI_VERSION + ".json"; - autosavePath = userDir + "/autosave-v" + ABI_VERSION + ".vcv"; + autosavePath = userDir + "/autosave-v" + ABI_VERSION; templatePath = userDir + "/template-v" + ABI_VERSION + ".vcv"; } } diff --git a/src/patch.cpp b/src/patch.cpp index 177800ba..6a0183e2 100644 --- a/src/patch.cpp +++ b/src/patch.cpp @@ -53,24 +53,9 @@ static bool promptClear(std::string text) { void PatchManager::save(std::string path) { INFO("Saving patch %s", path.c_str()); - json_t* rootJ = toJson(); - if (!rootJ) - return; - DEFER({ - json_decref(rootJ); - }); + saveAutosave(); - // Write to temporary path and then rename it to the correct path - std::string tmpPath = path + ".tmp"; - FILE* file = std::fopen(tmpPath.c_str(), "w"); - if (!file) { - // Fail silently - return; - } - - json_dumpf(rootJ, file, JSON_INDENT(2) | JSON_REAL_PRECISION(9)); - std::fclose(file); - system::moveFile(tmpPath, path); + // TODO Archive autosave folder } void PatchManager::saveDialog() { @@ -130,33 +115,34 @@ void PatchManager::saveTemplateDialog() { } void PatchManager::saveAutosave() { - save(asset::autosavePath); + INFO("Saving autosave"); + json_t* rootJ = toJson(); + if (!rootJ) + return; + DEFER({ + json_decref(rootJ); + }); + + // Write to temporary path and then rename it to the correct path + system::createDirectories(asset::autosavePath); + std::string patchPath = asset::autosavePath + "/patch.json"; + std::string tmpPath = patchPath + ".tmp"; + FILE* file = std::fopen(tmpPath.c_str(), "w"); + if (!file) { + // Fail silently + return; + } + + json_dumpf(rootJ, file, JSON_INDENT(2) | JSON_REAL_PRECISION(9)); + std::fclose(file); + system::moveFile(tmpPath, patchPath); } bool PatchManager::load(std::string path) { INFO("Loading patch %s", path.c_str()); - FILE* file = std::fopen(path.c_str(), "r"); - if (!file) { - // Exit silently - return false; - } - DEFER({ - std::fclose(file); - }); - json_error_t error; - json_t* rootJ = json_loadf(file, 0, &error); - if (!rootJ) { - std::string message = string::f("Failed to load patch. JSON parsing error at %s %d:%d %s", error.source, error.line, error.column, error.text); - osdialog_message(OSDIALOG_WARNING, OSDIALOG_OK, message.c_str()); - return false; - } - DEFER({ - json_decref(rootJ); - }); + // TODO Extract archive to autosave - clear(); - fromJson(rootJ); return true; } @@ -183,10 +169,30 @@ void PatchManager::loadTemplateDialog() { } void PatchManager::loadAutosave() { - if (load(asset::autosavePath)) { + INFO("Loading autosave"); + std::string patchPath = asset::autosavePath + "/patch.json"; + FILE* file = std::fopen(patchPath.c_str(), "r"); + if (!file) { + // Exit silently + // TODO Load autosave return; } - loadTemplate(); + DEFER({ + std::fclose(file); + }); + + json_error_t error; + json_t* rootJ = json_loadf(file, 0, &error); + if (!rootJ) { + std::string message = string::f("Failed to load patch. JSON parsing error at %s %d:%d %s", error.source, error.line, error.column, error.text); + osdialog_message(OSDIALOG_WARNING, OSDIALOG_OK, message.c_str()); + return; + } + DEFER({ + json_decref(rootJ); + }); + + fromJson(rootJ); } void PatchManager::loadAction(std::string path) { @@ -279,6 +285,7 @@ json_t* PatchManager::toJson() { } void PatchManager::fromJson(json_t* rootJ) { + clear(); legacy = 0; // version diff --git a/standalone/main.cpp b/standalone/main.cpp index 642a189e..b42238d7 100644 --- a/standalone/main.cpp +++ b/standalone/main.cpp @@ -177,7 +177,6 @@ int main(int argc, char* argv[]) { INFO("Initializing context"); contextSet(new Context); APP->engine = new engine::Engine; - APP->patch = new PatchManager; if (!settings::headless) { APP->event = new event::State; APP->history = new history::State; @@ -185,6 +184,7 @@ int main(int argc, char* argv[]) { APP->scene = new app::Scene; APP->event->rootWidget = APP->scene; } + APP->patch = new PatchManager; // On Mac, use a hacked-in GLFW addition to get the launched path. #if defined ARCH_MAC