From 6da740c35faa4ee3e8b04236374781bdafb10c9a Mon Sep 17 00:00:00 2001 From: textGamex Date: Fri, 12 Jul 2024 13:48:34 +0800 Subject: [PATCH 1/5] =?UTF-8?q?fix:=20=E8=AE=A1=E7=AE=97=E5=93=88=E5=B8=8C?= =?UTF-8?q?=E5=80=BC=E6=97=B6=E4=B8=8D=E6=AD=A3=E7=A1=AE=E7=9A=84=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E5=AF=B9=E8=B1=A1=E5=AF=BC=E8=87=B4=E5=8F=AF=E8=83=BD?= =?UTF-8?q?=E7=9A=84=E8=B5=84=E6=BA=90=E6=B3=84=E6=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/c#/GeneralUpdate.Core/HashAlgorithms/HashAlgorithmBase.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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++) { From 9a5133477c9fa2246f8400b0e4c618898e2e6e69 Mon Sep 17 00:00:00 2001 From: textGamex Date: Fri, 12 Jul 2024 14:28:41 +0800 Subject: [PATCH 2/5] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=20FileProvider?= =?UTF-8?q?=20=E8=BF=87=E6=BB=A4=E5=99=A8=E9=83=A8=E5=88=86=E6=80=A7?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ContentProvider/FileProvider-Filter.cs | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) 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 +} From cdefbc7e06c4d2b3a8323605e6306386c2434849 Mon Sep 17 00:00:00 2001 From: textGamex Date: Fri, 12 Jul 2024 14:41:31 +0800 Subject: [PATCH 3/5] =?UTF-8?q?fix:=20CreateJson=E4=BC=9A=E5=90=91?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=A4=B9=E5=86=99=E5=85=A5=E6=95=B0=E6=8D=AE?= =?UTF-8?q?,=E8=80=8C=E4=B8=8D=E6=98=AF=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ContentProvider/FileProvider-Serialization.cs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) 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); } From 93efc7eb3a223906a43a44895f536af58204ce00 Mon Sep 17 00:00:00 2001 From: textGamex Date: Sat, 13 Jul 2024 10:13:28 +0800 Subject: [PATCH 4/5] =?UTF-8?q?fix:=20=E5=BD=93=E6=96=87=E4=BB=B6=E6=B2=A1?= =?UTF-8?q?=E6=9C=89=E5=8F=91=E7=94=9F=E6=94=B9=E5=8F=98=E6=97=B6=E4=B9=9F?= =?UTF-8?q?=E4=BC=9A=E7=94=9F=E6=88=90=E8=A1=A5=E4=B8=81=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DifferentialCore.cs | 45 +++++++++++++------ 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/src/c#/GeneralUpdate.Differential/DifferentialCore.cs b/src/c#/GeneralUpdate.Differential/DifferentialCore.cs index 38586f63..96bf00fe 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,34 @@ 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) == hashAlgorithm.ComputeHash(newFile)) + { + 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 +154,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); + } } } From 1ab11fd94b1490c47207a50b0bbe93b8bd395c89 Mon Sep 17 00:00:00 2001 From: textGamex Date: Mon, 15 Jul 2024 23:40:19 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E5=93=88=E5=B8=8C=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2=E6=AF=94=E8=BE=83=E6=94=B9=E4=B8=BAEquals?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/c#/GeneralUpdate.Differential/DifferentialCore.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/c#/GeneralUpdate.Differential/DifferentialCore.cs b/src/c#/GeneralUpdate.Differential/DifferentialCore.cs index 96bf00fe..9fd835d3 100644 --- a/src/c#/GeneralUpdate.Differential/DifferentialCore.cs +++ b/src/c#/GeneralUpdate.Differential/DifferentialCore.cs @@ -103,7 +103,8 @@ public async Task Clean(string sourcePath, string targetPath, string patchPath = var extensionName = Path.GetExtension(file.FullName); if (File.Exists(oldFile) && File.Exists(newFile) && !FileProvider.GetBlackFileFormats().Contains(extensionName)) { - if (hashAlgorithm.ComputeHash(oldFile) == hashAlgorithm.ComputeHash(newFile)) + if (hashAlgorithm.ComputeHash(oldFile) + .Equals(hashAlgorithm.ComputeHash(newFile), StringComparison.OrdinalIgnoreCase)) { continue; }