EditorTool3D/Assets/ZXL/Scripts/Test/FileSystem.cs

115 lines
3.4 KiB
C#

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;
}
}
}