From 8996d73a9fe9ea1ed3e4c1d0632a92a5332523fa Mon Sep 17 00:00:00 2001 From: ninemine <106434473+NINEMINEsigma@users.noreply.github.com> Date: Wed, 25 Jun 2025 14:36:19 +0800 Subject: [PATCH] =?UTF-8?q?BS=200.0.1=20=E6=96=B0=E6=9E=84=E8=B1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Convention-CSharp.csproj | 1 + Convention/Runtime/Architecture.cs | 266 +++++++++++++++++++++++++++++ Convention/Runtime/Config.cs | 29 ---- 3 files changed, 267 insertions(+), 29 deletions(-) create mode 100644 Convention/Runtime/Architecture.cs diff --git a/Convention-CSharp.csproj b/Convention-CSharp.csproj index 14d63da..cdc5c57 100644 --- a/Convention-CSharp.csproj +++ b/Convention-CSharp.csproj @@ -1,6 +1,7 @@  + 9.0 Exe net8.0 Convention diff --git a/Convention/Runtime/Architecture.cs b/Convention/Runtime/Architecture.cs new file mode 100644 index 0000000..5daa48f --- /dev/null +++ b/Convention/Runtime/Architecture.cs @@ -0,0 +1,266 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; +using static System.Runtime.InteropServices.JavaScript.JSType; + +namespace Convention +{ + // Interface + + public interface IModel + { + string Save(); + void Load(string data); + } + + public interface IConvertModel + : IModel + { + T ConvertTo(); + } + + // Instance + + public class SingletonModel: IModel + { + private static T? InjectInstance = default; + + public static T? Instance + { + get => InjectInstance; + set + { + if (value == null && InjectInstance == null) + return; + if (InjectInstance == null || InjectInstance.Equals(value) == false) + { + InjectInstance = value; + } + } + } + + public T? Data => InjectInstance; + + void IModel.Load(string data) + { + if(typeof(T).GetInterfaces().Contains(typeof(IModel))) + { + typeof(T).GetMethod(nameof(IModel.Load))!.Invoke(Instance, new object[] { data }); + } + throw new InvalidOperationException(); + } + + string IModel.Save() + { + if (typeof(T).GetInterfaces().Contains(typeof(IModel))) + { + return (string)typeof(T).GetMethod(nameof(IModel.Save))!.Invoke(Instance, Array.Empty())!; + } + throw new InvalidOperationException(); + } + + public static implicit operator T?(SingletonModel _) => InjectInstance; + } + + public class DependenceModel + : IConvertModel, IEnumerable> + { + private readonly IConvertModel[] queries; + + public DependenceModel(params IConvertModel[] queries) + { + this.queries = queries; + } + public DependenceModel(IEnumerable> queries) + { + this.queries = queries.ToArray(); + } + + public bool ConvertTo() + { + foreach (var query in queries) + { + if (query.ConvertTo() == false) + return false; + } + return true; + } + + public IEnumerator> GetEnumerator() + { + return ((IEnumerable>)this.queries).GetEnumerator(); + } + + public virtual void Load(string data) + { + throw new NotImplementedException(); + } + + public virtual string Save() + { + throw new NotImplementedException(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return this.queries.GetEnumerator(); + } + } + + public static class Architecture + { + public static string FormatType(Type type) + { + return type.Assembly + "::" + type.FullName; + } + + public static Type? LoadFromFormat(string data) + { + var keys = data.Split("::"); + Assembly? asm = null; + try + { + asm = Assembly.LoadFrom(keys[0]); + return asm.GetType(keys[1]); + } + catch + { + return null; + } + } + + public static Type? LoadFromFormat(string data, out Exception? exception) + { + exception = null; + var keys = data.Split("::"); + Assembly? asm = null; + try + { + asm = Assembly.LoadFrom(keys[0]); + return asm.GetType(keys[1]); + } + catch (Exception ex) + { + exception = ex; + return null; + } + } + + private class TypeQuery + : IConvertModel + { + private Type queryType; + + public TypeQuery(Type queryType) + { + this.queryType = queryType; + } + + public bool ConvertTo() + { + return Architecture.Childs.ContainsKey(queryType); + } + + public void Load(string data) + { + throw new NotImplementedException(); + } + + public string Save() + { + throw new NotImplementedException(); + } + } + + private static HashSet RegisterHistory = new(); + private static Dictionary UncompleteTargets = new(); + private static Dictionary Completer = new(); + private static Dictionary Dependences = new(); + private static Dictionary Childs = new(); + + public class Registering : IConvertModel + { + private readonly Type registerSlot; + + public Registering(Type registerSlot) + { + this.registerSlot = registerSlot; + } + + public bool ConvertTo() + { + return Architecture.Childs.ContainsKey(registerSlot); + } + + public void Load(string data) + { + throw new InvalidOperationException($"Cannt use {nameof(Registering)} to load type"); + } + + public string Save() + { + return $"{FormatType(registerSlot)}[{ConvertTo()}]"; + } + } + + private static bool InternalComplete(out HashSet InternalUpdateBuffer) + { + InternalUpdateBuffer = new(); + bool result = false; + foreach (var dependence in Dependences) + { + if (dependence.Value.ConvertTo()) + { + InternalUpdateBuffer.Add(dependence.Key); + result = true; + } + } + return result; + } + + private static void InternalUpdate(HashSet InternalUpdateBuffer) + { + foreach (var complete in InternalUpdateBuffer) + { + Dependences.Remove(complete); + + } + foreach (var complete in InternalUpdateBuffer) + { + Childs.Add(complete, UncompleteTargets[complete]); + UncompleteTargets.Remove(complete); + } + InternalUpdateBuffer.Clear(); + } + + public static Registering Register(Type slot, object target, Action completer, params Type[] dependences) + { + if (RegisterHistory.Add(slot) == false) + { + throw new InvalidOperationException("Illegal duplicate registrations"); + } + Completer[slot] = completer; + UncompleteTargets[slot] = target; + Dependences[slot] = new DependenceModel(from dependence in dependences select new TypeQuery(dependence)); + while (InternalComplete(out var buffer)) + InternalUpdate(buffer); + return new Registering(slot); + } + + public static Registering Register(T target, Action completer, params Type[] dependences) => Register(typeof(T), target!, completer, dependences); + + public static bool Contains(Type type) => Childs.ContainsKey(type); + + public static bool Contains() => Contains(typeof(T)); + + public static object InternalGet(Type type) => Childs[type]; + + public static object Get(Type type) => InternalGet(type); + + public static T Get() => (T)Get(typeof(T)); + } +} diff --git a/Convention/Runtime/Config.cs b/Convention/Runtime/Config.cs index 5f72ee9..abd0192 100644 --- a/Convention/Runtime/Config.cs +++ b/Convention/Runtime/Config.cs @@ -6,34 +6,5 @@ using System.Threading.Tasks; namespace Convention { - namespace SAL - { - public interface IHasCheckMethod - { - bool Check(); - } - public class ReturnValue { } - - public enum ComparedFlag - { - Greater, Less, Equal, GreaterOrEqual, LessOrEqual, NotEqual - } - - [System.AttributeUsage(AttributeTargets.ReturnValue, Inherited = true, AllowMultiple = true)] - public sealed class SuccessAttribute : Attribute, IHasCheckMethod - { - private List exprs = new(); - - public bool Check() - { - throw new NotImplementedException(); - } - } - - } - - public static class PlatformIndictaor - { - } }