diff --git a/Shared/General/Logger.cs b/Shared/General/Logger.cs index 3d21cd4..c35e1d1 100644 --- a/Shared/General/Logger.cs +++ b/Shared/General/Logger.cs @@ -62,6 +62,7 @@ public static class Logger { public static ILogger Instance { get; set; } = new ConsoleLogger(); // 转发给实例的方法调用 + // TODO: Trace public static void Log(string message, LogLevels level = LogLevels.Info, LogBehaviors behavior = LogBehaviors.None, [CallerFilePath] string filePath = "") => Instance.Log(message, level, behavior, filePath); public static void Log(Exception ex, string? message = null, LogLevels level = LogLevels.Warning, LogBehaviors behavior = LogBehaviors.ToastIfDebug, [CallerFilePath] string filePath = "") diff --git a/Shared/General/MsDelta.cs b/Shared/General/MsDelta.cs index 5964099..945cad2 100644 --- a/Shared/General/MsDelta.cs +++ b/Shared/General/MsDelta.cs @@ -16,6 +16,7 @@ public static class MsDelta { if (!FileUtils.Exists(oldFilePath)) throw new FileNotFoundException($"旧文件不存在:{oldFilePath}", oldFilePath); if (!FileUtils.Exists(newFilePath)) throw new FileNotFoundException($"新文件不存在:{newFilePath}", newFilePath); DirectoryUtils.Create(deltaFilePath, isFilePath: true); + Logger.Info($"正在生成补丁:{oldFilePath} → {newFilePath},补丁文件将输出到:{deltaFilePath}"); // fileTypeSet:https://learn.microsoft.com/en-us/previous-versions/bb417345(v=msdn.10)?redirectedfrom=MSDN#file-type-sets (15L: DELTA_FILE_TYPE_SET_EXECUTABLES) // 131072L: IgnoreFileSizeLimit, 32u: Crc32 if (!CreateDelta(15L, 131072L, 0L, @@ -32,6 +33,7 @@ public static class MsDelta { if (!FileUtils.Exists(oldFilePath)) throw new FileNotFoundException($"旧文件不存在:{oldFilePath}", oldFilePath); if (!FileUtils.Exists(deltaFilePath)) throw new FileNotFoundException($"补丁文件不存在:{deltaFilePath}", deltaFilePath); DirectoryUtils.Create(newFilePath, isFilePath: true); + Logger.Info($"正在应用补丁:原始文件 {oldFilePath} + 补丁文件 {deltaFilePath} → 输出至 {newFilePath}"); // 1L: AllowLegacy if (!ApplyDelta(1L, PathUtils.WithLongPath(oldFilePath), PathUtils.WithLongPath(deltaFilePath), PathUtils.WithLongPath(newFilePath))) throw new Win32Exception(); diff --git a/Shared/Utils/DirectoryUtils.cs b/Shared/Utils/DirectoryUtils.cs index bbbbcd3..40b11bf 100644 --- a/Shared/Utils/DirectoryUtils.cs +++ b/Shared/Utils/DirectoryUtils.cs @@ -7,7 +7,10 @@ public static class DirectoryUtils { /// public static void Create(string path, bool isFilePath = false) { if (isFilePath) path = Path.GetDirectoryName(path); - Directory.CreateDirectory(PathUtils.WithLongPath(path)); + if (Exists(path)) return; + Logger.Trace($"新建文件夹:{path}"); + path = PathUtils.WithLongPath(path); + Directory.CreateDirectory(path); } /// @@ -29,6 +32,7 @@ public static class DirectoryUtils { /// public static IEnumerable GetFiles(string path, bool topDirectoryOnly = false, string searchPattern = "*") { if (!Exists(path)) return []; + Logger.Trace($"枚举文件({searchPattern},递归:{!topDirectoryOnly}):{path}"); return Directory.EnumerateFiles(PathUtils.WithLongPath(path), searchPattern, topDirectoryOnly ? SearchOption.TopDirectoryOnly : SearchOption.AllDirectories). Select(PathUtils.WithoutLongPath); } @@ -38,6 +42,7 @@ public static class DirectoryUtils { /// public static IEnumerable GetDirectories(string path, bool topDirectoryOnly = false, string searchPattern = "*") { if (!Exists(path)) return []; + Logger.Trace($"枚举文件夹({searchPattern},递归:{!topDirectoryOnly}):{path}"); return Directory.EnumerateDirectories(PathUtils.WithLongPath(path), searchPattern, topDirectoryOnly ? SearchOption.TopDirectoryOnly : SearchOption.AllDirectories). Select(PathUtils.WithoutLongPath); } @@ -57,6 +62,7 @@ public static class DirectoryUtils { public static void Move(string sourceDirName, string destDirName) { if (!Exists(sourceDirName)) return; Create(destDirName); + Logger.Trace($"剪切文件夹:{sourceDirName} → {destDirName}"); Directory.Move(PathUtils.WithLongPath(sourceDirName), PathUtils.WithLongPath(destDirName)); } @@ -64,8 +70,10 @@ public static class DirectoryUtils { /// 删除文件夹。 /// public static void Delete(string dirPath, bool toRecycleBin = false) { + if (!Exists(dirPath)) return; + Logger.Trace($"{(toRecycleBin ? "将文件夹删除到回收站" : "删除文件夹")}:{dirPath}"); if (toRecycleBin) { - if (Exists(dirPath)) FileUtils.DeleteToRecycleBin(dirPath); + FileUtils.DeleteToRecycleBin(dirPath); } else { Directory.Delete(PathUtils.WithLongPath(dirPath)); } diff --git a/Shared/Utils/FileUtils.cs b/Shared/Utils/FileUtils.cs index ad3b9c9..d250f43 100644 --- a/Shared/Utils/FileUtils.cs +++ b/Shared/Utils/FileUtils.cs @@ -1,4 +1,5 @@ -using System.IO.Compression; +using System.IO; +using System.IO.Compression; namespace MeloongCore; public static class FileUtils { @@ -10,8 +11,15 @@ public static class FileUtils { /// 如果文件或文件夹不存在,则会自动创建。若已存在,则会覆盖原文件。 /// public static void Write(string filePath, string text, Encoding? encoding = null) { - DirectoryUtils.Create(filePath, isFilePath: true); - File.WriteAllText(PathUtils.WithLongPath(filePath), text, encoding ?? new UTF8Encoding()); + Write(filePath, (encoding ?? new UTF8Encoding()).GetBytes(text)); + } + + /// + /// 写入文件。 + /// 如果文件或文件夹不存在,则会自动创建。若已存在,则会覆盖原文件。 + /// + public static void Write(string filePath, IEnumerable content) { + Write(filePath, [..content]); } /// @@ -20,18 +28,10 @@ public static class FileUtils { /// public static void Write(string filePath, byte[] content) { DirectoryUtils.Create(filePath, isFilePath: true); + Logger.Trace($"写入文件:{filePath}({content.Length} 字节)"); File.WriteAllBytes(PathUtils.WithLongPath(filePath), content); } - /// - /// 写入文件。 - /// 如果文件或文件夹不存在,则会自动创建。若已存在,则会覆盖原文件。 - /// - public static void Write(string filePath, IEnumerable content) { - DirectoryUtils.Create(filePath, isFilePath: true); - File.WriteAllBytes(PathUtils.WithLongPath(filePath), [..content]); - } - /// /// 将 写入文件。 /// 会将流的位置主动重置到开头。 @@ -40,6 +40,7 @@ public static class FileUtils { public static void Write(string filePath, Stream stream) { using FileStream fileStream = CreateAsStream(PathUtils.WithLongPath(filePath)); if (stream.CanSeek && stream.Position != 0) stream.Seek(0, SeekOrigin.Begin); + Logger.Trace($"写入文件:{filePath}({stream.GetType().Name} {stream.Length} 字节)"); stream.CopyTo(fileStream); } @@ -51,8 +52,10 @@ public static class FileUtils { /// 删除文件。 /// public static void Delete(string filePath, bool toRecycleBin = false) { + if (!Exists(filePath)) return; + Logger.Trace($"{(toRecycleBin ? "将文件删除到回收站" : "删除文件")}:{filePath}"); if (toRecycleBin) { - if (Exists(filePath)) DeleteToRecycleBin(filePath); + DeleteToRecycleBin(filePath); } else { File.Delete(PathUtils.WithLongPath(filePath)); } @@ -139,6 +142,7 @@ public static class FileUtils { /// public static FileStream CreateAsStream(string filePath) { DirectoryUtils.Create(filePath, isFilePath: true); + Logger.Trace($"创建文件流:{filePath}"); return new(PathUtils.WithLongPath(filePath), FileMode.Create); } @@ -152,6 +156,7 @@ public static class FileUtils { /// public static ZipArchive OpenZip(string zipFilePath) { ZipArchive TryOpen(Encoding encoding) { + Logger.Trace($"尝试以 {encoding.EncodingName} 编码打开压缩包:{zipFilePath}"); var result = ZipFile.Open(PathUtils.WithLongPath(zipFilePath), ZipArchiveMode.Read, encoding); try { _ = result.Entries; // 如果编码有误,会在这里抛出 DecoderFallbackException;如果文件异常,会在这里抛出 InvalidDataException @@ -182,9 +187,11 @@ public static class FileUtils { DirectoryUtils.Create(outputDirectory); // 解压 gz(gz 不需要考虑编码) if (compressionFile.EndsWithF(".gz", true)) { + string outFilePath = Path.Combine(outputDirectory, Path.GetFileNameWithoutExtension(compressionFile)); + Logger.Trace($"解压 gz 文件:{compressionFile} → {outFilePath}"); using var fileStream = FileUtils.ReadAsStream(compressionFile); using var gzipStream = new GZipStream(fileStream, CompressionMode.Decompress); - FileUtils.Write(Path.Combine(outputDirectory, Path.GetFileNameWithoutExtension(compressionFile)), gzipStream); + FileUtils.Write(outFilePath, gzipStream); progressHandler?.Invoke(1); return; } @@ -192,6 +199,7 @@ public static class FileUtils { using var archive = FileUtils.OpenZip(compressionFile); int totalCount = archive.Entries.Count; int doneCount = 0; + Logger.Trace($"解压 zip 文件:{compressionFile} → {outputDirectory}(共 {totalCount} 项)"); foreach (var entry in archive.Entries) { doneCount++; if (totalCount > 0) progressHandler?.Invoke((double) doneCount / totalCount); @@ -215,6 +223,7 @@ public static class FileUtils { sourceDirectory = PathUtils.WithLongPath(sourceDirectory); DirectoryUtils.Create(outputFullPath, isFilePath: true); FileUtils.Delete(outputFullPath); + Logger.Trace($"将文件夹中的内容压缩为 zip 文件:{sourceDirectory} → {outputFullPath}"); ZipFile.CreateFromDirectory(sourceDirectory, outputFullPath); } @@ -242,6 +251,7 @@ public static class FileUtils { DirectoryUtils.Create(outputFullPath, isFilePath: true); FileUtils.Delete(outputFullPath); using var archive = ZipFile.Open(outputFullPath, ZipArchiveMode.Create); + Logger.Trace($"创建 zip 文件:{sources.Count} 个文件 → {outputFullPath}\n{sources.Select(p => $"- {p.Value} → {p.Key}").Join('\n')}"); foreach (var pair in sources) archive.CreateEntryFromFile(PathUtils.WithLongPath(pair.Value), pair.Key.Replace('\\', '/'), CompressionLevel.Optimal); } @@ -268,6 +278,7 @@ public static class FileUtils { public static void Copy(string sourceFilePath, string destFilePath) { if (sourceFilePath == destFilePath) return; // 如果复制同一个文件则跳过 DirectoryUtils.Create(destFilePath, isFilePath: true); + Logger.Trace($"复制文件:{sourceFilePath} → {destFilePath}"); File.Copy(PathUtils.WithLongPath(sourceFilePath), PathUtils.WithLongPath(destFilePath), true); } @@ -278,6 +289,7 @@ public static class FileUtils { public static void Move(string sourceFilePath, string destFilePath) { if (sourceFilePath == destFilePath) return; // 如果移动同一个文件则跳过 DirectoryUtils.Create(destFilePath, isFilePath: true); + Logger.Trace($"剪切文件:{sourceFilePath} → {destFilePath}"); File.Move(PathUtils.WithLongPath(sourceFilePath), PathUtils.WithLongPath(destFilePath)); }