This commit is contained in:
Marc Gilleron 2022-07-29 21:30:07 +01:00
parent d7a6b8aaab
commit d8b1e090f5

View File

@ -1,4 +1,5 @@
#include "program_graph.h"
#include "../../util/errors.h"
#include <core/io/file_access.h>
#include <core/io/resource.h>
#include <core/variant/variant.h>
@ -8,7 +9,7 @@ namespace zylann {
template <typename T>
inline bool range_contains(const std::vector<T> &vec, const T &v, uint32_t begin, uint32_t end) {
CRASH_COND(end > vec.size());
ZN_ASSERT(end <= vec.size());
for (size_t i = begin; i < end; ++i) {
if (vec[i] == v) {
return true;
@ -22,7 +23,7 @@ ProgramGraph::~ProgramGraph() {
}
uint32_t ProgramGraph::Node::find_input_connection(PortLocation src, uint32_t input_port_index) const {
CRASH_COND(input_port_index >= inputs.size());
ZN_ASSERT(input_port_index < inputs.size());
const Port &p = inputs[input_port_index];
for (size_t i = 0; i < p.connections.size(); ++i) {
if (p.connections[i] == src) {
@ -33,7 +34,7 @@ uint32_t ProgramGraph::Node::find_input_connection(PortLocation src, uint32_t in
}
uint32_t ProgramGraph::Node::find_output_connection(uint32_t output_port_index, PortLocation dst) const {
CRASH_COND(output_port_index >= outputs.size());
ZN_ASSERT(output_port_index < outputs.size());
const Port &p = outputs[output_port_index];
for (size_t i = 0; i < p.connections.size(); ++i) {
if (p.connections[i] == dst) {
@ -59,7 +60,7 @@ ProgramGraph::Node *ProgramGraph::create_node(uint32_t type_id, uint32_t id) {
id = generate_node_id();
} else {
// ID must not be taken already
ERR_FAIL_COND_V(_nodes.find(id) != _nodes.end(), nullptr);
ZN_ASSERT_RETURN_V(_nodes.find(id) == _nodes.end(), nullptr);
if (_next_node_id <= id) {
_next_node_id = id + 1;
}
@ -81,7 +82,7 @@ void ProgramGraph::remove_node(uint32_t node_id) {
const PortLocation src = *it;
Node &src_node = get_node(src.node_id);
uint32_t i = src_node.find_output_connection(src.port_index, PortLocation{ node_id, dst_port_index });
CRASH_COND(i == NULL_INDEX);
ZN_ASSERT(i != NULL_INDEX);
std::vector<PortLocation> &connections = src_node.outputs[src.port_index].connections;
connections.erase(connections.begin() + i);
}
@ -94,7 +95,7 @@ void ProgramGraph::remove_node(uint32_t node_id) {
const PortLocation dst = *it;
Node &dst_node = get_node(dst.node_id);
uint32_t i = dst_node.find_input_connection(PortLocation{ node_id, src_port_index }, dst.port_index);
CRASH_COND(i == NULL_INDEX);
ZN_ASSERT(i != NULL_INDEX);
std::vector<PortLocation> &connections = dst_node.inputs[dst.port_index].connections;
connections.erase(connections.begin() + i);
}
@ -107,7 +108,7 @@ void ProgramGraph::remove_node(uint32_t node_id) {
void ProgramGraph::clear() {
for (auto it = _nodes.begin(); it != _nodes.end(); ++it) {
Node *node = it->second;
CRASH_COND(node == nullptr);
ZN_ASSERT(node != nullptr);
memdelete(node);
}
_nodes.clear();
@ -117,10 +118,10 @@ bool ProgramGraph::is_connected(PortLocation src, PortLocation dst) const {
const Node &src_node = get_node(src.node_id);
const Node &dst_node = get_node(dst.node_id);
if (src_node.find_output_connection(src.port_index, dst) != NULL_INDEX) {
CRASH_COND(dst_node.find_input_connection(src, dst.port_index) == NULL_INDEX);
ZN_ASSERT(dst_node.find_input_connection(src, dst.port_index) != NULL_INDEX);
return true;
} else {
CRASH_COND(dst_node.find_input_connection(src, dst.port_index) != NULL_INDEX);
ZN_ASSERT(dst_node.find_input_connection(src, dst.port_index) == NULL_INDEX);
return false;
}
}
@ -147,12 +148,12 @@ bool ProgramGraph::can_connect(PortLocation src, PortLocation dst) const {
}
void ProgramGraph::connect(PortLocation src, PortLocation dst) {
ERR_FAIL_COND(is_connected(src, dst));
ERR_FAIL_COND(has_path(dst.node_id, src.node_id));
ZN_ASSERT_RETURN(!is_connected(src, dst));
ZN_ASSERT_RETURN(!has_path(dst.node_id, src.node_id));
Node &src_node = get_node(src.node_id);
Node &dst_node = get_node(dst.node_id);
ERR_FAIL_COND_MSG(
dst_node.inputs[dst.port_index].connections.size() != 0, "Destination node's port is already connected");
ZN_ASSERT_RETURN_MSG(
dst_node.inputs[dst.port_index].connections.size() == 0, "Destination node's port is already connected");
src_node.outputs[src.port_index].connections.push_back(dst);
dst_node.inputs[dst.port_index].connections.push_back(src);
}
@ -165,7 +166,7 @@ bool ProgramGraph::disconnect(PortLocation src, PortLocation dst) {
return false;
}
uint32_t dst_i = dst_node.find_input_connection(src, dst.port_index);
CRASH_COND(dst_i == NULL_INDEX);
ZN_ASSERT(dst_i != NULL_INDEX);
std::vector<PortLocation> &src_connections = src_node.outputs[src.port_index].connections;
std::vector<PortLocation> &dst_connections = dst_node.inputs[dst.port_index].connections;
src_connections.erase(src_connections.begin() + src_i);
@ -197,9 +198,9 @@ bool ProgramGraph::is_output_port_valid(PortLocation loc) const {
ProgramGraph::Node &ProgramGraph::get_node(uint32_t id) const {
auto it = _nodes.find(id);
CRASH_COND(it == _nodes.end());
ZN_ASSERT(it != _nodes.end());
Node *node = it->second;
CRASH_COND(node == nullptr);
ZN_ASSERT(node != nullptr);
return *node;
}