mirror of
https://github.com/DCFApixels/DragonECS.git
synced 2025-09-18 09:54:35 +08:00
Update runner
This commit is contained in:
parent
66b136df92
commit
6ae6775e00
@ -6,8 +6,13 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace DCFApixels.DragonECS
|
namespace DCFApixels.DragonECS
|
||||||
{
|
{
|
||||||
|
public interface IEcsInject<T>
|
||||||
|
{
|
||||||
|
public void Inject(T obj);
|
||||||
|
}
|
||||||
public class InjectProcessor<T> : IDo<_PreInit>
|
public class InjectProcessor<T> : IDo<_PreInit>
|
||||||
{
|
{
|
||||||
|
|
||||||
private T _injectedData;
|
private T _injectedData;
|
||||||
|
|
||||||
public InjectProcessor(T injectedData)
|
public InjectProcessor(T injectedData)
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
namespace DCFApixels.DragonECS
|
namespace DCFApixels.DragonECS
|
||||||
{
|
{
|
||||||
public interface IEcsProcessor { }
|
|
||||||
|
|
||||||
public struct _PreInit { }
|
public struct _PreInit { }
|
||||||
public struct _Init { }
|
public struct _Init { }
|
||||||
|
@ -1,50 +0,0 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using System;
|
|
||||||
|
|
||||||
namespace DCFApixels.DragonECS
|
|
||||||
{
|
|
||||||
public interface IEcsProcessorsMessenger
|
|
||||||
{
|
|
||||||
public EcsSession Source { get; }
|
|
||||||
}
|
|
||||||
public interface IEcsProcessorsMessenger<TMessage> : IEcsProcessorsMessenger where TMessage : IEcsMessage { }
|
|
||||||
public class EcsProcessorsMessenger<TMessage> : IEcsProcessorsMessenger<TMessage>, IDisposable
|
|
||||||
where TMessage : IEcsMessage
|
|
||||||
{
|
|
||||||
private readonly EcsSession _source;
|
|
||||||
private readonly IReceive<TMessage>[] _targets;
|
|
||||||
|
|
||||||
public EcsSession Source => _source;
|
|
||||||
public IReadOnlyList<IReceive<TMessage>> Targets => _targets;
|
|
||||||
|
|
||||||
internal EcsProcessorsMessenger(EcsSession source)
|
|
||||||
{
|
|
||||||
_source = source;
|
|
||||||
List<IReceive<TMessage>> list = new List<IReceive<TMessage>>();
|
|
||||||
|
|
||||||
foreach (var item in _source.AllProcessors)
|
|
||||||
{
|
|
||||||
if (item is IReceive<TMessage> targetItem)
|
|
||||||
{
|
|
||||||
list.Add(targetItem);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_targets = list.ToArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Send(TMessage message)
|
|
||||||
{
|
|
||||||
Send(in message);
|
|
||||||
}
|
|
||||||
public void Send(in TMessage message)
|
|
||||||
{
|
|
||||||
foreach (var item in _targets)
|
|
||||||
{
|
|
||||||
item.Do(_source, in message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Destroy() => _source.OnMessengerDetroyed(this);
|
|
||||||
void IDisposable.Dispose() => Destroy();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,45 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
namespace DCFApixels.DragonECS
|
|
||||||
{
|
|
||||||
public interface IEcsProcessorsRunner
|
|
||||||
{
|
|
||||||
public EcsSession Source { get; }
|
|
||||||
public void Run();
|
|
||||||
}
|
|
||||||
public class EcsProcessorsRunner<TDoTag> : IEcsProcessorsRunner, IDisposable
|
|
||||||
{
|
|
||||||
private readonly EcsSession _source;
|
|
||||||
private readonly IDo<TDoTag>[] _targets;
|
|
||||||
|
|
||||||
public EcsSession Source => _source;
|
|
||||||
public IReadOnlyList<IDo<TDoTag>> Targets => _targets;
|
|
||||||
|
|
||||||
internal EcsProcessorsRunner(EcsSession source)
|
|
||||||
{
|
|
||||||
_source = source;
|
|
||||||
List<IDo<TDoTag>> list = new List<IDo<TDoTag>>();
|
|
||||||
|
|
||||||
foreach (var item in _source.AllProcessors)
|
|
||||||
{
|
|
||||||
if (item is IDo<TDoTag> targetItem)
|
|
||||||
{
|
|
||||||
list.Add(targetItem);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_targets = list.ToArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Run()
|
|
||||||
{
|
|
||||||
foreach (var item in _targets)
|
|
||||||
{
|
|
||||||
item.Do(_source);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Destroy() => _source.OnRunnerDetroyed(this);
|
|
||||||
void IDisposable.Dispose() => Destroy();
|
|
||||||
}
|
|
||||||
}
|
|
@ -7,35 +7,35 @@ using System.Runtime.CompilerServices;
|
|||||||
namespace DCFApixels.DragonECS
|
namespace DCFApixels.DragonECS
|
||||||
{
|
{
|
||||||
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = true)]
|
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = true)]
|
||||||
sealed class RunnerFilterAttribute : Attribute
|
sealed class EcsRunnerFilterAttribute : Attribute
|
||||||
{
|
{
|
||||||
public readonly Type interfaceType;
|
public readonly Type interfaceType;
|
||||||
public readonly object filter;
|
public readonly object filter;
|
||||||
public RunnerFilterAttribute(Type interfaceType, object filter)
|
public EcsRunnerFilterAttribute(Type interfaceType, object filter)
|
||||||
{
|
{
|
||||||
this.interfaceType = interfaceType;
|
this.interfaceType = interfaceType;
|
||||||
this.filter = filter;
|
this.filter = filter;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface IProcessor { }
|
public interface IEcsProcessor { }
|
||||||
|
|
||||||
public static class IProcessorExtensions
|
public static class IEcsProcessorExtensions
|
||||||
{
|
{
|
||||||
public static bool IsRunner(this IProcessor self)
|
public static bool IsRunner(this IEcsProcessor self)
|
||||||
{
|
{
|
||||||
return self is IRunner;
|
return self is IEcsRunner;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static class RunnerActivator
|
internal static class EcsRunnerActivator
|
||||||
{
|
{
|
||||||
private static bool _isInit = false;
|
private static bool _isInit = false;
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static void Init()
|
public static void Init()
|
||||||
{
|
{
|
||||||
if (_isInit) return;
|
if (_isInit) return;
|
||||||
Type targetType = typeof(Runner<>);
|
Type targetType = typeof(EcsRunner<>);
|
||||||
var subclasses = Assembly.GetAssembly(targetType).GetTypes().Where(type => type.BaseType != null && type.BaseType.IsGenericType && targetType == type.BaseType.GetGenericTypeDefinition());
|
var subclasses = Assembly.GetAssembly(targetType).GetTypes().Where(type => type.BaseType != null && type.BaseType.IsGenericType && targetType == type.BaseType.GetGenericTypeDefinition());
|
||||||
foreach (var item in subclasses)
|
foreach (var item in subclasses)
|
||||||
{
|
{
|
||||||
@ -45,10 +45,11 @@ namespace DCFApixels.DragonECS
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface IRunner { }
|
|
||||||
|
|
||||||
public abstract class Runner<TInterface> : IProcessor, IRunner
|
public interface IEcsRunner { }
|
||||||
where TInterface : IProcessor
|
|
||||||
|
public abstract class EcsRunner<TInterface> : IEcsProcessor, IEcsRunner
|
||||||
|
where TInterface : IEcsProcessor
|
||||||
{
|
{
|
||||||
internal static void Init(Type subclass)
|
internal static void Init(Type subclass)
|
||||||
{
|
{
|
||||||
@ -66,58 +67,56 @@ namespace DCFApixels.DragonECS
|
|||||||
{
|
{
|
||||||
throw new ArgumentException($"{typeof(TInterface).FullName} is not interface");
|
throw new ArgumentException($"{typeof(TInterface).FullName} is not interface");
|
||||||
}
|
}
|
||||||
if (interfaces.Length != 1 || interfaces[0] != typeof(IProcessor))
|
if (interfaces.Length != 1 || interfaces[0] != typeof(IEcsProcessor))
|
||||||
{
|
{
|
||||||
throw new ArgumentException($"{typeof(TInterface).FullName} does not directly inherit the {nameof(IProcessor)} interface");
|
throw new ArgumentException($"{typeof(TInterface).FullName} does not directly inherit the {nameof(IEcsProcessor)} interface");
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
_subclass = subclass;
|
_subclass = subclass;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TInterface Instantiate(IEnumerable<IProcessor> targets, object filter)
|
public static TInterface Instantiate(IEnumerable<IEcsProcessor> targets, object filter)
|
||||||
{
|
{
|
||||||
Type interfaceType = typeof(TInterface);
|
Type interfaceType = typeof(TInterface);
|
||||||
|
|
||||||
IEnumerable<IProcessor> newTargets;
|
IEnumerable<TInterface> newTargets = targets.OfType<TInterface>();
|
||||||
|
|
||||||
if (filter != null)
|
if (filter != null)
|
||||||
{
|
{
|
||||||
newTargets = targets.Where(o =>
|
newTargets = newTargets.Where(o =>
|
||||||
{
|
{
|
||||||
if (o is TInterface == false) return false;
|
if (o is TInterface == false) return false;
|
||||||
var atr = o.GetType().GetCustomAttribute<RunnerFilterAttribute>();
|
var atr = o.GetType().GetCustomAttribute<EcsRunnerFilterAttribute>();
|
||||||
return atr != null && atr.interfaceType == interfaceType && atr.filter.Equals(filter);
|
return atr != null && atr.interfaceType == interfaceType && atr.filter.Equals(filter);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
newTargets = targets.Where(o =>
|
newTargets = newTargets.Where(o =>
|
||||||
{
|
{
|
||||||
if (o is TInterface == false) return false;
|
if (o is TInterface == false) return false;
|
||||||
var atr = o.GetType().GetCustomAttribute<RunnerFilterAttribute>();
|
var atr = o.GetType().GetCustomAttribute<EcsRunnerFilterAttribute>();
|
||||||
return atr == null || atr.interfaceType == interfaceType && atr.filter == null;
|
return atr == null || atr.interfaceType == interfaceType && atr.filter == null;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return Instantiate(newTargets.Select(o => (TInterface)o).ToArray());
|
return Instantiate(newTargets.ToArray());
|
||||||
}
|
}
|
||||||
public static TInterface Instantiate(IEnumerable<IProcessor> targets)
|
public static TInterface Instantiate(IEnumerable<IEcsProcessor> targets)
|
||||||
{
|
{
|
||||||
return Instantiate(targets.Where(o => o is TInterface).Select(o => (TInterface)o).ToArray());
|
return Instantiate(targets.OfType<TInterface>().ToArray());
|
||||||
}
|
}
|
||||||
internal static TInterface Instantiate(TInterface[] targets)
|
internal static TInterface Instantiate(TInterface[] targets)
|
||||||
{
|
{
|
||||||
RunnerActivator.Init();
|
EcsRunnerActivator.Init();
|
||||||
var instance = (Runner<TInterface>)Activator.CreateInstance(_subclass);
|
var instance = (EcsRunner<TInterface>)Activator.CreateInstance(_subclass);
|
||||||
return (TInterface)(IProcessor)instance.Set(targets);
|
return (TInterface)(IEcsProcessor)instance.Set(targets);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private static Type _subclass;
|
private static Type _subclass;
|
||||||
protected TInterface[] targets;
|
protected TInterface[] targets;
|
||||||
|
|
||||||
private Runner<TInterface> Set(TInterface[] targets)
|
private EcsRunner<TInterface> Set(TInterface[] targets)
|
||||||
{
|
{
|
||||||
this.targets = targets;
|
this.targets = targets;
|
||||||
return this;
|
return this;
|
@ -1,15 +0,0 @@
|
|||||||
using System;
|
|
||||||
|
|
||||||
namespace DCFApixels.Assets.DragonECS.src.React
|
|
||||||
{
|
|
||||||
[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
|
|
||||||
sealed class WorldFilterAttribute : Attribute //TODO
|
|
||||||
{
|
|
||||||
public readonly string[] worlds;
|
|
||||||
|
|
||||||
public WorldFilterAttribute(params string[] worlds)
|
|
||||||
{
|
|
||||||
this.worlds = worlds;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user