Browse Source

add : FileSystem.BaseDirectory

Akeit0 6 months ago
parent
commit
f8bb5328fb
1 changed files with 19 additions and 4 deletions
  1. 19 4
      src/Lua/IO/FileSystem.cs

+ 19 - 4
src/Lua/IO/FileSystem.cs

@@ -1,7 +1,9 @@
 namespace Lua.IO
 {
-    public sealed class FileSystem : ILuaFileSystem
+    public sealed class FileSystem(string? baseDirectory = null) : ILuaFileSystem
     {
+        public string BaseDirectory => baseDirectory ?? Directory.GetCurrentDirectory();
+
         public static (FileMode, FileAccess access) GetFileMode(LuaFileOpenMode luaFileOpenMode)
         {
             return luaFileOpenMode switch
@@ -16,9 +18,19 @@
             };
         }
 
+        public string GetFullPath(string path)
+        {
+            if (baseDirectory == null || Path.IsPathFullyQualified(path))
+            {
+                return path;
+            }
+
+            return Path.Combine(baseDirectory, path);
+        }
+
         public bool IsReadable(string path)
         {
-            return File.Exists(path);
+            return File.Exists(GetFullPath(path));
         }
 
 
@@ -26,7 +38,7 @@
         {
             var (mode, access) = GetFileMode(openMode);
             Stream stream;
-
+            path = GetFullPath(path);
             if (openMode == LuaFileOpenMode.AppendUpdate)
             {
                 stream = File.Open(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite | FileShare.Delete);
@@ -41,7 +53,7 @@
 
             if (openMode == LuaFileOpenMode.AppendUpdate)
             {
-                wrapper.Seek(SeekOrigin.End,0);
+                wrapper.Seek(SeekOrigin.End, 0);
             }
 
             return new(wrapper);
@@ -49,6 +61,8 @@
 
         public ValueTask Rename(string oldName, string newName, CancellationToken cancellationToken)
         {
+            oldName = GetFullPath(oldName);
+            newName = GetFullPath(newName);
             if (oldName == newName) return default;
             if (File.Exists(newName)) File.Delete(newName);
             File.Move(oldName, newName);
@@ -58,6 +72,7 @@
 
         public ValueTask Remove(string path, CancellationToken cancellationToken)
         {
+            path = GetFullPath(path);
             File.Delete(path);
             return default;
         }