2025-03-14 16:53:25 +08:00
#if DISABLE_DEBUG
#undef DEBUG
#endif
using DCFApixels.DragonECS.Internal ;
2024-04-28 18:36:14 +08:00
using System ;
2024-02-22 22:16:03 +08:00
using System.Collections.Generic ;
2024-09-08 19:39:43 +08:00
using System.Linq ;
2024-08-20 11:53:23 +08:00
using System.Runtime.CompilerServices ;
2024-02-22 22:16:03 +08:00
namespace DCFApixels.DragonECS
{
2024-08-20 11:53:23 +08:00
public class Injector : IInjector
2024-02-22 22:16:03 +08:00
{
private EcsPipeline _pipeline ;
private Dictionary < Type , InjectionBranch > _branches = new Dictionary < Type , InjectionBranch > ( 32 ) ;
private Dictionary < Type , InjectionNodeBase > _nodes = new Dictionary < Type , InjectionNodeBase > ( 32 ) ;
private bool _isInit = false ;
2025-03-14 16:53:25 +08:00
#if DEBUG | | ENABLE_DRAGONECS_ASSERT_CHEKS
2024-09-08 19:39:43 +08:00
private HashSet < Type > _requiredInjectionTypes = new HashSet < Type > ( ) ;
#endif
2024-08-20 11:53:23 +08:00
public EcsPipeline Pipelie
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return _pipeline ; }
}
2024-04-28 18:36:14 +08:00
2024-02-22 22:16:03 +08:00
private Injector ( ) { }
2024-02-22 23:48:10 +08:00
2024-08-20 11:53:23 +08:00
#region Inject / Extract / AddNode
2024-02-22 22:16:03 +08:00
public void Inject < T > ( T obj )
{
2024-03-14 00:24:23 +08:00
object raw = obj ;
2024-09-08 19:39:43 +08:00
Type tType = typeof ( T ) ;
Type objType = obj . GetType ( ) ;
if ( _branches . TryGetValue ( objType , out InjectionBranch branch ) = = false )
2024-02-22 22:16:03 +08:00
{
2024-09-08 19:39:43 +08:00
if ( _nodes . ContainsKey ( tType ) = = false )
2024-02-25 02:33:24 +08:00
{
2024-09-08 19:39:43 +08:00
InitNode ( new InjectionNode < T > ( ) ) ;
2024-02-25 02:33:24 +08:00
}
2024-09-08 19:39:43 +08:00
bool hasNode = _nodes . ContainsKey ( objType ) ;
if ( hasNode = = false & & obj is IInjectionUnit unit )
2024-02-25 02:33:24 +08:00
{
2024-09-08 19:41:09 +08:00
unit . InitInjectionNode ( new InjectionGraph ( this ) ) ;
2024-09-08 19:39:43 +08:00
hasNode = _nodes . ContainsKey ( objType ) ;
}
2024-09-07 21:25:21 +08:00
2024-09-08 19:39:43 +08:00
branch = new InjectionBranch ( this , objType ) ;
InitBranch ( branch ) ;
2025-03-14 16:53:25 +08:00
#if DEBUG | | ENABLE_DRAGONECS_ASSERT_CHEKS
2024-09-08 19:39:43 +08:00
foreach ( var requiredInjectionType in _requiredInjectionTypes )
{
if ( requiredInjectionType . IsAssignableFrom ( objType ) )
{
if ( _nodes . ContainsKey ( requiredInjectionType ) = = false )
{
2025-03-14 21:57:52 +08:00
throw new InjectionException ( $"A systems in the pipeline implements IEcsInject<{requiredInjectionType.Name}> interface, but no suitable injection node was found in the Injector. To create a node, use Injector.AddNode<{requiredInjectionType.Name}>() or implement the IInjectionUnit interface for type {objType.Name}." ) ;
2024-09-08 19:39:43 +08:00
}
2024-04-28 18:36:14 +08:00
}
2024-02-25 02:33:24 +08:00
}
2024-09-08 19:39:43 +08:00
#endif
2024-02-22 22:16:03 +08:00
}
2024-03-14 00:24:23 +08:00
branch . Inject ( raw ) ;
2024-02-22 22:16:03 +08:00
}
2025-03-13 20:46:51 +08:00
public void ExtractAllTo ( object target )
{
if ( target is IEcsInjectProcess = = false ) { return ; }
foreach ( var node in _nodes )
{
node . Value . ExtractTo ( target ) ;
}
}
2024-08-20 11:53:23 +08:00
public T Extract < T > ( )
{
return ( T ) Extract_Internal ( typeof ( T ) ) ;
}
2024-09-08 19:39:43 +08:00
private object Extract_Internal ( Type type ) //TODO проверить
2024-08-20 11:53:23 +08:00
{
2024-09-08 19:39:43 +08:00
if ( _nodes . TryGetValue ( type , out InjectionNodeBase node ) )
2024-08-20 11:53:23 +08:00
{
2024-09-08 19:39:43 +08:00
return node . CurrentInjectedDependencyRaw ;
2024-08-20 11:53:23 +08:00
}
2025-03-14 21:57:52 +08:00
throw new InjectionException ( $"The injection graph is missing a node for {type.Name} type. To create a node, use the Injector.AddNode<{type.Name}>() method directly in the injector or in the implementation of the IInjectionUnit for {type.Name}." ) ;
2024-08-20 11:53:23 +08:00
}
2024-04-28 18:36:14 +08:00
public void AddNode < T > ( )
{
2024-09-08 19:39:43 +08:00
if ( _nodes . ContainsKey ( typeof ( T ) ) = = false )
2024-04-28 18:36:14 +08:00
{
2024-09-08 19:39:43 +08:00
InitNode ( new InjectionNode < T > ( ) ) ;
2024-04-28 18:36:14 +08:00
}
}
#endregion
2024-02-22 22:16:03 +08:00
#region Internal
private void InitBranch ( InjectionBranch branch )
{
_branches . Add ( branch . Type , branch ) ;
foreach ( var item in _nodes )
{
var type = item . Key ;
var node = item . Value ;
if ( type . IsAssignableFrom ( branch . Type ) )
{
branch . AddNode ( node ) ;
}
}
}
private void InitNode ( InjectionNodeBase node )
{
if ( _pipeline ! = null )
{
node . Init ( _pipeline ) ;
}
_nodes . Add ( node . Type , node ) ;
foreach ( var item in _branches )
{
2024-04-28 18:36:14 +08:00
//var type = item.Key;
2024-02-22 22:16:03 +08:00
var branch = item . Value ;
2024-03-14 00:24:23 +08:00
if ( node . Type . IsAssignableFrom ( branch . Type ) )
2024-02-22 22:16:03 +08:00
{
branch . AddNode ( node ) ;
}
}
}
private bool IsCanInstantiated ( Type type )
{
return ! type . IsAbstract & & ! type . IsInterface ;
}
#endregion
#region Build
private void Init ( EcsPipeline pipeline )
{
2024-09-08 19:39:43 +08:00
if ( _isInit ) { Throw . Exception ( "Already initialized" ) ; }
2024-02-22 22:16:03 +08:00
_pipeline = pipeline ;
2024-09-08 19:39:43 +08:00
foreach ( var pair in _nodes )
2024-02-22 22:16:03 +08:00
{
2024-09-08 19:39:43 +08:00
pair . Value . Init ( pipeline ) ;
2024-02-22 22:16:03 +08:00
}
_isInit = true ;
2024-09-08 19:39:43 +08:00
2025-03-14 16:53:25 +08:00
#if DEBUG | | ENABLE_DRAGONECS_ASSERT_CHEKS
2024-09-08 19:39:43 +08:00
var systems = _pipeline . AllSystems ;
var injectType = typeof ( IEcsInject < > ) ;
foreach ( var system in systems )
{
var type = system . GetType ( ) ;
foreach ( var requiredInjectionType in type . GetInterfaces ( ) . Where ( o = > o . IsGenericType & & o . GetGenericTypeDefinition ( ) = = injectType ) . Select ( o = > o . GenericTypeArguments [ 0 ] ) )
{
_requiredInjectionTypes . Add ( requiredInjectionType ) ;
}
}
#endif
2024-02-22 22:16:03 +08:00
}
private bool TryDeclare < T > ( )
{
Type type = typeof ( T ) ;
if ( _nodes . ContainsKey ( type ) )
{
return false ;
}
2024-09-08 19:39:43 +08:00
InitNode ( new InjectionNode < T > ( ) ) ;
2024-02-23 18:34:40 +08:00
#if ! REFLECTION_DISABLED
2024-02-22 22:16:03 +08:00
if ( IsCanInstantiated ( type ) )
2024-02-23 18:34:40 +08:00
#endif
2024-02-22 22:16:03 +08:00
{
2024-04-28 18:36:14 +08:00
InitBranch ( new InjectionBranch ( this , type ) ) ;
2024-02-22 22:16:03 +08:00
}
return true ;
}
2024-02-25 18:33:17 +08:00
2024-09-07 19:07:47 +08:00
public class Builder : IInjector
2024-02-22 22:16:03 +08:00
{
private EcsPipeline . Builder _source ;
private Injector _instance ;
private List < InitInjectBase > _initInjections = new List < InitInjectBase > ( 16 ) ;
2024-12-17 12:22:22 +08:00
private EcsWorld _monoWorld ;
2024-02-22 22:16:03 +08:00
internal Builder ( EcsPipeline . Builder source )
{
_source = source ;
_instance = new Injector ( ) ;
}
2024-02-26 10:37:58 +08:00
public EcsPipeline . Builder AddNode < T > ( )
2024-02-22 22:16:03 +08:00
{
_instance . TryDeclare < T > ( ) ;
return _source ;
}
2024-02-22 23:48:10 +08:00
public EcsPipeline . Builder Inject < T > ( T obj )
2024-02-22 22:16:03 +08:00
{
2025-01-06 10:48:39 +08:00
if ( obj is EcsWorld objWorld )
2024-12-17 12:22:22 +08:00
{
2025-01-06 10:48:39 +08:00
if ( _monoWorld = = null )
2024-12-17 12:22:22 +08:00
{
_monoWorld = objWorld ;
}
else
{
Type monoWorldType = _monoWorld . GetType ( ) ;
Type objWorldType = objWorld . GetType ( ) ;
if ( monoWorldType ! = objWorldType )
{
2025-01-06 10:48:39 +08:00
if ( objWorldType = = typeof ( EcsWorld ) )
2024-12-17 12:22:22 +08:00
{ // Екземпляр EcsWorld имеет самый больший приоритет.
_monoWorld = objWorld ;
}
2025-01-06 10:48:39 +08:00
if ( objWorldType = = typeof ( EcsDefaultWorld ) & &
2024-12-17 12:22:22 +08:00
monoWorldType ! = typeof ( EcsWorld ) )
{ // Екземпляр EcsDefaultWorld имеет приоритет больше других типов, но меньше приоритета EcsWorld.
_monoWorld = objWorld ;
}
}
}
}
2024-02-22 22:16:03 +08:00
_initInjections . Add ( new InitInject < T > ( obj ) ) ;
2024-02-22 23:48:10 +08:00
return _source ;
2024-02-22 22:16:03 +08:00
}
2024-12-17 12:22:22 +08:00
public EcsPipeline . Builder Extract < T > ( ref T obj ) // TODO проверить
2024-09-07 19:07:47 +08:00
{
Type type = typeof ( T ) ;
for ( int i = _initInjections . Count - 1 ; i > = 0 ; i - - )
{
var item = _initInjections [ i ] ;
2024-09-08 19:39:43 +08:00
if ( type . IsAssignableFrom ( item . Type ) )
2024-09-07 19:07:47 +08:00
{
obj = ( T ) item . Raw ;
return _source ;
}
}
Throw . UndefinedException ( ) ;
2025-03-14 21:57:52 +08:00
return default ;
2024-09-07 19:07:47 +08:00
}
2024-02-22 22:16:03 +08:00
public Injector Build ( EcsPipeline pipeline )
{
2024-12-17 12:22:22 +08:00
var monoWorldProcess = pipeline . GetProcess < IMonoWorldInject > ( ) ; // TODO Проверить IMonoWorldInject
foreach ( var monoWorldSystem in monoWorldProcess )
{
monoWorldSystem . World = _monoWorld ;
}
2025-03-13 11:16:09 +08:00
var initInjectionCallbacks = pipeline . GetProcess < IOnInitInjectionComplete > ( ) ;
foreach ( var system in initInjectionCallbacks )
{
system . OnBeforeInitInjection ( ) ;
}
2024-02-22 22:16:03 +08:00
_instance . Init ( pipeline ) ;
foreach ( var item in _initInjections )
{
item . InjectTo ( _instance ) ;
}
2025-03-13 11:16:09 +08:00
foreach ( var system in initInjectionCallbacks )
2024-02-22 23:48:10 +08:00
{
system . OnInitInjectionComplete ( ) ;
}
2024-02-22 22:16:03 +08:00
return _instance ;
}
2024-06-24 22:06:02 +08:00
public void Add ( Builder other )
{
foreach ( var item in other . _initInjections )
{
_initInjections . Add ( item ) ;
}
}
2024-02-22 22:16:03 +08:00
2024-09-07 19:07:47 +08:00
void IInjector . Inject < T > ( T obj ) { Inject ( obj ) ; }
T IInjector . Extract < T > ( )
{
T result = default ;
Extract ( ref result ) ;
return result ;
}
2024-02-22 22:16:03 +08:00
private abstract class InitInjectBase
{
2024-09-07 19:07:47 +08:00
public abstract Type Type { get ; }
public abstract object Raw { get ; }
2024-02-22 22:16:03 +08:00
public abstract void InjectTo ( Injector instance ) ;
}
private sealed class InitInject < T > : InitInjectBase
{
private T _injectedData ;
2024-09-07 19:07:47 +08:00
public override Type Type { get { return typeof ( T ) ; } }
public override object Raw { get { return _injectedData ; } }
2024-02-22 22:16:03 +08:00
public InitInject ( T injectedData )
{
_injectedData = injectedData ;
}
public override void InjectTo ( Injector instance )
{
instance . Inject < T > ( _injectedData ) ;
}
}
}
#endregion
}
2024-09-07 19:07:47 +08:00
}