mirror of
				https://github.com/godotengine/godot.git
				synced 2025-10-31 13:41:03 +00:00 
			
		
		
		
	Merge pull request #85271 from YuriSizov/core-check-please!
Correctly check scripts that must inherit `EditorPlugin`
This commit is contained in:
		
						commit
						3e7cc5ec32
					
				
					 7 changed files with 23 additions and 28 deletions
				
			
		|  | @ -1113,11 +1113,10 @@ bool ResourceLoader::add_custom_resource_format_loader(String script_path) { | ||||||
| 	Ref<Script> s = res; | 	Ref<Script> s = res; | ||||||
| 	StringName ibt = s->get_instance_base_type(); | 	StringName ibt = s->get_instance_base_type(); | ||||||
| 	bool valid_type = ClassDB::is_parent_class(ibt, "ResourceFormatLoader"); | 	bool valid_type = ClassDB::is_parent_class(ibt, "ResourceFormatLoader"); | ||||||
| 	ERR_FAIL_COND_V_MSG(!valid_type, false, "Script does not inherit a CustomResourceLoader: " + script_path + "."); | 	ERR_FAIL_COND_V_MSG(!valid_type, false, vformat("Failed to add a custom resource loader, script '%s' does not inherit 'ResourceFormatLoader'.", script_path)); | ||||||
| 
 | 
 | ||||||
| 	Object *obj = ClassDB::instantiate(ibt); | 	Object *obj = ClassDB::instantiate(ibt); | ||||||
| 
 | 	ERR_FAIL_NULL_V_MSG(obj, false, vformat("Failed to add a custom resource loader, cannot instantiate '%s'.", ibt)); | ||||||
| 	ERR_FAIL_NULL_V_MSG(obj, false, "Cannot instance script as custom resource loader, expected 'ResourceFormatLoader' inheritance, got: " + String(ibt) + "."); |  | ||||||
| 
 | 
 | ||||||
| 	Ref<ResourceFormatLoader> crl = Object::cast_to<ResourceFormatLoader>(obj); | 	Ref<ResourceFormatLoader> crl = Object::cast_to<ResourceFormatLoader>(obj); | ||||||
| 	crl->set_script(s); | 	crl->set_script(s); | ||||||
|  |  | ||||||
|  | @ -237,11 +237,10 @@ bool ResourceSaver::add_custom_resource_format_saver(String script_path) { | ||||||
| 	Ref<Script> s = res; | 	Ref<Script> s = res; | ||||||
| 	StringName ibt = s->get_instance_base_type(); | 	StringName ibt = s->get_instance_base_type(); | ||||||
| 	bool valid_type = ClassDB::is_parent_class(ibt, "ResourceFormatSaver"); | 	bool valid_type = ClassDB::is_parent_class(ibt, "ResourceFormatSaver"); | ||||||
| 	ERR_FAIL_COND_V_MSG(!valid_type, false, "Script does not inherit a CustomResourceSaver: " + script_path + "."); | 	ERR_FAIL_COND_V_MSG(!valid_type, false, vformat("Failed to add a custom resource saver, script '%s' does not inherit 'ResourceFormatSaver'.", script_path)); | ||||||
| 
 | 
 | ||||||
| 	Object *obj = ClassDB::instantiate(ibt); | 	Object *obj = ClassDB::instantiate(ibt); | ||||||
| 
 | 	ERR_FAIL_NULL_V_MSG(obj, false, vformat("Failed to add a custom resource saver, cannot instantiate '%s'.", ibt)); | ||||||
| 	ERR_FAIL_NULL_V_MSG(obj, false, "Cannot instance script as custom resource saver, expected 'ResourceFormatSaver' inheritance, got: " + String(ibt) + "."); |  | ||||||
| 
 | 
 | ||||||
| 	Ref<ResourceFormatSaver> crl = Object::cast_to<ResourceFormatSaver>(obj); | 	Ref<ResourceFormatSaver> crl = Object::cast_to<ResourceFormatSaver>(obj); | ||||||
| 	crl->set_script(s); | 	crl->set_script(s); | ||||||
|  |  | ||||||
|  | @ -407,31 +407,30 @@ Node *EditorAutoloadSettings::_create_autoload(const String &p_path) { | ||||||
| 		scn.instantiate(); | 		scn.instantiate(); | ||||||
| 		scn->set_path(p_path); | 		scn->set_path(p_path); | ||||||
| 		scn->reload_from_file(); | 		scn->reload_from_file(); | ||||||
| 		ERR_FAIL_COND_V_MSG(!scn.is_valid(), nullptr, vformat("Can't autoload: %s.", p_path)); | 		ERR_FAIL_COND_V_MSG(!scn.is_valid(), nullptr, vformat("Failed to create an autoload, can't load from path: %s.", p_path)); | ||||||
| 
 | 
 | ||||||
| 		if (scn.is_valid()) { | 		if (scn.is_valid()) { | ||||||
| 			n = scn->instantiate(); | 			n = scn->instantiate(); | ||||||
| 		} | 		} | ||||||
| 	} else { | 	} else { | ||||||
| 		Ref<Resource> res = ResourceLoader::load(p_path); | 		Ref<Resource> res = ResourceLoader::load(p_path); | ||||||
| 		ERR_FAIL_COND_V_MSG(res.is_null(), nullptr, vformat("Can't autoload: %s.", p_path)); | 		ERR_FAIL_COND_V_MSG(res.is_null(), nullptr, vformat("Failed to create an autoload, can't load from path: %s.", p_path)); | ||||||
| 
 | 
 | ||||||
| 		Ref<Script> scr = res; | 		Ref<Script> scr = res; | ||||||
| 		if (scr.is_valid()) { | 		if (scr.is_valid()) { | ||||||
| 			StringName ibt = scr->get_instance_base_type(); | 			StringName ibt = scr->get_instance_base_type(); | ||||||
| 			bool valid_type = ClassDB::is_parent_class(ibt, "Node"); | 			bool valid_type = ClassDB::is_parent_class(ibt, "Node"); | ||||||
| 			ERR_FAIL_COND_V_MSG(!valid_type, nullptr, vformat("Script does not inherit from Node: %s.", p_path)); | 			ERR_FAIL_COND_V_MSG(!valid_type, nullptr, vformat("Failed to create an autoload, script '%s' does not inherit from 'Node'.", p_path)); | ||||||
| 
 | 
 | ||||||
| 			Object *obj = ClassDB::instantiate(ibt); | 			Object *obj = ClassDB::instantiate(ibt); | ||||||
| 
 | 			ERR_FAIL_NULL_V_MSG(obj, nullptr, vformat("Failed to create an autoload, cannot instantiate '%s'.", ibt)); | ||||||
| 			ERR_FAIL_NULL_V_MSG(obj, nullptr, vformat("Cannot instance script for Autoload, expected 'Node' inheritance, got: %s.", ibt)); |  | ||||||
| 
 | 
 | ||||||
| 			n = Object::cast_to<Node>(obj); | 			n = Object::cast_to<Node>(obj); | ||||||
| 			n->set_script(scr); | 			n->set_script(scr); | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	ERR_FAIL_NULL_V_MSG(n, nullptr, vformat("Path in Autoload not a node or script: %s.", p_path)); | 	ERR_FAIL_NULL_V_MSG(n, nullptr, vformat("Failed to create an autoload, path is not pointing to a scene or a script: %s.", p_path)); | ||||||
| 
 | 
 | ||||||
| 	return n; | 	return n; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -3459,13 +3459,13 @@ void EditorNode::set_addon_plugin_enabled(const String &p_addon, bool p_enabled, | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		// Plugin init scripts must inherit from EditorPlugin and be tools.
 | 		// Plugin init scripts must inherit from EditorPlugin and be tools.
 | ||||||
| 		if (String(scr->get_instance_base_type()) != "EditorPlugin") { | 		if (!ClassDB::is_parent_class(scr->get_instance_base_type(), "EditorPlugin")) { | ||||||
| 			show_warning(vformat(TTR("Unable to load addon script from path: '%s' Base type is not EditorPlugin."), script_path)); | 			show_warning(vformat(TTR("Unable to load addon script from path: '%s'. Base type is not 'EditorPlugin'."), script_path)); | ||||||
| 			return; | 			return; | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		if (!scr->is_tool()) { | 		if (!scr->is_tool()) { | ||||||
| 			show_warning(vformat(TTR("Unable to load addon script from path: '%s' Script is not in tool mode."), script_path)); | 			show_warning(vformat(TTR("Unable to load addon script from path: '%s'. Script is not in tool mode."), script_path)); | ||||||
| 			return; | 			return; | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | @ -1650,7 +1650,7 @@ void ScriptTextEditor::reload(bool p_soft) { | ||||||
| 		return; | 		return; | ||||||
| 	} | 	} | ||||||
| 	scr->set_source_code(te->get_text()); | 	scr->set_source_code(te->get_text()); | ||||||
| 	bool soft = p_soft || scr->get_instance_base_type() == "EditorPlugin"; // Always soft-reload editor plugins.
 | 	bool soft = p_soft || ClassDB::is_parent_class(scr->get_instance_base_type(), "EditorPlugin"); // Always soft-reload editor plugins.
 | ||||||
| 
 | 
 | ||||||
| 	scr->get_language()->reload_tool_script(scr, soft); | 	scr->get_language()->reload_tool_script(scr, soft); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -3192,31 +3192,30 @@ bool Main::start() { | ||||||
| 						scn.instantiate(); | 						scn.instantiate(); | ||||||
| 						scn->set_path(info.path); | 						scn->set_path(info.path); | ||||||
| 						scn->reload_from_file(); | 						scn->reload_from_file(); | ||||||
| 						ERR_CONTINUE_MSG(!scn.is_valid(), vformat("Can't autoload: %s.", info.path)); | 						ERR_CONTINUE_MSG(!scn.is_valid(), vformat("Failed to instantiate an autoload, can't load from path: %s.", info.path)); | ||||||
| 
 | 
 | ||||||
| 						if (scn.is_valid()) { | 						if (scn.is_valid()) { | ||||||
| 							n = scn->instantiate(); | 							n = scn->instantiate(); | ||||||
| 						} | 						} | ||||||
| 					} else { | 					} else { | ||||||
| 						Ref<Resource> res = ResourceLoader::load(info.path); | 						Ref<Resource> res = ResourceLoader::load(info.path); | ||||||
| 						ERR_CONTINUE_MSG(res.is_null(), vformat("Can't autoload: %s.", info.path)); | 						ERR_CONTINUE_MSG(res.is_null(), vformat("Failed to instantiate an autoload, can't load from path: %s.", info.path)); | ||||||
| 
 | 
 | ||||||
| 						Ref<Script> script_res = res; | 						Ref<Script> script_res = res; | ||||||
| 						if (script_res.is_valid()) { | 						if (script_res.is_valid()) { | ||||||
| 							StringName ibt = script_res->get_instance_base_type(); | 							StringName ibt = script_res->get_instance_base_type(); | ||||||
| 							bool valid_type = ClassDB::is_parent_class(ibt, "Node"); | 							bool valid_type = ClassDB::is_parent_class(ibt, "Node"); | ||||||
| 							ERR_CONTINUE_MSG(!valid_type, vformat("Script does not inherit from Node: %s.", info.path)); | 							ERR_CONTINUE_MSG(!valid_type, vformat("Failed to instantiate an autoload, script '%s' does not inherit from 'Node'.", info.path)); | ||||||
| 
 | 
 | ||||||
| 							Object *obj = ClassDB::instantiate(ibt); | 							Object *obj = ClassDB::instantiate(ibt); | ||||||
| 
 | 							ERR_CONTINUE_MSG(!obj, vformat("Failed to instantiate an autoload, cannot instantiate '%s'.", ibt)); | ||||||
| 							ERR_CONTINUE_MSG(!obj, vformat("Cannot instance script for autoload, expected 'Node' inheritance, got: %s.")); |  | ||||||
| 
 | 
 | ||||||
| 							n = Object::cast_to<Node>(obj); | 							n = Object::cast_to<Node>(obj); | ||||||
| 							n->set_script(script_res); | 							n->set_script(script_res); | ||||||
| 						} | 						} | ||||||
| 					} | 					} | ||||||
| 
 | 
 | ||||||
| 					ERR_CONTINUE_MSG(!n, vformat("Path in autoload not a node or script: %s.", info.path)); | 					ERR_CONTINUE_MSG(!n, vformat("Failed to instantiate an autoload, path is not pointing to a scene or a script: %s.", info.path)); | ||||||
| 					n->set_name(info.name); | 					n->set_name(info.name); | ||||||
| 
 | 
 | ||||||
| 					//defer so references are all valid on _ready()
 | 					//defer so references are all valid on _ready()
 | ||||||
|  |  | ||||||
|  | @ -78,31 +78,30 @@ void init_autoloads() { | ||||||
| 			scn.instantiate(); | 			scn.instantiate(); | ||||||
| 			scn->set_path(info.path); | 			scn->set_path(info.path); | ||||||
| 			scn->reload_from_file(); | 			scn->reload_from_file(); | ||||||
| 			ERR_CONTINUE_MSG(!scn.is_valid(), vformat("Can't autoload: %s.", info.path)); | 			ERR_CONTINUE_MSG(!scn.is_valid(), vformat("Failed to instantiate an autoload, can't load from path: %s.", info.path)); | ||||||
| 
 | 
 | ||||||
| 			if (scn.is_valid()) { | 			if (scn.is_valid()) { | ||||||
| 				n = scn->instantiate(); | 				n = scn->instantiate(); | ||||||
| 			} | 			} | ||||||
| 		} else { | 		} else { | ||||||
| 			Ref<Resource> res = ResourceLoader::load(info.path); | 			Ref<Resource> res = ResourceLoader::load(info.path); | ||||||
| 			ERR_CONTINUE_MSG(res.is_null(), vformat("Can't autoload: %s.", info.path)); | 			ERR_CONTINUE_MSG(res.is_null(), vformat("Failed to instantiate an autoload, can't load from path: %s.", info.path)); | ||||||
| 
 | 
 | ||||||
| 			Ref<Script> scr = res; | 			Ref<Script> scr = res; | ||||||
| 			if (scr.is_valid()) { | 			if (scr.is_valid()) { | ||||||
| 				StringName ibt = scr->get_instance_base_type(); | 				StringName ibt = scr->get_instance_base_type(); | ||||||
| 				bool valid_type = ClassDB::is_parent_class(ibt, "Node"); | 				bool valid_type = ClassDB::is_parent_class(ibt, "Node"); | ||||||
| 				ERR_CONTINUE_MSG(!valid_type, vformat("Script does not inherit from Node: %s.", info.path)); | 				ERR_CONTINUE_MSG(!valid_type, vformat("Failed to instantiate an autoload, script '%s' does not inherit from 'Node'.", info.path)); | ||||||
| 
 | 
 | ||||||
| 				Object *obj = ClassDB::instantiate(ibt); | 				Object *obj = ClassDB::instantiate(ibt); | ||||||
| 
 | 				ERR_CONTINUE_MSG(!obj, vformat("Failed to instantiate an autoload, cannot instantiate '%s'.", ibt)); | ||||||
| 				ERR_CONTINUE_MSG(!obj, vformat("Cannot instance script for Autoload, expected 'Node' inheritance, got: %s.", ibt)); |  | ||||||
| 
 | 
 | ||||||
| 				n = Object::cast_to<Node>(obj); | 				n = Object::cast_to<Node>(obj); | ||||||
| 				n->set_script(scr); | 				n->set_script(scr); | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		ERR_CONTINUE_MSG(!n, vformat("Path in autoload not a node or script: %s.", info.path)); | 		ERR_CONTINUE_MSG(!n, vformat("Failed to instantiate an autoload, path is not pointing to a scene or a script: %s.", info.path)); | ||||||
| 		n->set_name(info.name); | 		n->set_name(info.name); | ||||||
| 
 | 
 | ||||||
| 		for (int i = 0; i < ScriptServer::get_language_count(); i++) { | 		for (int i = 0; i < ScriptServer::get_language_count(); i++) { | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Rémi Verschelde
						Rémi Verschelde