2023-03-29 06:19:24 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using DCFApixels.DragonECS.Internal;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace DCFApixels.DragonECS
|
2023-02-13 21:11:54 +08:00
|
|
|
|
{
|
2023-03-29 06:19:24 +08:00
|
|
|
|
|
|
|
|
|
namespace Internal
|
|
|
|
|
{
|
|
|
|
|
internal class InjectController
|
|
|
|
|
{
|
|
|
|
|
private EcsSystems _source;
|
|
|
|
|
private InjectSystemBase[] _injectSystems;
|
|
|
|
|
private int _injectCount;
|
|
|
|
|
|
|
|
|
|
public InjectController(EcsSystems source)
|
|
|
|
|
{
|
|
|
|
|
_injectCount = 0;
|
|
|
|
|
_source = source;
|
|
|
|
|
_injectSystems = _source.AllSystems.OfType<InjectSystemBase>().ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool OnInject()
|
|
|
|
|
{
|
|
|
|
|
_injectCount++;
|
|
|
|
|
return IsInjectionEnd;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsInjectionEnd => _injectSystems.Length <= _injectCount;
|
|
|
|
|
|
|
|
|
|
public void Destroy()
|
|
|
|
|
{
|
|
|
|
|
_source = null;
|
|
|
|
|
_injectSystems = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public interface IEcsInject : IEcsSystem
|
|
|
|
|
{
|
|
|
|
|
public void Inject(object obj);
|
|
|
|
|
}
|
|
|
|
|
[DebugColor(DebugColor.Gray)]
|
|
|
|
|
public sealed class InjectRunner : EcsRunner<IEcsInject>, IEcsInject
|
|
|
|
|
{
|
|
|
|
|
void IEcsInject.Inject(object obj)
|
|
|
|
|
{
|
|
|
|
|
foreach (var item in targets) item.Inject(obj);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-12 20:45:18 +08:00
|
|
|
|
public interface IEcsInject<T> : IEcsSystem
|
2023-03-12 01:33:48 +08:00
|
|
|
|
{
|
|
|
|
|
public void Inject(T obj);
|
|
|
|
|
}
|
2023-03-26 11:19:03 +08:00
|
|
|
|
[DebugColor(DebugColor.Gray)]
|
2023-03-12 02:48:51 +08:00
|
|
|
|
public sealed class InjectRunner<T> : EcsRunner<IEcsInject<T>>, IEcsInject<T>
|
2023-02-13 21:11:54 +08:00
|
|
|
|
{
|
2023-03-12 02:02:39 +08:00
|
|
|
|
void IEcsInject<T>.Inject(T obj)
|
|
|
|
|
{
|
2023-03-29 06:19:24 +08:00
|
|
|
|
foreach (var item in targets) item.Inject(obj);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public interface IEcsInjectCallbacks : IEcsSystem
|
|
|
|
|
{
|
|
|
|
|
public void OnInjectionBefore();
|
|
|
|
|
public void OnInjectionAfter();
|
|
|
|
|
}
|
|
|
|
|
[DebugColor(DebugColor.Gray)]
|
|
|
|
|
public sealed class InjectCallbacksRunner : EcsRunner<IEcsInjectCallbacks>, IEcsInjectCallbacks
|
|
|
|
|
{
|
|
|
|
|
public void OnInjectionAfter()
|
|
|
|
|
{
|
|
|
|
|
foreach (var item in targets) item.OnInjectionAfter();
|
|
|
|
|
}
|
|
|
|
|
public void OnInjectionBefore()
|
|
|
|
|
{
|
|
|
|
|
foreach (var item in targets) item.OnInjectionBefore();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class InjectSystemBase
|
|
|
|
|
{
|
|
|
|
|
private static int _injectSystemID = EcsDebug.RegisterMark("InjectSystem");
|
|
|
|
|
|
|
|
|
|
protected static void ProfileMarkerBegin()
|
|
|
|
|
{
|
|
|
|
|
EcsDebug.ProfileMarkBegin(_injectSystemID);
|
|
|
|
|
}
|
|
|
|
|
protected static void ProfileMarkerEnd()
|
|
|
|
|
{
|
|
|
|
|
EcsDebug.ProfileMarkEnd(_injectSystemID);
|
2023-03-12 02:02:39 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-12 01:33:48 +08:00
|
|
|
|
|
2023-03-26 11:19:03 +08:00
|
|
|
|
[DebugColor(DebugColor.Gray)]
|
2023-03-29 06:19:24 +08:00
|
|
|
|
public class InjectSystem<T> : InjectSystemBase, IEcsPreInitSystem, IEcsInject<InjectController>, IEcsInjectCallbacks
|
2023-03-12 02:02:39 +08:00
|
|
|
|
{
|
2023-02-13 21:11:54 +08:00
|
|
|
|
private T _injectedData;
|
|
|
|
|
|
2023-03-29 06:19:24 +08:00
|
|
|
|
private InjectController _injectController;
|
|
|
|
|
void IEcsInject<InjectController>.Inject(InjectController obj) => _injectController = obj;
|
|
|
|
|
|
2023-03-16 01:49:14 +08:00
|
|
|
|
public InjectSystem(T injectedData)
|
2023-02-13 21:11:54 +08:00
|
|
|
|
{
|
|
|
|
|
_injectedData = injectedData;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-26 11:19:03 +08:00
|
|
|
|
public void PreInit(EcsSystems systems)
|
2023-02-13 21:11:54 +08:00
|
|
|
|
{
|
2023-03-29 06:19:24 +08:00
|
|
|
|
if(_injectController == null)
|
|
|
|
|
{
|
|
|
|
|
ProfileMarkerBegin();
|
|
|
|
|
|
|
|
|
|
_injectController = new InjectController(systems);
|
|
|
|
|
var injectMapRunner = systems.GetRunner<IEcsInject<InjectController>>();
|
|
|
|
|
systems.GetRunner<IEcsInjectCallbacks>().OnInjectionBefore();
|
|
|
|
|
injectMapRunner.Inject(_injectController);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var injectRunnerGeneric = systems.GetRunner<IEcsInject<T>>();
|
|
|
|
|
var injectRunner = systems.GetRunner<IEcsInject>();
|
|
|
|
|
injectRunnerGeneric.Inject(_injectedData);
|
|
|
|
|
injectRunner.Inject(_injectedData);
|
|
|
|
|
|
|
|
|
|
if (_injectController.OnInject())
|
|
|
|
|
{
|
|
|
|
|
_injectController.Destroy();
|
|
|
|
|
var injectCallbacksRunner = systems.GetRunner<IEcsInjectCallbacks>();
|
|
|
|
|
injectCallbacksRunner.OnInjectionAfter();
|
|
|
|
|
ProfileMarkerEnd();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnInjectionBefore() { }
|
|
|
|
|
|
|
|
|
|
public void OnInjectionAfter()
|
|
|
|
|
{
|
|
|
|
|
_injectController = null;
|
2023-02-13 21:11:54 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-16 01:49:14 +08:00
|
|
|
|
public static class InjectSystemExstensions
|
2023-02-13 21:11:54 +08:00
|
|
|
|
{
|
2023-03-26 11:19:03 +08:00
|
|
|
|
public static EcsSystems.Builder Inject<T>(this EcsSystems.Builder self, T data)
|
2023-02-13 21:11:54 +08:00
|
|
|
|
{
|
2023-03-16 01:49:14 +08:00
|
|
|
|
self.Add(new InjectSystem<T>(data));
|
2023-02-13 21:11:54 +08:00
|
|
|
|
return self;
|
|
|
|
|
}
|
2023-03-26 11:19:03 +08:00
|
|
|
|
public static EcsSystems.Builder Inject<A, B>(this EcsSystems.Builder self, A a, B b)
|
2023-02-13 21:11:54 +08:00
|
|
|
|
{
|
2023-03-26 11:19:03 +08:00
|
|
|
|
self.Inject(a).Inject(b);
|
2023-02-13 21:11:54 +08:00
|
|
|
|
return self;
|
|
|
|
|
}
|
2023-03-26 11:19:03 +08:00
|
|
|
|
public static EcsSystems.Builder Inject<A, B, C, D>(this EcsSystems.Builder self, A a, B b, C c, D d)
|
2023-02-13 21:11:54 +08:00
|
|
|
|
{
|
2023-03-26 11:19:03 +08:00
|
|
|
|
self.Inject(a).Inject(b).Inject(c).Inject(d);
|
2023-02-13 21:11:54 +08:00
|
|
|
|
return self;
|
|
|
|
|
}
|
2023-03-26 11:19:03 +08:00
|
|
|
|
public static EcsSystems.Builder Inject<A, B, C, D, E>(this EcsSystems.Builder self, A a, B b, C c, D d, E e)
|
|
|
|
|
{
|
|
|
|
|
self.Inject(a).Inject(b).Inject(c).Inject(d).Inject(e);
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
public static EcsSystems.Builder Inject<A, B, C, D, E, F>(this EcsSystems.Builder self, A a, B b, C c, D d, E e, F f)
|
2023-02-13 21:11:54 +08:00
|
|
|
|
{
|
2023-03-26 11:19:03 +08:00
|
|
|
|
self.Inject(a).Inject(b).Inject(c).Inject(d).Inject(e).Inject(f);
|
2023-02-13 21:11:54 +08:00
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|