From 959ea86d318ef8883d46e6af6a11a08e3e661136 Mon Sep 17 00:00:00 2001 From: That_One_Nerd <78499572+That-One-Nerd@users.noreply.github.com> Date: Thu, 6 May 2021 10:54:57 -0400 Subject: [PATCH] Add files via upload + Added Filesaving.cs + Added public abstract class File + Added public class BinaryFile + Added public class ByteFile + Added public class TextFile --- Scripts/Filesaving.cs | 188 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 188 insertions(+) create mode 100644 Scripts/Filesaving.cs diff --git a/Scripts/Filesaving.cs b/Scripts/Filesaving.cs new file mode 100644 index 0000000..0392759 --- /dev/null +++ b/Scripts/Filesaving.cs @@ -0,0 +1,188 @@ +using System; +using Nerd_STF.Lists; +using System.IO; +using System.Runtime.Serialization.Formatters.Binary; + +namespace Nerd_STF.Filesaving +{ + [Obsolete(nameof(BinaryFile) + " uses the " + nameof(BinaryFormatter) + ", which is considered dangerous. Go to 'https://aka.ms/binaryformatter/' for more information.")] + public class BinaryFile : File + { + public BinaryFile(string path) => Path = path; + public BinaryFile(string path, object data) + { + Data = data; + Path = path; + } + + public static BinaryFile Load(string path) + { + BinaryFile file = new(path); + FileStream stream = new(path, FileMode.Open); + BinaryFormatter formatter = new(); + file.Data = formatter.Deserialize(stream); + stream.Close(); + return file; + } + + public override void Erase() => Data = null; + public override void Load(bool erase = true) + { + if (erase) Erase(); + FileStream stream = new(Path, FileMode.Open); + BinaryFormatter formatter = new(); + Data = formatter.Deserialize(stream); + stream.Close(); + } + public override void Save() + { + FileStream stream = new(Path, FileMode.Create); + BinaryFormatter formatter = new(); + formatter.Serialize(stream, Data); + stream.Close(); + } + } + public class ByteFile : File> + { + public byte this[int index] + { + get + { + if (index < 0 || index >= Data.Length) throw new ArgumentOutOfRangeException(nameof(index)); + return Data[index]; + } + set { Data[index] = value; } + } + + public ByteFile(string path) => Path = path; + public ByteFile(string path, byte[] data) + { + Data = new List(data); + Path = path; + } + public ByteFile(string path, List data) + { + Data = data; + Path = path; + } + + public static ByteFile Load(string path) + { + ByteFile file = new(path); + FileStream stream = new(file.Path, FileMode.Open); + for (long i = 0; i < stream.Length; i++) file.Data.Add((byte)stream.ReadByte()); + stream.Close(); + return file; + } + + public override void Erase() => Data = new(); + public void Fill(int length, byte fill = 0) => Data = new List(length, fill); + public override void Load(bool erase = true) + { + if (erase) Erase(); + FileStream stream = new(Path, FileMode.Open); + for (long i = 0; i < stream.Length; i++) Data.Add((byte)stream.ReadByte()); + stream.Close(); + } + public void Remove(int start, int amount) + { + List old = Data; + Data = new List(old.Length - amount); + for (int i = 0; i < old.Length; i++) + { + if (i > start && i < start + amount) i = start + amount; + Data[i] = old[i]; + } + } + public override void Save() + { + FileStream stream = new(Path, FileMode.Create); + foreach (byte b in Data) stream.WriteByte(b); + stream.Close(); + } + public void Write(byte write, bool toFile = false) + { + Data += write; + if (toFile) + { + FileStream stream = new(Path, FileMode.Append); + stream.WriteByte(write); + stream.Close(); + } + } + public void Write(List write, bool toFile = false) + { + Data += write; + if (toFile) + { + FileStream stream = new(Path, FileMode.Append); + foreach (byte b in write) stream.WriteByte(b); + stream.Close(); + } + } + } + public class TextFile : File + { + public TextFile(string path) => Path = path; + public TextFile(string path, string data) + { + Data = data; + Path = path; + } + + public static TextFile Load(string path) + { + TextFile file = new(path); + FileStream stream = new(file.Path, FileMode.Open); + for (long i = 0; i < stream.Length; i++) file.Data += ((char)stream.ReadByte()); + stream.Close(); + return file; + } + + public override void Erase() => Data = ""; + public override void Load(bool erase = true) + { + if (erase) Erase(); + FileStream stream = new(Path, FileMode.Open); + for (long i = 0; i < stream.Length; i++) Data += (char)stream.ReadByte(); + stream.Close(); + } + public void Remove(int start, int amount) => Data = Data.Remove(start, amount); + public override void Save() + { + FileStream stream = new(Path, FileMode.Create); + foreach (byte b in Data) stream.WriteByte(b); + stream.Close(); + } + public void Write(char write, bool toFile = false) + { + Data += write; + if (toFile) + { + FileStream stream = new(Path, FileMode.Append); + stream.WriteByte((byte)write); + stream.Close(); + } + } + public void Write(string write, bool toFile = false) + { + Data += write; + if (toFile) + { + FileStream stream = new(Path, FileMode.Append); + foreach (byte b in write) stream.WriteByte(b); + stream.Close(); + } + } + } + + public abstract class File + { + public T Data { get; set; } + public string Path { get; set; } + + public abstract void Erase(); + public abstract void Load(bool erase = true); + public abstract void Save(); + } +} \ No newline at end of file