From 0e03e477d7bbb8947ec9ed909a620777d29fce45 Mon Sep 17 00:00:00 2001 From: "Juster.zhu" Date: Mon, 23 Sep 2024 22:50:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8B=86=E5=88=86=E9=BB=91=E5=90=8D=E5=8D=95?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Strategys/WindowsStrategy.cs | 4 +- .../File/BlackListManager.cs | 99 +++++++++++++++++++ .../File/FileExtensions.cs | 4 +- .../File/GeneralFileManager.cs | 78 +-------------- .../Strategys/WindowsStrategy.cs | 4 +- .../DifferentialCore.cs | 20 ++-- 6 files changed, 117 insertions(+), 92 deletions(-) create mode 100644 src/c#/GeneralUpdate.Common/File/BlackListManager.cs diff --git a/src/c#/GeneralUpdate.ClientCore/Strategys/WindowsStrategy.cs b/src/c#/GeneralUpdate.ClientCore/Strategys/WindowsStrategy.cs index e9f9a705..cd445dca 100644 --- a/src/c#/GeneralUpdate.ClientCore/Strategys/WindowsStrategy.cs +++ b/src/c#/GeneralUpdate.ClientCore/Strategys/WindowsStrategy.cs @@ -63,8 +63,8 @@ public override async Task ExecuteAsync() //patch middleware context.Add("SourcePath", patchPath); context.Add("TargetPath", Packet.InstallPath); - context.Add("BlackFiles", GeneralFileManager.BlackFiles); - context.Add("BlackFileFormats", GeneralFileManager.BlackFileFormats); + context.Add("BlackFiles", BlackListManager.Instance.BlackFiles); + context.Add("BlackFileFormats", BlackListManager.Instance.BlackFileFormats); var pipelineBuilder = new PipelineBuilder(context) .UseMiddleware() diff --git a/src/c#/GeneralUpdate.Common/File/BlackListManager.cs b/src/c#/GeneralUpdate.Common/File/BlackListManager.cs new file mode 100644 index 00000000..a45d138e --- /dev/null +++ b/src/c#/GeneralUpdate.Common/File/BlackListManager.cs @@ -0,0 +1,99 @@ +using System.Collections.Generic; +using System.IO; + +namespace GeneralUpdate.Common; + +public class BlackListManager +{ + private readonly static object _lockObject = new object(); + + private static BlackListManager _instance; + + private static readonly List _blackFileFormats = + [ + ".patch", + ".7z", + ".zip", + ".rar", + ".tar", + ".json" + ]; + + private static readonly List _blackFiles = ["Newtonsoft.Json.dll"]; + + private BlackListManager() + { + } + + public static BlackListManager Instance + { + get + { + if (_instance == null) + { + lock (_lockObject) + { + if (_instance == null) + { + _instance = new BlackListManager(); + } + } + } + + return _instance; + } + } + + public IReadOnlyList BlackFileFormats => _blackFileFormats.AsReadOnly(); + public IReadOnlyList BlackFiles => _blackFiles.AsReadOnly(); + + public void AddBlackFileFormats(List formats) + { + foreach (var format in formats) + { + AddBlackFileFormat(format); + } + } + + public void AddBlackFileFormat(string format) + { + if (!_blackFileFormats.Contains(format)) + { + _blackFileFormats.Add(format); + } + } + + public void RemoveBlackFileFormat(string format) + { + _blackFileFormats.Remove(format); + } + + public void AddBlackFiles(List fileNames) + { + foreach (var fileName in fileNames) + { + AddBlackFile(fileName); + } + } + + public void AddBlackFile(string fileName) + { + if (!_blackFiles.Contains(fileName)) + { + _blackFiles.Add(fileName); + } + } + + public void RemoveBlackFile(string fileName) + { + _blackFiles.Remove(fileName); + } + + public bool IsBlacklisted(string relativeFilePath) + { + var fileName = Path.GetFileName(relativeFilePath); + var fileExtension = Path.GetExtension(relativeFilePath); + + return _blackFiles.Contains(fileName) || _blackFileFormats.Contains(fileExtension); + } +} \ No newline at end of file diff --git a/src/c#/GeneralUpdate.Common/File/FileExtensions.cs b/src/c#/GeneralUpdate.Common/File/FileExtensions.cs index 496ebfb8..1bd33175 100644 --- a/src/c#/GeneralUpdate.Common/File/FileExtensions.cs +++ b/src/c#/GeneralUpdate.Common/File/FileExtensions.cs @@ -6,12 +6,12 @@ namespace GeneralUpdate.Common; public static class FileExtensions { - public static List ToFileInfoList(this List filePaths) + public static List AsFileInfo(this List filePaths) { return filePaths.Select(path => new FileInfo(path)).ToList(); } - public static List ToFileInfoList(this IReadOnlyList filePaths) + public static List AsFileInfo(this IReadOnlyList filePaths) { return filePaths.Select(path => new FileInfo(path)).ToList(); } diff --git a/src/c#/GeneralUpdate.Common/File/GeneralFileManager.cs b/src/c#/GeneralUpdate.Common/File/GeneralFileManager.cs index 3840b417..bbd408ed 100644 --- a/src/c#/GeneralUpdate.Common/File/GeneralFileManager.cs +++ b/src/c#/GeneralUpdate.Common/File/GeneralFileManager.cs @@ -9,26 +9,7 @@ namespace GeneralUpdate.Common { public sealed class GeneralFileManager { - #region Private Members - - private static readonly List _blackFileFormats = - [ - ".patch", - ".7z", - ".zip", - ".rar", - ".tar", - ".json" - ]; - - private static readonly List _blackFiles = ["Newtonsoft.Json.dll"]; - - #endregion - #region Public Properties - - public static IReadOnlyList BlackFileFormats => _blackFileFormats.AsReadOnly(); - public static IReadOnlyList BlackFiles => _blackFiles.AsReadOnly(); public ComparisonResult ComparisonResult { get; private set; } @@ -36,53 +17,6 @@ public sealed class GeneralFileManager #region Public Methods - public static List ToFileInfoList(IReadOnlyList filePaths) - { - return filePaths.Select(path => new FileInfo(path)).ToList(); - } - - public static void AddBlackFileFormats(List formats) - { - foreach (var format in formats) - { - AddBlackFileFormat(format); - } - } - - public static void AddBlackFileFormat(string format) - { - if (!_blackFileFormats.Contains(format)) - { - _blackFileFormats.Add(format); - } - } - - public static void RemoveBlackFileFormat(string format) - { - _blackFileFormats.Remove(format); - } - - public static void AddBlackFiles(List fileNames) - { - foreach (var fileName in fileNames) - { - AddBlackFile(fileName); - } - } - - public static void AddBlackFile(string fileName) - { - if (!_blackFiles.Contains(fileName)) - { - _blackFiles.Add(fileName); - } - } - - public static void RemoveBlackFile(string fileName) - { - _blackFiles.Remove(fileName); - } - /// /// Compare two directories. /// @@ -92,8 +26,8 @@ public void CompareDirectories(string dirA, string dirB) { ComparisonResult = new ComparisonResult(); - var filesA = GetRelativeFilePaths(dirA, dirA).Where(f => !IsBlacklisted(f)).ToList(); - var filesB = GetRelativeFilePaths(dirB, dirB).Where(f => !IsBlacklisted(f)).ToList(); + var filesA = GetRelativeFilePaths(dirA, dirA).Where(f => !BlackListManager.Instance.IsBlacklisted(f)).ToList(); + var filesB = GetRelativeFilePaths(dirB, dirB).Where(f => !BlackListManager.Instance.IsBlacklisted(f)).ToList(); ComparisonResult.AddUniqueToA(filesA.Except(filesB).Select(f => Path.Combine(dirA, f))); ComparisonResult.AddUniqueToB(filesB.Except(filesA).Select(f => Path.Combine(dirB, f))); @@ -187,14 +121,6 @@ private string GetRelativePath(string fromPath, string toPath) return relativePath; } - private bool IsBlacklisted(string relativeFilePath) - { - var fileName = Path.GetFileName(relativeFilePath); - var fileExtension = Path.GetExtension(relativeFilePath); - - return _blackFiles.Contains(fileName) || _blackFileFormats.Contains(fileExtension); - } - private bool FilesAreEqual(string fileA, string fileB) { var sha256 = new Sha256HashAlgorithm(); diff --git a/src/c#/GeneralUpdate.Core/Strategys/WindowsStrategy.cs b/src/c#/GeneralUpdate.Core/Strategys/WindowsStrategy.cs index c9308a4a..c3e910f6 100644 --- a/src/c#/GeneralUpdate.Core/Strategys/WindowsStrategy.cs +++ b/src/c#/GeneralUpdate.Core/Strategys/WindowsStrategy.cs @@ -64,8 +64,8 @@ public override async Task ExecuteAsync() //patch middleware context.Add("SourcePath", patchPath); context.Add("TargetPath", Packet.InstallPath); - context.Add("BlackFiles", GeneralFileManager.BlackFiles); - context.Add("BlackFileFormats", GeneralFileManager.BlackFileFormats); + context.Add("BlackFiles", BlackListManager.Instance.BlackFiles); + context.Add("BlackFileFormats", BlackListManager.Instance.BlackFileFormats); //driver middleware context.Add("DriverPath", new List()); context.Add("Version", version.Version); diff --git a/src/c#/GeneralUpdate.Differential/DifferentialCore.cs b/src/c#/GeneralUpdate.Differential/DifferentialCore.cs index 3c99c8ab..3ac58970 100644 --- a/src/c#/GeneralUpdate.Differential/DifferentialCore.cs +++ b/src/c#/GeneralUpdate.Differential/DifferentialCore.cs @@ -79,7 +79,7 @@ public async Task Clean(string sourcePath, string targetPath, string patchPath = var result = fileManager.ComparisonResult; //Binary differencing of like terms . - foreach (var file in GeneralFileManager.ToFileInfoList(result.DifferentFiles)) + foreach (var file in result.DifferentFiles.AsFileInfo()) { var dirSeparatorChar = Path.DirectorySeparatorChar.ToString().ToCharArray(); var tempPath = file.FullName.Replace(targetPath, "").Replace(Path.GetFileName(file.FullName), "").TrimStart(dirSeparatorChar).TrimEnd(dirSeparatorChar); @@ -97,11 +97,11 @@ public async Task Clean(string sourcePath, string targetPath, string patchPath = tempPath0 = Path.Combine(tempDir, $"{file.Name}{PATCH_FORMAT}"); } - var finOldFile = GeneralFileManager.ToFileInfoList(result.UniqueToA).FirstOrDefault(i => i.Name.Equals(file.Name)); + var finOldFile = (result.UniqueToA.AsFileInfo()).FirstOrDefault(i => i.Name.Equals(file.Name)); var oldFile = finOldFile == null ? "" : finOldFile.FullName; var newFile = file.FullName; var extensionName = Path.GetExtension(file.FullName); - if (File.Exists(oldFile) && File.Exists(newFile) && !GeneralFileManager.BlackFileFormats.Contains(extensionName)) + if (File.Exists(oldFile) && File.Exists(newFile) && !BlackListManager.Instance.BlackFileFormats.Contains(extensionName)) { var hashAlgorithm = new Sha256HashAlgorithm(); if (hashAlgorithm.ComputeHash(oldFile) @@ -148,14 +148,14 @@ public async Task Dirty(string appPath, string patchPath) var fileManager = new GeneralFileManager(); fileManager.CompareDirectories(appPath, patchPath); var result = fileManager.ComparisonResult; - var patchFiles = GeneralFileManager.ToFileInfoList(result.DifferentFiles); - var oldFiles = GeneralFileManager.ToFileInfoList(result.UniqueToA); + var patchFiles = result.DifferentFiles.AsFileInfo(); + var oldFiles = result.UniqueToA.AsFileInfo(); //If a JSON file for the deletion list is found in the update package, it will be deleted based on its contents. var deleteListJson = patchFiles.FirstOrDefault(i => i.Name.Equals(DELETE_FILES_NAME)); if (deleteListJson != null) { - var deleteFiles = GeneralFileManager.ToFileInfoList(GeneralFileManager.GetJson>(deleteListJson.FullName)) ; + var deleteFiles = GeneralFileManager.GetJson>(deleteListJson.FullName).AsFileInfo() ; var hashAlgorithm = new Sha256HashAlgorithm(); foreach (var file in deleteFiles) { @@ -204,8 +204,8 @@ public async Task Dirty(string appPath, string patchPath) /// A collection of blacklist file name extensions that are skipped on update. public void SetBlocklist(List blackFiles, List blackFileFormats) { - GeneralFileManager.AddBlackFiles(blackFiles); - GeneralFileManager.AddBlackFileFormats(blackFileFormats); + BlackListManager.Instance.AddBlackFiles(blackFiles); + BlackListManager.Instance.AddBlackFileFormats(blackFileFormats); } #endregion Public Methods @@ -245,10 +245,10 @@ private Task DirtyUnknow(string appPath, string patchPath) var fileManager = new GeneralFileManager(); fileManager.CompareDirectories(appPath, patchPath); var result = fileManager.ComparisonResult; - foreach (var file in GeneralFileManager.ToFileInfoList(result.DifferentFiles)) + foreach (var file in (result.DifferentFiles.AsFileInfo())) { var extensionName = Path.GetExtension(file.FullName); - if (GeneralFileManager.BlackFileFormats.Contains(extensionName)) continue; + if (BlackListManager.Instance.BlackFileFormats.Contains(extensionName)) continue; var targetFileName = file.FullName.Replace(patchPath, "").TrimStart("\\".ToCharArray()); var targetPath = Path.Combine(appPath, targetFileName); var parentFolder = Directory.GetParent(targetPath);