plugin_config_dialog.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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_file_system.h"
  35. #include "editor/editor_node.h"
  36. #include "editor/gui/editor_validation_panel.h"
  37. #include "editor/plugins/editor_plugin.h"
  38. #include "editor/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. emit_signal(SNAME("plugin_ready"), scr.ptr(), active_edit->is_pressed() ? _to_absolute_plugin_path(_get_subfolder()) : "");
  96. }
  97. }
  98. void PluginConfigDialog::_on_canceled() {
  99. _clear_fields();
  100. }
  101. void PluginConfigDialog::_on_required_text_changed() {
  102. if (name_edit->get_text().is_empty()) {
  103. validation_panel->set_message(MSG_ID_PLUGIN, TTR("Plugin name cannot be blank."), EditorValidationPanel::MSG_ERROR);
  104. }
  105. if (subfolder_edit->is_visible()) {
  106. if (!subfolder_edit->get_text().is_empty() && !subfolder_edit->get_text().is_valid_filename()) {
  107. validation_panel->set_message(MSG_ID_SUBFOLDER, TTR("Subfolder name is not a valid folder name."), EditorValidationPanel::MSG_ERROR);
  108. } else {
  109. String path = "res://addons/" + _get_subfolder();
  110. if (!_edit_mode && DirAccess::exists(path)) { // Only show this error if in "create" mode.
  111. validation_panel->set_message(MSG_ID_SUBFOLDER, TTR("Subfolder cannot be one which already exists."), EditorValidationPanel::MSG_ERROR);
  112. }
  113. }
  114. } else {
  115. validation_panel->set_message(MSG_ID_SUBFOLDER, "", EditorValidationPanel::MSG_OK);
  116. }
  117. // Language and script validation.
  118. int lang_idx = script_option_edit->get_selected();
  119. ScriptLanguage *language = ScriptServer::get_language(lang_idx);
  120. if (language == nullptr) {
  121. return;
  122. }
  123. String ext = language->get_extension();
  124. if ((!script_edit->get_text().get_extension().is_empty() && script_edit->get_text().get_extension() != ext) || script_edit->get_text().ends_with(".")) {
  125. validation_panel->set_message(MSG_ID_SCRIPT, vformat(TTR("Script extension must match chosen language extension (.%s)."), ext), EditorValidationPanel::MSG_ERROR);
  126. }
  127. if (active_edit->is_visible()) {
  128. if (language->get_name() == "C#") {
  129. active_edit->set_pressed(false);
  130. active_edit->set_disabled(true);
  131. validation_panel->set_message(MSG_ID_ACTIVE, TTR("C# doesn't support activating the plugin on creation because the project must be built first."), EditorValidationPanel::MSG_WARNING);
  132. } else {
  133. active_edit->set_disabled(false);
  134. }
  135. }
  136. }
  137. String PluginConfigDialog::_get_subfolder() {
  138. return subfolder_edit->get_text().is_empty() ? name_edit->get_text().replace(" ", "_").to_lower() : subfolder_edit->get_text();
  139. }
  140. String PluginConfigDialog::_to_absolute_plugin_path(const String &p_plugin_name) {
  141. return "res://addons/" + p_plugin_name + "/plugin.cfg";
  142. }
  143. void PluginConfigDialog::_notification(int p_what) {
  144. switch (p_what) {
  145. case NOTIFICATION_VISIBILITY_CHANGED: {
  146. if (is_visible()) {
  147. name_edit->grab_focus();
  148. }
  149. } break;
  150. case NOTIFICATION_READY: {
  151. connect(SceneStringName(confirmed), callable_mp(this, &PluginConfigDialog::_on_confirmed));
  152. get_cancel_button()->connect(SceneStringName(pressed), callable_mp(this, &PluginConfigDialog::_on_canceled));
  153. } break;
  154. }
  155. }
  156. void PluginConfigDialog::config(const String &p_config_path) {
  157. if (!p_config_path.is_empty()) {
  158. Ref<ConfigFile> cf = memnew(ConfigFile);
  159. Error err = cf->load(p_config_path);
  160. ERR_FAIL_COND_MSG(err != OK, "Cannot load config file from path '" + p_config_path + "'.");
  161. name_edit->set_text(cf->get_value("plugin", "name", ""));
  162. subfolder_edit->set_text(p_config_path.get_base_dir().get_file());
  163. desc_edit->set_text(cf->get_value("plugin", "description", ""));
  164. author_edit->set_text(cf->get_value("plugin", "author", ""));
  165. version_edit->set_text(cf->get_value("plugin", "version", ""));
  166. script_edit->set_text(cf->get_value("plugin", "script", ""));
  167. _edit_mode = true;
  168. set_title(TTR("Edit a Plugin"));
  169. } else {
  170. _clear_fields();
  171. _edit_mode = false;
  172. set_title(TTR("Create a Plugin"));
  173. }
  174. for (Control *control : plugin_edit_hidden_controls) {
  175. control->set_visible(!_edit_mode);
  176. }
  177. validation_panel->update();
  178. get_ok_button()->set_disabled(!_edit_mode);
  179. set_ok_button_text(_edit_mode ? TTR("Update") : TTR("Create"));
  180. }
  181. void PluginConfigDialog::_bind_methods() {
  182. ADD_SIGNAL(MethodInfo("plugin_ready", PropertyInfo(Variant::STRING, "script_path", PROPERTY_HINT_NONE, ""), PropertyInfo(Variant::STRING, "activate_name")));
  183. }
  184. PluginConfigDialog::PluginConfigDialog() {
  185. get_ok_button()->set_disabled(true);
  186. set_hide_on_ok(true);
  187. VBoxContainer *vbox = memnew(VBoxContainer);
  188. vbox->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  189. vbox->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  190. add_child(vbox);
  191. GridContainer *grid = memnew(GridContainer);
  192. grid->set_columns(2);
  193. grid->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  194. vbox->add_child(grid);
  195. // Plugin Name
  196. Label *name_lb = memnew(Label);
  197. name_lb->set_text(TTR("Plugin Name:"));
  198. name_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  199. grid->add_child(name_lb);
  200. name_edit = memnew(LineEdit);
  201. name_edit->set_placeholder("MyPlugin");
  202. name_edit->set_tooltip_text(TTR("Required. This name will be displayed in the list of plugins."));
  203. name_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  204. grid->add_child(name_edit);
  205. // Subfolder
  206. Label *subfolder_lb = memnew(Label);
  207. subfolder_lb->set_text(TTR("Subfolder:"));
  208. subfolder_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  209. grid->add_child(subfolder_lb);
  210. plugin_edit_hidden_controls.push_back(subfolder_lb);
  211. subfolder_edit = memnew(LineEdit);
  212. subfolder_edit->set_placeholder("\"my_plugin\" -> res://addons/my_plugin");
  213. 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`."));
  214. subfolder_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  215. grid->add_child(subfolder_edit);
  216. plugin_edit_hidden_controls.push_back(subfolder_edit);
  217. // Description
  218. Label *desc_lb = memnew(Label);
  219. desc_lb->set_text(TTR("Description:"));
  220. desc_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  221. grid->add_child(desc_lb);
  222. desc_edit = memnew(TextEdit);
  223. 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."));
  224. desc_edit->set_custom_minimum_size(Size2(400, 80) * EDSCALE);
  225. desc_edit->set_line_wrapping_mode(TextEdit::LineWrappingMode::LINE_WRAPPING_BOUNDARY);
  226. desc_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  227. desc_edit->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  228. grid->add_child(desc_edit);
  229. // Author
  230. Label *author_lb = memnew(Label);
  231. author_lb->set_text(TTR("Author:"));
  232. author_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  233. grid->add_child(author_lb);
  234. author_edit = memnew(LineEdit);
  235. author_edit->set_placeholder("Godette");
  236. author_edit->set_tooltip_text(TTR("Optional. The author's username, full name, or organization name."));
  237. author_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  238. grid->add_child(author_edit);
  239. // Version
  240. Label *version_lb = memnew(Label);
  241. version_lb->set_text(TTR("Version:"));
  242. version_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  243. grid->add_child(version_lb);
  244. version_edit = memnew(LineEdit);
  245. version_edit->set_tooltip_text(TTR("Optional. A human-readable version identifier used for informational purposes only."));
  246. version_edit->set_placeholder("1.0");
  247. version_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  248. grid->add_child(version_edit);
  249. // Language dropdown
  250. Label *script_option_lb = memnew(Label);
  251. script_option_lb->set_text(TTR("Language:"));
  252. script_option_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  253. grid->add_child(script_option_lb);
  254. script_option_edit = memnew(OptionButton);
  255. 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."));
  256. int default_lang = 0;
  257. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  258. ScriptLanguage *lang = ScriptServer::get_language(i);
  259. script_option_edit->add_item(lang->get_name());
  260. if (lang->get_name() == "GDScript") {
  261. default_lang = i;
  262. }
  263. }
  264. script_option_edit->select(default_lang);
  265. grid->add_child(script_option_edit);
  266. // Plugin Script Name
  267. Label *script_name_label = memnew(Label);
  268. script_name_label->set_text(TTR("Script Name:"));
  269. script_name_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  270. grid->add_child(script_name_label);
  271. script_edit = memnew(LineEdit);
  272. script_edit->set_tooltip_text(TTR("Optional. The path to the script (relative to the add-on folder). If left empty, will default to \"plugin.gd\"."));
  273. script_edit->set_placeholder("\"plugin.gd\" -> res://addons/my_plugin/plugin.gd");
  274. script_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  275. grid->add_child(script_edit);
  276. // Activate now checkbox
  277. Label *active_label = memnew(Label);
  278. active_label->set_text(TTR("Activate now?"));
  279. active_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  280. grid->add_child(active_label);
  281. plugin_edit_hidden_controls.push_back(active_label);
  282. active_edit = memnew(CheckBox);
  283. active_edit->set_pressed(true);
  284. grid->add_child(active_edit);
  285. plugin_edit_hidden_controls.push_back(active_edit);
  286. Control *spacing = memnew(Control);
  287. vbox->add_child(spacing);
  288. spacing->set_custom_minimum_size(Size2(0, 10 * EDSCALE));
  289. validation_panel = memnew(EditorValidationPanel);
  290. vbox->add_child(validation_panel);
  291. validation_panel->add_line(MSG_ID_PLUGIN, TTR("Plugin name is valid."));
  292. validation_panel->add_line(MSG_ID_SCRIPT, TTR("Script extension is valid."));
  293. validation_panel->add_line(MSG_ID_SUBFOLDER, TTR("Subfolder name is valid."));
  294. validation_panel->add_line(MSG_ID_ACTIVE, "");
  295. validation_panel->set_update_callback(callable_mp(this, &PluginConfigDialog::_on_required_text_changed));
  296. validation_panel->set_accept_button(get_ok_button());
  297. script_option_edit->connect(SceneStringName(item_selected), callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1));
  298. name_edit->connect(SceneStringName(text_changed), callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1));
  299. subfolder_edit->connect(SceneStringName(text_changed), callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1));
  300. script_edit->connect(SceneStringName(text_changed), callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1));
  301. }
  302. PluginConfigDialog::~PluginConfigDialog() {
  303. }