This commit is contained in:
Mikhail 2024-02-22 17:06:30 +08:00
parent a890e1d184
commit 92697989e1
5 changed files with 25 additions and 198 deletions

View File

@ -37,7 +37,9 @@ namespace DCFApixels.DragonECS
}
}
foreach (var item in _systemProoperties.Keys)
{
_notInjected.Add(item);
}
}
private static void Do(Type type, List<IInjectedProperty> result)
@ -139,10 +141,10 @@ namespace DCFApixels.DragonECS
private readonly struct InjectedPropertyRecord
{
public readonly IEcsProcess target;
public readonly IEcsSystem target;
public readonly IInjectedProperty property;
public EcsInjectAttribute Attribute => property.GetAutoInjectAttribute();
public InjectedPropertyRecord(IEcsProcess target, IInjectedProperty property)
public InjectedPropertyRecord(IEcsSystem target, IInjectedProperty property)
{
this.target = target;
this.property = property;
@ -152,28 +154,38 @@ namespace DCFApixels.DragonECS
[MetaTags(MetaTags.HIDDEN)]
[MetaColor(MetaColor.Gray)]
public class AutoInjectSystem : IEcsPreInject, IEcsInject<EcsPipeline>, IEcsPreInitInjectProcess
public class AutoInjectSystem : IEcsInject<object>, IEcsPipelineMember, IEcsPreInitInjectProcess
{
private EcsPipeline _pipeline;
EcsPipeline IEcsPipelineMember.Pipeline { get => _pipeline; set => _pipeline = value; }
private List<object> _delayedInjects = new List<object>();
private AutoInjectionMap _autoInjectionMap;
private bool _preInitInjectCompleted = false;
public void Inject(EcsPipeline obj) => _pipeline = obj;
public void PreInject(object obj)
public void Inject(object obj)
{
if (!_preInitInjectCompleted)
{
_delayedInjects.Add(obj);
}
else
{
_autoInjectionMap.Inject(obj.GetType(), obj);
}
}
public void OnPreInitInjectionBefore(EcsPipeline pipeline)
{
_pipeline = pipeline;
}
public void OnPreInitInjectionBefore() { }
public void OnPreInitInjectionAfter()
{
_autoInjectionMap = new AutoInjectionMap(_pipeline);
_preInitInjectCompleted = true;
foreach (var obj in _delayedInjects)
{
_autoInjectionMap.Inject(obj.GetType(), obj);
}
_autoInjectionMap.InjectDummy();
_autoInjectionMap.OnPreInitInjectionComplete();

View File

@ -1,179 +0,0 @@
//using System;
//using System.Collections.Generic;
//using System.Reflection;
//using System.Runtime.CompilerServices;
//
//namespace DCFApixels.DragonECS
//{
// public static class AutoProcesses
// {
// private static Dictionary<string, BuilderHandler> _builders;
// [MethodImpl(MethodImplOptions.AggressiveInlining)]
// private static bool TryGetCustomAttribute<T>(Type self, out T attribute) where T : Attribute
// {
// attribute = self.GetCustomAttribute<T>();
// return attribute != null;
// }
// static AutoProcesses()
// {
// _builders = new Dictionary<string, BuilderHandler>();
// foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
// {
// var types = assembly.GetTypes();
// foreach (var type in types)
// {
// if (type.IsGenericType)
// continue;
// if (TryGetCustomAttribute(type, out EcsProcessWrapperBuilderAttribute attr))
// {
// MethodInfo method = type.GetMethod(attr.wrapperBuilderMethodName, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
// BuilderHandler action = (BuilderHandler)Delegate.CreateDelegate(typeof(BuilderHandler), null, method);
// _builders.Add(attr.processMethodName, action);
// }
// }
// }
// }
// public static EcsPipeline.Builder AddAuto(this EcsPipeline.Builder self, object system, string layerName = null)
// {
// var methods = system.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
// foreach (var method in methods)
// {
// if (_builders.TryGetValue(method.Name, out var builder))
// {
// var process = builder(system, method);
// if (process != null)
// self.Add(process, layerName);
// }
// }
// if (system is IEcsProcess systemInterface)
// self.Add(systemInterface, layerName);
// return self;
// }
// private delegate IEcsProcess BuilderHandler(object targete, MethodInfo method);
// }
//
// [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Interface, Inherited = false, AllowMultiple = false)]
// sealed class EcsProcessWrapperBuilderAttribute : Attribute
// {
// public readonly string processMethodName;
// public readonly string wrapperBuilderMethodName;
// public EcsProcessWrapperBuilderAttribute(string processMethodName, string wrapperBuilderMethodName = "Builder")
// {
// this.processMethodName = processMethodName;
// this.wrapperBuilderMethodName = wrapperBuilderMethodName;
// }
// }
// public static class EcsAutoProcessUtility
// {
// public static TDelegate CreateDelegate<TDelegate>(object system, MethodInfo method) where TDelegate : Delegate
// {
// return (TDelegate)Delegate.CreateDelegate(typeof(TDelegate), system, method);
// }
// }
//
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// internal class EcsProcessWrapperBase
// {
// [MethodImpl(MethodImplOptions.AggressiveInlining)]
// public static bool CheckParameters(MethodInfo method)
// {
// return method.GetParameters().Length <= 0;
// }
// [MethodImpl(MethodImplOptions.AggressiveInlining)]
// public static Action CreateAction(object target, MethodInfo method)
// {
// return EcsAutoProcessUtility.CreateDelegate<Action>(target, method);
// }
// }
// internal class IEcsProcessWrapper : EcsProcessWrapperBase, IEcsMetaProvider
// {
// public object system;
// public Action a;
// public object MetaSource => system;
// }
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// [EcsProcessWrapperBuilder(nameof(PreInit), nameof(Builder))]
// internal class EcsPreInitProcessWrapper : IEcsProcessWrapper, IEcsPreInitProcess
// {
// public EcsPreInitProcessWrapper(object target, Action a) { system = target; this.a = a; }
// [MethodImpl(MethodImplOptions.AggressiveInlining)]
// public void PreInit() => a();
// public static IEcsProcess Builder(object target, MethodInfo method)
// {
// if (target is IEcsPreInitProcess) return null;
// if (CheckParameters(method))
// return new EcsPreInitProcessWrapper(target, CreateAction(target, method));
// return null;
// }
// }
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// [EcsProcessWrapperBuilder(nameof(Init), nameof(Builder))]
// internal class EcsInitProcessWrapper : IEcsProcessWrapper, IEcsInitProcess
// {
// public EcsInitProcessWrapper(object target, Action a) { system = target; this.a = a; }
// [MethodImpl(MethodImplOptions.AggressiveInlining)]
// public void Init() => a();
// public static IEcsProcess Builder(object target, MethodInfo method)
// {
// if (target is IEcsInitProcess) return null;
// if (CheckParameters(method))
// return new EcsInitProcessWrapper(target, CreateAction(target, method));
// return null;
// }
// }
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// [EcsProcessWrapperBuilder(nameof(Run), nameof(Builder))]
// internal class EcsRunProcessEmptyWrapper : IEcsProcessWrapper, IEcsRunProcess
// {
// public EcsRunProcessEmptyWrapper(object target, Action a) { system = target; this.a = a; }
// [MethodImpl(MethodImplOptions.AggressiveInlining)]
// public void Run() => a();
// public static IEcsProcess Builder(object target, MethodInfo method)
// {
// if (target is IEcsRunProcess) return null;
// if (CheckParameters(method))
// return new EcsRunProcessEmptyWrapper(target, CreateAction(target, method));
// return null;
// }
// }
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// [EcsProcessWrapperBuilder(nameof(Destroy), nameof(Builder))]
// internal class EcsDestroyProcessEmptyWrapper : IEcsProcessWrapper, IEcsDestroyProcess
// {
// public EcsDestroyProcessEmptyWrapper(object target, Action a) { system = target; this.a = a; }
// [MethodImpl(MethodImplOptions.AggressiveInlining)]
// public void Destroy() => a();
// public static IEcsProcess Builder(object target, MethodInfo method)
// {
// if (target is IEcsRunProcess) return null;
// if (CheckParameters(method))
// return new EcsDestroyProcessEmptyWrapper(target, CreateAction(target, method));
// return null;
// }
// }
//
// /*
// public interface ISomeCustomeProcess : IEcsProcess
// {
// void DoSomething();
// }
// //Только при наличии этого атрибута будет вызван метод Builder который создаст обертку для DoSomething
// [EcsProcessWrapperBuilder(nameof(DoSomething), nameof(Builder))]
// internal class SomeCustomeProcessWrapper : ISomeCustomeProcess, IEcsDebugMetaProvider
// {
// public object system;
// public Action action;
// //IEcsDebugMetaProvider.DebugMetaSource используется чтобы для обертки отображалось данные из debug-атрибутов вроде DebugName
// public object DebugMetaSource => system;
// public SomeCustomeProcessWrapper(object system, Action action) { this.system = system; this.action = action; }
// public void DoSomething() => action();
// public static IEcsProcess Builder(object system, MethodInfo method)
// {
// //Исключает те системы которые уже имеют интерфейс, иначе в рантайме вызов метода-процесса будет дублироваться
// if (system is ISomeCustomeProcess) return null; //возвращение null
// return new SomeCustomeProcessWrapper(system, EcsAutoProcessUtility.CreateDelegate<Action>(system, method));
// }
// }
// */
//}
//

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: b12c6a19f3706144c80a1d33f86ec659
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

7
src/Processes.cs Normal file
View File

@ -0,0 +1,7 @@
namespace DCFApixels.DragonECS
{
public interface IInjectRaw : IEcsSystem
{
void Inject(object obj);
}
}

View File

@ -1,7 +1,6 @@
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
namespace DCFApixels.DragonECS
{
@ -34,6 +33,5 @@ namespace DCFApixels.DragonECS
public EcsAutoInjectionException() { }
public EcsAutoInjectionException(string message) : base(EcsConsts.EXCEPTION_MESSAGE_PREFIX + message) { }
public EcsAutoInjectionException(string message, Exception inner) : base(EcsConsts.EXCEPTION_MESSAGE_PREFIX + message, inner) { }
protected EcsAutoInjectionException(SerializationInfo info, StreamingContext context) : base(info, context) { }
}
}