1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-05 12:10:55 +00:00

GDScript: Fix issues with typed arrays

This commit is contained in:
Dmitrii Maganov
2022-11-27 09:56:53 +02:00
parent e9de988020
commit 5909f9f075
50 changed files with 959 additions and 555 deletions

View File

@@ -910,7 +910,6 @@ Error VariantParser::parse_value(Token &token, Variant &value, Stream *p_stream,
bool at_key = true;
String key;
Token token2;
bool need_comma = false;
while (true) {
@@ -920,18 +919,18 @@ Error VariantParser::parse_value(Token &token, Variant &value, Stream *p_stream,
}
if (at_key) {
Error err = get_token(p_stream, token2, line, r_err_str);
Error err = get_token(p_stream, token, line, r_err_str);
if (err != OK) {
return err;
}
if (token2.type == TK_PARENTHESIS_CLOSE) {
if (token.type == TK_PARENTHESIS_CLOSE) {
value = ref.is_valid() ? Variant(ref) : Variant(obj);
return OK;
}
if (need_comma) {
if (token2.type != TK_COMMA) {
if (token.type != TK_COMMA) {
r_err_str = "Expected '}' or ','";
return ERR_PARSE_ERROR;
} else {
@@ -940,31 +939,31 @@ Error VariantParser::parse_value(Token &token, Variant &value, Stream *p_stream,
}
}
if (token2.type != TK_STRING) {
if (token.type != TK_STRING) {
r_err_str = "Expected property name as string";
return ERR_PARSE_ERROR;
}
key = token2.value;
key = token.value;
err = get_token(p_stream, token2, line, r_err_str);
err = get_token(p_stream, token, line, r_err_str);
if (err != OK) {
return err;
}
if (token2.type != TK_COLON) {
if (token.type != TK_COLON) {
r_err_str = "Expected ':'";
return ERR_PARSE_ERROR;
}
at_key = false;
} else {
Error err = get_token(p_stream, token2, line, r_err_str);
Error err = get_token(p_stream, token, line, r_err_str);
if (err != OK) {
return err;
}
Variant v;
err = parse_value(token2, v, p_stream, line, r_err_str, p_res_parser);
err = parse_value(token, v, p_stream, line, r_err_str, p_res_parser);
if (err) {
return err;
}
@@ -1026,6 +1025,89 @@ Error VariantParser::parse_value(Token &token, Variant &value, Stream *p_stream,
return ERR_PARSE_ERROR;
}
}
} else if (id == "Array") {
Error err = OK;
get_token(p_stream, token, line, r_err_str);
if (token.type != TK_BRACKET_OPEN) {
r_err_str = "Expected '['";
return ERR_PARSE_ERROR;
}
get_token(p_stream, token, line, r_err_str);
if (token.type != TK_IDENTIFIER) {
r_err_str = "Expected type identifier";
return ERR_PARSE_ERROR;
}
static HashMap<StringName, Variant::Type> builtin_types;
if (builtin_types.is_empty()) {
for (int i = 1; i < Variant::VARIANT_MAX; i++) {
builtin_types[Variant::get_type_name((Variant::Type)i)] = (Variant::Type)i;
}
}
Array array = Array();
bool got_bracket_token = false;
if (builtin_types.has(token.value)) {
array.set_typed(builtin_types.get(token.value), StringName(), Variant());
} else if (token.value == "Resource" || token.value == "SubResource" || token.value == "ExtResource") {
Variant resource;
err = parse_value(token, resource, p_stream, line, r_err_str, p_res_parser);
if (err) {
if (token.value == "Resource" && err == ERR_PARSE_ERROR && r_err_str == "Expected '('" && token.type == TK_BRACKET_CLOSE) {
err = OK;
r_err_str = String();
array.set_typed(Variant::OBJECT, token.value, Variant());
got_bracket_token = true;
} else {
return err;
}
} else {
Ref<Script> script = resource;
if (script.is_valid() && script->is_valid()) {
array.set_typed(Variant::OBJECT, script->get_instance_base_type(), script);
}
}
} else if (ClassDB::class_exists(token.value)) {
array.set_typed(Variant::OBJECT, token.value, Variant());
}
if (!got_bracket_token) {
get_token(p_stream, token, line, r_err_str);
if (token.type != TK_BRACKET_CLOSE) {
r_err_str = "Expected ']'";
return ERR_PARSE_ERROR;
}
}
get_token(p_stream, token, line, r_err_str);
if (token.type != TK_PARENTHESIS_OPEN) {
r_err_str = "Expected '('";
return ERR_PARSE_ERROR;
}
get_token(p_stream, token, line, r_err_str);
if (token.type != TK_BRACKET_OPEN) {
r_err_str = "Expected '['";
return ERR_PARSE_ERROR;
}
Array values;
err = _parse_array(values, p_stream, line, r_err_str, p_res_parser);
if (err) {
return err;
}
get_token(p_stream, token, line, r_err_str);
if (token.type != TK_PARENTHESIS_CLOSE) {
r_err_str = "Expected ')'";
return ERR_PARSE_ERROR;
}
array.assign(values);
value = array;
} else if (id == "PackedByteArray" || id == "PoolByteArray" || id == "ByteArray") {
Vector<uint8_t> args;
Error err = _parse_construct<uint8_t>(p_stream, args, line, r_err_str);
@@ -1843,6 +1925,38 @@ Error VariantWriter::write(const Variant &p_variant, StoreStringFunc p_store_str
} break;
case Variant::ARRAY: {
Array array = p_variant;
if (array.get_typed_builtin() != Variant::NIL) {
p_store_string_func(p_store_string_ud, "Array[");
Variant::Type builtin_type = (Variant::Type)array.get_typed_builtin();
StringName class_name = array.get_typed_class_name();
Ref<Script> script = array.get_typed_script();
if (script.is_valid()) {
String resource_text = String();
if (p_encode_res_func) {
resource_text = p_encode_res_func(p_encode_res_ud, script);
}
if (resource_text.is_empty() && script->get_path().is_resource_file()) {
resource_text = "Resource(\"" + script->get_path() + "\")";
}
if (!resource_text.is_empty()) {
p_store_string_func(p_store_string_ud, resource_text);
} else {
ERR_PRINT("Failed to encode a path to a custom script for an array type.");
p_store_string_func(p_store_string_ud, class_name);
}
} else if (class_name != StringName()) {
p_store_string_func(p_store_string_ud, class_name);
} else {
p_store_string_func(p_store_string_ud, Variant::get_type_name(builtin_type));
}
p_store_string_func(p_store_string_ud, "](");
}
if (recursion_count > MAX_RECURSION) {
ERR_PRINT("Max recursion reached");
p_store_string_func(p_store_string_ud, "[]");
@@ -1850,7 +1964,6 @@ Error VariantWriter::write(const Variant &p_variant, StoreStringFunc p_store_str
recursion_count++;
p_store_string_func(p_store_string_ud, "[");
Array array = p_variant;
int len = array.size();
for (int i = 0; i < len; i++) {
if (i > 0) {
@@ -1862,11 +1975,14 @@ Error VariantWriter::write(const Variant &p_variant, StoreStringFunc p_store_str
p_store_string_func(p_store_string_ud, "]");
}
if (array.get_typed_builtin() != Variant::NIL) {
p_store_string_func(p_store_string_ud, ")");
}
} break;
case Variant::PACKED_BYTE_ARRAY: {
p_store_string_func(p_store_string_ud, "PackedByteArray(");
String s;
Vector<uint8_t> data = p_variant;
int len = data.size();
const uint8_t *ptr = data.ptr();
@@ -1954,15 +2070,11 @@ Error VariantWriter::write(const Variant &p_variant, StoreStringFunc p_store_str
int len = data.size();
const String *ptr = data.ptr();
String s;
//write_string("\n");
for (int i = 0; i < len; i++) {
if (i > 0) {
p_store_string_func(p_store_string_ud, ", ");
}
String str = ptr[i];
p_store_string_func(p_store_string_ud, "\"" + str.c_escape() + "\"");
p_store_string_func(p_store_string_ud, "\"" + ptr[i].c_escape() + "\"");
}
p_store_string_func(p_store_string_ud, ")");
@@ -2010,9 +2122,9 @@ Error VariantWriter::write(const Variant &p_variant, StoreStringFunc p_store_str
if (i > 0) {
p_store_string_func(p_store_string_ud, ", ");
}
p_store_string_func(p_store_string_ud, rtos_fix(ptr[i].r) + ", " + rtos_fix(ptr[i].g) + ", " + rtos_fix(ptr[i].b) + ", " + rtos_fix(ptr[i].a));
}
p_store_string_func(p_store_string_ud, ")");
} break;