diff options
Diffstat (limited to 'modules')
68 files changed, 5362 insertions, 426 deletions
diff --git a/modules/arkit/arkit_interface.h b/modules/arkit/arkit_interface.h index 8129611287..e1dbca1488 100644 --- a/modules/arkit/arkit_interface.h +++ b/modules/arkit/arkit_interface.h @@ -3,10 +3,10 @@ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ -/* http://www.godotengine.org */ +/* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ diff --git a/modules/arkit/arkit_interface.mm b/modules/arkit/arkit_interface.mm index 68844c54c2..9614f775a5 100644 --- a/modules/arkit/arkit_interface.mm +++ b/modules/arkit/arkit_interface.mm @@ -3,10 +3,10 @@ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ -/* http://www.godotengine.org */ +/* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ diff --git a/modules/arkit/arkit_session_delegate.h b/modules/arkit/arkit_session_delegate.h index afe093656b..9303552ca6 100644 --- a/modules/arkit/arkit_session_delegate.h +++ b/modules/arkit/arkit_session_delegate.h @@ -3,10 +3,10 @@ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ -/* http://www.godotengine.org */ +/* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ diff --git a/modules/arkit/arkit_session_delegate.mm b/modules/arkit/arkit_session_delegate.mm index 56485c987c..d4072fc391 100644 --- a/modules/arkit/arkit_session_delegate.mm +++ b/modules/arkit/arkit_session_delegate.mm @@ -3,10 +3,10 @@ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ -/* http://www.godotengine.org */ +/* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ diff --git a/modules/csg/csg.cpp b/modules/csg/csg.cpp index fd0d36eddf..f1b3fa2ac6 100644 --- a/modules/csg/csg.cpp +++ b/modules/csg/csg.cpp @@ -242,7 +242,7 @@ void CSGBrushOperation::BuildPoly::_clip_segment(const CSGBrush *p_brush, int p_ //check if edge and poly share a vertex, of so, assign it to segment_idx for (int i = 0; i < points.size(); i++) { for (int j = 0; j < 2; j++) { - if (Math::is_zero_approx(segment[j].distance_to(points[i].point))) { + if (segment[j] == points[i].point) { segment_idx[j] = i; inserted_points.push_back(i); break; @@ -310,7 +310,7 @@ void CSGBrushOperation::BuildPoly::_clip_segment(const CSGBrush *p_brush, int p_ Vector2 edgeseg[2] = { points[edges[i].points[0]].point, points[edges[i].points[1]].point }; Vector2 closest = Geometry::get_closest_point_to_segment_2d(segment[j], edgeseg); - if (Math::is_zero_approx(closest.distance_to(segment[j]))) { + if (closest == segment[j]) { //point rest of this edge res = closest; found = true; @@ -439,7 +439,7 @@ void CSGBrushOperation::BuildPoly::clip(const CSGBrush *p_brush, int p_face, Mes //transform A points to 2D - if (Math::is_zero_approx(segment[0].distance_to(segment[1]))) + if (segment[0] == segment[1]) return; //too small _clip_segment(p_brush, p_face, segment, mesh_merge, p_for_B); @@ -461,10 +461,10 @@ void CSGBrushOperation::_collision_callback(const CSGBrush *A, int p_face_a, Map { //check if either is a degenerate - if (Math::is_zero_approx(va[0].distance_to(va[1])) || Math::is_zero_approx(va[0].distance_to(va[2])) || Math::is_zero_approx(va[1].distance_to(va[2]))) + if (va[0] == va[1] || va[0] == va[2] || va[1] == va[2]) return; - if (Math::is_zero_approx(vb[0].distance_to(vb[1])) || Math::is_zero_approx(vb[0].distance_to(vb[2])) || Math::is_zero_approx(vb[1].distance_to(vb[2]))) + if (vb[0] == vb[1] || vb[0] == vb[2] || vb[1] == vb[2]) return; } diff --git a/modules/etc/texture_loader_pkm.cpp b/modules/etc/texture_loader_pkm.cpp index 3337460dfc..dd61d816d4 100644 --- a/modules/etc/texture_loader_pkm.cpp +++ b/modules/etc/texture_loader_pkm.cpp @@ -62,10 +62,8 @@ RES ResourceFormatPKM::load(const String &p_path, const String &p_original_path, f->set_endian_swap(true); ETC1Header h; - ERR_EXPLAIN("Invalid or Unsupported PKM texture file: " + p_path); f->get_buffer((uint8_t *)&h.tag, sizeof(h.tag)); - if (strncmp(h.tag, "PKM 10", sizeof(h.tag))) - ERR_FAIL_V(RES()); + ERR_FAIL_COND_V_MSG(strncmp(h.tag, "PKM 10", sizeof(h.tag)), RES(), "Invalid or unsupported PKM texture file: " + p_path + "."); h.format = f->get_16(); h.texWidth = f->get_16(); diff --git a/modules/gdnative/nativescript/nativescript.cpp b/modules/gdnative/nativescript/nativescript.cpp index 9f7c3880ec..7c313c983f 100644 --- a/modules/gdnative/nativescript/nativescript.cpp +++ b/modules/gdnative/nativescript/nativescript.cpp @@ -79,7 +79,7 @@ void NativeScript::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::STRING, "script_class_name"), "set_script_class_name", "get_script_class_name"); ADD_PROPERTY(PropertyInfo(Variant::STRING, "script_class_icon_path", PROPERTY_HINT_FILE), "set_script_class_icon_path", "get_script_class_icon_path"); - ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "new", &NativeScript::_new, MethodInfo(Variant::OBJECT, "new")); + ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "new", &NativeScript::_new, MethodInfo("new")); } #define NSL NativeScriptLanguage::get_singleton() diff --git a/modules/gdnative/pluginscript/pluginscript_script.cpp b/modules/gdnative/pluginscript/pluginscript_script.cpp index b82823ab64..94d38e1be1 100644 --- a/modules/gdnative/pluginscript/pluginscript_script.cpp +++ b/modules/gdnative/pluginscript/pluginscript_script.cpp @@ -50,7 +50,7 @@ #endif void PluginScript::_bind_methods() { - ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "new", &PluginScript::_new, MethodInfo(Variant::OBJECT, "new")); + ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "new", &PluginScript::_new, MethodInfo("new")); } PluginScriptInstance *PluginScript::_create_instance(const Variant **p_args, int p_argcount, Object *p_owner, Variant::CallError &r_error) { diff --git a/modules/gdscript/SCsub b/modules/gdscript/SCsub index 6904154953..6285e6bb54 100644 --- a/modules/gdscript/SCsub +++ b/modules/gdscript/SCsub @@ -9,3 +9,4 @@ env_gdscript.add_source_files(env.modules_sources, "*.cpp") if env['tools']: env_gdscript.add_source_files(env.modules_sources, "./editor/*.cpp") + env_gdscript.add_source_files(env.modules_sources, "./language_server/*.cpp") diff --git a/modules/gdscript/gdscript.cpp b/modules/gdscript/gdscript.cpp index d929bdb3e5..5dab063061 100644 --- a/modules/gdscript/gdscript.cpp +++ b/modules/gdscript/gdscript.cpp @@ -710,7 +710,7 @@ void GDScript::_get_property_list(List<PropertyInfo> *p_properties) const { void GDScript::_bind_methods() { - ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "new", &GDScript::_new, MethodInfo(Variant::OBJECT, "new")); + ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "new", &GDScript::_new, MethodInfo("new")); ClassDB::bind_method(D_METHOD("get_as_byte_code"), &GDScript::get_as_byte_code); } diff --git a/modules/gdscript/gdscript_editor.cpp b/modules/gdscript/gdscript_editor.cpp index 9f65a9fff1..7c01e85ff7 100644 --- a/modules/gdscript/gdscript_editor.cpp +++ b/modules/gdscript/gdscript_editor.cpp @@ -634,7 +634,7 @@ static GDScriptCompletionIdentifier _type_from_gdtype(const GDScriptDataType &p_ switch (p_gdtype.kind) { case GDScriptDataType::UNINITIALIZED: { - ERR_EXPLAIN("Uninitialized completion. Please report a bug."); + ERR_PRINT("Uninitialized completion. Please report a bug."); } break; case GDScriptDataType::BUILTIN: { ci.type.kind = GDScriptParser::DataType::BUILTIN; diff --git a/modules/gdscript/gdscript_parser.cpp b/modules/gdscript/gdscript_parser.cpp index 9a25e788c7..a9f22225a0 100644 --- a/modules/gdscript/gdscript_parser.cpp +++ b/modules/gdscript/gdscript_parser.cpp @@ -504,7 +504,7 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s Ref<GDScript> gds = res; if (gds.is_valid() && !gds->is_valid()) { - _set_error("Could not fully preload the script, possible cyclic reference or compilation error. Use 'load()' instead if a cyclic reference is intended."); + _set_error("Couldn't fully preload the script, possible cyclic reference or compilation error. Use \"load()\" instead if a cyclic reference is intended."); return NULL; } @@ -518,7 +518,7 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s } else if (tokenizer->get_token() == GDScriptTokenizer::TK_PR_YIELD) { if (!current_function) { - _set_error("yield() can only be used inside function blocks."); + _set_error("\"yield()\" can only be used inside function blocks."); return NULL; } @@ -526,7 +526,7 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s tokenizer->advance(); if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_OPEN) { - _set_error("Expected '(' after 'yield'"); + _set_error("Expected \"(\" after \"yield\"."); return NULL; } @@ -552,7 +552,7 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s yield->arguments.push_back(object); if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) { - _set_error("Expected ',' after first argument of 'yield'"); + _set_error("Expected \",\" after the first argument of \"yield\"."); return NULL; } @@ -578,7 +578,7 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s yield->arguments.push_back(signal); if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) { - _set_error("Expected ')' after second argument of 'yield'"); + _set_error("Expected \")\" after the second argument of \"yield\"."); return NULL; } @@ -592,7 +592,7 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s } else if (tokenizer->get_token() == GDScriptTokenizer::TK_SELF) { if (p_static) { - _set_error("'self'' not allowed in static function or constant expression"); + _set_error("\"self\" isn't allowed in a static function or constant expression."); return NULL; } //constant defined by tokenizer @@ -613,7 +613,7 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s if (identifier == StringName()) { - _set_error("Built-in type constant or static function expected after '.'"); + _set_error("Built-in type constant or static function expected after \".\"."); return NULL; } if (!Variant::has_constant(bi_type, identifier)) { @@ -2309,7 +2309,7 @@ void GDScriptParser::_generate_pattern(PatternNode *p_pattern, Node *p_node_to_m // static type check if possible if (pattern_type.has_type && to_match_type.has_type) { if (!_is_type_compatible(to_match_type, pattern_type) && !_is_type_compatible(pattern_type, to_match_type)) { - _set_error("Pattern type (" + pattern_type.to_string() + ") is not compatible with the type of the value to match (" + to_match_type.to_string() + ").", + _set_error("The pattern type (" + pattern_type.to_string() + ") isn't compatible with the type of the value to match (" + to_match_type.to_string() + ").", p_pattern->line); return; } @@ -2761,24 +2761,24 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) { case GDScriptTokenizer::TK_CF_PASS: { if (tokenizer->get_token(1) != GDScriptTokenizer::TK_SEMICOLON && tokenizer->get_token(1) != GDScriptTokenizer::TK_NEWLINE && tokenizer->get_token(1) != GDScriptTokenizer::TK_EOF) { - _set_error("Expected ';' or <NewLine>."); + _set_error("Expected \";\" or a line break."); return; } _mark_line_as_safe(tokenizer->get_token_line()); tokenizer->advance(); if (tokenizer->get_token() == GDScriptTokenizer::TK_SEMICOLON) { - // Ignore semicolon after 'pass' + // Ignore semicolon after 'pass'. tokenizer->advance(); } } break; case GDScriptTokenizer::TK_PR_VAR: { - //variale declaration and (eventual) initialization + // Variable declaration and (eventual) initialization. tokenizer->advance(); int var_line = tokenizer->get_token_line(); if (!tokenizer->is_token_literal(0, true)) { - _set_error("Expected identifier for local variable name."); + _set_error("Expected an identifier for the local variable name."); return; } StringName n = tokenizer->get_token_literal(); @@ -2786,7 +2786,7 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) { if (current_function) { for (int i = 0; i < current_function->arguments.size(); i++) { if (n == current_function->arguments[i]) { - _set_error("Variable '" + String(n) + "' already defined in the scope (at line: " + itos(current_function->line) + ")."); + _set_error("Variable \"" + String(n) + "\" already defined in the scope (at line " + itos(current_function->line) + ")."); return; } } @@ -2794,7 +2794,7 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) { BlockNode *check_block = p_block; while (check_block) { if (check_block->variables.has(n)) { - _set_error("Variable '" + String(n) + "' already defined in the scope (at line: " + itos(check_block->variables[n]->line) + ")."); + _set_error("Variable \"" + String(n) + "\" already defined in the scope (at line " + itos(check_block->variables[n]->line) + ")."); return; } check_block = check_block->parent_block; @@ -2816,7 +2816,7 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) { #endif tokenizer->advance(); } else if (!_parse_type(lv->datatype)) { - _set_error("Expected type for variable."); + _set_error("Expected a type for the variable."); return; } } @@ -2865,7 +2865,7 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) { lv->assign = assigned; if (!_end_statement()) { - _set_error("Expected end of statement (var)"); + _set_error("Expected end of statement (\"var\")."); return; } @@ -2894,7 +2894,7 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) { p_block->sub_blocks.push_back(cf_if->body); if (!_enter_indent_block(cf_if->body)) { - _set_error("Expected indented block after 'if'"); + _set_error("Expected an indented block after \"if\"."); p_block->end_line = tokenizer->get_token_line(); return; } @@ -2924,7 +2924,7 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) { if (tab_level.back()->get() > indent_level) { - _set_error("Invalid indent"); + _set_error("Invalid indentation."); return; } @@ -2955,7 +2955,7 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) { p_block->sub_blocks.push_back(cf_if->body); if (!_enter_indent_block(cf_if->body)) { - _set_error("Expected indented block after 'elif'"); + _set_error("Expected an indented block after \"elif\"."); p_block->end_line = tokenizer->get_token_line(); return; } @@ -2971,7 +2971,7 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) { } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CF_ELSE) { if (tab_level.back()->get() > indent_level) { - _set_error("Invalid indent"); + _set_error("Invalid indentation."); return; } @@ -2981,7 +2981,7 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) { p_block->sub_blocks.push_back(cf_if->body_else); if (!_enter_indent_block(cf_if->body_else)) { - _set_error("Expected indented block after 'else'"); + _set_error("Expected an indented block after \"else\"."); p_block->end_line = tokenizer->get_token_line(); return; } @@ -3026,7 +3026,7 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) { p_block->sub_blocks.push_back(cf_while->body); if (!_enter_indent_block(cf_while->body)) { - _set_error("Expected indented block after 'while'"); + _set_error("Expected an indented block after \"while\"."); p_block->end_line = tokenizer->get_token_line(); return; } @@ -3045,7 +3045,7 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) { if (!tokenizer->is_token_literal(0, true)) { - _set_error("identifier expected after 'for'"); + _set_error("Identifier expected after \"for\"."); } IdentifierNode *id = alloc_node<IdentifierNode>(); @@ -3054,7 +3054,7 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) { tokenizer->advance(); if (tokenizer->get_token() != GDScriptTokenizer::TK_OP_IN) { - _set_error("'in' expected after identifier"); + _set_error("\"in\" expected after identifier."); return; } @@ -3144,7 +3144,7 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) { p_block->sub_blocks.push_back(cf_for->body); if (!_enter_indent_block(cf_for->body)) { - _set_error("Expected indented block after 'for'"); + _set_error("Expected indented block after \"for\"."); p_block->end_line = tokenizer->get_token_line(); return; } @@ -3171,23 +3171,25 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) { } break; case GDScriptTokenizer::TK_CF_CONTINUE: { + _mark_line_as_safe(tokenizer->get_token_line()); tokenizer->advance(); ControlFlowNode *cf_continue = alloc_node<ControlFlowNode>(); cf_continue->cf_type = ControlFlowNode::CF_CONTINUE; p_block->statements.push_back(cf_continue); if (!_end_statement()) { - _set_error("Expected end of statement (continue)"); + _set_error("Expected end of statement (\"continue\")."); return; } } break; case GDScriptTokenizer::TK_CF_BREAK: { + _mark_line_as_safe(tokenizer->get_token_line()); tokenizer->advance(); ControlFlowNode *cf_break = alloc_node<ControlFlowNode>(); cf_break->cf_type = ControlFlowNode::CF_BREAK; p_block->statements.push_back(cf_break); if (!_end_statement()) { - _set_error("Expected end of statement (break)"); + _set_error("Expected end of statement (\"break\")."); return; } } break; @@ -3241,7 +3243,7 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) { match_node->val_to_match = val_to_match; if (!_enter_indent_block()) { - _set_error("Expected indented pattern matching block after 'match'"); + _set_error("Expected indented pattern matching block after \"match\"."); return; } @@ -3280,7 +3282,7 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) { p_block->statements.push_back(an); if (!_end_statement()) { - _set_error("Expected end of statement after assert."); + _set_error("Expected end of statement after \"assert\"."); return; } } break; @@ -3291,7 +3293,7 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) { p_block->statements.push_back(bn); if (!_end_statement()) { - _set_error("Expected end of statement after breakpoint."); + _set_error("Expected end of statement after \"breakpoint\"."); return; } } break; @@ -3323,7 +3325,7 @@ bool GDScriptParser::_parse_newline() { int current_indent = tab_level.back()->get(); if (indent > current_indent) { - _set_error("Unexpected indent."); + _set_error("Unexpected indentation."); return false; } @@ -3333,7 +3335,7 @@ bool GDScriptParser::_parse_newline() { //exit block if (tab_level.size() == 1) { - _set_error("Invalid indent. BUG?"); + _set_error("Invalid indentation. Bug?"); return false; } @@ -3360,13 +3362,13 @@ void GDScriptParser::_parse_extends(ClassNode *p_class) { if (p_class->extends_used) { - _set_error("'extends' already used for this class."); + _set_error("\"extends\" can only be present once per script."); return; } if (!p_class->constant_expressions.empty() || !p_class->subclasses.empty() || !p_class->functions.empty() || !p_class->variables.empty() || p_class->classname_used) { - _set_error("'extends' must be used before anything else."); + _set_error("\"extends\" must be used before anything else."); return; } @@ -3386,7 +3388,7 @@ void GDScriptParser::_parse_extends(ClassNode *p_class) { Variant constant = tokenizer->get_token_constant(); if (constant.get_type() != Variant::STRING) { - _set_error("'extends' constant must be a string."); + _set_error("\"extends\" constant must be a string."); return; } @@ -3421,7 +3423,7 @@ void GDScriptParser::_parse_extends(ClassNode *p_class) { default: { - _set_error("Invalid 'extends' syntax, expected string constant (path) and/or identifier (parent class)."); + _set_error("Invalid \"extends\" syntax, expected string constant (path) and/or identifier (parent class)."); return; } } @@ -3481,7 +3483,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { if (error_set) return; if (!_end_statement()) { - _set_error("Expected end of statement after extends"); + _set_error("Expected end of statement after \"extends\"."); return; } @@ -3490,20 +3492,20 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { _mark_line_as_safe(tokenizer->get_token_line()); if (p_class->owner) { - _set_error("'class_name' is only valid for the main class namespace."); + _set_error("\"class_name\" is only valid for the main class namespace."); return; } if (self_path.begins_with("res://") && self_path.find("::") != -1) { - _set_error("'class_name' not allowed in built-in scripts."); + _set_error("\"class_name\" isn't allowed in built-in scripts."); return; } if (tokenizer->get_token(1) != GDScriptTokenizer::TK_IDENTIFIER) { - _set_error("'class_name' syntax: 'class_name <UniqueName>'"); + _set_error("\"class_name\" syntax: \"class_name <UniqueName>\""); return; } if (p_class->classname_used) { - _set_error("'class_name' already used for this class."); + _set_error("\"class_name\" can only be present once per script."); return; } @@ -3512,12 +3514,12 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { p_class->name = tokenizer->get_token_identifier(1); if (self_path != String() && ScriptServer::is_global_class(p_class->name) && ScriptServer::get_global_class_path(p_class->name) != self_path) { - _set_error("Unique global class '" + p_class->name + "' already exists at path: " + ScriptServer::get_global_class_path(p_class->name)); + _set_error("Unique global class \"" + p_class->name + "\" already exists at path: " + ScriptServer::get_global_class_path(p_class->name)); return; } if (ClassDB::class_exists(p_class->name)) { - _set_error("Class '" + p_class->name + "' shadows a native class."); + _set_error("The class \"" + p_class->name + "\" shadows a native class."); return; } @@ -3544,12 +3546,12 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { tokenizer->advance(); } else { - _set_error("Optional parameter after 'class_name' must be a string constant file path to an icon."); + _set_error("The optional parameter after \"class_name\" must be a string constant file path to an icon."); return; } } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CONSTANT) { - _set_error("Class icon must be separated by a comma."); + _set_error("The class icon must be separated by a comma."); return; } @@ -3558,7 +3560,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { if (p_class->tool) { - _set_error("tool used more than once"); + _set_error("The \"tool\" keyword can only be present once per script."); return; } @@ -3573,7 +3575,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { if (tokenizer->get_token(1) != GDScriptTokenizer::TK_IDENTIFIER) { - _set_error("'class' syntax: 'class <Name>:' or 'class <Name> extends <BaseClass>:'"); + _set_error("\"class\" syntax: \"class <Name>:\" or \"class <Name> extends <BaseClass>:\""); return; } name = tokenizer->get_token_identifier(1); @@ -3581,23 +3583,23 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { // Check if name is shadowing something else if (ClassDB::class_exists(name) || ClassDB::class_exists("_" + name.operator String())) { - _set_error("Class '" + String(name) + "' shadows a native class."); + _set_error("The class \"" + String(name) + "\" shadows a native class."); return; } if (ScriptServer::is_global_class(name)) { - _set_error("Can't override name of unique global class '" + name + "' already exists at path: " + ScriptServer::get_global_class_path(p_class->name)); + _set_error("Can't override name of the unique global class \"" + name + "\". It already exists at: " + ScriptServer::get_global_class_path(p_class->name)); return; } ClassNode *outer_class = p_class; while (outer_class) { for (int i = 0; i < outer_class->subclasses.size(); i++) { if (outer_class->subclasses[i]->name == name) { - _set_error("Another class named '" + String(name) + "' already exists in this scope (at line " + itos(outer_class->subclasses[i]->line) + ")."); + _set_error("Another class named \"" + String(name) + "\" already exists in this scope (at line " + itos(outer_class->subclasses[i]->line) + ")."); return; } } if (outer_class->constant_expressions.has(name)) { - _set_error("A constant named '" + String(name) + "' already exists in the outer class scope (at line" + itos(outer_class->constant_expressions[name].expression->line) + ")."); + _set_error("A constant named \"" + String(name) + "\" already exists in the outer class scope (at line" + itos(outer_class->constant_expressions[name].expression->line) + ")."); return; } @@ -3641,7 +3643,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { tokenizer->advance(); if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) { - _set_error("Expected 'func'."); + _set_error("Expected \"func\"."); return; } @@ -3665,18 +3667,18 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { if (name == StringName()) { - _set_error("Expected identifier after 'func' (syntax: 'func <identifier>([arguments]):' )."); + _set_error("Expected an identifier after \"func\" (syntax: \"func <identifier>([arguments]):\")."); return; } for (int i = 0; i < p_class->functions.size(); i++) { if (p_class->functions[i]->name == name) { - _set_error("Function '" + String(name) + "' already exists in this class (at line: " + itos(p_class->functions[i]->line) + ")."); + _set_error("The function \"" + String(name) + "\" already exists in this class (at line " + itos(p_class->functions[i]->line) + ")."); } } for (int i = 0; i < p_class->static_functions.size(); i++) { if (p_class->static_functions[i]->name == name) { - _set_error("Function '" + String(name) + "' already exists in this class (at line: " + itos(p_class->static_functions[i]->line) + ")."); + _set_error("The function \"" + String(name) + "\" already exists in this class (at line " + itos(p_class->static_functions[i]->line) + ")."); } } @@ -3698,7 +3700,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_OPEN) { - _set_error("Expected '(' after identifier (syntax: 'func <identifier>([arguments]):' )."); + _set_error("Expected \"(\" after the identifier (syntax: \"func <identifier>([arguments]):\" )."); return; } @@ -3730,7 +3732,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { if (!tokenizer->is_token_literal(0, true)) { - _set_error("Expected identifier for argument."); + _set_error("Expected an identifier for an argument."); return; } @@ -3748,7 +3750,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { argtype.infer_type = true; tokenizer->advance(); } else if (!_parse_type(argtype)) { - _set_error("Expected type for argument."); + _set_error("Expected a type for an argument."); return; } } @@ -3797,7 +3799,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { continue; } else if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) { - _set_error("Expected ',' or ')'."); + _set_error("Expected \",\" or \")\"."); return; } @@ -3826,7 +3828,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { if (name == "_init") { if (_static) { - _set_error("Constructor cannot be static."); + _set_error("The constructor cannot be static."); return; } @@ -3843,7 +3845,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { if (tokenizer->get_token() == GDScriptTokenizer::TK_PERIOD) { tokenizer->advance(); if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_OPEN) { - _set_error("expected '(' for parent constructor arguments."); + _set_error("Expected \"(\" for parent constructor arguments."); return; } tokenizer->advance(); @@ -3863,7 +3865,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { continue; } else if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) { - _set_error("Expected ',' or ')'."); + _set_error("Expected \",\" or \")\"."); return; } @@ -3888,7 +3890,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { if (tokenizer->get_token() == GDScriptTokenizer::TK_FORWARD_ARROW) { if (!_parse_type(return_type, true)) { - _set_error("Expected return type for function."); + _set_error("Expected a return type for the function."); return; } } @@ -3918,7 +3920,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { tokenizer->advance(); if (!tokenizer->is_token_literal()) { - _set_error("Expected identifier after 'signal'."); + _set_error("Expected an identifier after \"signal\"."); return; } @@ -3942,7 +3944,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { } if (!tokenizer->is_token_literal(0, true)) { - _set_error("Expected identifier in signal argument."); + _set_error("Expected an identifier in a \"signal\" argument."); return; } @@ -3956,7 +3958,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) { tokenizer->advance(); } else if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) { - _set_error("Expected ',' or ')' after signal parameter identifier."); + _set_error("Expected \",\" or \")\" after a \"signal\" parameter identifier."); return; } } @@ -3965,7 +3967,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { p_class->_signals.push_back(sig); if (!_end_statement()) { - _set_error("Expected end of statement (signal)"); + _set_error("Expected end of statement (\"signal\")."); return; } } break; @@ -4024,7 +4026,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { break; } if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) { - _set_error("Expected ',' in bit flags hint."); + _set_error("Expected \",\" in the bit flags hint."); return; } @@ -4036,7 +4038,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) { current_export = PropertyInfo(); - _set_error("Expected a string constant in named bit flags hint."); + _set_error("Expected a string constant in the named bit flags hint."); return; } @@ -4054,7 +4056,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) { current_export = PropertyInfo(); - _set_error("Expected ')' or ',' in named bit flags hint."); + _set_error("Expected \")\" or \",\" in the named bit flags hint."); return; } tokenizer->advance(); @@ -4067,7 +4069,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { tokenizer->advance(); if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) { - _set_error("Expected ')' in layers 2D render hint."); + _set_error("Expected \")\" in the layers 2D render hint."); return; } current_export.hint = PROPERTY_HINT_LAYERS_2D_RENDER; @@ -4078,7 +4080,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { tokenizer->advance(); if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) { - _set_error("Expected ')' in layers 2D physics hint."); + _set_error("Expected \")\" in the layers 2D physics hint."); return; } current_export.hint = PROPERTY_HINT_LAYERS_2D_PHYSICS; @@ -4089,7 +4091,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { tokenizer->advance(); if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) { - _set_error("Expected ')' in layers 3D render hint."); + _set_error("Expected \")\" in the layers 3D render hint."); return; } current_export.hint = PROPERTY_HINT_LAYERS_3D_RENDER; @@ -4100,7 +4102,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { tokenizer->advance(); if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) { - _set_error("Expected ')' in layers 3D physics hint."); + _set_error("Expected \")\" in the layers 3D physics hint."); return; } current_export.hint = PROPERTY_HINT_LAYERS_3D_PHYSICS; @@ -4116,7 +4118,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) { current_export = PropertyInfo(); - _set_error("Expected a string constant in enumeration hint."); + _set_error("Expected a string constant in the enumeration hint."); return; } @@ -4134,7 +4136,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) { current_export = PropertyInfo(); - _set_error("Expected ')' or ',' in enumeration hint."); + _set_error("Expected \")\" or \",\" in the enumeration hint."); return; } @@ -4152,7 +4154,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { current_export.hint = PROPERTY_HINT_EXP_EASING; tokenizer->advance(); if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) { - _set_error("Expected ')' in hint."); + _set_error("Expected \")\" in the hint."); return; } break; @@ -4167,7 +4169,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) break; else if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) { - _set_error("Expected ')' or ',' in exponential range hint."); + _set_error("Expected \")\" or \",\" in the exponential range hint."); return; } tokenizer->advance(); @@ -4183,7 +4185,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) { current_export = PropertyInfo(); - _set_error("Expected a range in numeric hint."); + _set_error("Expected a range in the numeric hint."); return; } @@ -4198,7 +4200,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) { current_export = PropertyInfo(); - _set_error("Expected ',' or ')' in numeric range hint."); + _set_error("Expected \",\" or \")\" in the numeric range hint."); return; } @@ -4213,7 +4215,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) { current_export = PropertyInfo(); - _set_error("Expected a number as upper bound in numeric range hint."); + _set_error("Expected a number as upper bound in the numeric range hint."); return; } @@ -4226,7 +4228,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) { current_export = PropertyInfo(); - _set_error("Expected ',' or ')' in numeric range hint."); + _set_error("Expected \",\" or \")\" in the numeric range hint."); return; } @@ -4240,7 +4242,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) { current_export = PropertyInfo(); - _set_error("Expected a number as step in numeric range hint."); + _set_error("Expected a number as step in the numeric range hint."); return; } @@ -4259,7 +4261,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) { current_export = PropertyInfo(); - _set_error("Expected a string constant in enumeration hint."); + _set_error("Expected a string constant in the enumeration hint."); return; } @@ -4276,7 +4278,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) { current_export = PropertyInfo(); - _set_error("Expected ')' or ',' in enumeration hint."); + _set_error("Expected \")\" or \",\" in the enumeration hint."); return; } tokenizer->advance(); @@ -4296,7 +4298,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { tokenizer->advance(); if (tokenizer->get_token() != GDScriptTokenizer::TK_IDENTIFIER || !(tokenizer->get_token_identifier() == "GLOBAL")) { - _set_error("Expected 'GLOBAL' after comma in directory hint."); + _set_error("Expected \"GLOBAL\" after comma in the directory hint."); return; } if (!p_class->tool) { @@ -4307,11 +4309,11 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { tokenizer->advance(); if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) { - _set_error("Expected ')' in hint."); + _set_error("Expected \")\" in the hint."); return; } } else { - _set_error("Expected ')' or ',' in hint."); + _set_error("Expected \")\" or \",\" in the hint."); return; } break; @@ -4340,7 +4342,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { else if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) tokenizer->advance(); else { - _set_error("Expected ')' or ',' in hint."); + _set_error("Expected \")\" or \",\" in the hint."); return; } } @@ -4348,9 +4350,9 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) { if (current_export.hint == PROPERTY_HINT_GLOBAL_FILE) - _set_error("Expected string constant with filter"); + _set_error("Expected string constant with filter."); else - _set_error("Expected 'GLOBAL' or string constant with filter"); + _set_error("Expected \"GLOBAL\" or string constant with filter."); return; } current_export.hint_string = tokenizer->get_token_constant(); @@ -4358,7 +4360,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { } if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) { - _set_error("Expected ')' in hint."); + _set_error("Expected \")\" in the hint."); return; } break; @@ -4369,7 +4371,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { current_export.hint = PROPERTY_HINT_MULTILINE_TEXT; tokenizer->advance(); if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) { - _set_error("Expected ')' in hint."); + _set_error("Expected \")\" in the hint."); return; } break; @@ -4380,7 +4382,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { if (tokenizer->get_token() != GDScriptTokenizer::TK_IDENTIFIER) { current_export = PropertyInfo(); - _set_error("Color type hint expects RGB or RGBA as hints"); + _set_error("Color type hint expects RGB or RGBA as hints."); return; } @@ -4391,7 +4393,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { //none } else { current_export = PropertyInfo(); - _set_error("Color type hint expects RGB or RGBA as hints"); + _set_error("Color type hint expects RGB or RGBA as hints."); return; } tokenizer->advance(); @@ -4400,7 +4402,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { default: { current_export = PropertyInfo(); - _set_error("Type '" + Variant::get_type_name(type) + "' can't take hints."); + _set_error("Type \"" + Variant::get_type_name(type) + "\" can't take hints."); return; } break; } @@ -4438,7 +4440,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { } else { current_export = PropertyInfo(); - _set_error("Export hint not a resource type."); + _set_error("The export hint isn't a resource type."); } } else if (constant.get_type() == Variant::DICTIONARY) { // Enumeration @@ -4452,7 +4454,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { tokenizer->advance(); } else { current_export = PropertyInfo(); - _set_error("Expected 'FLAGS' after comma."); + _set_error("Expected \"FLAGS\" after comma."); } } @@ -4489,7 +4491,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) { current_export = PropertyInfo(); - _set_error("Expected ')' or ',' after export hint."); + _set_error("Expected \")\" or \",\" after the export hint."); return; } @@ -4509,7 +4511,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_ONREADY && tokenizer->get_token() != GDScriptTokenizer::TK_PR_REMOTE && tokenizer->get_token() != GDScriptTokenizer::TK_PR_MASTER && tokenizer->get_token() != GDScriptTokenizer::TK_PR_PUPPET && tokenizer->get_token() != GDScriptTokenizer::TK_PR_SYNC && tokenizer->get_token() != GDScriptTokenizer::TK_PR_REMOTESYNC && tokenizer->get_token() != GDScriptTokenizer::TK_PR_MASTERSYNC && tokenizer->get_token() != GDScriptTokenizer::TK_PR_PUPPETSYNC && tokenizer->get_token() != GDScriptTokenizer::TK_PR_SLAVE) { current_export = PropertyInfo(); - _set_error("Expected 'var', 'onready', 'remote', 'master', 'puppet', 'sync', 'remotesync', 'mastersync', 'puppetsync'."); + _set_error("Expected \"var\", \"onready\", \"remote\", \"master\", \"puppet\", \"sync\", \"remotesync\", \"mastersync\", \"puppetsync\"."); return; } @@ -4520,7 +4522,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { //may be fallthrough from export, ignore if so tokenizer->advance(); if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR) { - _set_error("Expected 'var'."); + _set_error("Expected \"var\"."); return; } @@ -4532,13 +4534,13 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { tokenizer->advance(); if (current_export.type) { if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR) { - _set_error("Expected 'var'."); + _set_error("Expected \"var\"."); return; } } else { if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) { - _set_error("Expected 'var' or 'func'."); + _set_error("Expected \"var\" or \"func\"."); return; } } @@ -4552,13 +4554,13 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { tokenizer->advance(); if (current_export.type) { if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR) { - _set_error("Expected 'var'."); + _set_error("Expected \"var\"."); return; } } else { if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) { - _set_error("Expected 'var' or 'func'."); + _set_error("Expected \"var\" or \"func\"."); return; } } @@ -4577,13 +4579,13 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { tokenizer->advance(); if (current_export.type) { if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR) { - _set_error("Expected 'var'."); + _set_error("Expected \"var\"."); return; } } else { if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) { - _set_error("Expected 'var' or 'func'."); + _set_error("Expected \"var\" or \"func\"."); return; } } @@ -4598,9 +4600,9 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { tokenizer->advance(); if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) { if (current_export.type) - _set_error("Expected 'var'."); + _set_error("Expected \"var\"."); else - _set_error("Expected 'var' or 'func'."); + _set_error("Expected \"var\" or \"func\"."); return; } @@ -4613,9 +4615,9 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { tokenizer->advance(); if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) { if (current_export.type) - _set_error("Expected 'var'."); + _set_error("Expected \"var\"."); else - _set_error("Expected 'var' or 'func'."); + _set_error("Expected \"var\" or \"func\"."); return; } @@ -4628,9 +4630,9 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { tokenizer->advance(); if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) { if (current_export.type) - _set_error("Expected 'var'."); + _set_error("Expected \"var\"."); else - _set_error("Expected 'var' or 'func'."); + _set_error("Expected \"var\" or \"func\"."); return; } @@ -4653,7 +4655,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { tokenizer->advance(); if (!tokenizer->is_token_literal(0, true)) { - _set_error("Expected identifier for member variable name."); + _set_error("Expected an identifier for the member variable name."); return; } @@ -4669,14 +4671,14 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { #endif if (current_class->constant_expressions.has(member.identifier)) { - _set_error("A constant named '" + String(member.identifier) + "' already exists in this class (at line: " + + _set_error("A constant named \"" + String(member.identifier) + "\" already exists in this class (at line: " + itos(current_class->constant_expressions[member.identifier].expression->line) + ")."); return; } for (int i = 0; i < current_class->variables.size(); i++) { if (current_class->variables[i].identifier == member.identifier) { - _set_error("Variable '" + String(member.identifier) + "' already exists in this class (at line: " + + _set_error("Variable \"" + String(member.identifier) + "\" already exists in this class (at line: " + itos(current_class->variables[i].line) + ")."); return; } @@ -4684,7 +4686,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { for (int i = 0; i < current_class->subclasses.size(); i++) { if (current_class->subclasses[i]->name == member.identifier) { - _set_error("A class named '" + String(member.identifier) + "' already exists in this class (at line " + itos(current_class->subclasses[i]->line) + ")."); + _set_error("A class named \"" + String(member.identifier) + "\" already exists in this class (at line " + itos(current_class->subclasses[i]->line) + ")."); return; } } @@ -4714,7 +4716,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { #endif tokenizer->advance(); } else if (!_parse_type(member.data_type)) { - _set_error("Expected type for class variable."); + _set_error("Expected a type for the class variable."); return; } } @@ -4742,7 +4744,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { IdentifierNode *id = static_cast<IdentifierNode *>(op->arguments[1]); if (id->name == "get_node") { - _set_error("Use 'onready var " + String(member.identifier) + " = get_node(..)' instead"); + _set_error("Use \"onready var " + String(member.identifier) + " = get_node(...)\" instead."); return; } } @@ -4770,7 +4772,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { Object *obj = cn->value; Resource *res = Object::cast_to<Resource>(obj); if (res == NULL) { - _set_error("Exported constant not a type or resource."); + _set_error("The exported constant isn't a type or resource."); return; } member._export.hint = PROPERTY_HINT_RESOURCE_TYPE; @@ -4788,7 +4790,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { const Variant *args = &cn->value; cn->value = Variant::construct(member._export.type, &args, 1, err); } else { - _set_error("Cannot convert the provided value to the export type."); + _set_error("Can't convert the provided value to the export type."); return; } } @@ -4886,7 +4888,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) { //just comma means using only getter if (!tokenizer->is_token_literal()) { - _set_error("Expected identifier for setter function after 'setget'."); + _set_error("Expected an identifier for the setter function after \"setget\"."); } member.setter = tokenizer->get_token_literal(); @@ -4899,7 +4901,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { tokenizer->advance(); if (!tokenizer->is_token_literal()) { - _set_error("Expected identifier for getter function after ','."); + _set_error("Expected an identifier for the getter function after \",\"."); } member.getter = tokenizer->get_token_literal(); @@ -4910,7 +4912,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { p_class->variables.push_back(member); if (!_end_statement()) { - _set_error("Expected end of statement (continue)"); + _set_error("Expected end of statement (\"continue\")."); return; } } break; @@ -4922,7 +4924,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { tokenizer->advance(); if (!tokenizer->is_token_literal(0, true)) { - _set_error("Expected name (identifier) for constant."); + _set_error("Expected an identifier for the constant."); return; } @@ -4930,14 +4932,14 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { int line = tokenizer->get_token_line(); if (current_class->constant_expressions.has(const_id)) { - _set_error("Constant '" + String(const_id) + "' already exists in this class (at line: " + + _set_error("Constant \"" + String(const_id) + "\" already exists in this class (at line " + itos(current_class->constant_expressions[const_id].expression->line) + ")."); return; } for (int i = 0; i < current_class->variables.size(); i++) { if (current_class->variables[i].identifier == const_id) { - _set_error("A variable named '" + String(const_id) + "' already exists in this class (at line: " + + _set_error("A variable named \"" + String(const_id) + "\" already exists in this class (at line " + itos(current_class->variables[i].line) + ")."); return; } @@ -4945,7 +4947,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { for (int i = 0; i < current_class->subclasses.size(); i++) { if (current_class->subclasses[i]->name == const_id) { - _set_error("A class named '" + String(const_id) + "' already exists in this class (at line " + itos(current_class->subclasses[i]->line) + ")."); + _set_error("A class named \"" + String(const_id) + "\" already exists in this class (at line " + itos(current_class->subclasses[i]->line) + ")."); return; } } @@ -4960,13 +4962,13 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { #endif tokenizer->advance(); } else if (!_parse_type(constant.type)) { - _set_error("Expected type for class constant."); + _set_error("Expected a type for the class constant."); return; } } if (tokenizer->get_token() != GDScriptTokenizer::TK_OP_ASSIGN) { - _set_error("Constant expects assignment."); + _set_error("Constants must be assigned immediately."); return; } @@ -4981,7 +4983,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { } if (subexpr->type != Node::TYPE_CONSTANT) { - _set_error("Expected constant expression", line); + _set_error("Expected a constant expression.", line); return; } subexpr->line = line; @@ -4990,7 +4992,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { p_class->constant_expressions.insert(const_id, constant); if (!_end_statement()) { - _set_error("Expected end of statement (constant)", line); + _set_error("Expected end of statement (constant).", line); return; } @@ -5007,14 +5009,14 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { enum_name = tokenizer->get_token_literal(); if (current_class->constant_expressions.has(enum_name)) { - _set_error("A constant named '" + String(enum_name) + "' already exists in this class (at line: " + + _set_error("A constant named \"" + String(enum_name) + "\" already exists in this class (at line " + itos(current_class->constant_expressions[enum_name].expression->line) + ")."); return; } for (int i = 0; i < current_class->variables.size(); i++) { if (current_class->variables[i].identifier == enum_name) { - _set_error("A variable named '" + String(enum_name) + "' already exists in this class (at line: " + + _set_error("A variable named \"" + String(enum_name) + "\" already exists in this class (at line " + itos(current_class->variables[i].line) + ")."); return; } @@ -5022,7 +5024,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { for (int i = 0; i < current_class->subclasses.size(); i++) { if (current_class->subclasses[i]->name == enum_name) { - _set_error("A class named '" + String(enum_name) + "' already exists in this class (at line " + itos(current_class->subclasses[i]->line) + ")."); + _set_error("A class named \"" + String(enum_name) + "\" already exists in this class (at line " + itos(current_class->subclasses[i]->line) + ")."); return; } } @@ -5030,7 +5032,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { tokenizer->advance(); } if (tokenizer->get_token() != GDScriptTokenizer::TK_CURLY_BRACKET_OPEN) { - _set_error("Expected '{' in enum declaration"); + _set_error("Expected \"{\" in the enum declaration."); return; } tokenizer->advance(); @@ -5048,7 +5050,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { if (tokenizer->get_token() == GDScriptTokenizer::TK_EOF) { _set_error("Unexpected end of file."); } else { - _set_error(String("Unexpected ") + GDScriptTokenizer::get_token_name(tokenizer->get_token()) + ", expected identifier"); + _set_error(String("Unexpected ") + GDScriptTokenizer::get_token_name(tokenizer->get_token()) + ", expected an identifier."); } return; @@ -5071,14 +5073,14 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { } if (subexpr->type != Node::TYPE_CONSTANT) { - _set_error("Expected constant expression"); + _set_error("Expected a constant expression."); return; } enum_value_expr = static_cast<ConstantNode *>(subexpr); if (enum_value_expr->value.get_type() != Variant::INT) { - _set_error("Expected an int value for enum"); + _set_error("Expected an integer value for \"enum\"."); return; } @@ -5094,7 +5096,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) { tokenizer->advance(); } else if (tokenizer->is_token_literal(0, true)) { - _set_error("Unexpected identifier"); + _set_error("Unexpected identifier."); return; } @@ -5102,14 +5104,14 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { enum_dict[const_id] = enum_value_expr->value; } else { if (current_class->constant_expressions.has(const_id)) { - _set_error("A constant named '" + String(const_id) + "' already exists in this class (at line: " + + _set_error("A constant named \"" + String(const_id) + "\" already exists in this class (at line " + itos(current_class->constant_expressions[const_id].expression->line) + ")."); return; } for (int i = 0; i < current_class->variables.size(); i++) { if (current_class->variables[i].identifier == const_id) { - _set_error("A variable named '" + String(const_id) + "' already exists in this class (at line: " + + _set_error("A variable named \"" + String(const_id) + "\" already exists in this class (at line " + itos(current_class->variables[i].line) + ")."); return; } @@ -5117,7 +5119,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { for (int i = 0; i < current_class->subclasses.size(); i++) { if (current_class->subclasses[i]->name == const_id) { - _set_error("A class named '" + String(const_id) + "' already exists in this class (at line " + itos(current_class->subclasses[i]->line) + ")."); + _set_error("A class named \"" + String(const_id) + "\" already exists in this class (at line " + itos(current_class->subclasses[i]->line) + ")."); return; } } @@ -5144,7 +5146,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) { } if (!_end_statement()) { - _set_error("Expected end of statement (enum)"); + _set_error("Expected end of statement (\"enum\")."); return; } @@ -5190,19 +5192,19 @@ void GDScriptParser::_determine_inheritance(ClassNode *p_class, bool p_recursive String base = base_path; if (base == "" || base.is_rel_path()) { - _set_error("Could not resolve relative path for parent class: " + path, p_class->line); + _set_error("Couldn't resolve relative path for the parent class: " + path, p_class->line); return; } path = base.plus_file(path).simplify_path(); } script = ResourceLoader::load(path); if (script.is_null()) { - _set_error("Could not load base class: " + path, p_class->line); + _set_error("Couldn't load the base class: " + path, p_class->line); return; } if (!script->is_valid()) { - _set_error("Script not fully loaded (cyclic preload?): " + path, p_class->line); + _set_error("Script isn't fully loaded (cyclic preload?): " + path, p_class->line); return; } @@ -5217,7 +5219,7 @@ void GDScriptParser::_determine_inheritance(ClassNode *p_class, bool p_recursive script = subclass; } else { - _set_error("Could not find subclass: " + sub, p_class->line); + _set_error("Couldn't find the subclass: " + sub, p_class->line); return; } } @@ -5239,7 +5241,7 @@ void GDScriptParser::_determine_inheritance(ClassNode *p_class, bool p_recursive if (ScriptServer::is_global_class(base)) { base_script = ResourceLoader::load(ScriptServer::get_global_class_path(base)); if (!base_script.is_valid()) { - _set_error("Class '" + base + "' could not be fully loaded (script error or cyclic dependency).", p_class->line); + _set_error("The class \"" + base + "\" couldn't be fully loaded (script error or cyclic dependency).", p_class->line); return; } p = NULL; @@ -5280,13 +5282,13 @@ void GDScriptParser::_determine_inheritance(ClassNode *p_class, bool p_recursive if (p->constant_expressions.has(base)) { if (p->constant_expressions[base].expression->type != Node::TYPE_CONSTANT) { - _set_error("Could not resolve constant '" + base + "'.", p_class->line); + _set_error("Couldn't resolve the constant \"" + base + "\".", p_class->line); return; } const ConstantNode *cn = static_cast<const ConstantNode *>(p->constant_expressions[base].expression); base_script = cn->value; if (base_script.is_null()) { - _set_error("Constant is not a class: " + base, p_class->line); + _set_error("Constant isn't a class: " + base, p_class->line); return; } break; @@ -5313,13 +5315,13 @@ void GDScriptParser::_determine_inheritance(ClassNode *p_class, bool p_recursive Ref<GDScript> new_base_class = base_script->get_constants()[subclass]; if (new_base_class.is_null()) { - _set_error("Constant is not a class: " + ident, p_class->line); + _set_error("Constant isn't a class: " + ident, p_class->line); return; } find_subclass = new_base_class; } else { - _set_error("Could not find subclass: " + ident, p_class->line); + _set_error("Couldn't find the subclass: " + ident, p_class->line); return; } } @@ -5330,13 +5332,13 @@ void GDScriptParser::_determine_inheritance(ClassNode *p_class, bool p_recursive if (p_class->extends_class.size() > 1) { - _set_error("Invalid inheritance (unknown class + subclasses)", p_class->line); + _set_error("Invalid inheritance (unknown class + subclasses).", p_class->line); return; } //if not found, try engine classes if (!GDScriptLanguage::get_singleton()->get_global_map().has(base)) { - _set_error("Unknown class: '" + base + "'", p_class->line); + _set_error("Unknown class: \"" + base + "\"", p_class->line); return; } @@ -5358,7 +5360,7 @@ void GDScriptParser::_determine_inheritance(ClassNode *p_class, bool p_recursive p_class->base_type.kind = DataType::NATIVE; p_class->base_type.native_type = native; } else { - _set_error("Could not determine inheritance", p_class->line); + _set_error("Couldn't determine inheritance.", p_class->line); return; } @@ -5503,7 +5505,7 @@ bool GDScriptParser::_parse_type(DataType &r_type, bool p_can_be_void) { switch (tokenizer->get_token()) { case GDScriptTokenizer::TK_PERIOD: { if (!can_index) { - _set_error("Unexpected '.'."); + _set_error("Unexpected \".\"."); return false; } can_index = false; @@ -5534,7 +5536,7 @@ bool GDScriptParser::_parse_type(DataType &r_type, bool p_can_be_void) { } if (tokenizer->get_token(-1) == GDScriptTokenizer::TK_PERIOD) { - _set_error("Expected subclass identifier."); + _set_error("Expected a subclass identifier."); return false; } @@ -5572,7 +5574,7 @@ GDScriptParser::DataType GDScriptParser::_resolve_type(const DataType &p_source, Ref<GDScript> gds = script; if (gds.is_valid()) { if (!gds->is_valid()) { - _set_error("Class '" + id + "' could not be fully loaded (script error or cyclic dependency).", p_line); + _set_error("The class \"" + id + "\" couldn't be fully loaded (script error or cyclic dependency).", p_line); return DataType(); } result.kind = DataType::GDSCRIPT; @@ -5581,7 +5583,7 @@ GDScriptParser::DataType GDScriptParser::_resolve_type(const DataType &p_source, result.kind = DataType::SCRIPT; result.script_type = script; } else { - _set_error("Class '" + id + "' was found in global scope but its script could not be loaded.", p_line); + _set_error("The class \"" + id + "\" was found in global scope, but its script couldn't be loaded.", p_line); return DataType(); } } @@ -5682,8 +5684,8 @@ GDScriptParser::DataType GDScriptParser::_resolve_type(const DataType &p_source, } else { base = result.to_string(); } - _set_error("Identifier '" + String(id) + "' is not a valid type (not a script or class), or could not be found on base '" + - base + "'.", + _set_error("The identifier \"" + String(id) + "\" isn't a valid type (not a script or class), or couldn't be found on base \"" + + base + "\".", p_line); return DataType(); } @@ -5761,7 +5763,7 @@ GDScriptParser::DataType GDScriptParser::_type_from_gdtype(const GDScriptDataTyp switch (p_gdtype.kind) { case GDScriptDataType::UNINITIALIZED: { - ERR_EXPLAIN("Uninitialized datatype. Please report a bug."); + ERR_PRINT("Uninitialized datatype. Please report a bug."); } break; case GDScriptDataType::BUILTIN: { result.kind = DataType::BUILTIN; @@ -6147,8 +6149,8 @@ GDScriptParser::DataType GDScriptParser::_reduce_node_type(Node *p_node) { } if (!valid) { - _set_error("Invalid cast. Cannot convert from '" + source_type.to_string() + - "' to '" + cn->cast_type.to_string() + "'.", + _set_error("Invalid cast. Cannot convert from \"" + source_type.to_string() + + "\" to \"" + cn->cast_type.to_string() + "\".", cn->line); return DataType(); } @@ -6177,11 +6179,11 @@ GDScriptParser::DataType GDScriptParser::_reduce_node_type(Node *p_node) { DataType signal_type = _reduce_node_type(op->arguments[1]); // TODO: Check if signal exists when it's a constant if (base_type.has_type && base_type.kind == DataType::BUILTIN && base_type.builtin_type != Variant::NIL && base_type.builtin_type != Variant::OBJECT) { - _set_error("First argument of 'yield()' must be an object.", op->line); + _set_error("The first argument of \"yield()\" must be an object.", op->line); return DataType(); } if (signal_type.has_type && (signal_type.kind != DataType::BUILTIN || signal_type.builtin_type != Variant::STRING)) { - _set_error("Second argument of 'yield()' must be a string.", op->line); + _set_error("The second argument of \"yield()\" must be a string.", op->line); return DataType(); } } @@ -6201,15 +6203,15 @@ GDScriptParser::DataType GDScriptParser::_reduce_node_type(Node *p_node) { if (check_types && type_type.has_type) { if (!type_type.is_meta_type && (type_type.kind != DataType::NATIVE || !ClassDB::is_parent_class(type_type.native_type, "Script"))) { - _set_error("Invalid 'is' test: right operand is not a type (not a native type nor a script).", op->line); + _set_error("Invalid \"is\" test: the right operand isn't a type (neither a native type nor a script).", op->line); return DataType(); } type_type.is_meta_type = false; // Test the actual type if (!_is_type_compatible(type_type, value_type) && !_is_type_compatible(value_type, type_type)) { if (op->op == OperatorNode::OP_IS) { - _set_error("A value of type '" + value_type.to_string() + "' will never be an instance of '" + type_type.to_string() + "'.", op->line); + _set_error("A value of type \"" + value_type.to_string() + "\" will never be an instance of \"" + type_type.to_string() + "\".", op->line); } else { - _set_error("A value of type '" + value_type.to_string() + "' will never be of type '" + type_type.to_string() + "'.", op->line); + _set_error("A value of type \"" + value_type.to_string() + "\" will never be of type \"" + type_type.to_string() + "\".", op->line); } return DataType(); } @@ -6237,8 +6239,8 @@ GDScriptParser::DataType GDScriptParser::_reduce_node_type(Node *p_node) { node_type = _get_operation_type(var_op, argument_type, argument_type, valid); if (check_types && !valid) { - _set_error("Invalid operand type ('" + argument_type.to_string() + - "') to unary operator '" + Variant::get_operator_name(var_op) + "'.", + _set_error("Invalid operand type (\"" + argument_type.to_string() + + "\") to unary operator \"" + Variant::get_operator_name(var_op) + "\".", op->line, op->column); return DataType(); } @@ -6282,8 +6284,8 @@ GDScriptParser::DataType GDScriptParser::_reduce_node_type(Node *p_node) { node_type = _get_operation_type(var_op, argument_a_type, argument_b_type, valid); if (check_types && !valid) { - _set_error("Invalid operand types ('" + argument_a_type.to_string() + "' and '" + - argument_b_type.to_string() + "') to operator '" + Variant::get_operator_name(var_op) + "'.", + _set_error("Invalid operand types (\"" + argument_a_type.to_string() + "\" and \"" + + argument_b_type.to_string() + "\") to operator \"" + Variant::get_operator_name(var_op) + "\".", op->line, op->column); return DataType(); } @@ -6298,7 +6300,7 @@ GDScriptParser::DataType GDScriptParser::_reduce_node_type(Node *p_node) { // Ternary operators case OperatorNode::OP_TERNARY_IF: { if (op->arguments.size() != 3) { - _set_error("Parser bug: ternary operation without 3 arguments"); + _set_error("Parser bug: ternary operation without 3 arguments."); ERR_FAIL_V(DataType()); } @@ -6331,7 +6333,7 @@ GDScriptParser::DataType GDScriptParser::_reduce_node_type(Node *p_node) { case OperatorNode::OP_ASSIGN_BIT_XOR: case OperatorNode::OP_INIT_ASSIGN: { - _set_error("Assignment inside expression is not allowed (parser bug?).", op->line); + _set_error("Assignment inside an expression isn't allowed (parser bug?).", op->line); return DataType(); } break; @@ -6367,8 +6369,8 @@ GDScriptParser::DataType GDScriptParser::_reduce_node_type(Node *p_node) { if (valid) { result = _type_from_variant(res); } else if (check_types) { - _set_error("Can't get index '" + String(member_id->name.operator String()) + "' on base '" + - base_type.to_string() + "'.", + _set_error("Can't get index \"" + String(member_id->name.operator String()) + "\" on base \"" + + base_type.to_string() + "\".", op->line); return DataType(); } @@ -6460,7 +6462,7 @@ GDScriptParser::DataType GDScriptParser::_reduce_node_type(Node *p_node) { } } if (error) { - _set_error("Invalid index type (" + index_type.to_string() + ") for base '" + base_type.to_string() + "'.", + _set_error("Invalid index type (" + index_type.to_string() + ") for base \"" + base_type.to_string() + "\".", op->line); return DataType(); } @@ -6493,8 +6495,8 @@ GDScriptParser::DataType GDScriptParser::_reduce_node_type(Node *p_node) { node_type = _type_from_variant(res); node_type.is_constant = false; } else if (check_types) { - _set_error("Can't get index '" + String(cn->value) + "' on base '" + - base_type.to_string() + "'.", + _set_error("Can't get index \"" + String(cn->value) + "\" on base \"" + + base_type.to_string() + "\".", op->line); return DataType(); } @@ -6504,7 +6506,7 @@ GDScriptParser::DataType GDScriptParser::_reduce_node_type(Node *p_node) { _mark_line_as_unsafe(op->line); } } else if (!for_completion && (index_type.kind != DataType::BUILTIN || index_type.builtin_type != Variant::STRING)) { - _set_error("Only strings can be used as index in the base type '" + base_type.to_string() + "'.", op->line); + _set_error("Only strings can be used as an index in the base type \"" + base_type.to_string() + "\".", op->line); return DataType(); } } @@ -6521,7 +6523,7 @@ GDScriptParser::DataType GDScriptParser::_reduce_node_type(Node *p_node) { case Variant::REAL: case Variant::NODE_PATH: case Variant::_RID: { - _set_error("Can't index on a value of type '" + base_type.to_string() + "'.", op->line); + _set_error("Can't index on a value of type \"" + base_type.to_string() + "\".", op->line); return DataType(); } break; // Return int @@ -6919,7 +6921,7 @@ GDScriptParser::DataType GDScriptParser::_reduce_function_call_type(const Operat if (check_types) { if (!tmp.has_method(callee_name)) { - _set_error("Method '" + callee_name + "' is not declared on base '" + base_type.to_string() + "'.", p_call->line); + _set_error("The method \"" + callee_name + "\" isn't declared on base \"" + base_type.to_string() + "\".", p_call->line); return DataType(); } @@ -6979,7 +6981,7 @@ GDScriptParser::DataType GDScriptParser::_reduce_function_call_type(const Operat if (!valid) { #ifdef DEBUG_ENABLED if (p_call->arguments[0]->type == Node::TYPE_SELF) { - _set_error("Method '" + callee_name + "' is not declared in the current class.", p_call->line); + _set_error("The method \"" + callee_name + "\" isn't declared in the current class.", p_call->line); return DataType(); } DataType tmp_type; @@ -7004,7 +7006,7 @@ GDScriptParser::DataType GDScriptParser::_reduce_function_call_type(const Operat } if (check_types && !is_static && !is_initializer && base_type.is_meta_type) { - _set_error("Non-static function '" + String(callee_name) + "' can only be called from an instance.", p_call->line); + _set_error("Non-static function \"" + String(callee_name) + "\" can only be called from an instance.", p_call->line); return DataType(); } @@ -7029,11 +7031,11 @@ GDScriptParser::DataType GDScriptParser::_reduce_function_call_type(const Operat } if (arg_count < arg_types.size() - default_args_count) { - _set_error("Too few arguments for '" + callee_name + "()' call. Expected at least " + itos(arg_types.size() - default_args_count) + ".", p_call->line); + _set_error("Too few arguments for \"" + callee_name + "()\" call. Expected at least " + itos(arg_types.size() - default_args_count) + ".", p_call->line); return return_type; } if (!is_vararg && arg_count > arg_types.size()) { - _set_error("Too many arguments for '" + callee_name + "()' call. Expected at most " + itos(arg_types.size()) + ".", p_call->line); + _set_error("Too many arguments for \"" + callee_name + "()\" call. Expected at most " + itos(arg_types.size()) + ".", p_call->line); return return_type; } @@ -7055,7 +7057,7 @@ GDScriptParser::DataType GDScriptParser::_reduce_function_call_type(const Operat } else if (!_is_type_compatible(arg_types[i - arg_diff], par_type, true)) { // Supertypes are acceptable for dynamic compliance if (!_is_type_compatible(par_type, arg_types[i - arg_diff])) { - _set_error("At '" + callee_name + "()' call, argument " + itos(i - arg_diff + 1) + ". Assigned type (" + + _set_error("At \"" + callee_name + "()\" call, argument " + itos(i - arg_diff + 1) + ". Assigned type (" + par_type.to_string() + ") doesn't match the function argument's type (" + arg_types[i - arg_diff].to_string() + ").", p_call->line); @@ -7203,7 +7205,7 @@ bool GDScriptParser::_get_member_type(const DataType &p_base_type, const StringN } if (!ClassDB::class_exists(native)) { if (!check_types) return false; - ERR_FAIL_V_MSG(false, "Parser bug: Class '" + String(native) + "' not found."); + ERR_FAIL_V_MSG(false, "Parser bug: Class \"" + String(native) + "\" not found."); } bool valid = false; @@ -7373,7 +7375,7 @@ GDScriptParser::DataType GDScriptParser::_reduce_identifier_type(const DataType Ref<GDScript> gds = scr; if (gds.is_valid()) { if (!gds->is_valid()) { - _set_error("Class '" + p_identifier + "' could not be fully loaded (script error or cyclic dependency)."); + _set_error("The class \"" + p_identifier + "\" couldn't be fully loaded (script error or cyclic dependency)."); return DataType(); } result.kind = DataType::GDSCRIPT; @@ -7382,7 +7384,7 @@ GDScriptParser::DataType GDScriptParser::_reduce_identifier_type(const DataType } return result; } - _set_error("Class '" + p_identifier + "' was found in global scope but its script could not be loaded."); + _set_error("The class \"" + p_identifier + "\" was found in global scope, but its script couldn't be loaded."); return DataType(); } @@ -7425,7 +7427,7 @@ GDScriptParser::DataType GDScriptParser::_reduce_identifier_type(const DataType Ref<GDScript> gds = singleton; if (gds.is_valid()) { if (!gds->is_valid()) { - _set_error("Couldn't fully load singleton script '" + p_identifier + "' (possible cyclic reference or parse error).", p_line); + _set_error("Couldn't fully load the singleton script \"" + p_identifier + "\" (possible cyclic reference or parse error).", p_line); return DataType(); } result.kind = DataType::GDSCRIPT; @@ -7437,7 +7439,7 @@ GDScriptParser::DataType GDScriptParser::_reduce_identifier_type(const DataType } // This means looking in the current class, which type is always known - _set_error("Identifier '" + p_identifier.operator String() + "' is not declared in the current scope.", p_line); + _set_error("The identifier \"" + p_identifier.operator String() + "\" isn't declared in the current scope.", p_line); } #ifdef DEBUG_ENABLED @@ -7469,7 +7471,7 @@ void GDScriptParser::_check_class_level_types(ClassNode *p_class) { DataType expr = _resolve_type(c.expression->get_datatype(), c.expression->line); if (check_types && !_is_type_compatible(cont, expr)) { - _set_error("Constant value type (" + expr.to_string() + ") is not compatible with declared type (" + cont.to_string() + ").", + _set_error("The constant value type (" + expr.to_string() + ") isn't compatible with declared type (" + cont.to_string() + ").", c.expression->line); return; } @@ -7480,7 +7482,7 @@ void GDScriptParser::_check_class_level_types(ClassNode *p_class) { DataType tmp; if (_get_member_type(p_class->base_type, E->key(), tmp)) { - _set_error("Member '" + String(E->key()) + "' already exists in parent class.", c.expression->line); + _set_error("The member \"" + String(E->key()) + "\" already exists in a parent class.", c.expression->line); return; } } @@ -7502,7 +7504,7 @@ void GDScriptParser::_check_class_level_types(ClassNode *p_class) { DataType tmp; if (_get_member_type(p_class->base_type, v.identifier, tmp)) { - _set_error("Member '" + String(v.identifier) + "' already exists in parent class.", v.line); + _set_error("The member \"" + String(v.identifier) + "\" already exists in a parent class.", v.line); return; } @@ -7519,7 +7521,7 @@ void GDScriptParser::_check_class_level_types(ClassNode *p_class) { } else { // Try with implicit conversion if (v.data_type.kind != DataType::BUILTIN || !_is_type_compatible(v.data_type, expr_type, true)) { - _set_error("Assigned expression type (" + expr_type.to_string() + ") doesn't match the variable's type (" + + _set_error("The assigned expression's type (" + expr_type.to_string() + ") doesn't match the variable's type (" + v.data_type.to_string() + ").", v.line); return; @@ -7548,7 +7550,7 @@ void GDScriptParser::_check_class_level_types(ClassNode *p_class) { if (v.data_type.infer_type) { if (!expr_type.has_type) { - _set_error("Assigned value does not have a set type, variable type cannot be inferred.", v.line); + _set_error("The assigned value doesn't have a set type; the variable type can't be inferred.", v.line); return; } v.data_type = expr_type; @@ -7560,7 +7562,7 @@ void GDScriptParser::_check_class_level_types(ClassNode *p_class) { if (v.data_type.has_type && v._export.type != Variant::NIL) { DataType export_type = _type_from_property(v._export); if (!_is_type_compatible(v.data_type, export_type, true)) { - _set_error("Export hint type (" + export_type.to_string() + ") doesn't match the variable's type (" + + _set_error("The export hint's type (" + export_type.to_string() + ") doesn't match the variable's type (" + v.data_type.to_string() + ").", v.line); return; @@ -7579,15 +7581,15 @@ void GDScriptParser::_check_class_level_types(ClassNode *p_class) { if (setter->get_required_argument_count() != 1 && !(setter->get_required_argument_count() == 0 && setter->default_values.size() > 0)) { - _set_error("Setter function needs to receive exactly 1 argument. See '" + setter->name + - "()' definition at line " + itos(setter->line) + ".", + _set_error("The setter function needs to receive exactly 1 argument. See \"" + setter->name + + "()\" definition at line " + itos(setter->line) + ".", v.line); return; } if (!_is_type_compatible(v.data_type, setter->argument_types[0])) { - _set_error("Setter argument type (" + setter->argument_types[0].to_string() + - ") doesn't match the variable's type (" + v.data_type.to_string() + "). See '" + - setter->name + "()' definition at line " + itos(setter->line) + ".", + _set_error("The setter argument's type (" + setter->argument_types[0].to_string() + + ") doesn't match the variable's type (" + v.data_type.to_string() + "). See \"" + + setter->name + "()\" definition at line " + itos(setter->line) + ".", v.line); return; } @@ -7598,15 +7600,15 @@ void GDScriptParser::_check_class_level_types(ClassNode *p_class) { FunctionNode *getter = p_class->functions[j]; if (getter->get_required_argument_count() != 0) { - _set_error("Getter function can't receive arguments. See '" + getter->name + - "()' definition at line " + itos(getter->line) + ".", + _set_error("The getter function can't receive arguments. See \"" + getter->name + + "()\" definition at line " + itos(getter->line) + ".", v.line); return; } if (!_is_type_compatible(v.data_type, getter->get_datatype())) { - _set_error("Getter return type (" + getter->get_datatype().to_string() + + _set_error("The getter return type (" + getter->get_datatype().to_string() + ") doesn't match the variable's type (" + v.data_type.to_string() + - "). See '" + getter->name + "()' definition at line " + itos(getter->line) + ".", + "). See \"" + getter->name + "()\" definition at line " + itos(getter->line) + ".", v.line); return; } @@ -7620,23 +7622,23 @@ void GDScriptParser::_check_class_level_types(ClassNode *p_class) { for (int j = 0; j < p_class->static_functions.size(); j++) { if (v.setter == p_class->static_functions[j]->name) { FunctionNode *setter = p_class->static_functions[j]; - _set_error("Setter can't be a static function. See '" + setter->name + "()' definition at line " + itos(setter->line) + ".", v.line); + _set_error("The setter can't be a static function. See \"" + setter->name + "()\" definition at line " + itos(setter->line) + ".", v.line); return; } if (v.getter == p_class->static_functions[j]->name) { FunctionNode *getter = p_class->static_functions[j]; - _set_error("Getter can't be a static function. See '" + getter->name + "()' definition at line " + itos(getter->line) + ".", v.line); + _set_error("The getter can't be a static function. See \"" + getter->name + "()\" definition at line " + itos(getter->line) + ".", v.line); return; } } if (!found_setter && v.setter != StringName()) { - _set_error("Setter function is not defined.", v.line); + _set_error("The setter function isn't defined.", v.line); return; } if (!found_getter && v.getter != StringName()) { - _set_error("Getter function is not defined.", v.line); + _set_error("The getter function isn't defined.", v.line); return; } } @@ -7683,7 +7685,7 @@ void GDScriptParser::_check_function_types(FunctionNode *p_function) { if (!_is_type_compatible(p_function->argument_types[i], def_type, true)) { String arg_name = p_function->arguments[i]; _set_error("Value type (" + def_type.to_string() + ") doesn't match the type of argument '" + - arg_name + "' (" + p_function->arguments[i] + ")", + arg_name + "' (" + p_function->arguments[i] + ").", p_function->line); } } @@ -7746,21 +7748,21 @@ void GDScriptParser::_check_function_types(FunctionNode *p_function) { } } parent_signature += ")"; - _set_error("Function signature doesn't match the parent. Parent signature is: '" + parent_signature + "'.", p_function->line); + _set_error("The function signature doesn't match the parent. Parent signature is: \"" + parent_signature + "\".", p_function->line); return; } } #endif // DEBUG_ENABLED } else { if (p_function->return_type.has_type && (p_function->return_type.kind != DataType::BUILTIN || p_function->return_type.builtin_type != Variant::NIL)) { - _set_error("Constructor cannot return a value.", p_function->line); + _set_error("The constructor can't return a value.", p_function->line); return; } } if (p_function->return_type.has_type && (p_function->return_type.kind != DataType::BUILTIN || p_function->return_type.builtin_type != Variant::NIL)) { if (!p_function->body->has_return) { - _set_error("Non-void function must return a value in all possible paths.", p_function->line); + _set_error("A non-void function must return a value in all possible paths.", p_function->line); return; } } @@ -7891,7 +7893,7 @@ void GDScriptParser::_check_block_types(BlockNode *p_block) { } else { // Try implicit conversion if (lv->datatype.kind != DataType::BUILTIN || !_is_type_compatible(lv->datatype, assign_type, true)) { - _set_error("Assigned value type (" + assign_type.to_string() + ") doesn't match the variable's type (" + + _set_error("The assigned value type (" + assign_type.to_string() + ") doesn't match the variable's type (" + lv->datatype.to_string() + ").", lv->line); return; @@ -7923,7 +7925,7 @@ void GDScriptParser::_check_block_types(BlockNode *p_block) { } if (lv->datatype.infer_type) { if (!assign_type.has_type) { - _set_error("Assigned value does not have a set type, variable type cannot be inferred.", lv->line); + _set_error("The assigned value doesn't have a set type; the variable type can't be inferred.", lv->line); return; } lv->datatype = assign_type; @@ -7974,7 +7976,7 @@ void GDScriptParser::_check_block_types(BlockNode *p_block) { } } if (lh_type.is_constant) { - _set_error("Cannot assign a new value to a constant.", op->line); + _set_error("Can't assign a new value to a constant.", op->line); return; } } @@ -7993,8 +7995,8 @@ void GDScriptParser::_check_block_types(BlockNode *p_block) { rh_type = _get_operation_type(oper, lh_type, arg_type, valid); if (check_types && !valid) { - _set_error("Invalid operand types ('" + lh_type.to_string() + "' and '" + arg_type.to_string() + - "') to assignment operator '" + Variant::get_operator_name(oper) + "'.", + _set_error("Invalid operand types (\"" + lh_type.to_string() + "\" and \"" + arg_type.to_string() + + "\") to assignment operator \"" + Variant::get_operator_name(oper) + "\".", op->line); return; } @@ -8022,7 +8024,7 @@ void GDScriptParser::_check_block_types(BlockNode *p_block) { } else { // Try implicit conversion if (lh_type.kind != DataType::BUILTIN || !_is_type_compatible(lh_type, rh_type, true)) { - _set_error("Assigned value type (" + rh_type.to_string() + ") doesn't match the variable's type (" + + _set_error("The assigned value's type (" + rh_type.to_string() + ") doesn't match the variable's type (" + lh_type.to_string() + ").", op->line); return; @@ -8107,18 +8109,18 @@ void GDScriptParser::_check_block_types(BlockNode *p_block) { if (function_type.kind == DataType::BUILTIN && function_type.builtin_type == Variant::NIL) { // Return void, should not have arguments if (cf->arguments.size() > 0) { - _set_error("Void function cannot return a value.", cf->line, cf->column); + _set_error("A void function cannot return a value.", cf->line, cf->column); return; } } else { // Return something, cannot be empty if (cf->arguments.size() == 0) { - _set_error("Non-void function must return a value.", cf->line, cf->column); + _set_error("A non-void function must return a value.", cf->line, cf->column); return; } if (!_is_type_compatible(function_type, ret_type)) { - _set_error("Returned value type (" + ret_type.to_string() + ") doesn't match the function return type (" + + _set_error("The returned value type (" + ret_type.to_string() + ") doesn't match the function return type (" + function_type.to_string() + ").", cf->line, cf->column); return; @@ -8255,6 +8257,10 @@ int GDScriptParser::get_error_column() const { return error_column; } +bool GDScriptParser::has_error() const { + return error_set; +} + Error GDScriptParser::_parse(const String &p_base_path) { base_path = p_base_path; @@ -8271,7 +8277,7 @@ Error GDScriptParser::_parse(const String &p_base_path) { if (tokenizer->get_token() == GDScriptTokenizer::TK_ERROR) { error_set = false; - _set_error("Parse Error: " + tokenizer->get_token_error()); + _set_error("Parse error: " + tokenizer->get_token_error()); } if (error_set && !for_completion) { diff --git a/modules/gdscript/gdscript_parser.h b/modules/gdscript/gdscript_parser.h index 62d7bdb393..72aa819a8c 100644 --- a/modules/gdscript/gdscript_parser.h +++ b/modules/gdscript/gdscript_parser.h @@ -632,6 +632,7 @@ private: Error _parse(const String &p_base_path); public: + bool has_error() const; String get_error() const; int get_error_line() const; int get_error_column() const; diff --git a/modules/gdscript/language_server/gdscript_extend_parser.cpp b/modules/gdscript/language_server/gdscript_extend_parser.cpp new file mode 100644 index 0000000000..45f9ec9c6a --- /dev/null +++ b/modules/gdscript/language_server/gdscript_extend_parser.cpp @@ -0,0 +1,759 @@ +/*************************************************************************/ +/* gdscript_extend_parser.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "gdscript_extend_parser.h" +#include "../gdscript.h" +#include "core/io/json.h" +#include "gdscript_language_protocol.h" +#include "gdscript_workspace.h" + +void ExtendGDScriptParser::update_diagnostics() { + + diagnostics.clear(); + + if (has_error()) { + lsp::Diagnostic diagnostic; + diagnostic.severity = lsp::DiagnosticSeverity::Error; + diagnostic.message = get_error(); + diagnostic.source = "gdscript"; + diagnostic.code = -1; + lsp::Range range; + lsp::Position pos; + int line = LINE_NUMBER_TO_INDEX(get_error_line()); + const String &line_text = get_lines()[line]; + pos.line = line; + pos.character = line_text.length() - line_text.strip_edges(true, false).length(); + range.start = pos; + range.end = range.start; + range.end.character = line_text.strip_edges(false).length(); + diagnostic.range = range; + diagnostics.push_back(diagnostic); + } + + const List<GDScriptWarning> &warnings = get_warnings(); + for (const List<GDScriptWarning>::Element *E = warnings.front(); E; E = E->next()) { + const GDScriptWarning &warning = E->get(); + lsp::Diagnostic diagnostic; + diagnostic.severity = lsp::DiagnosticSeverity::Warning; + diagnostic.message = warning.get_message(); + diagnostic.source = "gdscript"; + diagnostic.code = warning.code; + lsp::Range range; + lsp::Position pos; + int line = LINE_NUMBER_TO_INDEX(warning.line); + const String &line_text = get_lines()[line]; + pos.line = line; + pos.character = line_text.length() - line_text.strip_edges(true, false).length(); + range.start = pos; + range.end = pos; + range.end.character = line_text.strip_edges(false).length(); + diagnostic.range = range; + diagnostics.push_back(diagnostic); + } +} + +void ExtendGDScriptParser::update_symbols() { + + members.clear(); + + const GDScriptParser::Node *head = get_parse_tree(); + if (const GDScriptParser::ClassNode *gdclass = dynamic_cast<const GDScriptParser::ClassNode *>(head)) { + + parse_class_symbol(gdclass, class_symbol); + + for (int i = 0; i < class_symbol.children.size(); i++) { + const lsp::DocumentSymbol &symbol = class_symbol.children[i]; + members.set(symbol.name, &symbol); + + // cache level one inner classes + if (symbol.kind == lsp::SymbolKind::Class) { + ClassMembers inner_class; + for (int j = 0; j < symbol.children.size(); j++) { + const lsp::DocumentSymbol &s = symbol.children[j]; + inner_class.set(s.name, &s); + } + inner_classes.set(symbol.name, inner_class); + } + } + } +} + +void ExtendGDScriptParser::parse_class_symbol(const GDScriptParser::ClassNode *p_class, lsp::DocumentSymbol &r_symbol) { + + const String uri = get_uri(); + + r_symbol.uri = uri; + r_symbol.script_path = path; + r_symbol.children.clear(); + r_symbol.name = p_class->name; + if (r_symbol.name.empty()) + r_symbol.name = path.get_file(); + r_symbol.kind = lsp::SymbolKind::Class; + r_symbol.deprecated = false; + r_symbol.range.start.line = LINE_NUMBER_TO_INDEX(p_class->line); + r_symbol.range.start.character = p_class->column; + r_symbol.range.end.line = LINE_NUMBER_TO_INDEX(p_class->end_line); + r_symbol.selectionRange.start.line = r_symbol.range.start.line; + r_symbol.detail = "class " + r_symbol.name; + bool is_root_class = &r_symbol == &class_symbol; + r_symbol.documentation = parse_documentation_as_markdown(is_root_class ? 0 : LINE_NUMBER_TO_INDEX(p_class->line), is_root_class); + + for (int i = 0; i < p_class->variables.size(); ++i) { + + const GDScriptParser::ClassNode::Member &m = p_class->variables[i]; + + lsp::DocumentSymbol symbol; + symbol.name = m.identifier; + symbol.kind = lsp::SymbolKind::Variable; + symbol.deprecated = false; + const int line = LINE_NUMBER_TO_INDEX(m.line); + symbol.range.start.line = line; + symbol.range.start.character = lines[line].length() - lines[line].strip_edges(true, false).length(); + symbol.range.end.line = line; + symbol.range.end.character = lines[line].length(); + symbol.selectionRange.start.line = symbol.range.start.line; + if (m._export.type != Variant::NIL) { + symbol.detail += "export "; + } + symbol.detail += "var " + m.identifier; + if (m.data_type.kind != GDScriptParser::DataType::UNRESOLVED) { + symbol.detail += ": " + m.data_type.to_string(); + } + if (m.default_value.get_type() != Variant::NIL) { + symbol.detail += " = " + JSON::print(m.default_value); + } + + symbol.documentation = parse_documentation_as_markdown(line); + symbol.uri = uri; + symbol.script_path = path; + + r_symbol.children.push_back(symbol); + } + + for (int i = 0; i < p_class->_signals.size(); ++i) { + const GDScriptParser::ClassNode::Signal &signal = p_class->_signals[i]; + + lsp::DocumentSymbol symbol; + symbol.name = signal.name; + symbol.kind = lsp::SymbolKind::Event; + symbol.deprecated = false; + const int line = LINE_NUMBER_TO_INDEX(signal.line); + symbol.range.start.line = line; + symbol.range.start.character = lines[line].length() - lines[line].strip_edges(true, false).length(); + symbol.range.end.line = symbol.range.start.line; + symbol.range.end.character = lines[line].length(); + symbol.selectionRange.start.line = symbol.range.start.line; + symbol.documentation = parse_documentation_as_markdown(line); + symbol.uri = uri; + symbol.script_path = path; + symbol.detail = "signal " + signal.name + "("; + for (int j = 0; j < signal.arguments.size(); j++) { + if (j > 0) { + symbol.detail += ", "; + } + symbol.detail += signal.arguments[j]; + } + symbol.detail += ")"; + + r_symbol.children.push_back(symbol); + } + + for (Map<StringName, GDScriptParser::ClassNode::Constant>::Element *E = p_class->constant_expressions.front(); E; E = E->next()) { + lsp::DocumentSymbol symbol; + const GDScriptParser::ClassNode::Constant &c = E->value(); + const GDScriptParser::ConstantNode *node = dynamic_cast<const GDScriptParser::ConstantNode *>(c.expression); + symbol.name = E->key(); + symbol.kind = lsp::SymbolKind::Constant; + symbol.deprecated = false; + const int line = LINE_NUMBER_TO_INDEX(E->get().expression->line); + symbol.range.start.line = line; + symbol.range.start.character = E->get().expression->column; + symbol.range.end.line = symbol.range.start.line; + symbol.range.end.character = lines[line].length(); + symbol.selectionRange.start.line = symbol.range.start.line; + symbol.documentation = parse_documentation_as_markdown(line); + symbol.uri = uri; + symbol.script_path = path; + + symbol.detail = "const " + symbol.name; + if (c.type.kind != GDScriptParser::DataType::UNRESOLVED) { + symbol.detail += ": " + c.type.to_string(); + } + + String value_text; + if (node->value.get_type() == Variant::OBJECT) { + RES res = node->value; + if (res.is_valid() && !res->get_path().empty()) { + value_text = "preload(\"" + res->get_path() + "\")"; + if (symbol.documentation.empty()) { + if (Map<String, ExtendGDScriptParser *>::Element *S = GDScriptLanguageProtocol::get_singleton()->get_workspace()->scripts.find(res->get_path())) { + symbol.documentation = S->get()->class_symbol.documentation; + } + } + } else { + value_text = JSON::print(node->value); + } + } else { + value_text = JSON::print(node->value); + } + if (!value_text.empty()) { + symbol.detail += " = " + value_text; + } + + r_symbol.children.push_back(symbol); + } + + for (int i = 0; i < p_class->functions.size(); ++i) { + const GDScriptParser::FunctionNode *func = p_class->functions[i]; + lsp::DocumentSymbol symbol; + parse_function_symbol(func, symbol); + r_symbol.children.push_back(symbol); + } + + for (int i = 0; i < p_class->static_functions.size(); ++i) { + const GDScriptParser::FunctionNode *func = p_class->static_functions[i]; + lsp::DocumentSymbol symbol; + parse_function_symbol(func, symbol); + r_symbol.children.push_back(symbol); + } + + for (int i = 0; i < p_class->subclasses.size(); ++i) { + const GDScriptParser::ClassNode *subclass = p_class->subclasses[i]; + lsp::DocumentSymbol symbol; + parse_class_symbol(subclass, symbol); + r_symbol.children.push_back(symbol); + } +} + +void ExtendGDScriptParser::parse_function_symbol(const GDScriptParser::FunctionNode *p_func, lsp::DocumentSymbol &r_symbol) { + + const String uri = get_uri(); + + r_symbol.name = p_func->name; + r_symbol.kind = lsp::SymbolKind::Function; + r_symbol.detail = "func " + p_func->name + "("; + r_symbol.deprecated = false; + const int line = LINE_NUMBER_TO_INDEX(p_func->line); + r_symbol.range.start.line = line; + r_symbol.range.start.character = p_func->column; + r_symbol.range.end.line = MAX(p_func->body->end_line - 2, p_func->body->line); + r_symbol.range.end.character = lines[r_symbol.range.end.line].length(); + r_symbol.selectionRange.start.line = r_symbol.range.start.line; + r_symbol.documentation = parse_documentation_as_markdown(line); + r_symbol.uri = uri; + r_symbol.script_path = path; + + String arguments; + for (int i = 0; i < p_func->arguments.size(); i++) { + lsp::DocumentSymbol symbol; + symbol.kind = lsp::SymbolKind::Variable; + symbol.name = p_func->arguments[i]; + symbol.range.start.line = LINE_NUMBER_TO_INDEX(p_func->body->line); + symbol.range.start.character = p_func->body->column; + symbol.range.end = symbol.range.start; + symbol.uri = uri; + symbol.script_path = path; + r_symbol.children.push_back(symbol); + if (i > 0) { + arguments += ", "; + } + arguments += String(p_func->arguments[i]); + if (p_func->argument_types[i].kind != GDScriptParser::DataType::UNRESOLVED) { + arguments += ": " + p_func->argument_types[i].to_string(); + } + int default_value_idx = i - (p_func->arguments.size() - p_func->default_values.size()); + if (default_value_idx >= 0) { + const GDScriptParser::ConstantNode *const_node = dynamic_cast<const GDScriptParser::ConstantNode *>(p_func->default_values[default_value_idx]); + if (const_node == NULL) { + const GDScriptParser::OperatorNode *operator_node = dynamic_cast<const GDScriptParser::OperatorNode *>(p_func->default_values[default_value_idx]); + if (operator_node) { + const_node = dynamic_cast<const GDScriptParser::ConstantNode *>(operator_node->next); + } + } + + if (const_node) { + String value = JSON::print(const_node->value); + arguments += " = " + value; + } + } + } + r_symbol.detail += arguments + ")"; + if (p_func->return_type.kind != GDScriptParser::DataType::UNRESOLVED) { + r_symbol.detail += " -> " + p_func->return_type.to_string(); + } + + for (const Map<StringName, LocalVarNode *>::Element *E = p_func->body->variables.front(); E; E = E->next()) { + lsp::DocumentSymbol symbol; + const GDScriptParser::LocalVarNode *var = E->value(); + symbol.name = E->key(); + symbol.kind = lsp::SymbolKind::Variable; + symbol.range.start.line = LINE_NUMBER_TO_INDEX(E->get()->line); + symbol.range.start.character = E->get()->column; + symbol.range.end.line = symbol.range.start.line; + symbol.range.end.character = lines[symbol.range.end.line].length(); + symbol.uri = uri; + symbol.script_path = path; + symbol.detail = "var " + symbol.name; + if (var->datatype.kind != GDScriptParser::DataType::UNRESOLVED) { + symbol.detail += ": " + var->datatype.to_string(); + } + symbol.documentation = parse_documentation_as_markdown(line); + r_symbol.children.push_back(symbol); + } +} + +String ExtendGDScriptParser::marked_documentation(const String &p_bbcode) { + + String markdown = p_bbcode.strip_edges(); + + Vector<String> lines = markdown.split("\n"); + bool in_code_block = false; + int code_block_indent = -1; + + markdown = ""; + for (int i = 0; i < lines.size(); i++) { + String line = lines[i]; + int block_start = line.find("[codeblock]"); + if (block_start != -1) { + code_block_indent = block_start; + in_code_block = true; + line = "'''gdscript"; + line = "\n"; + } else if (in_code_block) { + line = "\t" + line.substr(code_block_indent, line.length()); + } + + if (in_code_block && line.find("[/codeblock]") != -1) { + line = "'''\n"; + line = "\n"; + in_code_block = false; + } + + if (!in_code_block) { + line = line.strip_edges(); + line = line.replace("[code]", "`"); + line = line.replace("[/code]", "`"); + line = line.replace("[i]", "*"); + line = line.replace("[/i]", "*"); + line = line.replace("[b]", "**"); + line = line.replace("[/b]", "**"); + line = line.replace("[u]", "__"); + line = line.replace("[/u]", "__"); + line = line.replace("[method ", "`"); + line = line.replace("[member ", "`"); + line = line.replace("[signal ", "`"); + line = line.replace("[enum ", "`"); + line = line.replace("[constant ", "`"); + line = line.replace("[", "`"); + line = line.replace("]", "`"); + } + + if (!in_code_block && i < lines.size() - 1) { + line += "\n\n"; + } else if (i < lines.size() - 1) { + line += "\n"; + } + markdown += line; + } + return markdown; +} + +String ExtendGDScriptParser::parse_documentation(int p_line, bool p_docs_down) { + ERR_FAIL_INDEX_V(p_line, lines.size(), String()); + + List<String> doc_lines; + + if (!p_docs_down) { // inline comment + String inline_comment = lines[p_line]; + int comment_start = inline_comment.find("#"); + if (comment_start != -1) { + inline_comment = inline_comment.substr(comment_start, inline_comment.length()).strip_edges(); + if (inline_comment.length() > 1) { + doc_lines.push_back(inline_comment.substr(1, inline_comment.length())); + } + } + } + + int step = p_docs_down ? 1 : -1; + int start_line = p_docs_down ? p_line : p_line - 1; + for (int i = start_line; true; i += step) { + + if (i < 0 || i >= lines.size()) break; + + String line_comment = lines[i].strip_edges(true, false); + if (line_comment.begins_with("#")) { + line_comment = line_comment.substr(1, line_comment.length()); + if (p_docs_down) { + doc_lines.push_back(line_comment); + } else { + doc_lines.push_front(line_comment); + } + } else { + break; + } + } + + String doc; + for (List<String>::Element *E = doc_lines.front(); E; E = E->next()) { + doc += E->get() + "\n"; + } + return doc; +} + +String ExtendGDScriptParser::get_text_for_completion(const lsp::Position &p_cursor) const { + + String longthing; + int len = lines.size(); + for (int i = 0; i < len; i++) { + + if (i == p_cursor.line) { + longthing += lines[i].substr(0, p_cursor.character); + longthing += String::chr(0xFFFF); //not unicode, represents the cursor + longthing += lines[i].substr(p_cursor.character, lines[i].size()); + } else { + + longthing += lines[i]; + } + + if (i != len - 1) + longthing += "\n"; + } + + return longthing; +} + +String ExtendGDScriptParser::get_text_for_lookup_symbol(const lsp::Position &p_cursor, const String &p_symbol, bool p_func_requred) const { + String longthing; + int len = lines.size(); + for (int i = 0; i < len; i++) { + + if (i == p_cursor.line) { + String line = lines[i]; + String first_part = line.substr(0, p_cursor.character); + String last_part = line.substr(p_cursor.character + 1, lines[i].length()); + if (!p_symbol.empty()) { + String left_cursor_text; + for (int c = p_cursor.character - 1; c >= 0; c--) { + left_cursor_text = line.substr(c, p_cursor.character - c); + if (p_symbol.begins_with(left_cursor_text)) { + first_part = line.substr(0, c); + first_part += p_symbol; + break; + } + } + } + + longthing += first_part; + longthing += String::chr(0xFFFF); //not unicode, represents the cursor + if (p_func_requred) { + longthing += "("; // tell the parser this is a function call + } + longthing += last_part; + } else { + + longthing += lines[i]; + } + + if (i != len - 1) + longthing += "\n"; + } + + return longthing; +} + +String ExtendGDScriptParser::get_identifier_under_position(const lsp::Position &p_position, Vector2i &p_offset) const { + + ERR_FAIL_INDEX_V(p_position.line, lines.size(), ""); + String line = lines[p_position.line]; + ERR_FAIL_INDEX_V(p_position.character, line.size(), ""); + + int start_pos = p_position.character; + for (int c = p_position.character; c >= 0; c--) { + start_pos = c; + CharType ch = line[c]; + bool valid_char = (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_'; + if (!valid_char) { + break; + } + } + + int end_pos = p_position.character; + for (int c = p_position.character; c < line.length(); c++) { + CharType ch = line[c]; + bool valid_char = (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_'; + if (!valid_char) { + break; + } + end_pos = c; + } + if (start_pos < end_pos) { + p_offset.x = start_pos - p_position.character; + p_offset.y = end_pos - p_position.character; + return line.substr(start_pos + 1, end_pos - start_pos); + } + + return ""; +} + +String ExtendGDScriptParser::get_uri() const { + return GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_uri(path); +} + +const lsp::DocumentSymbol *ExtendGDScriptParser::search_symbol_defined_at_line(int p_line, const lsp::DocumentSymbol &p_parent) const { + const lsp::DocumentSymbol *ret = NULL; + if (p_line < p_parent.range.start.line) { + return ret; + } else if (p_parent.range.start.line == p_line) { + return &p_parent; + } else { + for (int i = 0; i < p_parent.children.size(); i++) { + ret = search_symbol_defined_at_line(p_line, p_parent.children[i]); + if (ret) { + break; + } + } + } + return ret; +} + +String ExtendGDScriptParser::parse_documentation_as_markdown(int p_line, bool p_docs_down) { + return marked_documentation(parse_documentation(p_line, p_docs_down)); +} + +const lsp::DocumentSymbol *ExtendGDScriptParser::get_symbol_defined_at_line(int p_line) const { + if (p_line <= 0) { + return &class_symbol; + } + return search_symbol_defined_at_line(p_line, class_symbol); +} + +const lsp::DocumentSymbol *ExtendGDScriptParser::get_member_symbol(const String &p_name, const String &p_subclass) const { + + if (p_subclass.empty()) { + const lsp::DocumentSymbol *const *ptr = members.getptr(p_name); + if (ptr) { + return *ptr; + } + } else { + if (const ClassMembers *_class = inner_classes.getptr(p_subclass)) { + const lsp::DocumentSymbol *const *ptr = _class->getptr(p_name); + if (ptr) { + return *ptr; + } + } + } + + return NULL; +} + +const Array &ExtendGDScriptParser::get_member_completions() { + + if (member_completions.empty()) { + + const String *name = members.next(NULL); + while (name) { + + const lsp::DocumentSymbol *symbol = members.get(*name); + lsp::CompletionItem item = symbol->make_completion_item(); + item.data = JOIN_SYMBOLS(path, *name); + member_completions.push_back(item.to_json()); + + name = members.next(name); + } + + const String *_class = inner_classes.next(NULL); + while (_class) { + + const ClassMembers *inner_class = inner_classes.getptr(*_class); + const String *member_name = inner_class->next(NULL); + while (member_name) { + const lsp::DocumentSymbol *symbol = inner_class->get(*member_name); + lsp::CompletionItem item = symbol->make_completion_item(); + item.data = JOIN_SYMBOLS(path, JOIN_SYMBOLS(*_class, *member_name)); + member_completions.push_back(item.to_json()); + + member_name = inner_class->next(member_name); + } + + _class = inner_classes.next(_class); + } + } + + return member_completions; +} + +Dictionary ExtendGDScriptParser::dump_function_api(const GDScriptParser::FunctionNode *p_func) const { + Dictionary func; + ERR_FAIL_NULL_V(p_func, func); + func["name"] = p_func->name; + func["return_type"] = p_func->return_type.to_string(); + func["rpc_mode"] = p_func->rpc_mode; + Array arguments; + for (int i = 0; i < p_func->arguments.size(); i++) { + Dictionary arg; + arg["name"] = p_func->arguments[i]; + arg["type"] = p_func->argument_types[i].to_string(); + int default_value_idx = i - (p_func->arguments.size() - p_func->default_values.size()); + if (default_value_idx >= 0) { + const GDScriptParser::ConstantNode *const_node = dynamic_cast<const GDScriptParser::ConstantNode *>(p_func->default_values[default_value_idx]); + if (const_node == NULL) { + const GDScriptParser::OperatorNode *operator_node = dynamic_cast<const GDScriptParser::OperatorNode *>(p_func->default_values[default_value_idx]); + if (operator_node) { + const_node = dynamic_cast<const GDScriptParser::ConstantNode *>(operator_node->next); + } + } + if (const_node) { + arg["default_value"] = const_node->value; + } + } + arguments.push_back(arg); + } + if (const lsp::DocumentSymbol *symbol = get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(p_func->line))) { + func["signature"] = symbol->detail; + func["description"] = symbol->documentation; + } + func["arguments"] = arguments; + return func; +} + +Dictionary ExtendGDScriptParser::dump_class_api(const GDScriptParser::ClassNode *p_class) const { + Dictionary class_api; + + ERR_FAIL_NULL_V(p_class, class_api); + + class_api["name"] = String(p_class->name); + class_api["path"] = path; + Array extends_class; + for (int i = 0; i < p_class->extends_class.size(); i++) { + extends_class.append(String(p_class->extends_class[i])); + } + class_api["extends_class"] = extends_class; + class_api["extends_file"] = String(p_class->extends_file); + class_api["icon"] = String(p_class->icon_path); + + if (const lsp::DocumentSymbol *symbol = get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(p_class->line))) { + class_api["signature"] = symbol->detail; + class_api["description"] = symbol->documentation; + } + + Array subclasses; + for (int i = 0; i < p_class->subclasses.size(); i++) { + subclasses.push_back(dump_class_api(p_class->subclasses[i])); + } + class_api["sub_classes"] = subclasses; + + Array constants; + for (Map<StringName, GDScriptParser::ClassNode::Constant>::Element *E = p_class->constant_expressions.front(); E; E = E->next()) { + + const GDScriptParser::ClassNode::Constant &c = E->value(); + const GDScriptParser::ConstantNode *node = dynamic_cast<const GDScriptParser::ConstantNode *>(c.expression); + + Dictionary api; + api["name"] = E->key(); + api["value"] = node->value; + api["data_type"] = node->datatype.to_string(); + if (const lsp::DocumentSymbol *symbol = get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(node->line))) { + api["signature"] = symbol->detail; + api["description"] = symbol->documentation; + } + constants.push_back(api); + } + class_api["constants"] = constants; + + Array members; + for (int i = 0; i < p_class->variables.size(); ++i) { + const GDScriptParser::ClassNode::Member &m = p_class->variables[i]; + Dictionary api; + api["name"] = m.identifier; + api["data_type"] = m.data_type.to_string(); + api["default_value"] = m.default_value; + api["setter"] = String(m.setter); + api["getter"] = String(m.getter); + api["export"] = m._export.type != Variant::NIL; + if (const lsp::DocumentSymbol *symbol = get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(m.line))) { + api["signature"] = symbol->detail; + api["description"] = symbol->documentation; + } + members.push_back(api); + } + class_api["members"] = members; + + Array signals; + for (int i = 0; i < p_class->_signals.size(); ++i) { + const GDScriptParser::ClassNode::Signal &signal = p_class->_signals[i]; + Dictionary api; + api["name"] = signal.name; + Array args; + for (int j = 0; j < signal.arguments.size(); j++) { + args.append(signal.arguments[j]); + } + api["arguments"] = args; + if (const lsp::DocumentSymbol *symbol = get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(signal.line))) { + api["signature"] = symbol->detail; + api["description"] = symbol->documentation; + } + signals.push_back(api); + } + class_api["signals"] = signals; + + Array methods; + for (int i = 0; i < p_class->functions.size(); ++i) { + methods.append(dump_function_api(p_class->functions[i])); + } + class_api["methods"] = methods; + + Array static_functions; + for (int i = 0; i < p_class->static_functions.size(); ++i) { + static_functions.append(dump_function_api(p_class->functions[i])); + } + class_api["static_functions"] = static_functions; + + return class_api; +} + +Dictionary ExtendGDScriptParser::generate_api() const { + + Dictionary api; + const GDScriptParser::Node *head = get_parse_tree(); + if (const GDScriptParser::ClassNode *gdclass = dynamic_cast<const GDScriptParser::ClassNode *>(head)) { + api = dump_class_api(gdclass); + } + return api; +} + +Error ExtendGDScriptParser::parse(const String &p_code, const String &p_path) { + path = p_path; + lines = p_code.split("\n"); + + Error err = GDScriptParser::parse(p_code, p_path.get_base_dir(), false, p_path, false, NULL, false); + update_diagnostics(); + update_symbols(); + return err; +} diff --git a/modules/gdscript/language_server/gdscript_extend_parser.h b/modules/gdscript/language_server/gdscript_extend_parser.h new file mode 100644 index 0000000000..dd0453d3ff --- /dev/null +++ b/modules/gdscript/language_server/gdscript_extend_parser.h @@ -0,0 +1,103 @@ +/*************************************************************************/ +/* gdscript_extend_parser.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef GDSCRIPT_EXTEND_PARSER_H +#define GDSCRIPT_EXTEND_PARSER_H + +#include "../gdscript_parser.h" +#include "core/variant.h" +#include "lsp.hpp" + +#ifndef LINE_NUMBER_TO_INDEX +#define LINE_NUMBER_TO_INDEX(p_line) ((p_line)-1) +#endif + +#ifndef SYMBOL_SEPERATOR +#define SYMBOL_SEPERATOR "::" +#endif + +#ifndef JOIN_SYMBOLS +#define JOIN_SYMBOLS(p_path, name) ((p_path) + SYMBOL_SEPERATOR + (name)) +#endif + +typedef HashMap<String, const lsp::DocumentSymbol *> ClassMembers; + +class ExtendGDScriptParser : public GDScriptParser { + + String path; + Vector<String> lines; + + lsp::DocumentSymbol class_symbol; + Vector<lsp::Diagnostic> diagnostics; + ClassMembers members; + HashMap<String, ClassMembers> inner_classes; + + void update_diagnostics(); + + void update_symbols(); + void parse_class_symbol(const GDScriptParser::ClassNode *p_class, lsp::DocumentSymbol &r_symbol); + void parse_function_symbol(const GDScriptParser::FunctionNode *p_func, lsp::DocumentSymbol &r_symbol); + + Dictionary dump_function_api(const GDScriptParser::FunctionNode *p_func) const; + Dictionary dump_class_api(const GDScriptParser::ClassNode *p_class) const; + + String parse_documentation(int p_line, bool p_docs_down = false); + const lsp::DocumentSymbol *search_symbol_defined_at_line(int p_line, const lsp::DocumentSymbol &p_parent) const; + + Array member_completions; + + String parse_documentation_as_markdown(int p_line, bool p_docs_down = false); + +public: + static String marked_documentation(const String &p_bbcode); + +public: + _FORCE_INLINE_ const String &get_path() const { return path; } + _FORCE_INLINE_ const Vector<String> &get_lines() const { return lines; } + _FORCE_INLINE_ const lsp::DocumentSymbol &get_symbols() const { return class_symbol; } + _FORCE_INLINE_ const Vector<lsp::Diagnostic> &get_diagnostics() const { return diagnostics; } + _FORCE_INLINE_ const ClassMembers &get_members() const { return members; } + _FORCE_INLINE_ const HashMap<String, ClassMembers> &get_inner_classes() const { return inner_classes; } + + String get_text_for_completion(const lsp::Position &p_cursor) const; + String get_text_for_lookup_symbol(const lsp::Position &p_cursor, const String &p_symbol = "", bool p_func_requred = false) const; + String get_identifier_under_position(const lsp::Position &p_position, Vector2i &p_offset) const; + String get_uri() const; + + const lsp::DocumentSymbol *get_symbol_defined_at_line(int p_line) const; + const lsp::DocumentSymbol *get_member_symbol(const String &p_name, const String &p_subclass = "") const; + + const Array &get_member_completions(); + Dictionary generate_api() const; + + Error parse(const String &p_code, const String &p_path); +}; + +#endif diff --git a/modules/gdscript/language_server/gdscript_language_protocol.cpp b/modules/gdscript/language_server/gdscript_language_protocol.cpp new file mode 100644 index 0000000000..afe461b68e --- /dev/null +++ b/modules/gdscript/language_server/gdscript_language_protocol.cpp @@ -0,0 +1,211 @@ +/*************************************************************************/ +/* gdscript_language_protocol.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "gdscript_language_protocol.h" +#include "core/io/json.h" +#include "core/os/copymem.h" +#include "core/project_settings.h" +#include "editor/editor_node.h" + +GDScriptLanguageProtocol *GDScriptLanguageProtocol::singleton = NULL; + +void GDScriptLanguageProtocol::on_data_received(int p_id) { + lastest_client_id = p_id; + Ref<WebSocketPeer> peer = server->get_peer(p_id); + PoolByteArray data; + if (OK == peer->get_packet_buffer(data)) { + String message; + message.parse_utf8((const char *)data.read().ptr(), data.size()); + if (message.begins_with("Content-Length:")) return; + String output = process_message(message); + if (!output.empty()) { + CharString charstr = output.utf8(); + peer->put_packet((const uint8_t *)charstr.ptr(), charstr.length()); + } + } +} + +void GDScriptLanguageProtocol::on_client_connected(int p_id, const String &p_protocal) { + clients.set(p_id, server->get_peer(p_id)); +} + +void GDScriptLanguageProtocol::on_client_disconnected(int p_id, bool p_was_clean_close) { + clients.erase(p_id); +} + +String GDScriptLanguageProtocol::process_message(const String &p_text) { + String ret = process_string(p_text); + if (ret.empty()) { + return ret; + } else { + return format_output(ret); + } +} + +String GDScriptLanguageProtocol::format_output(const String &p_text) { + + String header = "Content-Length: "; + CharString charstr = p_text.utf8(); + size_t len = charstr.length(); + header += itos(len); + header += "\r\n\r\n"; + + return header + p_text; +} + +void GDScriptLanguageProtocol::_bind_methods() { + ClassDB::bind_method(D_METHOD("initialize", "params"), &GDScriptLanguageProtocol::initialize); + ClassDB::bind_method(D_METHOD("initialized", "params"), &GDScriptLanguageProtocol::initialized); + ClassDB::bind_method(D_METHOD("on_data_received"), &GDScriptLanguageProtocol::on_data_received); + ClassDB::bind_method(D_METHOD("on_client_connected"), &GDScriptLanguageProtocol::on_client_connected); + ClassDB::bind_method(D_METHOD("on_client_disconnected"), &GDScriptLanguageProtocol::on_client_disconnected); + ClassDB::bind_method(D_METHOD("notify_all_clients", "p_method", "p_params"), &GDScriptLanguageProtocol::notify_all_clients, DEFVAL(Variant())); + ClassDB::bind_method(D_METHOD("notify_client", "p_method", "p_params", "p_client"), &GDScriptLanguageProtocol::notify_client, DEFVAL(Variant()), DEFVAL(-1)); + ClassDB::bind_method(D_METHOD("is_smart_resolve_enabled"), &GDScriptLanguageProtocol::is_smart_resolve_enabled); + ClassDB::bind_method(D_METHOD("get_text_document"), &GDScriptLanguageProtocol::get_text_document); + ClassDB::bind_method(D_METHOD("get_workspace"), &GDScriptLanguageProtocol::get_workspace); + ClassDB::bind_method(D_METHOD("is_initialized"), &GDScriptLanguageProtocol::is_initialized); +} + +Dictionary GDScriptLanguageProtocol::initialize(const Dictionary &p_params) { + + lsp::InitializeResult ret; + + String root_uri = p_params["rootUri"]; + String root = p_params["rootPath"]; + bool is_same_workspace = root == workspace->root; + is_same_workspace = root.to_lower() == workspace->root.to_lower(); +#ifdef WINDOWS_ENABLED + is_same_workspace = root.replace("\\", "/").to_lower() == workspace->root.to_lower(); +#endif + + if (root_uri.length() && is_same_workspace) { + workspace->root_uri = root_uri; + } else { + + workspace->root_uri = "file://" + workspace->root; + + Dictionary params; + params["path"] = workspace->root; + Dictionary request = make_notification("gdscrip_client/changeWorkspace", params); + if (Ref<WebSocketPeer> *peer = clients.getptr(lastest_client_id)) { + String msg = JSON::print(request); + msg = format_output(msg); + CharString charstr = msg.utf8(); + (*peer)->put_packet((const uint8_t *)charstr.ptr(), charstr.length()); + } + } + + if (!_initialized) { + workspace->initialize(); + text_document->initialize(); + _initialized = true; + } + + return ret.to_json(); +} + +void GDScriptLanguageProtocol::initialized(const Variant &p_params) { +} + +void GDScriptLanguageProtocol::poll() { + server->poll(); +} + +Error GDScriptLanguageProtocol::start(int p_port) { + if (server == NULL) { + server = dynamic_cast<WebSocketServer *>(ClassDB::instance("WebSocketServer")); + server->set_buffers(8192, 1024, 8192, 1024); // 8mb should be way more than enough + server->connect("data_received", this, "on_data_received"); + server->connect("client_connected", this, "on_client_connected"); + server->connect("client_disconnected", this, "on_client_disconnected"); + } + return server->listen(p_port); +} + +void GDScriptLanguageProtocol::stop() { + server->stop(); +} + +void GDScriptLanguageProtocol::notify_all_clients(const String &p_method, const Variant &p_params) { + + Dictionary message = make_notification(p_method, p_params); + String msg = JSON::print(message); + msg = format_output(msg); + CharString charstr = msg.utf8(); + const int *p_id = clients.next(NULL); + while (p_id != NULL) { + Ref<WebSocketPeer> peer = clients.get(*p_id); + (*peer)->put_packet((const uint8_t *)charstr.ptr(), charstr.length()); + p_id = clients.next(p_id); + } +} + +void GDScriptLanguageProtocol::notify_client(const String &p_method, const Variant &p_params, int p_client) { + + if (p_client == -1) { + p_client = lastest_client_id; + } + + Ref<WebSocketPeer> *peer = clients.getptr(p_client); + ERR_FAIL_COND(peer == NULL); + + Dictionary message = make_notification(p_method, p_params); + String msg = JSON::print(message); + msg = format_output(msg); + CharString charstr = msg.utf8(); + + (*peer)->put_packet((const uint8_t *)charstr.ptr(), charstr.length()); +} + +bool GDScriptLanguageProtocol::is_smart_resolve_enabled() const { + return bool(_EDITOR_GET("network/language_server/enable_smart_resolve")); +} + +bool GDScriptLanguageProtocol::is_goto_native_symbols_enabled() const { + return bool(_EDITOR_GET("network/language_server/show_native_symbols_in_editor")); +} + +GDScriptLanguageProtocol::GDScriptLanguageProtocol() { + server = NULL; + singleton = this; + _initialized = false; + workspace.instance(); + text_document.instance(); + set_scope("textDocument", text_document.ptr()); + set_scope("completionItem", text_document.ptr()); + set_scope("workspace", workspace.ptr()); + workspace->root = ProjectSettings::get_singleton()->get_resource_path(); +} + +GDScriptLanguageProtocol::~GDScriptLanguageProtocol() { + memdelete(server); + server = NULL; +} diff --git a/modules/gdscript/language_server/gdscript_language_protocol.h b/modules/gdscript/language_server/gdscript_language_protocol.h new file mode 100644 index 0000000000..136b45fd78 --- /dev/null +++ b/modules/gdscript/language_server/gdscript_language_protocol.h @@ -0,0 +1,93 @@ +/*************************************************************************/ +/* gdscript_language_protocol.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef GDSCRIPT_PROTOCAL_SERVER_H +#define GDSCRIPT_PROTOCAL_SERVER_H + +#include "gdscript_text_document.h" +#include "gdscript_workspace.h" +#include "lsp.hpp" +#include "modules/jsonrpc/jsonrpc.h" +#include "modules/websocket/websocket_peer.h" +#include "modules/websocket/websocket_server.h" + +class GDScriptLanguageProtocol : public JSONRPC { + GDCLASS(GDScriptLanguageProtocol, JSONRPC) + + enum LSPErrorCode { + RequestCancelled = -32800, + ContentModified = -32801, + }; + + static GDScriptLanguageProtocol *singleton; + + HashMap<int, Ref<WebSocketPeer> > clients; + WebSocketServer *server; + int lastest_client_id; + + Ref<GDScriptTextDocument> text_document; + Ref<GDScriptWorkspace> workspace; + + void on_data_received(int p_id); + void on_client_connected(int p_id, const String &p_protocal); + void on_client_disconnected(int p_id, bool p_was_clean_close); + + String process_message(const String &p_text); + String format_output(const String &p_text); + + bool _initialized; + +protected: + static void _bind_methods(); + + Dictionary initialize(const Dictionary &p_params); + void initialized(const Variant &p_params); + +public: + _FORCE_INLINE_ static GDScriptLanguageProtocol *get_singleton() { return singleton; } + _FORCE_INLINE_ Ref<GDScriptWorkspace> get_workspace() { return workspace; } + _FORCE_INLINE_ Ref<GDScriptTextDocument> get_text_document() { return text_document; } + _FORCE_INLINE_ bool is_initialized() const { return _initialized; } + + void poll(); + Error start(int p_port); + void stop(); + + void notify_all_clients(const String &p_method, const Variant &p_params = Variant()); + void notify_client(const String &p_method, const Variant &p_params = Variant(), int p_client = -1); + + bool is_smart_resolve_enabled() const; + bool is_goto_native_symbols_enabled() const; + + GDScriptLanguageProtocol(); + ~GDScriptLanguageProtocol(); +}; + +#endif diff --git a/modules/gdscript/language_server/gdscript_language_server.cpp b/modules/gdscript/language_server/gdscript_language_server.cpp new file mode 100644 index 0000000000..9bea4557ac --- /dev/null +++ b/modules/gdscript/language_server/gdscript_language_server.cpp @@ -0,0 +1,88 @@ +/*************************************************************************/ +/* gdscript_language_server.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "gdscript_language_server.h" +#include "core/os/file_access.h" +#include "core/os/os.h" +#include "editor/editor_node.h" + +GDScriptLanguageServer::GDScriptLanguageServer() { + thread = NULL; + thread_exit = false; + _EDITOR_DEF("network/language_server/remote_port", 6008); + _EDITOR_DEF("network/language_server/enable_smart_resolve", false); + _EDITOR_DEF("network/language_server/show_native_symbols_in_editor", false); +} + +void GDScriptLanguageServer::_notification(int p_what) { + + switch (p_what) { + case NOTIFICATION_ENTER_TREE: + start(); + break; + case NOTIFICATION_EXIT_TREE: + stop(); + break; + } +} + +void GDScriptLanguageServer::thread_main(void *p_userdata) { + GDScriptLanguageServer *self = static_cast<GDScriptLanguageServer *>(p_userdata); + while (!self->thread_exit) { + self->protocol.poll(); + OS::get_singleton()->delay_usec(10); + } +} + +void GDScriptLanguageServer::start() { + int port = (int)_EDITOR_GET("network/language_server/remote_port"); + if (protocol.start(port) == OK) { + EditorNode::get_log()->add_message("** GDScript Language Server Started **"); + ERR_FAIL_COND(thread != NULL || thread_exit); + thread_exit = false; + thread = Thread::create(GDScriptLanguageServer::thread_main, this); + } +} + +void GDScriptLanguageServer::stop() { + ERR_FAIL_COND(NULL == thread || thread_exit); + thread_exit = true; + Thread::wait_to_finish(thread); + memdelete(thread); + thread = NULL; + protocol.stop(); + EditorNode::get_log()->add_message("** GDScript Language Server Stopped **"); +} + +void register_lsp_types() { + ClassDB::register_class<GDScriptLanguageProtocol>(); + ClassDB::register_class<GDScriptTextDocument>(); + ClassDB::register_class<GDScriptWorkspace>(); +} diff --git a/modules/gdscript/language_server/gdscript_language_server.h b/modules/gdscript/language_server/gdscript_language_server.h new file mode 100644 index 0000000000..83c2320d98 --- /dev/null +++ b/modules/gdscript/language_server/gdscript_language_server.h @@ -0,0 +1,60 @@ +/*************************************************************************/ +/* gdscript_language_server.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef GDSCRIPT_LANGUAGE_SERVER_H +#define GDSCRIPT_LANGUAGE_SERVER_H + +#include "../gdscript_parser.h" +#include "editor/editor_plugin.h" +#include "gdscript_language_protocol.h" + +class GDScriptLanguageServer : public EditorPlugin { + GDCLASS(GDScriptLanguageServer, EditorPlugin); + + GDScriptLanguageProtocol protocol; + + Thread *thread; + bool thread_exit; + static void thread_main(void *p_userdata); + +private: + void _notification(int p_what); + void _iteration(); + +public: + Error parse_script_file(const String &p_path); + GDScriptLanguageServer(); + void start(); + void stop(); +}; + +void register_lsp_types(); + +#endif // GDSCRIPT_LANGUAGE_SERVER_H diff --git a/modules/gdscript/language_server/gdscript_text_document.cpp b/modules/gdscript/language_server/gdscript_text_document.cpp new file mode 100644 index 0000000000..f211fae526 --- /dev/null +++ b/modules/gdscript/language_server/gdscript_text_document.cpp @@ -0,0 +1,391 @@ +/*************************************************************************/ +/* gdscript_text_document.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "gdscript_text_document.h" +#include "../gdscript.h" +#include "core/os/os.h" +#include "editor/editor_settings.h" +#include "editor/plugins/script_text_editor.h" +#include "gdscript_extend_parser.h" +#include "gdscript_language_protocol.h" + +void GDScriptTextDocument::_bind_methods() { + ClassDB::bind_method(D_METHOD("didOpen"), &GDScriptTextDocument::didOpen); + ClassDB::bind_method(D_METHOD("didChange"), &GDScriptTextDocument::didChange); + ClassDB::bind_method(D_METHOD("documentSymbol"), &GDScriptTextDocument::documentSymbol); + ClassDB::bind_method(D_METHOD("completion"), &GDScriptTextDocument::completion); + ClassDB::bind_method(D_METHOD("resolve"), &GDScriptTextDocument::resolve); + ClassDB::bind_method(D_METHOD("foldingRange"), &GDScriptTextDocument::foldingRange); + ClassDB::bind_method(D_METHOD("codeLens"), &GDScriptTextDocument::codeLens); + ClassDB::bind_method(D_METHOD("documentLink"), &GDScriptTextDocument::documentLink); + ClassDB::bind_method(D_METHOD("colorPresentation"), &GDScriptTextDocument::colorPresentation); + ClassDB::bind_method(D_METHOD("hover"), &GDScriptTextDocument::hover); + ClassDB::bind_method(D_METHOD("definition"), &GDScriptTextDocument::definition); + ClassDB::bind_method(D_METHOD("show_native_symbol_in_editor"), &GDScriptTextDocument::show_native_symbol_in_editor); +} + +void GDScriptTextDocument::didOpen(const Variant &p_param) { + lsp::TextDocumentItem doc = load_document_item(p_param); + sync_script_content(doc.uri, doc.text); +} + +void GDScriptTextDocument::didChange(const Variant &p_param) { + lsp::TextDocumentItem doc = load_document_item(p_param); + Dictionary dict = p_param; + Array contentChanges = dict["contentChanges"]; + for (int i = 0; i < contentChanges.size(); ++i) { + lsp::TextDocumentContentChangeEvent evt; + evt.load(contentChanges[i]); + doc.text = evt.text; + } + sync_script_content(doc.uri, doc.text); +} + +lsp::TextDocumentItem GDScriptTextDocument::load_document_item(const Variant &p_param) { + lsp::TextDocumentItem doc; + Dictionary params = p_param; + doc.load(params["textDocument"]); + return doc; +} + +void GDScriptTextDocument::initialize() { + + if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) { + + const HashMap<StringName, ClassMembers> &native_members = GDScriptLanguageProtocol::get_singleton()->get_workspace()->native_members; + + const StringName *class_ptr = native_members.next(NULL); + while (class_ptr) { + + const ClassMembers &members = native_members.get(*class_ptr); + + const String *name = members.next(NULL); + while (name) { + + const lsp::DocumentSymbol *symbol = members.get(*name); + lsp::CompletionItem item = symbol->make_completion_item(); + item.data = JOIN_SYMBOLS(String(*class_ptr), *name); + native_member_completions.push_back(item.to_json()); + + name = members.next(name); + } + + class_ptr = native_members.next(class_ptr); + } + } +} + +Array GDScriptTextDocument::documentSymbol(const Dictionary &p_params) { + Dictionary params = p_params["textDocument"]; + String uri = params["uri"]; + String path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(uri); + Array arr; + if (const Map<String, ExtendGDScriptParser *>::Element *parser = GDScriptLanguageProtocol::get_singleton()->get_workspace()->scripts.find(path)) { + Vector<lsp::DocumentedSymbolInformation> list; + parser->get()->get_symbols().symbol_tree_as_list(uri, list); + for (int i = 0; i < list.size(); i++) { + arr.push_back(list[i].to_json()); + } + } + return arr; +} + +Array GDScriptTextDocument::completion(const Dictionary &p_params) { + + Array arr; + + lsp::CompletionParams params; + params.load(p_params); + Dictionary request_data = params.to_json(); + + List<ScriptCodeCompletionOption> options; + GDScriptLanguageProtocol::get_singleton()->get_workspace()->completion(params, &options); + + if (!options.empty()) { + + int i = 0; + arr.resize(options.size()); + + for (const List<ScriptCodeCompletionOption>::Element *E = options.front(); E; E = E->next()) { + + const ScriptCodeCompletionOption &option = E->get(); + lsp::CompletionItem item; + item.label = option.display; + item.data = request_data; + + switch (option.kind) { + case ScriptCodeCompletionOption::KIND_ENUM: + item.kind = lsp::CompletionItemKind::Enum; + break; + case ScriptCodeCompletionOption::KIND_CLASS: + item.kind = lsp::CompletionItemKind::Class; + break; + case ScriptCodeCompletionOption::KIND_MEMBER: + item.kind = lsp::CompletionItemKind::Property; + break; + case ScriptCodeCompletionOption::KIND_FUNCTION: + item.kind = lsp::CompletionItemKind::Method; + break; + case ScriptCodeCompletionOption::KIND_SIGNAL: + item.kind = lsp::CompletionItemKind::Event; + break; + case ScriptCodeCompletionOption::KIND_CONSTANT: + item.kind = lsp::CompletionItemKind::Constant; + break; + case ScriptCodeCompletionOption::KIND_VARIABLE: + item.kind = lsp::CompletionItemKind::Variable; + break; + case ScriptCodeCompletionOption::KIND_FILE_PATH: + item.kind = lsp::CompletionItemKind::File; + break; + case ScriptCodeCompletionOption::KIND_NODE_PATH: + item.kind = lsp::CompletionItemKind::Snippet; + break; + case ScriptCodeCompletionOption::KIND_PLAIN_TEXT: + item.kind = lsp::CompletionItemKind::Text; + break; + } + + arr[i] = item.to_json(); + i++; + } + } else if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) { + + arr = native_member_completions.duplicate(); + + for (Map<String, ExtendGDScriptParser *>::Element *E = GDScriptLanguageProtocol::get_singleton()->get_workspace()->scripts.front(); E; E = E->next()) { + + ExtendGDScriptParser *script = E->get(); + const Array &items = script->get_member_completions(); + + const int start_size = arr.size(); + arr.resize(start_size + items.size()); + for (int i = start_size; i < arr.size(); i++) { + arr[i] = items[i - start_size]; + } + } + } + return arr; +} + +Dictionary GDScriptTextDocument::resolve(const Dictionary &p_params) { + + lsp::CompletionItem item; + item.load(p_params); + + lsp::CompletionParams params; + Variant data = p_params["data"]; + + const lsp::DocumentSymbol *symbol = NULL; + + if (data.get_type() == Variant::DICTIONARY) { + + params.load(p_params["data"]); + symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(params, item.label, item.kind == lsp::CompletionItemKind::Method || item.kind == lsp::CompletionItemKind::Function); + + } else if (data.get_type() == Variant::STRING) { + + String query = data; + + Vector<String> param_symbols = query.split(SYMBOL_SEPERATOR, false); + + if (param_symbols.size() >= 2) { + + String class_ = param_symbols[0]; + StringName class_name = class_; + String member_name = param_symbols[param_symbols.size() - 1]; + String inner_class_name; + if (param_symbols.size() >= 3) { + inner_class_name = param_symbols[1]; + } + + if (const ClassMembers *members = GDScriptLanguageProtocol::get_singleton()->get_workspace()->native_members.getptr(class_name)) { + if (const lsp::DocumentSymbol *const *member = members->getptr(member_name)) { + symbol = *member; + } + } + + if (!symbol) { + if (const Map<String, ExtendGDScriptParser *>::Element *E = GDScriptLanguageProtocol::get_singleton()->get_workspace()->scripts.find(class_name)) { + symbol = E->get()->get_member_symbol(member_name, inner_class_name); + } + } + } + } + + if (symbol) { + item.documentation = symbol->render(); + } + + if ((item.kind == lsp::CompletionItemKind::Method || item.kind == lsp::CompletionItemKind::Function) && !item.label.ends_with("):")) { + item.insertText = item.label + "("; + if (symbol && symbol->detail.find(",") == -1) { + item.insertText += ")"; + } + } else if (item.kind == lsp::CompletionItemKind::Event) { + if (params.context.triggerKind == lsp::CompletionTriggerKind::TriggerCharacter && (params.context.triggerCharacter == "(")) { + const String quote_style = EDITOR_DEF("text_editor/completion/use_single_quotes", false) ? "'" : "\""; + item.insertText = quote_style + item.label + quote_style; + } + } + + return item.to_json(true); +} + +Array GDScriptTextDocument::foldingRange(const Dictionary &p_params) { + Dictionary params = p_params["textDocument"]; + String path = params["uri"]; + Array arr; + return arr; +} + +Array GDScriptTextDocument::codeLens(const Dictionary &p_params) { + Array arr; + return arr; +} + +Variant GDScriptTextDocument::documentLink(const Dictionary &p_params) { + Variant ret; + return ret; +} + +Array GDScriptTextDocument::colorPresentation(const Dictionary &p_params) { + Array arr; + return arr; +} + +Variant GDScriptTextDocument::hover(const Dictionary &p_params) { + + lsp::TextDocumentPositionParams params; + params.load(p_params); + + const lsp::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(params); + if (symbol) { + + lsp::Hover hover; + hover.contents = symbol->render(); + return hover.to_json(); + + } else if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) { + + Dictionary ret; + Array contents; + List<const lsp::DocumentSymbol *> list; + GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_related_symbols(params, list); + for (List<const lsp::DocumentSymbol *>::Element *E = list.front(); E; E = E->next()) { + if (const lsp::DocumentSymbol *s = E->get()) { + contents.push_back(s->render().value); + } + } + ret["contents"] = contents; + return ret; + } + + return Variant(); +} + +Array GDScriptTextDocument::definition(const Dictionary &p_params) { + Array arr; + + lsp::TextDocumentPositionParams params; + params.load(p_params); + + const lsp::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(params); + if (symbol) { + lsp::Location location; + location.uri = symbol->uri; + location.range = symbol->range; + + const String &path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(symbol->uri); + if (file_checker->file_exists(path)) { + arr.push_back(location.to_json()); + } else if (!symbol->native_class.empty() && GDScriptLanguageProtocol::get_singleton()->is_goto_native_symbols_enabled()) { + String id; + switch (symbol->kind) { + case lsp::SymbolKind::Class: + id = "class_name:" + symbol->name; + break; + case lsp::SymbolKind::Constant: + id = "class_constant:" + symbol->native_class + ":" + symbol->name; + break; + case lsp::SymbolKind::Property: + case lsp::SymbolKind::Variable: + id = "class_property:" + symbol->native_class + ":" + symbol->name; + break; + case lsp::SymbolKind::Enum: + id = "class_enum:" + symbol->native_class + ":" + symbol->name; + break; + case lsp::SymbolKind::Method: + case lsp::SymbolKind::Function: + id = "class_method:" + symbol->native_class + ":" + symbol->name; + break; + default: + id = "class_global:" + symbol->native_class + ":" + symbol->name; + break; + } + call_deferred("show_native_symbol_in_editor", id); + } + } else if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) { + + List<const lsp::DocumentSymbol *> list; + GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_related_symbols(params, list); + for (List<const lsp::DocumentSymbol *>::Element *E = list.front(); E; E = E->next()) { + + if (const lsp::DocumentSymbol *s = E->get()) { + if (!s->uri.empty()) { + lsp::Location location; + location.uri = s->uri; + location.range = s->range; + arr.push_back(location.to_json()); + } + } + } + } + + return arr; +} + +GDScriptTextDocument::GDScriptTextDocument() { + file_checker = FileAccess::create(FileAccess::ACCESS_RESOURCES); +} + +GDScriptTextDocument::~GDScriptTextDocument() { + memdelete(file_checker); +} + +void GDScriptTextDocument::sync_script_content(const String &p_uri, const String &p_content) { + String path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(p_uri); + GDScriptLanguageProtocol::get_singleton()->get_workspace()->parse_script(path, p_content); +} + +void GDScriptTextDocument::show_native_symbol_in_editor(const String &p_symbol_id) { + ScriptEditor::get_singleton()->call_deferred("_help_class_goto", p_symbol_id); + OS::get_singleton()->move_window_to_foreground(); +} diff --git a/modules/gdscript/language_server/gdscript_text_document.h b/modules/gdscript/language_server/gdscript_text_document.h new file mode 100644 index 0000000000..d15022d2c4 --- /dev/null +++ b/modules/gdscript/language_server/gdscript_text_document.h @@ -0,0 +1,73 @@ +/*************************************************************************/ +/* gdscript_text_document.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef GDSCRIPT_TEXT_DOCUMENT_H +#define GDSCRIPT_TEXT_DOCUMENT_H + +#include "core/os/file_access.h" +#include "core/reference.h" +#include "lsp.hpp" + +class GDScriptTextDocument : public Reference { + GDCLASS(GDScriptTextDocument, Reference) +protected: + static void _bind_methods(); + + FileAccess *file_checker; + + void didOpen(const Variant &p_param); + void didChange(const Variant &p_param); + + void sync_script_content(const String &p_path, const String &p_content); + void show_native_symbol_in_editor(const String &p_symbol_id); + + Array native_member_completions; + +private: + lsp::TextDocumentItem load_document_item(const Variant &p_param); + +public: + Array documentSymbol(const Dictionary &p_params); + Array completion(const Dictionary &p_params); + Dictionary resolve(const Dictionary &p_params); + Array foldingRange(const Dictionary &p_params); + Array codeLens(const Dictionary &p_params); + Variant documentLink(const Dictionary &p_params); + Array colorPresentation(const Dictionary &p_params); + Variant hover(const Dictionary &p_params); + Array definition(const Dictionary &p_params); + + void initialize(); + + GDScriptTextDocument(); + virtual ~GDScriptTextDocument(); +}; + +#endif diff --git a/modules/gdscript/language_server/gdscript_workspace.cpp b/modules/gdscript/language_server/gdscript_workspace.cpp new file mode 100644 index 0000000000..1901daacff --- /dev/null +++ b/modules/gdscript/language_server/gdscript_workspace.cpp @@ -0,0 +1,504 @@ +/*************************************************************************/ +/* gdscript_workspace.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "gdscript_workspace.h" +#include "../gdscript.h" +#include "../gdscript_parser.h" +#include "core/project_settings.h" +#include "core/script_language.h" +#include "editor/editor_help.h" +#include "gdscript_language_protocol.h" + +void GDScriptWorkspace::_bind_methods() { + ClassDB::bind_method(D_METHOD("symbol"), &GDScriptWorkspace::symbol); + ClassDB::bind_method(D_METHOD("parse_script", "p_path", "p_content"), &GDScriptWorkspace::parse_script); + ClassDB::bind_method(D_METHOD("parse_local_script", "p_path"), &GDScriptWorkspace::parse_local_script); + ClassDB::bind_method(D_METHOD("get_file_path", "p_uri"), &GDScriptWorkspace::get_file_path); + ClassDB::bind_method(D_METHOD("get_file_uri", "p_path"), &GDScriptWorkspace::get_file_uri); + ClassDB::bind_method(D_METHOD("publish_diagnostics", "p_path"), &GDScriptWorkspace::publish_diagnostics); + ClassDB::bind_method(D_METHOD("generate_script_api", "p_path"), &GDScriptWorkspace::generate_script_api); +} + +void GDScriptWorkspace::remove_cache_parser(const String &p_path) { + Map<String, ExtendGDScriptParser *>::Element *parser = parse_results.find(p_path); + Map<String, ExtendGDScriptParser *>::Element *script = scripts.find(p_path); + if (parser && script) { + if (script->get() && script->get() == script->get()) { + memdelete(script->get()); + } else { + memdelete(script->get()); + memdelete(parser->get()); + } + parse_results.erase(p_path); + scripts.erase(p_path); + } else if (parser) { + memdelete(parser->get()); + parse_results.erase(p_path); + } else if (script) { + memdelete(script->get()); + scripts.erase(p_path); + } +} + +const lsp::DocumentSymbol *GDScriptWorkspace::get_native_symbol(const String &p_class, const String &p_member) const { + + StringName class_name = p_class; + StringName empty; + + while (class_name != empty) { + if (const Map<StringName, lsp::DocumentSymbol>::Element *E = native_symbols.find(class_name)) { + const lsp::DocumentSymbol &class_symbol = E->value(); + + if (p_member.empty()) { + return &class_symbol; + } else { + for (int i = 0; i < class_symbol.children.size(); i++) { + const lsp::DocumentSymbol &symbol = class_symbol.children[i]; + if (symbol.name == p_member) { + return &symbol; + } + } + } + } + class_name = ClassDB::get_parent_class(class_name); + } + + return NULL; +} + +const lsp::DocumentSymbol *GDScriptWorkspace::get_script_symbol(const String &p_path) const { + const Map<String, ExtendGDScriptParser *>::Element *S = scripts.find(p_path); + if (S) { + return &(S->get()->get_symbols()); + } + return NULL; +} + +void GDScriptWorkspace::reload_all_workspace_scripts() { + List<String> pathes; + list_script_files("res://", pathes); + for (List<String>::Element *E = pathes.front(); E; E = E->next()) { + const String &path = E->get(); + Error err; + String content = FileAccess::get_file_as_string(path, &err); + ERR_CONTINUE(err != OK); + err = parse_script(path, content); + + if (err != OK) { + Map<String, ExtendGDScriptParser *>::Element *S = parse_results.find(path); + String err_msg = "Failed parse script " + path; + if (S) { + err_msg += "\n" + S->get()->get_error(); + } + ERR_EXPLAIN(err_msg); + ERR_CONTINUE(err != OK); + } + } +} + +void GDScriptWorkspace::list_script_files(const String &p_root_dir, List<String> &r_files) { + Error err; + DirAccessRef dir = DirAccess::open(p_root_dir, &err); + if (OK == err) { + dir->list_dir_begin(); + String file_name = dir->get_next(); + while (file_name.length()) { + if (dir->current_is_dir() && file_name != "." && file_name != ".." && file_name != "./") { + list_script_files(p_root_dir.plus_file(file_name), r_files); + } else if (file_name.ends_with(".gd")) { + String script_file = p_root_dir.plus_file(file_name); + r_files.push_back(script_file); + } + file_name = dir->get_next(); + } + } +} + +ExtendGDScriptParser *GDScriptWorkspace::get_parse_successed_script(const String &p_path) { + const Map<String, ExtendGDScriptParser *>::Element *S = scripts.find(p_path); + if (!S) { + parse_local_script(p_path); + S = scripts.find(p_path); + } + if (S) { + return S->get(); + } + return NULL; +} + +ExtendGDScriptParser *GDScriptWorkspace::get_parse_result(const String &p_path) { + const Map<String, ExtendGDScriptParser *>::Element *S = parse_results.find(p_path); + if (!S) { + parse_local_script(p_path); + S = parse_results.find(p_path); + } + if (S) { + return S->get(); + } + return NULL; +} + +Array GDScriptWorkspace::symbol(const Dictionary &p_params) { + String query = p_params["query"]; + Array arr; + if (!query.empty()) { + for (Map<String, ExtendGDScriptParser *>::Element *E = scripts.front(); E; E = E->next()) { + Vector<lsp::DocumentedSymbolInformation> script_symbols; + E->get()->get_symbols().symbol_tree_as_list(E->key(), script_symbols); + for (int i = 0; i < script_symbols.size(); ++i) { + if (query.is_subsequence_ofi(script_symbols[i].name)) { + arr.push_back(script_symbols[i].to_json()); + } + } + } + } + return arr; +} + +Error GDScriptWorkspace::initialize() { + if (initialized) return OK; + + DocData *doc = EditorHelp::get_doc_data(); + for (Map<String, DocData::ClassDoc>::Element *E = doc->class_list.front(); E; E = E->next()) { + + const DocData::ClassDoc &class_data = E->value(); + lsp::DocumentSymbol class_symbol; + String class_name = E->key(); + class_symbol.name = class_name; + class_symbol.native_class = class_name; + class_symbol.kind = lsp::SymbolKind::Class; + class_symbol.detail = String("<Native> class ") + class_name; + if (!class_data.inherits.empty()) { + class_symbol.detail += " extends " + class_data.inherits; + } + class_symbol.documentation = ExtendGDScriptParser::marked_documentation(class_data.brief_description) + "\n" + ExtendGDScriptParser::marked_documentation(class_data.description); + + for (int i = 0; i < class_data.constants.size(); i++) { + const DocData::ConstantDoc &const_data = class_data.constants[i]; + lsp::DocumentSymbol symbol; + symbol.name = const_data.name; + symbol.native_class = class_name; + symbol.kind = lsp::SymbolKind::Constant; + symbol.detail = "const " + class_name + "." + const_data.name; + if (const_data.enumeration.length()) { + symbol.detail += ": " + const_data.enumeration; + } + symbol.detail += " = " + const_data.value; + symbol.documentation = ExtendGDScriptParser::marked_documentation(const_data.description); + class_symbol.children.push_back(symbol); + } + + Vector<DocData::PropertyDoc> properties; + properties.append_array(class_data.properties); + const int theme_prop_start_idx = properties.size(); + properties.append_array(class_data.theme_properties); + + for (int i = 0; i < class_data.properties.size(); i++) { + const DocData::PropertyDoc &data = class_data.properties[i]; + lsp::DocumentSymbol symbol; + symbol.name = data.name; + symbol.native_class = class_name; + symbol.kind = lsp::SymbolKind::Property; + symbol.detail = String(i >= theme_prop_start_idx ? "<Theme> var" : "var") + " " + class_name + "." + data.name; + if (data.enumeration.length()) { + symbol.detail += ": " + data.enumeration; + } else { + symbol.detail += ": " + data.type; + } + symbol.documentation = ExtendGDScriptParser::marked_documentation(data.description); + class_symbol.children.push_back(symbol); + } + + Vector<DocData::MethodDoc> methods_signals; + methods_signals.append_array(class_data.methods); + const int signal_start_idx = methods_signals.size(); + methods_signals.append_array(class_data.signals); + + for (int i = 0; i < methods_signals.size(); i++) { + const DocData::MethodDoc &data = methods_signals[i]; + + lsp::DocumentSymbol symbol; + symbol.name = data.name; + symbol.native_class = class_name; + symbol.kind = i >= signal_start_idx ? lsp::SymbolKind::Event : lsp::SymbolKind::Method; + + String params = ""; + bool arg_default_value_started = false; + for (int j = 0; j < data.arguments.size(); j++) { + const DocData::ArgumentDoc &arg = data.arguments[j]; + if (!arg_default_value_started && !arg.default_value.empty()) { + arg_default_value_started = true; + } + String arg_str = arg.name + ": " + arg.type; + if (arg_default_value_started) { + arg_str += " = " + arg.default_value; + } + if (j < data.arguments.size() - 1) { + arg_str += ", "; + } + params += arg_str; + } + if (data.qualifiers.find("vararg") != -1) { + params += params.empty() ? "..." : ", ..."; + } + + symbol.detail = "func " + class_name + "." + data.name + "(" + params + ") -> " + data.return_type; + symbol.documentation = ExtendGDScriptParser::marked_documentation(data.description); + class_symbol.children.push_back(symbol); + } + + native_symbols.insert(class_name, class_symbol); + } + + reload_all_workspace_scripts(); + + if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) { + for (Map<StringName, lsp::DocumentSymbol>::Element *E = native_symbols.front(); E; E = E->next()) { + ClassMembers members; + const lsp::DocumentSymbol &class_symbol = E->get(); + for (int i = 0; i < class_symbol.children.size(); i++) { + const lsp::DocumentSymbol &symbol = class_symbol.children[i]; + members.set(symbol.name, &symbol); + } + native_members.set(E->key(), members); + } + + // cache member completions + for (Map<String, ExtendGDScriptParser *>::Element *S = scripts.front(); S; S = S->next()) { + S->get()->get_member_completions(); + } + } + + return OK; +} + +Error GDScriptWorkspace::parse_script(const String &p_path, const String &p_content) { + + ExtendGDScriptParser *parser = memnew(ExtendGDScriptParser); + Error err = parser->parse(p_content, p_path); + Map<String, ExtendGDScriptParser *>::Element *last_parser = parse_results.find(p_path); + Map<String, ExtendGDScriptParser *>::Element *last_script = scripts.find(p_path); + + if (err == OK) { + + remove_cache_parser(p_path); + parse_results[p_path] = parser; + scripts[p_path] = parser; + + } else { + if (last_parser && last_script && last_parser->get() != last_script->get()) { + memdelete(last_parser->get()); + } + parse_results[p_path] = parser; + } + + publish_diagnostics(p_path); + + return err; +} + +Error GDScriptWorkspace::parse_local_script(const String &p_path) { + Error err; + String content = FileAccess::get_file_as_string(p_path, &err); + if (err == OK) { + err = parse_script(p_path, content); + } + return err; +} + +String GDScriptWorkspace::get_file_path(const String &p_uri) const { + String path = p_uri; + path = path.replace(root_uri + "/", "res://"); + path = path.http_unescape(); + return path; +} + +String GDScriptWorkspace::get_file_uri(const String &p_path) const { + String uri = p_path; + uri = uri.replace("res://", root_uri + "/"); + return uri; +} + +void GDScriptWorkspace::publish_diagnostics(const String &p_path) { + Dictionary params; + Array errors; + const Map<String, ExtendGDScriptParser *>::Element *ele = parse_results.find(p_path); + if (ele) { + const Vector<lsp::Diagnostic> &list = ele->get()->get_diagnostics(); + errors.resize(list.size()); + for (int i = 0; i < list.size(); ++i) { + errors[i] = list[i].to_json(); + } + } + params["diagnostics"] = errors; + params["uri"] = get_file_uri(p_path); + GDScriptLanguageProtocol::get_singleton()->notify_client("textDocument/publishDiagnostics", params); +} + +void GDScriptWorkspace::completion(const lsp::CompletionParams &p_params, List<ScriptCodeCompletionOption> *r_options) { + + String path = get_file_path(p_params.textDocument.uri); + String call_hint; + bool forced = false; + + if (const ExtendGDScriptParser *parser = get_parse_result(path)) { + String code = parser->get_text_for_completion(p_params.position); + GDScriptLanguage::get_singleton()->complete_code(code, path, NULL, r_options, forced, call_hint); + } +} + +const lsp::DocumentSymbol *GDScriptWorkspace::resolve_symbol(const lsp::TextDocumentPositionParams &p_doc_pos, const String &p_symbol_name, bool p_func_requred) { + + const lsp::DocumentSymbol *symbol = NULL; + + String path = get_file_path(p_doc_pos.textDocument.uri); + if (const ExtendGDScriptParser *parser = get_parse_result(path)) { + + String symbol_identifier = p_symbol_name; + Vector<String> identifier_parts = symbol_identifier.split("("); + if (identifier_parts.size()) { + symbol_identifier = identifier_parts[0]; + } + + lsp::Position pos = p_doc_pos.position; + if (symbol_identifier.empty()) { + Vector2i offset; + symbol_identifier = parser->get_identifier_under_position(p_doc_pos.position, offset); + pos.character += offset.y; + } + + if (!symbol_identifier.empty()) { + + if (ScriptServer::is_global_class(symbol_identifier)) { + + String class_path = ScriptServer::get_global_class_path(symbol_identifier); + symbol = get_script_symbol(class_path); + + } else { + + ScriptLanguage::LookupResult ret; + if (OK == GDScriptLanguage::get_singleton()->lookup_code(parser->get_text_for_lookup_symbol(pos, symbol_identifier, p_func_requred), symbol_identifier, path, NULL, ret)) { + + if (ret.type == ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION) { + + String target_script_path = path; + if (!ret.script.is_null()) { + target_script_path = ret.script->get_path(); + } + + if (const ExtendGDScriptParser *target_parser = get_parse_result(target_script_path)) { + symbol = target_parser->get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(ret.location)); + } + + } else { + + String member = ret.class_member; + if (member.empty() && symbol_identifier != ret.class_name) { + member = symbol_identifier; + } + symbol = get_native_symbol(ret.class_name, member); + } + } else { + symbol = parser->get_member_symbol(symbol_identifier); + } + } + } + } + + return symbol; +} + +void GDScriptWorkspace::resolve_related_symbols(const lsp::TextDocumentPositionParams &p_doc_pos, List<const lsp::DocumentSymbol *> &r_list) { + + String path = get_file_path(p_doc_pos.textDocument.uri); + if (const ExtendGDScriptParser *parser = get_parse_result(path)) { + + String symbol_identifier; + Vector2i offset; + symbol_identifier = parser->get_identifier_under_position(p_doc_pos.position, offset); + + const StringName *class_ptr = native_members.next(NULL); + while (class_ptr) { + const ClassMembers &members = native_members.get(*class_ptr); + if (const lsp::DocumentSymbol *const *symbol = members.getptr(symbol_identifier)) { + r_list.push_back(*symbol); + } + class_ptr = native_members.next(class_ptr); + } + + for (Map<String, ExtendGDScriptParser *>::Element *E = scripts.front(); E; E = E->next()) { + const ExtendGDScriptParser *script = E->get(); + const ClassMembers &members = script->get_members(); + if (const lsp::DocumentSymbol *const *symbol = members.getptr(symbol_identifier)) { + r_list.push_back(*symbol); + } + + const HashMap<String, ClassMembers> &inner_classes = script->get_inner_classes(); + const String *_class = inner_classes.next(NULL); + while (_class) { + + const ClassMembers *inner_class = inner_classes.getptr(*_class); + if (const lsp::DocumentSymbol *const *symbol = inner_class->getptr(symbol_identifier)) { + r_list.push_back(*symbol); + } + + _class = inner_classes.next(_class); + } + } + } +} + +Dictionary GDScriptWorkspace::generate_script_api(const String &p_path) { + Dictionary api; + if (const ExtendGDScriptParser *parser = get_parse_successed_script(p_path)) { + api = parser->generate_api(); + } + return api; +} + +GDScriptWorkspace::GDScriptWorkspace() { + ProjectSettings::get_singleton()->get_resource_path(); +} + +GDScriptWorkspace::~GDScriptWorkspace() { + Set<String> cached_parsers; + + for (Map<String, ExtendGDScriptParser *>::Element *E = parse_results.front(); E; E = E->next()) { + cached_parsers.insert(E->key()); + } + + for (Map<String, ExtendGDScriptParser *>::Element *E = scripts.front(); E; E = E->next()) { + cached_parsers.insert(E->key()); + } + + for (Set<String>::Element *E = cached_parsers.front(); E; E = E->next()) { + remove_cache_parser(E->get()); + } +} diff --git a/modules/gdscript/language_server/gdscript_workspace.h b/modules/gdscript/language_server/gdscript_workspace.h new file mode 100644 index 0000000000..adce169d4b --- /dev/null +++ b/modules/gdscript/language_server/gdscript_workspace.h @@ -0,0 +1,91 @@ +/*************************************************************************/ +/* gdscript_workspace.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef GDSCRIPT_WORKSPACE_H +#define GDSCRIPT_WORKSPACE_H + +#include "../gdscript_parser.h" +#include "core/variant.h" +#include "gdscript_extend_parser.h" +#include "lsp.hpp" + +class GDScriptWorkspace : public Reference { + GDCLASS(GDScriptWorkspace, Reference); + +protected: + static void _bind_methods(); + void remove_cache_parser(const String &p_path); + bool initialized = false; + Map<StringName, lsp::DocumentSymbol> native_symbols; + + const lsp::DocumentSymbol *get_native_symbol(const String &p_class, const String &p_member = "") const; + const lsp::DocumentSymbol *get_script_symbol(const String &p_path) const; + + void reload_all_workspace_scripts(); + + ExtendGDScriptParser *get_parse_successed_script(const String &p_path); + ExtendGDScriptParser *get_parse_result(const String &p_path); + + void strip_flat_symbols(const String &p_branch); + void list_script_files(const String &p_root_dir, List<String> &r_files); + +public: + String root; + String root_uri; + + Map<String, ExtendGDScriptParser *> scripts; + Map<String, ExtendGDScriptParser *> parse_results; + HashMap<StringName, ClassMembers> native_members; + +public: + Array symbol(const Dictionary &p_params); + +public: + Error initialize(); + + Error parse_script(const String &p_path, const String &p_content); + Error parse_local_script(const String &p_path); + + String get_file_path(const String &p_uri) const; + String get_file_uri(const String &p_path) const; + + void publish_diagnostics(const String &p_path); + void completion(const lsp::CompletionParams &p_params, List<ScriptCodeCompletionOption> *r_options); + + const lsp::DocumentSymbol *resolve_symbol(const lsp::TextDocumentPositionParams &p_doc_pos, const String &p_symbol_name = "", bool p_func_requred = false); + void resolve_related_symbols(const lsp::TextDocumentPositionParams &p_doc_pos, List<const lsp::DocumentSymbol *> &r_list); + + Dictionary generate_script_api(const String &p_path); + + GDScriptWorkspace(); + ~GDScriptWorkspace(); +}; + +#endif diff --git a/modules/gdscript/language_server/lsp.hpp b/modules/gdscript/language_server/lsp.hpp new file mode 100644 index 0000000000..3e57b6ee7e --- /dev/null +++ b/modules/gdscript/language_server/lsp.hpp @@ -0,0 +1,1506 @@ +/*************************************************************************/ +/* lsp.hpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef GODOT_LSP_H +#define GODOT_LSP_H + +#include "core/variant.h" + +namespace lsp { + +typedef String DocumentUri; + +/** + * Text documents are identified using a URI. On the protocol level, URIs are passed as strings. + */ +struct TextDocumentIdentifier { + /** + * The text document's URI. + */ + DocumentUri uri; + + _FORCE_INLINE_ void load(const Dictionary &p_params) { + uri = p_params["uri"]; + } + + _FORCE_INLINE_ Dictionary to_json() const { + Dictionary dict; + dict["uri"] = uri; + return dict; + } +}; + +/** + * Position in a text document expressed as zero-based line and zero-based character offset. + * A position is between two characters like an ‘insert’ cursor in a editor. + * Special values like for example -1 to denote the end of a line are not supported. + */ +struct Position { + /** + * Line position in a document (zero-based). + */ + int line = 0; + + /** + * Character offset on a line in a document (zero-based). Assuming that the line is + * represented as a string, the `character` value represents the gap between the + * `character` and `character + 1`. + * + * If the character value is greater than the line length it defaults back to the + * line length. + */ + int character = 0; + + _FORCE_INLINE_ void load(const Dictionary &p_params) { + line = p_params["line"]; + character = p_params["character"]; + } + + _FORCE_INLINE_ Dictionary to_json() const { + Dictionary dict; + dict["line"] = line; + dict["character"] = character; + return dict; + } +}; + +/** + * A range in a text document expressed as (zero-based) start and end positions. + * A range is comparable to a selection in an editor. Therefore the end position is exclusive. + * If you want to specify a range that contains a line including the line ending character(s) then use an end position denoting the start of the next line. + */ +struct Range { + /** + * The range's start position. + */ + Position start; + + /** + * The range's end position. + */ + Position end; + + _FORCE_INLINE_ void load(const Dictionary &p_params) { + start.load(p_params["start"]); + end.load(p_params["end"]); + } + + _FORCE_INLINE_ Dictionary to_json() const { + Dictionary dict; + dict["start"] = start.to_json(); + dict["end"] = end.to_json(); + return dict; + } +}; + +/** + * Represents a location inside a resource, such as a line inside a text file. + */ +struct Location { + DocumentUri uri; + Range range; + + _FORCE_INLINE_ void load(const Dictionary &p_params) { + uri = p_params["uri"]; + range.load(p_params["range"]); + } + + _FORCE_INLINE_ Dictionary to_json() const { + Dictionary dict; + dict["uri"] = uri; + dict["range"] = range.to_json(); + return dict; + } +}; + +/** + * Represents a link between a source and a target location. + */ +struct LocationLink { + + /** + * Span of the origin of this link. + * + * Used as the underlined span for mouse interaction. Defaults to the word range at + * the mouse position. + */ + Range *originSelectionRange = NULL; + + /** + * The target resource identifier of this link. + */ + String targetUri; + + /** + * The full target range of this link. If the target for example is a symbol then target range is the + * range enclosing this symbol not including leading/trailing whitespace but everything else + * like comments. This information is typically used to highlight the range in the editor. + */ + Range targetRange; + + /** + * The range that should be selected and revealed when this link is being followed, e.g the name of a function. + * Must be contained by the the `targetRange`. See also `DocumentSymbol#range` + */ + Range targetSelectionRange; +}; + +/** + * A parameter literal used in requests to pass a text document and a position inside that document. + */ +struct TextDocumentPositionParams { + /** + * The text document. + */ + TextDocumentIdentifier textDocument; + + /** + * The position inside the text document. + */ + Position position; + + _FORCE_INLINE_ void load(const Dictionary &p_params) { + textDocument.load(p_params["textDocument"]); + position.load(p_params["position"]); + } + + _FORCE_INLINE_ Dictionary to_json() const { + Dictionary dict; + dict["textDocument"] = textDocument.to_json(); + dict["position"] = position.to_json(); + return dict; + } +}; + +/** + * A textual edit applicable to a text document. + */ +struct TextEdit { + /** + * The range of the text document to be manipulated. To insert + * text into a document create a range where start === end. + */ + Range range; + + /** + * The string to be inserted. For delete operations use an + * empty string. + */ + String newText; +}; + +/** + * Represents a reference to a command. + * Provides a title which will be used to represent a command in the UI. + * Commands are identified by a string identifier. + * The recommended way to handle commands is to implement their execution on the server side if the client and server provides the corresponding capabilities. + * Alternatively the tool extension code could handle the command. The protocol currently doesn’t specify a set of well-known commands. + */ +struct Command { + /** + * Title of the command, like `save`. + */ + String title; + /** + * The identifier of the actual command handler. + */ + String command; + /** + * Arguments that the command handler should be + * invoked with. + */ + Array arguments; + + Dictionary to_json() const { + Dictionary dict; + dict["title"] = title; + dict["command"] = command; + if (arguments.size()) dict["arguments"] = arguments; + return dict; + } +}; + +namespace TextDocumentSyncKind { +/** + * Documents should not be synced at all. + */ +static const int None = 0; + +/** + * Documents are synced by always sending the full content + * of the document. + */ +static const int Full = 1; + +/** + * Documents are synced by sending the full content on open. + * After that only incremental updates to the document are + * send. + */ +static const int Incremental = 2; +}; // namespace TextDocumentSyncKind + +/** + * Completion options. + */ +struct CompletionOptions { + /** + * The server provides support to resolve additional + * information for a completion item. + */ + bool resolveProvider = true; + + /** + * The characters that trigger completion automatically. + */ + Vector<String> triggerCharacters; + + CompletionOptions() { + triggerCharacters.push_back("."); + triggerCharacters.push_back("$"); + triggerCharacters.push_back("'"); + triggerCharacters.push_back("\""); + triggerCharacters.push_back("("); + triggerCharacters.push_back(","); + } + + Dictionary to_json() const { + Dictionary dict; + dict["resolveProvider"] = resolveProvider; + dict["triggerCharacters"] = triggerCharacters; + return dict; + } +}; + +/** + * Signature help options. + */ +struct SignatureHelpOptions { + /** + * The characters that trigger signature help + * automatically. + */ + Vector<String> triggerCharacters; + + Dictionary to_json() { + Dictionary dict; + dict["triggerCharacters"] = triggerCharacters; + return dict; + } +}; + +/** + * Code Lens options. + */ +struct CodeLensOptions { + /** + * Code lens has a resolve provider as well. + */ + bool resolveProvider = false; + + Dictionary to_json() { + Dictionary dict; + dict["resolveProvider"] = resolveProvider; + return dict; + } +}; + +/** + * Rename options + */ +struct RenameOptions { + /** + * Renames should be checked and tested before being executed. + */ + bool prepareProvider = false; + + Dictionary to_json() { + Dictionary dict; + dict["prepareProvider"] = prepareProvider; + return dict; + } +}; + +/** + * Document link options. + */ +struct DocumentLinkOptions { + /** + * Document links have a resolve provider as well. + */ + bool resolveProvider = false; + + Dictionary to_json() { + Dictionary dict; + dict["resolveProvider"] = resolveProvider; + return dict; + } +}; + +/** + * Execute command options. + */ +struct ExecuteCommandOptions { + /** + * The commands to be executed on the server + */ + Vector<String> commands; + + Dictionary to_json() { + Dictionary dict; + dict["commands"] = commands; + return dict; + } +}; + +/** + * Save options. + */ +struct SaveOptions { + /** + * The client is supposed to include the content on save. + */ + bool includeText = true; + + Dictionary to_json() { + Dictionary dict; + dict["includeText"] = includeText; + return dict; + } +}; + +/** + * Color provider options. + */ +struct ColorProviderOptions { + Dictionary to_json() { + Dictionary dict; + return dict; + } +}; + +/** + * Folding range provider options. + */ +struct FoldingRangeProviderOptions { + Dictionary to_json() { + Dictionary dict; + return dict; + } +}; + +struct TextDocumentSyncOptions { + /** + * Open and close notifications are sent to the server. If omitted open close notification should not + * be sent. + */ + bool openClose = true; + + /** + * Change notifications are sent to the server. See TextDocumentSyncKind.None, TextDocumentSyncKind.Full + * and TextDocumentSyncKind.Incremental. If omitted it defaults to TextDocumentSyncKind.None. + */ + int change = TextDocumentSyncKind::Full; + + /** + * If present will save notifications are sent to the server. If omitted the notification should not be + * sent. + */ + bool willSave = false; + + /** + * If present will save wait until requests are sent to the server. If omitted the request should not be + * sent. + */ + bool willSaveWaitUntil = false; + + /** + * If present save notifications are sent to the server. If omitted the notification should not be + * sent. + */ + SaveOptions save; + + Dictionary to_json() { + Dictionary dict; + dict["willSaveWaitUntil"] = willSaveWaitUntil; + dict["willSave"] = willSave; + dict["openClose"] = openClose; + dict["change"] = change; + dict["change"] = save.to_json(); + return dict; + } +}; + +/** + * Static registration options to be returned in the initialize request. + */ +struct StaticRegistrationOptions { + /** + * The id used to register the request. The id can be used to deregister + * the request again. See also Registration#id. + */ + String id; +}; + +/** + * Format document on type options. + */ +struct DocumentOnTypeFormattingOptions { + /** + * A character on which formatting should be triggered, like `}`. + */ + String firstTriggerCharacter; + + /** + * More trigger characters. + */ + Vector<String> moreTriggerCharacter; + + Dictionary to_json() { + Dictionary dict; + dict["firstTriggerCharacter"] = firstTriggerCharacter; + dict["moreTriggerCharacter"] = moreTriggerCharacter; + return dict; + } +}; + +struct TextDocumentItem { + /** + * The text document's URI. + */ + DocumentUri uri; + + /** + * The text document's language identifier. + */ + String languageId; + + /** + * The version number of this document (it will increase after each + * change, including undo/redo). + */ + int version; + + /** + * The content of the opened text document. + */ + String text; + + void load(const Dictionary &p_dict) { + uri = p_dict["uri"]; + languageId = p_dict["languageId"]; + version = p_dict["version"]; + text = p_dict["text"]; + } + + Dictionary to_json() const { + Dictionary dict; + dict["uri"] = uri; + dict["languageId"] = languageId; + dict["version"] = version; + dict["text"] = text; + return dict; + } +}; + +/** + * An event describing a change to a text document. If range and rangeLength are omitted + * the new text is considered to be the full content of the document. + */ +struct TextDocumentContentChangeEvent { + /** + * The range of the document that changed. + */ + Range range; + + /** + * The length of the range that got replaced. + */ + int rangeLength; + + /** + * The new text of the range/document. + */ + String text; + + void load(const Dictionary &p_params) { + text = p_params["text"]; + rangeLength = p_params["rangeLength"]; + range.load(p_params["range"]); + } +}; + +namespace DiagnosticSeverity { +/** + * Reports an error. + */ +static const int Error = 1; +/** + * Reports a warning. + */ +static const int Warning = 2; +/** + * Reports an information. + */ +static const int Information = 3; +/** + * Reports a hint. + */ +static const int Hint = 4; +}; // namespace DiagnosticSeverity + +/** + * Represents a related message and source code location for a diagnostic. This should be + * used to point to code locations that cause or related to a diagnostics, e.g when duplicating + * a symbol in a scope. + */ +struct DiagnosticRelatedInformation { + /** + * The location of this related diagnostic information. + */ + Location location; + + /** + * The message of this related diagnostic information. + */ + String message; + + Dictionary to_json() const { + Dictionary dict; + dict["location"] = location.to_json(), + dict["message"] = message; + return dict; + } +}; + +/** + * Represents a diagnostic, such as a compiler error or warning. + * Diagnostic objects are only valid in the scope of a resource. + */ +struct Diagnostic { + /** + * The range at which the message applies. + */ + Range range; + + /** + * The diagnostic's severity. Can be omitted. If omitted it is up to the + * client to interpret diagnostics as error, warning, info or hint. + */ + int severity; + + /** + * The diagnostic's code, which might appear in the user interface. + */ + int code; + + /** + * A human-readable string describing the source of this + * diagnostic, e.g. 'typescript' or 'super lint'. + */ + String source; + + /** + * The diagnostic's message. + */ + String message; + + /** + * An array of related diagnostic information, e.g. when symbol-names within + * a scope collide all definitions can be marked via this property. + */ + Vector<DiagnosticRelatedInformation> relatedInformation; + + Dictionary to_json() const { + Dictionary dict; + dict["range"] = range.to_json(); + dict["code"] = code; + dict["severity"] = severity; + dict["message"] = message; + dict["source"] = source; + if (!relatedInformation.empty()) { + Array arr; + arr.resize(relatedInformation.size()); + for (int i = 0; i < relatedInformation.size(); i++) { + arr[i] = relatedInformation[i].to_json(); + } + dict["relatedInformation"] = arr; + } + return dict; + } +}; + +/** + * Describes the content type that a client supports in various + * result literals like `Hover`, `ParameterInfo` or `CompletionItem`. + * + * Please note that `MarkupKinds` must not start with a `$`. This kinds + * are reserved for internal usage. + */ +namespace MarkupKind { +static const String PlainText = "plaintext"; +static const String Markdown = "markdown"; +}; // namespace MarkupKind + +/** + * A `MarkupContent` literal represents a string value which content is interpreted base on its + * kind flag. Currently the protocol supports `plaintext` and `markdown` as markup kinds. + * + * If the kind is `markdown` then the value can contain fenced code blocks like in GitHub issues. + * See https://help.github.com/articles/creating-and-highlighting-code-blocks/#syntax-highlighting + * + * Here is an example how such a string can be constructed using JavaScript / TypeScript: + * ```typescript + * let markdown: MarkdownContent = { + * kind: MarkupKind.Markdown, + * value: [ + * '# Header', + * 'Some text', + * '```typescript', + * 'someCode();', + * '```' + * ].join('\n') + * }; + * ``` + * + * *Please Note* that clients might sanitize the return markdown. A client could decide to + * remove HTML from the markdown to avoid script execution. + */ +struct MarkupContent { + /** + * The type of the Markup + */ + String kind; + + /** + * The content itself + */ + String value; + + MarkupContent() { + kind = MarkupKind::Markdown; + } + + MarkupContent(const String &p_value) { + value = p_value; + kind = MarkupKind::Markdown; + } + + Dictionary to_json() const { + Dictionary dict; + dict["kind"] = kind; + dict["value"] = value; + return dict; + } +}; + +/** + * The kind of a completion entry. + */ +namespace CompletionItemKind { +static const int Text = 1; +static const int Method = 2; +static const int Function = 3; +static const int Constructor = 4; +static const int Field = 5; +static const int Variable = 6; +static const int Class = 7; +static const int Interface = 8; +static const int Module = 9; +static const int Property = 10; +static const int Unit = 11; +static const int Value = 12; +static const int Enum = 13; +static const int Keyword = 14; +static const int Snippet = 15; +static const int Color = 16; +static const int File = 17; +static const int Reference = 18; +static const int Folder = 19; +static const int EnumMember = 20; +static const int Constant = 21; +static const int Struct = 22; +static const int Event = 23; +static const int Operator = 24; +static const int TypeParameter = 25; +}; // namespace CompletionItemKind + +/** + * Defines whether the insert text in a completion item should be interpreted as + * plain text or a snippet. + */ +namespace InsertTextFormat { +/** + * The primary text to be inserted is treated as a plain string. + */ +static const int PlainText = 1; + +/** + * The primary text to be inserted is treated as a snippet. + * + * A snippet can define tab stops and placeholders with `$1`, `$2` + * and `${3:foo}`. `$0` defines the final tab stop, it defaults to + * the end of the snippet. Placeholders with equal identifiers are linked, + * that is typing in one will update others too. + */ +static const int Snippet = 2; +}; // namespace InsertTextFormat + +struct CompletionItem { + /** + * The label of this completion item. By default + * also the text that is inserted when selecting + * this completion. + */ + String label; + + /** + * The kind of this completion item. Based of the kind + * an icon is chosen by the editor. The standardized set + * of available values is defined in `CompletionItemKind`. + */ + int kind; + + /** + * A human-readable string with additional information + * about this item, like type or symbol information. + */ + String detail; + + /** + * A human-readable string that represents a doc-comment. + */ + MarkupContent documentation; + + /** + * Indicates if this item is deprecated. + */ + bool deprecated = false; + + /** + * Select this item when showing. + * + * *Note* that only one completion item can be selected and that the + * tool / client decides which item that is. The rule is that the *first* + * item of those that match best is selected. + */ + bool preselect = false; + + /** + * A string that should be used when comparing this item + * with other items. When `falsy` the label is used. + */ + String sortText; + + /** + * A string that should be used when filtering a set of + * completion items. When `falsy` the label is used. + */ + String filterText; + + /** + * A string that should be inserted into a document when selecting + * this completion. When `falsy` the label is used. + * + * The `insertText` is subject to interpretation by the client side. + * Some tools might not take the string literally. For example + * VS Code when code complete is requested in this example `con<cursor position>` + * and a completion item with an `insertText` of `console` is provided it + * will only insert `sole`. Therefore it is recommended to use `textEdit` instead + * since it avoids additional client side interpretation. + * + * @deprecated Use textEdit instead. + */ + String insertText; + + /** + * The format of the insert text. The format applies to both the `insertText` property + * and the `newText` property of a provided `textEdit`. + */ + int insertTextFormat; + + /** + * An edit which is applied to a document when selecting this completion. When an edit is provided the value of + * `insertText` is ignored. + * + * *Note:* The range of the edit must be a single line range and it must contain the position at which completion + * has been requested. + */ + TextEdit textEdit; + + /** + * An optional array of additional text edits that are applied when + * selecting this completion. Edits must not overlap (including the same insert position) + * with the main edit nor with themselves. + * + * Additional text edits should be used to change text unrelated to the current cursor position + * (for example adding an import statement at the top of the file if the completion item will + * insert an unqualified type). + */ + Vector<TextEdit> additionalTextEdits; + + /** + * An optional set of characters that when pressed while this completion is active will accept it first and + * then type that character. *Note* that all commit characters should have `length=1` and that superfluous + * characters will be ignored. + */ + Vector<String> commitCharacters; + + /** + * An optional command that is executed *after* inserting this completion. *Note* that + * additional modifications to the current document should be described with the + * additionalTextEdits-property. + */ + Command command; + + /** + * A data entry field that is preserved on a completion item between + * a completion and a completion resolve request. + */ + Variant data; + + _FORCE_INLINE_ Dictionary to_json(bool resolved = false) const { + Dictionary dict; + dict["label"] = label; + dict["kind"] = kind; + dict["data"] = data; + if (resolved) { + dict["insertText"] = insertText; + dict["detail"] = detail; + dict["documentation"] = documentation.to_json(); + dict["deprecated"] = deprecated; + dict["preselect"] = preselect; + dict["sortText"] = sortText; + dict["filterText"] = filterText; + if (commitCharacters.size()) dict["commitCharacters"] = commitCharacters; + dict["command"] = command.to_json(); + } + return dict; + } + + void load(const Dictionary &p_dict) { + if (p_dict.has("label")) label = p_dict["label"]; + if (p_dict.has("kind")) kind = p_dict["kind"]; + if (p_dict.has("detail")) detail = p_dict["detail"]; + if (p_dict.has("documentation")) { + Variant doc = p_dict["documentation"]; + if (doc.get_type() == Variant::STRING) { + documentation.value = doc; + } else if (doc.get_type() == Variant::DICTIONARY) { + Dictionary v = doc; + documentation.value = v["value"]; + } + } + if (p_dict.has("deprecated")) deprecated = p_dict["deprecated"]; + if (p_dict.has("preselect")) preselect = p_dict["preselect"]; + if (p_dict.has("sortText")) sortText = p_dict["sortText"]; + if (p_dict.has("filterText")) filterText = p_dict["filterText"]; + if (p_dict.has("insertText")) insertText = p_dict["insertText"]; + if (p_dict.has("data")) data = p_dict["data"]; + } +}; + +/** + * Represents a collection of [completion items](#CompletionItem) to be presented + * in the editor. + */ +struct CompletionList { + /** + * This list it not complete. Further typing should result in recomputing + * this list. + */ + bool isIncomplete; + + /** + * The completion items. + */ + Vector<CompletionItem> items; +}; + +/** + * A symbol kind. + */ +namespace SymbolKind { +static const int File = 1; +static const int Module = 2; +static const int Namespace = 3; +static const int Package = 4; +static const int Class = 5; +static const int Method = 6; +static const int Property = 7; +static const int Field = 8; +static const int Constructor = 9; +static const int Enum = 10; +static const int Interface = 11; +static const int Function = 12; +static const int Variable = 13; +static const int Constant = 14; +static const int String = 15; +static const int Number = 16; +static const int Boolean = 17; +static const int Array = 18; +static const int Object = 19; +static const int Key = 20; +static const int Null = 21; +static const int EnumMember = 22; +static const int Struct = 23; +static const int Event = 24; +static const int Operator = 25; +static const int TypeParameter = 26; +}; // namespace SymbolKind + +/** + * Represents information about programming constructs like variables, classes, + * interfaces etc. + */ +struct SymbolInformation { + /** + * The name of this symbol. + */ + String name; + + /** + * The kind of this symbol. + */ + int kind = SymbolKind::File; + + /** + * Indicates if this symbol is deprecated. + */ + bool deprecated = false; + + /** + * The location of this symbol. The location's range is used by a tool + * to reveal the location in the editor. If the symbol is selected in the + * tool the range's start information is used to position the cursor. So + * the range usually spans more then the actual symbol's name and does + * normally include things like visibility modifiers. + * + * The range doesn't have to denote a node range in the sense of a abstract + * syntax tree. It can therefore not be used to re-construct a hierarchy of + * the symbols. + */ + Location location; + + /** + * The name of the symbol containing this symbol. This information is for + * user interface purposes (e.g. to render a qualifier in the user interface + * if necessary). It can't be used to re-infer a hierarchy for the document + * symbols. + */ + String containerName; + + _FORCE_INLINE_ Dictionary to_json() const { + Dictionary dict; + dict["name"] = name; + dict["kind"] = kind; + dict["deprecated"] = deprecated; + dict["location"] = location.to_json(); + dict["containerName"] = containerName; + return dict; + } +}; + +struct DocumentedSymbolInformation : public SymbolInformation { + /** + * A human-readable string with additional information + */ + String detail; + + /** + * A human-readable string that represents a doc-comment. + */ + String documentation; +}; + +/** + * Represents programming constructs like variables, classes, interfaces etc. that appear in a document. Document symbols can be + * hierarchical and they have two ranges: one that encloses its definition and one that points to its most interesting range, + * e.g. the range of an identifier. + */ +struct DocumentSymbol { + + /** + * The name of this symbol. Will be displayed in the user interface and therefore must not be + * an empty string or a string only consisting of white spaces. + */ + String name; + + /** + * More detail for this symbol, e.g the signature of a function. + */ + String detail; + + /** + * Documentation for this symbol + */ + String documentation; + + /** + * Class name for the native symbols + */ + String native_class; + + /** + * The kind of this symbol. + */ + int kind = SymbolKind::File; + + /** + * Indicates if this symbol is deprecated. + */ + bool deprecated = false; + + /** + * The range enclosing this symbol not including leading/trailing whitespace but everything else + * like comments. This information is typically used to determine if the clients cursor is + * inside the symbol to reveal in the symbol in the UI. + */ + Range range; + + /** + * The range that should be selected and revealed when this symbol is being picked, e.g the name of a function. + * Must be contained by the `range`. + */ + Range selectionRange; + + DocumentUri uri; + String script_path; + + /** + * Children of this symbol, e.g. properties of a class. + */ + Vector<DocumentSymbol> children; + + _FORCE_INLINE_ Dictionary to_json() const { + Dictionary dict; + dict["name"] = name; + dict["detail"] = detail; + dict["kind"] = kind; + dict["deprecated"] = deprecated; + dict["range"] = range.to_json(); + dict["selectionRange"] = selectionRange.to_json(); + Array arr; + arr.resize(children.size()); + for (int i = 0; i < children.size(); i++) { + arr[i] = children[i].to_json(); + } + dict["children"] = arr; + return dict; + } + + void symbol_tree_as_list(const String &p_uri, Vector<DocumentedSymbolInformation> &r_list, const String &p_container = "", bool p_join_name = false) const { + DocumentedSymbolInformation si; + if (p_join_name && !p_container.empty()) { + si.name = p_container + ">" + name; + } else { + si.name = name; + } + si.kind = kind; + si.containerName = p_container; + si.deprecated = deprecated; + si.location.uri = p_uri; + si.location.range = range; + si.detail = detail; + si.documentation = documentation; + r_list.push_back(si); + for (int i = 0; i < children.size(); i++) { + children[i].symbol_tree_as_list(p_uri, r_list, si.name, p_join_name); + } + } + + _FORCE_INLINE_ MarkupContent render() const { + MarkupContent markdown; + if (detail.length()) { + markdown.value = "\t" + detail + "\n\n"; + } + if (documentation.length()) { + markdown.value += documentation + "\n\n"; + } + if (script_path.length()) { + markdown.value += "Defined in [" + script_path + "](" + uri + ")"; + } + return markdown; + } + + _FORCE_INLINE_ CompletionItem make_completion_item(bool resolved = false) const { + + lsp::CompletionItem item; + item.label = name; + + if (resolved) { + item.documentation = render(); + } + + switch (kind) { + case lsp::SymbolKind::Enum: + item.kind = lsp::CompletionItemKind::Enum; + break; + case lsp::SymbolKind::Class: + item.kind = lsp::CompletionItemKind::Class; + break; + case lsp::SymbolKind::Property: + item.kind = lsp::CompletionItemKind::Property; + break; + case lsp::SymbolKind::Method: + case lsp::SymbolKind::Function: + item.kind = lsp::CompletionItemKind::Method; + break; + case lsp::SymbolKind::Event: + item.kind = lsp::CompletionItemKind::Event; + break; + case lsp::SymbolKind::Constant: + item.kind = lsp::CompletionItemKind::Constant; + break; + case lsp::SymbolKind::Variable: + item.kind = lsp::CompletionItemKind::Variable; + break; + case lsp::SymbolKind::File: + item.kind = lsp::CompletionItemKind::File; + break; + default: + item.kind = lsp::CompletionItemKind::Text; + break; + } + + return item; + } +}; + +/** + * Enum of known range kinds + */ +namespace FoldingRangeKind { +/** + * Folding range for a comment + */ +static const String Comment = "comment"; +/** + * Folding range for a imports or includes + */ +static const String Imports = "imports"; +/** + * Folding range for a region (e.g. `#region`) + */ +static const String Region = "region"; +} // namespace FoldingRangeKind + +/** + * Represents a folding range. + */ +struct FoldingRange { + + /** + * The zero-based line number from where the folded range starts. + */ + int startLine = 0; + + /** + * The zero-based character offset from where the folded range starts. If not defined, defaults to the length of the start line. + */ + int startCharacter = 0; + + /** + * The zero-based line number where the folded range ends. + */ + int endLine = 0; + + /** + * The zero-based character offset before the folded range ends. If not defined, defaults to the length of the end line. + */ + int endCharacter = 0; + + /** + * Describes the kind of the folding range such as `comment' or 'region'. The kind + * is used to categorize folding ranges and used by commands like 'Fold all comments'. See + * [FoldingRangeKind](#FoldingRangeKind) for an enumeration of standardized kinds. + */ + String kind = FoldingRangeKind::Region; + + _FORCE_INLINE_ Dictionary to_json() const { + Dictionary dict; + dict["startLine"] = startLine; + dict["startCharacter"] = startCharacter; + dict["endLine"] = endLine; + dict["endCharacter"] = endCharacter; + return dict; + } +}; + +/** + * How a completion was triggered + */ +namespace CompletionTriggerKind { +/** + * Completion was triggered by typing an identifier (24x7 code + * complete), manual invocation (e.g Ctrl+Space) or via API. + */ +static const int Invoked = 1; + +/** + * Completion was triggered by a trigger character specified by + * the `triggerCharacters` properties of the `CompletionRegistrationOptions`. + */ +static const int TriggerCharacter = 2; + +/** + * Completion was re-triggered as the current completion list is incomplete. + */ +static const int TriggerForIncompleteCompletions = 3; +} // namespace CompletionTriggerKind + +/** + * Contains additional information about the context in which a completion request is triggered. + */ +struct CompletionContext { + /** + * How the completion was triggered. + */ + int triggerKind = CompletionTriggerKind::TriggerCharacter; + + /** + * The trigger character (a single character) that has trigger code complete. + * Is undefined if `triggerKind !== CompletionTriggerKind.TriggerCharacter` + */ + String triggerCharacter; + + void load(const Dictionary &p_params) { + triggerKind = int(p_params["triggerKind"]); + triggerCharacter = p_params["triggerCharacter"]; + } +}; + +struct CompletionParams : public TextDocumentPositionParams { + + /** + * The completion context. This is only available if the client specifies + * to send this using `ClientCapabilities.textDocument.completion.contextSupport === true` + */ + CompletionContext context; + + void load(const Dictionary &p_params) { + TextDocumentPositionParams::load(p_params); + context.load(p_params["context"]); + } +}; + +/** + * The result of a hover request. + */ +struct Hover { + /** + * The hover's content + */ + MarkupContent contents; + + /** + * An optional range is a range inside a text document + * that is used to visualize a hover, e.g. by changing the background color. + */ + Range range; + + _FORCE_INLINE_ Dictionary to_json() const { + Dictionary dict; + dict["range"] = range.to_json(); + dict["contents"] = contents.to_json(); + return dict; + } +}; + +struct ServerCapabilities { + /** + * Defines how text documents are synced. Is either a detailed structure defining each notification or + * for backwards compatibility the TextDocumentSyncKind number. If omitted it defaults to `TextDocumentSyncKind.None`. + */ + TextDocumentSyncOptions textDocumentSync; + + /** + * The server provides hover support. + */ + bool hoverProvider = true; + + /** + * The server provides completion support. + */ + CompletionOptions completionProvider; + + /** + * The server provides signature help support. + */ + SignatureHelpOptions signatureHelpProvider; + + /** + * The server provides goto definition support. + */ + bool definitionProvider = true; + + /** + * The server provides Goto Type Definition support. + * + * Since 3.6.0 + */ + bool typeDefinitionProvider = false; + + /** + * The server provides Goto Implementation support. + * + * Since 3.6.0 + */ + bool implementationProvider = false; + + /** + * The server provides find references support. + */ + bool referencesProvider = false; + + /** + * The server provides document highlight support. + */ + bool documentHighlightProvider = false; + + /** + * The server provides document symbol support. + */ + bool documentSymbolProvider = true; + + /** + * The server provides workspace symbol support. + */ + bool workspaceSymbolProvider = true; + + /** + * The server provides code actions. The `CodeActionOptions` return type is only + * valid if the client signals code action literal support via the property + * `textDocument.codeAction.codeActionLiteralSupport`. + */ + bool codeActionProvider = false; + + /** + * The server provides code lens. + */ + CodeLensOptions codeLensProvider; + + /** + * The server provides document formatting. + */ + bool documentFormattingProvider = false; + + /** + * The server provides document range formatting. + */ + bool documentRangeFormattingProvider = false; + + /** + * The server provides document formatting on typing. + */ + DocumentOnTypeFormattingOptions documentOnTypeFormattingProvider; + + /** + * The server provides rename support. RenameOptions may only be + * specified if the client states that it supports + * `prepareSupport` in its initial `initialize` request. + */ + RenameOptions renameProvider; + + /** + * The server provides document link support. + */ + DocumentLinkOptions documentLinkProvider; + + /** + * The server provides color provider support. + * + * Since 3.6.0 + */ + ColorProviderOptions colorProvider; + + /** + * The server provides folding provider support. + * + * Since 3.10.0 + */ + FoldingRangeProviderOptions foldingRangeProvider; + + /** + * The server provides go to declaration support. + * + * Since 3.14.0 + */ + bool declarationProvider = true; + + /** + * The server provides execute command support. + */ + ExecuteCommandOptions executeCommandProvider; + + _FORCE_INLINE_ Dictionary to_json() { + Dictionary dict; + dict["textDocumentSync"] = (int)textDocumentSync.change; + dict["completionProvider"] = completionProvider.to_json(); + dict["signatureHelpProvider"] = signatureHelpProvider.to_json(); + dict["codeLensProvider"] = false; // codeLensProvider.to_json(); + dict["documentOnTypeFormattingProvider"] = documentOnTypeFormattingProvider.to_json(); + dict["renameProvider"] = renameProvider.to_json(); + dict["documentLinkProvider"] = documentLinkProvider.to_json(); + dict["colorProvider"] = false; // colorProvider.to_json(); + dict["foldingRangeProvider"] = false; //foldingRangeProvider.to_json(); + dict["executeCommandProvider"] = executeCommandProvider.to_json(); + dict["hoverProvider"] = hoverProvider; + dict["definitionProvider"] = definitionProvider; + dict["typeDefinitionProvider"] = typeDefinitionProvider; + dict["implementationProvider"] = implementationProvider; + dict["referencesProvider"] = referencesProvider; + dict["documentHighlightProvider"] = documentHighlightProvider; + dict["documentSymbolProvider"] = documentSymbolProvider; + dict["workspaceSymbolProvider"] = workspaceSymbolProvider; + dict["codeActionProvider"] = codeActionProvider; + dict["documentFormattingProvider"] = documentFormattingProvider; + dict["documentRangeFormattingProvider"] = documentRangeFormattingProvider; + dict["declarationProvider"] = declarationProvider; + return dict; + } +}; + +struct InitializeResult { + /** + * The capabilities the language server provides. + */ + ServerCapabilities capabilities; + + _FORCE_INLINE_ Dictionary to_json() { + Dictionary dict; + dict["capabilities"] = capabilities.to_json(); + return dict; + } +}; + +} // namespace lsp + +#endif diff --git a/modules/gdscript/register_types.cpp b/modules/gdscript/register_types.cpp index 62117dcaf3..d07949b34b 100644 --- a/modules/gdscript/register_types.cpp +++ b/modules/gdscript/register_types.cpp @@ -37,6 +37,7 @@ #include "editor/gdscript_highlighter.h" #include "gdscript.h" #include "gdscript_tokenizer.h" +#include "language_server/gdscript_language_server.h" GDScriptLanguage *script_language_gd = NULL; Ref<ResourceFormatLoaderGDScript> resource_loader_gd; @@ -44,6 +45,7 @@ Ref<ResourceFormatSaverGDScript> resource_saver_gd; #ifdef TOOLS_ENABLED +#include "core/engine.h" #include "editor/editor_export.h" #include "editor/editor_node.h" #include "editor/editor_settings.h" @@ -134,6 +136,11 @@ static void _editor_init() { Ref<EditorExportGDScript> gd_export; gd_export.instance(); EditorExport::get_singleton()->add_export_plugin(gd_export); + + register_lsp_types(); + GDScriptLanguageServer *lsp_plugin = memnew(GDScriptLanguageServer); + EditorNode::get_singleton()->add_editor_plugin(lsp_plugin); + Engine::get_singleton()->add_singleton(Engine::Singleton("GDScriptLanguageProtocol", GDScriptLanguageProtocol::get_singleton())); } #endif diff --git a/modules/hdr/image_loader_hdr.cpp b/modules/hdr/image_loader_hdr.cpp index e610619b54..1abf26bfee 100644 --- a/modules/hdr/image_loader_hdr.cpp +++ b/modules/hdr/image_loader_hdr.cpp @@ -37,7 +37,7 @@ Error ImageLoaderHDR::load_image(Ref<Image> p_image, FileAccess *f, bool p_force String header = f->get_token(); - ERR_FAIL_COND_V(header != "#?RADIANCE" && header != "#?RGBE", ERR_FILE_UNRECOGNIZED); + ERR_FAIL_COND_V_MSG(header != "#?RADIANCE" && header != "#?RGBE", ERR_FILE_UNRECOGNIZED, "Unsupported header information in HDR: " + header + "."); while (true) { String line = f->get_line(); @@ -45,12 +45,9 @@ Error ImageLoaderHDR::load_image(Ref<Image> p_image, FileAccess *f, bool p_force if (line == "") // empty line indicates end of header break; if (line.begins_with("FORMAT=")) { // leave option to implement other commands - if (line != "FORMAT=32-bit_rle_rgbe") { - ERR_EXPLAIN("Only 32-bit_rle_rgbe is supported for HDR files."); - return ERR_FILE_UNRECOGNIZED; - } + ERR_FAIL_COND_V_MSG(line != "FORMAT=32-bit_rle_rgbe", ERR_FILE_UNRECOGNIZED, "Only 32-bit_rle_rgbe is supported for HDR files."); } else if (!line.begins_with("#")) { // not comment - WARN_PRINTS("Ignoring unsupported header information in HDR : " + line); + WARN_PRINTS("Ignoring unsupported header information in HDR: " + line + "."); } } diff --git a/modules/jsonrpc/SCsub b/modules/jsonrpc/SCsub new file mode 100644 index 0000000000..13c9ffb253 --- /dev/null +++ b/modules/jsonrpc/SCsub @@ -0,0 +1,7 @@ +#!/usr/bin/env python + +Import('env') +Import('env_modules') + +env_jsonrpc = env_modules.Clone() +env_jsonrpc.add_source_files(env.modules_sources, "*.cpp") diff --git a/modules/jsonrpc/config.py b/modules/jsonrpc/config.py new file mode 100644 index 0000000000..53bc827027 --- /dev/null +++ b/modules/jsonrpc/config.py @@ -0,0 +1,5 @@ +def can_build(env, platform): + return True + +def configure(env): + pass diff --git a/modules/jsonrpc/jsonrpc.cpp b/modules/jsonrpc/jsonrpc.cpp new file mode 100644 index 0000000000..b18b48d1b0 --- /dev/null +++ b/modules/jsonrpc/jsonrpc.cpp @@ -0,0 +1,171 @@ +/*************************************************************************/ +/* jsonrpc.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "jsonrpc.h" +#include "core/io/json.h" + +JSONRPC::JSONRPC() { +} + +JSONRPC::~JSONRPC() { +} + +void JSONRPC::_bind_methods() { + ClassDB::bind_method(D_METHOD("set_scope", "scope", "target"), &JSONRPC::set_scope); + ClassDB::bind_method(D_METHOD("process_action", "action", "recurse"), &JSONRPC::process_action, DEFVAL(false)); + ClassDB::bind_method(D_METHOD("process_string", "action"), &JSONRPC::process_string); + + ClassDB::bind_method(D_METHOD("make_request", "method", "params", "id"), &JSONRPC::make_request); + ClassDB::bind_method(D_METHOD("make_response", "result", "id"), &JSONRPC::make_response); + ClassDB::bind_method(D_METHOD("make_notification", "method", "params"), &JSONRPC::make_notification); + ClassDB::bind_method(D_METHOD("make_response_error", "code", "message", "id"), &JSONRPC::make_response_error, DEFVAL(Variant())); + + BIND_ENUM_CONSTANT(ParseError) + BIND_ENUM_CONSTANT(InvalidRequest) + BIND_ENUM_CONSTANT(MethodNotFound) + BIND_ENUM_CONSTANT(InvalidParams) + BIND_ENUM_CONSTANT(InternalError) +} + +Dictionary JSONRPC::make_response_error(int p_code, const String &p_message, const Variant &p_id) const { + Dictionary dict; + dict["jsonrpc"] = "2.0"; + + Dictionary err; + err["code"] = p_code; + err["message"] = p_message; + + dict["error"] = err; + dict["id"] = p_id; + + return dict; +} + +Dictionary JSONRPC::make_response(const Variant &p_value, const Variant &p_id) { + Dictionary dict; + dict["jsonrpc"] = "2.0"; + dict["id"] = p_id; + dict["result"] = p_value; + return dict; +} + +Dictionary JSONRPC::make_notification(const String &p_method, const Variant &p_params) { + Dictionary dict; + dict["jsonrpc"] = "2.0"; + dict["method"] = p_method; + dict["params"] = p_params; + return dict; +} + +Dictionary JSONRPC::make_request(const String &p_method, const Variant &p_params, const Variant &p_id) { + Dictionary dict; + dict["jsonrpc"] = "2.0"; + dict["method"] = p_method; + dict["params"] = p_params; + dict["id"] = p_id; + return dict; +} + +Variant JSONRPC::process_action(const Variant &p_action, bool p_process_arr_elements) { + Variant ret; + if (p_action.get_type() == Variant::DICTIONARY) { + Dictionary dict = p_action; + String method = dict.get("method", ""); + Array args; + if (dict.has("params")) { + Variant params = dict.get("params", Variant()); + if (params.get_type() == Variant::ARRAY) { + args = params; + } else { + args.push_back(params); + } + } + + Object *object = this; + if (method_scopes.has(method.get_base_dir())) { + object = method_scopes[method.get_base_dir()]; + method = method.get_file(); + } + + Variant id; + if (dict.has("id")) { + id = dict["id"]; + } + + if (object == NULL || !object->has_method(method)) { + ret = make_response_error(JSONRPC::MethodNotFound, "Method not found", id); + } else { + Variant call_ret = object->callv(method, args); + if (id.get_type() != Variant::NIL) { + ret = make_response(call_ret, id); + } + } + } else if (p_action.get_type() == Variant::ARRAY && p_process_arr_elements) { + Array arr = p_action; + int size = arr.size(); + if (size) { + Array arr_ret; + for (int i = 0; i < size; i++) { + const Variant &var = arr.get(i); + arr_ret.push_back(process_action(var)); + } + ret = arr_ret; + } else { + ret = make_response_error(JSONRPC::InvalidRequest, "Invalid Request"); + } + } else { + ret = make_response_error(JSONRPC::InvalidRequest, "Invalid Request"); + } + return ret; +} + +String JSONRPC::process_string(const String &p_input) { + + if (p_input.empty()) return String(); + + Variant ret; + Variant input; + String err_message; + int err_line; + if (OK != JSON::parse(p_input, input, err_message, err_line)) { + ret = make_response_error(JSONRPC::ParseError, "Parse error"); + } else { + ret = process_action(input, true); + } + + if (ret.get_type() == Variant::NIL) { + return ""; + } + return JSON::print(ret); +} + +void JSONRPC::set_scope(const String &p_scope, Object *p_obj) { + method_scopes[p_scope] = p_obj; +} diff --git a/modules/jsonrpc/jsonrpc.h b/modules/jsonrpc/jsonrpc.h new file mode 100644 index 0000000000..bcb34ecc65 --- /dev/null +++ b/modules/jsonrpc/jsonrpc.h @@ -0,0 +1,70 @@ +/*************************************************************************/ +/* jsonrpc.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef GODOT_JSON_RPC_H +#define GODOT_JSON_RPC_H + +#include "core/object.h" +#include "core/variant.h" + +class JSONRPC : public Object { + GDCLASS(JSONRPC, Object) + + Map<String, Object *> method_scopes; + +protected: + static void _bind_methods(); + +public: + JSONRPC(); + ~JSONRPC(); + + enum ErrorCode { + ParseError = -32700, + InvalidRequest = -32600, + MethodNotFound = -32601, + InvalidParams = -32602, + InternalError = -32603, + }; + + Dictionary make_response_error(int p_code, const String &p_message, const Variant &p_id = Variant()) const; + Dictionary make_response(const Variant &p_value, const Variant &p_id); + Dictionary make_notification(const String &p_method, const Variant &p_params); + Dictionary make_request(const String &p_method, const Variant &p_params, const Variant &p_id); + + Variant process_action(const Variant &p_action, bool p_process_arr_elements = false); + String process_string(const String &p_input); + + void set_scope(const String &p_scope, Object *p_obj); +}; + +VARIANT_ENUM_CAST(JSONRPC::ErrorCode); + +#endif diff --git a/modules/jsonrpc/register_types.cpp b/modules/jsonrpc/register_types.cpp new file mode 100644 index 0000000000..242b0e9df4 --- /dev/null +++ b/modules/jsonrpc/register_types.cpp @@ -0,0 +1,40 @@ +/*************************************************************************/ +/* register_types.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "register_types.h" +#include "core/class_db.h" +#include "jsonrpc.h" + +void register_jsonrpc_types() { + ClassDB::register_class<JSONRPC>(); +} + +void unregister_jsonrpc_types() { +} diff --git a/modules/jsonrpc/register_types.h b/modules/jsonrpc/register_types.h new file mode 100644 index 0000000000..e4648b901f --- /dev/null +++ b/modules/jsonrpc/register_types.h @@ -0,0 +1,32 @@ +/*************************************************************************/ +/* register_types.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +void register_jsonrpc_types(); +void unregister_jsonrpc_types(); diff --git a/modules/mbedtls/crypto_mbedtls.cpp b/modules/mbedtls/crypto_mbedtls.cpp new file mode 100644 index 0000000000..1e02084ae2 --- /dev/null +++ b/modules/mbedtls/crypto_mbedtls.cpp @@ -0,0 +1,285 @@ +/*************************************************************************/ +/* crypto_mbedtls.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "crypto_mbedtls.h" + +#include "core/os/file_access.h" + +#include "core/engine.h" +#include "core/io/certs_compressed.gen.h" +#include "core/io/compression.h" +#include "core/project_settings.h" + +#ifdef TOOLS_ENABLED +#include "editor/editor_settings.h" +#endif +#define PEM_BEGIN_CRT "-----BEGIN CERTIFICATE-----\n" +#define PEM_END_CRT "-----END CERTIFICATE-----\n" + +#include "mbedtls/pem.h" +#include <mbedtls/debug.h> + +CryptoKey *CryptoKeyMbedTLS::create() { + return memnew(CryptoKeyMbedTLS); +} + +Error CryptoKeyMbedTLS::load(String p_path) { + ERR_FAIL_COND_V_MSG(locks, ERR_ALREADY_IN_USE, "Key is in use"); + + PoolByteArray out; + FileAccess *f = FileAccess::open(p_path, FileAccess::READ); + ERR_FAIL_COND_V(!f, ERR_INVALID_PARAMETER); + + int flen = f->get_len(); + out.resize(flen + 1); + { + PoolByteArray::Write w = out.write(); + f->get_buffer(w.ptr(), flen); + w[flen] = 0; //end f string + } + memdelete(f); + + int ret = mbedtls_pk_parse_key(&pkey, out.read().ptr(), out.size(), NULL, 0); + // We MUST zeroize the memory for safety! + mbedtls_platform_zeroize(out.write().ptr(), out.size()); + ERR_FAIL_COND_V_MSG(ret, FAILED, "Error parsing private key: " + itos(ret)); + + return OK; +} + +Error CryptoKeyMbedTLS::save(String p_path) { + FileAccess *f = FileAccess::open(p_path, FileAccess::WRITE); + ERR_FAIL_COND_V(!f, ERR_INVALID_PARAMETER); + + unsigned char w[16000]; + memset(w, 0, sizeof(w)); + + int ret = mbedtls_pk_write_key_pem(&pkey, w, sizeof(w)); + if (ret != 0) { + memdelete(f); + memset(w, 0, sizeof(w)); // Zeroize anything we might have written. + ERR_FAIL_V_MSG(FAILED, "Error writing key: " + itos(ret)); + } + + size_t len = strlen((char *)w); + f->store_buffer(w, len); + memdelete(f); + memset(w, 0, sizeof(w)); // Zeroize temporary buffer. + return OK; +} + +X509Certificate *X509CertificateMbedTLS::create() { + return memnew(X509CertificateMbedTLS); +} + +Error X509CertificateMbedTLS::load(String p_path) { + ERR_FAIL_COND_V_MSG(locks, ERR_ALREADY_IN_USE, "Certificate is in use"); + + PoolByteArray out; + FileAccess *f = FileAccess::open(p_path, FileAccess::READ); + ERR_FAIL_COND_V(!f, ERR_INVALID_PARAMETER); + + int flen = f->get_len(); + out.resize(flen + 1); + { + PoolByteArray::Write w = out.write(); + f->get_buffer(w.ptr(), flen); + w[flen] = 0; //end f string + } + memdelete(f); + + int ret = mbedtls_x509_crt_parse(&cert, out.read().ptr(), out.size()); + ERR_FAIL_COND_V_MSG(ret, FAILED, "Error parsing some certificates: " + itos(ret)); + + return OK; +} + +Error X509CertificateMbedTLS::load_from_memory(const uint8_t *p_buffer, int p_len) { + ERR_FAIL_COND_V_MSG(locks, ERR_ALREADY_IN_USE, "Certificate is in use"); + + int ret = mbedtls_x509_crt_parse(&cert, p_buffer, p_len); + ERR_FAIL_COND_V_MSG(ret, FAILED, "Error parsing certificates: " + itos(ret)); + return OK; +} + +Error X509CertificateMbedTLS::save(String p_path) { + FileAccess *f = FileAccess::open(p_path, FileAccess::WRITE); + ERR_FAIL_COND_V(!f, ERR_INVALID_PARAMETER); + + mbedtls_x509_crt *crt = &cert; + while (crt) { + unsigned char w[4096]; + size_t wrote = 0; + int ret = mbedtls_pem_write_buffer(PEM_BEGIN_CRT, PEM_END_CRT, cert.raw.p, cert.raw.len, w, sizeof(w), &wrote); + if (ret != 0 || wrote == 0) { + memdelete(f); + ERR_FAIL_V_MSG(FAILED, "Error writing certificate: " + itos(ret)); + } + + f->store_buffer(w, wrote - 1); // don't write the string terminator + crt = crt->next; + } + memdelete(f); + return OK; +} + +Crypto *CryptoMbedTLS::create() { + return memnew(CryptoMbedTLS); +} + +void CryptoMbedTLS::initialize_crypto() { + +#ifdef DEBUG_ENABLED + mbedtls_debug_set_threshold(1); +#endif + + Crypto::_create = create; + Crypto::_load_default_certificates = load_default_certificates; + X509CertificateMbedTLS::make_default(); + CryptoKeyMbedTLS::make_default(); +} + +void CryptoMbedTLS::finalize_crypto() { + Crypto::_create = NULL; + Crypto::_load_default_certificates = NULL; + if (default_certs) { + memdelete(default_certs); + default_certs = NULL; + } + X509CertificateMbedTLS::finalize(); + CryptoKeyMbedTLS::finalize(); +} + +CryptoMbedTLS::CryptoMbedTLS() { + mbedtls_ctr_drbg_init(&ctr_drbg); + mbedtls_entropy_init(&entropy); + int ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0); + if (ret != 0) { + ERR_PRINTS(" failed\n ! mbedtls_ctr_drbg_seed returned an error" + itos(ret)); + } +} + +CryptoMbedTLS::~CryptoMbedTLS() { + mbedtls_ctr_drbg_free(&ctr_drbg); + mbedtls_entropy_free(&entropy); +} + +X509CertificateMbedTLS *CryptoMbedTLS::default_certs = NULL; + +X509CertificateMbedTLS *CryptoMbedTLS::get_default_certificates() { + return default_certs; +} + +void CryptoMbedTLS::load_default_certificates(String p_path) { + ERR_FAIL_COND(default_certs != NULL); + + default_certs = memnew(X509CertificateMbedTLS); + ERR_FAIL_COND(default_certs == NULL); + + String certs_path = GLOBAL_DEF("network/ssl/certificates", ""); + + if (p_path != "") { + // Use certs defined in project settings. + default_certs->load(p_path); + } +#ifdef BUILTIN_CERTS_ENABLED + else { + // Use builtin certs only if user did not override it in project settings. + PoolByteArray out; + out.resize(_certs_uncompressed_size + 1); + PoolByteArray::Write w = out.write(); + Compression::decompress(w.ptr(), _certs_uncompressed_size, _certs_compressed, _certs_compressed_size, Compression::MODE_DEFLATE); + w[_certs_uncompressed_size] = 0; // Make sure it ends with string terminator +#ifdef DEBUG_ENABLED + print_verbose("Loaded builtin certs"); +#endif + default_certs->load_from_memory(out.read().ptr(), out.size()); + } +#endif +} + +Ref<CryptoKey> CryptoMbedTLS::generate_rsa(int p_bytes) { + Ref<CryptoKeyMbedTLS> out; + out.instance(); + int ret = mbedtls_pk_setup(&(out->pkey), mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)); + ERR_FAIL_COND_V(ret != 0, NULL); + ret = mbedtls_rsa_gen_key(mbedtls_pk_rsa(out->pkey), mbedtls_ctr_drbg_random, &ctr_drbg, p_bytes, 65537); + ERR_FAIL_COND_V(ret != 0, NULL); + return out; +} + +Ref<X509Certificate> CryptoMbedTLS::generate_self_signed_certificate(Ref<CryptoKey> p_key, String p_issuer_name, String p_not_before, String p_not_after) { + Ref<CryptoKeyMbedTLS> key = static_cast<Ref<CryptoKeyMbedTLS> >(p_key); + mbedtls_x509write_cert crt; + mbedtls_x509write_crt_init(&crt); + + mbedtls_x509write_crt_set_subject_key(&crt, &(key->pkey)); + mbedtls_x509write_crt_set_issuer_key(&crt, &(key->pkey)); + mbedtls_x509write_crt_set_subject_name(&crt, p_issuer_name.utf8().get_data()); + mbedtls_x509write_crt_set_issuer_name(&crt, p_issuer_name.utf8().get_data()); + mbedtls_x509write_crt_set_version(&crt, MBEDTLS_X509_CRT_VERSION_3); + mbedtls_x509write_crt_set_md_alg(&crt, MBEDTLS_MD_SHA256); + + mbedtls_mpi serial; + mbedtls_mpi_init(&serial); + uint8_t rand_serial[20]; + mbedtls_ctr_drbg_random(&ctr_drbg, rand_serial, 20); + ERR_FAIL_COND_V(mbedtls_mpi_read_binary(&serial, rand_serial, 20), NULL); + mbedtls_x509write_crt_set_serial(&crt, &serial); + + mbedtls_x509write_crt_set_validity(&crt, p_not_before.utf8().get_data(), p_not_after.utf8().get_data()); + mbedtls_x509write_crt_set_basic_constraints(&crt, 1, -1); + mbedtls_x509write_crt_set_basic_constraints(&crt, 1, 0); + + unsigned char buf[4096]; + memset(buf, 0, 4096); + Ref<X509CertificateMbedTLS> out; + out.instance(); + mbedtls_x509write_crt_pem(&crt, buf, 4096, mbedtls_ctr_drbg_random, &ctr_drbg); + + int err = mbedtls_x509_crt_parse(&(out->cert), buf, 4096); + if (err != 0) { + mbedtls_mpi_free(&serial); + mbedtls_x509write_crt_free(&crt); + ERR_PRINTS("Generated invalid certificate: " + itos(err)); + return NULL; + } + + mbedtls_mpi_free(&serial); + mbedtls_x509write_crt_free(&crt); + return out; +} + +PoolByteArray CryptoMbedTLS::generate_random_bytes(int p_bytes) { + PoolByteArray out; + out.resize(p_bytes); + mbedtls_ctr_drbg_random(&ctr_drbg, out.write().ptr(), p_bytes); + return out; +} diff --git a/modules/mbedtls/crypto_mbedtls.h b/modules/mbedtls/crypto_mbedtls.h new file mode 100644 index 0000000000..06b3ecd234 --- /dev/null +++ b/modules/mbedtls/crypto_mbedtls.h @@ -0,0 +1,124 @@ +/*************************************************************************/ +/* crypto_mbedtls.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef CRYPTO_MBEDTLS_H +#define CRYPTO_MBEDTLS_H + +#include "core/crypto/crypto.h" +#include "core/resource.h" + +#include <mbedtls/ctr_drbg.h> +#include <mbedtls/entropy.h> +#include <mbedtls/ssl.h> + +class CryptoMbedTLS; +class SSLContextMbedTLS; +class CryptoKeyMbedTLS : public CryptoKey { + +private: + mbedtls_pk_context pkey; + int locks; + +public: + static CryptoKey *create(); + static void make_default() { CryptoKey::_create = create; } + static void finalize() { CryptoKey::_create = NULL; } + + virtual Error load(String p_path); + virtual Error save(String p_path); + + CryptoKeyMbedTLS() { + mbedtls_pk_init(&pkey); + locks = 0; + } + ~CryptoKeyMbedTLS() { + mbedtls_pk_free(&pkey); + } + + _FORCE_INLINE_ void lock() { locks++; } + _FORCE_INLINE_ void unlock() { locks--; } + + friend class CryptoMbedTLS; + friend class SSLContextMbedTLS; +}; + +class X509CertificateMbedTLS : public X509Certificate { + +private: + mbedtls_x509_crt cert; + int locks; + +public: + static X509Certificate *create(); + static void make_default() { X509Certificate::_create = create; } + static void finalize() { X509Certificate::_create = NULL; } + + virtual Error load(String p_path); + virtual Error load_from_memory(const uint8_t *p_buffer, int p_len); + virtual Error save(String p_path); + + X509CertificateMbedTLS() { + mbedtls_x509_crt_init(&cert); + locks = 0; + } + ~X509CertificateMbedTLS() { + mbedtls_x509_crt_free(&cert); + } + + _FORCE_INLINE_ void lock() { locks++; } + _FORCE_INLINE_ void unlock() { locks--; } + + friend class CryptoMbedTLS; + friend class SSLContextMbedTLS; +}; + +class CryptoMbedTLS : public Crypto { + +private: + mbedtls_entropy_context entropy; + mbedtls_ctr_drbg_context ctr_drbg; + static X509CertificateMbedTLS *default_certs; + +public: + static Crypto *create(); + static void initialize_crypto(); + static void finalize_crypto(); + static X509CertificateMbedTLS *get_default_certificates(); + static void load_default_certificates(String p_path); + + virtual PoolByteArray generate_random_bytes(int p_bytes); + virtual Ref<CryptoKey> generate_rsa(int p_bytes); + virtual Ref<X509Certificate> generate_self_signed_certificate(Ref<CryptoKey> p_key, String p_issuer_name, String p_not_before, String p_not_after); + + CryptoMbedTLS(); + ~CryptoMbedTLS(); +}; + +#endif // CRYPTO_MBEDTLS_H diff --git a/modules/mbedtls/register_types.cpp b/modules/mbedtls/register_types.cpp index 121ed5eb02..f7dc6c785f 100755 --- a/modules/mbedtls/register_types.cpp +++ b/modules/mbedtls/register_types.cpp @@ -30,15 +30,17 @@ #include "register_types.h" -#include "stream_peer_mbed_tls.h" +#include "crypto_mbedtls.h" +#include "stream_peer_mbedtls.h" void register_mbedtls_types() { - ClassDB::register_class<StreamPeerMbedTLS>(); + CryptoMbedTLS::initialize_crypto(); StreamPeerMbedTLS::initialize_ssl(); } void unregister_mbedtls_types() { StreamPeerMbedTLS::finalize_ssl(); + CryptoMbedTLS::finalize_crypto(); } diff --git a/modules/mbedtls/ssl_context_mbedtls.cpp b/modules/mbedtls/ssl_context_mbedtls.cpp new file mode 100644 index 0000000000..eeaf831b4a --- /dev/null +++ b/modules/mbedtls/ssl_context_mbedtls.cpp @@ -0,0 +1,151 @@ +/*************************************************************************/ +/* ssl_context_mbedtls.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "ssl_context_mbedtls.h" + +static void my_debug(void *ctx, int level, + const char *file, int line, + const char *str) { + + printf("%s:%04d: %s", file, line, str); + fflush(stdout); +} + +Error SSLContextMbedTLS::_setup(int p_endpoint, int p_transport, int p_authmode) { + ERR_FAIL_COND_V_MSG(inited, ERR_ALREADY_IN_USE, "This SSL context is already active"); + + mbedtls_ssl_init(&ssl); + mbedtls_ssl_config_init(&conf); + mbedtls_ctr_drbg_init(&ctr_drbg); + mbedtls_entropy_init(&entropy); + inited = true; + + int ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0); + if (ret != 0) { + clear(); // Never leave unusable resources around. + ERR_FAIL_V_MSG(FAILED, "mbedtls_ctr_drbg_seed returned an error" + itos(ret)); + } + + ret = mbedtls_ssl_config_defaults(&conf, p_endpoint, p_transport, MBEDTLS_SSL_PRESET_DEFAULT); + if (ret != 0) { + clear(); + ERR_FAIL_V_MSG(FAILED, "mbedtls_ssl_config_defaults returned an error" + itos(ret)); + } + mbedtls_ssl_conf_authmode(&conf, p_authmode); + mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg); + mbedtls_ssl_conf_dbg(&conf, my_debug, stdout); + return OK; +} + +Error SSLContextMbedTLS::init_server(int p_transport, int p_authmode, Ref<CryptoKeyMbedTLS> p_pkey, Ref<X509CertificateMbedTLS> p_cert) { + ERR_FAIL_COND_V(!p_pkey.is_valid(), ERR_INVALID_PARAMETER); + ERR_FAIL_COND_V(!p_cert.is_valid(), ERR_INVALID_PARAMETER); + + Error err = _setup(MBEDTLS_SSL_IS_SERVER, p_transport, p_authmode); + ERR_FAIL_COND_V(err != OK, err); + + // Locking key and certificate(s) + pkey = p_pkey; + certs = p_cert; + if (pkey.is_valid()) + pkey->lock(); + if (certs.is_valid()) + certs->lock(); + + // Adding key and certificate + int ret = mbedtls_ssl_conf_own_cert(&conf, &(certs->cert), &(pkey->pkey)); + if (ret != 0) { + clear(); + ERR_FAIL_V_MSG(ERR_INVALID_PARAMETER, "Invalid cert/key combination " + itos(ret)); + } + // Adding CA chain if available. + if (certs->cert.next) { + mbedtls_ssl_conf_ca_chain(&conf, certs->cert.next, NULL); + } + mbedtls_ssl_setup(&ssl, &conf); + return OK; +} + +Error SSLContextMbedTLS::init_client(int p_transport, int p_authmode, Ref<X509CertificateMbedTLS> p_valid_cas) { + Error err = _setup(MBEDTLS_SSL_IS_CLIENT, p_transport, p_authmode); + ERR_FAIL_COND_V(err != OK, err); + + X509CertificateMbedTLS *cas = NULL; + + if (p_valid_cas.is_valid()) { + // Locking CA certificates + certs = p_valid_cas; + certs->lock(); + cas = certs.ptr(); + } else { + // Fall back to default certificates (no need to lock those). + cas = CryptoMbedTLS::get_default_certificates(); + if (cas == NULL) { + clear(); + ERR_FAIL_V_MSG(ERR_UNCONFIGURED, "SSL module failed to initialize!"); + } + } + + // Set valid CAs + mbedtls_ssl_conf_ca_chain(&conf, &(cas->cert), NULL); + mbedtls_ssl_setup(&ssl, &conf); + return OK; +} + +void SSLContextMbedTLS::clear() { + if (!inited) + return; + mbedtls_ssl_free(&ssl); + mbedtls_ssl_config_free(&conf); + mbedtls_ctr_drbg_free(&ctr_drbg); + mbedtls_entropy_free(&entropy); + + // Unlock and key and certificates + if (certs.is_valid()) + certs->unlock(); + certs = Ref<X509Certificate>(); + if (pkey.is_valid()) + pkey->unlock(); + pkey = Ref<CryptoKeyMbedTLS>(); + inited = false; +} + +mbedtls_ssl_context *SSLContextMbedTLS::get_context() { + ERR_FAIL_COND_V(!inited, NULL); + return &ssl; +} + +SSLContextMbedTLS::SSLContextMbedTLS() { + inited = false; +} + +SSLContextMbedTLS::~SSLContextMbedTLS() { + clear(); +} diff --git a/modules/mbedtls/ssl_context_mbedtls.h b/modules/mbedtls/ssl_context_mbedtls.h new file mode 100644 index 0000000000..e49d532912 --- /dev/null +++ b/modules/mbedtls/ssl_context_mbedtls.h @@ -0,0 +1,73 @@ +/*************************************************************************/ +/* ssl_context_mbedtls.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef SSL_CONTEXT_MBED_TLS_H +#define SSL_CONTEXT_MBED_TLS_H + +#include "crypto_mbedtls.h" + +#include "core/os/file_access.h" +#include "core/pool_vector.h" +#include "core/reference.h" + +#include <mbedtls/config.h> +#include <mbedtls/ctr_drbg.h> +#include <mbedtls/debug.h> +#include <mbedtls/entropy.h> +#include <mbedtls/ssl.h> + +class SSLContextMbedTLS : public Reference { + +protected: + bool inited; + + static PoolByteArray _read_file(String p_path); + +public: + Ref<X509CertificateMbedTLS> certs; + mbedtls_entropy_context entropy; + mbedtls_ctr_drbg_context ctr_drbg; + mbedtls_ssl_context ssl; + mbedtls_ssl_config conf; + + Ref<CryptoKeyMbedTLS> pkey; + + Error _setup(int p_endpoint, int p_transport, int p_authmode); + Error init_server(int p_transport, int p_authmode, Ref<CryptoKeyMbedTLS> p_pkey, Ref<X509CertificateMbedTLS> p_cert); + Error init_client(int p_transport, int p_authmode, Ref<X509CertificateMbedTLS> p_valid_cas); + void clear(); + + mbedtls_ssl_context *get_context(); + + SSLContextMbedTLS(); + ~SSLContextMbedTLS(); +}; + +#endif // SSL_CONTEXT_MBED_TLS_H diff --git a/modules/mbedtls/stream_peer_mbed_tls.cpp b/modules/mbedtls/stream_peer_mbedtls.cpp index 4bb7557150..a2e342e219 100755 --- a/modules/mbedtls/stream_peer_mbed_tls.cpp +++ b/modules/mbedtls/stream_peer_mbedtls.cpp @@ -1,5 +1,5 @@ /*************************************************************************/ -/* stream_peer_mbed_tls.cpp */ +/* stream_peer_mbedtls.cpp */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ @@ -28,19 +28,11 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -#include "stream_peer_mbed_tls.h" +#include "stream_peer_mbedtls.h" #include "core/io/stream_peer_tcp.h" #include "core/os/file_access.h" -static void my_debug(void *ctx, int level, - const char *file, int line, - const char *str) { - - printf("%s:%04d: %s", file, line, str); - fflush(stdout); -} - void _print_error(int ret) { printf("mbedtls error: returned -0x%x\n\n", -ret); fflush(stdout); @@ -86,18 +78,14 @@ int StreamPeerMbedTLS::bio_recv(void *ctx, unsigned char *buf, size_t len) { void StreamPeerMbedTLS::_cleanup() { - mbedtls_ssl_free(&ssl); - mbedtls_ssl_config_free(&conf); - mbedtls_ctr_drbg_free(&ctr_drbg); - mbedtls_entropy_free(&entropy); - + ssl_ctx->clear(); base = Ref<StreamPeer>(); status = STATUS_DISCONNECTED; } Error StreamPeerMbedTLS::_do_handshake() { int ret = 0; - while ((ret = mbedtls_ssl_handshake(&ssl)) != 0) { + while ((ret = mbedtls_ssl_handshake(ssl_ctx->get_context())) != 0) { if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) { // An error occurred. ERR_PRINTS("TLS handshake error: " + itos(ret)); @@ -118,7 +106,7 @@ Error StreamPeerMbedTLS::_do_handshake() { return OK; } -Error StreamPeerMbedTLS::connect_to_stream(Ref<StreamPeer> p_base, bool p_validate_certs, const String &p_for_hostname) { +Error StreamPeerMbedTLS::connect_to_stream(Ref<StreamPeer> p_base, bool p_validate_certs, const String &p_for_hostname, Ref<X509Certificate> p_ca_certs) { ERR_FAIL_COND_V(p_base.is_null(), ERR_INVALID_PARAMETER); @@ -126,31 +114,11 @@ Error StreamPeerMbedTLS::connect_to_stream(Ref<StreamPeer> p_base, bool p_valida int ret = 0; int authmode = p_validate_certs ? MBEDTLS_SSL_VERIFY_REQUIRED : MBEDTLS_SSL_VERIFY_NONE; - mbedtls_ssl_init(&ssl); - mbedtls_ssl_config_init(&conf); - mbedtls_ctr_drbg_init(&ctr_drbg); - mbedtls_entropy_init(&entropy); - - ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0); - if (ret != 0) { - ERR_PRINTS(" failed\n ! mbedtls_ctr_drbg_seed returned an error" + itos(ret)); - _cleanup(); - return FAILED; - } + Error err = ssl_ctx->init_client(MBEDTLS_SSL_TRANSPORT_STREAM, authmode, p_ca_certs); + ERR_FAIL_COND_V(err != OK, err); - mbedtls_ssl_config_defaults(&conf, - MBEDTLS_SSL_IS_CLIENT, - MBEDTLS_SSL_TRANSPORT_STREAM, - MBEDTLS_SSL_PRESET_DEFAULT); - - mbedtls_ssl_conf_authmode(&conf, authmode); - mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL); - mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg); - mbedtls_ssl_conf_dbg(&conf, my_debug, stdout); - mbedtls_ssl_setup(&ssl, &conf); - mbedtls_ssl_set_hostname(&ssl, p_for_hostname.utf8().get_data()); - - mbedtls_ssl_set_bio(&ssl, this, bio_send, bio_recv, NULL); + mbedtls_ssl_set_hostname(ssl_ctx->get_context(), p_for_hostname.utf8().get_data()); + mbedtls_ssl_set_bio(ssl_ctx->get_context(), this, bio_send, bio_recv, NULL); status = STATUS_HANDSHAKING; @@ -162,11 +130,26 @@ Error StreamPeerMbedTLS::connect_to_stream(Ref<StreamPeer> p_base, bool p_valida return OK; } -Error StreamPeerMbedTLS::accept_stream(Ref<StreamPeer> p_base) { +Error StreamPeerMbedTLS::accept_stream(Ref<StreamPeer> p_base, Ref<CryptoKey> p_key, Ref<X509Certificate> p_cert, Ref<X509Certificate> p_ca_chain) { + + ERR_FAIL_COND_V(p_base.is_null(), ERR_INVALID_PARAMETER); + + Error err = ssl_ctx->init_server(MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_VERIFY_NONE, p_key, p_cert); + ERR_FAIL_COND_V(err != OK, err); + + base = p_base; + + mbedtls_ssl_set_bio(ssl_ctx->get_context(), this, bio_send, bio_recv, NULL); + + status = STATUS_HANDSHAKING; + + if ((err = _do_handshake()) != OK) { + return FAILED; + } + status = STATUS_CONNECTED; return OK; } - Error StreamPeerMbedTLS::put_data(const uint8_t *p_data, int p_bytes) { ERR_FAIL_COND_V(status != STATUS_CONNECTED, ERR_UNCONFIGURED); @@ -197,7 +180,7 @@ Error StreamPeerMbedTLS::put_partial_data(const uint8_t *p_data, int p_bytes, in if (p_bytes == 0) return OK; - int ret = mbedtls_ssl_write(&ssl, p_data, p_bytes); + int ret = mbedtls_ssl_write(ssl_ctx->get_context(), p_data, p_bytes); if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) { // Non blocking IO ret = 0; @@ -243,7 +226,7 @@ Error StreamPeerMbedTLS::get_partial_data(uint8_t *p_buffer, int p_bytes, int &r r_received = 0; - int ret = mbedtls_ssl_read(&ssl, p_buffer, p_bytes); + int ret = mbedtls_ssl_read(ssl_ctx->get_context(), p_buffer, p_bytes); if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) { ret = 0; // non blocking io } else if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) { @@ -273,7 +256,7 @@ void StreamPeerMbedTLS::poll() { // We could pass NULL as second parameter, but some behaviour sanitizers doesn't seem to like that. // Passing a 1 byte buffer to workaround it. uint8_t byte; - int ret = mbedtls_ssl_read(&ssl, &byte, 0); + int ret = mbedtls_ssl_read(ssl_ctx->get_context(), &byte, 0); if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) { // Nothing to read/write (non blocking IO) @@ -298,10 +281,11 @@ int StreamPeerMbedTLS::get_available_bytes() const { ERR_FAIL_COND_V(status != STATUS_CONNECTED, 0); - return mbedtls_ssl_get_bytes_avail(&ssl); + return mbedtls_ssl_get_bytes_avail(&(ssl_ctx->ssl)); } StreamPeerMbedTLS::StreamPeerMbedTLS() { + ssl_ctx.instance(); status = STATUS_DISCONNECTED; } @@ -317,7 +301,7 @@ void StreamPeerMbedTLS::disconnect_from_stream() { Ref<StreamPeerTCP> tcp = base; if (tcp.is_valid() && tcp->get_status() == StreamPeerTCP::STATUS_CONNECTED) { // We are still connected on the socket, try to send close notify. - mbedtls_ssl_close_notify(&ssl); + mbedtls_ssl_close_notify(ssl_ctx->get_context()); } _cleanup(); @@ -333,28 +317,9 @@ StreamPeerSSL *StreamPeerMbedTLS::_create_func() { return memnew(StreamPeerMbedTLS); } -mbedtls_x509_crt StreamPeerMbedTLS::cacert; - -void StreamPeerMbedTLS::_load_certs(const PoolByteArray &p_array) { - int arr_len = p_array.size(); - PoolByteArray::Read r = p_array.read(); - int err = mbedtls_x509_crt_parse(&cacert, &r[0], arr_len); - if (err != 0) { - WARN_PRINTS("Error parsing some certificates: " + itos(err)); - } -} - void StreamPeerMbedTLS::initialize_ssl() { _create = _create_func; - load_certs_func = _load_certs; - - mbedtls_x509_crt_init(&cacert); - -#ifdef DEBUG_ENABLED - mbedtls_debug_set_threshold(1); -#endif - available = true; } @@ -362,6 +327,4 @@ void StreamPeerMbedTLS::finalize_ssl() { available = false; _create = NULL; - load_certs_func = NULL; - mbedtls_x509_crt_free(&cacert); } diff --git a/modules/mbedtls/stream_peer_mbed_tls.h b/modules/mbedtls/stream_peer_mbedtls.h index ab87b779c1..eec7eab631 100755 --- a/modules/mbedtls/stream_peer_mbed_tls.h +++ b/modules/mbedtls/stream_peer_mbedtls.h @@ -1,5 +1,5 @@ /*************************************************************************/ -/* stream_peer_mbed_tls.h */ +/* stream_peer_mbedtls.h */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ @@ -32,15 +32,7 @@ #define STREAM_PEER_OPEN_SSL_H #include "core/io/stream_peer_ssl.h" - -#include <mbedtls/config.h> -#include <mbedtls/ctr_drbg.h> -#include <mbedtls/debug.h> -#include <mbedtls/entropy.h> -#include <mbedtls/ssl.h> - -#include <stdio.h> -#include <stdlib.h> +#include "ssl_context_mbedtls.h" class StreamPeerMbedTLS : public StreamPeerSSL { private: @@ -50,19 +42,13 @@ private: Ref<StreamPeer> base; static StreamPeerSSL *_create_func(); - static void _load_certs(const PoolByteArray &p_array); static int bio_recv(void *ctx, unsigned char *buf, size_t len); static int bio_send(void *ctx, const unsigned char *buf, size_t len); void _cleanup(); protected: - static mbedtls_x509_crt cacert; - - mbedtls_entropy_context entropy; - mbedtls_ctr_drbg_context ctr_drbg; - mbedtls_ssl_context ssl; - mbedtls_ssl_config conf; + Ref<SSLContextMbedTLS> ssl_ctx; static void _bind_methods(); @@ -70,8 +56,8 @@ protected: public: virtual void poll(); - virtual Error accept_stream(Ref<StreamPeer> p_base); - virtual Error connect_to_stream(Ref<StreamPeer> p_base, bool p_validate_certs = false, const String &p_for_hostname = String()); + virtual Error accept_stream(Ref<StreamPeer> p_base, Ref<CryptoKey> p_key, Ref<X509Certificate> p_cert, Ref<X509Certificate> p_ca_chain = Ref<X509Certificate>()); + virtual Error connect_to_stream(Ref<StreamPeer> p_base, bool p_validate_certs = false, const String &p_for_hostname = String(), Ref<X509Certificate> p_valid_cert = Ref<X509Certificate>()); virtual Status get_status() const; virtual void disconnect_from_stream(); diff --git a/modules/mono/SCsub b/modules/mono/SCsub index cc60e64a11..a9afa7ccf6 100644 --- a/modules/mono/SCsub +++ b/modules/mono/SCsub @@ -8,13 +8,7 @@ Import('env_modules') env_mono = env_modules.Clone() -env_mono.add_source_files(env.modules_sources, '*.cpp') -env_mono.add_source_files(env.modules_sources, 'glue/*.cpp') -env_mono.add_source_files(env.modules_sources, 'mono_gd/*.cpp') -env_mono.add_source_files(env.modules_sources, 'utils/*.cpp') - if env['tools']: - env_mono.add_source_files(env.modules_sources, 'editor/*.cpp') # NOTE: It is safe to generate this file here, since this is still executed serially import build_scripts.make_cs_compressed_header as make_cs_compressed_header make_cs_compressed_header.generate_header( @@ -62,3 +56,13 @@ if env_mono['tools']: # GodotTools.ProjectEditor which doesn't depend on the Godot API solution and # is required by the bindings generator in order to be able to generated it. godot_tools_build.build_project_editor_only(env_mono) + +# Add sources + +env_mono.add_source_files(env.modules_sources, '*.cpp') +env_mono.add_source_files(env.modules_sources, 'glue/*.cpp') +env_mono.add_source_files(env.modules_sources, 'mono_gd/*.cpp') +env_mono.add_source_files(env.modules_sources, 'utils/*.cpp') + +if env['tools']: + env_mono.add_source_files(env.modules_sources, 'editor/*.cpp') diff --git a/modules/mono/build_scripts/make_android_mono_config.py b/modules/mono/build_scripts/make_android_mono_config.py index cd9210897d..8cad204d7b 100644 --- a/modules/mono/build_scripts/make_android_mono_config.py +++ b/modules/mono/build_scripts/make_android_mono_config.py @@ -3,23 +3,6 @@ def generate_compressed_config(config_src, output_dir): import os.path from compat import byte_to_str - # Header file - with open(os.path.join(output_dir, 'android_mono_config.gen.h'), 'w') as header: - header.write('''/* THIS FILE IS GENERATED DO NOT EDIT */ -#ifndef ANDROID_MONO_CONFIG_GEN_H -#define ANDROID_MONO_CONFIG_GEN_H - -#ifdef ANDROID_ENABLED - -#include "core/ustring.h" - -String get_godot_android_mono_config(); - -#endif // ANDROID_ENABLED - -#endif // ANDROID_MONO_CONFIG_GEN_H -''') - # Source file with open(os.path.join(output_dir, 'android_mono_config.gen.cpp'), 'w') as cpp: with open(config_src, 'rb') as f: @@ -36,7 +19,7 @@ String get_godot_android_mono_config(); bytes_seq_str += byte_to_str(buf[buf_idx]) cpp.write('''/* THIS FILE IS GENERATED DO NOT EDIT */ -#include "android_mono_config.gen.h" +#include "android_mono_config.h" #ifdef ANDROID_ENABLED diff --git a/modules/mono/class_db_api_json.cpp b/modules/mono/class_db_api_json.cpp index 4a6637434a..7580911a0a 100644 --- a/modules/mono/class_db_api_json.cpp +++ b/modules/mono/class_db_api_json.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ diff --git a/modules/mono/class_db_api_json.h b/modules/mono/class_db_api_json.h index ddfe2debea..9888ecfb55 100644 --- a/modules/mono/class_db_api_json.h +++ b/modules/mono/class_db_api_json.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ diff --git a/modules/mono/csharp_script.cpp b/modules/mono/csharp_script.cpp index 8c17bac3c9..4c9dd9c1a9 100644 --- a/modules/mono/csharp_script.cpp +++ b/modules/mono/csharp_script.cpp @@ -2661,7 +2661,7 @@ void CSharpScript::_get_property_list(List<PropertyInfo> *p_properties) const { void CSharpScript::_bind_methods() { - ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "new", &CSharpScript::_new, MethodInfo(Variant::OBJECT, "new")); + ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "new", &CSharpScript::_new, MethodInfo("new")); } Ref<CSharpScript> CSharpScript::create_for_managed_type(GDMonoClass *p_class, GDMonoClass *p_native) { diff --git a/modules/mono/editor/GodotTools/GodotTools.BuildLogger/GodotTools.BuildLogger.csproj b/modules/mono/editor/GodotTools/GodotTools.BuildLogger/GodotTools.BuildLogger.csproj index f3ac353c0f..dcfdd83831 100644 --- a/modules/mono/editor/GodotTools/GodotTools.BuildLogger/GodotTools.BuildLogger.csproj +++ b/modules/mono/editor/GodotTools/GodotTools.BuildLogger/GodotTools.BuildLogger.csproj @@ -11,6 +11,7 @@ <AssemblyName>GodotTools.BuildLogger</AssemblyName> <TargetFrameworkVersion>v4.5</TargetFrameworkVersion> <FileAlignment>512</FileAlignment> + <LangVersion>7</LangVersion> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PlatformTarget>AnyCPU</PlatformTarget> diff --git a/modules/mono/editor/GodotTools/GodotTools.Core/GodotTools.Core.csproj b/modules/mono/editor/GodotTools/GodotTools.Core/GodotTools.Core.csproj index f36b40f87c..24c7cb1573 100644 --- a/modules/mono/editor/GodotTools/GodotTools.Core/GodotTools.Core.csproj +++ b/modules/mono/editor/GodotTools/GodotTools.Core/GodotTools.Core.csproj @@ -8,6 +8,7 @@ <RootNamespace>GodotTools.Core</RootNamespace> <AssemblyName>GodotTools.Core</AssemblyName> <TargetFrameworkVersion>v4.5</TargetFrameworkVersion> + <LangVersion>7</LangVersion> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <DebugSymbols>true</DebugSymbols> diff --git a/modules/mono/editor/GodotTools/GodotTools.IdeConnection/GodotTools.IdeConnection.csproj b/modules/mono/editor/GodotTools/GodotTools.IdeConnection/GodotTools.IdeConnection.csproj index 84c08251ab..94e525715b 100644 --- a/modules/mono/editor/GodotTools/GodotTools.IdeConnection/GodotTools.IdeConnection.csproj +++ b/modules/mono/editor/GodotTools/GodotTools.IdeConnection/GodotTools.IdeConnection.csproj @@ -11,6 +11,7 @@ <AssemblyName>GodotTools.IdeConnection</AssemblyName> <TargetFrameworkVersion>v4.5</TargetFrameworkVersion> <FileAlignment>512</FileAlignment> + <LangVersion>7</LangVersion> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PlatformTarget>AnyCPU</PlatformTarget> diff --git a/modules/mono/editor/GodotTools/GodotTools.ProjectEditor/GodotTools.ProjectEditor.csproj b/modules/mono/editor/GodotTools/GodotTools.ProjectEditor/GodotTools.ProjectEditor.csproj index 08b8ba3946..c745fe321b 100644 --- a/modules/mono/editor/GodotTools/GodotTools.ProjectEditor/GodotTools.ProjectEditor.csproj +++ b/modules/mono/editor/GodotTools/GodotTools.ProjectEditor/GodotTools.ProjectEditor.csproj @@ -9,6 +9,7 @@ <AssemblyName>GodotTools.ProjectEditor</AssemblyName> <TargetFrameworkVersion>v4.5</TargetFrameworkVersion> <BaseIntermediateOutputPath>obj</BaseIntermediateOutputPath> + <LangVersion>7</LangVersion> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <DebugSymbols>true</DebugSymbols> diff --git a/modules/mono/editor/GodotTools/GodotTools/GodotSharpEditor.cs b/modules/mono/editor/GodotTools/GodotTools/GodotSharpEditor.cs index 099c7fcb56..7da7cff933 100644 --- a/modules/mono/editor/GodotTools/GodotTools/GodotSharpEditor.cs +++ b/modules/mono/editor/GodotTools/GodotTools/GodotSharpEditor.cs @@ -359,7 +359,7 @@ namespace GodotTools aboutLabel.Text = "C# support in Godot Engine is in late alpha stage and, while already usable, " + "it is not meant for use in production.\n\n" + - "Projects can be exported to Linux, macOS and Windows, but not yet to mobile or web platforms. " + + "Projects can be exported to Linux, macOS, Windows and Android, but not yet to iOS, HTML5 or UWP. " + "Bugs and usability issues will be addressed gradually over future releases, " + "potentially including compatibility breaking changes as new features are implemented for a better overall C# experience.\n\n" + "If you experience issues with this Mono build, please report them on Godot's issue tracker with details about your system, MSBuild version, IDE, etc.:\n\n" + diff --git a/modules/mono/editor/GodotTools/GodotTools/GodotTools.csproj b/modules/mono/editor/GodotTools/GodotTools/GodotTools.csproj index e2d576caef..3c57900873 100644 --- a/modules/mono/editor/GodotTools/GodotTools/GodotTools.csproj +++ b/modules/mono/editor/GodotTools/GodotTools/GodotTools.csproj @@ -10,6 +10,7 @@ <TargetFrameworkVersion>v4.5</TargetFrameworkVersion> <GodotSourceRootPath>$(SolutionDir)/../../../../</GodotSourceRootPath> <GodotApiConfiguration>Debug</GodotApiConfiguration> + <LangVersion>7</LangVersion> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <DebugSymbols>true</DebugSymbols> diff --git a/modules/mono/editor/GodotTools/GodotTools/Ides/GodotIdeServer.cs b/modules/mono/editor/GodotTools/GodotTools/Ides/GodotIdeServer.cs index d515254e65..309b917c71 100644 --- a/modules/mono/editor/GodotTools/GodotTools/Ides/GodotIdeServer.cs +++ b/modules/mono/editor/GodotTools/GodotTools/Ides/GodotIdeServer.cs @@ -9,6 +9,7 @@ using System.Threading.Tasks; using GodotTools.IdeConnection; using GodotTools.Internals; using GodotTools.Utils; +using Directory = System.IO.Directory; using File = System.IO.File; using Thread = System.Threading.Thread; @@ -33,6 +34,9 @@ namespace GodotTools.Ides this.launchIdeAction = launchIdeAction; + // Make sure the directory exists + Directory.CreateDirectory(projectMetadataDir); + // The Godot editor's file system thread can keep the file open for writing, so we are forced to allow write sharing... const FileShare metaFileShare = FileShare.ReadWrite; diff --git a/modules/mono/editor/GodotTools/GodotTools/Utils/NotifyAwaiter.cs b/modules/mono/editor/GodotTools/GodotTools/Utils/NotifyAwaiter.cs index a3490fa89f..700b786752 100644 --- a/modules/mono/editor/GodotTools/GodotTools/Utils/NotifyAwaiter.cs +++ b/modules/mono/editor/GodotTools/GodotTools/Utils/NotifyAwaiter.cs @@ -51,7 +51,7 @@ namespace GodotTools.Utils { continuation = null; exception = null; - result = default; + result = default(T); IsCompleted = false; return this; } diff --git a/modules/mono/editor/editor_internal_calls.cpp b/modules/mono/editor/editor_internal_calls.cpp index 7db1090e2a..cd1ca2a2c7 100644 --- a/modules/mono/editor/editor_internal_calls.cpp +++ b/modules/mono/editor/editor_internal_calls.cpp @@ -271,7 +271,7 @@ MonoString *godot_icall_Internal_SimplifyGodotPath(MonoString *p_path) { MonoBoolean godot_icall_Internal_IsOsxAppBundleInstalled(MonoString *p_bundle_id) { #ifdef OSX_ENABLED String bundle_id = GDMonoMarshal::mono_string_to_godot(p_bundle_id); - return (MonoBoolean)osx_is_app_bundle_installed; + return (MonoBoolean)osx_is_app_bundle_installed(bundle_id); #else (void)p_bundle_id; // UNUSED return (MonoBoolean) false; diff --git a/modules/mono/glue/Managed/Files/Vector2.cs b/modules/mono/glue/Managed/Files/Vector2.cs index b1c1dae3c2..0daa94057e 100644 --- a/modules/mono/glue/Managed/Files/Vector2.cs +++ b/modules/mono/glue/Managed/Files/Vector2.cs @@ -14,10 +14,19 @@ using real_t = System.Single; namespace Godot { + /// <summary> + /// 2-element structure that can be used to represent positions in 2D space or any other pair of numeric values. + /// </summary> [Serializable] [StructLayout(LayoutKind.Sequential)] public struct Vector2 : IEquatable<Vector2> { + public enum Axis + { + X = 0, + Y + } + public real_t x; public real_t y; @@ -202,6 +211,22 @@ namespace Godot return v; } + public Vector2 PosMod(real_t mod) + { + Vector2 v; + v.x = Mathf.PosMod(x, mod); + v.y = Mathf.PosMod(y, mod); + return v; + } + + public Vector2 PosMod(Vector2 modv) + { + Vector2 v; + v.x = Mathf.PosMod(x, modv.x); + v.y = Mathf.PosMod(y, modv.y); + return v; + } + public Vector2 Project(Vector2 onNormal) { return onNormal * (Dot(onNormal) / onNormal.LengthSquared()); @@ -236,6 +261,14 @@ namespace Godot y = v.y; } + public Vector2 Sign() + { + Vector2 v; + v.x = Mathf.Sign(x); + v.y = Mathf.Sign(y); + return v; + } + public Vector2 Slerp(Vector2 b, real_t t) { real_t theta = AngleTo(b); @@ -265,7 +298,7 @@ namespace Godot private static readonly Vector2 _up = new Vector2(0, -1); private static readonly Vector2 _down = new Vector2(0, 1); - private static readonly Vector2 _right = new Vector2(1, 0); + private static readonly Vector2 _right = new Vector2(1, 0); private static readonly Vector2 _left = new Vector2(-1, 0); public static Vector2 Zero { get { return _zero; } } @@ -346,6 +379,20 @@ namespace Godot return left; } + public static Vector2 operator %(Vector2 vec, real_t divisor) + { + vec.x %= divisor; + vec.y %= divisor; + return vec; + } + + public static Vector2 operator %(Vector2 vec, Vector2 divisorv) + { + vec.x %= divisorv.x; + vec.y %= divisorv.y; + return vec; + } + public static bool operator ==(Vector2 left, Vector2 right) { return left.Equals(right); diff --git a/modules/mono/glue/Managed/Files/Vector3.cs b/modules/mono/glue/Managed/Files/Vector3.cs index c2da7b8bb1..9076dbd3b0 100644 --- a/modules/mono/glue/Managed/Files/Vector3.cs +++ b/modules/mono/glue/Managed/Files/Vector3.cs @@ -14,6 +14,9 @@ using real_t = System.Single; namespace Godot { + /// <summary> + /// 3-element structure that can be used to represent positions in 3D space or any other pair of numeric values. + /// </summary> [Serializable] [StructLayout(LayoutKind.Sequential)] public struct Vector3 : IEquatable<Vector3> @@ -225,6 +228,24 @@ namespace Godot ); } + public Vector3 PosMod(real_t mod) + { + Vector3 v; + v.x = Mathf.PosMod(x, mod); + v.y = Mathf.PosMod(y, mod); + v.z = Mathf.PosMod(z, mod); + return v; + } + + public Vector3 PosMod(Vector3 modv) + { + Vector3 v; + v.x = Mathf.PosMod(x, modv.x); + v.y = Mathf.PosMod(y, modv.y); + v.z = Mathf.PosMod(z, modv.z); + return v; + } + public Vector3 Project(Vector3 onNormal) { return onNormal * (Dot(onNormal) / onNormal.LengthSquared()); @@ -264,6 +285,15 @@ namespace Godot z = v.z; } + public Vector3 Sign() + { + Vector3 v; + v.x = Mathf.Sign(x); + v.y = Mathf.Sign(y); + v.z = Mathf.Sign(z); + return v; + } + public Vector3 Slerp(Vector3 b, real_t t) { real_t theta = AngleTo(b); @@ -397,6 +427,22 @@ namespace Godot return left; } + public static Vector3 operator %(Vector3 vec, real_t divisor) + { + vec.x %= divisor; + vec.y %= divisor; + vec.z %= divisor; + return vec; + } + + public static Vector3 operator %(Vector3 vec, Vector3 divisorv) + { + vec.x %= divisorv.x; + vec.y %= divisorv.y; + vec.z %= divisorv.z; + return vec; + } + public static bool operator ==(Vector3 left, Vector3 right) { return left.Equals(right); diff --git a/modules/mono/glue/Managed/Managed.csproj b/modules/mono/glue/Managed/Managed.csproj index ad55fe9539..c8eca71199 100644 --- a/modules/mono/glue/Managed/Managed.csproj +++ b/modules/mono/glue/Managed/Managed.csproj @@ -8,6 +8,7 @@ <RootNamespace>Managed</RootNamespace> <AssemblyName>Managed</AssemblyName> <TargetFrameworkVersion>v4.5</TargetFrameworkVersion> + <LangVersion>7</LangVersion> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' "> <DebugSymbols>true</DebugSymbols> diff --git a/modules/mono/mono_gd/android_mono_config.h b/modules/mono/mono_gd/android_mono_config.h new file mode 100644 index 0000000000..c5cc244aec --- /dev/null +++ b/modules/mono/mono_gd/android_mono_config.h @@ -0,0 +1,43 @@ +/*************************************************************************/ +/* android_mono_config.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef ANDROID_MONO_CONFIG_H +#define ANDROID_MONO_CONFIG_H + +#ifdef ANDROID_ENABLED + +#include "core/ustring.h" + +// This function is defined in an auto-generated source file +String get_godot_android_mono_config(); + +#endif // ANDROID_ENABLED + +#endif // ANDROID_MONO_CONFIG_H diff --git a/modules/mono/mono_gd/gd_mono.cpp b/modules/mono/mono_gd/gd_mono.cpp index eed8812305..aa69803a58 100644 --- a/modules/mono/mono_gd/gd_mono.cpp +++ b/modules/mono/mono_gd/gd_mono.cpp @@ -56,7 +56,7 @@ #endif #ifdef ANDROID_ENABLED -#include "android_mono_config.gen.h" +#include "android_mono_config.h" #endif GDMono *GDMono::singleton = NULL; diff --git a/modules/webp/image_loader_webp.cpp b/modules/webp/image_loader_webp.cpp index 8986e49cf0..d1bfa20842 100644 --- a/modules/webp/image_loader_webp.cpp +++ b/modules/webp/image_loader_webp.cpp @@ -106,8 +106,7 @@ static Ref<Image> _webp_lossy_unpack(const PoolVector<uint8_t> &p_buffer) { errdec = WebPDecodeRGBInto(&r[4], size, dst_w.ptr(), datasize, 3 * features.width) == NULL; } - //ERR_EXPLAIN("Error decoding webp! - "+p_file); - ERR_FAIL_COND_V(errdec, Ref<Image>()); + ERR_FAIL_COND_V_MSG(errdec, Ref<Image>(), "Failed decoding WebP image."); dst_w.release(); @@ -121,7 +120,6 @@ Error webp_load_image_from_buffer(Image *p_image, const uint8_t *p_buffer, int p WebPBitstreamFeatures features; if (WebPGetFeatures(p_buffer, p_buffer_len, &features) != VP8_STATUS_OK) { - // ERR_EXPLAIN("Error decoding WEBP image"); ERR_FAIL_V(ERR_FILE_CORRUPT); } @@ -138,8 +136,7 @@ Error webp_load_image_from_buffer(Image *p_image, const uint8_t *p_buffer, int p } dst_w.release(); - //ERR_EXPLAIN("Error decoding webp!"); - ERR_FAIL_COND_V(errdec, ERR_FILE_CORRUPT); + ERR_FAIL_COND_V_MSG(errdec, ERR_FILE_CORRUPT, "Failed decoding WebP image."); p_image->create(features.width, features.height, 0, features.has_alpha ? Image::FORMAT_RGBA8 : Image::FORMAT_RGB8, dst_image); diff --git a/modules/webrtc/register_types.cpp b/modules/webrtc/register_types.cpp index 58b68d926b..6f97842064 100644 --- a/modules/webrtc/register_types.cpp +++ b/modules/webrtc/register_types.cpp @@ -29,6 +29,7 @@ /*************************************************************************/ #include "register_types.h" +#include "core/project_settings.h" #include "webrtc_data_channel.h" #include "webrtc_peer_connection.h" @@ -43,6 +44,12 @@ #include "webrtc_multiplayer.h" void register_webrtc_types() { +#define _SET_HINT(NAME, _VAL_, _MAX_) \ + GLOBAL_DEF(NAME, _VAL_); \ + ProjectSettings::get_singleton()->set_custom_property_info(NAME, PropertyInfo(Variant::INT, NAME, PROPERTY_HINT_RANGE, "2," #_MAX_ ",1,or_greater")); + + _SET_HINT(WRTC_IN_BUF, 64, 4096); + #ifdef JAVASCRIPT_ENABLED WebRTCPeerConnectionJS::make_default(); #elif defined(WEBRTC_GDNATIVE_ENABLED) diff --git a/modules/webrtc/webrtc_data_channel.cpp b/modules/webrtc/webrtc_data_channel.cpp index 2bd30e68f5..7b3843410a 100644 --- a/modules/webrtc/webrtc_data_channel.cpp +++ b/modules/webrtc/webrtc_data_channel.cpp @@ -29,6 +29,7 @@ /*************************************************************************/ #include "webrtc_data_channel.h" +#include "core/project_settings.h" void WebRTCDataChannel::_bind_methods() { ClassDB::bind_method(D_METHOD("poll"), &WebRTCDataChannel::poll); @@ -58,6 +59,7 @@ void WebRTCDataChannel::_bind_methods() { } WebRTCDataChannel::WebRTCDataChannel() { + _in_buffer_shift = nearest_shift((int)GLOBAL_GET(WRTC_IN_BUF) - 1) + 10; } WebRTCDataChannel::~WebRTCDataChannel() { diff --git a/modules/webrtc/webrtc_data_channel.h b/modules/webrtc/webrtc_data_channel.h index 0b161da784..7e2c08d9d7 100644 --- a/modules/webrtc/webrtc_data_channel.h +++ b/modules/webrtc/webrtc_data_channel.h @@ -33,6 +33,8 @@ #include "core/io/packet_peer.h" +#define WRTC_IN_BUF "network/limits/webrtc/max_channel_in_buffer_kb" + class WebRTCDataChannel : public PacketPeer { GDCLASS(WebRTCDataChannel, PacketPeer); @@ -50,6 +52,8 @@ public: }; protected: + unsigned int _in_buffer_shift; + static void _bind_methods(); public: diff --git a/modules/webrtc/webrtc_data_channel_js.cpp b/modules/webrtc/webrtc_data_channel_js.cpp index 996db35cba..2edd212a50 100644 --- a/modules/webrtc/webrtc_data_channel_js.cpp +++ b/modules/webrtc/webrtc_data_channel_js.cpp @@ -56,7 +56,7 @@ EMSCRIPTEN_KEEPALIVE void _emrtc_on_ch_message(void *obj, uint8_t *p_data, uint3 } void WebRTCDataChannelJS::_on_open() { - in_buffer.resize(16); + in_buffer.resize(_in_buffer_shift); } void WebRTCDataChannelJS::_on_close() { diff --git a/modules/websocket/websocket_multiplayer_peer.cpp b/modules/websocket/websocket_multiplayer_peer.cpp index 62f09bfbf9..dd86c758bf 100644 --- a/modules/websocket/websocket_multiplayer_peer.cpp +++ b/modules/websocket/websocket_multiplayer_peer.cpp @@ -265,7 +265,10 @@ Error WebSocketMultiplayerPeer::_server_relay(int32_t p_from, int32_t p_to, cons ERR_FAIL_COND_V(p_to == p_from, FAILED); - return get_peer(p_to)->put_packet(p_buffer, p_buffer_size); // Sending to specific peer + Ref<WebSocketPeer> peer_to = get_peer(p_to); + ERR_FAIL_COND_V(peer_to.is_null(), FAILED); + + return peer_to->put_packet(p_buffer, p_buffer_size); // Sending to specific peer } } @@ -296,8 +299,6 @@ void WebSocketMultiplayerPeer::_process_multiplayer(Ref<WebSocketPeer> p_peer, u ERR_FAIL_COND(type != SYS_NONE); // Only server sends sys messages ERR_FAIL_COND(from != p_peer_id); // Someone is cheating - _server_relay(from, to, in_buffer, size); // Relay if needed - if (to == 1) { // This is for the server _store_pkt(from, to, in_buffer, data_size); @@ -312,13 +313,9 @@ void WebSocketMultiplayerPeer::_process_multiplayer(Ref<WebSocketPeer> p_peer, u // All but one, for us if not excluded if (_peer_id != -(int32_t)p_peer_id) _store_pkt(from, to, in_buffer, data_size); - - } else { - - // Send to specific peer - ERR_FAIL_COND(!_peer_map.has(to)); - get_peer(to)->put_packet(in_buffer, size); } + // Relay if needed (i.e. "to" includes a peer that is not the server) + _server_relay(from, to, in_buffer, size); } else { diff --git a/modules/websocket/wsl_client.cpp b/modules/websocket/wsl_client.cpp index cf2ad55b4a..0006a057e0 100644 --- a/modules/websocket/wsl_client.cpp +++ b/modules/websocket/wsl_client.cpp @@ -121,17 +121,17 @@ bool WSLClient::_verify_headers(String &r_protocol) { headers[name] = value; } -#define _WLS_EXPLAIN(NAME, VALUE) \ - ERR_EXPLAIN("Missing or invalid header '" + String(NAME) + "'. Expected value '" + VALUE + "'"); -#define _WLS_CHECK(NAME, VALUE) \ - _WLS_EXPLAIN(NAME, VALUE); \ - ERR_FAIL_COND_V(!headers.has(NAME) || headers[NAME].to_lower() != VALUE, false); -#define _WLS_CHECK_NC(NAME, VALUE) \ - _WLS_EXPLAIN(NAME, VALUE); \ - ERR_FAIL_COND_V(!headers.has(NAME) || headers[NAME] != VALUE, false); - _WLS_CHECK("connection", "upgrade"); - _WLS_CHECK("upgrade", "websocket"); - _WLS_CHECK_NC("sec-websocket-accept", WSLPeer::compute_key_response(_key)); +#define _WSL_CHECK(NAME, VALUE) \ + ERR_FAIL_COND_V_MSG(!headers.has(NAME) || headers[NAME].to_lower() != VALUE, false, \ + "Missing or invalid header '" + String(NAME) + "'. Expected value '" + VALUE + "'."); +#define _WSL_CHECK_NC(NAME, VALUE) \ + ERR_FAIL_COND_V_MSG(!headers.has(NAME) || headers[NAME] != VALUE, false, \ + "Missing or invalid header '" + String(NAME) + "'. Expected value '" + VALUE + "'."); + _WSL_CHECK("connection", "upgrade"); + _WSL_CHECK("upgrade", "websocket"); + _WSL_CHECK_NC("sec-websocket-accept", WSLPeer::compute_key_response(_key)); +#undef _WSL_CHECK_NC +#undef _WSL_CHECK if (_protocols.size() == 0) { // We didn't request a custom protocol ERR_FAIL_COND_V(headers.has("sec-websocket-protocol"), false); @@ -148,10 +148,6 @@ bool WSLClient::_verify_headers(String &r_protocol) { if (!valid) return false; } -#undef _WLS_CHECK_NC -#undef _WLS_CHECK -#undef _WLS_EXPLAIN - return true; } diff --git a/modules/websocket/wsl_peer.cpp b/modules/websocket/wsl_peer.cpp index b11bd2b70f..74fb901232 100644 --- a/modules/websocket/wsl_peer.cpp +++ b/modules/websocket/wsl_peer.cpp @@ -1,5 +1,5 @@ /*************************************************************************/ -/* lws_peer.cpp */ +/* wsl_peer.cpp */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ @@ -35,7 +35,7 @@ #include "wsl_client.h" #include "wsl_server.h" -#include "core/math/crypto_core.h" +#include "core/crypto/crypto_core.h" #include "core/math/random_number_generator.h" #include "core/os/os.h" diff --git a/modules/websocket/wsl_server.cpp b/modules/websocket/wsl_server.cpp index 1feae420b9..efb526eed1 100644 --- a/modules/websocket/wsl_server.cpp +++ b/modules/websocket/wsl_server.cpp @@ -1,5 +1,5 @@ /*************************************************************************/ -/* lws_server.cpp */ +/* wsl_server.cpp */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ @@ -64,18 +64,17 @@ bool WSLServer::PendingPeer::_parse_request(const PoolStringArray p_protocols) { else headers[name] = value; } -#define _WLS_CHECK(NAME, VALUE) \ - ERR_EXPLAIN("Missing or invalid header '" + String(NAME) + "'. Expected value '" + VALUE + "'"); \ - ERR_FAIL_COND_V(!headers.has(NAME) || headers[NAME].to_lower() != VALUE, false); -#define _WLS_CHECK_EX(NAME) \ - ERR_EXPLAIN("Missing header '" + String(NAME) + "'."); \ - ERR_FAIL_COND_V(!headers.has(NAME), false); - _WLS_CHECK("upgrade", "websocket"); - _WLS_CHECK("sec-websocket-version", "13"); - _WLS_CHECK_EX("sec-websocket-key"); - _WLS_CHECK_EX("connection"); -#undef _WLS_CHECK_EX -#undef _WLS_CHECK +#define _WSL_CHECK(NAME, VALUE) \ + ERR_FAIL_COND_V_MSG(!headers.has(NAME) || headers[NAME].to_lower() != VALUE, false, \ + "Missing or invalid header '" + String(NAME) + "'. Expected value '" + VALUE + "'."); +#define _WSL_CHECK_EX(NAME) \ + ERR_FAIL_COND_V_MSG(!headers.has(NAME), false, "Missing header '" + String(NAME) + "'."); + _WSL_CHECK("upgrade", "websocket"); + _WSL_CHECK("sec-websocket-version", "13"); + _WSL_CHECK_EX("sec-websocket-key"); + _WSL_CHECK_EX("connection"); +#undef _WSL_CHECK_EX +#undef _WSL_CHECK key = headers["sec-websocket-key"]; if (headers.has("sec-websocket-protocol")) { Vector<String> protos = headers["sec-websocket-protocol"].split(","); diff --git a/modules/xatlas_unwrap/register_types.cpp b/modules/xatlas_unwrap/register_types.cpp index c18aa04336..04911301ff 100644 --- a/modules/xatlas_unwrap/register_types.cpp +++ b/modules/xatlas_unwrap/register_types.cpp @@ -29,11 +29,14 @@ /*************************************************************************/ #include "register_types.h" + #include "core/error_macros.h" + #include "thirdparty/xatlas/xatlas.h" #include <stdio.h> #include <stdlib.h> + extern bool (*array_mesh_lightmap_unwrap_callback)(float p_texel_size, const float *p_vertices, const float *p_normals, int p_vertex_count, const int *p_indices, const int *p_face_materials, int p_index_count, float **r_uv, int **r_vertex, int *r_vertex_count, int **r_index, int *r_index_count, int *r_size_hint_x, int *r_size_hint_y); bool xatlas_mesh_lightmap_unwrap_callback(float p_texel_size, const float *p_vertices, const float *p_normals, int p_vertex_count, const int *p_indices, const int *p_face_materials, int p_index_count, float **r_uv, int **r_vertex, int *r_vertex_count, int **r_index, int *r_index_count, int *r_size_hint_x, int *r_size_hint_y) { |