Explorar o código

迁移部分常用路径

龙腾猫跃 hai 4 meses
pai
achega
966b2db312
Modificáronse 5 ficheiros con 28 adicións e 12 borrados
  1. 1 1
      Shared/General/Logger.cs
  2. 3 1
      Shared/Main.cs
  3. 1 1
      Shared/Utils/DirectoryUtils.cs
  4. 21 7
      Shared/Utils/PathUtils.cs
  5. 2 2
      Tests/TestUtils.cs

+ 1 - 1
Shared/General/Logger.cs

@@ -291,7 +291,7 @@ public class BaseLogger {
 /// </summary>
 public class FileLogger : BaseLogger, IDisposable {
 
-    public string logFolder = Path.Combine(PathUtils.CurrentFolder, "logs");
+    public string logFolder = Path.Combine(Paths.Base, "logs");
     public string fileNamePrefix = "Log";
     public string fileNameSuffix = ".txt";
     private StreamWriter? writer;

+ 3 - 1
Shared/Main.cs

@@ -1,7 +1,9 @@
 namespace MeloongCore;
 public static class Main {
 
-    public static void Init(BaseLogger? logger = null) {
+    public static string AppName = "Meloong";
+    public static void Init(string appName, BaseLogger? logger = null) {
+        AppName = appName;
         AppDomain.CurrentDomain.SetData("REGEX_DEFAULT_MATCH_TIMEOUT", TimeSpan.FromSeconds(5));
         if (logger != null) Logger.Instance = logger;
     }

+ 1 - 1
Shared/Utils/DirectoryUtils.cs

@@ -173,7 +173,7 @@ public static class DirectoryUtils {
             throw new UnauthorizedAccessException($"不应操作文件夹:{folder}");
     }
     private static readonly Lazy<HashSet<string>> criticalFolders = new(() => new(new[] {
-        PathUtils.CurrentFolder,
+        Paths.Base,
         Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
         Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),
         Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),

+ 21 - 7
Shared/Utils/PathUtils.cs

@@ -173,13 +173,27 @@ public static class PathUtils {
 
     #endregion
 
-    #region 常用路径
+}
 
+/// <summary>
+/// 常用路径合集。
+/// 所有路径均以分隔符结尾。
+/// </summary>
+public static class Paths {
     /// <summary>
-    /// 程序可执行文件的所在文件夹,以 \ 结尾。
+    /// 可执行文件所在的文件夹。
     /// </summary>
-    public static string CurrentFolder => PathUtils.AddSlashSuffix(AppDomain.CurrentDomain.BaseDirectory);
-
-    #endregion
-
-}
+    public static string Base => PathUtils.AddSlashSuffix(AppContext.BaseDirectory);
+    /// <summary>
+    /// 可执行文件所在的文件夹中,名为 <see cref="Main.AppName"/> 的子文件夹。
+    /// </summary>
+    public static string BaseThenName => PathUtils.AddSlashSuffix(Base + Main.AppName);
+    /// <summary>
+    /// %AppData% 文件夹。
+    /// </summary>
+    public static string AppData => PathUtils.AddSlashSuffix(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
+    /// <summary>
+    /// %AppData% 文件夹中,名为 <see cref="Main.AppName"/> 的子文件夹。
+    /// </summary>
+    public static string AppDataThenName => PathUtils.AddSlashSuffix(AppData + Main.AppName);
+}

+ 2 - 2
Tests/TestUtils.cs

@@ -7,7 +7,7 @@
 public abstract class TestBase {
 
     public TestBase() {
-        MeloongCore.Main.Init(new TestLogger());
+        MeloongCore.Main.Init("MeloongCoreTest", new TestLogger());
     }
     private class TestLogger : BaseLogger {
         public override void HandleBehavior(string? rawMessage, string formattedMessage, LogBehavior behavior, Exception? ex)
@@ -40,7 +40,7 @@ public abstract class TestWithFolder : TestBase {
     /// 输出指定的测试用文件,返回文件路径。
     /// </summary>
     public string GetTestFile(string fileName) {
-        var sourceFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestFiles", GetType().Name.Replace("Test", ""), fileName);
+        var sourceFilePath = Path.Combine(Paths.Base, "TestFiles", GetType().Name.Replace("Test", ""), fileName);
         var distPath = Path.Combine(tempFolder, PathUtils.GetLastPart(sourceFilePath));
         FileUtils.Copy(sourceFilePath, distPath);
         return distPath;