aboutsummaryrefslogtreecommitdiff
path: root/ShiftOS.Engine/ShiftFS
diff options
context:
space:
mode:
Diffstat (limited to 'ShiftOS.Engine/ShiftFS')
-rw-r--r--ShiftOS.Engine/ShiftFS/ShiftDirectory.cs50
-rw-r--r--ShiftOS.Engine/ShiftFS/ShiftDrive.cs20
-rw-r--r--ShiftOS.Engine/ShiftFS/ShiftFS.cs34
-rw-r--r--ShiftOS.Engine/ShiftFS/ShiftFSObject.cs25
-rw-r--r--ShiftOS.Engine/ShiftFS/ShiftFile.cs48
5 files changed, 177 insertions, 0 deletions
diff --git a/ShiftOS.Engine/ShiftFS/ShiftDirectory.cs b/ShiftOS.Engine/ShiftFS/ShiftDirectory.cs
new file mode 100644
index 0000000..64ac4f2
--- /dev/null
+++ b/ShiftOS.Engine/ShiftFS/ShiftDirectory.cs
@@ -0,0 +1,50 @@
+using System.Collections.ObjectModel;
+using System.IO;
+using System.Linq;
+
+namespace ShiftOS.Engine.ShiftFS
+{
+ public class ShiftDirectory : ShiftFsObject
+ {
+ public ShiftDirectory(string path) : base(path)
+ {
+ 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) => { };
+ }
+
+ 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 ObservableCollection<ShiftFsObject> Children
+ {
+ get
+ {
+ var collection = new ObservableCollection<ShiftFsObject>();
+
+ foreach (var dir in new DirectoryInfo(Path.Combine(ShiftFs.SavePath, FullName)).EnumerateDirectories())
+ {
+ collection.Add(new ShiftDirectory(dir.FullName));
+ }
+
+ foreach (var file in new DirectoryInfo(Path.Combine(ShiftFs.SavePath, FullName)).EnumerateFiles())
+ {
+ collection.Add(new ShiftFile(file.FullName.Replace(ShiftFs.SavePath, "")));
+ }
+
+ return collection;
+ }
+ }
+
+ public ObservableCollection<ShiftFile> Files => new ObservableCollection<ShiftFile>(Children.OfType<ShiftFile>());
+
+ public ObservableCollection<ShiftDirectory> Directories
+ => new ObservableCollection<ShiftDirectory>(Children.OfType<ShiftDirectory>());
+ }
+} \ No newline at end of file
diff --git a/ShiftOS.Engine/ShiftFS/ShiftDrive.cs b/ShiftOS.Engine/ShiftFS/ShiftDrive.cs
new file mode 100644
index 0000000..cde9e8d
--- /dev/null
+++ b/ShiftOS.Engine/ShiftFS/ShiftDrive.cs
@@ -0,0 +1,20 @@
+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
new file mode 100644
index 0000000..d188bee
--- /dev/null
+++ b/ShiftOS.Engine/ShiftFS/ShiftFS.cs
@@ -0,0 +1,34 @@
+using System;
+using System.Collections.ObjectModel;
+using System.IO;
+
+namespace ShiftOS.Engine.ShiftFS
+{
+ public static class ShiftFs
+ {
+ internal static readonly string SavePath = Path.Combine(Environment.CurrentDirectory, "Save") + "\\";
+
+ public static ObservableCollection<ShiftDrive> Drives = new ObservableCollection<ShiftDrive>();
+
+ static ShiftFs()
+ {
+ if (Directory.Exists(SavePath))
+ {
+ var info = new DirectoryInfo(SavePath);
+ foreach (var dir in info.EnumerateDirectories())
+ {
+ Drives.Add(new ShiftDrive(dir));
+ }
+ }
+ else
+ {
+ CreateSaveFile();
+ }
+ }
+
+ public static void CreateSaveFile()
+ {
+ throw new NotImplementedException();
+ }
+ }
+} \ No newline at end of file
diff --git a/ShiftOS.Engine/ShiftFS/ShiftFSObject.cs b/ShiftOS.Engine/ShiftFS/ShiftFSObject.cs
new file mode 100644
index 0000000..69750fa
--- /dev/null
+++ b/ShiftOS.Engine/ShiftFS/ShiftFSObject.cs
@@ -0,0 +1,25 @@
+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
new file mode 100644
index 0000000..1a2413c
--- /dev/null
+++ b/ShiftOS.Engine/ShiftFS/ShiftFile.cs
@@ -0,0 +1,48 @@
+using System;
+using System.IO;
+using System.Runtime.Serialization.Formatters.Binary;
+
+namespace ShiftOS.Engine.ShiftFS
+{
+ public class ShiftFile : ShiftFsObject
+ {
+ public ShiftFile(string path) : base(path)
+ {
+ 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;
+ }
+
+ public new ShiftDirectory Parent => new ShiftDirectory(new FileInfo(FullDiskName).Directory.FullName);
+
+ public MemoryStream Bytes { get; set; }
+ public long Length => Bytes.Length;
+ }
+
+ public class ShiftFile<T> : ShiftFile // please C# gods let us constrain to attributes
+ {
+ public ShiftFile(string path) : base(path)
+ {
+ if (!typeof(T).IsSerializable)
+ {
+ throw new InvalidOperationException("Non-serializable types are not supported");
+ }
+ }
+
+ public T Object
+ {
+ get => (T) new BinaryFormatter().Deserialize(Bytes);
+ set => new BinaryFormatter().Serialize(Bytes, value);
+ }
+ }
+} \ No newline at end of file