Fix expression node crashes

This commit is contained in:
Chaosus 2019-06-22 17:41:51 +03:00
parent 05a0a68c72
commit 7bed1a0f21
2 changed files with 40 additions and 0 deletions

View file

@ -1762,6 +1762,7 @@ void VisualShaderNodeGroupBase::set_inputs(const String &p_inputs) {
for (int i = 0; i < input_port_count; i++) {
Vector<String> arr = input_strings[i].split(",");
ERR_FAIL_COND(arr.size() != 3);
int port_idx = arr[0].to_int();
int port_type = arr[1].to_int();
@ -1794,6 +1795,7 @@ void VisualShaderNodeGroupBase::set_outputs(const String &p_outputs) {
for (int i = 0; i < output_port_count; i++) {
Vector<String> arr = output_strings[i].split(",");
ERR_FAIL_COND(arr.size() != 3);
int port_idx = arr[0].to_int();
int port_type = arr[1].to_int();
@ -1810,6 +1812,23 @@ String VisualShaderNodeGroupBase::get_outputs() const {
return outputs;
}
bool VisualShaderNodeGroupBase::is_valid_port_name(const String &p_name) const {
if (!p_name.is_valid_identifier()) {
return false;
}
for (int i = 0; i < get_input_port_count(); i++) {
if (get_input_port_name(i) == p_name) {
return false;
}
}
for (int i = 0; i < get_output_port_count(); i++) {
if (get_output_port_name(i) == p_name) {
return false;
}
}
return true;
}
void VisualShaderNodeGroupBase::add_input_port(int p_id, int p_type, const String &p_name) {
String str = itos(p_id) + "," + itos(p_type) + "," + p_name + ";";
@ -1849,6 +1868,8 @@ void VisualShaderNodeGroupBase::add_input_port(int p_id, int p_type, const Strin
void VisualShaderNodeGroupBase::remove_input_port(int p_id) {
ERR_FAIL_COND(!has_input_port(p_id));
Vector<String> inputs_strings = inputs.split(";", false);
int count = 0;
int index = 0;
@ -1917,6 +1938,8 @@ void VisualShaderNodeGroupBase::add_output_port(int p_id, int p_type, const Stri
void VisualShaderNodeGroupBase::remove_output_port(int p_id) {
ERR_FAIL_COND(!has_output_port(p_id));
Vector<String> outputs_strings = outputs.split(";", false);
int count = 0;
int index = 0;
@ -1956,6 +1979,9 @@ void VisualShaderNodeGroupBase::clear_output_ports() {
void VisualShaderNodeGroupBase::set_input_port_type(int p_id, int p_type) {
ERR_FAIL_COND(!has_input_port(p_id));
ERR_FAIL_COND(p_type < 0 || p_type > PORT_TYPE_TRANSFORM);
if (input_ports[p_id].type == p_type)
return;
@ -1986,6 +2012,9 @@ VisualShaderNodeGroupBase::PortType VisualShaderNodeGroupBase::get_input_port_ty
void VisualShaderNodeGroupBase::set_input_port_name(int p_id, const String &p_name) {
ERR_FAIL_COND(!has_input_port(p_id));
ERR_FAIL_COND(!is_valid_port_name(p_name));
if (input_ports[p_id].name == p_name)
return;
@ -2016,6 +2045,9 @@ String VisualShaderNodeGroupBase::get_input_port_name(int p_id) const {
void VisualShaderNodeGroupBase::set_output_port_type(int p_id, int p_type) {
ERR_FAIL_COND(!has_output_port(p_id));
ERR_FAIL_COND(p_type < 0 || p_type > PORT_TYPE_TRANSFORM);
if (output_ports[p_id].type == p_type)
return;
@ -2045,6 +2077,10 @@ VisualShaderNodeGroupBase::PortType VisualShaderNodeGroupBase::get_output_port_t
}
void VisualShaderNodeGroupBase::set_output_port_name(int p_id, const String &p_name) {
ERR_FAIL_COND(!has_output_port(p_id));
ERR_FAIL_COND(!is_valid_port_name(p_name));
if (output_ports[p_id].name == p_name)
return;
@ -2125,6 +2161,8 @@ void VisualShaderNodeGroupBase::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_outputs", "outputs"), &VisualShaderNodeGroupBase::set_outputs);
ClassDB::bind_method(D_METHOD("get_outputs"), &VisualShaderNodeGroupBase::get_outputs);
ClassDB::bind_method(D_METHOD("is_valid_port_name", "name"), &VisualShaderNodeGroupBase::is_valid_port_name);
ClassDB::bind_method(D_METHOD("add_input_port", "id", "type", "name"), &VisualShaderNodeGroupBase::add_input_port);
ClassDB::bind_method(D_METHOD("remove_input_port", "id"), &VisualShaderNodeGroupBase::remove_input_port);
ClassDB::bind_method(D_METHOD("get_input_port_count"), &VisualShaderNodeGroupBase::get_input_port_count);

View file

@ -355,6 +355,8 @@ public:
void set_outputs(const String &p_outputs);
String get_outputs() const;
bool is_valid_port_name(const String &p_name) const;
void add_input_port(int p_id, int p_type, const String &p_name);
void remove_input_port(int p_id);
virtual int get_input_port_count() const;