Browse Source

Clean up fromJson Engine and RackWidget methods. Fix v0.6 patch loading.

tags/v2.0.0
Andrew Belt 4 years ago
parent
commit
c4cc1c4368
5 changed files with 78 additions and 47 deletions
  1. +28
    -22
      src/app/RackWidget.cpp
  2. +15
    -15
      src/engine/Cable.cpp
  3. +28
    -6
      src/engine/Engine.cpp
  4. +6
    -3
      src/engine/Module.cpp
  5. +1
    -1
      src/plugin.cpp

+ 28
- 22
src/app/RackWidget.cpp View File

@@ -187,7 +187,7 @@ void RackWidget::mergeJson(json_t* rootJ) {
// TODO Legacy v0.6? // TODO Legacy v0.6?
ModuleWidget* moduleWidget = getModule(id); ModuleWidget* moduleWidget = getModule(id);
if (!moduleWidget) { if (!moduleWidget) {
WARN("Cannot find ModuleWidget with ID %" PRId64 "", id);
WARN("Cannot find ModuleWidget %" PRId64, id);
continue; continue;
} }


@@ -212,7 +212,7 @@ void RackWidget::mergeJson(json_t* rootJ) {
int64_t id = json_integer_value(idJ); int64_t id = json_integer_value(idJ);
CableWidget* cw = getCable(id); CableWidget* cw = getCable(id);
if (!cw) { if (!cw) {
WARN("Cannot find CableWidget with ID %" PRId64 "", id);
WARN("Cannot find CableWidget %" PRId64, id);
continue; continue;
} }


@@ -226,32 +226,33 @@ void RackWidget::mergeJson(json_t* rootJ) {
void RackWidget::fromJson(json_t* rootJ) { void RackWidget::fromJson(json_t* rootJ) {
// modules // modules
json_t* modulesJ = json_object_get(rootJ, "modules"); json_t* modulesJ = json_object_get(rootJ, "modules");
assert(modulesJ);
if (!modulesJ)
return;

size_t moduleIndex; size_t moduleIndex;
json_t* moduleJ; json_t* moduleJ;
json_array_foreach(modulesJ, moduleIndex, moduleJ) { json_array_foreach(modulesJ, moduleIndex, moduleJ) {
// module
// Create ModuleWidget and attach it to existing Module from Engine.
// Get module ID
json_t* idJ = json_object_get(moduleJ, "id"); json_t* idJ = json_object_get(moduleJ, "id");
if (!idJ)
continue;
int64_t id = json_integer_value(idJ);
int64_t id;
if (idJ)
id = json_integer_value(idJ);
else
id = moduleIndex;

// Get Module
engine::Module* module = APP->engine->getModule(id); engine::Module* module = APP->engine->getModule(id);
if (!module) { if (!module) {
WARN("Cannot find module with ID %" PRId64 "", id);
WARN("Cannot find Module %" PRId64, id);
continue; continue;
} }


// Create ModuleWidget
ModuleWidget* moduleWidget = module->model->createModuleWidget(module); ModuleWidget* moduleWidget = module->model->createModuleWidget(module);


// Before 1.0, the module ID was the index in the "modules" array
if (APP->patch->isLegacy(2)) {
module->id = moduleIndex;
}

// pos // pos
json_t* posJ = json_object_get(moduleJ, "pos"); json_t* posJ = json_object_get(moduleJ, "pos");
double x, y;
double x = 0.0, y = 0.0;
json_unpack(posJ, "[F, F]", &x, &y); json_unpack(posJ, "[F, F]", &x, &y);
math::Vec pos = math::Vec(x, y); math::Vec pos = math::Vec(x, y);
if (APP->patch->isLegacy(1)) { if (APP->patch->isLegacy(1)) {
@@ -271,22 +272,27 @@ void RackWidget::fromJson(json_t* rootJ) {
// In <=v0.6, cables were called wires // In <=v0.6, cables were called wires
if (!cablesJ) if (!cablesJ)
cablesJ = json_object_get(rootJ, "wires"); cablesJ = json_object_get(rootJ, "wires");
assert(cablesJ);
if (!cablesJ)
return;
size_t cableIndex; size_t cableIndex;
json_t* cableJ; json_t* cableJ;
json_array_foreach(cablesJ, cableIndex, cableJ) { json_array_foreach(cablesJ, cableIndex, cableJ) {
// cable
// Get Cable from Engine
// Get cable ID
json_t* idJ = json_object_get(cableJ, "id"); json_t* idJ = json_object_get(cableJ, "id");
if (!idJ)
continue;
int64_t id = json_integer_value(idJ);
int64_t id;
if (idJ)
id = json_integer_value(idJ);
else
id = cableIndex;

// Get Cable
engine::Cable* cable = APP->engine->getCable(id); engine::Cable* cable = APP->engine->getCable(id);
if (!cable) { if (!cable) {
WARN("Cannot find cable with ID %" PRId64 "", id);
WARN("Cannot find Cable %" PRId64, id);
continue; continue;
} }


// Create CableWidget
CableWidget* cw = new CableWidget; CableWidget* cw = new CableWidget;
cw->setCable(cable); cw->setCable(cable);
cw->fromJson(cableJ); cw->fromJson(cableJ);


+ 15
- 15
src/engine/Cable.cpp View File

@@ -19,44 +19,44 @@ json_t* Cable::toJson() {




void Cable::fromJson(json_t* rootJ) { void Cable::fromJson(json_t* rootJ) {
// Only set ID if unset
if (id < 0) {
// id
json_t* idJ = json_object_get(rootJ, "id");
// Before 1.0, cables IDs were not used, so just leave it as default and Engine will assign one automatically.
if (idJ)
id = json_integer_value(idJ);
}

// inputModuleId // inputModuleId
json_t* inputModuleIdJ = json_object_get(rootJ, "inputModuleId"); json_t* inputModuleIdJ = json_object_get(rootJ, "inputModuleId");
if (!inputModuleIdJ) if (!inputModuleIdJ)
throw Exception("inputModuleId not found for cable");
throw Exception("Input module ID not found for cable %" PRId64, id);
int64_t inputModuleId = json_integer_value(inputModuleIdJ); int64_t inputModuleId = json_integer_value(inputModuleIdJ);
inputModule = APP->engine->getModule(inputModuleId); inputModule = APP->engine->getModule(inputModuleId);
if (!inputModule) if (!inputModule)
throw Exception("inputModule not found for cable");
throw Exception("Input module %" PRId64 " not found for cable %" PRId64, inputModuleId, id);


// inputId // inputId
json_t* inputIdJ = json_object_get(rootJ, "inputId"); json_t* inputIdJ = json_object_get(rootJ, "inputId");
if (!inputIdJ) if (!inputIdJ)
throw Exception("inputId not found for cable");
throw Exception("Input ID not found for cable %" PRId64, id);
inputId = json_integer_value(inputIdJ); inputId = json_integer_value(inputIdJ);


// outputModuleId // outputModuleId
json_t* outputModuleIdJ = json_object_get(rootJ, "outputModuleId"); json_t* outputModuleIdJ = json_object_get(rootJ, "outputModuleId");
if (!outputModuleIdJ) if (!outputModuleIdJ)
throw Exception("outputModuleId not found for cable");
throw Exception("Output module ID not found for cable %" PRId64, id);
int64_t outputModuleId = json_integer_value(outputModuleIdJ); int64_t outputModuleId = json_integer_value(outputModuleIdJ);
outputModule = APP->engine->getModule(outputModuleId); outputModule = APP->engine->getModule(outputModuleId);
if (!outputModule) if (!outputModule)
throw Exception("outputModule not found for cable");
throw Exception("Output module %" PRId64 " not found for cable %" PRId64, outputModuleId, id);


// outputId // outputId
json_t* outputIdJ = json_object_get(rootJ, "outputId"); json_t* outputIdJ = json_object_get(rootJ, "outputId");
if (!outputIdJ) if (!outputIdJ)
throw Exception("outputId not found for cable");
throw Exception("Output ID not found for cable %" PRId64, id);
outputId = json_integer_value(outputIdJ); outputId = json_integer_value(outputIdJ);

// Only set ID if unset
if (id < 0) {
// id
json_t* idJ = json_object_get(rootJ, "id");
// Before 1.0, cables IDs were not used, so just leave it as default and Engine will assign one automatically.
if (idJ)
id = json_integer_value(idJ);
}
} }






+ 28
- 6
src/engine/Engine.cpp View File

@@ -1111,15 +1111,27 @@ void Engine::fromJson(json_t* rootJ) {
size_t moduleIndex; size_t moduleIndex;
json_t* moduleJ; json_t* moduleJ;
json_array_foreach(modulesJ, moduleIndex, moduleJ) { json_array_foreach(modulesJ, moduleIndex, moduleJ) {
// Get model
plugin::Model* model;
try {
model = plugin::modelFromJson(moduleJ);
}
catch (Exception& e) {
WARN("Cannot load model: %s", e.what());
APP->patch->log(e.what());
continue;
}

// Create module
Module* module = model->createModule();
assert(module);

try { try {
plugin::Model* model = plugin::modelFromJson(moduleJ);
Module* module = model->createModule();
assert(module);
// This doesn't need a lock because the Module is not added to the Engine yet. // This doesn't need a lock because the Module is not added to the Engine yet.
module->fromJson(moduleJ); module->fromJson(moduleJ);


// Before 1.0, the module ID was the index in the "modules" array // Before 1.0, the module ID was the index in the "modules" array
if (APP->patch->isLegacy(2)) {
if (module->id < 0) {
module->id = moduleIndex; module->id = moduleIndex;
} }


@@ -1127,8 +1139,10 @@ void Engine::fromJson(json_t* rootJ) {
addModule(module); addModule(module);
} }
catch (Exception& e) { catch (Exception& e) {
WARN("Cannot deserialize module: %s", e.what());
WARN("Cannot load module: %s", e.what());
APP->patch->log(e.what()); APP->patch->log(e.what());
delete module;
continue;
} }
} }


@@ -1144,15 +1158,23 @@ void Engine::fromJson(json_t* rootJ) {
json_array_foreach(cablesJ, cableIndex, cableJ) { json_array_foreach(cablesJ, cableIndex, cableJ) {
// cable // cable
Cable* cable = new Cable; Cable* cable = new Cable;

try { try {
cable->fromJson(cableJ); cable->fromJson(cableJ);

// Before 1.0, the cable ID was the index in the "cables" array
if (cable->id < 0) {
cable->id = cableIndex;
}

// Write-locks // Write-locks
addCable(cable); addCable(cable);
} }
catch (Exception& e) { catch (Exception& e) {
WARN("Cannot deserialize cable: %s", e.what());
WARN("Cannot load cable: %s", e.what());
delete cable; delete cable;
// Don't log exceptions because missing modules create unnecessary complaining when cables try to connect to them. // Don't log exceptions because missing modules create unnecessary complaining when cables try to connect to them.
continue;
} }
} }
} }


+ 6
- 3
src/engine/Module.cpp View File

@@ -154,9 +154,12 @@ void Module::fromJson(json_t* rootJ) {
} }


// id // id
json_t* idJ = json_object_get(rootJ, "id");
if (idJ)
id = json_integer_value(idJ);
// Only set ID if unset
if (id < 0) {
json_t* idJ = json_object_get(rootJ, "id");
if (idJ)
id = json_integer_value(idJ);
}


// params // params
json_t* paramsJ = json_object_get(rootJ, "params"); json_t* paramsJ = json_object_get(rootJ, "params");


+ 1
- 1
src/plugin.cpp View File

@@ -318,7 +318,7 @@ Model* modelFromJson(json_t* moduleJ) {
// Get Model // Get Model
Model* model = getModel(pluginSlug, modelSlug); Model* model = getModel(pluginSlug, modelSlug);
if (!model) if (!model)
throw Exception("Could not find module \"%s\" of plugin \"%s\"", modelSlug.c_str(), pluginSlug.c_str());
throw Exception("Could not find module \"%s\" \"%s\"", pluginSlug.c_str(), modelSlug.c_str());
return model; return model;
} }




Loading…
Cancel
Save