瀏覽代碼

Merge pull request #44981 from RandomShaper/fix_res_lifetime_3.2

Fix cases of resources destroyed too early (3.2)
Rémi Verschelde 4 年之前
父節點
當前提交
2a928ea880

+ 3 - 3
editor/create_dialog.cpp

@@ -515,7 +515,7 @@ String CreateDialog::get_selected_type() {
 		return String();
 }
 
-Object *CreateDialog::instance_selected() {
+Variant CreateDialog::instance_selected() {
 
 	TreeItem *selected = search_options->get_selected();
 
@@ -529,7 +529,7 @@ Object *CreateDialog::instance_selected() {
 
 		if (custom != String()) {
 			if (ScriptServer::is_global_class(custom)) {
-				Object *obj = EditorNode::get_editor_data().script_class_instance(custom);
+				Variant obj = EditorNode::get_editor_data().script_class_instance(custom);
 				Node *n = Object::cast_to<Node>(obj);
 				if (n)
 					n->set_name(custom);
@@ -541,7 +541,7 @@ Object *CreateDialog::instance_selected() {
 		}
 	}
 
-	return NULL;
+	return Variant();
 }
 
 void CreateDialog::_item_selected() {

+ 1 - 1
editor/create_dialog.h

@@ -97,7 +97,7 @@ protected:
 	void _save_and_update_favorite_list();
 
 public:
-	Object *instance_selected();
+	Variant instance_selected();
 	String get_selected_type();
 
 	void set_base_type(const String &p_base);

+ 12 - 11
editor/editor_data.cpp

@@ -497,7 +497,7 @@ void EditorData::add_custom_type(const String &p_type, const String &p_inherits,
 	custom_types[p_inherits].push_back(ct);
 }
 
-Object *EditorData::instance_custom_type(const String &p_type, const String &p_inherits) {
+Variant EditorData::instance_custom_type(const String &p_type, const String &p_inherits) {
 
 	if (get_custom_types().has(p_inherits)) {
 
@@ -505,18 +505,19 @@ Object *EditorData::instance_custom_type(const String &p_type, const String &p_i
 			if (get_custom_types()[p_inherits][i].name == p_type) {
 				Ref<Script> script = get_custom_types()[p_inherits][i].script;
 
-				Object *ob = ClassDB::instance(p_inherits);
-				ERR_FAIL_COND_V(!ob, NULL);
-				if (ob->is_class("Node")) {
-					ob->call("set_name", p_type);
+				Variant ob = ClassDB::instance(p_inherits);
+				ERR_FAIL_COND_V(!ob, Variant());
+				Node *n = Object::cast_to<Node>(ob);
+				if (n) {
+					n->set_name(p_type);
 				}
-				ob->set_script(script.get_ref_ptr());
+				((Object *)ob)->set_script(script.get_ref_ptr());
 				return ob;
 			}
 		}
 	}
 
-	return NULL;
+	return Variant();
 }
 
 void EditorData::remove_custom_type(const String &p_type) {
@@ -905,17 +906,17 @@ StringName EditorData::script_class_get_base(const String &p_class) const {
 	return script->get_language()->get_global_class_name(base_script->get_path());
 }
 
-Object *EditorData::script_class_instance(const String &p_class) {
+Variant EditorData::script_class_instance(const String &p_class) {
 	if (ScriptServer::is_global_class(p_class)) {
-		Object *obj = ClassDB::instance(ScriptServer::get_global_class_native_base(p_class));
+		Variant obj = ClassDB::instance(ScriptServer::get_global_class_native_base(p_class));
 		if (obj) {
 			Ref<Script> script = script_class_load_script(p_class);
 			if (script.is_valid())
-				obj->set_script(script.get_ref_ptr());
+				((Object *)obj)->set_script(script.get_ref_ptr());
 			return obj;
 		}
 	}
-	return NULL;
+	return Variant();
 }
 
 Ref<Script> EditorData::script_class_load_script(const String &p_class) const {

+ 2 - 2
editor/editor_data.h

@@ -179,7 +179,7 @@ public:
 	void restore_editor_global_states();
 
 	void add_custom_type(const String &p_type, const String &p_inherits, const Ref<Script> &p_script, const Ref<Texture> &p_icon);
-	Object *instance_custom_type(const String &p_type, const String &p_inherits);
+	Variant instance_custom_type(const String &p_type, const String &p_inherits);
 	void remove_custom_type(const String &p_type);
 	const Map<String, Vector<CustomType> > &get_custom_types() const { return custom_types; }
 
@@ -217,7 +217,7 @@ public:
 
 	bool script_class_is_parent(const String &p_class, const String &p_inherits);
 	StringName script_class_get_base(const String &p_class) const;
-	Object *script_class_instance(const String &p_class);
+	Variant script_class_instance(const String &p_class);
 
 	Ref<Script> script_class_load_script(const String &p_class) const;
 

+ 6 - 9
editor/editor_properties.cpp

@@ -2342,35 +2342,32 @@ void EditorPropertyResource::_menu_option(int p_which) {
 				return;
 			}
 
-			Object *obj = NULL;
-			RES res_temp;
+			Variant obj;
 
 			if (ScriptServer::is_global_class(intype)) {
 				obj = ClassDB::instance(ScriptServer::get_global_class_native_base(intype));
 				if (obj) {
-					res_temp = obj;
 					Ref<Script> script = ResourceLoader::load(ScriptServer::get_global_class_path(intype));
 					if (script.is_valid()) {
-						obj->set_script(Variant(script));
+						((Object *)obj)->set_script(script.get_ref_ptr());
 					}
 				}
 			} else {
 				obj = ClassDB::instance(intype);
-				res_temp = obj;
 			}
 
 			if (!obj) {
 				obj = EditorNode::get_editor_data().instance_custom_type(intype, "Resource");
-				res_temp = obj;
 			}
 
-			ERR_BREAK(!res_temp.is_valid());
+			Resource *resp = Object::cast_to<Resource>(obj);
+			ERR_BREAK(!resp);
 			if (get_edited_object() && base_type != String() && base_type == "Script") {
 				//make visual script the right type
-				res_temp->call("set_instance_base_type", get_edited_object()->get_class());
+				resp->call("set_instance_base_type", get_edited_object()->get_class());
 			}
 
-			res = res_temp;
+			res = RES(resp);
 			emit_changed(get_edited_property(), res);
 			update_property();
 

+ 3 - 6
editor/filesystem_dock.cpp

@@ -1781,7 +1781,7 @@ void FileSystemDock::_file_option(int p_option, const Vector<String> &p_selected
 }
 
 void FileSystemDock::_resource_created() const {
-	Object *c = new_resource_dialog->instance_selected();
+	Variant c = new_resource_dialog->instance_selected();
 
 	ERR_FAIL_COND(!c);
 	Resource *r = Object::cast_to<Resource>(c);
@@ -1795,17 +1795,14 @@ void FileSystemDock::_resource_created() const {
 		memdelete(node);
 	}
 
-	REF res(r);
-	editor->push_item(c);
-
-	RES current_res = RES(r);
+	editor->push_item(r);
 
 	String fpath = path;
 	if (!fpath.ends_with("/")) {
 		fpath = fpath.get_base_dir();
 	}
 
-	editor->save_resource_as(current_res, fpath);
+	editor->save_resource_as(RES(r), fpath);
 }
 
 void FileSystemDock::_search_changed(const String &p_text, const Control *p_from) {

+ 2 - 3
editor/inspector_dock.cpp

@@ -269,14 +269,13 @@ void InspectorDock::_select_history(int p_idx) const {
 }
 
 void InspectorDock::_resource_created() const {
-	Object *c = new_resource_dialog->instance_selected();
+	Variant c = new_resource_dialog->instance_selected();
 
 	ERR_FAIL_COND(!c);
 	Resource *r = Object::cast_to<Resource>(c);
 	ERR_FAIL_COND(!r);
 
-	REF res(r);
-	editor->push_item(c);
+	editor->push_item(r);
 }
 
 void InspectorDock::_resource_selected(const RES &p_res, const String &p_property) const {

+ 8 - 11
editor/property_editor.cpp

@@ -293,7 +293,7 @@ void CustomPropertyEditor::_menu_option(int p_which) {
 						return;
 					}
 
-					Object *obj = ClassDB::instance(intype);
+					Variant obj = ClassDB::instance(intype);
 
 					if (!obj) {
 						if (ScriptServer::is_global_class(intype)) {
@@ -311,7 +311,7 @@ void CustomPropertyEditor::_menu_option(int p_which) {
 						res->call("set_instance_base_type", owner->get_class());
 					}
 
-					v = Ref<Resource>(res).get_ref_ptr();
+					v = obj;
 					emit_signal("variant_changed");
 
 				} break;
@@ -1153,7 +1153,7 @@ void CustomPropertyEditor::_type_create_selected(int p_idx) {
 
 		String intype = inheritors_array[p_idx];
 
-		Object *obj = ClassDB::instance(intype);
+		Variant obj = ClassDB::instance(intype);
 
 		if (!obj) {
 			if (ScriptServer::is_global_class(intype)) {
@@ -1164,11 +1164,9 @@ void CustomPropertyEditor::_type_create_selected(int p_idx) {
 		}
 
 		ERR_FAIL_COND(!obj);
+		ERR_FAIL_COND(!Object::cast_to<Resource>(obj));
 
-		Resource *res = Object::cast_to<Resource>(obj);
-		ERR_FAIL_COND(!res);
-
-		v = Ref<Resource>(res).get_ref_ptr();
+		v = obj;
 		emit_signal("variant_changed");
 		hide();
 	}
@@ -1359,7 +1357,7 @@ void CustomPropertyEditor::_action_pressed(int p_which) {
 
 				if (hint == PROPERTY_HINT_RESOURCE_TYPE) {
 
-					Object *obj = ClassDB::instance(intype);
+					Variant obj = ClassDB::instance(intype);
 
 					if (!obj) {
 						if (ScriptServer::is_global_class(intype)) {
@@ -1370,10 +1368,9 @@ void CustomPropertyEditor::_action_pressed(int p_which) {
 					}
 
 					ERR_BREAK(!obj);
-					Resource *res = Object::cast_to<Resource>(obj);
-					ERR_BREAK(!res);
+					ERR_BREAK(!Object::cast_to<Resource>(obj));
 
-					v = Ref<Resource>(res).get_ref_ptr();
+					v = obj;
 					emit_signal("variant_changed");
 					hide();
 				}

+ 2 - 2
editor/scene_tree_dock.cpp

@@ -1968,7 +1968,7 @@ Node *SceneTreeDock::_get_selection_group_tail(Node *p_node, List<Node *> p_list
 }
 
 void SceneTreeDock::_do_create(Node *p_parent) {
-	Object *c = create_dialog->instance_selected();
+	Variant c = create_dialog->instance_selected();
 
 	ERR_FAIL_COND(!c);
 	Node *child = Object::cast_to<Node>(c);
@@ -2046,7 +2046,7 @@ void SceneTreeDock::_create() {
 			Node *n = E->get();
 			ERR_FAIL_COND(!n);
 
-			Object *c = create_dialog->instance_selected();
+			Variant c = create_dialog->instance_selected();
 
 			ERR_FAIL_COND(!c);
 			Node *newnode = Object::cast_to<Node>(c);