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

Make script UIDs optional #87706

Closed
wants to merge 1 commit into from
Closed
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 @@ -253,6 +253,7 @@ class ScriptLanguage : public Object {
#endif
virtual bool supports_builtin_mode() const = 0;
virtual bool supports_documentation() const { return false; }
virtual bool supports_uid() const { return false; }
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;
Expand Down
25 changes: 25 additions & 0 deletions editor/plugins/script_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1429,6 +1429,30 @@ void ScriptEditor::_menu_option(int p_option) {
es->run();
} break;

case FILE_INSERT_UID: {
Ref<Script> scr = current->get_edited_resource();
if (scr.is_null()) {
EditorToaster::get_singleton()->popup_str(TTR("Cannot insert UID: edited file is not a Script."), EditorToaster::SEVERITY_WARNING);
break;
}
if (!scr->get_language()->supports_uid()) {
EditorToaster::get_singleton()->popup_str(TTR("Cannot insert UID: edited script's language does not support UIDs."), EditorToaster::SEVERITY_WARNING);
break;
}
if (scr->is_built_in()) {
EditorToaster::get_singleton()->popup_str(TTR("Cannot insert UID: edited script is built-in."), EditorToaster::SEVERITY_WARNING);
break;
}

const ResourceUID::ID uid = ResourceSaver::get_resource_id_for_path(scr->get_path(), false);
if (uid != ResourceUID::INVALID_ID) {
EditorToaster::get_singleton()->popup_str(TTR("Cannot insert UID: edited script already has a UID."), EditorToaster::SEVERITY_WARNING);
break;
}

ResourceSaver::set_uid(scr->get_path(), ResourceUID::get_singleton()->create_id());
} break;

case FILE_CLOSE: {
if (current->is_unsaved()) {
_ask_close_current_unsaved_tab(current);
Expand Down Expand Up @@ -4005,6 +4029,7 @@ ScriptEditor::ScriptEditor(WindowWrapper *p_wrapper) {

file_menu->get_popup()->add_separator();
file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/run_file", TTR("Run"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::X), FILE_RUN);
file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/insert_uid", TTR("Insert UID")), FILE_INSERT_UID);

file_menu->get_popup()->add_separator();
file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/toggle_scripts_panel", TTR("Toggle Scripts Panel"), KeyModifierMask::CMD_OR_CTRL | Key::BACKSLASH), TOGGLE_SCRIPTS_PANEL);
Expand Down
1 change: 1 addition & 0 deletions editor/plugins/script_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ class ScriptEditor : public PanelContainer {
FILE_SAVE_ALL,
FILE_THEME,
FILE_RUN,
FILE_INSERT_UID,
FILE_CLOSE,
CLOSE_DOCS,
CLOSE_ALL,
Expand Down
29 changes: 29 additions & 0 deletions editor/script_create_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,10 @@ void ScriptCreateDialog::_create_new() {
alert->popup_centered();
return;
}

if (add_uid->is_pressed()) {
ResourceSaver::set_uid(lpath, ResourceUID::get_singleton()->create_id());
}
}

emit_signal(SNAME("script_created"), scr);
Expand All @@ -379,6 +383,17 @@ void ScriptCreateDialog::_load_exist() {
hide();
}

void ScriptCreateDialog::_update_uid_button() {
if (language->supports_uid() && !is_built_in) {
const bool use_uid = EditorSettings::get_singleton()->get_project_metadata("script_setup", "add_uid", true);
add_uid->set_pressed_no_signal(use_uid);
add_uid->set_disabled(false);
} else {
add_uid->set_pressed_no_signal(false);
add_uid->set_disabled(true);
}
}

void ScriptCreateDialog::_language_changed(int l) {
language = ScriptServer::get_language(l);

Expand Down Expand Up @@ -423,6 +438,7 @@ void ScriptCreateDialog::_language_changed(int l) {
file_path->set_text(path);

EditorSettings::get_singleton()->set_project_metadata("script_setup", "last_selected_language", language_menu->get_item_text(language_menu->get_selected()));
_update_uid_button();

_parent_name_changed(parent_name->get_text());
validation_panel->update();
Expand All @@ -436,9 +452,14 @@ void ScriptCreateDialog::_built_in_pressed() {
is_built_in = false;
_path_changed(file_path->get_text());
}
_update_uid_button();
validation_panel->update();
}

void ScriptCreateDialog::_add_uid_toggled(bool p_enabled) {
EditorSettings::get_singleton()->set_project_metadata("script_setup", "add_uid", p_enabled);
}

void ScriptCreateDialog::_use_template_pressed() {
is_using_templates = use_templates->is_pressed();
EditorSettings::get_singleton()->set_meta("script_setup_use_script_templates", is_using_templates);
Expand Down Expand Up @@ -946,6 +967,14 @@ ScriptCreateDialog::ScriptCreateDialog() {
gc->add_child(memnew(Label(TTR("Built-in Script:"))));
gc->add_child(built_in);

/* Add UID. */

add_uid = memnew(CheckBox);
add_uid->set_text(TTR("On"));
add_uid->connect("toggled", callable_mp(this, &ScriptCreateDialog::_add_uid_toggled));
gc->add_child(memnew(Label(TTR("Add UID:"))));
gc->add_child(add_uid);

/* Path */

hb = memnew(HBoxContainer);
Expand Down
3 changes: 3 additions & 0 deletions editor/script_create_dialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class ScriptCreateDialog : public ConfirmationDialog {
Button *path_button = nullptr;
EditorFileDialog *file_browse = nullptr;
CheckBox *built_in = nullptr;
CheckBox *add_uid = nullptr;
CheckBox *use_templates = nullptr;
VBoxContainer *path_vb = nullptr;
AcceptDialog *alert = nullptr;
Expand Down Expand Up @@ -99,6 +100,7 @@ class ScriptCreateDialog : public ConfirmationDialog {
void _path_changed(const String &p_path = String());
void _language_changed(int l = 0);
void _built_in_pressed();
void _add_uid_toggled(bool p_enabled);
void _use_template_pressed();
bool _validate_parent(const String &p_string);
String _validate_path(const String &p_path, bool p_file_must_exist);
Expand All @@ -111,6 +113,7 @@ class ScriptCreateDialog : public ConfirmationDialog {
virtual void ok_pressed() override;
void _create_new();
void _load_exist();
void _update_uid_button();
void _update_template_menu();
void _update_dialog();
ScriptLanguage::ScriptTemplate _get_current_template() const;
Expand Down
49 changes: 14 additions & 35 deletions modules/gdscript/gdscript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2879,49 +2879,17 @@ Error ResourceFormatSaverGDScript::save(const Ref<Resource> &p_resource, const S
ERR_FAIL_COND_V(sqscr.is_null(), ERR_INVALID_PARAMETER);

String source = sqscr->get_source_code();
ResourceUID::ID uid = ResourceSaver::get_resource_id_for_path(p_path, !p_resource->is_built_in());

{
bool source_changed = false;
Error err;
Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::WRITE, &err);

ERR_FAIL_COND_V_MSG(err, err, "Cannot save GDScript file '" + p_path + "'.");

if (uid != ResourceUID::INVALID_ID) {
GDScriptParser parser;
parser.parse(source, "", false);
const GDScriptParser::ClassNode *c = parser.get_tree();
if (c && ResourceUID::get_singleton()->text_to_id(c->uid_string) != uid) {
const Vector2i &uid_idx = c->uid_lines;
PackedStringArray lines = source.split("\n");

if (uid_idx.x > -1) {
for (int i = uid_idx.x + 1; i <= uid_idx.y; i++) {
// If UID is written across multiple lines, erase extra lines.
lines.remove_at(uid_idx.x + 1);
}
lines.write[uid_idx.x] = GDScript::create_uid_line(ResourceUID::get_singleton()->id_to_text(uid));
} else {
lines.insert(0, GDScript::create_uid_line(ResourceUID::get_singleton()->id_to_text(uid)));
}
source = String("\n").join(lines);
source_changed = true;
file->store_string(String("\n").join(lines));
} else {
file->store_string(source);
}
}

file->store_string(source);
if (file->get_error() != OK && file->get_error() != ERR_FILE_EOF) {
return ERR_CANT_CREATE;
}

if (source_changed) {
sqscr->set_source_code(source);
sqscr->reload();
sqscr->emit_changed();
}
}

if (ScriptServer::is_reload_scripts_on_save_enabled()) {
Expand Down Expand Up @@ -2964,9 +2932,20 @@ Error ResourceFormatSaverGDScript::set_uid(const String &p_path, ResourceUID::ID
}
lines.write[uid_idx.x] = GDScript::create_uid_line(ResourceUID::get_singleton()->id_to_text(p_uid));
} else {
f->store_line(GDScript::create_uid_line(ResourceUID::get_singleton()->id_to_text(p_uid)));
lines.insert(0, GDScript::create_uid_line(ResourceUID::get_singleton()->id_to_text(p_uid)));
}

const String source = String("\n").join(lines);
f->store_string(source);
f->close();

Ref<GDScript> existing = ResourceCache::get_ref(p_path);
if (existing.is_valid()) {
existing->set_last_modified_time(FileAccess::get_modified_time(p_path));
existing->set_source_code(source);
existing->reload();
existing->emit_changed();
}
f->store_string(String("\n").join(lines));

return OK;
}
1 change: 1 addition & 0 deletions modules/gdscript/gdscript.h
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@ class GDScriptLanguage : public ScriptLanguage {
#endif
virtual bool supports_builtin_mode() const override;
virtual bool supports_documentation() const override;
virtual bool supports_uid() const override;
virtual bool can_inherit_from_file() const override { return true; }
virtual int find_function(const String &p_function, const String &p_code) const override;
virtual String make_function(const String &p_class, const String &p_name, const PackedStringArray &p_args) const override;
Expand Down
4 changes: 4 additions & 0 deletions modules/gdscript/gdscript_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ bool GDScriptLanguage::supports_documentation() const {
return true;
}

bool GDScriptLanguage::supports_uid() const {
return true;
}

int GDScriptLanguage::find_function(const String &p_function, const String &p_code) const {
GDScriptTokenizer tokenizer;
tokenizer.set_source_code(p_code);
Expand Down
5 changes: 2 additions & 3 deletions modules/gdscript/tests/test_gdscript_uid.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ static void test_script(const String &p_source, const String &p_target_source) {
script.instantiate();
script->set_source_code(p_source);
ResourceSaver::save(script, script_path);
ResourceSaver::set_uid(script_path, _resource_saver_get_resource_id_for_path(script_path, true));

Ref<FileAccess> fa = FileAccess::open(script_path, FileAccess::READ);
CHECK_EQ(fa->get_as_text(), p_target_source);
Expand Down Expand Up @@ -100,11 +101,9 @@ extends Node
class_name TestClass
)";

// Script has wrong UID saved. Remove it and add a correct one.
// Set script UID, replacing the old one.
// Inserts in the same line, but multiline annotations are flattened.
test_script(wrong_id_source, corrected_id_source);
// The stored UID is correct, so do not modify it.
test_script(correct_id_source, correct_id_source);
}
}

Expand Down