重构文件系统

master
zxl 2024-12-18 21:55:31 +08:00
parent 5650741955
commit 0e07b7e405
22 changed files with 330 additions and 119 deletions

View File

@ -0,0 +1,7 @@
namespace ZXL.Manager
{
public class ZFileSystem
{
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 63f2843889a64090a60431d1d84b908c
timeCreated: 1734501412

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: ea24213f7e924534aee9ed7f26f0ac48
timeCreated: 1734501440

View File

@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
namespace ZXLA
{
class Directory : FileSystemObject
{
private List<string> _childList;
public Directory(string path, int depth, bool isDisplay = true) : base(path, depth, isDisplay)
{
fileType = FileObjectType.Directory;
_childList = new List<string>();
}
public void AddChild(string child)
{
_childList.Add(child);
}
public void RemoveChild(string child)
{
_childList.Remove(child);
}
public List<string> GetChildren()
{
return _childList;
}
public void Rename(string newPath)
{
}
public void MoveTo(string newPath)
{
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a73e26494a0a4a6ea696904f8b266978
timeCreated: 1734501469

View File

@ -0,0 +1,10 @@
namespace ZXLA
{
class File : FileSystemObject
{
public File(string path, int depth, bool isDisplay = true) : base(path, depth, isDisplay)
{
fileType = FileObjectType.File;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d048818bfa77481cae5bc70c5e944327
timeCreated: 1734501462

View File

@ -0,0 +1,180 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using ZXL;
namespace ZXLA
{
public enum FileOperationType
{
Create,
Rename,
Move,
Delete,
}
public enum FileObjectType
{
File,
Directory
}
public class FileSystem : MonoBehaviour, IFileSystem
{
private SortedDictionary<string, FileSystemObject> dic = new SortedDictionary<string, FileSystemObject>();
public event Action<string> OnCeateFile;
public event Action<string> OnCeateDirectory;
public event Action<FileSystemOperation> OnFileSystemChanged;
public void CreateFile(string path, int parentDepth)
{
dic.Add(path, new File(path, parentDepth + 1));
OnFileSystemChanged?.Invoke(new FileSystemOperation(FileObjectType.File, FileOperationType.Create, null,
path, null, null));
}
public bool FileExists(string path)
{
return dic.ContainsKey(path);
}
public void RenameFile(string path, string newName)
{
var fileSystemObject = TmpFile(path);
DeleteFileInternal(path);
var s = path.Split("/")[^1];
var replace = path.Replace(s, newName);
dic.Add(replace, new File(replace, fileSystemObject.depth + 1, fileSystemObject.isDisplay));
OnFileSystemChanged?.Invoke(new FileSystemOperation(FileObjectType.File,
FileOperationType.Rename, null, replace, null, null));
}
public void MoveFile(string oldPath, string newPath)
{
var fileSystemObject = TmpFile(oldPath);
DeleteFileInternal(oldPath);
dic.Add(newPath, new File(newPath, fileSystemObject.depth + 1, fileSystemObject.isDisplay));
OnFileSystemChanged?.Invoke(new FileSystemOperation(FileObjectType.File,
FileOperationType.Move, null, newPath, null, null));
}
public void DeleteFile(string path)
{
DeleteFileInternal(path);
OnFileSystemChanged?.Invoke(new FileSystemOperation(FileObjectType.File, FileOperationType.Delete, null,
path, null, null));
}
/// <summary>
/// 缓存一下
/// </summary>
FileSystemObject tmpFileSystemObject;
FileSystemObject TmpFile(string path)
{
tmpFileSystemObject = new FileSystemObject(null, -1);
tmpFileSystemObject.path = dic[path].path;
tmpFileSystemObject.depth = dic[path].depth;
tmpFileSystemObject.fileType = dic[path].fileType;
tmpFileSystemObject.isDisplay = dic[path].isDisplay;
return tmpFileSystemObject;
}
private void DeleteFileInternal(string path)
{
dic.Remove(path);
}
private List<FileSystemObject> tmpFileSystemObjects;
FileSystemObject TmpDirectory(string path)
{
tmpFileSystemObject = new FileSystemObject(null, -1);
tmpFileSystemObject.path = dic[path].path;
tmpFileSystemObject.depth = dic[path].depth;
tmpFileSystemObject.fileType = dic[path].fileType;
tmpFileSystemObject.isDisplay = dic[path].isDisplay;
return tmpFileSystemObject;
}
List<FileSystemObject> TmpDirectoryWithFiles(string path)
{
tmpFileSystemObjects ??= new List<FileSystemObject>();
tmpFileSystemObjects.Clear();
var keys = dic.Keys.ToListPooled();
var indexOf = keys.IndexOf(path);
for (int i = indexOf + 1; i < keys.Count; i++)
{
if (dic[keys[i]] is not File)
break;
var fileSystemObject = TmpFile(keys[i]);
tmpFileSystemObjects.Add(fileSystemObject);
DeleteFileInternal(keys[i]);
}
keys.Free();
return tmpFileSystemObjects;
}
public void CreateDirectory(string path, int parentDepth)
{
dic.Add(path, new Directory(path, parentDepth + 1));
OnFileSystemChanged?.Invoke(new FileSystemOperation(FileObjectType.Directory, FileOperationType.Create,
null, path, null, null));
}
public bool DirectoryExists(string path)
{
return dic.ContainsKey(path);
}
public void RenameDirectory(string path, string newName)
{
var s = path.Split("/")[^1];
var replace = path.Replace(s, newName);
var fileSystemObject = TmpDirectory(path);
dic.Remove(path);
dic.Add(replace, fileSystemObject);
OnFileSystemChanged?.Invoke(new FileSystemOperation(FileObjectType.Directory, FileOperationType.Rename,
null, replace, null, null));
}
public void MoveDirectory(string oldPath, string newPath)
{
var tmpDirectoryWithFiles = TmpDirectoryWithFiles(oldPath);
DeleteDirectory(oldPath);
var depth = dic[newPath].depth;
foreach (var tmpDirectoryWithFile in tmpDirectoryWithFiles)
{
var replace = tmpDirectoryWithFile.path.Replace(oldPath, newPath);
var tmp = new FileSystemObject(replace, depth + 1, tmpDirectoryWithFile.isDisplay);
dic.Add(replace, tmp);
}
}
public void DeleteDirectory(string path)
{
var keys = dic.Keys.ToListPooled();
var indexOf = keys.IndexOf(path);
for (int i = indexOf + 1; i < keys.Count; i++)
{
if (dic[keys[i]] is not File)
break;
DeleteFileInternal(keys[i]);
}
keys.Free();
dic.Remove(path);
OnFileSystemChanged?.Invoke(new FileSystemOperation(FileObjectType.Directory, FileOperationType.Delete,
null, path, null, null));
}
public IReadOnlyDictionary<string, FileSystemObject> GetAllObjects()
{
return this.dic;
}
}
}

View File

@ -0,0 +1,19 @@
namespace ZXLA
{
public class FileSystemObject
{
public string path;
public int depth;
public bool isDisplay;
public FileObjectType fileType;
public string name;
public FileSystemObject(string path, int depth, bool isDisplay = true)
{
this.path = path;
this.depth = depth;
this.isDisplay = isDisplay;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f8e4580806ac4499b6753aee0ac8ff60
timeCreated: 1734501454

View File

@ -0,0 +1,25 @@
using System.Collections.Generic;
namespace ZXLA
{
public class FileSystemOperation
{
public FileObjectType FileObjectType { get; }
public FileOperationType FileOperationType { get; }
public string OldPath { get; }
public string NewPath { get; }
public IReadOnlyList<string> OldPaths { get; }
public IReadOnlyList<string> NewPaths { get; }
public FileSystemOperation(FileObjectType fileObjectType, FileOperationType fileOperationType, string oldPath,
string newPath, IReadOnlyList<string> oldPaths, IReadOnlyList<string> newPaths)
{
FileObjectType = fileObjectType;
FileOperationType = fileOperationType;
OldPath = oldPath;
NewPath = newPath;
OldPaths = oldPaths;
NewPaths = newPaths;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 161d78b2cedf4a46a6fd6e281ace7f3d
timeCreated: 1734501489

View File

@ -0,0 +1,23 @@
using System;
namespace ZXLA
{
public interface IFileSystem
{
// event Action<string> OnCreateFile;
// event Action<string> OnCreateDirectory;
event Action<FileSystemOperation> OnFileSystemChanged;
void CreateFile(string path, int parentDepth);
bool FileExists(string path);
void RenameFile(string path, string newName);
void MoveFile(string oldPath, string newPath);
void DeleteFile(string path);
void CreateDirectory(string path, int parentDepth);
bool DirectoryExists(string path);
void RenameDirectory(string path, string newName);
void MoveDirectory(string oldPath, string newPath);
void DeleteDirectory(string path);
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 589df65e86a744b8b56776720fbfad21
timeCreated: 1734501482

View File

@ -1,115 +0,0 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using ZXL;
namespace ZXLA
{
public enum FileOperationType
{
Create,
Delete
}
public enum FileObjectType
{
File,
Directory
}
public class FileSystemOperation
{
public FileObjectType FileObjectType { get; }
public FileOperationType FileOperationType { get; }
public string OldPath { get; }
public string NewPath { get; }
public IReadOnlyList<string> OldPaths { get; }
public IReadOnlyList<string> NewPaths { get; }
public FileSystemOperation(FileObjectType fileObjectType, FileOperationType fileOperationType, string oldPath, string newPath, IReadOnlyList<string> oldPaths, IReadOnlyList<string> newPaths)
{
FileObjectType = fileObjectType;
FileOperationType = fileOperationType;
OldPath = oldPath;
NewPath = newPath;
OldPaths = oldPaths;
NewPaths = newPaths;
}
}
public class FileSystem : MonoBehaviour
{
private SortedDictionary<string, FileSystemObject> dic = new SortedDictionary<string, FileSystemObject>();
public event Action<string> OnCeateFile;
public event Action<string> OnCeateDirectory;
public event Action<FileSystemOperation> OnFileSystemChanged;
public void CreateFile(string path, int parentDepth)
{
dic.Add(path, new File() { path = path, depth = parentDepth + 1 });
OnFileSystemChanged?.Invoke(new FileSystemOperation(FileObjectType.File, FileOperationType.Create, null, path, null, null));
}
public void CreateDirectory(string path, int parentDepth)
{
dic.Add(path, new Directory() { path = path, depth = parentDepth + 1 });
OnFileSystemChanged?.Invoke(new FileSystemOperation(FileObjectType.Directory, FileOperationType.Create, null, path, null, null));
}
public void DeleteDirectory(string path)
{
var keys = dic.Keys.ToListPooled();
var indexOf = keys.IndexOf(path);
for (int i = indexOf + 1; i < keys.Count; i++)
{
if (dic[keys[i]] is not File)
break;
DeleteFileInternal(keys[i]);
}
keys.Free();
dic.Remove(path);
OnFileSystemChanged?.Invoke(new FileSystemOperation(FileObjectType.Directory, FileOperationType.Delete, null, path, null, null));
}
public void DeleteFile(string path)
{
DeleteFileInternal(path);
OnFileSystemChanged?.Invoke(new FileSystemOperation(FileObjectType.File, FileOperationType.Delete, null, path, null, null));
}
private void DeleteFileInternal(string path)
{
dic.Remove(path);
}
public IReadOnlyDictionary<string, FileSystemObject> GetAllObjects()
{
return this.dic;
}
}
public class FileSystemObject
{
public string path;
public int depth;
public bool isDisplay;
public FileObjectType fileType;
}
class File : FileSystemObject
{
private void Awake()
{
fileType = FileObjectType.File;
}
}
class Directory : FileSystemObject
{
private void Awake()
{
fileType = FileObjectType.Directory;
}
}
}

View File

@ -1,19 +1,19 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Assembly-CSharp-Editor", "Assembly-CSharp-Editor.csproj", "{17979be4-dee9-189e-5d07-fdd058a166b7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Assembly-CSharp", "Assembly-CSharp.csproj", "{005d29ad-e0bd-eb88-4469-295a9a5f266c}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Assembly-CSharp-Editor", "Assembly-CSharp-Editor.csproj", "{17979be4-dee9-189e-5d07-fdd058a166b7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{17979be4-dee9-189e-5d07-fdd058a166b7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{17979be4-dee9-189e-5d07-fdd058a166b7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{005d29ad-e0bd-eb88-4469-295a9a5f266c}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{005d29ad-e0bd-eb88-4469-295a9a5f266c}.Debug|Any CPU.Build.0 = Debug|Any CPU
{17979be4-dee9-189e-5d07-fdd058a166b7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{17979be4-dee9-189e-5d07-fdd058a166b7}.Debug|Any CPU.Build.0 = Debug|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -1,2 +1,4 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ADirectoryInfo_002Ecs_002Fl_003AC_0021_003FUsers_003FAdministrator_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F5a41d6b7189842eca409fd0b1c3e3dcf17bf78_003Fb9_003F5b634a7f_003FDirectoryInfo_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AFileInfo_002Ecs_002Fl_003AC_0021_003FUsers_003FAdministrator_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F5a41d6b7189842eca409fd0b1c3e3dcf17bf78_003Fe1_003Fb009330d_003FFileInfo_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ASortedDictionary_00602_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003FUsers_003F10628_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F5a41d6b7189842eca409fd0b1c3e3dcf17bf78_003F9b_003Fdfd6117f_003FSortedDictionary_00602_002Ecs/@EntryIndexedValue">ForceIncluded</s:String></wpf:ResourceDictionary>