77using System . Reflection ;
88
99namespace Leopotam . EcsLite . Di {
10+ #if LEOECSLITE_DI
11+ public interface IEcsInjectSystem : IEcsSystem {
12+ void Inject ( IEcsSystems systems , params object [ ] injects ) ;
13+ }
14+ #endif
1015 public static class Extensions {
11- public static EcsSystems Inject ( this EcsSystems systems , params object [ ] injects ) {
16+ public static IEcsSystems Inject ( this IEcsSystems systems , params object [ ] injects ) {
1217 if ( injects == null ) { injects = Array . Empty < object > ( ) ; }
13- IEcsSystem [ ] allSystems = null ;
14- var systemsCount = systems . GetAllSystems ( ref allSystems ) ;
15-
16- for ( var i = 0 ; i < systemsCount ; i ++ ) {
17- var system = allSystems [ i ] ;
18- foreach ( var f in system . GetType ( ) . GetFields ( BindingFlags . Public | BindingFlags . NonPublic | BindingFlags . Instance ) ) {
19- // skip statics.
20- if ( f . IsStatic ) { continue ; }
21- // EcsWorldInject, EcsFilterInject, EcsPoolInject, EcsSharedInject.
22- if ( InjectBuiltIns ( f , system , systems ) ) { continue ; }
23- // EcsDataInject.
24- if ( InjectCustoms ( f , system , injects ) ) { continue ; }
18+ foreach ( var system in systems . GetAllSystems ( ) ) {
19+ #if LEOECSLITE_DI
20+ if ( system is IEcsInjectSystem injectSystem ) {
21+ injectSystem . Inject ( systems , injects ) ;
22+ continue ;
2523 }
24+ #endif
25+ InjectToSystem ( system , systems , injects ) ;
2626 }
2727
2828 return systems ;
2929 }
3030
31- static bool InjectBuiltIns ( FieldInfo fieldInfo , IEcsSystem system , EcsSystems systems ) {
31+ public static void InjectToSystem ( IEcsSystem system , IEcsSystems systems , object [ ] injects ) {
32+ foreach ( var f in system . GetType ( ) . GetFields ( BindingFlags . Public | BindingFlags . NonPublic | BindingFlags . Instance ) ) {
33+ // skip statics.
34+ if ( f . IsStatic ) { continue ; }
35+ // EcsWorldInject, EcsFilterInject, EcsPoolInject, EcsSharedInject.
36+ if ( InjectBuiltIns ( f , system , systems ) ) { continue ; }
37+ // EcsDataInject.
38+ if ( InjectCustoms ( f , system , injects ) ) { }
39+ }
40+ }
41+
42+ static bool InjectBuiltIns ( FieldInfo fieldInfo , IEcsSystem system , IEcsSystems systems ) {
3243 if ( typeof ( IEcsDataInject ) . IsAssignableFrom ( fieldInfo . FieldType ) ) {
3344 var instance = ( IEcsDataInject ) fieldInfo . GetValue ( system ) ;
3445 instance . Fill ( systems ) ;
@@ -50,7 +61,7 @@ static bool InjectCustoms (FieldInfo fieldInfo, IEcsSystem system, object[] inje
5061 }
5162
5263 public interface IEcsDataInject {
53- void Fill ( EcsSystems systems ) ;
64+ void Fill ( IEcsSystems systems ) ;
5465 }
5566
5667 public interface IEcsCustomDataInject {
@@ -75,7 +86,7 @@ public static implicit operator EcsFilterInject<TInc> (string worldName) {
7586 return new EcsFilterInject < TInc > { _worldName = worldName } ;
7687 }
7788
78- void IEcsDataInject . Fill ( EcsSystems systems ) {
89+ void IEcsDataInject . Fill ( IEcsSystems systems ) {
7990 Pools = default ;
8091 Value = Pools . Fill ( systems . GetWorld ( _worldName ) ) . End ( ) ;
8192 }
@@ -92,7 +103,7 @@ public static implicit operator EcsFilterInject<TInc, TExc> (string worldName) {
92103 return new EcsFilterInject < TInc , TExc > { _worldName = worldName } ;
93104 }
94105
95- void IEcsDataInject . Fill ( EcsSystems systems ) {
106+ void IEcsDataInject . Fill ( IEcsSystems systems ) {
96107 Pools = default ;
97108 TExc exc = default ;
98109 Value = exc . Fill ( Pools . Fill ( systems . GetWorld ( _worldName ) ) ) . End ( ) ;
@@ -107,15 +118,15 @@ public static implicit operator EcsPoolInject<T> (string worldName) {
107118 return new EcsPoolInject < T > { _worldName = worldName } ;
108119 }
109120
110- void IEcsDataInject . Fill ( EcsSystems systems ) {
121+ void IEcsDataInject . Fill ( IEcsSystems systems ) {
111122 Value = systems . GetWorld ( _worldName ) . GetPool < T > ( ) ;
112123 }
113124 }
114125
115126 public struct EcsSharedInject < T > : IEcsDataInject where T : class {
116127 public T Value ;
117128
118- void IEcsDataInject . Fill ( EcsSystems systems ) {
129+ void IEcsDataInject . Fill ( IEcsSystems systems ) {
119130 Value = systems . GetShared < T > ( ) ;
120131 }
121132 }
@@ -144,7 +155,7 @@ public static implicit operator EcsWorldInject (string worldName) {
144155 return new EcsWorldInject { _worldName = worldName } ;
145156 }
146157
147- void IEcsDataInject . Fill ( EcsSystems systems ) {
158+ void IEcsDataInject . Fill ( IEcsSystems systems ) {
148159 Value = systems . GetWorld ( _worldName ) ;
149160 }
150161 }
0 commit comments