From 7317ac5afda9515a520fd1d7c2fd1d38c1e7f5d2 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Tue, 7 Dec 2021 15:59:50 -0500 Subject: [PATCH] If input or output PortWidgets do not exist when adding a CableWidget, delete the CableWidget and remove Cable from Engine. --- src/app/CableWidget.cpp | 25 +++++++++++++++---------- src/app/RackWidget.cpp | 20 +++++++++++++++----- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/app/CableWidget.cpp b/src/app/CableWidget.cpp index 8342fb84..863d964a 100644 --- a/src/app/CableWidget.cpp +++ b/src/app/CableWidget.cpp @@ -166,17 +166,22 @@ void CableWidget::setCable(engine::Cable* cable) { delete this->cable; this->cable = NULL; } - this->cable = cable; if (cable) { - app::ModuleWidget* outputModule = APP->scene->rack->getModule(cable->outputModule->id); - assert(outputModule); - outputPort = outputModule->getOutput(cable->outputId); - assert(outputPort); - - app::ModuleWidget* inputModule = APP->scene->rack->getModule(cable->inputModule->id); - assert(inputModule); - inputPort = inputModule->getInput(cable->inputId); - assert(inputPort); + app::ModuleWidget* outputMw = APP->scene->rack->getModule(cable->outputModule->id); + if (!outputMw) + throw Exception("Cable cannot find output ModuleWidget %lld", cable->outputModule->id); + outputPort = outputMw->getOutput(cable->outputId); + if (!outputPort) + throw Exception("Cable cannot find output port %d", cable->outputId); + + app::ModuleWidget* inputMw = APP->scene->rack->getModule(cable->inputModule->id); + if (!inputMw) + throw Exception("Cable cannot find input ModuleWidget %lld", cable->inputModule->id); + inputPort = inputMw->getInput(cable->inputId); + if (!inputPort) + throw Exception("Cable cannot find input port %d", cable->inputId); + + this->cable = cable; } else { outputPort = NULL; diff --git a/src/app/RackWidget.cpp b/src/app/RackWidget.cpp index 9e167b4f..a46baabb 100644 --- a/src/app/RackWidget.cpp +++ b/src/app/RackWidget.cpp @@ -395,6 +395,7 @@ void RackWidget::fromJson(json_t* rootJ) { // Get cable ID json_t* idJ = json_object_get(cableJ, "id"); int64_t id; + // In <=v0.6, the cable ID was the index in the array. if (idJ) id = json_integer_value(idJ); else @@ -409,11 +410,20 @@ void RackWidget::fromJson(json_t* rootJ) { // Create CableWidget CableWidget* cw = new CableWidget; - cw->setCable(cable); - cw->fromJson(cableJ); - // In <=v1, cable colors were not serialized, so choose one from the available colors. - if (cw->color.a == 0.f) { - cw->color = getNextCableColor(); + try { + cw->setCable(cable); + cw->fromJson(cableJ); + // In <=v1, cable colors were not serialized, so choose one from the available colors. + if (cw->color.a == 0.f) { + cw->color = getNextCableColor(); + } + } + catch (Exception& e) { + delete cw; + // If creating CableWidget fails, remove Cable from Engine. + APP->engine->removeCable(cable); + delete cable; + continue; } addCable(cw); }