new version

This commit is contained in:
That_One_Nerd 2021-06-17 13:59:27 -04:00
parent a6b6c612a7
commit 9b051edb11
7 changed files with 2008 additions and 2463 deletions

Binary file not shown.

View File

@ -1,188 +1,261 @@
using System; using System;
using Nerd_STF.Lists; using System.Text;
using System.IO; using Nerd_STF.Lists;
using System.Runtime.Serialization.Formatters.Binary; 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> [Obsolete(nameof(BinaryFile) + " uses the " + nameof(BinaryFormatter) + ", which is considered dangerous. Go to 'https://aka.ms/binaryformatter/' for more information.")]
{ [Serializable]
public BinaryFile(string path) => Path = path; public class BinaryFile
public BinaryFile(string path, object data) {
{ public object Data { get; set; }
Data = data; public string Path { get; set; }
Path = path;
} public BinaryFile(string path) => Path = path;
public BinaryFile(string path, object data)
public static BinaryFile Load(string path) {
{ Data = data;
BinaryFile file = new(path); Path = path;
FileStream stream = new(path, FileMode.Open); }
BinaryFormatter formatter = new();
file.Data = formatter.Deserialize(stream); public static BinaryFile Load(string path)
stream.Close(); {
return file; BinaryFile file = new(path);
} FileStream stream = new(path, FileMode.Open);
BinaryFormatter formatter = new();
public override void Erase() => Data = null; file.Data = formatter.Deserialize(stream);
public override void Load(bool erase = true) stream.Close();
{ return file;
if (erase) Erase(); }
FileStream stream = new(Path, FileMode.Open);
BinaryFormatter formatter = new(); public void Erase() => Data = null;
Data = formatter.Deserialize(stream); public void Load()
stream.Close(); {
} FileStream stream = new(Path, FileMode.Open);
public override void Save() BinaryFormatter formatter = new();
{ Data = formatter.Deserialize(stream);
FileStream stream = new(Path, FileMode.Create); stream.Close();
BinaryFormatter formatter = new(); }
formatter.Serialize(stream, Data); public void Save()
stream.Close(); {
} FileStream stream = new(Path, FileMode.Create);
} BinaryFormatter formatter = new();
public class ByteFile : File<List<byte>> formatter.Serialize(stream, Data);
{ stream.Close();
public byte this[int index] }
{ }
get [Obsolete(nameof(BinaryFile) + " uses the " + nameof(BinaryFormatter) + ", which is considered dangerous. Go to 'https://aka.ms/binaryformatter/' for more information.")]
{ [Serializable]
if (index < 0 || index >= Data.Length) throw new ArgumentOutOfRangeException(nameof(index)); public class BinaryFile<T>
return Data[index]; {
} public T Data { get; set; }
set { Data[index] = value; } public string Path { get; set; }
}
public BinaryFile(string path) => Path = path;
public ByteFile(string path) => Path = path; public BinaryFile(string path, T data)
public ByteFile(string path, byte[] data) {
{ Data = data;
Data = new List<byte>(data); Path = path;
Path = path; }
}
public ByteFile(string path, List<byte> data) public static BinaryFile<T> Load(string path)
{ {
Data = data; BinaryFile<T> file = new(path);
Path = path; FileStream stream = new(path, FileMode.Open);
} BinaryFormatter formatter = new();
file.Data = (T)formatter.Deserialize(stream);
public static ByteFile Load(string path) stream.Close();
{ return file;
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()); public void Erase() => Data = default;
stream.Close(); public void Load()
return file; {
} FileStream stream = new(Path, FileMode.Open);
BinaryFormatter formatter = new();
public override void Erase() => Data = new(); Data = (T)formatter.Deserialize(stream);
public void Fill(int length, byte fill = 0) => Data = new List<byte>(length, fill); stream.Close();
public override void Load(bool erase = true) }
{ public void Save()
if (erase) Erase(); {
FileStream stream = new(Path, FileMode.Open); FileStream stream = new(Path, FileMode.Create);
for (long i = 0; i < stream.Length; i++) Data.Add((byte)stream.ReadByte()); BinaryFormatter formatter = new();
stream.Close(); formatter.Serialize(stream, Data);
} stream.Close();
public void Remove(int start, int amount) }
{ }
List<byte> old = Data; [Serializable]
Data = new List<byte>(old.Length - amount); public class ByteFile : File<List<byte>>
for (int i = 0; i < old.Length; i++) {
{ public byte this[int index]
if (i > start && i < start + amount) i = start + amount; {
Data[i] = old[i]; get
} {
} if (index < 0 || index >= Data.Length) throw new ArgumentOutOfRangeException(nameof(index));
public override void Save() return Data[index];
{ }
FileStream stream = new(Path, FileMode.Create); set { Data[index] = value; }
foreach (byte b in Data) stream.WriteByte(b); }
stream.Close();
} public ByteFile(string path) => Path = path;
public void Write(byte write, bool toFile = false) public ByteFile(string path, params byte[] data)
{ {
Data += write; Data = new List<byte>(data);
if (toFile) Path = path;
{ }
FileStream stream = new(Path, FileMode.Append); public ByteFile(string path, List<byte> data)
stream.WriteByte(write); {
stream.Close(); Data = data;
} Path = path;
} }
public void Write(List<byte> write, bool toFile = false)
{ public static ByteFile Load(string path)
Data += write; {
if (toFile) ByteFile file = new(path);
{ FileStream stream = new(file.Path, FileMode.Open);
FileStream stream = new(Path, FileMode.Append); byte[] b = new byte[stream.Length];
foreach (byte b in write) stream.WriteByte(b); while (stream.Read(b, 0, b.Length) > 0) ;
stream.Close(); file.Data = new(b);
} stream.Close();
} return file;
} }
public class TextFile : File<string>
{ public override void Erase() => Data = new();
public TextFile(string path) => Path = path; public void Fill(int length, byte fill = 0) => Data = new List<byte>(length, fill);
public TextFile(string path, string data) public override void Load(bool erase = true)
{ {
Data = data; if (erase) Erase();
Path = path; FileStream stream = new(Path, FileMode.Open);
} byte[] b = new byte[stream.Length];
while (stream.Read(b, 0, b.Length) > 0) ;
public static TextFile Load(string path) Data.AddRange(b);
{ stream.Close();
TextFile file = new(path); }
FileStream stream = new(file.Path, FileMode.Open); public void Remove(int start, int amount)
for (long i = 0; i < stream.Length; i++) file.Data += ((char)stream.ReadByte()); {
stream.Close(); List<byte> old = Data;
return file; Data = new List<byte>(old.Length - amount);
} for (int i = 0; i < old.Length; i++)
{
public override void Erase() => Data = ""; if (i > start && i < start + amount) i = start + amount;
public override void Load(bool erase = true) Data[i] = old[i];
{ }
if (erase) Erase(); }
FileStream stream = new(Path, FileMode.Open); public override void Save()
for (long i = 0; i < stream.Length; i++) Data += (char)stream.ReadByte(); {
stream.Close(); FileStream stream = new(Path, FileMode.Create);
} stream.Write(Data.ToArray(), 0, Data.Length);
public void Remove(int start, int amount) => Data = Data.Remove(start, amount); stream.Close();
public override void Save() }
{ public override bool TryLoad(out File<List<byte>> file)
FileStream stream = new(Path, FileMode.Create); {
foreach (byte b in Data) stream.WriteByte(b); bool success = false;
stream.Close(); try
} {
public void Write(char write, bool toFile = false) file = new ByteFile(Path);
{ FileStream stream = new(file.Path, FileMode.Open);
Data += write; byte[] b = new byte[stream.Length];
if (toFile) while (stream.Read(b, 0, b.Length) > 0) ;
{ file.Data.AddRange(b);
FileStream stream = new(Path, FileMode.Append); stream.Close();
stream.WriteByte((byte)write); success = true;
stream.Close(); }
} catch { file = null; }
}
public void Write(string write, bool toFile = false) return success;
{ }
Data += write; public void Write(byte write, bool toFile = false)
if (toFile) {
{ Data += write;
FileStream stream = new(Path, FileMode.Append); if (toFile) Save();
foreach (byte b in write) stream.WriteByte(b); }
stream.Close(); public override void Write(List<byte> write, bool toFile = false)
} {
} Data += write;
} if (toFile) Save();
}
public abstract class File<T> }
{ [Serializable]
public T Data { get; set; } public class TextFile : File<string>
public string Path { get; set; } {
public TextFile(string path) => Path = path;
public abstract void Erase(); public TextFile(string path, string data)
public abstract void Load(bool erase = true); {
public abstract void Save(); Data = data;
} Path = path;
}
public static TextFile Load(string path)
{
TextFile file = new(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();
return file;
}
public override void Erase() => Data = "";
public override void Load(bool erase = true)
{
if (erase) Erase();
FileStream stream = new(Path, FileMode.Open);
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);
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) Save();
}
public override void Write(string write, bool toFile = false)
{
Data += write;
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;
using System.Linq; using System.Text;
using System.Security.Cryptography;
namespace Nerd_STF namespace Nerd_STF
{ {
public static class Hashes public static class Hashes
{ {
public static int Default(object obj) public static int Default(object obj) => obj.GetHashCode();
{ public static byte[] MD5(byte[] input) => System.Security.Cryptography.MD5.Create().ComputeHash(input);
return obj.GetHashCode();
}
public static string MD5(string 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); byte[] hash = md5.ComputeHash(inputB);
var builder = new System.Text.StringBuilder(); string s = "";
for (int i = 0; i < hash.Length; i++) for (int i = 0; i < hash.Length; i++) s += hash[i].ToString("X2");
{ return s;
builder.Append(hash[i].ToString("X2"));
}
return builder.ToString();
} }
public static uint SchechterTurbulence(uint seed) public static uint SchechterTurbulence(uint seed)
{ {
@ -35,6 +29,68 @@ namespace Nerd_STF
return seed; 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 public static class Stats
@ -46,32 +102,18 @@ namespace Nerd_STF
"Github: https://https://github.com/that-one-nerd", "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"; public static readonly string Version = "2021.2";
} }
[Serializable]
public struct Optional<T> public struct Optional<T>
{ {
public bool Exists public bool Exists => Value != null;
{
get
{
return Value != null;
}
}
public T Value { get; internal set; } public T Value { get; internal set; }
public Optional(T input) public Optional(T input) => Value = input;
{
Value = input;
}
public static explicit operator T(Optional<T> input) public static explicit operator T(Optional<T> input) => input.Value;
{ public static explicit operator Optional<T>(T input) => new(input);
return input.Value;
}
public static explicit operator Optional<T>(T input)
{
return new Optional<T>(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,11 +23,143 @@
* public static class Stats * public static class Stats
# Version 2021.1: # Version 2021.1:
Mainly centered around files and filesaving. This update is mainly centered around files and filesaving.
* Nerd_STF * Nerd_STF
+ Nerd_STF.Filesaving + Nerd_STF.Filesaving
+ File<T> + File<T>
+ BinaryFile + BinaryFile
+ ByteFile + ByteFile
+ TextFile + TextFile
- public class Miscellaneous - 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)