| 1 | /**************************************************************************/ |
| 2 | /* editor_plugin_settings.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 "editor_plugin_settings.h" |
| 32 | |
| 33 | #include "core/config/project_settings.h" |
| 34 | #include "core/io/config_file.h" |
| 35 | #include "core/io/dir_access.h" |
| 36 | #include "core/io/file_access.h" |
| 37 | #include "core/os/main_loop.h" |
| 38 | #include "editor/editor_node.h" |
| 39 | #include "editor/editor_scale.h" |
| 40 | #include "scene/gui/margin_container.h" |
| 41 | #include "scene/gui/tree.h" |
| 42 | |
| 43 | void EditorPluginSettings::_notification(int p_what) { |
| 44 | switch (p_what) { |
| 45 | case NOTIFICATION_WM_WINDOW_FOCUS_IN: { |
| 46 | update_plugins(); |
| 47 | } break; |
| 48 | |
| 49 | case Node::NOTIFICATION_READY: { |
| 50 | plugin_config_dialog->connect("plugin_ready" , callable_mp(EditorNode::get_singleton(), &EditorNode::_on_plugin_ready)); |
| 51 | plugin_list->connect("button_clicked" , callable_mp(this, &EditorPluginSettings::_cell_button_pressed)); |
| 52 | } break; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | void EditorPluginSettings::update_plugins() { |
| 57 | plugin_list->clear(); |
| 58 | updating = true; |
| 59 | TreeItem *root = plugin_list->create_item(); |
| 60 | |
| 61 | Vector<String> plugins = _get_plugins("res://addons" ); |
| 62 | plugins.sort(); |
| 63 | |
| 64 | for (int i = 0; i < plugins.size(); i++) { |
| 65 | Ref<ConfigFile> cf; |
| 66 | cf.instantiate(); |
| 67 | const String path = plugins[i]; |
| 68 | |
| 69 | Error err2 = cf->load(path); |
| 70 | |
| 71 | if (err2 != OK) { |
| 72 | WARN_PRINT("Can't load plugin config: " + path); |
| 73 | } else { |
| 74 | bool key_missing = false; |
| 75 | |
| 76 | if (!cf->has_section_key("plugin" , "name" )) { |
| 77 | WARN_PRINT("Plugin config misses \"plugin/name\" key: " + path); |
| 78 | key_missing = true; |
| 79 | } |
| 80 | if (!cf->has_section_key("plugin" , "author" )) { |
| 81 | WARN_PRINT("Plugin config misses \"plugin/author\" key: " + path); |
| 82 | key_missing = true; |
| 83 | } |
| 84 | if (!cf->has_section_key("plugin" , "version" )) { |
| 85 | WARN_PRINT("Plugin config misses \"plugin/version\" key: " + path); |
| 86 | key_missing = true; |
| 87 | } |
| 88 | if (!cf->has_section_key("plugin" , "description" )) { |
| 89 | WARN_PRINT("Plugin config misses \"plugin/description\" key: " + path); |
| 90 | key_missing = true; |
| 91 | } |
| 92 | if (!cf->has_section_key("plugin" , "script" )) { |
| 93 | WARN_PRINT("Plugin config misses \"plugin/script\" key: " + path); |
| 94 | key_missing = true; |
| 95 | } |
| 96 | |
| 97 | if (!key_missing) { |
| 98 | String name = cf->get_value("plugin" , "name" ); |
| 99 | String author = cf->get_value("plugin" , "author" ); |
| 100 | String version = cf->get_value("plugin" , "version" ); |
| 101 | String description = cf->get_value("plugin" , "description" ); |
| 102 | String scr = cf->get_value("plugin" , "script" ); |
| 103 | |
| 104 | const PackedInt32Array boundaries = TS->string_get_word_breaks(description, "" , 80); |
| 105 | String wrapped_description; |
| 106 | |
| 107 | for (int j = 0; j < boundaries.size(); j += 2) { |
| 108 | const int start = boundaries[j]; |
| 109 | const int end = boundaries[j + 1]; |
| 110 | wrapped_description += "\n" + description.substr(start, end - start + 1).rstrip("\n" ); |
| 111 | } |
| 112 | |
| 113 | TreeItem *item = plugin_list->create_item(root); |
| 114 | item->set_text(0, name); |
| 115 | item->set_tooltip_text(0, TTR("Name:" ) + " " + name + "\n" + TTR("Path:" ) + " " + path + "\n" + TTR("Main Script:" ) + " " + scr + "\n" + TTR("Description:" ) + " " + wrapped_description); |
| 116 | item->set_metadata(0, path); |
| 117 | item->set_text(1, version); |
| 118 | item->set_metadata(1, scr); |
| 119 | item->set_text(2, author); |
| 120 | item->set_metadata(2, description); |
| 121 | item->set_cell_mode(3, TreeItem::CELL_MODE_CHECK); |
| 122 | item->set_text(3, TTR("Enable" )); |
| 123 | bool is_active = EditorNode::get_singleton()->is_addon_plugin_enabled(path); |
| 124 | item->set_checked(3, is_active); |
| 125 | item->set_editable(3, true); |
| 126 | item->add_button(4, get_editor_theme_icon(SNAME("Edit" )), BUTTON_PLUGIN_EDIT, false, TTR("Edit Plugin" )); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | updating = false; |
| 132 | } |
| 133 | |
| 134 | void EditorPluginSettings::_plugin_activity_changed() { |
| 135 | if (updating) { |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | TreeItem *ti = plugin_list->get_edited(); |
| 140 | ERR_FAIL_NULL(ti); |
| 141 | bool active = ti->is_checked(3); |
| 142 | String name = ti->get_metadata(0); |
| 143 | |
| 144 | EditorNode::get_singleton()->set_addon_plugin_enabled(name, active, true); |
| 145 | |
| 146 | bool is_active = EditorNode::get_singleton()->is_addon_plugin_enabled(name); |
| 147 | |
| 148 | if (is_active != active) { |
| 149 | updating = true; |
| 150 | ti->set_checked(3, is_active); |
| 151 | updating = false; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | void EditorPluginSettings::_create_clicked() { |
| 156 | plugin_config_dialog->config("" ); |
| 157 | plugin_config_dialog->popup_centered(); |
| 158 | } |
| 159 | |
| 160 | void EditorPluginSettings::_cell_button_pressed(Object *p_item, int p_column, int p_id, MouseButton p_button) { |
| 161 | if (p_button != MouseButton::LEFT) { |
| 162 | return; |
| 163 | } |
| 164 | TreeItem *item = Object::cast_to<TreeItem>(p_item); |
| 165 | if (!item) { |
| 166 | return; |
| 167 | } |
| 168 | if (p_id == BUTTON_PLUGIN_EDIT) { |
| 169 | if (p_column == 4) { |
| 170 | String dir = item->get_metadata(0); |
| 171 | plugin_config_dialog->config(dir); |
| 172 | plugin_config_dialog->popup_centered(); |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | Vector<String> EditorPluginSettings::_get_plugins(const String &p_dir) { |
| 178 | Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES); |
| 179 | Error err = da->change_dir(p_dir); |
| 180 | if (err != OK) { |
| 181 | return Vector<String>(); |
| 182 | } |
| 183 | |
| 184 | Vector<String> plugins; |
| 185 | da->list_dir_begin(); |
| 186 | for (String path = da->get_next(); !path.is_empty(); path = da->get_next()) { |
| 187 | if (path[0] == '.' || !da->current_is_dir()) { |
| 188 | continue; |
| 189 | } |
| 190 | |
| 191 | const String full_path = p_dir.path_join(path); |
| 192 | const String plugin_config = full_path.path_join("plugin.cfg" ); |
| 193 | if (FileAccess::exists(plugin_config)) { |
| 194 | plugins.push_back(plugin_config); |
| 195 | } else { |
| 196 | plugins.append_array(_get_plugins(full_path)); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | da->list_dir_end(); |
| 201 | return plugins; |
| 202 | } |
| 203 | |
| 204 | void EditorPluginSettings::_bind_methods() { |
| 205 | } |
| 206 | |
| 207 | EditorPluginSettings::EditorPluginSettings() { |
| 208 | ProjectSettings::get_singleton()->add_hidden_prefix("editor_plugins/" ); |
| 209 | |
| 210 | plugin_config_dialog = memnew(PluginConfigDialog); |
| 211 | plugin_config_dialog->config("" ); |
| 212 | add_child(plugin_config_dialog); |
| 213 | |
| 214 | HBoxContainer *title_hb = memnew(HBoxContainer); |
| 215 | Label *l = memnew(Label(TTR("Installed Plugins:" ))); |
| 216 | l->set_theme_type_variation("HeaderSmall" ); |
| 217 | title_hb->add_child(l); |
| 218 | title_hb->add_spacer(); |
| 219 | Button *create_plugin = memnew(Button(TTR("Create New Plugin" ))); |
| 220 | create_plugin->connect("pressed" , callable_mp(this, &EditorPluginSettings::_create_clicked)); |
| 221 | title_hb->add_child(create_plugin); |
| 222 | add_child(title_hb); |
| 223 | |
| 224 | plugin_list = memnew(Tree); |
| 225 | plugin_list->set_v_size_flags(SIZE_EXPAND_FILL); |
| 226 | plugin_list->set_columns(5); |
| 227 | plugin_list->set_column_titles_visible(true); |
| 228 | plugin_list->set_column_title(0, TTR("Name" )); |
| 229 | plugin_list->set_column_title(1, TTR("Version" )); |
| 230 | plugin_list->set_column_title(2, TTR("Author" )); |
| 231 | plugin_list->set_column_title(3, TTR("Status" )); |
| 232 | plugin_list->set_column_title(4, TTR("Edit" )); |
| 233 | plugin_list->set_column_expand(0, true); |
| 234 | plugin_list->set_column_clip_content(0, true); |
| 235 | plugin_list->set_column_expand(1, false); |
| 236 | plugin_list->set_column_clip_content(1, true); |
| 237 | plugin_list->set_column_expand(2, false); |
| 238 | plugin_list->set_column_clip_content(2, true); |
| 239 | plugin_list->set_column_expand(3, false); |
| 240 | plugin_list->set_column_clip_content(3, true); |
| 241 | plugin_list->set_column_expand(4, false); |
| 242 | plugin_list->set_column_clip_content(4, true); |
| 243 | plugin_list->set_column_custom_minimum_width(1, 100 * EDSCALE); |
| 244 | plugin_list->set_column_custom_minimum_width(2, 250 * EDSCALE); |
| 245 | plugin_list->set_column_custom_minimum_width(3, 80 * EDSCALE); |
| 246 | plugin_list->set_column_custom_minimum_width(4, 40 * EDSCALE); |
| 247 | plugin_list->set_hide_root(true); |
| 248 | plugin_list->connect("item_edited" , callable_mp(this, &EditorPluginSettings::_plugin_activity_changed), CONNECT_DEFERRED); |
| 249 | |
| 250 | VBoxContainer *mc = memnew(VBoxContainer); |
| 251 | mc->add_child(plugin_list); |
| 252 | mc->set_v_size_flags(SIZE_EXPAND_FILL); |
| 253 | mc->set_h_size_flags(SIZE_EXPAND_FILL); |
| 254 | |
| 255 | add_child(mc); |
| 256 | } |
| 257 | |