Skip to content

Commit 1fd3018

Browse files
committed
Merge branch 'release/2022.7.22'
2 parents 2a47fbf + b64eec6 commit 1fd3018

File tree

3 files changed

+37
-26
lines changed

3 files changed

+37
-26
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class TestSystem : IEcsRunSystem {
5959
// Поле будет содержать ссылку на мир "events".
6060
readonly EcsWorldInject _eventsWorld = "events";
6161

62-
public void Run (EcsSystems systems) {
62+
public void Run (IEcsSystems systems) {
6363
// Все поля заполнены и могут быть использованы:
6464
// _defaultWorld.Value.xxx
6565
// _eventsWorld.Value.xxx
@@ -76,7 +76,7 @@ class TestSystem : IEcsRunSystem {
7676
// Поле будет содержать ссылку на пул из мира "events".
7777
readonly EcsPoolInject<C1> _c1EventsPool = "events";
7878

79-
public void Run (EcsSystems systems) {
79+
public void Run (IEcsSystems systems) {
8080
// Все поля заполнены и могут быть использованы:
8181
// _c1Pool.Value.xxx
8282
// _c1EventsPool.Value.xxx
@@ -99,7 +99,7 @@ class TestSystem : IEcsRunSystem {
9999
// Поле будет содержать ссылку на фильтр (с C1, но без C2) из мира "events".
100100
readonly EcsFilter<Inc<C1>, Exc<C2>> _eventsFilter11 = "events";
101101

102-
public void Run (EcsSystems systems) {
102+
public void Run (IEcsSystems systems) {
103103
// Все поля заполнены и могут быть использованы:
104104
// _filter1.Value.xxx
105105
// _filter2.Value.xxx
@@ -188,7 +188,7 @@ class TestSystem : IEcsRunSystem {
188188
// Поле будет содержать ссылку на GetShared() объект.
189189
readonly EcsSharedInject<Shared> _shared = default;
190190

191-
public void Run (EcsSystems systems) {
191+
public void Run (IEcsSystems systems) {
192192
// Все поля заполнены и могут быть использованы:
193193
// _shared.Value.xxx
194194
}
@@ -209,7 +209,7 @@ class TestSystem : IEcsRunSystem {
209209
// Поле будет содержать ссылку на объект совместимого типа, переданого в вызов EcsSystems.Inject(xxx).
210210
readonly EcsCustomInject<CustomData2> _custom2 = default;
211211

212-
public void Run (EcsSystems systems) {
212+
public void Run (IEcsSystems systems) {
213213
// Все поля заполнены и могут быть использованы:
214214
// _custom1.Value.xxx
215215
// _custom2.Value.xxx

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"displayName": "LeoECS Lite DI",
55
"description": "LeoECS Lite DI - Поддержка автоматической инъекции данных в поля ECS-систем.",
66
"unity": "2020.3",
7-
"version": "2022.3.22",
7+
"version": "2022.7.22",
88
"keywords": [
99
"leoecslite",
1010
"leoecs",

src/extensions.cs

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,39 @@
77
using System.Reflection;
88

99
namespace 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

Comments
 (0)