summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.github/workflows/linux_builds.yml61
-rw-r--r--doc/classes/DynamicFont.xml2
-rw-r--r--editor/editor_properties.cpp33
-rw-r--r--editor/plugins/sprite_frames_editor_plugin.cpp186
-rw-r--r--editor/plugins/sprite_frames_editor_plugin.h25
-rw-r--r--editor/rename_dialog.cpp29
-rw-r--r--main/main.cpp88
-rw-r--r--main/main.h6
-rw-r--r--modules/mobile_vr/register_types.cpp8
-rw-r--r--platform/linuxbsd/os_linuxbsd.cpp4
-rw-r--r--platform/linuxbsd/os_linuxbsd.h2
-rw-r--r--platform/osx/detect.py1
-rw-r--r--platform/osx/display_server_osx.mm6
-rw-r--r--platform/osx/os_osx.h2
-rw-r--r--platform/osx/os_osx.mm4
-rw-r--r--platform/windows/display_server_windows.cpp8
-rw-r--r--scene/register_scene_types.cpp12
-rw-r--r--scene/resources/resource_format_text.cpp6
-rw-r--r--tests/test_class_db.cpp882
-rw-r--r--tests/test_class_db.h798
-rw-r--r--tests/test_validate_testing.h4
21 files changed, 1217 insertions, 950 deletions
diff --git a/.github/workflows/linux_builds.yml b/.github/workflows/linux_builds.yml
index 85ae6a5a8f..087b2d15de 100644
--- a/.github/workflows/linux_builds.yml
+++ b/.github/workflows/linux_builds.yml
@@ -68,6 +68,67 @@ jobs:
run: |
./bin/godot.linuxbsd.opt.tools.64.mono --test
+ linux-editor-sanitizers:
+ runs-on: "ubuntu-20.04"
+ name: Editor with sanitizers (target=debug, tools=yes, tests=yes, use_asan=yes, use_ubsan=yes)
+
+ steps:
+ - uses: actions/checkout@v2
+
+ # Azure repositories are not reliable, we need to prevent azure giving us packages.
+ - name: Make apt sources.list use the default Ubuntu repositories
+ run: |
+ sudo rm -f /etc/apt/sources.list.d/*
+ sudo cp -f misc/ci/sources.list /etc/apt/sources.list
+ sudo apt-get update
+
+ # Install all packages (except scons)
+ - name: Configure dependencies
+ run: |
+ sudo apt-get install build-essential pkg-config libx11-dev libxcursor-dev \
+ libxinerama-dev libgl1-mesa-dev libglu-dev libasound2-dev libpulse-dev libudev-dev libxi-dev libxrandr-dev yasm
+
+ # Upload cache on completion and check it out now
+ - name: Load .scons_cache directory
+ id: linux-editor-cache
+ uses: actions/cache@v2
+ with:
+ path: ${{github.workspace}}/.scons_cache/
+ key: ${{github.job}}-${{env.GODOT_BASE_BRANCH}}-${{github.ref}}-${{github.sha}}
+ restore-keys: |
+ ${{github.job}}-${{env.GODOT_BASE_BRANCH}}-${{github.ref}}-${{github.sha}}
+ ${{github.job}}-${{env.GODOT_BASE_BRANCH}}-${{github.ref}}
+ ${{github.job}}-${{env.GODOT_BASE_BRANCH}}
+
+ # Use python 3.x release (works cross platform; best to keep self contained in it's own step)
+ - name: Set up Python 3.x
+ uses: actions/setup-python@v2
+ with:
+ # Semantic version range syntax or exact version of a Python version
+ python-version: '3.x'
+ # Optional - x64 or x86 architecture, defaults to x64
+ architecture: 'x64'
+
+ # Setup scons, print python version and scons version info, so if anything is broken it won't run the build.
+ - name: Configuring Python packages
+ run: |
+ python -c "import sys; print(sys.version)"
+ python -m pip install scons
+ python --version
+ scons --version
+
+ # We should always be explicit with our flags usage here since it's gonna be sure to always set those flags
+ - name: Compilation
+ env:
+ SCONS_CACHE: ${{github.workspace}}/.scons_cache/
+ run: |
+ scons -j2 verbose=yes warnings=all werror=yes platform=linuxbsd tools=yes tests=yes target=debug use_asan=yes use_ubsan=yes
+
+ # Execute unit tests for the editor
+ - name: Unit Tests
+ run: |
+ ./bin/godot.linuxbsd.tools.64s --test
+
linux-template:
runs-on: "ubuntu-20.04"
name: Template w/ Mono (target=release, tools=no)
diff --git a/doc/classes/DynamicFont.xml b/doc/classes/DynamicFont.xml
index 24df056c6b..5d8ae29175 100644
--- a/doc/classes/DynamicFont.xml
+++ b/doc/classes/DynamicFont.xml
@@ -12,7 +12,7 @@
dynamic_font.size = 64
$"Label".set("custom_fonts/font", dynamic_font)
[/codeblock]
- [b]Note:[/b] DynamicFont doesn't support features such as right-to-left typesetting, ligatures, text shaping, variable fonts and optional font features yet. If you wish to "bake" an optional font feature into a TTF font file, you can use [url=https://fontforge.org/]FontForge[/url] to do so. In FontForge, use [b]File > Generate Fonts[/b], click [b]Options[/b], choose the desired features then generate the font.
+ [b]Note:[/b] DynamicFont doesn't support features such as kerning, right-to-left typesetting, ligatures, text shaping, variable fonts and optional font features yet. If you wish to "bake" an optional font feature into a TTF font file, you can use [url=https://fontforge.org/]FontForge[/url] to do so. In FontForge, use [b]File > Generate Fonts[/b], click [b]Options[/b], choose the desired features then generate the font.
</description>
<tutorials>
</tutorials>
diff --git a/editor/editor_properties.cpp b/editor/editor_properties.cpp
index dea76ac997..34d553b5f9 100644
--- a/editor/editor_properties.cpp
+++ b/editor/editor_properties.cpp
@@ -946,14 +946,11 @@ void EditorPropertyEasing::_drag_easing(const Ref<InputEvent> &p_ev) {
}
float val = get_edited_object()->get(get_edited_property());
- if (val == 0) {
- return;
- }
bool sg = val < 0;
val = Math::absf(val);
val = Math::log(val) / Math::log((float)2.0);
- //logspace
+ // Logarithmic space.
val += rel * 0.05;
val = Math::pow(2.0f, val);
@@ -961,6 +958,16 @@ void EditorPropertyEasing::_drag_easing(const Ref<InputEvent> &p_ev) {
val = -val;
}
+ // 0 is a singularity, but both positive and negative values
+ // are otherwise allowed. Enforce 0+ as workaround.
+ if (Math::is_zero_approx(val)) {
+ val = 0.00001;
+ }
+
+ // Limit to a reasonable value to prevent the curve going into infinity,
+ // which can cause crashes and other issues.
+ val = CLAMP(val, -1'000'000, 1'000'000);
+
emit_changed(get_edited_property(), val);
easing_draw->update();
}
@@ -1003,7 +1010,18 @@ void EditorPropertyEasing::_draw_easing() {
}
easing_draw->draw_multiline(lines, line_color, 1.0);
- f->draw(ci, Point2(10, 10 + f->get_ascent()), String::num(exp, 2), font_color);
+ // Draw more decimals for small numbers since higher precision is usually required for fine adjustments.
+ int decimals;
+ if (Math::abs(exp) < 0.1 - CMP_EPSILON) {
+ decimals = 4;
+ } else if (Math::abs(exp) < 1 - CMP_EPSILON) {
+ decimals = 3;
+ } else if (Math::abs(exp) < 10 - CMP_EPSILON) {
+ decimals = 2;
+ } else {
+ decimals = 1;
+ }
+ f->draw(ci, Point2(10, 10 + f->get_ascent()), rtos(exp).pad_decimals(decimals), font_color);
}
void EditorPropertyEasing::update_property() {
@@ -1035,6 +1053,11 @@ void EditorPropertyEasing::_spin_value_changed(double p_value) {
if (Math::is_zero_approx(p_value)) {
p_value = 0.00001;
}
+
+ // Limit to a reasonable value to prevent the curve going into infinity,
+ // which can cause crashes and other issues.
+ p_value = CLAMP(p_value, -1'000'000, 1'000'000);
+
emit_changed(get_edited_property(), p_value);
_spin_focus_exited();
}
diff --git a/editor/plugins/sprite_frames_editor_plugin.cpp b/editor/plugins/sprite_frames_editor_plugin.cpp
index 1073da7d8c..18942b371c 100644
--- a/editor/plugins/sprite_frames_editor_plugin.cpp
+++ b/editor/plugins/sprite_frames_editor_plugin.cpp
@@ -36,6 +36,8 @@
#include "editor/editor_settings.h"
#include "scene/3d/sprite_3d.h"
#include "scene/gui/center_container.h"
+#include "scene/gui/margin_container.h"
+#include "scene/gui/panel_container.h"
void SpriteFramesEditor::_gui_input(Ref<InputEvent> p_event) {
}
@@ -140,8 +142,27 @@ void SpriteFramesEditor::_sheet_preview_input(const Ref<InputEvent> &p_event) {
}
}
+void SpriteFramesEditor::_sheet_scroll_input(const Ref<InputEvent> &p_event) {
+ const Ref<InputEventMouseButton> mb = p_event;
+
+ if (mb.is_valid()) {
+ // Zoom in/out using Ctrl + mouse wheel. This is done on the ScrollContainer
+ // to allow performing this action anywhere, even if the cursor isn't
+ // hovering the texture in the workspace.
+ if (mb->get_button_index() == BUTTON_WHEEL_UP && mb->is_pressed() && mb->get_control()) {
+ _sheet_zoom_in();
+ // Don't scroll up after zooming in.
+ accept_event();
+ } else if (mb->get_button_index() == BUTTON_WHEEL_DOWN && mb->is_pressed() && mb->get_control()) {
+ _sheet_zoom_out();
+ // Don't scroll down after zooming out.
+ accept_event();
+ }
+ }
+}
+
void SpriteFramesEditor::_sheet_add_frames() {
- Size2i size = split_sheet_preview->get_size();
+ Size2i size = split_sheet_preview->get_texture()->get_size();
int h = split_sheet_h->get_value();
int v = split_sheet_v->get_value();
@@ -180,6 +201,28 @@ void SpriteFramesEditor::_sheet_add_frames() {
undo_redo->commit_action();
}
+void SpriteFramesEditor::_sheet_zoom_in() {
+ if (sheet_zoom < max_sheet_zoom) {
+ sheet_zoom *= scale_ratio;
+ Size2 texture_size = split_sheet_preview->get_texture()->get_size();
+ split_sheet_preview->set_custom_minimum_size(texture_size * sheet_zoom);
+ }
+}
+
+void SpriteFramesEditor::_sheet_zoom_out() {
+ if (sheet_zoom > min_sheet_zoom) {
+ sheet_zoom /= scale_ratio;
+ Size2 texture_size = split_sheet_preview->get_texture()->get_size();
+ split_sheet_preview->set_custom_minimum_size(texture_size * sheet_zoom);
+ }
+}
+
+void SpriteFramesEditor::_sheet_zoom_reset() {
+ sheet_zoom = 1.f;
+ Size2 texture_size = split_sheet_preview->get_texture()->get_size();
+ split_sheet_preview->set_custom_minimum_size(texture_size * sheet_zoom);
+}
+
void SpriteFramesEditor::_sheet_select_clear_all_frames() {
bool should_clear = true;
for (int i = 0; i < split_sheet_h->get_value() * split_sheet_v->get_value(); i++) {
@@ -207,15 +250,18 @@ void SpriteFramesEditor::_prepare_sprite_sheet(const String &p_file) {
EditorNode::get_singleton()->show_warning(TTR("Unable to load images"));
ERR_FAIL_COND(!texture.is_valid());
}
- if (texture != split_sheet_preview->get_texture()) {
- //different texture, reset to 4x4
- split_sheet_h->set_value(4);
- split_sheet_v->set_value(4);
- }
+ bool new_texture = texture != split_sheet_preview->get_texture();
frames_selected.clear();
last_frame_selected = -1;
split_sheet_preview->set_texture(texture);
+ if (new_texture) {
+ //different texture, reset to 4x4
+ split_sheet_h->set_value(4);
+ split_sheet_v->set_value(4);
+ //reset zoom
+ _sheet_zoom_reset();
+ }
split_sheet_dialog->popup_centered_ratio(0.65);
}
@@ -231,8 +277,14 @@ void SpriteFramesEditor::_notification(int p_what) {
move_up->set_icon(get_theme_icon("MoveLeft", "EditorIcons"));
move_down->set_icon(get_theme_icon("MoveRight", "EditorIcons"));
_delete->set_icon(get_theme_icon("Remove", "EditorIcons"));
+ zoom_out->set_icon(get_theme_icon("ZoomLess", "EditorIcons"));
+ zoom_1->set_icon(get_theme_icon("ZoomReset", "EditorIcons"));
+ zoom_in->set_icon(get_theme_icon("ZoomMore", "EditorIcons"));
new_anim->set_icon(get_theme_icon("New", "EditorIcons"));
remove_anim->set_icon(get_theme_icon("Remove", "EditorIcons"));
+ split_sheet_zoom_out->set_icon(get_theme_icon("ZoomLess", "EditorIcons"));
+ split_sheet_zoom_1->set_icon(get_theme_icon("ZoomReset", "EditorIcons"));
+ split_sheet_zoom_in->set_icon(get_theme_icon("ZoomMore", "EditorIcons"));
[[fallthrough]];
}
case NOTIFICATION_THEME_CHANGED: {
@@ -636,6 +688,54 @@ void SpriteFramesEditor::_animation_fps_changed(double p_value) {
undo_redo->commit_action();
}
+void SpriteFramesEditor::_tree_input(const Ref<InputEvent> &p_event) {
+ const Ref<InputEventMouseButton> mb = p_event;
+
+ if (mb.is_valid()) {
+ if (mb->get_button_index() == BUTTON_WHEEL_UP && mb->is_pressed() && mb->get_control()) {
+ _zoom_in();
+ // Don't scroll up after zooming in.
+ accept_event();
+ } else if (mb->get_button_index() == BUTTON_WHEEL_DOWN && mb->is_pressed() && mb->get_control()) {
+ _zoom_out();
+ // Don't scroll down after zooming out.
+ accept_event();
+ }
+ }
+}
+
+void SpriteFramesEditor::_zoom_in() {
+ // Do not zoom in or out with no visible frames
+ if (frames->get_frame_count(edited_anim) <= 0) {
+ return;
+ }
+ if (thumbnail_zoom < max_thumbnail_zoom) {
+ thumbnail_zoom *= scale_ratio;
+ int thumbnail_size = (int)(thumbnail_default_size * thumbnail_zoom);
+ tree->set_fixed_column_width(thumbnail_size * 3 / 2);
+ tree->set_fixed_icon_size(Size2(thumbnail_size, thumbnail_size));
+ }
+}
+
+void SpriteFramesEditor::_zoom_out() {
+ // Do not zoom in or out with no visible frames
+ if (frames->get_frame_count(edited_anim) <= 0) {
+ return;
+ }
+ if (thumbnail_zoom > min_thumbnail_zoom) {
+ thumbnail_zoom /= scale_ratio;
+ int thumbnail_size = (int)(thumbnail_default_size * thumbnail_zoom);
+ tree->set_fixed_column_width(thumbnail_size * 3 / 2);
+ tree->set_fixed_icon_size(Size2(thumbnail_size, thumbnail_size));
+ }
+}
+
+void SpriteFramesEditor::_zoom_reset() {
+ thumbnail_zoom = 1.0f;
+ tree->set_fixed_column_width(thumbnail_default_size * 3 / 2);
+ tree->set_fixed_icon_size(Size2(thumbnail_default_size, thumbnail_default_size));
+}
+
void SpriteFramesEditor::_update_library(bool p_skip_selector) {
updating = true;
@@ -727,6 +827,9 @@ void SpriteFramesEditor::edit(SpriteFrames *p_frames) {
}
_update_library();
+ // Clear zoom and split sheet texture
+ split_sheet_preview->set_texture(Ref<Texture2D>());
+ _zoom_reset();
} else {
hide();
}
@@ -965,6 +1068,24 @@ SpriteFramesEditor::SpriteFramesEditor() {
_delete->set_tooltip(TTR("Delete"));
hbc->add_child(_delete);
+ hbc->add_spacer();
+
+ zoom_out = memnew(Button);
+ zoom_out->connect("pressed", callable_mp(this, &SpriteFramesEditor::_zoom_out));
+ zoom_out->set_flat(true);
+ zoom_out->set_tooltip(TTR("Zoom Out"));
+ hbc->add_child(zoom_out);
+ zoom_1 = memnew(Button);
+ zoom_1->connect("pressed", callable_mp(this, &SpriteFramesEditor::_zoom_reset));
+ zoom_1->set_flat(true);
+ zoom_1->set_tooltip(TTR("Zoom Reset"));
+ hbc->add_child(zoom_1);
+ zoom_in = memnew(Button);
+ zoom_in->connect("pressed", callable_mp(this, &SpriteFramesEditor::_zoom_in));
+ zoom_in->set_flat(true);
+ zoom_in->set_tooltip(TTR("Zoom In"));
+ hbc->add_child(zoom_in);
+
file = memnew(EditorFileDialog);
add_child(file);
@@ -972,13 +1093,11 @@ SpriteFramesEditor::SpriteFramesEditor() {
tree->set_v_size_flags(SIZE_EXPAND_FILL);
tree->set_icon_mode(ItemList::ICON_MODE_TOP);
- int thumbnail_size = 96;
tree->set_max_columns(0);
tree->set_icon_mode(ItemList::ICON_MODE_TOP);
- tree->set_fixed_column_width(thumbnail_size * 3 / 2);
tree->set_max_text_lines(2);
- tree->set_fixed_icon_size(Size2(thumbnail_size, thumbnail_size));
tree->set_drag_forwarding(this);
+ tree->connect("gui_input", callable_mp(this, &SpriteFramesEditor::_tree_input));
sub_vb->add_child(tree);
@@ -1042,8 +1161,13 @@ SpriteFramesEditor::SpriteFramesEditor() {
split_sheet_vb->add_child(split_sheet_hb);
+ PanelContainer *split_sheet_panel = memnew(PanelContainer);
+ split_sheet_panel->set_h_size_flags(SIZE_EXPAND_FILL);
+ split_sheet_panel->set_v_size_flags(SIZE_EXPAND_FILL);
+ split_sheet_vb->add_child(split_sheet_panel);
+
split_sheet_preview = memnew(TextureRect);
- split_sheet_preview->set_expand(false);
+ split_sheet_preview->set_expand(true);
split_sheet_preview->set_mouse_filter(MOUSE_FILTER_PASS);
split_sheet_preview->connect("draw", callable_mp(this, &SpriteFramesEditor::_sheet_preview_draw));
split_sheet_preview->connect("gui_input", callable_mp(this, &SpriteFramesEditor::_sheet_preview_input));
@@ -1051,20 +1175,58 @@ SpriteFramesEditor::SpriteFramesEditor() {
splite_sheet_scroll = memnew(ScrollContainer);
splite_sheet_scroll->set_enable_h_scroll(true);
splite_sheet_scroll->set_enable_v_scroll(true);
- splite_sheet_scroll->set_v_size_flags(SIZE_EXPAND_FILL);
+ splite_sheet_scroll->connect("gui_input", callable_mp(this, &SpriteFramesEditor::_sheet_scroll_input));
+ split_sheet_panel->add_child(splite_sheet_scroll);
CenterContainer *cc = memnew(CenterContainer);
cc->add_child(split_sheet_preview);
cc->set_h_size_flags(SIZE_EXPAND_FILL);
cc->set_v_size_flags(SIZE_EXPAND_FILL);
splite_sheet_scroll->add_child(cc);
- split_sheet_vb->add_child(splite_sheet_scroll);
+ MarginContainer *split_sheet_zoom_margin = memnew(MarginContainer);
+ split_sheet_panel->add_child(split_sheet_zoom_margin);
+ split_sheet_zoom_margin->set_h_size_flags(0);
+ split_sheet_zoom_margin->set_v_size_flags(0);
+ split_sheet_zoom_margin->add_theme_constant_override("margin_top", 5);
+ split_sheet_zoom_margin->add_theme_constant_override("margin_left", 5);
+ HBoxContainer *split_sheet_zoom_hb = memnew(HBoxContainer);
+ split_sheet_zoom_margin->add_child(split_sheet_zoom_hb);
+
+ split_sheet_zoom_out = memnew(Button);
+ split_sheet_zoom_out->set_flat(true);
+ split_sheet_zoom_out->set_focus_mode(FOCUS_NONE);
+ split_sheet_zoom_out->set_tooltip(TTR("Zoom Out"));
+ split_sheet_zoom_out->connect("pressed", callable_mp(this, &SpriteFramesEditor::_sheet_zoom_out));
+ split_sheet_zoom_hb->add_child(split_sheet_zoom_out);
+ split_sheet_zoom_1 = memnew(Button);
+ split_sheet_zoom_1->set_flat(true);
+ split_sheet_zoom_1->set_focus_mode(FOCUS_NONE);
+ split_sheet_zoom_1->set_tooltip(TTR("Zoom Reset"));
+ split_sheet_zoom_1->connect("pressed", callable_mp(this, &SpriteFramesEditor::_sheet_zoom_reset));
+ split_sheet_zoom_hb->add_child(split_sheet_zoom_1);
+ split_sheet_zoom_in = memnew(Button);
+ split_sheet_zoom_in->set_flat(true);
+ split_sheet_zoom_in->set_focus_mode(FOCUS_NONE);
+ split_sheet_zoom_in->set_tooltip(TTR("Zoom In"));
+ split_sheet_zoom_in->connect("pressed", callable_mp(this, &SpriteFramesEditor::_sheet_zoom_in));
+ split_sheet_zoom_hb->add_child(split_sheet_zoom_in);
file_split_sheet = memnew(EditorFileDialog);
file_split_sheet->set_title(TTR("Create Frames from Sprite Sheet"));
file_split_sheet->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
add_child(file_split_sheet);
file_split_sheet->connect("file_selected", callable_mp(this, &SpriteFramesEditor::_prepare_sprite_sheet));
+
+ // Config scale.
+ scale_ratio = 1.2f;
+ thumbnail_default_size = 96;
+ thumbnail_zoom = 1.0f;
+ max_thumbnail_zoom = 8.0f;
+ min_thumbnail_zoom = 0.1f;
+ sheet_zoom = 1.0f;
+ max_sheet_zoom = 16.0f;
+ min_sheet_zoom = 0.01f;
+ _zoom_reset();
}
void SpriteFramesEditorPlugin::edit(Object *p_object) {
diff --git a/editor/plugins/sprite_frames_editor_plugin.h b/editor/plugins/sprite_frames_editor_plugin.h
index ee743fe60d..0dce93f55a 100644
--- a/editor/plugins/sprite_frames_editor_plugin.h
+++ b/editor/plugins/sprite_frames_editor_plugin.h
@@ -36,6 +36,7 @@
#include "scene/2d/animated_sprite_2d.h"
#include "scene/gui/dialogs.h"
#include "scene/gui/file_dialog.h"
+#include "scene/gui/scroll_container.h"
#include "scene/gui/split_container.h"
#include "scene/gui/texture_rect.h"
#include "scene/gui/tree.h"
@@ -52,6 +53,9 @@ class SpriteFramesEditor : public HSplitContainer {
Button *empty2;
Button *move_up;
Button *move_down;
+ Button *zoom_out;
+ Button *zoom_1;
+ Button *zoom_in;
ItemList *tree;
bool loading_scene;
int sel;
@@ -79,10 +83,22 @@ class SpriteFramesEditor : public HSplitContainer {
TextureRect *split_sheet_preview;
SpinBox *split_sheet_h;
SpinBox *split_sheet_v;
+ Button *split_sheet_zoom_out;
+ Button *split_sheet_zoom_1;
+ Button *split_sheet_zoom_in;
EditorFileDialog *file_split_sheet;
Set<int> frames_selected;
int last_frame_selected;
+ float scale_ratio;
+ int thumbnail_default_size;
+ float thumbnail_zoom;
+ float max_thumbnail_zoom;
+ float min_thumbnail_zoom;
+ float sheet_zoom;
+ float max_sheet_zoom;
+ float min_sheet_zoom;
+
void _load_pressed();
void _load_scene_pressed();
void _file_load_request(const Vector<String> &p_path, int p_at_pos = -1);
@@ -103,6 +119,11 @@ class SpriteFramesEditor : public HSplitContainer {
void _animation_loop_changed();
void _animation_fps_changed(double p_value);
+ void _tree_input(const Ref<InputEvent> &p_event);
+ void _zoom_in();
+ void _zoom_out();
+ void _zoom_reset();
+
bool updating;
UndoRedo *undo_redo;
@@ -117,7 +138,11 @@ class SpriteFramesEditor : public HSplitContainer {
void _sheet_preview_draw();
void _sheet_spin_changed(double);
void _sheet_preview_input(const Ref<InputEvent> &p_event);
+ void _sheet_scroll_input(const Ref<InputEvent> &p_event);
void _sheet_add_frames();
+ void _sheet_zoom_in();
+ void _sheet_zoom_out();
+ void _sheet_zoom_reset();
void _sheet_select_clear_all_frames();
protected:
diff --git a/editor/rename_dialog.cpp b/editor/rename_dialog.cpp
index 211e365454..23990bca07 100644
--- a/editor/rename_dialog.cpp
+++ b/editor/rename_dialog.cpp
@@ -61,18 +61,16 @@ RenameDialog::RenameDialog(SceneTreeEditor *p_scene_tree_editor, UndoRedo *p_und
// ---- 1st & 2nd row
Label *lbl_search = memnew(Label);
- lbl_search->set_text(TTR("Search"));
+ lbl_search->set_text(TTR("Search:"));
lne_search = memnew(LineEdit);
- lne_search->set_placeholder(TTR("Search"));
lne_search->set_name("lne_search");
lne_search->set_h_size_flags(Control::SIZE_EXPAND_FILL);
Label *lbl_replace = memnew(Label);
- lbl_replace->set_text(TTR("Replace"));
+ lbl_replace->set_text(TTR("Replace:"));
lne_replace = memnew(LineEdit);
- lne_replace->set_placeholder(TTR("Replace"));
lne_replace->set_name("lne_replace");
lne_replace->set_h_size_flags(Control::SIZE_EXPAND_FILL);
@@ -84,18 +82,16 @@ RenameDialog::RenameDialog(SceneTreeEditor *p_scene_tree_editor, UndoRedo *p_und
// ---- 3rd & 4th row
Label *lbl_prefix = memnew(Label);
- lbl_prefix->set_text(TTR("Prefix"));
+ lbl_prefix->set_text(TTR("Prefix:"));
lne_prefix = memnew(LineEdit);
- lne_prefix->set_placeholder(TTR("Prefix"));
lne_prefix->set_name("lne_prefix");
lne_prefix->set_h_size_flags(Control::SIZE_EXPAND_FILL);
Label *lbl_suffix = memnew(Label);
- lbl_suffix->set_text(TTR("Suffix"));
+ lbl_suffix->set_text(TTR("Suffix:"));
lne_suffix = memnew(LineEdit);
- lne_suffix->set_placeholder(TTR("Suffix"));
lne_suffix->set_name("lne_suffix");
lne_suffix->set_h_size_flags(Control::SIZE_EXPAND_FILL);
@@ -106,8 +102,6 @@ RenameDialog::RenameDialog(SceneTreeEditor *p_scene_tree_editor, UndoRedo *p_und
// -- Feature Tabs
- const int feature_min_height = 160 * EDSCALE;
-
cbut_regex = memnew(CheckButton);
cbut_regex->set_text(TTR("Use Regular Expressions"));
vbc->add_child(cbut_regex);
@@ -118,13 +112,13 @@ RenameDialog::RenameDialog(SceneTreeEditor *p_scene_tree_editor, UndoRedo *p_und
tabc_features = memnew(TabContainer);
tabc_features->set_tab_align(TabContainer::ALIGN_LEFT);
+ tabc_features->set_use_hidden_tabs_for_min_size(true);
vbc->add_child(tabc_features);
// ---- Tab Substitute
VBoxContainer *vbc_substitute = memnew(VBoxContainer);
vbc_substitute->set_h_size_flags(Control::SIZE_EXPAND_FILL);
- vbc_substitute->set_custom_minimum_size(Size2(0, feature_min_height));
vbc_substitute->set_name(TTR("Substitute"));
tabc_features->add_child(vbc_substitute);
@@ -199,7 +193,7 @@ RenameDialog::RenameDialog(SceneTreeEditor *p_scene_tree_editor, UndoRedo *p_und
chk_per_level_counter = memnew(CheckBox);
chk_per_level_counter->set_text(TTR("Per-level Counter"));
- chk_per_level_counter->set_tooltip(TTR("If set the counter restarts for each group of child nodes."));
+ chk_per_level_counter->set_tooltip(TTR("If set, the counter restarts for each group of child nodes."));
vbc_substitute->add_child(chk_per_level_counter);
HBoxContainer *hbc_count_options = memnew(HBoxContainer);
@@ -241,7 +235,6 @@ RenameDialog::RenameDialog(SceneTreeEditor *p_scene_tree_editor, UndoRedo *p_und
VBoxContainer *vbc_process = memnew(VBoxContainer);
vbc_process->set_h_size_flags(Control::SIZE_EXPAND_FILL);
vbc_process->set_name(TTR("Post-Process"));
- vbc_process->set_custom_minimum_size(Size2(0, feature_min_height));
tabc_features->add_child(vbc_process);
cbut_process = memnew(CheckBox);
@@ -285,18 +278,14 @@ RenameDialog::RenameDialog(SceneTreeEditor *p_scene_tree_editor, UndoRedo *p_und
vbc->add_child(sep_preview);
lbl_preview_title = memnew(Label);
- lbl_preview_title->set_text(TTR("Preview"));
vbc->add_child(lbl_preview_title);
lbl_preview = memnew(Label);
- lbl_preview->set_text("");
- lbl_preview->add_theme_color_override("font_color", EditorNode::get_singleton()->get_gui_base()->get_theme_color("error_color", "Editor"));
vbc->add_child(lbl_preview);
// ---- Dialog related
set_min_size(Size2(383, 0));
- //set_as_toplevel(true);
get_ok()->set_text(TTR("Rename"));
Button *but_reset = add_button(TTR("Reset"));
@@ -307,7 +296,7 @@ RenameDialog::RenameDialog(SceneTreeEditor *p_scene_tree_editor, UndoRedo *p_und
cbut_collapse_features->connect("toggled", callable_mp(this, &RenameDialog::_features_toggled));
- // Substitite Buttons
+ // Substitute Buttons
lne_search->connect("focus_entered", callable_mp(this, &RenameDialog::_update_substitute));
lne_search->connect("focus_exited", callable_mp(this, &RenameDialog::_update_substitute));
@@ -391,7 +380,7 @@ void RenameDialog::_update_preview(String new_text) {
String new_name = _apply_rename(preview_node, spn_count_start->get_value());
if (!has_errors) {
- lbl_preview_title->set_text(TTR("Preview"));
+ lbl_preview_title->set_text(TTR("Preview:"));
lbl_preview->set_text(new_name);
if (new_name == preview_node->get_name()) {
@@ -482,7 +471,7 @@ void RenameDialog::_error_handler(void *p_self, const char *p_func, const char *
}
self->has_errors = true;
- self->lbl_preview_title->set_text(TTR("Regular Expression Error"));
+ self->lbl_preview_title->set_text(TTR("Regular Expression Error:"));
self->lbl_preview->add_theme_color_override("font_color", EditorNode::get_singleton()->get_gui_base()->get_theme_color("error_color", "Editor"));
self->lbl_preview->set_text(vformat(TTR("At character %s"), err_str));
}
diff --git a/main/main.cpp b/main/main.cpp
index e371bef25f..e45162c0f3 100644
--- a/main/main.cpp
+++ b/main/main.cpp
@@ -369,16 +369,94 @@ void Main::print_help(const char *p_binary) {
#endif
}
+#ifdef TESTS_ENABLED
+// The order is the same as in `Main::setup()`, only core and some editor types
+// are initialized here. This also combines `Main::setup2()` initialization.
+Error Main::test_setup() {
+ OS::get_singleton()->initialize();
+
+ engine = memnew(Engine);
+
+ ClassDB::init();
+
+ register_core_types();
+ register_core_driver_types();
+
+ globals = memnew(ProjectSettings);
+
+ GLOBAL_DEF("debug/settings/crash_handler/message",
+ String("Please include this when reporting the bug on https://github.com/godotengine/godot/issues"));
+
+ // From `Main::setup2()`.
+ preregister_module_types();
+ preregister_server_types();
+
+ register_core_singletons();
+
+ register_server_types();
+ register_scene_types();
+
+#ifdef TOOLS_ENABLED
+ ClassDB::set_current_api(ClassDB::API_EDITOR);
+ EditorNode::register_editor_types();
+
+ ClassDB::set_current_api(ClassDB::API_CORE);
+#endif
+ register_platform_apis();
+
+ register_module_types();
+ register_driver_types();
+
+ ClassDB::set_current_api(ClassDB::API_NONE);
+
+ _start_success = true;
+
+ return OK;
+}
+// The order is the same as in `Main::cleanup()`.
+void Main::test_cleanup() {
+ ERR_FAIL_COND(!_start_success);
+
+ EngineDebugger::deinitialize();
+
+ ResourceLoader::remove_custom_loaders();
+ ResourceSaver::remove_custom_savers();
+
+#ifdef TOOLS_ENABLED
+ EditorNode::unregister_editor_types();
+#endif
+ unregister_driver_types();
+ unregister_module_types();
+ unregister_platform_apis();
+ unregister_scene_types();
+ unregister_server_types();
+
+ OS::get_singleton()->finalize();
+
+ if (globals) {
+ memdelete(globals);
+ }
+ if (engine) {
+ memdelete(engine);
+ }
+
+ unregister_core_driver_types();
+ unregister_core_types();
+
+ OS::get_singleton()->finalize_core();
+}
+#endif
+
int Main::test_entrypoint(int argc, char *argv[], bool &tests_need_run) {
#ifdef TESTS_ENABLED
for (int x = 0; x < argc; x++) {
if ((strncmp(argv[x], "--test", 6) == 0) && (strlen(argv[x]) == 6)) {
tests_need_run = true;
- OS::get_singleton()->initialize();
- StringName::setup();
+ // TODO: need to come up with different test contexts.
+ // Not every test requires high-level functionality like `ClassDB`.
+ test_setup();
int status = test_main(argc, argv);
- StringName::cleanup();
- // TODO: fix OS::singleton cleanup
+ test_cleanup();
return status;
}
}
@@ -1140,7 +1218,7 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph
}
if (bool(GLOBAL_GET("display/window/size/always_on_top"))) {
- window_flags |= DisplayServer::WINDOW_FLAG_ALWAYS_ON_TOP;
+ window_flags |= DisplayServer::WINDOW_FLAG_ALWAYS_ON_TOP_BIT;
}
}
diff --git a/main/main.h b/main/main.h
index 20c0bebefa..75a1c0d8cd 100644
--- a/main/main.h
+++ b/main/main.h
@@ -48,6 +48,10 @@ public:
static int test_entrypoint(int argc, char *argv[], bool &tests_need_run);
static Error setup(const char *execpath, int argc, char *argv[], bool p_second_phase = true);
static Error setup2(Thread::ID p_main_tid_override = 0);
+#ifdef TESTS_ENABLED
+ static Error test_setup();
+ static void test_cleanup();
+#endif
static bool start();
static bool iteration();
@@ -58,7 +62,7 @@ public:
static void cleanup();
};
-// Test main override is for the testing behaviour
+// Test main override is for the testing behaviour.
#define TEST_MAIN_OVERRIDE \
bool run_test = false; \
int return_code = Main::test_entrypoint(argc, argv, run_test); \
diff --git a/modules/mobile_vr/register_types.cpp b/modules/mobile_vr/register_types.cpp
index 75638d47c4..0bb555e780 100644
--- a/modules/mobile_vr/register_types.cpp
+++ b/modules/mobile_vr/register_types.cpp
@@ -35,9 +35,11 @@
void register_mobile_vr_types() {
ClassDB::register_class<MobileVRInterface>();
- Ref<MobileVRInterface> mobile_vr;
- mobile_vr.instance();
- XRServer::get_singleton()->add_interface(mobile_vr);
+ if (XRServer::get_singleton()) {
+ Ref<MobileVRInterface> mobile_vr;
+ mobile_vr.instance();
+ XRServer::get_singleton()->add_interface(mobile_vr);
+ }
}
void unregister_mobile_vr_types() {
diff --git a/platform/linuxbsd/os_linuxbsd.cpp b/platform/linuxbsd/os_linuxbsd.cpp
index 8c6f3b1167..e00a32e3ba 100644
--- a/platform/linuxbsd/os_linuxbsd.cpp
+++ b/platform/linuxbsd/os_linuxbsd.cpp
@@ -88,7 +88,9 @@ void OS_LinuxBSD::finalize() {
#endif
#ifdef JOYDEV_ENABLED
- memdelete(joypad);
+ if (joypad) {
+ memdelete(joypad);
+ }
#endif
}
diff --git a/platform/linuxbsd/os_linuxbsd.h b/platform/linuxbsd/os_linuxbsd.h
index 4295721c68..cd4fbd9db5 100644
--- a/platform/linuxbsd/os_linuxbsd.h
+++ b/platform/linuxbsd/os_linuxbsd.h
@@ -48,7 +48,7 @@ class OS_LinuxBSD : public OS_Unix {
bool force_quit;
#ifdef JOYDEV_ENABLED
- JoypadLinux *joypad;
+ JoypadLinux *joypad = nullptr;
#endif
#ifdef ALSA_ENABLED
diff --git a/platform/osx/detect.py b/platform/osx/detect.py
index 272ae1b620..a4a382f3a9 100644
--- a/platform/osx/detect.py
+++ b/platform/osx/detect.py
@@ -76,6 +76,7 @@ def configure(env):
elif env["target"] == "debug":
env.Prepend(CCFLAGS=["-g3"])
env.Prepend(CPPDEFINES=["DEBUG_ENABLED"])
+ env.Prepend(LINKFLAGS=["-Xlinker", "-no_deduplicate"])
## Architecture
diff --git a/platform/osx/display_server_osx.mm b/platform/osx/display_server_osx.mm
index 3b82950224..c4a5849d43 100644
--- a/platform/osx/display_server_osx.mm
+++ b/platform/osx/display_server_osx.mm
@@ -2374,7 +2374,11 @@ void DisplayServerOSX::_update_window(WindowData p_wd) {
[p_wd.window_object setHidesOnDeactivate:YES];
} else {
// Reset these when our window is not a borderless window that covers up the screen
- [p_wd.window_object setLevel:NSNormalWindowLevel];
+ if (p_wd.on_top) {
+ [p_wd.window_object setLevel:NSFloatingWindowLevel];
+ } else {
+ [p_wd.window_object setLevel:NSNormalWindowLevel];
+ }
[p_wd.window_object setHidesOnDeactivate:NO];
}
}
diff --git a/platform/osx/os_osx.h b/platform/osx/os_osx.h
index 9204a145bf..5a9e43450f 100644
--- a/platform/osx/os_osx.h
+++ b/platform/osx/os_osx.h
@@ -44,7 +44,7 @@ class OS_OSX : public OS_Unix {
bool force_quit;
- JoypadOSX *joypad_osx;
+ JoypadOSX *joypad_osx = nullptr;
#ifdef COREAUDIO_ENABLED
AudioDriverCoreAudio audio_driver;
diff --git a/platform/osx/os_osx.mm b/platform/osx/os_osx.mm
index c4eb5407af..399a29cbe0 100644
--- a/platform/osx/os_osx.mm
+++ b/platform/osx/os_osx.mm
@@ -145,7 +145,9 @@ void OS_OSX::finalize() {
delete_main_loop();
- memdelete(joypad_osx);
+ if (joypad_osx) {
+ memdelete(joypad_osx);
+ }
}
void OS_OSX::set_main_loop(MainLoop *p_main_loop) {
diff --git a/platform/windows/display_server_windows.cpp b/platform/windows/display_server_windows.cpp
index e4a7814cc1..cf7bebfbdf 100644
--- a/platform/windows/display_server_windows.cpp
+++ b/platform/windows/display_server_windows.cpp
@@ -2037,8 +2037,8 @@ LRESULT DisplayServerWindows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARA
Ref<InputEventMouseMotion> mm;
mm.instance();
mm->set_window_id(window_id);
- mm->set_control(GetKeyState(VK_CONTROL) != 0);
- mm->set_shift(GetKeyState(VK_SHIFT) != 0);
+ mm->set_control(GetKeyState(VK_CONTROL) < 0);
+ mm->set_shift(GetKeyState(VK_SHIFT) < 0);
mm->set_alt(alt_mem);
mm->set_pressure(windows[window_id].last_pressure);
@@ -2180,8 +2180,8 @@ LRESULT DisplayServerWindows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARA
mm->set_tilt(Vector2((float)pen_info.tiltX / 90, (float)pen_info.tiltY / 90));
}
- mm->set_control((wParam & MK_CONTROL) != 0);
- mm->set_shift((wParam & MK_SHIFT) != 0);
+ mm->set_control(GetKeyState(VK_CONTROL) < 0);
+ mm->set_shift(GetKeyState(VK_SHIFT) < 0);
mm->set_alt(alt_mem);
mm->set_button_mask(last_button_state);
diff --git a/scene/register_scene_types.cpp b/scene/register_scene_types.cpp
index 47b8b6073b..996d249bbf 100644
--- a/scene/register_scene_types.cpp
+++ b/scene/register_scene_types.cpp
@@ -372,7 +372,11 @@ void register_scene_types() {
OS::get_singleton()->yield(); //may take time to init
- AcceptDialog::set_swap_cancel_ok(GLOBAL_DEF_NOVAL("gui/common/swap_cancel_ok", bool(DisplayServer::get_singleton()->get_swap_cancel_ok())));
+ bool swap_cancel_ok = false;
+ if (DisplayServer::get_singleton()) {
+ swap_cancel_ok = GLOBAL_DEF_NOVAL("gui/common/swap_cancel_ok", bool(DisplayServer::get_singleton()->get_swap_cancel_ok()));
+ }
+ AcceptDialog::set_swap_cancel_ok(swap_cancel_ok);
#endif
/* REGISTER 3D */
@@ -912,8 +916,10 @@ void register_scene_types() {
}
}
- // Always make the default theme to avoid invalid default font/icon/style in the given theme
- make_default_theme(default_theme_hidpi, font);
+ // Always make the default theme to avoid invalid default font/icon/style in the given theme.
+ if (RenderingServer::get_singleton()) {
+ make_default_theme(default_theme_hidpi, font);
+ }
if (theme_path != String()) {
Ref<Theme> theme = ResourceLoader::load(theme_path);
diff --git a/scene/resources/resource_format_text.cpp b/scene/resources/resource_format_text.cpp
index 93db8b725f..d4d8018d43 100644
--- a/scene/resources/resource_format_text.cpp
+++ b/scene/resources/resource_format_text.cpp
@@ -572,7 +572,7 @@ Error ResourceLoaderText::load() {
}
}
- if (progress) {
+ if (progress && resources_total > 0) {
*progress = resource_current / float(resources_total);
}
}
@@ -640,7 +640,7 @@ Error ResourceLoaderText::load() {
return error;
} else {
error = OK;
- if (progress) {
+ if (progress && resources_total > 0) {
*progress = resource_current / float(resources_total);
}
@@ -674,7 +674,7 @@ Error ResourceLoaderText::load() {
resource_current++;
- if (progress) {
+ if (progress && resources_total > 0) {
*progress = resource_current / float(resources_total);
}
diff --git a/tests/test_class_db.cpp b/tests/test_class_db.cpp
deleted file mode 100644
index 3171091402..0000000000
--- a/tests/test_class_db.cpp
+++ /dev/null
@@ -1,882 +0,0 @@
-/*************************************************************************/
-/* test_class_db.cpp */
-/*************************************************************************/
-/* This file is part of: */
-/* GODOT ENGINE */
-/* https://godotengine.org */
-/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 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 "test_class_db.h"
-
-#include "core/global_constants.h"
-#include "core/ordered_hash_map.h"
-#include "core/os/os.h"
-#include "core/string_name.h"
-#include "core/ustring.h"
-#include "core/variant.h"
-
-namespace TestClassDB {
-
-enum class [[nodiscard]] TestResult{
- FAILED,
- PASS
-};
-
-#define TEST_FAIL_COND(m_cond, m_msg) \
- if (unlikely(m_cond)) { \
- ERR_PRINT(m_msg); \
- return TestResult::FAILED; \
- } else \
- ((void)0)
-
-#define TEST_COND(m_cond, m_msg) \
- if (unlikely(m_cond)) { \
- ERR_PRINT(m_msg); \
- __test_result__ = TestResult::FAILED; \
- } else \
- ((void)0)
-
-#define TEST_FAIL_CHECK(m_test_expr) \
- if (unlikely((m_test_expr) == TestResult::FAILED)) { \
- return TestResult::FAILED; \
- } else \
- ((void)0)
-
-#define TEST_CHECK(m_test_expr) \
- if (unlikely((m_test_expr) == TestResult::FAILED)) { \
- __test_result__ = TestResult::FAILED; \
- } else \
- ((void)0)
-
-#define TEST_START() \
- TestResult __test_result__ = TestResult::PASS; \
- ((void)0)
-
-#define TEST_END() return __test_result__;
-
-struct TypeReference {
- StringName name;
- bool is_enum = false;
-};
-
-struct ConstantData {
- String name;
- int value = 0;
-};
-
-struct EnumData {
- StringName name;
- List<ConstantData> constants;
-
- _FORCE_INLINE_ bool operator==(const EnumData &p_enum) const {
- return p_enum.name == name;
- }
-};
-
-struct PropertyData {
- StringName name;
- int index = 0;
-
- StringName getter;
- StringName setter;
-};
-
-struct ArgumentData {
- TypeReference type;
- String name;
- bool has_defval = false;
- Variant defval;
-};
-
-struct MethodData {
- StringName name;
- TypeReference return_type;
- List<ArgumentData> arguments;
- bool is_virtual = false;
- bool is_vararg = false;
-};
-
-struct SignalData {
- StringName name;
- List<ArgumentData> arguments;
-};
-
-struct ExposedClass {
- StringName name;
- StringName base;
-
- bool is_singleton = false;
- bool is_instantiable = false;
- bool is_reference = false;
-
- ClassDB::APIType api_type;
-
- List<ConstantData> constants;
- List<EnumData> enums;
- List<PropertyData> properties;
- List<MethodData> methods;
- List<SignalData> signals_;
-
- const PropertyData *find_property_by_name(const StringName &p_name) const {
- for (const List<PropertyData>::Element *E = properties.front(); E; E = E->next()) {
- if (E->get().name == p_name) {
- return &E->get();
- }
- }
-
- return nullptr;
- }
-
- const MethodData *find_method_by_name(const StringName &p_name) const {
- for (const List<MethodData>::Element *E = methods.front(); E; E = E->next()) {
- if (E->get().name == p_name) {
- return &E->get();
- }
- }
-
- return nullptr;
- }
-};
-
-struct NamesCache {
- StringName variant_type = StaticCString::create("Variant");
- StringName object_class = StaticCString::create("Object");
- StringName reference_class = StaticCString::create("Reference");
- StringName string_type = StaticCString::create("String");
- StringName string_name_type = StaticCString::create("StringName");
- StringName node_path_type = StaticCString::create("NodePath");
- StringName bool_type = StaticCString::create("bool");
- StringName int_type = StaticCString::create("int");
- StringName float_type = StaticCString::create("float");
- StringName void_type = StaticCString::create("void");
- StringName vararg_stub_type = StaticCString::create("@VarArg@");
- StringName vector2_type = StaticCString::create("Vector2");
- StringName rect2_type = StaticCString::create("Rect2");
- StringName vector3_type = StaticCString::create("Vector3");
-
- // Object not included as it must be checked for all derived classes
- static constexpr int nullable_types_count = 17;
- StringName nullable_types[nullable_types_count] = {
- string_type,
- string_name_type,
- node_path_type,
-
- StaticCString::create(_STR(Array)),
- StaticCString::create(_STR(Dictionary)),
- StaticCString::create(_STR(Callable)),
- StaticCString::create(_STR(Signal)),
-
- StaticCString::create(_STR(PackedByteArray)),
- StaticCString::create(_STR(PackedInt32Array)),
- StaticCString::create(_STR(PackedInt64rray)),
- StaticCString::create(_STR(PackedFloat32Array)),
- StaticCString::create(_STR(PackedFloat64Array)),
- StaticCString::create(_STR(PackedStringArray)),
- StaticCString::create(_STR(PackedVector2Array)),
- StaticCString::create(_STR(PackedVector3Array)),
- StaticCString::create(_STR(PackedColorArray)),
- };
-
- bool is_nullable_type(const StringName &p_type) const {
- for (int i = 0; i < nullable_types_count; i++) {
- if (p_type == nullable_types[i]) {
- return true;
- }
- }
-
- return false;
- }
-};
-
-typedef OrderedHashMap<StringName, ExposedClass> ExposedClasses;
-
-struct Context {
- Vector<StringName> enum_types;
- Vector<StringName> builtin_types;
- ExposedClasses exposed_classes;
- List<EnumData> global_enums;
- NamesCache names_cache;
-
- const ExposedClass *find_exposed_class(const StringName &p_name) const {
- ExposedClasses::ConstElement elem = exposed_classes.find(p_name);
- return elem ? &elem.value() : nullptr;
- }
-
- const ExposedClass *find_exposed_class(const TypeReference &p_type_ref) const {
- ExposedClasses::ConstElement elem = exposed_classes.find(p_type_ref.name);
- return elem ? &elem.value() : nullptr;
- }
-
- bool has_type(const TypeReference &p_type_ref) const {
- if (builtin_types.find(p_type_ref.name) >= 0) {
- return true;
- }
-
- if (p_type_ref.is_enum) {
- if (enum_types.find(p_type_ref.name) >= 0) {
- return true;
- }
-
- // Enum not found. Most likely because none of its constants were bound, so it's empty. That's fine. Use int instead.
- return builtin_types.find(names_cache.int_type);
- }
-
- return false;
- }
-};
-
-bool arg_default_value_is_assignable_to_type(const Context &p_context, const Variant &p_val, const TypeReference &p_arg_type) {
- if (p_arg_type.name == p_context.names_cache.variant_type) {
- // Variant can take anything
- return true;
- }
-
- switch (p_val.get_type()) {
- case Variant::NIL:
- return p_context.find_exposed_class(p_arg_type) ||
- p_context.names_cache.is_nullable_type(p_arg_type.name);
- case Variant::BOOL:
- return p_arg_type.name == p_context.names_cache.bool_type;
- case Variant::INT:
- return p_arg_type.name == p_context.names_cache.int_type ||
- p_arg_type.name == p_context.names_cache.float_type ||
- p_arg_type.is_enum;
- case Variant::FLOAT:
- return p_arg_type.name == p_context.names_cache.float_type;
- case Variant::STRING:
- case Variant::STRING_NAME:
- return p_arg_type.name == p_context.names_cache.string_type ||
- p_arg_type.name == p_context.names_cache.string_name_type ||
- p_arg_type.name == p_context.names_cache.node_path_type;
- case Variant::NODE_PATH:
- return p_arg_type.name == p_context.names_cache.node_path_type;
- case Variant::TRANSFORM:
- case Variant::TRANSFORM2D:
- case Variant::BASIS:
- case Variant::QUAT:
- case Variant::PLANE:
- case Variant::AABB:
- case Variant::COLOR:
- case Variant::VECTOR2:
- case Variant::RECT2:
- case Variant::VECTOR3:
- case Variant::_RID:
- case Variant::ARRAY:
- case Variant::DICTIONARY:
- case Variant::PACKED_BYTE_ARRAY:
- case Variant::PACKED_INT32_ARRAY:
- case Variant::PACKED_INT64_ARRAY:
- case Variant::PACKED_FLOAT32_ARRAY:
- case Variant::PACKED_FLOAT64_ARRAY:
- case Variant::PACKED_STRING_ARRAY:
- case Variant::PACKED_VECTOR2_ARRAY:
- case Variant::PACKED_VECTOR3_ARRAY:
- case Variant::PACKED_COLOR_ARRAY:
- case Variant::CALLABLE:
- case Variant::SIGNAL:
- return p_arg_type.name == Variant::get_type_name(p_val.get_type());
- case Variant::OBJECT:
- return p_context.find_exposed_class(p_arg_type);
- case Variant::VECTOR2I:
- return p_arg_type.name == p_context.names_cache.vector2_type ||
- p_arg_type.name == Variant::get_type_name(p_val.get_type());
- case Variant::RECT2I:
- return p_arg_type.name == p_context.names_cache.rect2_type ||
- p_arg_type.name == Variant::get_type_name(p_val.get_type());
- case Variant::VECTOR3I:
- return p_arg_type.name == p_context.names_cache.vector3_type ||
- p_arg_type.name == Variant::get_type_name(p_val.get_type());
- default:
- ERR_PRINT("Unexpected Variant type: " + itos(p_val.get_type()));
- break;
- }
-
- return false;
-}
-
-TestResult validate_property(const Context &p_context, const ExposedClass &p_class, const PropertyData &p_prop) {
- TEST_START();
-
- const MethodData *setter = p_class.find_method_by_name(p_prop.setter);
-
- // Search it in base classes too
- const ExposedClass *top = &p_class;
- while (!setter && top->base != StringName()) {
- top = p_context.find_exposed_class(top->base);
- TEST_FAIL_COND(!top, "Class not found '" + top->base + "'. Inherited by '" + top->name + "'.");
- setter = top->find_method_by_name(p_prop.setter);
- }
-
- const MethodData *getter = p_class.find_method_by_name(p_prop.getter);
-
- // Search it in base classes too
- top = &p_class;
- while (!getter && top->base != StringName()) {
- top = p_context.find_exposed_class(top->base);
- TEST_FAIL_COND(!top, "Class not found '" + top->base + "'. Inherited by '" + top->name + "'.");
- getter = top->find_method_by_name(p_prop.getter);
- }
-
- TEST_FAIL_COND(!setter && !getter,
- "Couldn't find neither the setter nor the getter for property: '" + p_class.name + "." + String(p_prop.name) + "'.");
-
- if (setter) {
- int setter_argc = p_prop.index != -1 ? 2 : 1;
- TEST_FAIL_COND(setter->arguments.size() != setter_argc,
- "Invalid property setter argument count: '" + p_class.name + "." + String(p_prop.name) + "'.");
- }
-
- if (getter) {
- int getter_argc = p_prop.index != -1 ? 1 : 0;
- TEST_FAIL_COND(getter->arguments.size() != getter_argc,
- "Invalid property setter argument count: '" + p_class.name + "." + String(p_prop.name) + "'.");
- }
-
- if (getter && setter) {
- const ArgumentData &setter_first_arg = setter->arguments.back()->get();
- if (getter->return_type.name != setter_first_arg.type.name) {
- // Special case for Node::set_name
- bool whitelisted = getter->return_type.name == p_context.names_cache.string_name_type &&
- setter_first_arg.type.name == p_context.names_cache.string_type;
-
- TEST_FAIL_COND(!whitelisted,
- "Return type from getter doesn't match first argument of setter, for property: '" + p_class.name + "." + String(p_prop.name) + "'.");
- }
- }
-
- const TypeReference &prop_type_ref = getter ? getter->return_type : setter->arguments.back()->get().type;
-
- const ExposedClass *prop_class = p_context.find_exposed_class(prop_type_ref);
- if (prop_class) {
- TEST_COND(prop_class->is_singleton,
- "Property type is a singleton: '" + p_class.name + "." + String(p_prop.name) + "'.");
- } else {
- TEST_FAIL_COND(!p_context.has_type(prop_type_ref),
- "Property type '" + prop_type_ref.name + "' not found: '" + p_class.name + "." + String(p_prop.name) + "'.");
- }
-
- if (getter) {
- if (p_prop.index != -1) {
- const ArgumentData &idx_arg = getter->arguments.front()->get();
- if (idx_arg.type.name != p_context.names_cache.int_type) {
- // If not an int, it can be an enum
- TEST_COND(p_context.enum_types.find(idx_arg.type.name) < 0,
- "Invalid type '" + idx_arg.type.name + "' for index argument of property getter: '" + p_class.name + "." + String(p_prop.name) + "'.");
- }
- }
- }
-
- if (setter) {
- if (p_prop.index != -1) {
- const ArgumentData &idx_arg = setter->arguments.front()->get();
- if (idx_arg.type.name != p_context.names_cache.int_type) {
- // Assume the index parameter is an enum
- // If not an int, it can be an enum
- TEST_COND(p_context.enum_types.find(idx_arg.type.name) < 0,
- "Invalid type '" + idx_arg.type.name + "' for index argument of property setter: '" + p_class.name + "." + String(p_prop.name) + "'.");
- }
- }
- }
-
- TEST_END();
-}
-
-TestResult validate_method(const Context &p_context, const ExposedClass &p_class, const MethodData &p_method) {
- TEST_START();
-
- const ExposedClass *return_class = p_context.find_exposed_class(p_method.return_type);
- if (return_class) {
- TEST_COND(return_class->is_singleton,
- "Method return type is a singleton: '" + p_class.name + "." + p_method.name + "'.");
- }
-
- for (const List<ArgumentData>::Element *F = p_method.arguments.front(); F; F = F->next()) {
- const ArgumentData &arg = F->get();
-
- const ExposedClass *arg_class = p_context.find_exposed_class(arg.type);
- if (arg_class) {
- TEST_COND(arg_class->is_singleton,
- "Argument type is a singleton: '" + arg.name + "' of method '" + p_class.name + "." + p_method.name + "'.");
- } else {
- TEST_FAIL_COND(!p_context.has_type(arg.type),
- "Argument type '" + arg.type.name + "' not found: '" + arg.name + "' of method" + p_class.name + "." + p_method.name + "'.");
- }
-
- if (arg.has_defval) {
- TEST_COND(!arg_default_value_is_assignable_to_type(p_context, arg.defval, arg.type),
- "Invalid default value for parameter '" + arg.name + "' of method '" + p_class.name + "." + p_method.name + "'.");
- }
- }
-
- TEST_END();
-}
-
-TestResult validate_signal(const Context &p_context, const ExposedClass &p_class, const SignalData &p_signal) {
- TEST_START();
-
- for (const List<ArgumentData>::Element *F = p_signal.arguments.front(); F; F = F->next()) {
- const ArgumentData &arg = F->get();
-
- const ExposedClass *arg_class = p_context.find_exposed_class(arg.type);
- if (arg_class) {
- TEST_COND(arg_class->is_singleton,
- "Argument class is a singleton: '" + arg.name + "' of signal" + p_class.name + "." + p_signal.name + "'.");
- } else {
- TEST_FAIL_COND(!p_context.has_type(arg.type),
- "Argument type '" + arg.type.name + "' not found: '" + arg.name + "' of signal" + p_class.name + "." + p_signal.name + "'.");
- }
- }
-
- TEST_END();
-}
-
-TestResult validate_class(const Context &p_context, const ExposedClass &p_exposed_class) {
- TEST_START();
-
- bool is_derived_type = p_exposed_class.base != StringName();
-
- if (!is_derived_type) {
- // Asserts about the base Object class
- TEST_FAIL_COND(p_exposed_class.name != p_context.names_cache.object_class,
- "Class '" + p_exposed_class.name + "' has no base class.");
- TEST_FAIL_COND(!p_exposed_class.is_instantiable,
- "Object class is not instantiable.");
- TEST_FAIL_COND(p_exposed_class.api_type != ClassDB::API_CORE,
- "Object class is API is not API_CORE.");
- TEST_FAIL_COND(p_exposed_class.is_singleton,
- "Object class is registered as a singleton.");
- }
-
- TEST_FAIL_COND(p_exposed_class.is_singleton && p_exposed_class.base != p_context.names_cache.object_class,
- "Singleton base class '" + String(p_exposed_class.base) + "' is not Object, for class '" + p_exposed_class.name + "'.");
-
- TEST_FAIL_COND(is_derived_type && !p_context.exposed_classes.has(p_exposed_class.base),
- "Base type '" + p_exposed_class.base.operator String() + "' does not exist, for class '" + p_exposed_class.name + "'.");
-
- for (const List<PropertyData>::Element *F = p_exposed_class.properties.front(); F; F = F->next()) {
- TEST_CHECK(validate_property(p_context, p_exposed_class, F->get()));
- }
-
- for (const List<MethodData>::Element *F = p_exposed_class.methods.front(); F; F = F->next()) {
- TEST_CHECK(validate_method(p_context, p_exposed_class, F->get()));
- }
-
- for (const List<SignalData>::Element *F = p_exposed_class.signals_.front(); F; F = F->next()) {
- TEST_CHECK(validate_signal(p_context, p_exposed_class, F->get()));
- }
-
- TEST_END();
-}
-
-TestResult add_exposed_classes(Context &r_context) {
- TEST_START();
-
- List<StringName> class_list;
- ClassDB::get_class_list(&class_list);
- class_list.sort_custom<StringName::AlphCompare>();
-
- while (class_list.size()) {
- StringName class_name = class_list.front()->get();
-
- ClassDB::APIType api_type = ClassDB::get_api_type(class_name);
-
- if (api_type == ClassDB::API_NONE) {
- class_list.pop_front();
- continue;
- }
-
- if (!ClassDB::is_class_exposed(class_name)) {
- OS::get_singleton()->print("Ignoring class '%s' because it's not exposed\n", String(class_name).utf8().get_data());
- class_list.pop_front();
- continue;
- }
-
- if (!ClassDB::is_class_enabled(class_name)) {
- OS::get_singleton()->print("Ignoring class '%s' because it's not enabled\n", String(class_name).utf8().get_data());
- class_list.pop_front();
- continue;
- }
-
- ClassDB::ClassInfo *class_info = ClassDB::classes.getptr(class_name);
-
- ExposedClass exposed_class;
- exposed_class.name = class_name;
- exposed_class.api_type = api_type;
- exposed_class.is_singleton = Engine::get_singleton()->has_singleton(class_name);
- exposed_class.is_instantiable = class_info->creation_func && !exposed_class.is_singleton;
- exposed_class.is_reference = ClassDB::is_parent_class(class_name, "Reference");
- exposed_class.base = ClassDB::get_parent_class(class_name);
-
- // Add properties
-
- List<PropertyInfo> property_list;
- ClassDB::get_property_list(class_name, &property_list, true);
-
- Map<StringName, StringName> accessor_methods;
-
- for (const List<PropertyInfo>::Element *E = property_list.front(); E; E = E->next()) {
- const PropertyInfo &property = E->get();
-
- if (property.usage & PROPERTY_USAGE_GROUP || property.usage & PROPERTY_USAGE_SUBGROUP || property.usage & PROPERTY_USAGE_CATEGORY) {
- continue;
- }
-
- PropertyData prop;
- prop.name = property.name;
- prop.setter = ClassDB::get_property_setter(class_name, prop.name);
- prop.getter = ClassDB::get_property_getter(class_name, prop.name);
-
- if (prop.setter != StringName()) {
- accessor_methods[prop.setter] = prop.name;
- }
- if (prop.getter != StringName()) {
- accessor_methods[prop.getter] = prop.name;
- }
-
- bool valid = false;
- prop.index = ClassDB::get_property_index(class_name, prop.name, &valid);
- TEST_FAIL_COND(!valid, "Invalid property: '" + exposed_class.name + "." + String(prop.name) + "'.");
-
- exposed_class.properties.push_back(prop);
- }
-
- // Add methods
-
- List<MethodInfo> virtual_method_list;
- ClassDB::get_virtual_methods(class_name, &virtual_method_list, true);
-
- List<MethodInfo> method_list;
- ClassDB::get_method_list(class_name, &method_list, true);
- method_list.sort();
-
- for (List<MethodInfo>::Element *E = method_list.front(); E; E = E->next()) {
- const MethodInfo &method_info = E->get();
-
- int argc = method_info.arguments.size();
-
- if (method_info.name.empty()) {
- continue;
- }
-
- MethodData method;
- method.name = method_info.name;
-
- if (method_info.flags & METHOD_FLAG_VIRTUAL) {
- method.is_virtual = true;
- }
-
- PropertyInfo return_info = method_info.return_val;
-
- MethodBind *m = method.is_virtual ? nullptr : ClassDB::get_method(class_name, method_info.name);
-
- method.is_vararg = m && m->is_vararg();
-
- if (!m && !method.is_virtual) {
- TEST_FAIL_COND(!virtual_method_list.find(method_info),
- "Missing MethodBind for non-virtual method: '" + exposed_class.name + "." + method.name + "'.");
-
- // A virtual method without the virtual flag. This is a special case.
-
- // The method Object.free is registered as a virtual method, but without the virtual flag.
- // This is because this method is not supposed to be overridden, but called.
- // We assume the return type is void.
- method.return_type.name = r_context.names_cache.void_type;
-
- // Actually, more methods like this may be added in the future, which could return
- // something different. Let's put this check to notify us if that ever happens.
- if (exposed_class.name != r_context.names_cache.object_class || String(method.name) != "free") {
- WARN_PRINT("Notification: New unexpected virtual non-overridable method found."
- " We only expected Object.free, but found '" +
- exposed_class.name + "." + method.name + "'.");
- }
- } else if (return_info.type == Variant::INT && return_info.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
- method.return_type.name = return_info.class_name;
- method.return_type.is_enum = true;
- } else if (return_info.class_name != StringName()) {
- method.return_type.name = return_info.class_name;
-
- bool bad_reference_hint = !method.is_virtual && return_info.hint != PROPERTY_HINT_RESOURCE_TYPE &&
- ClassDB::is_parent_class(return_info.class_name, r_context.names_cache.reference_class);
- TEST_COND(bad_reference_hint, String() + "Return type is reference but hint is not '" _STR(PROPERTY_HINT_RESOURCE_TYPE) "'." +
- " Are you returning a reference type by pointer? Method: '" +
- exposed_class.name + "." + method.name + "'.");
- } else if (return_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {
- method.return_type.name = return_info.hint_string;
- } else if (return_info.type == Variant::NIL && return_info.usage & PROPERTY_USAGE_NIL_IS_VARIANT) {
- method.return_type.name = r_context.names_cache.variant_type;
- } else if (return_info.type == Variant::NIL) {
- method.return_type.name = r_context.names_cache.void_type;
- } else {
- // NOTE: We don't care about the size and sign of int and float in these tests
- method.return_type.name = Variant::get_type_name(return_info.type);
- }
-
- for (int i = 0; i < argc; i++) {
- PropertyInfo arg_info = method_info.arguments[i];
-
- String orig_arg_name = arg_info.name;
-
- ArgumentData arg;
- arg.name = orig_arg_name;
-
- if (arg_info.type == Variant::INT && arg_info.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
- arg.type.name = arg_info.class_name;
- arg.type.is_enum = true;
- } else if (arg_info.class_name != StringName()) {
- arg.type.name = arg_info.class_name;
- } else if (arg_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {
- arg.type.name = arg_info.hint_string;
- } else if (arg_info.type == Variant::NIL) {
- arg.type.name = r_context.names_cache.variant_type;
- } else {
- // NOTE: We don't care about the size and sign of int and float in these tests
- arg.type.name = Variant::get_type_name(arg_info.type);
- }
-
- if (m && m->has_default_argument(i)) {
- arg.has_defval = true;
- arg.defval = m->get_default_argument(i);
- }
-
- method.arguments.push_back(arg);
- }
-
- if (method.is_vararg) {
- ArgumentData vararg;
- vararg.type.name = r_context.names_cache.vararg_stub_type;
- vararg.name = "@varargs@";
- method.arguments.push_back(vararg);
- }
-
- TEST_COND(exposed_class.find_property_by_name(method.name),
- "Method name conflicts with property: '" + String(class_name) + "." + String(method.name) + "'.");
-
- // Classes starting with an underscore are ignored unless they're used as a property setter or getter
- if (!method.is_virtual && String(method.name)[0] == '_') {
- for (const List<PropertyData>::Element *F = exposed_class.properties.front(); F; F = F->next()) {
- const PropertyData &prop = F->get();
-
- if (prop.setter == method.name || prop.getter == method.name) {
- exposed_class.methods.push_back(method);
- break;
- }
- }
- } else {
- exposed_class.methods.push_back(method);
- }
- }
-
- // Add signals
-
- const HashMap<StringName, MethodInfo> &signal_map = class_info->signal_map;
- const StringName *k = nullptr;
-
- while ((k = signal_map.next(k))) {
- SignalData signal;
-
- const MethodInfo &method_info = signal_map.get(*k);
-
- signal.name = method_info.name;
-
- int argc = method_info.arguments.size();
-
- for (int i = 0; i < argc; i++) {
- PropertyInfo arg_info = method_info.arguments[i];
-
- String orig_arg_name = arg_info.name;
-
- ArgumentData arg;
- arg.name = orig_arg_name;
-
- if (arg_info.type == Variant::INT && arg_info.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
- arg.type.name = arg_info.class_name;
- arg.type.is_enum = true;
- } else if (arg_info.class_name != StringName()) {
- arg.type.name = arg_info.class_name;
- } else if (arg_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {
- arg.type.name = arg_info.hint_string;
- } else if (arg_info.type == Variant::NIL) {
- arg.type.name = r_context.names_cache.variant_type;
- } else {
- // NOTE: We don't care about the size and sign of int and float in these tests
- arg.type.name = Variant::get_type_name(arg_info.type);
- }
-
- signal.arguments.push_back(arg);
- }
-
- bool method_conflict = exposed_class.find_property_by_name(signal.name);
-
- if (method_conflict || exposed_class.find_method_by_name(signal.name)) {
- // TODO:
- // ClassDB allows signal names that conflict with method or property names.
- // However registering a signal with a conflicting name is still considered wrong.
- // Unfortunately there are some existing cases that are yet to be fixed.
- // Until those are fixed we will print a warning instead of failing the test.
- WARN_PRINT("Signal name conflicts with " + String(method_conflict ? "method" : "property") +
- ": '" + String(class_name) + "." + String(signal.name) + "'.");
- }
-
- exposed_class.signals_.push_back(signal);
- }
-
- // Add enums and constants
-
- List<String> constants;
- ClassDB::get_integer_constant_list(class_name, &constants, true);
-
- const HashMap<StringName, List<StringName>> &enum_map = class_info->enum_map;
- k = nullptr;
-
- while ((k = enum_map.next(k))) {
- EnumData enum_;
- enum_.name = *k;
-
- const List<StringName> &enum_constants = enum_map.get(*k);
- for (const List<StringName>::Element *E = enum_constants.front(); E; E = E->next()) {
- const StringName &constant_name = E->get();
- int *value = class_info->constant_map.getptr(constant_name);
- TEST_FAIL_COND(!value, "Missing enum constant value: '" +
- String(class_name) + "." + String(enum_.name) + "." + String(constant_name) + "'.");
- constants.erase(constant_name);
-
- ConstantData constant;
- constant.name = constant_name;
- constant.value = *value;
-
- enum_.constants.push_back(constant);
- }
-
- exposed_class.enums.push_back(enum_);
-
- r_context.enum_types.push_back(String(class_name) + "." + String(*k));
- }
-
- for (const List<String>::Element *E = constants.front(); E; E = E->next()) {
- const String &constant_name = E->get();
- int *value = class_info->constant_map.getptr(StringName(E->get()));
- TEST_FAIL_COND(!value, "Missing enum constant value: '" + String(class_name) + "." + String(constant_name) + "'.");
-
- ConstantData constant;
- constant.name = constant_name;
- constant.value = *value;
-
- exposed_class.constants.push_back(constant);
- }
-
- r_context.exposed_classes.insert(class_name, exposed_class);
- class_list.pop_front();
- }
-
- TEST_END();
-}
-
-void add_builtin_types(Context &r_context) {
- // NOTE: We don't care about the size and sign of int and float in these tests
- for (int i = 0; i < Variant::VARIANT_MAX; i++) {
- r_context.builtin_types.push_back(Variant::get_type_name(Variant::Type(i)));
- }
-
- r_context.builtin_types.push_back(_STR(Variant));
- r_context.builtin_types.push_back(r_context.names_cache.vararg_stub_type);
- r_context.builtin_types.push_back("void");
-}
-
-void add_global_enums(Context &r_context) {
- int global_constants_count = GlobalConstants::get_global_constant_count();
-
- if (global_constants_count > 0) {
- for (int i = 0; i < global_constants_count; i++) {
- StringName enum_name = GlobalConstants::get_global_constant_enum(i);
-
- if (enum_name != StringName()) {
- ConstantData constant;
- constant.name = GlobalConstants::get_global_constant_name(i);
- constant.value = GlobalConstants::get_global_constant_value(i);
-
- EnumData enum_;
- enum_.name = enum_name;
- List<EnumData>::Element *enum_match = r_context.global_enums.find(enum_);
- if (enum_match) {
- enum_match->get().constants.push_back(constant);
- } else {
- enum_.constants.push_back(constant);
- r_context.global_enums.push_back(enum_);
- }
- }
- }
-
- for (List<EnumData>::Element *E = r_context.global_enums.front(); E; E = E->next()) {
- r_context.enum_types.push_back(E->get().name);
- }
- }
-
- // HARDCODED
- List<StringName> hardcoded_enums;
- hardcoded_enums.push_back("Vector2.Axis");
- hardcoded_enums.push_back("Vector2i.Axis");
- hardcoded_enums.push_back("Vector3.Axis");
- hardcoded_enums.push_back("Vector3i.Axis");
- for (List<StringName>::Element *E = hardcoded_enums.front(); E; E = E->next()) {
- // These enums are not generated and must be written manually (e.g.: Vector3.Axis)
- // Here, we assume core types do not begin with underscore
- r_context.enum_types.push_back(E->get());
- }
-}
-
-TestResult run_class_db_tests() {
- TEST_START();
-
- Context context;
-
- TEST_FAIL_CHECK(add_exposed_classes(context));
- add_builtin_types(context);
- add_global_enums(context);
-
- const ExposedClass *object_class = context.find_exposed_class(context.names_cache.object_class);
- TEST_FAIL_COND(!object_class, "Object class not found.");
- TEST_FAIL_COND(object_class->base != StringName(),
- "Object class derives from another class: '" + object_class->base + "'.");
-
- for (ExposedClasses::Element E = context.exposed_classes.front(); E; E = E.next()) {
- TEST_CHECK(validate_class(context, E.value()));
- }
-
- TEST_END();
-}
-
-MainLoop *test() {
- TestResult pass = run_class_db_tests();
-
- OS::get_singleton()->print("ClassDB tests: %s\n", pass == TestResult::PASS ? "PASS" : "FAILED");
-
- if (pass == TestResult::FAILED) {
- OS::get_singleton()->set_exit_code(pass == TestResult::PASS ? 0 : 1);
- }
-
- return nullptr;
-}
-
-} // namespace TestClassDB
diff --git a/tests/test_class_db.h b/tests/test_class_db.h
index 1a31cfb01b..d0d8136874 100644
--- a/tests/test_class_db.h
+++ b/tests/test_class_db.h
@@ -31,12 +31,806 @@
#ifndef GODOT_TEST_CLASS_DB_H
#define GODOT_TEST_CLASS_DB_H
-#include "core/os/main_loop.h"
+#include "core/register_core_types.h"
+
+#include "core/global_constants.h"
+#include "core/ordered_hash_map.h"
+#include "core/os/os.h"
+#include "core/string_name.h"
+#include "core/ustring.h"
+#include "core/variant.h"
+
+#include "tests/test_macros.h"
+
+#define TEST_COND DOCTEST_CHECK_FALSE_MESSAGE
+#define TEST_FAIL DOCTEST_FAIL
+#define TEST_FAIL_COND DOCTEST_REQUIRE_FALSE_MESSAGE
+#define TEST_FAIL_COND_WARN DOCTEST_WARN_FALSE_MESSAGE
namespace TestClassDB {
-MainLoop *test();
+struct TypeReference {
+ StringName name;
+ bool is_enum = false;
+};
+
+struct ConstantData {
+ String name;
+ int value = 0;
+};
+
+struct EnumData {
+ StringName name;
+ List<ConstantData> constants;
+
+ _FORCE_INLINE_ bool operator==(const EnumData &p_enum) const {
+ return p_enum.name == name;
+ }
+};
+
+struct PropertyData {
+ StringName name;
+ int index = 0;
+
+ StringName getter;
+ StringName setter;
+};
+
+struct ArgumentData {
+ TypeReference type;
+ String name;
+ bool has_defval = false;
+ Variant defval;
+};
+
+struct MethodData {
+ StringName name;
+ TypeReference return_type;
+ List<ArgumentData> arguments;
+ bool is_virtual = false;
+ bool is_vararg = false;
+};
+
+struct SignalData {
+ StringName name;
+ List<ArgumentData> arguments;
+};
+
+struct ExposedClass {
+ StringName name;
+ StringName base;
+
+ bool is_singleton = false;
+ bool is_instantiable = false;
+ bool is_reference = false;
+
+ ClassDB::APIType api_type;
+
+ List<ConstantData> constants;
+ List<EnumData> enums;
+ List<PropertyData> properties;
+ List<MethodData> methods;
+ List<SignalData> signals_;
+
+ const PropertyData *find_property_by_name(const StringName &p_name) const {
+ for (const List<PropertyData>::Element *E = properties.front(); E; E = E->next()) {
+ if (E->get().name == p_name) {
+ return &E->get();
+ }
+ }
+
+ return nullptr;
+ }
+
+ const MethodData *find_method_by_name(const StringName &p_name) const {
+ for (const List<MethodData>::Element *E = methods.front(); E; E = E->next()) {
+ if (E->get().name == p_name) {
+ return &E->get();
+ }
+ }
+
+ return nullptr;
+ }
+};
+
+struct NamesCache {
+ StringName variant_type = StaticCString::create("Variant");
+ StringName object_class = StaticCString::create("Object");
+ StringName reference_class = StaticCString::create("Reference");
+ StringName string_type = StaticCString::create("String");
+ StringName string_name_type = StaticCString::create("StringName");
+ StringName node_path_type = StaticCString::create("NodePath");
+ StringName bool_type = StaticCString::create("bool");
+ StringName int_type = StaticCString::create("int");
+ StringName float_type = StaticCString::create("float");
+ StringName void_type = StaticCString::create("void");
+ StringName vararg_stub_type = StaticCString::create("@VarArg@");
+ StringName vector2_type = StaticCString::create("Vector2");
+ StringName rect2_type = StaticCString::create("Rect2");
+ StringName vector3_type = StaticCString::create("Vector3");
+ // Object not included as it must be checked for all derived classes
+ static constexpr int nullable_types_count = 17;
+ StringName nullable_types[nullable_types_count] = {
+ string_type,
+ string_name_type,
+ node_path_type,
+
+ StaticCString::create(_STR(Array)),
+ StaticCString::create(_STR(Dictionary)),
+ StaticCString::create(_STR(Callable)),
+ StaticCString::create(_STR(Signal)),
+
+ StaticCString::create(_STR(PackedByteArray)),
+ StaticCString::create(_STR(PackedInt32Array)),
+ StaticCString::create(_STR(PackedInt64rray)),
+ StaticCString::create(_STR(PackedFloat32Array)),
+ StaticCString::create(_STR(PackedFloat64Array)),
+ StaticCString::create(_STR(PackedStringArray)),
+ StaticCString::create(_STR(PackedVector2Array)),
+ StaticCString::create(_STR(PackedVector3Array)),
+ StaticCString::create(_STR(PackedColorArray)),
+ };
+
+ bool is_nullable_type(const StringName &p_type) const {
+ for (int i = 0; i < nullable_types_count; i++) {
+ if (p_type == nullable_types[i]) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+};
+
+typedef OrderedHashMap<StringName, ExposedClass> ExposedClasses;
+
+struct Context {
+ Vector<StringName> enum_types;
+ Vector<StringName> builtin_types;
+ ExposedClasses exposed_classes;
+ List<EnumData> global_enums;
+ NamesCache names_cache;
+
+ const ExposedClass *find_exposed_class(const StringName &p_name) const {
+ ExposedClasses::ConstElement elem = exposed_classes.find(p_name);
+ return elem ? &elem.value() : nullptr;
+ }
+
+ const ExposedClass *find_exposed_class(const TypeReference &p_type_ref) const {
+ ExposedClasses::ConstElement elem = exposed_classes.find(p_type_ref.name);
+ return elem ? &elem.value() : nullptr;
+ }
+
+ bool has_type(const TypeReference &p_type_ref) const {
+ if (builtin_types.find(p_type_ref.name) >= 0) {
+ return true;
+ }
+
+ if (p_type_ref.is_enum) {
+ if (enum_types.find(p_type_ref.name) >= 0) {
+ return true;
+ }
+
+ // Enum not found. Most likely because none of its constants were bound, so it's empty. That's fine. Use int instead.
+ return builtin_types.find(names_cache.int_type);
+ }
+
+ return false;
+ }
+};
+
+bool arg_default_value_is_assignable_to_type(const Context &p_context, const Variant &p_val, const TypeReference &p_arg_type, String *r_err_msg = nullptr) {
+ if (p_arg_type.name == p_context.names_cache.variant_type) {
+ // Variant can take anything
+ return true;
+ }
+
+ switch (p_val.get_type()) {
+ case Variant::NIL:
+ return p_context.find_exposed_class(p_arg_type) ||
+ p_context.names_cache.is_nullable_type(p_arg_type.name);
+ case Variant::BOOL:
+ return p_arg_type.name == p_context.names_cache.bool_type;
+ case Variant::INT:
+ return p_arg_type.name == p_context.names_cache.int_type ||
+ p_arg_type.name == p_context.names_cache.float_type ||
+ p_arg_type.is_enum;
+ case Variant::FLOAT:
+ return p_arg_type.name == p_context.names_cache.float_type;
+ case Variant::STRING:
+ case Variant::STRING_NAME:
+ return p_arg_type.name == p_context.names_cache.string_type ||
+ p_arg_type.name == p_context.names_cache.string_name_type ||
+ p_arg_type.name == p_context.names_cache.node_path_type;
+ case Variant::NODE_PATH:
+ return p_arg_type.name == p_context.names_cache.node_path_type;
+ case Variant::TRANSFORM:
+ case Variant::TRANSFORM2D:
+ case Variant::BASIS:
+ case Variant::QUAT:
+ case Variant::PLANE:
+ case Variant::AABB:
+ case Variant::COLOR:
+ case Variant::VECTOR2:
+ case Variant::RECT2:
+ case Variant::VECTOR3:
+ case Variant::_RID:
+ case Variant::ARRAY:
+ case Variant::DICTIONARY:
+ case Variant::PACKED_BYTE_ARRAY:
+ case Variant::PACKED_INT32_ARRAY:
+ case Variant::PACKED_INT64_ARRAY:
+ case Variant::PACKED_FLOAT32_ARRAY:
+ case Variant::PACKED_FLOAT64_ARRAY:
+ case Variant::PACKED_STRING_ARRAY:
+ case Variant::PACKED_VECTOR2_ARRAY:
+ case Variant::PACKED_VECTOR3_ARRAY:
+ case Variant::PACKED_COLOR_ARRAY:
+ case Variant::CALLABLE:
+ case Variant::SIGNAL:
+ return p_arg_type.name == Variant::get_type_name(p_val.get_type());
+ case Variant::OBJECT:
+ return p_context.find_exposed_class(p_arg_type);
+ case Variant::VECTOR2I:
+ return p_arg_type.name == p_context.names_cache.vector2_type ||
+ p_arg_type.name == Variant::get_type_name(p_val.get_type());
+ case Variant::RECT2I:
+ return p_arg_type.name == p_context.names_cache.rect2_type ||
+ p_arg_type.name == Variant::get_type_name(p_val.get_type());
+ case Variant::VECTOR3I:
+ return p_arg_type.name == p_context.names_cache.vector3_type ||
+ p_arg_type.name == Variant::get_type_name(p_val.get_type());
+ default:
+ if (r_err_msg) {
+ *r_err_msg = "Unexpected Variant type: " + itos(p_val.get_type());
+ }
+ break;
+ }
+
+ return false;
}
+void validate_property(const Context &p_context, const ExposedClass &p_class, const PropertyData &p_prop) {
+ const MethodData *setter = p_class.find_method_by_name(p_prop.setter);
+
+ // Search it in base classes too
+ const ExposedClass *top = &p_class;
+ while (!setter && top->base != StringName()) {
+ top = p_context.find_exposed_class(top->base);
+ TEST_FAIL_COND(!top, "Class not found '" + top->base + "'. Inherited by '" + top->name + "'.");
+ setter = top->find_method_by_name(p_prop.setter);
+ }
+
+ const MethodData *getter = p_class.find_method_by_name(p_prop.getter);
+
+ // Search it in base classes too
+ top = &p_class;
+ while (!getter && top->base != StringName()) {
+ top = p_context.find_exposed_class(top->base);
+ TEST_FAIL_COND(!top, "Class not found '" + top->base + "'. Inherited by '" + top->name + "'.");
+ getter = top->find_method_by_name(p_prop.getter);
+ }
+
+ TEST_FAIL_COND((!setter && !getter),
+ "Couldn't find neither the setter nor the getter for property: '" + p_class.name + "." + String(p_prop.name) + "'.");
+
+ if (setter) {
+ int setter_argc = p_prop.index != -1 ? 2 : 1;
+ TEST_FAIL_COND(setter->arguments.size() != setter_argc,
+ "Invalid property setter argument count: '" + p_class.name + "." + String(p_prop.name) + "'.");
+ }
+
+ if (getter) {
+ int getter_argc = p_prop.index != -1 ? 1 : 0;
+ TEST_FAIL_COND(getter->arguments.size() != getter_argc,
+ "Invalid property setter argument count: '" + p_class.name + "." + String(p_prop.name) + "'.");
+ }
+
+ if (getter && setter) {
+ const ArgumentData &setter_first_arg = setter->arguments.back()->get();
+ if (getter->return_type.name != setter_first_arg.type.name) {
+ // Special case for Node::set_name
+ bool whitelisted = getter->return_type.name == p_context.names_cache.string_name_type &&
+ setter_first_arg.type.name == p_context.names_cache.string_type;
+
+ TEST_FAIL_COND(!whitelisted,
+ "Return type from getter doesn't match first argument of setter, for property: '" + p_class.name + "." + String(p_prop.name) + "'.");
+ }
+ }
+
+ const TypeReference &prop_type_ref = getter ? getter->return_type : setter->arguments.back()->get().type;
+
+ const ExposedClass *prop_class = p_context.find_exposed_class(prop_type_ref);
+ if (prop_class) {
+ TEST_COND(prop_class->is_singleton,
+ "Property type is a singleton: '" + p_class.name + "." + String(p_prop.name) + "'.");
+ } else {
+ TEST_FAIL_COND(!p_context.has_type(prop_type_ref),
+ "Property type '" + prop_type_ref.name + "' not found: '" + p_class.name + "." + String(p_prop.name) + "'.");
+ }
+
+ if (getter) {
+ if (p_prop.index != -1) {
+ const ArgumentData &idx_arg = getter->arguments.front()->get();
+ if (idx_arg.type.name != p_context.names_cache.int_type) {
+ // If not an int, it can be an enum
+ TEST_COND(p_context.enum_types.find(idx_arg.type.name) < 0,
+ "Invalid type '" + idx_arg.type.name + "' for index argument of property getter: '" + p_class.name + "." + String(p_prop.name) + "'.");
+ }
+ }
+ }
+
+ if (setter) {
+ if (p_prop.index != -1) {
+ const ArgumentData &idx_arg = setter->arguments.front()->get();
+ if (idx_arg.type.name != p_context.names_cache.int_type) {
+ // Assume the index parameter is an enum
+ // If not an int, it can be an enum
+ TEST_COND(p_context.enum_types.find(idx_arg.type.name) < 0,
+ "Invalid type '" + idx_arg.type.name + "' for index argument of property setter: '" + p_class.name + "." + String(p_prop.name) + "'.");
+ }
+ }
+ }
+}
+
+void validate_method(const Context &p_context, const ExposedClass &p_class, const MethodData &p_method) {
+ const ExposedClass *return_class = p_context.find_exposed_class(p_method.return_type);
+ if (return_class) {
+ TEST_COND(return_class->is_singleton,
+ "Method return type is a singleton: '" + p_class.name + "." + p_method.name + "'.");
+ }
+
+ for (const List<ArgumentData>::Element *F = p_method.arguments.front(); F; F = F->next()) {
+ const ArgumentData &arg = F->get();
+
+ const ExposedClass *arg_class = p_context.find_exposed_class(arg.type);
+ if (arg_class) {
+ TEST_COND(arg_class->is_singleton,
+ "Argument type is a singleton: '" + arg.name + "' of method '" + p_class.name + "." + p_method.name + "'.");
+ } else {
+ TEST_FAIL_COND(!p_context.has_type(arg.type),
+ "Argument type '" + arg.type.name + "' not found: '" + arg.name + "' of method" + p_class.name + "." + p_method.name + "'.");
+ }
+
+ if (arg.has_defval) {
+ String type_error_msg;
+ bool arg_defval_assignable_to_type = arg_default_value_is_assignable_to_type(p_context, arg.defval, arg.type, &type_error_msg);
+ String err_msg = vformat("Invalid default value for parameter '%s' of method '%s.%s'.", arg.name, p_class.name, p_method.name);
+ if (!type_error_msg.empty()) {
+ err_msg += " " + type_error_msg;
+ }
+ TEST_COND(!arg_defval_assignable_to_type, err_msg.utf8().get_data());
+ }
+ }
+}
+
+void validate_signal(const Context &p_context, const ExposedClass &p_class, const SignalData &p_signal) {
+ for (const List<ArgumentData>::Element *F = p_signal.arguments.front(); F; F = F->next()) {
+ const ArgumentData &arg = F->get();
+
+ const ExposedClass *arg_class = p_context.find_exposed_class(arg.type);
+ if (arg_class) {
+ TEST_COND(arg_class->is_singleton,
+ "Argument class is a singleton: '" + arg.name + "' of signal" + p_class.name + "." + p_signal.name + "'.");
+ } else {
+ TEST_FAIL_COND(!p_context.has_type(arg.type),
+ "Argument type '" + arg.type.name + "' not found: '" + arg.name + "' of signal" + p_class.name + "." + p_signal.name + "'.");
+ }
+ }
+}
+
+void validate_class(const Context &p_context, const ExposedClass &p_exposed_class) {
+ bool is_derived_type = p_exposed_class.base != StringName();
+
+ if (!is_derived_type) {
+ // Asserts about the base Object class
+ TEST_FAIL_COND(p_exposed_class.name != p_context.names_cache.object_class,
+ "Class '" + p_exposed_class.name + "' has no base class.");
+ TEST_FAIL_COND(!p_exposed_class.is_instantiable,
+ "Object class is not instantiable.");
+ TEST_FAIL_COND(p_exposed_class.api_type != ClassDB::API_CORE,
+ "Object class is API is not API_CORE.");
+ TEST_FAIL_COND(p_exposed_class.is_singleton,
+ "Object class is registered as a singleton.");
+ }
+
+ TEST_FAIL_COND((p_exposed_class.is_singleton && p_exposed_class.base != p_context.names_cache.object_class),
+ "Singleton base class '" + String(p_exposed_class.base) + "' is not Object, for class '" + p_exposed_class.name + "'.");
+
+ TEST_FAIL_COND((is_derived_type && !p_context.exposed_classes.has(p_exposed_class.base)),
+ "Base type '" + p_exposed_class.base.operator String() + "' does not exist, for class '" + p_exposed_class.name + "'.");
+
+ for (const List<PropertyData>::Element *F = p_exposed_class.properties.front(); F; F = F->next()) {
+ validate_property(p_context, p_exposed_class, F->get());
+ }
+
+ for (const List<MethodData>::Element *F = p_exposed_class.methods.front(); F; F = F->next()) {
+ validate_method(p_context, p_exposed_class, F->get());
+ }
+
+ for (const List<SignalData>::Element *F = p_exposed_class.signals_.front(); F; F = F->next()) {
+ validate_signal(p_context, p_exposed_class, F->get());
+ }
+}
+
+void add_exposed_classes(Context &r_context) {
+ List<StringName> class_list;
+ ClassDB::get_class_list(&class_list);
+ class_list.sort_custom<StringName::AlphCompare>();
+
+ while (class_list.size()) {
+ StringName class_name = class_list.front()->get();
+
+ ClassDB::APIType api_type = ClassDB::get_api_type(class_name);
+
+ if (api_type == ClassDB::API_NONE) {
+ class_list.pop_front();
+ continue;
+ }
+
+ if (!ClassDB::is_class_exposed(class_name)) {
+ MESSAGE(vformat("Ignoring class '%s' because it's not exposed.", class_name).utf8().get_data());
+ class_list.pop_front();
+ continue;
+ }
+
+ if (!ClassDB::is_class_enabled(class_name)) {
+ MESSAGE(vformat("Ignoring class '%s' because it's not enabled.", class_name).utf8().get_data());
+ class_list.pop_front();
+ continue;
+ }
+
+ ClassDB::ClassInfo *class_info = ClassDB::classes.getptr(class_name);
+
+ ExposedClass exposed_class;
+ exposed_class.name = class_name;
+ exposed_class.api_type = api_type;
+ exposed_class.is_singleton = Engine::get_singleton()->has_singleton(class_name);
+ exposed_class.is_instantiable = class_info->creation_func && !exposed_class.is_singleton;
+ exposed_class.is_reference = ClassDB::is_parent_class(class_name, "Reference");
+ exposed_class.base = ClassDB::get_parent_class(class_name);
+
+ // Add properties
+
+ List<PropertyInfo> property_list;
+ ClassDB::get_property_list(class_name, &property_list, true);
+
+ Map<StringName, StringName> accessor_methods;
+
+ for (const List<PropertyInfo>::Element *E = property_list.front(); E; E = E->next()) {
+ const PropertyInfo &property = E->get();
+
+ if (property.usage & PROPERTY_USAGE_GROUP || property.usage & PROPERTY_USAGE_SUBGROUP || property.usage & PROPERTY_USAGE_CATEGORY) {
+ continue;
+ }
+
+ PropertyData prop;
+ prop.name = property.name;
+ prop.setter = ClassDB::get_property_setter(class_name, prop.name);
+ prop.getter = ClassDB::get_property_getter(class_name, prop.name);
+
+ if (prop.setter != StringName()) {
+ accessor_methods[prop.setter] = prop.name;
+ }
+ if (prop.getter != StringName()) {
+ accessor_methods[prop.getter] = prop.name;
+ }
+
+ bool valid = false;
+ prop.index = ClassDB::get_property_index(class_name, prop.name, &valid);
+ TEST_FAIL_COND(!valid, "Invalid property: '" + exposed_class.name + "." + String(prop.name) + "'.");
+
+ exposed_class.properties.push_back(prop);
+ }
+
+ // Add methods
+
+ List<MethodInfo> virtual_method_list;
+ ClassDB::get_virtual_methods(class_name, &virtual_method_list, true);
+
+ List<MethodInfo> method_list;
+ ClassDB::get_method_list(class_name, &method_list, true);
+ method_list.sort();
+
+ for (List<MethodInfo>::Element *E = method_list.front(); E; E = E->next()) {
+ const MethodInfo &method_info = E->get();
+
+ int argc = method_info.arguments.size();
+
+ if (method_info.name.empty()) {
+ continue;
+ }
+
+ MethodData method;
+ method.name = method_info.name;
+
+ if (method_info.flags & METHOD_FLAG_VIRTUAL) {
+ method.is_virtual = true;
+ }
+
+ PropertyInfo return_info = method_info.return_val;
+
+ MethodBind *m = method.is_virtual ? nullptr : ClassDB::get_method(class_name, method_info.name);
+
+ method.is_vararg = m && m->is_vararg();
+
+ if (!m && !method.is_virtual) {
+ TEST_FAIL_COND(!virtual_method_list.find(method_info),
+ "Missing MethodBind for non-virtual method: '" + exposed_class.name + "." + method.name + "'.");
+
+ // A virtual method without the virtual flag. This is a special case.
+
+ // The method Object.free is registered as a virtual method, but without the virtual flag.
+ // This is because this method is not supposed to be overridden, but called.
+ // We assume the return type is void.
+ method.return_type.name = r_context.names_cache.void_type;
+
+ // Actually, more methods like this may be added in the future, which could return
+ // something different. Let's put this check to notify us if that ever happens.
+ String warn_msg = vformat(
+ "Notification: New unexpected virtual non-overridable method found. "
+ "We only expected Object.free, but found '%s.%s'.",
+ exposed_class.name, method.name);
+ TEST_FAIL_COND_WARN(
+ (exposed_class.name != r_context.names_cache.object_class || String(method.name) != "free"),
+ warn_msg.utf8().get_data());
+
+ } else if (return_info.type == Variant::INT && return_info.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
+ method.return_type.name = return_info.class_name;
+ method.return_type.is_enum = true;
+ } else if (return_info.class_name != StringName()) {
+ method.return_type.name = return_info.class_name;
+
+ bool bad_reference_hint = !method.is_virtual && return_info.hint != PROPERTY_HINT_RESOURCE_TYPE &&
+ ClassDB::is_parent_class(return_info.class_name, r_context.names_cache.reference_class);
+ TEST_COND(bad_reference_hint, String() + "Return type is reference but hint is not '" _STR(PROPERTY_HINT_RESOURCE_TYPE) "'." +
+ " Are you returning a reference type by pointer? Method: '" +
+ exposed_class.name + "." + method.name + "'.");
+ } else if (return_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {
+ method.return_type.name = return_info.hint_string;
+ } else if (return_info.type == Variant::NIL && return_info.usage & PROPERTY_USAGE_NIL_IS_VARIANT) {
+ method.return_type.name = r_context.names_cache.variant_type;
+ } else if (return_info.type == Variant::NIL) {
+ method.return_type.name = r_context.names_cache.void_type;
+ } else {
+ // NOTE: We don't care about the size and sign of int and float in these tests
+ method.return_type.name = Variant::get_type_name(return_info.type);
+ }
+
+ for (int i = 0; i < argc; i++) {
+ PropertyInfo arg_info = method_info.arguments[i];
+
+ String orig_arg_name = arg_info.name;
+
+ ArgumentData arg;
+ arg.name = orig_arg_name;
+
+ if (arg_info.type == Variant::INT && arg_info.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
+ arg.type.name = arg_info.class_name;
+ arg.type.is_enum = true;
+ } else if (arg_info.class_name != StringName()) {
+ arg.type.name = arg_info.class_name;
+ } else if (arg_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {
+ arg.type.name = arg_info.hint_string;
+ } else if (arg_info.type == Variant::NIL) {
+ arg.type.name = r_context.names_cache.variant_type;
+ } else {
+ // NOTE: We don't care about the size and sign of int and float in these tests
+ arg.type.name = Variant::get_type_name(arg_info.type);
+ }
+
+ if (m && m->has_default_argument(i)) {
+ arg.has_defval = true;
+ arg.defval = m->get_default_argument(i);
+ }
+
+ method.arguments.push_back(arg);
+ }
+
+ if (method.is_vararg) {
+ ArgumentData vararg;
+ vararg.type.name = r_context.names_cache.vararg_stub_type;
+ vararg.name = "@varargs@";
+ method.arguments.push_back(vararg);
+ }
+
+ TEST_COND(exposed_class.find_property_by_name(method.name),
+ "Method name conflicts with property: '" + String(class_name) + "." + String(method.name) + "'.");
+
+ // Classes starting with an underscore are ignored unless they're used as a property setter or getter
+ if (!method.is_virtual && String(method.name)[0] == '_') {
+ for (const List<PropertyData>::Element *F = exposed_class.properties.front(); F; F = F->next()) {
+ const PropertyData &prop = F->get();
+
+ if (prop.setter == method.name || prop.getter == method.name) {
+ exposed_class.methods.push_back(method);
+ break;
+ }
+ }
+ } else {
+ exposed_class.methods.push_back(method);
+ }
+ }
+
+ // Add signals
+
+ const HashMap<StringName, MethodInfo> &signal_map = class_info->signal_map;
+ const StringName *k = nullptr;
+
+ while ((k = signal_map.next(k))) {
+ SignalData signal;
+
+ const MethodInfo &method_info = signal_map.get(*k);
+
+ signal.name = method_info.name;
+
+ int argc = method_info.arguments.size();
+
+ for (int i = 0; i < argc; i++) {
+ PropertyInfo arg_info = method_info.arguments[i];
+
+ String orig_arg_name = arg_info.name;
+
+ ArgumentData arg;
+ arg.name = orig_arg_name;
+
+ if (arg_info.type == Variant::INT && arg_info.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
+ arg.type.name = arg_info.class_name;
+ arg.type.is_enum = true;
+ } else if (arg_info.class_name != StringName()) {
+ arg.type.name = arg_info.class_name;
+ } else if (arg_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {
+ arg.type.name = arg_info.hint_string;
+ } else if (arg_info.type == Variant::NIL) {
+ arg.type.name = r_context.names_cache.variant_type;
+ } else {
+ // NOTE: We don't care about the size and sign of int and float in these tests
+ arg.type.name = Variant::get_type_name(arg_info.type);
+ }
+
+ signal.arguments.push_back(arg);
+ }
+
+ bool method_conflict = exposed_class.find_property_by_name(signal.name);
+
+ // TODO:
+ // ClassDB allows signal names that conflict with method or property names.
+ // However registering a signal with a conflicting name is still considered wrong.
+ // Unfortunately there are some existing cases that are yet to be fixed.
+ // Until those are fixed we will print a warning instead of failing the test.
+ String warn_msg = vformat(
+ "Signal name conflicts with %s: '%s.%s.",
+ method_conflict ? "method" : "property", class_name, signal.name);
+ TEST_FAIL_COND_WARN((method_conflict || exposed_class.find_method_by_name(signal.name)),
+ warn_msg.utf8().get_data());
+
+ exposed_class.signals_.push_back(signal);
+ }
+
+ // Add enums and constants
+
+ List<String> constants;
+ ClassDB::get_integer_constant_list(class_name, &constants, true);
+
+ const HashMap<StringName, List<StringName>> &enum_map = class_info->enum_map;
+ k = nullptr;
+
+ while ((k = enum_map.next(k))) {
+ EnumData enum_;
+ enum_.name = *k;
+
+ const List<StringName> &enum_constants = enum_map.get(*k);
+ for (const List<StringName>::Element *E = enum_constants.front(); E; E = E->next()) {
+ const StringName &constant_name = E->get();
+ int *value = class_info->constant_map.getptr(constant_name);
+ TEST_FAIL_COND(!value, "Missing enum constant value: '" +
+ String(class_name) + "." + String(enum_.name) + "." + String(constant_name) + "'.");
+ constants.erase(constant_name);
+
+ ConstantData constant;
+ constant.name = constant_name;
+ constant.value = *value;
+
+ enum_.constants.push_back(constant);
+ }
+
+ exposed_class.enums.push_back(enum_);
+
+ r_context.enum_types.push_back(String(class_name) + "." + String(*k));
+ }
+
+ for (const List<String>::Element *E = constants.front(); E; E = E->next()) {
+ const String &constant_name = E->get();
+ int *value = class_info->constant_map.getptr(StringName(E->get()));
+ TEST_FAIL_COND(!value, "Missing enum constant value: '" + String(class_name) + "." + String(constant_name) + "'.");
+
+ ConstantData constant;
+ constant.name = constant_name;
+ constant.value = *value;
+
+ exposed_class.constants.push_back(constant);
+ }
+
+ r_context.exposed_classes.insert(class_name, exposed_class);
+ class_list.pop_front();
+ }
+}
+
+void add_builtin_types(Context &r_context) {
+ // NOTE: We don't care about the size and sign of int and float in these tests
+ for (int i = 0; i < Variant::VARIANT_MAX; i++) {
+ r_context.builtin_types.push_back(Variant::get_type_name(Variant::Type(i)));
+ }
+
+ r_context.builtin_types.push_back(_STR(Variant));
+ r_context.builtin_types.push_back(r_context.names_cache.vararg_stub_type);
+ r_context.builtin_types.push_back("void");
+}
+
+void add_global_enums(Context &r_context) {
+ int global_constants_count = GlobalConstants::get_global_constant_count();
+
+ if (global_constants_count > 0) {
+ for (int i = 0; i < global_constants_count; i++) {
+ StringName enum_name = GlobalConstants::get_global_constant_enum(i);
+
+ if (enum_name != StringName()) {
+ ConstantData constant;
+ constant.name = GlobalConstants::get_global_constant_name(i);
+ constant.value = GlobalConstants::get_global_constant_value(i);
+
+ EnumData enum_;
+ enum_.name = enum_name;
+ List<EnumData>::Element *enum_match = r_context.global_enums.find(enum_);
+ if (enum_match) {
+ enum_match->get().constants.push_back(constant);
+ } else {
+ enum_.constants.push_back(constant);
+ r_context.global_enums.push_back(enum_);
+ }
+ }
+ }
+
+ for (List<EnumData>::Element *E = r_context.global_enums.front(); E; E = E->next()) {
+ r_context.enum_types.push_back(E->get().name);
+ }
+ }
+
+ // HARDCODED
+ List<StringName> hardcoded_enums;
+ hardcoded_enums.push_back("Vector2.Axis");
+ hardcoded_enums.push_back("Vector2i.Axis");
+ hardcoded_enums.push_back("Vector3.Axis");
+ hardcoded_enums.push_back("Vector3i.Axis");
+ for (List<StringName>::Element *E = hardcoded_enums.front(); E; E = E->next()) {
+ // These enums are not generated and must be written manually (e.g.: Vector3.Axis)
+ // Here, we assume core types do not begin with underscore
+ r_context.enum_types.push_back(E->get());
+ }
+}
+
+TEST_SUITE("[ClassDB]") {
+ TEST_CASE("[ClassDB] Add exposed classes, builtin types, and global enums") {
+ Context context;
+
+ add_exposed_classes(context);
+ add_builtin_types(context);
+ add_global_enums(context);
+
+ SUBCASE("[ClassDB] Find exposed class") {
+ const ExposedClass *object_class = context.find_exposed_class(context.names_cache.object_class);
+ TEST_FAIL_COND(!object_class, "Object class not found.");
+ TEST_FAIL_COND(object_class->base != StringName(),
+ "Object class derives from another class: '" + object_class->base + "'.");
+
+ for (ExposedClasses::Element E = context.exposed_classes.front(); E; E = E.next()) {
+ validate_class(context, E.value());
+ }
+ }
+ }
+}
+
+} // namespace TestClassDB
+
#endif //GODOT_TEST_CLASS_DB_H
diff --git a/tests/test_validate_testing.h b/tests/test_validate_testing.h
index 4bcc57d9c5..b4ea6eb576 100644
--- a/tests/test_validate_testing.h
+++ b/tests/test_validate_testing.h
@@ -54,8 +54,6 @@ TEST_SUITE("Validate tests") {
CHECK_MESSAGE(_print_error_enabled, "Error printing should be re-enabled.");
}
TEST_CASE("Stringify Variant types") {
- ClassDB::init(); // For objects.
-
Variant var;
INFO(var);
@@ -185,8 +183,6 @@ TEST_SUITE("Validate tests") {
<< var << " " << vec2 << " " << rect2 << " " << color);
CHECK(true); // So all above prints.
-
- ClassDB::cleanup();
}
}