Merge pull request #73033 from akien-mga/gdscript-fix-gd4-renames-build

Clean up ProjectConverter3To4 architecture, move renames map to separate file
This commit is contained in:
Rémi Verschelde 2023-02-11 22:05:38 +01:00
commit cb2e73d578
No known key found for this signature in database
GPG key ID: C3336907360768E1
7 changed files with 2070 additions and 1994 deletions

File diff suppressed because it is too large Load diff

View file

@ -33,31 +33,36 @@
#ifndef DISABLE_DEPRECATED
#include "core/io/file_access.h"
#include "core/object/ref_counted.h"
#include "modules/modules_enabled.gen.h" // For regex.
#ifndef MODULE_REGEX_ENABLED
#include "core/error/error_macros.h"
class ProjectConverter3To4 {
public:
ProjectConverter3To4(int, int) {}
bool validate_conversion() {
ERR_FAIL_V_MSG(false, "Can't validate conversion for Godot 3.x projects, because RegEx module is disabled.");
}
bool convert() {
ERR_FAIL_V_MSG(false, "Can't run converter for Godot 3.x projects, because RegEx module is disabled.");
}
};
#else // Has regex.
#include "core/string/ustring.h"
#include "core/templates/local_vector.h"
#include "core/templates/vector.h"
class RegEx;
class ProjectConverter3To4 {
public:
class RegExContainer;
static const char *enum_renames[][2];
static const char *gdscript_function_renames[][2];
static const char *csharp_function_renames[][2];
static const char *gdscript_properties_renames[][2];
static const char *csharp_properties_renames[][2];
static const char *gdscript_signals_renames[][2];
static const char *csharp_signals_renames[][2];
static const char *project_settings_renames[][2];
static const char *input_map_renames[][2];
static const char *builtin_types_renames[][2];
static const char *shaders_renames[][2];
static const char *class_renames[][2];
static const char *color_renames[][2];
private:
uint64_t maximum_file_size;
uint64_t maximum_line_length;
@ -108,10 +113,12 @@ private:
public:
ProjectConverter3To4(int, int);
int validate_conversion();
int convert();
bool validate_conversion();
bool convert();
};
#endif // MODULE_REGEX_ENABLED
#endif // DISABLE_DEPRECATED
#endif // PROJECT_CONVERTER_3_TO_4_H

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,54 @@
/**************************************************************************/
/* renames_map_3_to_4.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* 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 RENAMES_MAP_3_TO_4_H
#define RENAMES_MAP_3_TO_4_H
#ifndef DISABLE_DEPRECATED
struct RenamesMap3To4 {
static const char *enum_renames[][2];
static const char *gdscript_function_renames[][2];
static const char *csharp_function_renames[][2];
static const char *gdscript_properties_renames[][2];
static const char *csharp_properties_renames[][2];
static const char *gdscript_signals_renames[][2];
static const char *csharp_signals_renames[][2];
static const char *project_settings_renames[][2];
static const char *input_map_renames[][2];
static const char *builtin_types_renames[][2];
static const char *shaders_renames[][2];
static const char *class_renames[][2];
static const char *color_renames[][2];
};
#endif // DISABLE_DEPRECATED
#endif // RENAMES_MAP_3_TO_4_H

View file

@ -94,13 +94,17 @@
#include "editor/editor_settings.h"
#include "editor/editor_translation.h"
#include "editor/progress_dialog.h"
#include "editor/project_converter_3_to_4.h"
#include "editor/project_manager.h"
#include "editor/register_editor_types.h"
#ifndef NO_EDITOR_SPLASH
#include "main/splash_editor.gen.h"
#endif
#endif
#ifndef DISABLE_DEPRECATED
#include "editor/project_converter_3_to_4.h"
#endif // DISABLE_DEPRECATED
#endif // TOOLS_ENABLED
#include "modules/modules_enabled.gen.h" // For mono.
@ -164,8 +168,10 @@ static OS::ProcessID editor_pid = 0;
static bool found_project = false;
static bool auto_build_solutions = false;
static String debug_server_uri;
#ifndef DISABLE_DEPRECATED
static int converter_max_kb_file = 4 * 1024; // 4MB
static int converter_max_line_length = 100000;
#endif // DISABLE_DEPRECATED
HashMap<Main::CLIScope, Vector<String>> forwardable_cli_arguments;
#endif
@ -437,10 +443,12 @@ void Main::print_help(const char *p_binary) {
OS::get_singleton()->print(" The target directory must exist.\n");
OS::get_singleton()->print(" --export-debug <preset> <path> Export the project in debug mode using the given preset and output path. See --export-release description for other considerations.\n");
OS::get_singleton()->print(" --export-pack <preset> <path> Export the project data only using the given preset and output path. The <path> extension determines whether it will be in PCK or ZIP format.\n");
#ifndef DISABLE_DEPRECATED
OS::get_singleton()->print(" --convert-3to4 [<max_file_kb>] [<max_line_size>]\n");
OS::get_singleton()->print(" Converts project from Godot 3.x to Godot 4.x.\n");
OS::get_singleton()->print(" --validate-conversion-3to4 [<max_file_kb>] [<max_line_size>]\n");
OS::get_singleton()->print(" Shows what elements will be renamed when converting project from Godot 3.x to Godot 4.x.\n");
#endif // DISABLE_DEPRECATED
OS::get_singleton()->print(" --doctool [<path>] Dump the engine API reference to the given <path> (defaults to current dir) in XML format, merging if existing files are found.\n");
OS::get_singleton()->print(" --no-docbase Disallow dumping the base types (used with --doctool).\n");
OS::get_singleton()->print(" --build-solutions Build the scripting solutions (e.g. for C# projects). Implies --editor and requires a valid project to edit.\n");
@ -1122,6 +1130,7 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph
editor = true;
cmdline_tool = true;
main_args.push_back(I->get());
#ifndef DISABLE_DEPRECATED
} else if (I->get() == "--convert-3to4") {
// Actually handling is done in start().
cmdline_tool = true;
@ -1152,6 +1161,7 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph
}
}
}
#endif // DISABLE_DEPRECATED
} else if (I->get() == "--doctool") {
// Actually handling is done in start().
cmdline_tool = true;
@ -1161,7 +1171,7 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph
audio_driver = NULL_AUDIO_DRIVER;
display_driver = NULL_DISPLAY_DRIVER;
main_args.push_back(I->get());
#endif
#endif // TOOLS_ENABLED
} else if (I->get() == "--path") { // set path of project to start or edit
if (I->next()) {
@ -2384,7 +2394,7 @@ bool Main::start() {
bool converting_project = false;
bool validating_converting_project = false;
#endif // DISABLE_DEPRECATED
#endif
#endif // TOOLS_ENABLED
main_timer_sync.init(OS::get_singleton()->get_ticks_usec());
List<String> args = OS::get_singleton()->get_cmdline_args();
@ -2409,7 +2419,7 @@ bool Main::start() {
editor = true;
} else if (args[i] == "-p" || args[i] == "--project-manager") {
project_manager = true;
#endif
#endif // TOOLS_ENABLED
} else if (args[i].length() && args[i][0] != '-' && positional_arg.is_empty()) {
positional_arg = args[i];
@ -2567,18 +2577,22 @@ bool Main::start() {
#ifndef DISABLE_DEPRECATED
if (converting_project) {
int exit_code = ProjectConverter3To4(converter_max_kb_file, converter_max_line_length).convert();
OS::get_singleton()->set_exit_code(exit_code);
int ret = ProjectConverter3To4(converter_max_kb_file, converter_max_line_length).convert();
if (ret) {
OS::get_singleton()->set_exit_code(EXIT_SUCCESS);
}
return false;
}
if (validating_converting_project) {
int exit_code = ProjectConverter3To4(converter_max_kb_file, converter_max_line_length).validate_conversion();
OS::get_singleton()->set_exit_code(exit_code);
bool ret = ProjectConverter3To4(converter_max_kb_file, converter_max_line_length).validate_conversion();
if (ret) {
OS::get_singleton()->set_exit_code(EXIT_SUCCESS);
}
return false;
}
#endif // DISABLE_DEPRECATED
#endif
#endif // TOOLS_ENABLED
if (script.is_empty() && game_path.is_empty() && String(GLOBAL_GET("application/run/main_scene")) != "") {
game_path = GLOBAL_GET("application/run/main_scene");

View file

@ -42,6 +42,11 @@
#include "gdscript_utility_functions.h"
#include "scene/resources/packed_scene.h"
#if defined(TOOLS_ENABLED) && !defined(DISABLE_DEPRECATED)
#define SUGGEST_GODOT4_RENAMES
#include "editor/renames_map_3_to_4.h"
#endif
#define UNNAMED_ENUM "<anonymous enum>"
#define ENUM_SEPARATOR "::"
@ -2601,9 +2606,8 @@ void GDScriptAnalyzer::reduce_binary_op(GDScriptParser::BinaryOpNode *p_binary_o
p_binary_op->set_datatype(result);
}
#ifdef TOOLS_ENABLED
#ifndef DISABLE_DEPRECATED
const char *GDScriptAnalyzer::get_rename_from_map(const char *map[][2], String key) {
#ifdef SUGGEST_GODOT4_RENAMES
const char *get_rename_from_map(const char *map[][2], String key) {
for (int index = 0; map[index][0]; index++) {
if (map[index][0] == key) {
return map[index][1];
@ -2614,39 +2618,39 @@ const char *GDScriptAnalyzer::get_rename_from_map(const char *map[][2], String k
// Checks if an identifier/function name has been renamed in Godot 4, uses ProjectConverter3To4 for rename map.
// Returns the new name if found, nullptr otherwise.
const char *GDScriptAnalyzer::check_for_renamed_identifier(String identifier, GDScriptParser::Node::Type type) {
const char *check_for_renamed_identifier(String identifier, GDScriptParser::Node::Type type) {
switch (type) {
case GDScriptParser::Node::IDENTIFIER: {
// Check properties
const char *result = get_rename_from_map(ProjectConverter3To4::gdscript_properties_renames, identifier);
const char *result = get_rename_from_map(RenamesMap3To4::gdscript_properties_renames, identifier);
if (result) {
return result;
}
// Check enum values
result = get_rename_from_map(ProjectConverter3To4::enum_renames, identifier);
result = get_rename_from_map(RenamesMap3To4::enum_renames, identifier);
if (result) {
return result;
}
// Check color constants
result = get_rename_from_map(ProjectConverter3To4::color_renames, identifier);
result = get_rename_from_map(RenamesMap3To4::color_renames, identifier);
if (result) {
return result;
}
// Check type names
result = get_rename_from_map(ProjectConverter3To4::class_renames, identifier);
result = get_rename_from_map(RenamesMap3To4::class_renames, identifier);
if (result) {
return result;
}
return get_rename_from_map(ProjectConverter3To4::builtin_types_renames, identifier);
return get_rename_from_map(RenamesMap3To4::builtin_types_renames, identifier);
}
case GDScriptParser::Node::CALL: {
const char *result = get_rename_from_map(ProjectConverter3To4::gdscript_function_renames, identifier);
const char *result = get_rename_from_map(RenamesMap3To4::gdscript_function_renames, identifier);
if (result) {
return result;
}
// Built-in Types are mistaken for function calls when the built-in type is not found.
// Check built-in types if function rename not found
return get_rename_from_map(ProjectConverter3To4::builtin_types_renames, identifier);
return get_rename_from_map(RenamesMap3To4::builtin_types_renames, identifier);
}
// Signal references don't get parsed through the GDScriptAnalyzer. No support for signal rename hints.
default:
@ -2654,8 +2658,7 @@ const char *GDScriptAnalyzer::check_for_renamed_identifier(String identifier, GD
return nullptr;
}
}
#endif // DISABLE_DEPRECATED
#endif // TOOLS_ENABLED
#endif // SUGGEST_GODOT4_RENAMES
void GDScriptAnalyzer::reduce_call(GDScriptParser::CallNode *p_call, bool p_is_await, bool p_is_root) {
bool all_is_constant = true;
@ -3078,8 +3081,7 @@ void GDScriptAnalyzer::reduce_call(GDScriptParser::CallNode *p_call, bool p_is_a
}
if (!found && (is_self || (base_type.is_hard_type() && base_type.kind == GDScriptParser::DataType::BUILTIN))) {
String base_name = is_self && !p_call->is_super ? "self" : base_type.to_string();
#ifdef TOOLS_ENABLED
#ifndef DISABLE_DEPRECATED
#ifdef SUGGEST_GODOT4_RENAMES
String rename_hint = String();
if (GLOBAL_GET(GDScriptWarning::get_settings_path_from_code(GDScriptWarning::Code::RENAMED_IN_GD4_HINT)).booleanize()) {
const char *renamed_function_name = check_for_renamed_identifier(p_call->function_name, p_call->type);
@ -3088,12 +3090,9 @@ void GDScriptAnalyzer::reduce_call(GDScriptParser::CallNode *p_call, bool p_is_a
}
}
push_error(vformat(R"*(Function "%s()" not found in base %s.%s)*", p_call->function_name, base_name, rename_hint), p_call->is_super ? p_call : p_call->callee);
#else // !DISABLE_DEPRECATED
push_error(vformat(R"*(Function "%s()" not found in base %s.)*", p_call->function_name, base_name), p_call->is_super ? p_call : p_call->callee);
#endif // DISABLE_DEPRECATED
#else
push_error(vformat(R"*(Function "%s()" not found in base %s.)*", p_call->function_name, base_name), p_call->is_super ? p_call : p_call->callee);
#endif
#endif // SUGGEST_GODOT4_RENAMES
} else if (!found && (!p_call->is_super && base_type.is_hard_type() && base_type.kind == GDScriptParser::DataType::NATIVE && base_type.is_meta_type)) {
push_error(vformat(R"*(Static function "%s()" not found in base "%s".)*", p_call->function_name, base_type.native_type), p_call);
}
@ -3283,8 +3282,7 @@ void GDScriptAnalyzer::reduce_identifier_from_base(GDScriptParser::IdentifierNod
p_identifier->reduced_value = result;
p_identifier->set_datatype(type_from_variant(result, p_identifier));
} else if (base.is_hard_type()) {
#ifdef TOOLS_ENABLED
#ifndef DISABLE_DEPRECATED
#ifdef SUGGEST_GODOT4_RENAMES
String rename_hint = String();
if (GLOBAL_GET(GDScriptWarning::get_settings_path_from_code(GDScriptWarning::Code::RENAMED_IN_GD4_HINT)).booleanize()) {
const char *renamed_identifier_name = check_for_renamed_identifier(name, p_identifier->type);
@ -3293,12 +3291,9 @@ void GDScriptAnalyzer::reduce_identifier_from_base(GDScriptParser::IdentifierNod
}
}
push_error(vformat(R"(Cannot find constant "%s" on base "%s".%s)", name, base.to_string(), rename_hint), p_identifier);
#else // !DISABLE_DEPRECATED
push_error(vformat(R"(Cannot find constant "%s" on base "%s".)", name, base.to_string()), p_identifier);
#endif // DISABLE_DEPRECATED
#else
push_error(vformat(R"(Cannot find constant "%s" on base "%s".)", name, base.to_string()), p_identifier);
#endif
#endif // SUGGEST_GODOT4_RENAMES
}
} else {
switch (base.builtin_type) {
@ -3327,8 +3322,7 @@ void GDScriptAnalyzer::reduce_identifier_from_base(GDScriptParser::IdentifierNod
}
}
if (base.is_hard_type()) {
#ifdef TOOLS_ENABLED
#ifndef DISABLE_DEPRECATED
#ifdef SUGGEST_GODOT4_RENAMES
String rename_hint = String();
if (GLOBAL_GET(GDScriptWarning::get_settings_path_from_code(GDScriptWarning::Code::RENAMED_IN_GD4_HINT)).booleanize()) {
const char *renamed_identifier_name = check_for_renamed_identifier(name, p_identifier->type);
@ -3337,12 +3331,9 @@ void GDScriptAnalyzer::reduce_identifier_from_base(GDScriptParser::IdentifierNod
}
}
push_error(vformat(R"(Cannot find property "%s" on base "%s".%s)", name, base.to_string(), rename_hint), p_identifier);
#else // !DISABLE_DEPRECATED
push_error(vformat(R"(Cannot find property "%s" on base "%s".)", name, base.to_string()), p_identifier);
#endif // DISABLE_DEPRECATED
#else
push_error(vformat(R"(Cannot find property "%s" on base "%s".)", name, base.to_string()), p_identifier);
#endif
#endif // SUGGEST_GODOT4_RENAMES
}
}
}
@ -3682,8 +3673,7 @@ void GDScriptAnalyzer::reduce_identifier(GDScriptParser::IdentifierNode *p_ident
if (GDScriptUtilityFunctions::function_exists(name)) {
push_error(vformat(R"(Built-in function "%s" cannot be used as an identifier.)", name), p_identifier);
} else {
#ifdef TOOLS_ENABLED
#ifndef DISABLE_DEPRECATED
#ifdef SUGGEST_GODOT4_RENAMES
String rename_hint = String();
if (GLOBAL_GET(GDScriptWarning::get_settings_path_from_code(GDScriptWarning::Code::RENAMED_IN_GD4_HINT)).booleanize()) {
const char *renamed_identifier_name = check_for_renamed_identifier(name, p_identifier->type);
@ -3692,12 +3682,9 @@ void GDScriptAnalyzer::reduce_identifier(GDScriptParser::IdentifierNode *p_ident
}
}
push_error(vformat(R"(Identifier "%s" not declared in the current scope.%s)", name, rename_hint), p_identifier);
#else // !DISABLE_DEPRECATED
push_error(vformat(R"(Identifier "%s" not declared in the current scope.)", name), p_identifier);
#endif // DISABLE_DEPRECATED
#else
push_error(vformat(R"(Identifier "%s" not declared in the current scope.)", name), p_identifier);
#endif
#endif // SUGGEST_GODOT4_RENAMES
}
GDScriptParser::DataType dummy;
dummy.kind = GDScriptParser::DataType::VARIANT;

View file

@ -37,10 +37,6 @@
#include "gdscript_cache.h"
#include "gdscript_parser.h"
#ifdef TOOLS_ENABLED
#include "editor/project_converter_3_to_4.h"
#endif
class GDScriptAnalyzer {
GDScriptParser *parser = nullptr;
HashMap<String, Ref<GDScriptParserRef>> depended_parsers;
@ -137,13 +133,6 @@ class GDScriptAnalyzer {
bool is_shadowing(GDScriptParser::IdentifierNode *p_local, const String &p_context);
#endif
#ifdef TOOLS_ENABLED
#ifndef DISABLE_DEPRECATED
const char *get_rename_from_map(const char *map[][2], String key);
const char *check_for_renamed_identifier(String identifier, GDScriptParser::Node::Type type);
#endif // DISABLE_DEPRECATED
#endif // TOOLS_ENABLED
public:
Error resolve_inheritance();
Error resolve_interface();