Browse Source

Reimport now checks source path changes and imported files and their md5, fixes #14728

Juan Linietsky 7 years ago
parent
commit
988d29bdd8
3 changed files with 74 additions and 4 deletions
  1. 31 0
      core/os/file_access.cpp
  2. 1 0
      core/os/file_access.h
  3. 42 4
      editor/editor_file_system.cpp

+ 31 - 0
core/os/file_access.cpp

@@ -569,6 +569,37 @@ String FileAccess::get_md5(const String &p_file) {
 	return ret;
 	return ret;
 }
 }
 
 
+String FileAccess::get_multiple_md5(const Vector<String> &p_file) {
+
+	MD5_CTX md5;
+	MD5Init(&md5);
+
+	for (int i = 0; i < p_file.size(); i++) {
+		FileAccess *f = FileAccess::open(p_file[i], READ);
+		ERR_CONTINUE(!f);
+
+		unsigned char step[32768];
+
+		while (true) {
+
+			int br = f->get_buffer(step, 32768);
+			if (br > 0) {
+
+				MD5Update(&md5, step, br);
+			}
+			if (br < 4096)
+				break;
+		}
+		memdelete(f);
+	}
+
+	MD5Final(&md5);
+
+	String ret = String::md5(md5.digest);
+
+	return ret;
+}
+
 String FileAccess::get_sha256(const String &p_file) {
 String FileAccess::get_sha256(const String &p_file) {
 
 
 	FileAccess *f = FileAccess::open(p_file, READ);
 	FileAccess *f = FileAccess::open(p_file, READ);

+ 1 - 0
core/os/file_access.h

@@ -155,6 +155,7 @@ public:
 
 
 	static String get_md5(const String &p_file);
 	static String get_md5(const String &p_file);
 	static String get_sha256(const String &p_file);
 	static String get_sha256(const String &p_file);
+	static String get_multiple_md5(const Vector<String> &p_file);
 
 
 	static Vector<uint8_t> get_file_as_array(const String &p_path);
 	static Vector<uint8_t> get_file_as_array(const String &p_path);
 
 

+ 42 - 4
editor/editor_file_system.cpp

@@ -320,7 +320,10 @@ bool EditorFileSystem::_test_for_reimport(const String &p_path, bool p_only_impo
 
 
 	List<String> to_check;
 	List<String> to_check;
 
 
+	String source_file;
 	String source_md5;
 	String source_md5;
+	Vector<String> dest_files;
+	String dest_md5;
 
 
 	while (true) {
 	while (true) {
 
 
@@ -346,8 +349,16 @@ bool EditorFileSystem::_test_for_reimport(const String &p_path, bool p_only_impo
 				for (int i = 0; i < fa.size(); i++) {
 				for (int i = 0; i < fa.size(); i++) {
 					to_check.push_back(fa[i]);
 					to_check.push_back(fa[i]);
 				}
 				}
-			} else if (!p_only_imported_files && assign == "source_md5") {
-				source_md5 = value;
+			} else if (!p_only_imported_files) {
+				if (assign == "source_md5") {
+					source_md5 = value;
+				} else if (assign == "source_file") {
+					source_file = value;
+				} else if (assign == "dest_md5") {
+					dest_md5 = value;
+				} else if (assign == "dest_files") {
+					dest_files = value;
+				}
 			}
 			}
 
 
 		} else if (next_tag.name != "remap" && next_tag.name != "deps") {
 		} else if (next_tag.name != "remap" && next_tag.name != "deps") {
@@ -366,6 +377,11 @@ bool EditorFileSystem::_test_for_reimport(const String &p_path, bool p_only_impo
 
 
 	//check source md5 matching
 	//check source md5 matching
 	if (!p_only_imported_files) {
 	if (!p_only_imported_files) {
+
+		if (source_file != String() && source_file != p_path) {
+			return true; //file was moved, reimport
+		}
+
 		if (source_md5 == String()) {
 		if (source_md5 == String()) {
 			return true; //lacks md5, so just reimport
 			return true; //lacks md5, so just reimport
 		}
 		}
@@ -374,6 +390,13 @@ bool EditorFileSystem::_test_for_reimport(const String &p_path, bool p_only_impo
 		if (md5 != source_md5) {
 		if (md5 != source_md5) {
 			return true;
 			return true;
 		}
 		}
+
+		if (dest_files.size() && dest_md5 != String()) {
+			md5 = FileAccess::get_multiple_md5(dest_files);
+			if (md5 != dest_md5) {
+				return true;
+			}
+		}
 	}
 	}
 
 
 	return false; //nothing changed
 	return false; //nothing changed
@@ -1388,6 +1411,8 @@ void EditorFileSystem::_reimport_file(const String &p_file) {
 		f->store_line("type=\"" + importer->get_resource_type() + "\"");
 		f->store_line("type=\"" + importer->get_resource_type() + "\"");
 	}
 	}
 
 
+	Vector<String> dest_paths;
+
 	if (err == OK) {
 	if (err == OK) {
 
 
 		if (importer->get_save_extension() == "") {
 		if (importer->get_save_extension() == "") {
@@ -1399,10 +1424,12 @@ void EditorFileSystem::_reimport_file(const String &p_file) {
 				String path = base_path.c_escape() + "." + E->get() + "." + importer->get_save_extension();
 				String path = base_path.c_escape() + "." + E->get() + "." + importer->get_save_extension();
 
 
 				f->store_line("path." + E->get() + "=\"" + path + "\"");
 				f->store_line("path." + E->get() + "=\"" + path + "\"");
+				dest_paths.push_back(path);
 			}
 			}
 		} else {
 		} else {
-
-			f->store_line("path=\"" + base_path + "." + importer->get_save_extension() + "\"");
+			String path = base_path + "." + importer->get_save_extension();
+			f->store_line("path=\"" + path + "\"");
+			dest_paths.push_back(path);
 		}
 		}
 
 
 	} else {
 	} else {
@@ -1418,6 +1445,7 @@ void EditorFileSystem::_reimport_file(const String &p_file) {
 		Array genf;
 		Array genf;
 		for (List<String>::Element *E = gen_files.front(); E; E = E->next()) {
 		for (List<String>::Element *E = gen_files.front(); E; E = E->next()) {
 			genf.push_back(E->get());
 			genf.push_back(E->get());
+			dest_paths.push_back(E->get());
 		}
 		}
 
 
 		String value;
 		String value;
@@ -1426,8 +1454,18 @@ void EditorFileSystem::_reimport_file(const String &p_file) {
 		f->store_line("");
 		f->store_line("");
 	}
 	}
 
 
+	f->store_line("source_file=" + Variant(p_file).get_construct_string());
 	f->store_line("source_md5=\"" + FileAccess::get_md5(p_file) + "\"\n");
 	f->store_line("source_md5=\"" + FileAccess::get_md5(p_file) + "\"\n");
 
 
+	if (dest_paths.size()) {
+		Array dp;
+		for (int i = 0; i < dest_paths.size(); i++) {
+			dp.push_back(dest_paths[i]);
+		}
+		f->store_line("dest_files=" + Variant(dp).get_construct_string());
+		f->store_line("dest_md5=\"" + FileAccess::get_multiple_md5(dest_paths) + "\"\n");
+	}
+
 	f->store_line("[params]");
 	f->store_line("[params]");
 	f->store_line("");
 	f->store_line("");