重要操作的 trace
This commit is contained in:
1 parent
3f3b8369ff
commit
5d997664c1
4 files changed
+39
-16
No files matched your search
@@ -62,6 +62,7 @@ public static class Logger {
|
|||||||
public static ILogger Instance { get; set; } = new ConsoleLogger();
|
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 = "")
|
public static void Log(string message, LogLevels level = LogLevels.Info, LogBehaviors behavior = LogBehaviors.None, [CallerFilePath] string filePath = "")
|
||||||
=> Instance.Log(message, level, behavior, 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 = "")
|
public static void Log(Exception ex, string? message = null, LogLevels level = LogLevels.Warning, LogBehaviors behavior = LogBehaviors.ToastIfDebug, [CallerFilePath] string filePath = "")
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ public static class MsDelta {
|
|||||||
if (!FileUtils.Exists(oldFilePath)) throw new FileNotFoundException($"旧文件不存在:{oldFilePath}", oldFilePath);
|
if (!FileUtils.Exists(oldFilePath)) throw new FileNotFoundException($"旧文件不存在:{oldFilePath}", oldFilePath);
|
||||||
if (!FileUtils.Exists(newFilePath)) throw new FileNotFoundException($"新文件不存在:{newFilePath}", newFilePath);
|
if (!FileUtils.Exists(newFilePath)) throw new FileNotFoundException($"新文件不存在:{newFilePath}", newFilePath);
|
||||||
DirectoryUtils.Create(deltaFilePath, isFilePath: true);
|
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)
|
// 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
|
// 131072L: IgnoreFileSizeLimit, 32u: Crc32
|
||||||
if (!CreateDelta(15L, 131072L, 0L,
|
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(oldFilePath)) throw new FileNotFoundException($"旧文件不存在:{oldFilePath}", oldFilePath);
|
||||||
if (!FileUtils.Exists(deltaFilePath)) throw new FileNotFoundException($"补丁文件不存在:{deltaFilePath}", deltaFilePath);
|
if (!FileUtils.Exists(deltaFilePath)) throw new FileNotFoundException($"补丁文件不存在:{deltaFilePath}", deltaFilePath);
|
||||||
DirectoryUtils.Create(newFilePath, isFilePath: true);
|
DirectoryUtils.Create(newFilePath, isFilePath: true);
|
||||||
|
Logger.Info($"正在应用补丁:原始文件 {oldFilePath} + 补丁文件 {deltaFilePath} → 输出至 {newFilePath}");
|
||||||
// 1L: AllowLegacy
|
// 1L: AllowLegacy
|
||||||
if (!ApplyDelta(1L, PathUtils.WithLongPath(oldFilePath), PathUtils.WithLongPath(deltaFilePath), PathUtils.WithLongPath(newFilePath)))
|
if (!ApplyDelta(1L, PathUtils.WithLongPath(oldFilePath), PathUtils.WithLongPath(deltaFilePath), PathUtils.WithLongPath(newFilePath)))
|
||||||
throw new Win32Exception();
|
throw new Win32Exception();
|
||||||
|
|||||||
@@ -7,7 +7,10 @@ public static class DirectoryUtils {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static void Create(string path, bool isFilePath = false) {
|
public static void Create(string path, bool isFilePath = false) {
|
||||||
if (isFilePath) path = Path.GetDirectoryName(path);
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -29,6 +32,7 @@ public static class DirectoryUtils {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static IEnumerable<string> GetFiles(string path, bool topDirectoryOnly = false, string searchPattern = "*") {
|
public static IEnumerable<string> GetFiles(string path, bool topDirectoryOnly = false, string searchPattern = "*") {
|
||||||
if (!Exists(path)) return [];
|
if (!Exists(path)) return [];
|
||||||
|
Logger.Trace($"枚举文件({searchPattern},递归:{!topDirectoryOnly}):{path}");
|
||||||
return Directory.EnumerateFiles(PathUtils.WithLongPath(path), searchPattern, topDirectoryOnly ? SearchOption.TopDirectoryOnly : SearchOption.AllDirectories).
|
return Directory.EnumerateFiles(PathUtils.WithLongPath(path), searchPattern, topDirectoryOnly ? SearchOption.TopDirectoryOnly : SearchOption.AllDirectories).
|
||||||
Select(PathUtils.WithoutLongPath);
|
Select(PathUtils.WithoutLongPath);
|
||||||
}
|
}
|
||||||
@@ -38,6 +42,7 @@ public static class DirectoryUtils {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static IEnumerable<string> GetDirectories(string path, bool topDirectoryOnly = false, string searchPattern = "*") {
|
public static IEnumerable<string> GetDirectories(string path, bool topDirectoryOnly = false, string searchPattern = "*") {
|
||||||
if (!Exists(path)) return [];
|
if (!Exists(path)) return [];
|
||||||
|
Logger.Trace($"枚举文件夹({searchPattern},递归:{!topDirectoryOnly}):{path}");
|
||||||
return Directory.EnumerateDirectories(PathUtils.WithLongPath(path), searchPattern, topDirectoryOnly ? SearchOption.TopDirectoryOnly : SearchOption.AllDirectories).
|
return Directory.EnumerateDirectories(PathUtils.WithLongPath(path), searchPattern, topDirectoryOnly ? SearchOption.TopDirectoryOnly : SearchOption.AllDirectories).
|
||||||
Select(PathUtils.WithoutLongPath);
|
Select(PathUtils.WithoutLongPath);
|
||||||
}
|
}
|
||||||
@@ -57,6 +62,7 @@ public static class DirectoryUtils {
|
|||||||
public static void Move(string sourceDirName, string destDirName) {
|
public static void Move(string sourceDirName, string destDirName) {
|
||||||
if (!Exists(sourceDirName)) return;
|
if (!Exists(sourceDirName)) return;
|
||||||
Create(destDirName);
|
Create(destDirName);
|
||||||
|
Logger.Trace($"剪切文件夹:{sourceDirName} → {destDirName}");
|
||||||
Directory.Move(PathUtils.WithLongPath(sourceDirName), PathUtils.WithLongPath(destDirName));
|
Directory.Move(PathUtils.WithLongPath(sourceDirName), PathUtils.WithLongPath(destDirName));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,8 +70,10 @@ public static class DirectoryUtils {
|
|||||||
/// 删除文件夹。
|
/// 删除文件夹。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static void Delete(string dirPath, bool toRecycleBin = false) {
|
public static void Delete(string dirPath, bool toRecycleBin = false) {
|
||||||
|
if (!Exists(dirPath)) return;
|
||||||
|
Logger.Trace($"{(toRecycleBin ? "将文件夹删除到回收站" : "删除文件夹")}:{dirPath}");
|
||||||
if (toRecycleBin) {
|
if (toRecycleBin) {
|
||||||
if (Exists(dirPath)) FileUtils.DeleteToRecycleBin(dirPath);
|
FileUtils.DeleteToRecycleBin(dirPath);
|
||||||
} else {
|
} else {
|
||||||
Directory.Delete(PathUtils.WithLongPath(dirPath));
|
Directory.Delete(PathUtils.WithLongPath(dirPath));
|
||||||
}
|
}
|
||||||
|
|||||||
+26
-14
@@ -1,4 +1,5 @@
|
|||||||
using System.IO.Compression;
|
using System.IO;
|
||||||
|
using System.IO.Compression;
|
||||||
|
|
||||||
namespace MeloongCore;
|
namespace MeloongCore;
|
||||||
public static class FileUtils {
|
public static class FileUtils {
|
||||||
@@ -10,8 +11,15 @@ public static class FileUtils {
|
|||||||
/// 如果文件或文件夹不存在,则会自动创建。若已存在,则会覆盖原文件。
|
/// 如果文件或文件夹不存在,则会自动创建。若已存在,则会覆盖原文件。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static void Write(string filePath, string text, Encoding? encoding = null) {
|
public static void Write(string filePath, string text, Encoding? encoding = null) {
|
||||||
DirectoryUtils.Create(filePath, isFilePath: true);
|
Write(filePath, (encoding ?? new UTF8Encoding()).GetBytes(text));
|
||||||
File.WriteAllText(PathUtils.WithLongPath(filePath), text, encoding ?? new UTF8Encoding());
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 写入文件。
|
||||||
|
/// 如果文件或文件夹不存在,则会自动创建。若已存在,则会覆盖原文件。
|
||||||
|
/// </summary>
|
||||||
|
public static void Write(string filePath, IEnumerable<byte> content) {
|
||||||
|
Write(filePath, [..content]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -20,18 +28,10 @@ public static class FileUtils {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static void Write(string filePath, byte[] content) {
|
public static void Write(string filePath, byte[] content) {
|
||||||
DirectoryUtils.Create(filePath, isFilePath: true);
|
DirectoryUtils.Create(filePath, isFilePath: true);
|
||||||
|
Logger.Trace($"写入文件:{filePath}({content.Length} 字节)");
|
||||||
File.WriteAllBytes(PathUtils.WithLongPath(filePath), content);
|
File.WriteAllBytes(PathUtils.WithLongPath(filePath), content);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 写入文件。
|
|
||||||
/// 如果文件或文件夹不存在,则会自动创建。若已存在,则会覆盖原文件。
|
|
||||||
/// </summary>
|
|
||||||
public static void Write(string filePath, IEnumerable<byte> content) {
|
|
||||||
DirectoryUtils.Create(filePath, isFilePath: true);
|
|
||||||
File.WriteAllBytes(PathUtils.WithLongPath(filePath), [..content]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 将 <paramref name="stream" /> 写入文件。
|
/// 将 <paramref name="stream" /> 写入文件。
|
||||||
/// 会将流的位置主动重置到开头。
|
/// 会将流的位置主动重置到开头。
|
||||||
@@ -40,6 +40,7 @@ public static class FileUtils {
|
|||||||
public static void Write(string filePath, Stream stream) {
|
public static void Write(string filePath, Stream stream) {
|
||||||
using FileStream fileStream = CreateAsStream(PathUtils.WithLongPath(filePath));
|
using FileStream fileStream = CreateAsStream(PathUtils.WithLongPath(filePath));
|
||||||
if (stream.CanSeek && stream.Position != 0) stream.Seek(0, SeekOrigin.Begin);
|
if (stream.CanSeek && stream.Position != 0) stream.Seek(0, SeekOrigin.Begin);
|
||||||
|
Logger.Trace($"写入文件:{filePath}({stream.GetType().Name} {stream.Length} 字节)");
|
||||||
stream.CopyTo(fileStream);
|
stream.CopyTo(fileStream);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,8 +52,10 @@ public static class FileUtils {
|
|||||||
/// 删除文件。
|
/// 删除文件。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static void Delete(string filePath, bool toRecycleBin = false) {
|
public static void Delete(string filePath, bool toRecycleBin = false) {
|
||||||
|
if (!Exists(filePath)) return;
|
||||||
|
Logger.Trace($"{(toRecycleBin ? "将文件删除到回收站" : "删除文件")}:{filePath}");
|
||||||
if (toRecycleBin) {
|
if (toRecycleBin) {
|
||||||
if (Exists(filePath)) DeleteToRecycleBin(filePath);
|
DeleteToRecycleBin(filePath);
|
||||||
} else {
|
} else {
|
||||||
File.Delete(PathUtils.WithLongPath(filePath));
|
File.Delete(PathUtils.WithLongPath(filePath));
|
||||||
}
|
}
|
||||||
@@ -139,6 +142,7 @@ public static class FileUtils {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static FileStream CreateAsStream(string filePath) {
|
public static FileStream CreateAsStream(string filePath) {
|
||||||
DirectoryUtils.Create(filePath, isFilePath: true);
|
DirectoryUtils.Create(filePath, isFilePath: true);
|
||||||
|
Logger.Trace($"创建文件流:{filePath}");
|
||||||
return new(PathUtils.WithLongPath(filePath), FileMode.Create);
|
return new(PathUtils.WithLongPath(filePath), FileMode.Create);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -152,6 +156,7 @@ public static class FileUtils {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static ZipArchive OpenZip(string zipFilePath) {
|
public static ZipArchive OpenZip(string zipFilePath) {
|
||||||
ZipArchive TryOpen(Encoding encoding) {
|
ZipArchive TryOpen(Encoding encoding) {
|
||||||
|
Logger.Trace($"尝试以 {encoding.EncodingName} 编码打开压缩包:{zipFilePath}");
|
||||||
var result = ZipFile.Open(PathUtils.WithLongPath(zipFilePath), ZipArchiveMode.Read, encoding);
|
var result = ZipFile.Open(PathUtils.WithLongPath(zipFilePath), ZipArchiveMode.Read, encoding);
|
||||||
try {
|
try {
|
||||||
_ = result.Entries; // 如果编码有误,会在这里抛出 DecoderFallbackException;如果文件异常,会在这里抛出 InvalidDataException
|
_ = result.Entries; // 如果编码有误,会在这里抛出 DecoderFallbackException;如果文件异常,会在这里抛出 InvalidDataException
|
||||||
@@ -182,9 +187,11 @@ public static class FileUtils {
|
|||||||
DirectoryUtils.Create(outputDirectory);
|
DirectoryUtils.Create(outputDirectory);
|
||||||
// 解压 gz(gz 不需要考虑编码)
|
// 解压 gz(gz 不需要考虑编码)
|
||||||
if (compressionFile.EndsWithF(".gz", true)) {
|
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 fileStream = FileUtils.ReadAsStream(compressionFile);
|
||||||
using var gzipStream = new GZipStream(fileStream, CompressionMode.Decompress);
|
using var gzipStream = new GZipStream(fileStream, CompressionMode.Decompress);
|
||||||
FileUtils.Write(Path.Combine(outputDirectory, Path.GetFileNameWithoutExtension(compressionFile)), gzipStream);
|
FileUtils.Write(outFilePath, gzipStream);
|
||||||
progressHandler?.Invoke(1);
|
progressHandler?.Invoke(1);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -192,6 +199,7 @@ public static class FileUtils {
|
|||||||
using var archive = FileUtils.OpenZip(compressionFile);
|
using var archive = FileUtils.OpenZip(compressionFile);
|
||||||
int totalCount = archive.Entries.Count;
|
int totalCount = archive.Entries.Count;
|
||||||
int doneCount = 0;
|
int doneCount = 0;
|
||||||
|
Logger.Trace($"解压 zip 文件:{compressionFile} → {outputDirectory}(共 {totalCount} 项)");
|
||||||
foreach (var entry in archive.Entries) {
|
foreach (var entry in archive.Entries) {
|
||||||
doneCount++;
|
doneCount++;
|
||||||
if (totalCount > 0) progressHandler?.Invoke((double) doneCount / totalCount);
|
if (totalCount > 0) progressHandler?.Invoke((double) doneCount / totalCount);
|
||||||
@@ -215,6 +223,7 @@ public static class FileUtils {
|
|||||||
sourceDirectory = PathUtils.WithLongPath(sourceDirectory);
|
sourceDirectory = PathUtils.WithLongPath(sourceDirectory);
|
||||||
DirectoryUtils.Create(outputFullPath, isFilePath: true);
|
DirectoryUtils.Create(outputFullPath, isFilePath: true);
|
||||||
FileUtils.Delete(outputFullPath);
|
FileUtils.Delete(outputFullPath);
|
||||||
|
Logger.Trace($"将文件夹中的内容压缩为 zip 文件:{sourceDirectory} → {outputFullPath}");
|
||||||
ZipFile.CreateFromDirectory(sourceDirectory, outputFullPath);
|
ZipFile.CreateFromDirectory(sourceDirectory, outputFullPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -242,6 +251,7 @@ public static class FileUtils {
|
|||||||
DirectoryUtils.Create(outputFullPath, isFilePath: true);
|
DirectoryUtils.Create(outputFullPath, isFilePath: true);
|
||||||
FileUtils.Delete(outputFullPath);
|
FileUtils.Delete(outputFullPath);
|
||||||
using var archive = ZipFile.Open(outputFullPath, ZipArchiveMode.Create);
|
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);
|
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) {
|
public static void Copy(string sourceFilePath, string destFilePath) {
|
||||||
if (sourceFilePath == destFilePath) return; // 如果复制同一个文件则跳过
|
if (sourceFilePath == destFilePath) return; // 如果复制同一个文件则跳过
|
||||||
DirectoryUtils.Create(destFilePath, isFilePath: true);
|
DirectoryUtils.Create(destFilePath, isFilePath: true);
|
||||||
|
Logger.Trace($"复制文件:{sourceFilePath} → {destFilePath}");
|
||||||
File.Copy(PathUtils.WithLongPath(sourceFilePath), PathUtils.WithLongPath(destFilePath), true);
|
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) {
|
public static void Move(string sourceFilePath, string destFilePath) {
|
||||||
if (sourceFilePath == destFilePath) return; // 如果移动同一个文件则跳过
|
if (sourceFilePath == destFilePath) return; // 如果移动同一个文件则跳过
|
||||||
DirectoryUtils.Create(destFilePath, isFilePath: true);
|
DirectoryUtils.Create(destFilePath, isFilePath: true);
|
||||||
|
Logger.Trace($"剪切文件:{sourceFilePath} → {destFilePath}");
|
||||||
File.Move(PathUtils.WithLongPath(sourceFilePath), PathUtils.WithLongPath(destFilePath));
|
File.Move(PathUtils.WithLongPath(sourceFilePath), PathUtils.WithLongPath(destFilePath));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user