aboutsummaryrefslogtreecommitdiff
path: root/ShiftOS.Engine/ShiftFS
diff options
context:
space:
mode:
Diffstat (limited to 'ShiftOS.Engine/ShiftFS')
-rw-r--r--ShiftOS.Engine/ShiftFS/IShiftNode.cs23
-rw-r--r--ShiftOS.Engine/ShiftFS/ShiftDirectory.cs89
-rw-r--r--ShiftOS.Engine/ShiftFS/ShiftFS.cs80
-rw-r--r--ShiftOS.Engine/ShiftFS/ShiftFile.cs88
-rw-r--r--ShiftOS.Engine/ShiftFS/ShiftFileStream.cs49
-rw-r--r--ShiftOS.Engine/ShiftFS/ShiftTree.cs66
6 files changed, 395 insertions, 0 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
new file mode 100644
index 0000000..249738f
--- /dev/null
+++ b/ShiftOS.Engine/ShiftFS/ShiftDirectory.cs
@@ -0,0 +1,89 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using Whoa;
+
+namespace ShiftOS.Engine.ShiftFS
+{
+ [Serializable]
+ public class ShiftDirectory : List<IShiftNode>, IShiftNode
+ {
+ public ShiftDirectory(string name) => Name = name;
+ public ShiftDirectory(string name, ShiftDirectory parent)
+ {
+ Name = name;
+ Parent = parent;
+ }
+
+
+ public IShiftNode this[string name] => this.First(n => string.Equals(n.Name, name, StringComparison.Ordinal));
+
+
+ public string Name { get; set; }
+
+ public 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 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 )
+ {
+ list.Add(currentNode.Name);
+ currentNode = currentNode.Parent;
+ }
+
+ return Path.Combine(list.Reverse<string>().ToArray());
+ }
+ }
+
+ public ShiftDirectory Parent
+ {
+ get => Drive.FlattenFolders().FirstOrDefault(x => x.Contains(this));
+ set
+ {
+ value.Add(this);
+ Parent?.Remove(this);
+ }
+ }
+
+ public ShiftTree Drive => ShiftFS.Drives.First(d => d.FlattenFolders().Contains(this));
+
+
+ public Guid Guid { get; } = Guid.NewGuid();
+ }
+} \ No newline at end of file
diff --git a/ShiftOS.Engine/ShiftFS/ShiftFS.cs b/ShiftOS.Engine/ShiftFS/ShiftFS.cs
new file mode 100644
index 0000000..de406a7
--- /dev/null
+++ b/ShiftOS.Engine/ShiftFS/ShiftFS.cs
@@ -0,0 +1,80 @@
+using System;
+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
+ {
+ static readonly string FilePath = Path.Combine(Environment.CurrentDirectory, "save.bin");
+
+ static readonly FileSystemWatcher _watcher;
+
+ static readonly BinaryFormatter _formatter = new BinaryFormatter();
+
+ public static EventList<ShiftTree> Drives { get; private set; } = new EventList<ShiftTree>();
+
+ public static void Save()
+ {
+ using (var fs = File.OpenWrite(FilePath))
+ {
+ //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 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);
+ }
+
+ WatcherOnChanged(null, null);
+
+ _watcher = new FileSystemWatcher(Environment.CurrentDirectory)
+ {
+ Filter = "save.bin",
+ };
+
+ _watcher.Changed += WatcherOnChanged;
+ }
+
+ static void WatcherOnChanged(object sender, FileSystemEventArgs e)
+ {
+ 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/ShiftFile.cs b/ShiftOS.Engine/ShiftFS/ShiftFile.cs
new file mode 100644
index 0000000..c8a8ef4
--- /dev/null
+++ b/ShiftOS.Engine/ShiftFS/ShiftFile.cs
@@ -0,0 +1,88 @@
+using System;
+using System.Collections.Generic;
+using System.Drawing;
+using System.IO;
+using System.Linq;
+using Whoa;
+
+namespace ShiftOS.Engine.ShiftFS
+{
+ [Serializable]
+ public class ShiftFile<T> : ShiftFile
+ {
+ public ShiftFile(string name) => Name = name;
+ public ShiftFile(string name, ShiftDirectory directory)
+ {
+ 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 T Object { get; set; }
+ }
+
+ [Serializable]
+ public abstract class ShiftFile : IShiftNode
+ {
+ public Bitmap Icon { get; set; }
+
+ public string Name { get; set; }
+
+ public string FullName
+ {
+ get
+ {
+ 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 ShiftDirectory Parent
+ {
+ 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