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?
ModuleWidget* moduleWidget = getModule(id);
if (!moduleWidget) {
WARN("Cannot find ModuleWidget with ID %" PRId64 "", id);
WARN("Cannot find ModuleWidget %" PRId64, id);
continue;
}

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

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

size_t moduleIndex;
json_t* 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");
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);
if (!module) {
WARN("Cannot find module with ID %" PRId64 "", id);
WARN("Cannot find Module %" PRId64, id);
continue;
}

// Create ModuleWidget
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
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);
math::Vec pos = math::Vec(x, y);
if (APP->patch->isLegacy(1)) {
@@ -271,22 +272,27 @@ void RackWidget::fromJson(json_t* rootJ) {
// In <=v0.6, cables were called wires
if (!cablesJ)
cablesJ = json_object_get(rootJ, "wires");
assert(cablesJ);
if (!cablesJ)
return;
size_t cableIndex;
json_t* cableJ;
json_array_foreach(cablesJ, cableIndex, cableJ) {
// cable
// Get Cable from Engine
// Get cable 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);
if (!cable) {
WARN("Cannot find cable with ID %" PRId64 "", id);
WARN("Cannot find Cable %" PRId64, id);
continue;
}

// Create CableWidget
CableWidget* cw = new CableWidget;
cw->setCable(cable);
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) {
// 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
json_t* inputModuleIdJ = json_object_get(rootJ, "inputModuleId");
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);
inputModule = APP->engine->getModule(inputModuleId);
if (!inputModule)
throw Exception("inputModule not found for cable");
throw Exception("Input module %" PRId64 " not found for cable %" PRId64, inputModuleId, id);

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

// outputModuleId
json_t* outputModuleIdJ = json_object_get(rootJ, "outputModuleId");
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);
outputModule = APP->engine->getModule(outputModuleId);
if (!outputModule)
throw Exception("outputModule not found for cable");
throw Exception("Output module %" PRId64 " not found for cable %" PRId64, outputModuleId, id);

// outputId
json_t* outputIdJ = json_object_get(rootJ, "outputId");
if (!outputIdJ)
throw Exception("outputId not found for cable");
throw Exception("Output ID not found for cable %" PRId64, id);
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;
json_t* 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 {
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.
module->fromJson(moduleJ);

// 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;
}

@@ -1127,8 +1139,10 @@ void Engine::fromJson(json_t* rootJ) {
addModule(module);
}
catch (Exception& e) {
WARN("Cannot deserialize module: %s", e.what());
WARN("Cannot load module: %s", 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) {
// cable
Cable* cable = new Cable;

try {
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
addCable(cable);
}
catch (Exception& e) {
WARN("Cannot deserialize cable: %s", e.what());
WARN("Cannot load cable: %s", e.what());
delete cable;
// 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
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
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
Model* model = getModel(pluginSlug, modelSlug);
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;
}



Loading…
Cancel
Save