Merge pull request #3 from That-One-Nerd/v2021.2

updating main branch to contain new version
This commit is contained in:
That_One_Nerd 2021-06-17 14:00:40 -04:00 committed by GitHub
commit dd4d0befd3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 2008 additions and 2463 deletions

Binary file not shown.

View File

@ -1,13 +1,18 @@
using System;
using System.Text;
using Nerd_STF.Lists;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace Nerd_STF.Filesaving
namespace Nerd_STF.File.Saving
{
[Obsolete(nameof(BinaryFile) + " uses the " + nameof(BinaryFormatter) + ", which is considered dangerous. Go to 'https://aka.ms/binaryformatter/' for more information.")]
public class BinaryFile : File<object>
[Serializable]
public class BinaryFile
{
public object Data { get; set; }
public string Path { get; set; }
public BinaryFile(string path) => Path = path;
public BinaryFile(string path, object data)
{
@ -25,16 +30,15 @@ namespace Nerd_STF.Filesaving
return file;
}
public override void Erase() => Data = null;
public override void Load(bool erase = true)
public void Erase() => Data = null;
public void Load()
{
if (erase) Erase();
FileStream stream = new(Path, FileMode.Open);
BinaryFormatter formatter = new();
Data = formatter.Deserialize(stream);
stream.Close();
}
public override void Save()
public void Save()
{
FileStream stream = new(Path, FileMode.Create);
BinaryFormatter formatter = new();
@ -42,6 +46,47 @@ namespace Nerd_STF.Filesaving
stream.Close();
}
}
[Obsolete(nameof(BinaryFile) + " uses the " + nameof(BinaryFormatter) + ", which is considered dangerous. Go to 'https://aka.ms/binaryformatter/' for more information.")]
[Serializable]
public class BinaryFile<T>
{
public T Data { get; set; }
public string Path { get; set; }
public BinaryFile(string path) => Path = path;
public BinaryFile(string path, T data)
{
Data = data;
Path = path;
}
public static BinaryFile<T> Load(string path)
{
BinaryFile<T> file = new(path);
FileStream stream = new(path, FileMode.Open);
BinaryFormatter formatter = new();
file.Data = (T)formatter.Deserialize(stream);
stream.Close();
return file;
}
public void Erase() => Data = default;
public void Load()
{
FileStream stream = new(Path, FileMode.Open);
BinaryFormatter formatter = new();
Data = (T)formatter.Deserialize(stream);
stream.Close();
}
public void Save()
{
FileStream stream = new(Path, FileMode.Create);
BinaryFormatter formatter = new();
formatter.Serialize(stream, Data);
stream.Close();
}
}
[Serializable]
public class ByteFile : File<List<byte>>
{
public byte this[int index]
@ -55,7 +100,7 @@ namespace Nerd_STF.Filesaving
}
public ByteFile(string path) => Path = path;
public ByteFile(string path, byte[] data)
public ByteFile(string path, params byte[] data)
{
Data = new List<byte>(data);
Path = path;
@ -70,7 +115,9 @@ namespace Nerd_STF.Filesaving
{
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());
byte[] b = new byte[stream.Length];
while (stream.Read(b, 0, b.Length) > 0) ;
file.Data = new(b);
stream.Close();
return file;
}
@ -81,7 +128,9 @@ namespace Nerd_STF.Filesaving
{
if (erase) Erase();
FileStream stream = new(Path, FileMode.Open);
for (long i = 0; i < stream.Length; i++) Data.Add((byte)stream.ReadByte());
byte[] b = new byte[stream.Length];
while (stream.Read(b, 0, b.Length) > 0) ;
Data.AddRange(b);
stream.Close();
}
public void Remove(int start, int amount)
@ -97,30 +146,38 @@ namespace Nerd_STF.Filesaving
public override void Save()
{
FileStream stream = new(Path, FileMode.Create);
foreach (byte b in Data) stream.WriteByte(b);
stream.Write(Data.ToArray(), 0, Data.Length);
stream.Close();
}
public override bool TryLoad(out File<List<byte>> file)
{
bool success = false;
try
{
file = new ByteFile(Path);
FileStream stream = new(file.Path, FileMode.Open);
byte[] b = new byte[stream.Length];
while (stream.Read(b, 0, b.Length) > 0) ;
file.Data.AddRange(b);
stream.Close();
success = true;
}
catch { file = null; }
return success;
}
public void Write(byte write, bool toFile = false)
{
Data += write;
if (toFile)
{
FileStream stream = new(Path, FileMode.Append);
stream.WriteByte(write);
stream.Close();
}
if (toFile) Save();
}
public void Write(List<byte> write, bool toFile = false)
public override void Write(List<byte> write, bool toFile = false)
{
Data += write;
if (toFile)
{
FileStream stream = new(Path, FileMode.Append);
foreach (byte b in write) stream.WriteByte(b);
stream.Close();
}
if (toFile) Save();
}
}
[Serializable]
public class TextFile : File<string>
{
public TextFile(string path) => Path = path;
@ -134,7 +191,9 @@ namespace Nerd_STF.Filesaving
{
TextFile file = new(path);
FileStream stream = new(file.Path, FileMode.Open);
for (long i = 0; i < stream.Length; i++) file.Data += ((char)stream.ReadByte());
byte[] b = new byte[stream.Length];
while (stream.Read(b, 0, b.Length) > 0) ;
file.Data += Encoding.Default.GetString(b);
stream.Close();
return file;
}
@ -144,45 +203,59 @@ namespace Nerd_STF.Filesaving
{
if (erase) Erase();
FileStream stream = new(Path, FileMode.Open);
for (long i = 0; i < stream.Length; i++) Data += (char)stream.ReadByte();
byte[] b = new byte[stream.Length];
while (stream.Read(b, 0, b.Length) > 0) ;
Data += Encoding.Default.GetString(b);
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);
byte[] b = Encoding.Default.GetBytes(Data);
stream.Write(b, 0, b.Length);
stream.Close();
}
public override bool TryLoad(out File<string> file)
{
bool success = false;
try
{
file = new TextFile(Path);
FileStream stream = new(file.Path, FileMode.Open);
byte[] b = new byte[stream.Length];
while (stream.Read(b, 0, b.Length) > 0) ;
file.Data += Encoding.Default.GetString(b);
stream.Close();
success = true;
}
catch { file = null; }
return success;
}
public void Write(char write, bool toFile = false)
{
Data += write;
if (toFile)
{
FileStream stream = new(Path, FileMode.Append);
stream.WriteByte((byte)write);
stream.Close();
}
if (toFile) Save();
}
public void Write(string write, bool toFile = false)
public override 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();
}
if (toFile) Save();
}
}
[Serializable]
public abstract class File<T>
{
public T Data { get; set; }
public bool Exists => System.IO.File.Exists(Path);
public string Path { get; set; }
public abstract void Erase();
public abstract void Load(bool erase = true);
public abstract void Save();
public abstract bool TryLoad(out File<T> file);
public abstract void Write(T write, bool toFile = false);
}
}

View File

@ -1,28 +1,22 @@
using System;
using System.Linq;
using System.Security.Cryptography;
using System;
using System.Text;
namespace Nerd_STF
{
public static class Hashes
{
public static int Default(object obj)
{
return obj.GetHashCode();
}
public static int Default(object obj) => obj.GetHashCode();
public static byte[] MD5(byte[] input) => System.Security.Cryptography.MD5.Create().ComputeHash(input);
public static string MD5(string input)
{
var md5 = System.Security.Cryptography.MD5.Create();
System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
byte[] inputB = System.Text.Encoding.ASCII.GetBytes(input);
byte[] inputB = Encoding.ASCII.GetBytes(input);
byte[] hash = md5.ComputeHash(inputB);
var builder = new System.Text.StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
builder.Append(hash[i].ToString("X2"));
}
return builder.ToString();
string s = "";
for (int i = 0; i < hash.Length; i++) s += hash[i].ToString("X2");
return s;
}
public static uint SchechterTurbulence(uint seed)
{
@ -35,6 +29,68 @@ namespace Nerd_STF
return seed;
}
public static byte[] SHA1(byte[] input) => System.Security.Cryptography.SHA1.Create().ComputeHash(input);
public static string SHA1(string input)
{
System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1.Create();
byte[] inputB = Encoding.ASCII.GetBytes(input);
byte[] hash = sha1.ComputeHash(inputB);
string s = "";
for (int i = 0; i < hash.Length; i++) s += hash[i].ToString("X2");
return s;
}
public static byte[] SHA256(byte[] input) => System.Security.Cryptography.SHA256.Create().ComputeHash(input);
public static string SHA256(string input)
{
System.Security.Cryptography.SHA256 sha256 = System.Security.Cryptography.SHA256.Create();
byte[] inputB = Encoding.ASCII.GetBytes(input);
byte[] hash = sha256.ComputeHash(inputB);
string s = "";
for (int i = 0; i < hash.Length; i++) s += hash[i].ToString("X2");
return s;
}
public static byte[] SHA384(byte[] input) => System.Security.Cryptography.SHA384.Create().ComputeHash(input);
public static string SHA384(string input)
{
System.Security.Cryptography.SHA384 sha384 = System.Security.Cryptography.SHA384.Create();
byte[] inputB = Encoding.ASCII.GetBytes(input);
byte[] hash = sha384.ComputeHash(inputB);
string s = "";
for (int i = 0; i < hash.Length; i++) s += hash[i].ToString("X2");
return s;
}
public static byte[] SHA512(byte[] input) => System.Security.Cryptography.SHA512.Create().ComputeHash(input);
public static string SHA512(string input)
{
System.Security.Cryptography.SHA512 sha512 = System.Security.Cryptography.SHA512.Create();
byte[] inputB = Encoding.ASCII.GetBytes(input);
byte[] hash = sha512.ComputeHash(inputB);
string s = "";
for (int i = 0; i < hash.Length; i++) s += hash[i].ToString("X2");
return s;
}
}
public static class Misc
{
public static string PlaceMaker(int num)
{
return num.ToString()[^1] switch
{
'1' => num + "st",
'2' => num + "nd",
'3' => num + "rd",
_ => num + "th",
};
}
}
public static class Stats
@ -46,32 +102,18 @@ namespace Nerd_STF
"Github: https://https://github.com/that-one-nerd",
"Itch: https://that-one-nerd.itch.io/"
};
public static readonly string Version = "2021.0";
public static readonly string Version = "2021.2";
}
[Serializable]
public struct Optional<T>
{
public bool Exists
{
get
{
return Value != null;
}
}
public bool Exists => Value != null;
public T Value { get; internal set; }
public Optional(T input)
{
Value = input;
}
public Optional(T input) => Value = input;
public static explicit operator T(Optional<T> input)
{
return input.Value;
}
public static explicit operator Optional<T>(T input)
{
return new Optional<T>(input);
}
public static explicit operator T(Optional<T> input) => input.Value;
public static explicit operator Optional<T>(T input) => new(input);
}
}

View File

@ -1,16 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Nerd_STF.Interfaces
{
public interface INegatives<T>
{
public T Absolute { get; }
public bool IsNegative { get; }
public T Negative { get; }
public T Positive { get; }
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -23,7 +23,7 @@
* public static class Stats
# Version 2021.1:
Mainly centered around files and filesaving.
This update is mainly centered around files and filesaving.
* Nerd_STF
+ Nerd_STF.Filesaving
+ File<T>
@ -31,3 +31,135 @@
+ ByteFile
+ TextFile
- public class Miscellaneous
# Version 2021.2:
This update is centered around lists.
* Nerd_STF:
* public static class Hashes:
+ public static string SHA1(string)
+ public static string SHA256(string)
+ public static string SHA384(string)
+ public static string SHA512(string)
+ public static byte[] MD5(byte[])
+ public static byte[] SHA1(byte[])
+ public static byte[] SHA256(byte[])
+ public static byte[] SHA384(byte[])
+ public static byte[] SHA512(byte[])
= Made `public static string MD5(string)` include more of my own scripting
* Nerd_STF.File:
= Nerd_STF.Filesaving: Moved to Nerd_STF.File.Saving
* Nerd_STF.File.Saving:
+ public class BinaryFile<T>
* public class ByteFile:
+ public override bool TryLoad(out File<List<byte>>)
= Made `public static ByteFile Load(string)` load files faster
= Made `public override void Load(bool)` load files faster
= Made `public override void Save()` save files faster
= Made `public override void Write(byte, bool)` save files faster
= Made `public override void Write(List<byte>, bool)` save files faster
* public abstract class File<T>:
+ public bool Exists;
+ public abstract bool TryLoad(out File<T>)
* public class TextFile:
+ public override bool TryLoad(out File<string>)
= Made `public static TextFile Load(string)` load files faster
= Made `public override void Load(bool)` load files faster
= Made `public override Save()` save files faster
= Made `public override void Write(char, bool)` save files faster
= Made `public override void Write(string, bool)` save files faster
- Nerd_STF.Interfaces:
= Moved `public interface INegatives<T>` to `Nerd_STF.Mathematics.Interfaces`
* Nerd_STF.Lists:
+ public class ReadOnlyList<T>
= public class List<T>: Completely reworked everything in `List<T>`
+ public int this[T] { get; set; }
+ public List(IEnumerable)
+ public bool IsEmpty { get; }
+ public bool IsNull { get; }
+ public bool IsNullOrEmpty { get; }
+ public bool Contains(Predicate<T>)
+ public int Count (Predicate<T>)
+ public void AddRange(IEnumerable)
+ public bool Any(Predicate<T>)
+ public bool Any(T)
+ public T Find(Predicate<T>)
+ public T Find(Predicate<T>, int)
+ public T Find(Predicate<T>, int, int)
+ public T FindOrDefault(Predicate<T>)
+ public T FindOrDefault(T)
+ public T FindOrDefault(Predicate<T>, int)
+ public T FindOrDefault(T, int)
+ public T FindOrDefault(Predicate<T>, int, int)
+ public T FindOrDefault(T, int, int)
+ public List<T> FindAll(Predicate<T>)
+ public List<T> FindAll(Predicate<T>, int)
+ public List<T> FindAll(Predicate<T>, int, int)
+ public List<T> FindLast(Predicate<T>)
+ public List<T> FindLast(Predicate<T>, int)
+ public List<T> FindLast(Predicate<T>, int, int)
+ public T FindLastOrDefault(Predicate<T>)
+ public T FindLastOrDefault(T)
+ public T FindLastOrDefault(Predicate<T>, int)
+ public T FindLastOrDefault(T, int)
+ public T FindLastOrDefault(Predicate<T>, int, int)
+ public T FindLastOrDefault(T, int, int)
+ public int FindIndex(Predicate<T>)
+ public int FindIndex(Predicate<T>, int)
+ public int FindIndex(Predicate<T>, int, int)
+ public List<int> FindAllIndex(Predicate<T>)
+ public List<int> FindAllIndex(Predicate<T>, int)
+ public List<int> FindAllIndex(Predicate<T>, int, int)
+ public int FindLastIndex(Predicate<T>)
+ public int FindLastIndex(Predicate<T>, int)
+ public int FindLastIndex(Predicate<T>, int, int)
+ public bool MatchesAll(Predicate<T>)
+ public void Remove(Predicate<T>)
+ public void RemoveAll(Predicate<T>)
+ public void RemoveAll(T)
+ public void RemoveLast(Predicate<T>)
+ public void RemoveLast(T)
+ IEnumerator<T> IEnumerable<T>.GetEnumerator()
+ public void Randomize()
+ public List<int> FindAllIndex()
+ public void Shuffle()
+ public List<int> FindAllIndex(int)
+ public List<int> FindAllIndex(int, int)
+ public ReadOnlyList<T> ToReadOnly()
= Made `public List<T> Duplicate()` a readonly variable and better (`public List<T> Duplicate { get; }`)
= Renamed the internal array to `array,` as opposed to `list`
= Renamed `public void Add(T[])` to `public void AddRange(T[])`
= Renamed `public void Add(List<T>)` to `public void AddRange(List<T>)`
= Renamed `public bool Compare(T)` to `public bool Contains(T)`
= Renamed `public void Remove(int, bool)` to `public void Remove(int)`
= Renamed `public void SetAll(T)` to `public void Fill(T)`
= Made `public string ToString(bool)` count up from zero instead of one when the bool is set to true.
= Renamed `public ReadOnlyCollection<T> AsReadOnly()` to `public ReadOnlyCollection<T> ToSystemReadOnly()`
- public bool Check(int)
- public void Convert(T)
- public void Convert(T[])
- public void Convert(List<T>)
- public T Get(int)
- public void Get(int, out T)
- public T[] GetAll()
- public void Set(int, T)
- public void Set(T[])
- public void Set(List<T>)
- public static List<T> AllDefault(int)
- public static List<T> operator +(T[], List<T>)
- public static List<T> operator +(List<T>, int)
- public static List<T> operator +(T, List<T>)
- public static List<T> operator *(List<T>, int)
- public static explicit operator T[](List<T>)
- public static explicit operator List<T>(T[])
= Marked `public class Nerd_STF.Lists.Matrix<T>` as deprecated. This class will be removed or heavily modified in a future release. Also removed all instances of removed List<T> methods and replaced them with work-arounds.
* Nerd_STF.Mathematics:
= Marked `public struct Angle` as serializable
= Marked `public struct Color` as serializable
= Marked `public struct ColorByte` as serializable
= Marked `public struct Percent` as serializable
= Marked `public struct Vector` as serializable
= Marked `public struct Vector2` as serializable
= Marked `public struct Vector3` as serializable
= Marked `public struct Vector4` as serializable
+ public static class Misc
+ public static string PlaceMaker(int)