Skip to content

Commit

Permalink
Make script UIDs optional
Browse files Browse the repository at this point in the history
  • Loading branch information
KoBeWi committed Jan 29, 2024
1 parent fa48a51 commit d184b4c
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 35 deletions.
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
14 changes: 14 additions & 0 deletions editor/plugins/script_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1429,6 +1429,19 @@ 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()) {
ERR_FAIL_MSG("Can't insert UID, script is null.");
}
if (scr->is_built_in()) {
ERR_FAIL_MSG("Can't insert UID into built-in script.");
}

const ResourceUID::ID uid = ResourceSaver::get_resource_id_for_path(scr->get_path(), true);
ResourceSaver::set_uid(scr->get_path(), uid);
} break;

case FILE_CLOSE: {
if (current->is_unsaved()) {
_ask_close_current_unsaved_tab(current);
Expand Down Expand Up @@ -4005,6 +4018,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
47 changes: 12 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,18 @@ 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);

Ref<GDScript> existing = ResourceCache::get_ref(p_path);
if (existing.is_valid()) {
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

0 comments on commit d184b4c

Please sign in to comment.