Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable signal callback generation in C# #87952

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/object/script_language.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ class ScriptLanguage : public Object {
virtual bool can_inherit_from_file() const { return false; }
virtual int find_function(const String &p_function, const String &p_code) const = 0;
virtual String make_function(const String &p_class, const String &p_name, const PackedStringArray &p_args) const = 0;
virtual bool can_make_function() const { return true; }
virtual Error open_in_external_editor(const Ref<Script> &p_script, int p_line, int p_col) { return ERR_UNAVAILABLE; }
virtual bool overrides_external_editor() { return false; }

Expand Down
1 change: 1 addition & 0 deletions core/object/script_language_extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ void ScriptLanguageExtension::_bind_methods() {
GDVIRTUAL_BIND(_can_inherit_from_file);
GDVIRTUAL_BIND(_find_function, "function", "code");
GDVIRTUAL_BIND(_make_function, "class_name", "function_name", "function_args");
GDVIRTUAL_BIND(_can_make_function);
GDVIRTUAL_BIND(_open_in_external_editor, "script", "line", "column");
GDVIRTUAL_BIND(_overrides_external_editor);

Expand Down
1 change: 1 addition & 0 deletions core/object/script_language_extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ class ScriptLanguageExtension : public ScriptLanguage {

EXBIND2RC(int, find_function, const String &, const String &)
EXBIND3RC(String, make_function, const String &, const String &, const PackedStringArray &)
EXBIND0RC(bool, can_make_function)
EXBIND3R(Error, open_in_external_editor, const Ref<Script> &, int, int)
EXBIND0R(bool, overrides_external_editor)

Expand Down
5 changes: 5 additions & 0 deletions doc/classes/ScriptLanguageExtension.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
<description>
</description>
</method>
<method name="_can_make_function" qualifiers="virtual const">
<return type="bool" />
<description>
</description>
</method>
<method name="_complete_code" qualifiers="virtual const">
<return type="Dictionary" />
<param index="0" name="code" type="String" />
Expand Down
23 changes: 23 additions & 0 deletions editor/connections_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ void ConnectDialog::_tree_node_selected() {
set_dst_method(generate_method_callback_name(source, signal, current));
}
_update_method_tree();
_update_warning_label();
_update_ok_enabled();
}

Expand Down Expand Up @@ -433,6 +434,23 @@ void ConnectDialog::_update_ok_enabled() {
get_ok_button()->set_disabled(false);
}

void ConnectDialog::_update_warning_label() {
Ref<Script> scr = source->get_node(dst_path)->get_script();
if (scr.is_null()) {
warning_label->set_visible(false);
return;
}

ScriptLanguage *language = scr->get_language();
if (language->can_make_function()) {
warning_label->set_visible(false);
return;
}

warning_label->set_text(vformat(TTR("%s: Callback code won't be generated, please add it manually."), language->get_name()));
warning_label->set_visible(true);
}

void ConnectDialog::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE: {
Expand Down Expand Up @@ -617,6 +635,7 @@ void ConnectDialog::init(const ConnectionData &p_cd, const PackedStringArray &p_

void ConnectDialog::popup_dialog(const String p_for_signal) {
from_signal->set_text(p_for_signal);
warning_label->add_theme_color_override("font_color", warning_label->get_theme_color(SNAME("warning_color"), EditorStringName(Editor)));
error_label->add_theme_color_override("font_color", error_label->get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
filter_nodes->clear();

Expand Down Expand Up @@ -694,6 +713,10 @@ ConnectDialog::ConnectDialog() {
connect_to_label = Object::cast_to<Label>(vbc_left->get_child(mc->get_index() - 1));
vbc_left->add_child(tree);

warning_label = memnew(Label);
vbc_left->add_child(warning_label);
warning_label->hide();

error_label = memnew(Label);
error_label->set_text(TTR("Scene does not contain any script."));
vbc_left->add_child(error_label);
Expand Down
2 changes: 2 additions & 0 deletions editor/connections_dialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ class ConnectDialog : public ConfirmationDialog {
CheckButton *advanced = nullptr;
Vector<Control *> bind_controls;

Label *warning_label = nullptr;
Label *error_label = nullptr;

void ok_pressed() override;
Expand All @@ -155,6 +156,7 @@ class ConnectDialog : public ConfirmationDialog {
void _remove_bind();
void _advanced_pressed();
void _update_ok_enabled();
void _update_warning_label();

protected:
void _notification(int p_what);
Expand Down
4 changes: 4 additions & 0 deletions editor/plugins/script_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2726,6 +2726,10 @@ void ScriptEditor::_add_callback(Object *p_obj, const String &p_function, const
Ref<Script> scr = p_obj->get_script();
ERR_FAIL_COND(!scr.is_valid());

if (!scr->get_language()->can_make_function()) {
return;
}

EditorNode::get_singleton()->push_item(scr.ptr());

for (int i = 0; i < tab_container->get_tab_count(); i++) {
Expand Down
9 changes: 7 additions & 2 deletions editor/plugins/script_text_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,14 +345,19 @@ void ScriptTextEditor::reload_text() {
}

void ScriptTextEditor::add_callback(const String &p_function, PackedStringArray p_args) {
ScriptLanguage *language = script->get_language();
if (!language->can_make_function()) {
return;
}

String code = code_editor->get_text_editor()->get_text();
int pos = script->get_language()->find_function(p_function, code);
int pos = language->find_function(p_function, code);
code_editor->get_text_editor()->remove_secondary_carets();
if (pos == -1) {
//does not exist
code_editor->get_text_editor()->deselect();
pos = code_editor->get_text_editor()->get_line_count() + 2;
String func = script->get_language()->make_function("", p_function, p_args);
String func = language->make_function("", p_function, p_args);
//code=code+func;
code_editor->get_text_editor()->set_caret_line(pos + 1);
code_editor->get_text_editor()->set_caret_column(1000000); //none shall be that big
Expand Down
21 changes: 5 additions & 16 deletions modules/mono/csharp_script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,22 +516,11 @@ static String variant_type_to_managed_name(const String &p_var_type_name) {
}

String CSharpLanguage::make_function(const String &, const String &p_name, const PackedStringArray &p_args) const {
// FIXME
// - Due to Godot's API limitation this just appends the function to the end of the file
// - Use fully qualified name if there is ambiguity
String s = "private void " + p_name + "(";
for (int i = 0; i < p_args.size(); i++) {
const String &arg = p_args[i];

if (i > 0) {
s += ", ";
}

s += variant_type_to_managed_name(arg.get_slice(":", 1)) + " " + escape_csharp_keyword(arg.get_slice(":", 0));
}
s += ")\n{\n // Replace with function body.\n}\n";

return s;
// The make_function() API does not work for C# scripts.
// It will always append the generated function at the very end of the script. In C#, it will break compilation by
// appending code after the final closing bracket (either the class' or the namespace's).
// To prevent issues, we have can_make_function() returning false, and make_function() is never implemented.
return String();
}
#else
String CSharpLanguage::make_function(const String &, const String &, const PackedStringArray &) const {
Expand Down
1 change: 1 addition & 0 deletions modules/mono/csharp_script.h
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ class CSharpLanguage : public ScriptLanguage {
return -1;
}
String make_function(const String &p_class, const String &p_name, const PackedStringArray &p_args) const override;
virtual bool can_make_function() const override { return false; }
virtual String _get_indentation() const;
/* TODO? */ void auto_indent_code(String &p_code, int p_from_line, int p_to_line) const override {}
/* TODO */ void add_global_constant(const StringName &p_variable, const Variant &p_value) override {}
Expand Down
Loading