diff options
Diffstat (limited to 'ShiftOS.Engine/ShiftFS')
| -rw-r--r-- | ShiftOS.Engine/ShiftFS/IShiftNode.cs | 23 | ||||
| -rw-r--r-- | ShiftOS.Engine/ShiftFS/ShiftDirectory.cs | 89 | ||||
| -rw-r--r-- | ShiftOS.Engine/ShiftFS/ShiftDrive.cs | 20 | ||||
| -rw-r--r-- | ShiftOS.Engine/ShiftFS/ShiftFS.cs | 74 | ||||
| -rw-r--r-- | ShiftOS.Engine/ShiftFS/ShiftFSObject.cs | 25 | ||||
| -rw-r--r-- | ShiftOS.Engine/ShiftFS/ShiftFile.cs | 94 | ||||
| -rw-r--r-- | ShiftOS.Engine/ShiftFS/ShiftFileStream.cs | 49 | ||||
| -rw-r--r-- | ShiftOS.Engine/ShiftFS/ShiftTree.cs | 66 |
8 files changed, 329 insertions, 111 deletions
diff --git a/ShiftOS.Engine/ShiftFS/IShiftNode.cs b/ShiftOS.Engine/ShiftFS/IShiftNode.cs new file mode 100644 index 0000000..f3a1a19 --- /dev/null +++ b/ShiftOS.Engine/ShiftFS/IShiftNode.cs @@ -0,0 +1,23 @@ +using System; +using Whoa; + +namespace ShiftOS.Engine.ShiftFS +{ + public interface IShiftNode + { + + string Name { get; set; } + + + string FullName { get; } + + + ShiftDirectory Parent { get; set; } + + + ShiftTree Drive { get; } + + + Guid Guid { get; } + } +}
\ No newline at end of file diff --git a/ShiftOS.Engine/ShiftFS/ShiftDirectory.cs b/ShiftOS.Engine/ShiftFS/ShiftDirectory.cs index 64ac4f2..249738f 100644 --- a/ShiftOS.Engine/ShiftFS/ShiftDirectory.cs +++ b/ShiftOS.Engine/ShiftFS/ShiftDirectory.cs @@ -1,50 +1,89 @@ -using System.Collections.ObjectModel; +using System; +using System.Collections.Generic; using System.IO; using System.Linq; +using Whoa; namespace ShiftOS.Engine.ShiftFS { - public class ShiftDirectory : ShiftFsObject + [Serializable] + public class ShiftDirectory : List<IShiftNode>, IShiftNode { - public ShiftDirectory(string path) : base(path) + public ShiftDirectory(string name) => Name = name; + public ShiftDirectory(string name, ShiftDirectory parent) { - path = path.Replace(ShiftFs.SavePath, ""); - var dir = new DirectoryInfo(Path.Combine(ShiftFs.SavePath, path)); - Name = dir.Name; - FullDiskName = dir.FullName; - FullName = path; - - Children.CollectionChanged += (sender, e) => { }; + Name = name; + Parent = parent; } - public ShiftFsObject this[string name] => Children.First(f => f.Name == name); - public ShiftFsObject this[int index] => Children[index]; - public new ShiftDirectory Parent => new ShiftDirectory(new DirectoryInfo(FullDiskName).Parent.FullName); + public IShiftNode this[string name] => this.First(n => string.Equals(n.Name, name, StringComparison.Ordinal)); + + + public string Name { get; set; } - public ObservableCollection<ShiftFsObject> Children + public IEnumerable<ShiftFile> Flatten() { - get + foreach (var item in this) { - var collection = new ObservableCollection<ShiftFsObject>(); - - foreach (var dir in new DirectoryInfo(Path.Combine(ShiftFs.SavePath, FullName)).EnumerateDirectories()) + switch (item) { - collection.Add(new ShiftDirectory(dir.FullName)); + case ShiftFile file: + yield return file; + break; + case ShiftDirectory dir: + foreach (var shiftNode in dir.Flatten()) + { + yield return shiftNode; + } + break; } + } + } - foreach (var file in new DirectoryInfo(Path.Combine(ShiftFs.SavePath, FullName)).EnumerateFiles()) + public IEnumerable<ShiftDirectory> FlattenFolders() + { + foreach (var item in this) + { + if (!(item is ShiftDirectory dir)) continue; + yield return dir; + + foreach (var subdir in dir.FlattenFolders()) + { + yield return subdir; + } + } + } + + public string FullName + { + get + { + var list = new List<string> { Name }; + var currentNode = Parent; + while (currentNode?.Parent != null ) { - collection.Add(new ShiftFile(file.FullName.Replace(ShiftFs.SavePath, ""))); + list.Add(currentNode.Name); + currentNode = currentNode.Parent; } + + return Path.Combine(list.Reverse<string>().ToArray()); + } + } - return collection; + public ShiftDirectory Parent + { + get => Drive.FlattenFolders().FirstOrDefault(x => x.Contains(this)); + set + { + value.Add(this); + Parent?.Remove(this); } } - public ObservableCollection<ShiftFile> Files => new ObservableCollection<ShiftFile>(Children.OfType<ShiftFile>()); + public ShiftTree Drive => ShiftFS.Drives.First(d => d.FlattenFolders().Contains(this)); - public ObservableCollection<ShiftDirectory> Directories - => new ObservableCollection<ShiftDirectory>(Children.OfType<ShiftDirectory>()); + + public Guid Guid { get; } = Guid.NewGuid(); } }
\ No newline at end of file diff --git a/ShiftOS.Engine/ShiftFS/ShiftDrive.cs b/ShiftOS.Engine/ShiftFS/ShiftDrive.cs deleted file mode 100644 index cde9e8d..0000000 --- a/ShiftOS.Engine/ShiftFS/ShiftDrive.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System.IO; -using ShiftOS.Engine.Misc; - -namespace ShiftOS.Engine.ShiftFS -{ - public class ShiftDrive - { - internal ShiftDrive(DirectoryInfo dir) - { - Label = dir.Name; - var file = new IniFile(Path.Combine(dir.FullName, "driveinfo.ini")); - Letter = char.TryParse(file.ReadValue("", "DriveLetter"), out var letter) ? letter : '?'; - Contents = new ShiftDirectory(dir.FullName); - } - - public string Label { get; } - public char Letter { get; } - public ShiftDirectory Contents { get; } - } -}
\ No newline at end of file diff --git a/ShiftOS.Engine/ShiftFS/ShiftFS.cs b/ShiftOS.Engine/ShiftFS/ShiftFS.cs index d188bee..de406a7 100644 --- a/ShiftOS.Engine/ShiftFS/ShiftFS.cs +++ b/ShiftOS.Engine/ShiftFS/ShiftFS.cs @@ -1,34 +1,80 @@ using System; -using System.Collections.ObjectModel; +using System.Collections.Generic; +using System.Diagnostics; using System.IO; +using System.Runtime.Serialization.Formatters.Binary; +using System.Windows.Forms; +using ShiftOS.Engine.Misc; namespace ShiftOS.Engine.ShiftFS { - public static class ShiftFs + public static class ShiftFS { - internal static readonly string SavePath = Path.Combine(Environment.CurrentDirectory, "Save") + "\\"; + static readonly string FilePath = Path.Combine(Environment.CurrentDirectory, "save.bin"); - public static ObservableCollection<ShiftDrive> Drives = new ObservableCollection<ShiftDrive>(); + static readonly FileSystemWatcher _watcher; + + static readonly BinaryFormatter _formatter = new BinaryFormatter(); + + public static EventList<ShiftTree> Drives { get; private set; } = new EventList<ShiftTree>(); - static ShiftFs() + public static void Save() { - if (Directory.Exists(SavePath)) + using (var fs = File.OpenWrite(FilePath)) { - var info = new DirectoryInfo(SavePath); - foreach (var dir in info.EnumerateDirectories()) + //Whoa.Whoa.SerialiseObject(fs, Drives); + _formatter.Serialize(fs, Drives); + } + } + + + static ShiftFS() + { + Drives.ItemAdded += (sender, e) => Debug.WriteLine(e.Item.Name + e.Item.Letter); + + if (!File.Exists(FilePath)) + { + using (File.Create(FilePath)) { - Drives.Add(new ShiftDrive(dir)); + + Drives.Add(new ShiftTree("Local Disk", 'C') + { + new ShiftDirectory("usr") + { + //i'll put in extensions later + new ShiftFile<string>("stringfile.txt", "THIS IS SECRETEXT") + }, + new ShiftDirectory("libs") + { + new ShiftFile<string>("thing.dll", "oh no it's not code FACH") + } + + }); } + + Save(); + + MessageBox.Show("Save file created."); + Debug.WriteLine("Drives: " + Drives.Count); } - else + + WatcherOnChanged(null, null); + + _watcher = new FileSystemWatcher(Environment.CurrentDirectory) { - CreateSaveFile(); - } + Filter = "save.bin", + }; + + _watcher.Changed += WatcherOnChanged; } - public static void CreateSaveFile() + static void WatcherOnChanged(object sender, FileSystemEventArgs e) { - throw new NotImplementedException(); + using (var fs = File.OpenRead(FilePath)) + { + //Drives = Whoa.Whoa.DeserialiseObject<EventList<ShiftTree>>(fs); + Drives = (EventList<ShiftTree>) _formatter.Deserialize(fs); + } } } }
\ No newline at end of file diff --git a/ShiftOS.Engine/ShiftFS/ShiftFSObject.cs b/ShiftOS.Engine/ShiftFS/ShiftFSObject.cs deleted file mode 100644 index 69750fa..0000000 --- a/ShiftOS.Engine/ShiftFS/ShiftFSObject.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System.IO; - -namespace ShiftOS.Engine.ShiftFS -{ - public abstract class ShiftFsObject - { - protected ShiftFsObject(string path) - { - if (!File.Exists(Path.Combine(ShiftFs.SavePath, path)) && !Directory.Exists(Path.Combine(ShiftFs.SavePath, path))) - { - throw new FileNotFoundException(); - } - } - - public string Name { get; set; } - public ShiftDirectory Parent { get; protected set; } - public string FullName { get; set; } - protected string FullDiskName { get; set; } - - public void Delete() - { - File.Delete(FullDiskName); - } - } -}
\ No newline at end of file diff --git a/ShiftOS.Engine/ShiftFS/ShiftFile.cs b/ShiftOS.Engine/ShiftFS/ShiftFile.cs index 1a2413c..c8a8ef4 100644 --- a/ShiftOS.Engine/ShiftFS/ShiftFile.cs +++ b/ShiftOS.Engine/ShiftFS/ShiftFile.cs @@ -1,48 +1,88 @@ using System; +using System.Collections.Generic; +using System.Drawing; using System.IO; -using System.Runtime.Serialization.Formatters.Binary; +using System.Linq; +using Whoa; namespace ShiftOS.Engine.ShiftFS { - public class ShiftFile : ShiftFsObject + [Serializable] + public class ShiftFile<T> : ShiftFile { - public ShiftFile(string path) : base(path) + public ShiftFile(string name) => Name = name; + public ShiftFile(string name, ShiftDirectory directory) { - path = path.Replace(ShiftFs.SavePath, ""); - - using (var fs = new FileStream(Path.Combine(ShiftFs.SavePath, path), FileMode.Open)) - { - Bytes = new MemoryStream(); - fs.CopyTo(Bytes); - } - - var file = new FileInfo(Path.Combine(ShiftFs.SavePath, path)); - - Name = file.Name; - FullDiskName = file.FullName; - FullName = path; + Name = name; + Parent = directory; + } + public ShiftFile(string name, T @object, ShiftDirectory directory) + { + Name = name; + Object = @object; + Parent = directory; + } + public ShiftFile(string name, T @object, ShiftDirectory directory, Bitmap icon) + { + Name = name; + Object = @object; + Parent = directory; + Icon = icon; + } + public ShiftFile(string name, T @object) + { + Name = name; + Object = @object; } + public ShiftFile(string name, T @object, Bitmap icon) + { + Name = name; + Object = @object; + Icon = icon; + } + - public new ShiftDirectory Parent => new ShiftDirectory(new FileInfo(FullDiskName).Directory.FullName); - public MemoryStream Bytes { get; set; } - public long Length => Bytes.Length; + public T Object { get; set; } } - public class ShiftFile<T> : ShiftFile // please C# gods let us constrain to attributes + [Serializable] + public abstract class ShiftFile : IShiftNode { - public ShiftFile(string path) : base(path) + public Bitmap Icon { get; set; } + + public string Name { get; set; } + + public string FullName { - if (!typeof(T).IsSerializable) + get { - throw new InvalidOperationException("Non-serializable types are not supported"); + var list = new List<string> { Name }; + var currentNode = Parent; + while (currentNode?.Parent != null) + { + list.Add(currentNode.Name); + currentNode = currentNode.Parent; + } + + return Path.Combine(list.Reverse<string>().ToArray()) + "\\"; } } - - public T Object + + public ShiftDirectory Parent { - get => (T) new BinaryFormatter().Deserialize(Bytes); - set => new BinaryFormatter().Serialize(Bytes, value); + get => Drive.FlattenFolders().FirstOrDefault(x => x.Contains(this)); + set + { + value.Add(this); + Parent?.Remove(this); + } } + + public ShiftTree Drive => ShiftFS.Drives.First(d => d.FlattenFolders().FirstOrDefault(f => f.Contains(this)) != null); + + + public Guid Guid { get; } = Guid.NewGuid(); + } }
\ No newline at end of file diff --git a/ShiftOS.Engine/ShiftFS/ShiftFileStream.cs b/ShiftOS.Engine/ShiftFS/ShiftFileStream.cs new file mode 100644 index 0000000..9d81a28 --- /dev/null +++ b/ShiftOS.Engine/ShiftFS/ShiftFileStream.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Security.AccessControl; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Win32.SafeHandles; + +namespace ShiftOS.Engine.ShiftFS +{ + /// <summary> + /// To be implemented + /// </summary> + class ShiftFileStream : Stream + { + public ShiftFileStream() => throw new NotImplementedException(); + + /// <inheritdoc /> + public override void Flush() => throw new NotImplementedException(); + + /// <inheritdoc /> + public override long Seek(long offset, SeekOrigin origin) => throw new NotImplementedException(); + + /// <inheritdoc /> + public override void SetLength(long value) => throw new NotImplementedException(); + + /// <inheritdoc /> + public override int Read(byte[] buffer, int offset, int count) => throw new NotImplementedException(); + + /// <inheritdoc /> + public override void Write(byte[] buffer, int offset, int count) => throw new NotImplementedException(); + + /// <inheritdoc /> + public override bool CanRead { get; } + + /// <inheritdoc /> + public override bool CanSeek { get; } + + /// <inheritdoc /> + public override bool CanWrite { get; } + + /// <inheritdoc /> + public override long Length { get; } + + /// <inheritdoc /> + public override long Position { get; set; } + } +} diff --git a/ShiftOS.Engine/ShiftFS/ShiftTree.cs b/ShiftOS.Engine/ShiftFS/ShiftTree.cs new file mode 100644 index 0000000..33c300a --- /dev/null +++ b/ShiftOS.Engine/ShiftFS/ShiftTree.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using Whoa; + +namespace ShiftOS.Engine.ShiftFS +{ + [Serializable] + public class ShiftTree : ShiftDirectory, IShiftNode + { + public ShiftTree(string name, char letter) : base(name) + { + Name = name; + Letter = letter; + } + + + public new IEnumerable<ShiftFile> Flatten() + { + foreach (var item in this) + { + switch (item) + { + case ShiftFile file: + yield return file; + break; + case ShiftDirectory dir: + foreach (var shiftNode in dir.Flatten()) + { + yield return shiftNode; + } + break; + } + } + } + + public new IEnumerable<ShiftDirectory> FlattenFolders() + { + foreach (var item in this) + { + if (!(item is ShiftDirectory dir)) continue; + yield return dir; + + foreach (var subdir in dir.FlattenFolders()) + { + yield return subdir; + } + } + } + + + public new string Name { get; set; } + + + public char Letter { get; } + + public new string FullName => $@"{Name}:\"; + + public new ShiftDirectory Parent + { + get => null; + set => throw new InvalidOperationException("Cannot set parent of ShiftTree"); + } + + public new ShiftTree Drive => this; + } +}
\ No newline at end of file |
