diff --git a/Nerd_STF.dll b/Nerd_STF.dll index 62c5532..c1bd85b 100644 Binary files a/Nerd_STF.dll and b/Nerd_STF.dll differ 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 diff --git a/Scripts/General.cs b/Scripts/General.cs index 1447d86..1b6a633 100644 --- a/Scripts/General.cs +++ b/Scripts/General.cs @@ -1,5 +1,6 @@ -using System; +using System; using System.Linq; +using System.Security.Cryptography; namespace Nerd_STF { @@ -36,40 +37,14 @@ namespace Nerd_STF } } - public static class Miscellaneous - { - public static int SyllableCount(string input) - { - // Starter code by KeithS on StackExchange. - - input = input.ToLower().Trim(); - bool lastWasVowel = false; - char[] vowels = new[] { 'a', 'e', 'i', 'o', 'u', 'y' }; - int count = 0; - - foreach (var c in input) - { - if (vowels.Contains(c)) - { - if (!lastWasVowel) count++; - lastWasVowel = true; - } - else lastWasVowel = false; - } - - if ((input.EndsWith("e") || input.EndsWith("es") || input.EndsWith("ed")) && !input.EndsWith("le")) count--; - - return count; - } - } - public static class Stats { - public static readonly string Creator = "That_One_Nerd"; + public static readonly string Creator = "That_One_Nerd"; public static readonly string[] Links = new[] - { "Discord: https://discord.gg/ySXMtWDTYY/", + { + "Discord: https://discord.gg/ySXMtWDTYY/", "Github: https://https://github.com/that-one-nerd", - "Itch: https://that-one-nerd.itch.io/" + "Itch: https://that-one-nerd.itch.io/" }; public static readonly string Version = "2021.0"; } @@ -99,4 +74,4 @@ namespace Nerd_STF return new Optional(input); } } -} \ No newline at end of file +} diff --git a/changelog.md b/changelog.md index db1883d..1e3dc60 100644 --- a/changelog.md +++ b/changelog.md @@ -20,3 +20,16 @@ - Miscellaneous - Optional - Stats + +# Version 2021.1: + Mainly centered around files and filesaving. + Added: + + Filesaving.cs + + File + + BinaryFile + + ByteFile + + TextFile + + Removed: + General.cs + - Miscellaneous