Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow plugin name in is_plugin_enabled #61820

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 25 additions & 15 deletions editor/editor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3306,33 +3306,39 @@ void EditorNode::_update_addon_config() {
}

void EditorNode::set_addon_plugin_enabled(const String &p_addon, bool p_enabled, bool p_config_changed) {
ERR_FAIL_COND(p_enabled && addon_name_to_plugin.has(p_addon));
ERR_FAIL_COND(!p_enabled && !addon_name_to_plugin.has(p_addon));
String addon_path = p_addon;

if (!addon_path.begins_with("res://")) {
addon_path = "res://addons/" + addon_path + "/plugin.cfg";
}

ERR_FAIL_COND(p_enabled && addon_name_to_plugin.has(addon_path));
ERR_FAIL_COND(!p_enabled && !addon_name_to_plugin.has(addon_path));

if (!p_enabled) {
EditorPlugin *addon = addon_name_to_plugin[p_addon];
EditorPlugin *addon = addon_name_to_plugin[addon_path];
remove_editor_plugin(addon, p_config_changed);
memdelete(addon);
addon_name_to_plugin.erase(p_addon);
addon_name_to_plugin.erase(addon_path);
_update_addon_config();
return;
}

Ref<ConfigFile> cf;
cf.instantiate();
if (!DirAccess::exists(p_addon.get_base_dir())) {
_remove_plugin_from_enabled(p_addon);
WARN_PRINT("Addon '" + p_addon + "' failed to load. No directory found. Removing from enabled plugins.");
if (!DirAccess::exists(addon_path.get_base_dir())) {
_remove_plugin_from_enabled(addon_path);
WARN_PRINT("Addon '" + addon_path + "' failed to load. No directory found. Removing from enabled plugins.");
return;
}
Error err = cf->load(p_addon);
Error err = cf->load(addon_path);
if (err != OK) {
show_warning(vformat(TTR("Unable to enable addon plugin at: '%s' parsing of config failed."), p_addon));
show_warning(vformat(TTR("Unable to enable addon plugin at: '%s' parsing of config failed."), addon_path));
return;
}

if (!cf->has_section_key("plugin", "script")) {
show_warning(vformat(TTR("Unable to find script field for addon plugin at: '%s'."), p_addon));
show_warning(vformat(TTR("Unable to find script field for addon plugin at: '%s'."), addon_path));
return;
}

Expand All @@ -3341,7 +3347,7 @@ void EditorNode::set_addon_plugin_enabled(const String &p_addon, bool p_enabled,

// Only try to load the script if it has a name. Else, the plugin has no init script.
if (script_path.length() > 0) {
script_path = p_addon.get_base_dir().plus_file(script_path);
script_path = addon_path.get_base_dir().plus_file(script_path);
script = ResourceLoader::load(script_path);

if (script.is_null()) {
Expand All @@ -3351,8 +3357,8 @@ void EditorNode::set_addon_plugin_enabled(const String &p_addon, bool p_enabled,

// Errors in the script cause the base_type to be an empty StringName.
if (script->get_instance_base_type() == StringName()) {
show_warning(vformat(TTR("Unable to load addon script from path: '%s'. This might be due to a code error in that script.\nDisabling the addon at '%s' to prevent further errors."), script_path, p_addon));
_remove_plugin_from_enabled(p_addon);
show_warning(vformat(TTR("Unable to load addon script from path: '%s'. This might be due to a code error in that script.\nDisabling the addon at '%s' to prevent further errors."), script_path, addon_path));
_remove_plugin_from_enabled(addon_path);
return;
}

Expand All @@ -3370,14 +3376,18 @@ void EditorNode::set_addon_plugin_enabled(const String &p_addon, bool p_enabled,

EditorPlugin *ep = memnew(EditorPlugin);
ep->set_script(script);
addon_name_to_plugin[p_addon] = ep;
addon_name_to_plugin[addon_path] = ep;
add_editor_plugin(ep, p_config_changed);

_update_addon_config();
}

bool EditorNode::is_addon_plugin_enabled(const String &p_addon) const {
return addon_name_to_plugin.has(p_addon);
if (p_addon.begins_with("res://")) {
return addon_name_to_plugin.has(p_addon);
}

return addon_name_to_plugin.has("res://addons/" + p_addon + "/plugin.cfg");
}

void EditorNode::_remove_edited_scene(bool p_change_tab) {
Expand Down