Debian Codegraph @

editor/plugins/plugin_config_dialog.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* plugin_config_dialog.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "plugin_config_dialog.h"
32
33 #include "core/io/config_file.h"
34 #include "core/io/dir_access.h"
35 #include "core/object/script_language.h"
36 #include "editor/editor_node.h"
37 #include "editor/file_system/editor_file_system.h"
38 #include "editor/gui/editor_validation_panel.h"
39 #include "editor/plugins/editor_plugin.h"
40 #include "editor/settings/project_settings_editor.h"
41 #include "editor/themes/editor_scale.h"
42 #include "scene/gui/grid_container.h"
43
44 void PluginConfigDialog::_clear_fields() {
45 name_edit->set_text("");
46 subfolder_edit->set_text("");
47 desc_edit->set_text("");
48 author_edit->set_text("");
49 version_edit->set_text("");
50 script_edit->set_text("");
51 }
52
53 void PluginConfigDialog::_on_confirmed() {
54 String path = "res://addons/" + _get_subfolder();
55
56 if (!_edit_mode) {
57 Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
58 if (d.is_null() || d->make_dir_recursive(path) != OK) {
59 return;
60 }
61 }
62 // Create the plugin.cfg file.
63 Ref<ConfigFile> cf = memnew(ConfigFile);
64 cf->load(path.path_join("plugin.cfg"));
65 cf->set_value("plugin", "name", name_edit->get_text());
66 cf->set_value("plugin", "description", desc_edit->get_text());
67 cf->set_value("plugin", "author", author_edit->get_text());
68 cf->set_value("plugin", "version", version_edit->get_text());
69 // Language-specific settings.
70 int lang_index = script_option_edit->get_selected();
71 _create_script_for_plugin(path, cf, lang_index);
72 // Save and inform the editor.
73 cf->save(path.path_join("plugin.cfg"));
74 EditorNode::get_singleton()->get_project_settings()->update_plugins();
75 EditorFileSystem::get_singleton()->scan();
76 _clear_fields();
77 }
78
79 void PluginConfigDialog::_create_script_for_plugin(const String &p_plugin_path, Ref<ConfigFile> p_config_file, int p_script_lang_index) {
80 ScriptLanguage *language = ScriptServer::get_language(p_script_lang_index);
81 ERR_FAIL_COND(language == nullptr);
82 String ext = language->get_extension();
83 String script_name = script_edit->get_text().is_empty() ? _get_subfolder() : script_edit->get_text();
84 if (script_name.get_extension() != ext) {
85 script_name += "." + ext;
86 }
87 String script_path = p_plugin_path.path_join(script_name);
88 p_config_file->set_value("plugin", "script", script_name);
89 // If the requested script does not exist, create it.
90 if (!_edit_mode && !FileAccess::exists(script_path)) {
91 String class_name = script_name.get_basename();
92 String template_content = "";
93 Vector<ScriptLanguage::ScriptTemplate> templates = language->get_built_in_templates("EditorPlugin");
94 if (!templates.is_empty()) {
95 template_content = templates[0].content;
96 }
97 Ref<Script> scr = language->make_template(template_content, class_name, "EditorPlugin");
98 scr->set_path(script_path, true);
99 ResourceSaver::save(scr);
100 p_config_file->save(p_plugin_path.path_join("plugin.cfg"));
101 }
102 }
103
104 void PluginConfigDialog::_on_canceled() {
105 _clear_fields();
106 }
107
108 void PluginConfigDialog::_on_required_text_changed() {
109 if (name_edit->get_text().is_empty()) {
110 validation_panel->set_message(MSG_ID_PLUGIN, TTR("Plugin name cannot be blank."), EditorValidationPanel::MSG_ERROR);
111 }
112 if (subfolder_edit->is_visible()) {
113 if (!subfolder_edit->get_text().is_empty() && !subfolder_edit->get_text().is_valid_filename()) {
114 validation_panel->set_message(MSG_ID_SUBFOLDER, TTR("Subfolder name is not a valid folder name."), EditorValidationPanel::MSG_ERROR);
115 } else {
116 String path = "res://addons/" + _get_subfolder();
117 if (!_edit_mode && DirAccess::exists(path)) { // Only show this error if in "create" mode.
118 validation_panel->set_message(MSG_ID_SUBFOLDER, TTR("Subfolder cannot be one which already exists."), EditorValidationPanel::MSG_ERROR);
119 }
120 }
121 } else {
122 validation_panel->set_message(MSG_ID_SUBFOLDER, "", EditorValidationPanel::MSG_OK);
123 }
124 // Language and script validation.
125 int lang_idx = script_option_edit->get_selected();
126 ScriptLanguage *language = ScriptServer::get_language(lang_idx);
127 if (language == nullptr) {
128 return;
129 }
130 String ext = language->get_extension();
131 if ((!script_edit->get_text().get_extension().is_empty() && script_edit->get_text().get_extension() != ext) || script_edit->get_text().ends_with(".")) {
132 validation_panel->set_message(MSG_ID_SCRIPT, vformat(TTR("Script extension must match chosen language extension (.%s)."), ext), EditorValidationPanel::MSG_ERROR);
133 }
134 if (language->get_name() == "GDScript") {
135 validation_panel->set_message(MSG_ID_ENABLE_WARNINGS, TTR("Consider enabling GDScript warnings for this plugin by adding an entry for it to the project setting Debug > GDScript > Warnings > Directory Rules."), EditorValidationPanel::MSG_INFO);
136 }
137 }
138
139 String PluginConfigDialog::_get_subfolder() {
140 return subfolder_edit->get_text().is_empty() ? name_edit->get_text().replace_char(' ', '_').to_lower() : subfolder_edit->get_text();
141 }
142
143 String PluginConfigDialog::_to_absolute_plugin_path(const String &p_plugin_name) {
144 return "res://addons/" + p_plugin_name + "/plugin.cfg";
145 }
146
147 void PluginConfigDialog::_notification(int p_what) {
148 switch (p_what) {
149 case NOTIFICATION_VISIBILITY_CHANGED: {
150 if (is_visible()) {
151 name_edit->grab_focus();
152 }
153 } break;
154
155 case NOTIFICATION_READY: {
156 connect(SceneStringName(confirmed), callable_mp(this, &PluginConfigDialog::_on_confirmed));
157 get_cancel_button()->connect(SceneStringName(pressed), callable_mp(this, &PluginConfigDialog::_on_canceled));
158 } break;
159 }
160 }
161
162 void PluginConfigDialog::config(const String &p_config_path) {
163 if (!p_config_path.is_empty()) {
164 Ref<ConfigFile> cf = memnew(ConfigFile);
165 Error err = cf->load(p_config_path);
166 ERR_FAIL_COND_MSG(err != OK, "Cannot load config file from path '" + p_config_path + "'.");
167
168 name_edit->set_text(cf->get_value("plugin", "name", ""));
169 subfolder_edit->set_text(p_config_path.get_base_dir().get_file());
170 desc_edit->set_text(cf->get_value("plugin", "description", ""));
171 author_edit->set_text(cf->get_value("plugin", "author", ""));
172 version_edit->set_text(cf->get_value("plugin", "version", ""));
173 script_edit->set_text(cf->get_value("plugin", "script", ""));
174
175 _edit_mode = true;
176 set_title(TTR("Edit a Plugin"));
177 } else {
178 _clear_fields();
179 _edit_mode = false;
180 set_title(TTR("Create a Plugin"));
181 }
182
183 for (Control *control : plugin_edit_hidden_controls) {
184 control->set_visible(!_edit_mode);
185 }
186
187 validation_panel->update();
188
189 get_ok_button()->set_disabled(!_edit_mode);
190 set_ok_button_text(_edit_mode ? TTR("Update") : TTR("Create"));
191 }
192
193 void PluginConfigDialog::_bind_methods() {
194 ADD_SIGNAL(MethodInfo("plugin_ready", PropertyInfo(Variant::STRING, "script_path", PROPERTY_HINT_NONE, ""), PropertyInfo(Variant::STRING, "activate_name")));
195 }
196
197 PluginConfigDialog::PluginConfigDialog() {
198 get_ok_button()->set_disabled(true);
199 set_hide_on_ok(true);
200
201 VBoxContainer *vbox = memnew(VBoxContainer);
202 vbox->set_h_size_flags(Control::SIZE_EXPAND_FILL);
203 vbox->set_v_size_flags(Control::SIZE_EXPAND_FILL);
204 add_child(vbox);
205
206 GridContainer *grid = memnew(GridContainer);
207 grid->set_columns(2);
208 grid->set_v_size_flags(Control::SIZE_EXPAND_FILL);
209 vbox->add_child(grid);
210
211 // Plugin Name
212 Label *name_lb = memnew(Label);
213 name_lb->set_text(TTR("Plugin Name:"));
214 name_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
215 grid->add_child(name_lb);
216
217 name_edit = memnew(LineEdit);
218 name_edit->set_placeholder("MyPlugin");
219 name_edit->set_tooltip_text(TTR("Required. This name will be displayed in the list of plugins."));
220 name_edit->set_accessibility_name(TTRC("Plugin Name:"));
221 name_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
222 grid->add_child(name_edit);
223
224 // Subfolder
225 Label *subfolder_lb = memnew(Label);
226 subfolder_lb->set_text(TTR("Subfolder:"));
227 subfolder_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
228 grid->add_child(subfolder_lb);
229 plugin_edit_hidden_controls.push_back(subfolder_lb);
230
231 subfolder_edit = memnew(LineEdit);
232 subfolder_edit->set_placeholder(U"\"my_plugin\" → res://addons/my_plugin");
233 subfolder_edit->set_tooltip_text(TTR("Optional. The folder name should generally use `snake_case` naming (avoid spaces and special characters).\nIf left empty, the folder will be named after the plugin name converted to `snake_case`."));
234 subfolder_edit->set_accessibility_name(TTRC("Subfolder:"));
235 subfolder_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
236 grid->add_child(subfolder_edit);
237 plugin_edit_hidden_controls.push_back(subfolder_edit);
238
239 // Description
240 Label *desc_lb = memnew(Label);
241 desc_lb->set_text(TTR("Description:"));
242 desc_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
243 grid->add_child(desc_lb);
244
245 desc_edit = memnew(TextEdit);
246 desc_edit->set_tooltip_text(TTR("Optional. This description should be kept relatively short (up to 5 lines).\nIt will display when hovering the plugin in the list of plugins."));
247 desc_edit->set_accessibility_name(TTRC("Description:"));
248 desc_edit->set_custom_minimum_size(Size2(400, 80) * EDSCALE);
249 desc_edit->set_line_wrapping_mode(TextEdit::LineWrappingMode::LINE_WRAPPING_BOUNDARY);
250 desc_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
251 desc_edit->set_v_size_flags(Control::SIZE_EXPAND_FILL);
252 grid->add_child(desc_edit);
253
254 // Author
255 Label *author_lb = memnew(Label);
256 author_lb->set_text(TTR("Author:"));
257 author_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
258 grid->add_child(author_lb);
259
260 author_edit = memnew(LineEdit);
261 author_edit->set_placeholder("Godette");
262 author_edit->set_accessibility_name(TTRC("Author:"));
263 author_edit->set_tooltip_text(TTR("Optional. The author's username, full name, or organization name."));
264 author_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
265 grid->add_child(author_edit);
266
267 // Version
268 Label *version_lb = memnew(Label);
269 version_lb->set_text(TTR("Version:"));
270 version_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
271 grid->add_child(version_lb);
272
273 version_edit = memnew(LineEdit);
274 version_edit->set_tooltip_text(TTR("Optional. A human-readable version identifier used for informational purposes only."));
275 version_edit->set_placeholder("1.0");
276 version_edit->set_accessibility_name(TTRC("Version:"));
277 version_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
278 grid->add_child(version_edit);
279
280 // Language dropdown
281 Label *script_option_lb = memnew(Label);
282 script_option_lb->set_text(TTR("Language:"));
283 script_option_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
284 grid->add_child(script_option_lb);
285
286 script_option_edit = memnew(OptionButton);
287 script_option_edit->set_tooltip_text(TTR("Required. The scripting language to use for the script.\nNote that a plugin may use several languages at once by adding more scripts to the plugin."));
288 script_option_edit->set_accessibility_name(TTRC("Language:"));
289 int default_lang = 0;
290 for (int i = 0; i < ScriptServer::get_language_count(); i++) {
291 ScriptLanguage *lang = ScriptServer::get_language(i);
292 script_option_edit->add_item(lang->get_name());
293 if (lang->get_name() == "GDScript") {
294 default_lang = i;
295 }
296 }
297 script_option_edit->select(default_lang);
298 grid->add_child(script_option_edit);
299
300 // Plugin Script Name
301 Label *script_name_label = memnew(Label);
302 script_name_label->set_text(TTR("Script Name:"));
303 script_name_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
304 grid->add_child(script_name_label);
305
306 script_edit = memnew(LineEdit);
307 script_edit->set_tooltip_text(TTR("Optional. The name of the script file. If left empty, will default to the subfolder name."));
308 script_edit->set_placeholder(U"\"plugin.gd\" → res://addons/my_plugin/plugin.gd");
309 script_edit->set_accessibility_name(TTRC("Script Name:"));
310 script_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
311 grid->add_child(script_edit);
312
313 Control *spacing = memnew(Control);
314 vbox->add_child(spacing);
315 spacing->set_custom_minimum_size(Size2(0, 10 * EDSCALE));
316
317 validation_panel = memnew(EditorValidationPanel);
318 vbox->add_child(validation_panel);
319 validation_panel->add_line(MSG_ID_PLUGIN, TTR("Plugin name is valid."));
320 validation_panel->add_line(MSG_ID_SCRIPT, TTR("Script extension is valid."));
321 validation_panel->add_line(MSG_ID_SUBFOLDER, TTR("Subfolder name is valid."));
322 validation_panel->add_line(MSG_ID_ACTIVE, "");
323 validation_panel->add_line(MSG_ID_ENABLE_WARNINGS, "");
324 validation_panel->set_update_callback(callable_mp(this, &PluginConfigDialog::_on_required_text_changed));
325 validation_panel->set_accept_button(get_ok_button());
326
327 script_option_edit->connect(SceneStringName(item_selected), callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1));
328 name_edit->connect(SceneStringName(text_changed), callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1));
329 subfolder_edit->connect(SceneStringName(text_changed), callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1));
330 script_edit->connect(SceneStringName(text_changed), callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1));
331 }