Преглед на файлове

Properly rename scenes and resources after renaming or moving files, should fix #13976
It's not tested, so please test.

Juan Linietsky преди 7 години
родител
ревизия
57061413eb
променени са 4 файла, в които са добавени 66 реда и са изтрити 0 реда
  1. 9 0
      editor/editor_data.cpp
  2. 1 0
      editor/editor_data.h
  3. 55 0
      editor/filesystem_dock.cpp
  4. 1 0
      editor/filesystem_dock.h

+ 9 - 0
editor/editor_data.cpp

@@ -701,6 +701,15 @@ String EditorData::get_scene_title(int p_idx) const {
 	return name;
 }
 
+void EditorData::set_scene_path(int p_idx, const String &p_path) {
+
+	ERR_FAIL_INDEX(p_idx, edited_scene.size());
+
+	if (!edited_scene[p_idx].root)
+		return;
+	edited_scene[p_idx].root->set_filename(p_path);
+}
+
 String EditorData::get_scene_path(int p_idx) const {
 
 	ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), String());

+ 1 - 0
editor/editor_data.h

@@ -185,6 +185,7 @@ public:
 	String get_scene_title(int p_idx) const;
 	String get_scene_path(int p_idx) const;
 	String get_scene_type(int p_idx) const;
+	void set_scene_path(int p_idx, const String &p_path);
 	Ref<Script> get_scene_root_script(int p_idx) const;
 	void set_edited_scene_version(uint64_t version, int p_scene_idx = -1);
 	uint64_t get_edited_scene_version() const;

+ 55 - 0
editor/filesystem_dock.cpp

@@ -834,6 +834,58 @@ void FileSystemDock::_try_duplicate_item(const FileOrFolder &p_item, const Strin
 	memdelete(da);
 }
 
+void FileSystemDock::_update_resource_paths_after_move(const Map<String, String> &p_renames) const {
+
+	//Rename all resources loaded, be it subresources or actual resources
+	List<Ref<Resource> > cached;
+	ResourceCache::get_cached_resources(&cached);
+
+	for (List<Ref<Resource> >::Element *E = cached.front(); E; E = E->next()) {
+
+		Ref<Resource> r = E->get();
+
+		String base_path = r->get_path();
+		String extra_path;
+		int sep_pos = r->get_path().find("::");
+		if (sep_pos >= 0) {
+			extra_path = base_path.substr(sep_pos, base_path.length());
+			base_path = base_path.substr(0, sep_pos);
+		}
+
+		if (p_renames.has(base_path)) {
+			base_path = p_renames[base_path];
+		}
+
+		r->set_path(base_path + extra_path);
+	}
+
+	for (int i = 0; i < EditorNode::get_editor_data().get_edited_scene_count(); i++) {
+
+		String path;
+		if (i == EditorNode::get_editor_data().get_edited_scene()) {
+			if (!get_tree()->get_edited_scene_root())
+				continue;
+
+			path = get_tree()->get_edited_scene_root()->get_filename();
+		} else {
+
+			path = EditorNode::get_editor_data().get_scene_path(i);
+		}
+
+		if (p_renames.has(path)) {
+			path = p_renames[path];
+		}
+
+		if (i == EditorNode::get_editor_data().get_edited_scene()) {
+
+			get_tree()->get_edited_scene_root()->set_filename(path);
+		} else {
+
+			EditorNode::get_editor_data().set_scene_path(i, path);
+		}
+	}
+}
+
 void FileSystemDock::_update_dependencies_after_move(const Map<String, String> &p_renames) const {
 	//The following code assumes that the following holds:
 	// 1) EditorFileSystem contains the old paths/folder structure from before the rename/move.
@@ -910,6 +962,7 @@ void FileSystemDock::_rename_operation_confirm() {
 	Map<String, String> renames;
 	_try_move_item(to_rename, new_path, renames);
 	_update_dependencies_after_move(renames);
+	_update_resource_paths_after_move(renames);
 
 	//Rescan everything
 	print_line("call rescan!");
@@ -959,6 +1012,8 @@ void FileSystemDock::_move_operation_confirm(const String &p_to_path) {
 	}
 
 	_update_dependencies_after_move(renames);
+	_update_resource_paths_after_move(renames);
+
 	print_line("call rescan!");
 	_rescan();
 }

+ 1 - 0
editor/filesystem_dock.h

@@ -178,6 +178,7 @@ private:
 	void _try_move_item(const FileOrFolder &p_item, const String &p_new_path, Map<String, String> &p_renames) const;
 	void _try_duplicate_item(const FileOrFolder &p_item, const String &p_new_path) const;
 	void _update_dependencies_after_move(const Map<String, String> &p_renames) const;
+	void _update_resource_paths_after_move(const Map<String, String> &p_renames) const;
 
 	void _make_dir_confirm();
 	void _rename_operation_confirm();