aboutsummaryrefslogtreecommitdiff
path: root/ShiftOS.Engine/ShiftFS/ShiftFile.cs
blob: 1a2413c7473c756ef2da2a33b08a7756ef42674b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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);
		}
	}
}