diff --git a/src/c#/GeneralUpdate.Core/ContentProvider/FileProvider-Filter.cs b/src/c#/GeneralUpdate.Core/ContentProvider/FileProvider-Filter.cs index e399f574..6fe01323 100644 --- a/src/c#/GeneralUpdate.Core/ContentProvider/FileProvider-Filter.cs +++ b/src/c#/GeneralUpdate.Core/ContentProvider/FileProvider-Filter.cs @@ -4,7 +4,18 @@ namespace GeneralUpdate.Core.ContentProvider { public partial class FileProvider { - private static List _blackFiles, _blackFileFormats; + private static List _blackFiles, + _blackFileFormats; + private static readonly List DefaultBlackFileFormats = new List(6) + { + ".patch", + ".7z", + ".zip", + ".rar", + ".tar", + ".json" + }; + private static readonly List DefaultBlackFiles = new List(1) { "Newtonsoft.Json.dll" }; /// /// Set a blacklist. @@ -21,12 +32,14 @@ public static void SetBlacklist(List blackFiles, List blackFileF /// These files will be skipped when updating. /// /// - public static List GetBlackFiles() => _blackFiles ?? new List() { "Newtonsoft.Json.dll" }; + public static List GetBlackFiles() => + _blackFiles ?? DefaultBlackFiles; /// /// These files that contain the file suffix will be skipped when updating. /// /// - public static List GetBlackFileFormats() => _blackFileFormats ?? new List() { ".patch", ".7z", ".zip", ".rar", ".tar", ".json" }; + public static List GetBlackFileFormats() => + _blackFileFormats ?? DefaultBlackFileFormats; } -} \ No newline at end of file +} diff --git a/src/c#/GeneralUpdate.Core/ContentProvider/FileProvider-Serialization.cs b/src/c#/GeneralUpdate.Core/ContentProvider/FileProvider-Serialization.cs index ebf0a6de..9dd15143 100644 --- a/src/c#/GeneralUpdate.Core/ContentProvider/FileProvider-Serialization.cs +++ b/src/c#/GeneralUpdate.Core/ContentProvider/FileProvider-Serialization.cs @@ -9,8 +9,13 @@ public partial class FileProvider { public static void CreateJson(string targetPath, T obj) { - if (!Directory.Exists(targetPath)) Directory.CreateDirectory(targetPath); - if (File.Exists(targetPath)) File.Delete(targetPath); + var folderPath = Path.GetDirectoryName(targetPath) ?? + throw new ArgumentException("invalid path", nameof(targetPath)); + if (!Directory.Exists(folderPath)) + { + Directory.CreateDirectory(folderPath); + } + var jsonString = JsonConvert.SerializeObject(obj); File.WriteAllText(targetPath, jsonString); } @@ -20,10 +25,7 @@ public static T GetJson(string path) if (File.Exists(path)) { var json = File.ReadAllText(path); - if (json != null) - { - return JsonConvert.DeserializeObject(json); - } + return JsonConvert.DeserializeObject(json); } return default(T); } diff --git a/src/c#/GeneralUpdate.Core/HashAlgorithms/HashAlgorithmBase.cs b/src/c#/GeneralUpdate.Core/HashAlgorithms/HashAlgorithmBase.cs index 09ed6b71..c9db372c 100644 --- a/src/c#/GeneralUpdate.Core/HashAlgorithms/HashAlgorithmBase.cs +++ b/src/c#/GeneralUpdate.Core/HashAlgorithms/HashAlgorithmBase.cs @@ -16,7 +16,7 @@ public string ComputeHash(string fileName) { using (var file = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) { - var dataArray = GetHashAlgorithm().ComputeHash(file); + var dataArray = hashAlgorithm.ComputeHash(file); var stringBuilder = new StringBuilder(); for (int i = 0; i < dataArray.Length; i++) { diff --git a/src/c#/GeneralUpdate.Differential/DifferentialCore.cs b/src/c#/GeneralUpdate.Differential/DifferentialCore.cs index 38586f63..9fd835d3 100644 --- a/src/c#/GeneralUpdate.Differential/DifferentialCore.cs +++ b/src/c#/GeneralUpdate.Differential/DifferentialCore.cs @@ -68,12 +68,15 @@ public async Task Clean(string sourcePath, string targetPath, string patchPath = { try { - if (string.IsNullOrWhiteSpace(patchPath)) patchPath = Path.Combine(Environment.CurrentDirectory, PATCHS); - if (!Directory.Exists(patchPath)) Directory.CreateDirectory(patchPath); + if (string.IsNullOrWhiteSpace(patchPath)) + patchPath = Path.Combine(Environment.CurrentDirectory, PATCHS); + if (!Directory.Exists(patchPath)) + Directory.CreateDirectory(patchPath); //Take the left tree as the center to match the files that are not in the right tree . var fileProvider = new FileProvider(); var nodes = await fileProvider.Compare(sourcePath, targetPath); + var hashAlgorithm = new Sha256HashAlgorithm(); //Binary differencing of like terms . foreach (var file in nodes.Item3) @@ -93,25 +96,35 @@ public async Task Clean(string sourcePath, string targetPath, string patchPath = if (!Directory.Exists(tempDir)) Directory.CreateDirectory(tempDir); tempPath0 = Path.Combine(tempDir, $"{file.Name}{PATCH_FORMAT}"); } + var finOldFile = nodes.Item1.FirstOrDefault(i => i.Name.Equals(file.Name)); - var oldfile = finOldFile == null ? "" : finOldFile.FullName; - var newfile = file.FullName; + var oldFile = finOldFile == null ? "" : finOldFile.FullName; + var newFile = file.FullName; var extensionName = Path.GetExtension(file.FullName); - if (File.Exists(oldfile) && File.Exists(newfile) && !FileProvider.GetBlackFileFormats().Contains(extensionName)) + if (File.Exists(oldFile) && File.Exists(newFile) && !FileProvider.GetBlackFileFormats().Contains(extensionName)) { + if (hashAlgorithm.ComputeHash(oldFile) + .Equals(hashAlgorithm.ComputeHash(newFile), StringComparison.OrdinalIgnoreCase)) + { + continue; + } + //Generate the difference file to the difference directory . - await new BinaryHandle().Clean(oldfile, newfile, tempPath0); + await new BinaryHandle().Clean(oldFile, newFile, tempPath0); } else { - File.Copy(newfile, Path.Combine(tempDir, Path.GetFileName(newfile)), true); + File.Copy(newFile, Path.Combine(tempDir, Path.GetFileName(newFile)), true); } } //If a file is found that needs to be deleted, a list of files is written to the update package. - var exceptFiles = await fileProvider.Except(sourcePath, targetPath); - if (exceptFiles != null && exceptFiles.Count() > 0) - FileProvider.CreateJson(Path.Combine(patchPath, DELETE_FILES_NAME), exceptFiles); + var exceptFiles = (await fileProvider.Except(sourcePath, targetPath)).ToList(); + if (exceptFiles.Count != 0) + { + var path = Path.Combine(patchPath, DELETE_FILES_NAME); + FileProvider.CreateJson(path, exceptFiles); + } } catch (Exception ex) { @@ -142,9 +155,16 @@ public async Task Dirty(string appPath, string patchPath) var hashAlgorithm = new Sha256HashAlgorithm(); foreach (var file in deleteFiles) { - var resultFile = oldFiles.FirstOrDefault(i => string.Equals(hashAlgorithm.ComputeHash(i.FullName), file.Hash, StringComparison.OrdinalIgnoreCase)); - if (resultFile == null) continue; - if (File.Exists(resultFile.FullName)) File.Delete(resultFile.FullName); + var resultFile = oldFiles.FirstOrDefault(i => + string.Equals(hashAlgorithm.ComputeHash(i.FullName), file.Hash, StringComparison.OrdinalIgnoreCase)); + if (resultFile == null) + { + continue; + } + if (File.Exists(resultFile.FullName)) + { + File.Delete(resultFile.FullName); + } } }