Browse Source

Add rough code to load/save patch to new autosave directory.

tags/v2.0.0
Andrew Belt 4 years ago
parent
commit
53cf78eb72
3 changed files with 50 additions and 43 deletions
  1. +2
    -2
      src/asset.cpp
  2. +47
    -40
      src/patch.cpp
  3. +1
    -1
      standalone/main.cpp

+ 2
- 2
src/asset.cpp View File

@@ -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";
}
}


+ 47
- 40
src/patch.cpp View File

@@ -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


+ 1
- 1
standalone/main.cpp View File

@@ -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


Loading…
Cancel
Save