From 30bde5d2cd3938e5949b5d688196cc9a88b80d46 Mon Sep 17 00:00:00 2001 From: Michael Becker Date: Sun, 21 Jul 2024 22:32:37 -0400 Subject: [PATCH 1/5] add capital alphanumeric alphabets for convenience --- framework-dotnet/src/lib/MBS.Core/NanoId.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/framework-dotnet/src/lib/MBS.Core/NanoId.cs b/framework-dotnet/src/lib/MBS.Core/NanoId.cs index 6259def..5f58995 100755 --- a/framework-dotnet/src/lib/MBS.Core/NanoId.cs +++ b/framework-dotnet/src/lib/MBS.Core/NanoId.cs @@ -163,6 +163,8 @@ namespace MBS.Core public const string DefaultAlphabet = "_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; public const string DefaultAlphabetNoSpecialChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; + public const string CapitalAlphanumeric = "_-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + public const string CapitalAlphanumericNoSpecialChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; private static readonly CryptoRandom Random = new CryptoRandom(); /// From c275c017c14c66399ad03386f2e8f52faf4d6fd1 Mon Sep 17 00:00:00 2001 From: Michael Becker Date: Sun, 21 Jul 2024 22:35:30 -0400 Subject: [PATCH 2/5] add BeforeStartInternal event --- framework-dotnet/src/lib/MBS.Core/Application.cs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/framework-dotnet/src/lib/MBS.Core/Application.cs b/framework-dotnet/src/lib/MBS.Core/Application.cs index 024c65c..2a84844 100644 --- a/framework-dotnet/src/lib/MBS.Core/Application.cs +++ b/framework-dotnet/src/lib/MBS.Core/Application.cs @@ -1,4 +1,6 @@ -namespace MBS.Core; +using System.ComponentModel; + +namespace MBS.Core; public class Application { @@ -25,9 +27,21 @@ public class Application return e.ExitCode; } + public event EventHandler BeforeStartInternal; + protected virtual void OnBeforeStartInternal(CancelEventArgs e) + { + BeforeStartInternal?.Invoke(this, e); + } + public int Start() { Instance = this; + + CancelEventArgs e = new CancelEventArgs(); + OnBeforeStartInternal(e); + if (e.Cancel) + return 2; + int exitCode = StartInternal(); Instance = null; From 9df55b9c082a37da070fe351fbb91d0aa3a114d9 Mon Sep 17 00:00:00 2001 From: Michael Becker Date: Sun, 21 Jul 2024 22:36:57 -0400 Subject: [PATCH 3/5] add ReadToEnd function --- .../src/lib/MBS.Core/StreamExtensions.cs | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/framework-dotnet/src/lib/MBS.Core/StreamExtensions.cs b/framework-dotnet/src/lib/MBS.Core/StreamExtensions.cs index b1a5d14..3d0e9c9 100644 --- a/framework-dotnet/src/lib/MBS.Core/StreamExtensions.cs +++ b/framework-dotnet/src/lib/MBS.Core/StreamExtensions.cs @@ -96,4 +96,33 @@ public static class StreamExtensions } _streamPositions[st].Push(st.Position); } + + private const long BUFFER_SIZE=4096; + public static byte[] ReadToEnd(this Stream st) + { + byte[] buffer = new byte[BUFFER_SIZE]; + byte[] output = new byte[0]; + int i = 0; + bool done = false; + while (!done) + { + int length = st.Read(buffer, 0, buffer.Length); + if (length == 0) + { + done = true; + break; + } + + if (length < BUFFER_SIZE) + { + Array.Resize(ref buffer, length); + done = true; + } + + Array.Resize(ref output, output.Length + buffer.Length); + Array.Copy(buffer, 0, output, i, buffer.Length); + i += buffer.Length; + } + return output; + } } \ No newline at end of file From 623402805b12b11c6a32ef1db6848e7f5587b48c Mon Sep 17 00:00:00 2001 From: Michael Becker Date: Sun, 21 Jul 2024 22:37:57 -0400 Subject: [PATCH 4/5] fix AddRange to allow any type of Collection<>, not just List<> --- .../src/lib/MBS.Core/Collections/Generic/ExtensionMethods.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework-dotnet/src/lib/MBS.Core/Collections/Generic/ExtensionMethods.cs b/framework-dotnet/src/lib/MBS.Core/Collections/Generic/ExtensionMethods.cs index db03cbf..0b58403 100755 --- a/framework-dotnet/src/lib/MBS.Core/Collections/Generic/ExtensionMethods.cs +++ b/framework-dotnet/src/lib/MBS.Core/Collections/Generic/ExtensionMethods.cs @@ -55,7 +55,7 @@ namespace MBS.Core.Collections.Generic return ((List)(cacheOfT[obj][typeof(T)])).ToArray(); } - public static void AddRange(this IList list, IEnumerable items) + public static void AddRange(this ICollection list, IEnumerable items) { foreach (T item in items) { From 6ee1135732e56641877d3f5b21b5870553301051 Mon Sep 17 00:00:00 2001 From: Michael Becker Date: Sun, 21 Jul 2024 22:38:38 -0400 Subject: [PATCH 5/5] add TypeExtensions to support Type.IsSubclassOfGeneric(...) --- .../lib/MBS.Core/Reflection/TypeExtensions.cs | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 framework-dotnet/src/lib/MBS.Core/Reflection/TypeExtensions.cs diff --git a/framework-dotnet/src/lib/MBS.Core/Reflection/TypeExtensions.cs b/framework-dotnet/src/lib/MBS.Core/Reflection/TypeExtensions.cs new file mode 100644 index 0000000..b7930ed --- /dev/null +++ b/framework-dotnet/src/lib/MBS.Core/Reflection/TypeExtensions.cs @@ -0,0 +1,32 @@ +namespace MBS.Core.Reflection; + +public static class TypeExtensions +{ + public static bool IsSubclassOfGeneric(this Type toCheck, Type generic) + { + // thanks https://stackoverflow.com/a/457708 + + while (toCheck != null && toCheck != typeof(object)) + { + var cur = toCheck.IsGenericType ? toCheck.GetGenericTypeDefinition() : toCheck; + if (generic == cur) + { + return true; + } + + Type[] intfs = toCheck.GetInterfaces(); + foreach (Type intf in intfs) + { + // !!! HACK HACK HACK !!! + bool hack = intf.Namespace.Equals(generic.Namespace) && intf.Name.Equals(generic.Name); + if (hack) + { + return true; + } + } + toCheck = toCheck.BaseType; + } + return false; + } + +} \ No newline at end of file