plugin_config_dialog.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. #include "plugin_config_dialog.h"
  31. #include "core/io/config_file.h"
  32. #include "core/io/dir_access.h"
  33. #include "core/object/script_language.h"
  34. #include "editor/editor_node.h"
  35. #include "editor/file_system/editor_file_system.h"
  36. #include "editor/gui/editor_validation_panel.h"
  37. #include "editor/plugins/editor_plugin.h"
  38. #include "editor/settings/project_settings_editor.h"
  39. #include "editor/themes/editor_scale.h"
  40. #include "scene/gui/grid_container.h"
  41. void PluginConfigDialog::_clear_fields() {
  42. name_edit->set_text("");
  43. subfolder_edit->set_text("");
  44. desc_edit->set_text("");
  45. author_edit->set_text("");
  46. version_edit->set_text("");
  47. script_edit->set_text("");
  48. }
  49. void PluginConfigDialog::_on_confirmed() {
  50. String path = "res://addons/" + _get_subfolder();
  51. if (!_edit_mode) {
  52. Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  53. if (d.is_null() || d->make_dir_recursive(path) != OK) {
  54. return;
  55. }
  56. }
  57. // Create the plugin.cfg file.
  58. Ref<ConfigFile> cf = memnew(ConfigFile);
  59. cf->load(path.path_join("plugin.cfg"));
  60. cf->set_value("plugin", "name", name_edit->get_text());
  61. cf->set_value("plugin", "description", desc_edit->get_text());
  62. cf->set_value("plugin", "author", author_edit->get_text());
  63. cf->set_value("plugin", "version", version_edit->get_text());
  64. // Language-specific settings.
  65. int lang_index = script_option_edit->get_selected();
  66. _create_script_for_plugin(path, cf, lang_index);
  67. // Save and inform the editor.
  68. cf->save(path.path_join("plugin.cfg"));
  69. EditorNode::get_singleton()->get_project_settings()->update_plugins();
  70. EditorFileSystem::get_singleton()->scan();
  71. _clear_fields();
  72. }
  73. void PluginConfigDialog::_create_script_for_plugin(const String &p_plugin_path, Ref<ConfigFile> p_config_file, int p_script_lang_index) {
  74. ScriptLanguage *language = ScriptServer::get_language(p_script_lang_index);
  75. ERR_FAIL_COND(language == nullptr);
  76. String ext = language->get_extension();
  77. String script_name = script_edit->get_text().is_empty() ? _get_subfolder() : script_edit->get_text();
  78. if (script_name.get_extension() != ext) {
  79. script_name += "." + ext;
  80. }
  81. String script_path = p_plugin_path.path_join(script_name);
  82. p_config_file->set_value("plugin", "script", script_name);
  83. // If the requested script does not exist, create it.
  84. if (!_edit_mode && !FileAccess::exists(script_path)) {
  85. String class_name = script_name.get_basename();
  86. String template_content = "";
  87. Vector<ScriptLanguage::ScriptTemplate> templates = language->get_built_in_templates("EditorPlugin");
  88. if (!templates.is_empty()) {
  89. template_content = templates[0].content;
  90. }
  91. Ref<Script> scr = language->make_template(template_content, class_name, "EditorPlugin");
  92. scr->set_path(script_path, true);
  93. ResourceSaver::save(scr);
  94. p_config_file->save(p_plugin_path.path_join("plugin.cfg"));
  95. }
  96. }
  97. void PluginConfigDialog::_on_canceled() {
  98. _clear_fields();
  99. }
  100. void PluginConfigDialog::_on_required_text_changed() {
  101. if (name_edit->get_text().is_empty()) {
  102. validation_panel->set_message(MSG_ID_PLUGIN, TTR("Plugin name cannot be blank."), EditorValidationPanel::MSG_ERROR);
  103. }
  104. if (subfolder_edit->is_visible()) {
  105. if (!subfolder_edit->get_text().is_empty() && !subfolder_edit->get_text().is_valid_filename()) {
  106. validation_panel->set_message(MSG_ID_SUBFOLDER, TTR("Subfolder name is not a valid folder name."), EditorValidationPanel::MSG_ERROR);
  107. } else {
  108. String path = "res://addons/" + _get_subfolder();
  109. if (!_edit_mode && DirAccess::exists(path)) { // Only show this error if in "create" mode.
  110. validation_panel->set_message(MSG_ID_SUBFOLDER, TTR("Subfolder cannot be one which already exists."), EditorValidationPanel::MSG_ERROR);
  111. }
  112. }
  113. } else {
  114. validation_panel->set_message(MSG_ID_SUBFOLDER, "", EditorValidationPanel::MSG_OK);
  115. }
  116. // Language and script validation.
  117. int lang_idx = script_option_edit->get_selected();
  118. ScriptLanguage *language = ScriptServer::get_language(lang_idx);
  119. if (language == nullptr) {
  120. return;
  121. }
  122. String ext = language->get_extension();
  123. if ((!script_edit->get_text().get_extension().is_empty() && script_edit->get_text().get_extension() != ext) || script_edit->get_text().ends_with(".")) {
  124. validation_panel->set_message(MSG_ID_SCRIPT, vformat(TTR("Script extension must match chosen language extension (.%s)."), ext), EditorValidationPanel::MSG_ERROR);
  125. }
  126. if (language->get_name() == "GDScript") {
  127. 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);
  128. }
  129. }
  130. String PluginConfigDialog::_get_subfolder() {
  131. return subfolder_edit->get_text().is_empty() ? name_edit->get_text().replace_char(' ', '_').to_lower() : subfolder_edit->get_text();
  132. }
  133. String PluginConfigDialog::_to_absolute_plugin_path(const String &p_plugin_name) {
  134. return "res://addons/" + p_plugin_name + "/plugin.cfg";
  135. }
  136. void PluginConfigDialog::_notification(int p_what) {
  137. switch (p_what) {
  138. case NOTIFICATION_VISIBILITY_CHANGED: {
  139. if (is_visible()) {
  140. name_edit->grab_focus();
  141. }
  142. } break;
  143. case NOTIFICATION_READY: {
  144. connect(SceneStringName(confirmed), callable_mp(this, &PluginConfigDialog::_on_confirmed));
  145. get_cancel_button()->connect(SceneStringName(pressed), callable_mp(this, &PluginConfigDialog::_on_canceled));
  146. } break;
  147. }
  148. }
  149. void PluginConfigDialog::config(const String &p_config_path) {
  150. if (!p_config_path.is_empty()) {
  151. Ref<ConfigFile> cf = memnew(ConfigFile);
  152. Error err = cf->load(p_config_path);
  153. ERR_FAIL_COND_MSG(err != OK, "Cannot load config file from path '" + p_config_path + "'.");
  154. name_edit->set_text(cf->get_value("plugin", "name", ""));
  155. subfolder_edit->set_text(p_config_path.get_base_dir().get_file());
  156. desc_edit->set_text(cf->get_value("plugin", "description", ""));
  157. author_edit->set_text(cf->get_value("plugin", "author", ""));
  158. version_edit->set_text(cf->get_value("plugin", "version", ""));
  159. script_edit->set_text(cf->get_value("plugin", "script", ""));
  160. _edit_mode = true;
  161. set_title(TTR("Edit a Plugin"));
  162. } else {
  163. _clear_fields();
  164. _edit_mode = false;
  165. set_title(TTR("Create a Plugin"));
  166. }
  167. for (Control *control : plugin_edit_hidden_controls) {
  168. control->set_visible(!_edit_mode);
  169. }
  170. validation_panel->update();
  171. get_ok_button()->set_disabled(!_edit_mode);
  172. set_ok_button_text(_edit_mode ? TTR("Update") : TTR("Create"));
  173. }
  174. void PluginConfigDialog::_bind_methods() {
  175. ADD_SIGNAL(MethodInfo("plugin_ready", PropertyInfo(Variant::STRING, "script_path", PROPERTY_HINT_NONE, ""), PropertyInfo(Variant::STRING, "activate_name")));
  176. }
  177. PluginConfigDialog::PluginConfigDialog() {
  178. get_ok_button()->set_disabled(true);
  179. set_hide_on_ok(true);
  180. VBoxContainer *vbox = memnew(VBoxContainer);
  181. vbox->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  182. vbox->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  183. add_child(vbox);
  184. GridContainer *grid = memnew(GridContainer);
  185. grid->set_columns(2);
  186. grid->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  187. vbox->add_child(grid);
  188. // Plugin Name
  189. Label *name_lb = memnew(Label);
  190. name_lb->set_text(TTR("Plugin Name:"));
  191. name_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  192. grid->add_child(name_lb);
  193. name_edit = memnew(LineEdit);
  194. name_edit->set_placeholder("MyPlugin");
  195. name_edit->set_tooltip_text(TTR("Required. This name will be displayed in the list of plugins."));
  196. name_edit->set_accessibility_name(TTRC("Plugin Name:"));
  197. name_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  198. grid->add_child(name_edit);
  199. // Subfolder
  200. Label *subfolder_lb = memnew(Label);
  201. subfolder_lb->set_text(TTR("Subfolder:"));
  202. subfolder_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  203. grid->add_child(subfolder_lb);
  204. plugin_edit_hidden_controls.push_back(subfolder_lb);
  205. subfolder_edit = memnew(LineEdit);
  206. subfolder_edit->set_placeholder(U"\"my_plugin\" → res://addons/my_plugin");
  207. 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`."));
  208. subfolder_edit->set_accessibility_name(TTRC("Subfolder:"));
  209. subfolder_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  210. grid->add_child(subfolder_edit);
  211. plugin_edit_hidden_controls.push_back(subfolder_edit);
  212. // Description
  213. Label *desc_lb = memnew(Label);
  214. desc_lb->set_text(TTR("Description:"));
  215. desc_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  216. grid->add_child(desc_lb);
  217. desc_edit = memnew(TextEdit);
  218. 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."));
  219. desc_edit->set_accessibility_name(TTRC("Description:"));
  220. desc_edit->set_custom_minimum_size(Size2(400, 80) * EDSCALE);
  221. desc_edit->set_line_wrapping_mode(TextEdit::LineWrappingMode::LINE_WRAPPING_BOUNDARY);
  222. desc_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  223. desc_edit->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  224. grid->add_child(desc_edit);
  225. // Author
  226. Label *author_lb = memnew(Label);
  227. author_lb->set_text(TTR("Author:"));
  228. author_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  229. grid->add_child(author_lb);
  230. author_edit = memnew(LineEdit);
  231. author_edit->set_placeholder("Godette");
  232. author_edit->set_accessibility_name(TTRC("Author:"));
  233. author_edit->set_tooltip_text(TTR("Optional. The author's username, full name, or organization name."));
  234. author_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  235. grid->add_child(author_edit);
  236. // Version
  237. Label *version_lb = memnew(Label);
  238. version_lb->set_text(TTR("Version:"));
  239. version_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  240. grid->add_child(version_lb);
  241. version_edit = memnew(LineEdit);
  242. version_edit->set_tooltip_text(TTR("Optional. A human-readable version identifier used for informational purposes only."));
  243. version_edit->set_placeholder("1.0");
  244. version_edit->set_accessibility_name(TTRC("Version:"));
  245. version_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  246. grid->add_child(version_edit);
  247. // Language dropdown
  248. Label *script_option_lb = memnew(Label);
  249. script_option_lb->set_text(TTR("Language:"));
  250. script_option_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  251. grid->add_child(script_option_lb);
  252. script_option_edit = memnew(OptionButton);
  253. 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."));
  254. script_option_edit->set_accessibility_name(TTRC("Language:"));
  255. int default_lang = 0;
  256. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  257. ScriptLanguage *lang = ScriptServer::get_language(i);
  258. script_option_edit->add_item(lang->get_name());
  259. if (lang->get_name() == "GDScript") {
  260. default_lang = i;
  261. }
  262. }
  263. script_option_edit->select(default_lang);
  264. grid->add_child(script_option_edit);
  265. // Plugin Script Name
  266. Label *script_name_label = memnew(Label);
  267. script_name_label->set_text(TTR("Script Name:"));
  268. script_name_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  269. grid->add_child(script_name_label);
  270. script_edit = memnew(LineEdit);
  271. script_edit->set_tooltip_text(TTR("Optional. The name of the script file. If left empty, will default to the subfolder name."));
  272. script_edit->set_placeholder(U"\"plugin.gd\" → res://addons/my_plugin/plugin.gd");
  273. script_edit->set_accessibility_name(TTRC("Script Name:"));
  274. script_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  275. grid->add_child(script_edit);
  276. Control *spacing = memnew(Control);
  277. vbox->add_child(spacing);
  278. spacing->set_custom_minimum_size(Size2(0, 10 * EDSCALE));
  279. validation_panel = memnew(EditorValidationPanel);
  280. vbox->add_child(validation_panel);
  281. validation_panel->add_line(MSG_ID_PLUGIN, TTR("Plugin name is valid."));
  282. validation_panel->add_line(MSG_ID_SCRIPT, TTR("Script extension is valid."));
  283. validation_panel->add_line(MSG_ID_SUBFOLDER, TTR("Subfolder name is valid."));
  284. validation_panel->add_line(MSG_ID_ACTIVE, "");
  285. validation_panel->add_line(MSG_ID_ENABLE_WARNINGS, "");
  286. validation_panel->set_update_callback(callable_mp(this, &PluginConfigDialog::_on_required_text_changed));
  287. validation_panel->set_accept_button(get_ok_button());
  288. script_option_edit->connect(SceneStringName(item_selected), callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1));
  289. name_edit->connect(SceneStringName(text_changed), callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1));
  290. subfolder_edit->connect(SceneStringName(text_changed), callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1));
  291. script_edit->connect(SceneStringName(text_changed), callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1));
  292. }