From e28547e01fba3dd49f3ce281fa7d3579310124e2 Mon Sep 17 00:00:00 2001 From: zc <1062808664@qq.com> Date: Thu, 19 Dec 2024 23:07:52 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E8=99=9A=E6=8B=9F=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E7=B3=BB=E7=BB=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ZXL/Scripts/FileSystem/DirectoryInfo.cs | 40 +++--- Assets/ZXL/Scripts/FileSystem/FileInfo.cs | 16 +-- Assets/ZXL/Scripts/FileSystem/FileSystem.cs | 125 +++++++++++++++++- .../ZXL/Scripts/FileSystem/FileSystemInfo.cs | 9 +- 4 files changed, 152 insertions(+), 38 deletions(-) diff --git a/Assets/ZXL/Scripts/FileSystem/DirectoryInfo.cs b/Assets/ZXL/Scripts/FileSystem/DirectoryInfo.cs index 71a7d42..93c5fb8 100644 --- a/Assets/ZXL/Scripts/FileSystem/DirectoryInfo.cs +++ b/Assets/ZXL/Scripts/FileSystem/DirectoryInfo.cs @@ -4,17 +4,19 @@ namespace ZXLT.FileSystem { public sealed class DirectoryInfo : FileSystemInfo { - List directories = new List(); - List files = new List(); + // List directories = new List(); + // List files = new List(); public override bool Exists { get; } public override string Name => _name; + public override FileSystemInfoType FileSystemInfoType { get; } public DirectoryInfo Parent { get { - var directoryInfo = new DirectoryInfo(System.IO.Path.GetDirectoryName(FullPath)); + var parentPath = FullPath.Split("\\")[^1]; + var directoryInfo = new DirectoryInfo(System.IO.Path.GetFullPath(parentPath)); return directoryInfo; } } @@ -23,28 +25,18 @@ namespace ZXLT.FileSystem public DirectoryInfo(string path) { - this.Init(path, System.IO.Path.GetFullPath(path), isNormalized: true); - FullPath = path; - _name = path; + FullPath = System.IO.Path.GetFullPath(path); + _name = System.IO.Path.GetDirectoryName(path); } - private void Init(string originalPath, string fullPath = null, string fileName = null, bool isNormalized = false) - { - if (fullPath == null) - fullPath = originalPath; - fullPath = isNormalized ? fullPath : System.IO.Path.GetFullPath(fullPath); - this._name = fileName; - this.FullPath = fullPath; - } - - public DirectoryInfo[] GetDirectories() - { - return directories.ToArray(); - } - - public FileInfo[] GetFiles() - { - return files.ToArray(); - } + // public DirectoryInfo[] GetDirectories() + // { + // return directories.ToArray(); + // } + // + // public FileInfo[] GetFiles() + // { + // return files.ToArray(); + // } } } \ No newline at end of file diff --git a/Assets/ZXL/Scripts/FileSystem/FileInfo.cs b/Assets/ZXL/Scripts/FileSystem/FileInfo.cs index 0b8b612..2ff9529 100644 --- a/Assets/ZXL/Scripts/FileSystem/FileInfo.cs +++ b/Assets/ZXL/Scripts/FileSystem/FileInfo.cs @@ -5,9 +5,10 @@ namespace ZXLT.FileSystem { public sealed class FileInfo : FileSystemInfo { - public string Extension { get; } + public string Extension => Path.GetExtension(this.FullName); public override bool Exists { get; } public override string Name => _name; + public override FileSystemInfoType FileSystemInfoType { get; } public string? DirectoryName => Path.GetDirectoryName(this.FullPath); public DirectoryInfo Directory @@ -20,16 +21,11 @@ namespace ZXLT.FileSystem } public FileInfo(string fileName) - : this(fileName ?? throw new ArgumentNullException(nameof(fileName)), (string)null, (string)null, false) { - } - - internal FileInfo(string originalPath, string fullPath = null, string fileName = null, bool isNormalized = false) - { - if (fullPath == null) - fullPath = originalPath; - this.FullPath = isNormalized ? fullPath ?? originalPath : Path.GetFullPath(fullPath); - this._name = fileName; + fileName = fileName ?? throw new ArgumentNullException(nameof(fileName)); + _name = Path.GetFileName(fileName); + this.FullPath = Path.GetFullPath(fileName); + FileSystemInfoType = FileSystemInfoType.File; } } } \ No newline at end of file diff --git a/Assets/ZXL/Scripts/FileSystem/FileSystem.cs b/Assets/ZXL/Scripts/FileSystem/FileSystem.cs index 8885867..e171212 100644 --- a/Assets/ZXL/Scripts/FileSystem/FileSystem.cs +++ b/Assets/ZXL/Scripts/FileSystem/FileSystem.cs @@ -1,7 +1,126 @@ -namespace ZXLT.FileSystem +using System; +using System.Collections.Generic; +using ZXL.Helper; + +namespace ZXLT.FileSystem { - public class FileSystem + class FileBase { - + } + + class Directory : FileBase + { + DirectoryInfo directoryInfo; + + List directories = new List(); + List files = new List(); + + public Directory(DirectoryInfo directoryInfo) + { + this.directoryInfo = directoryInfo; + } + + public void AddDirectory(Directory dir) + { + directories.Add(dir); + } + + public void AddFile(File file) + { + files.Add(file); + } + + public void RemoveDirectory(Directory dir) + { + directories.Remove(dir); + } + + public void RemoveFile(File file) + { + files.Remove(file); + } + + public void Clear() + { + directories.Clear(); + } + } + + class File : FileBase + { + FileInfo fileInfo; + + public File(FileInfo fileInfo) + { + this.fileInfo = fileInfo; + } + } + + interface IFileSystem + { + event Action OnUpdateFileSystem; + void CreateDirectory(string path); + void DeleteDirectory(string path); + void DeleteFile(string path); + void MoveDirectory(string sourcePath, string destPath); + void MoveFile(string sourcePath, string destPath); + void RenameDirectory(string sourcePath, string destPath); + void RenameFile(string sourcePath, string destPath); + } + + public class FileSystem : IFileSystem + { + Directory rootDirectory; + SortedDictionary fileBases = new SortedDictionary(); + public event Action OnUpdateFileSystem; + + void UpdateFileSystem(FileSystemInfo systemInfo) + { + OnUpdateFileSystem?.Invoke(systemInfo); + } + + public void CreateDirectory(string path) + { + var directory = new Directory(new DirectoryInfo(path)); + fileBases.Add(path, directory); + FileHelper.CreateDirectory(path); + } + + public void CreateFile(string path) + { + var file = new File(new FileInfo(path)); + fileBases.Add(path, file); + FileHelper.CreateDirectory(path); + } + + public void DeleteDirectory(string path) + { + throw new NotImplementedException(); + } + + public void DeleteFile(string path) + { + throw new NotImplementedException(); + } + + public void MoveDirectory(string sourcePath, string destPath) + { + throw new NotImplementedException(); + } + + public void MoveFile(string sourcePath, string destPath) + { + throw new NotImplementedException(); + } + + public void RenameDirectory(string sourcePath, string destPath) + { + throw new NotImplementedException(); + } + + public void RenameFile(string sourcePath, string destPath) + { + throw new NotImplementedException(); + } } } \ No newline at end of file diff --git a/Assets/ZXL/Scripts/FileSystem/FileSystemInfo.cs b/Assets/ZXL/Scripts/FileSystem/FileSystemInfo.cs index 6c1a5a9..77518b5 100644 --- a/Assets/ZXL/Scripts/FileSystem/FileSystemInfo.cs +++ b/Assets/ZXL/Scripts/FileSystem/FileSystemInfo.cs @@ -2,13 +2,20 @@ namespace ZXLT.FileSystem { + public enum FileSystemInfoType + { + File, + Directory, + } + public abstract class FileSystemInfo { protected string _name; protected string FullPath; - public DateTime CreationTime { get; set; } + public DateTime CreationTime { get; } public abstract bool Exists { get; } public abstract string Name { get; } public virtual string FullName => FullPath; + public abstract FileSystemInfoType FileSystemInfoType { get; } } } \ No newline at end of file