-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathuFrameKernel.cs
More file actions
299 lines (256 loc) · 8.75 KB
/
uFrameKernel.cs
File metadata and controls
299 lines (256 loc) · 8.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using uFrame.Attributes;
using uFrame.IOC;
using UniRx;
namespace uFrame.Kernel
{
public class uFrameKernel : MonoBehaviour
{
private static UFrameContainer _container;
private static IEventAggregator _eventAggregator;
private static bool _isKernelLoaded;
private List<ISystemService> _services;
private List<ISystemLoader> _systemLoaders;
public static IEnumerator InstantiateSceneAsyncAdditively(string sceneName)
{
var asyncOperation = Application.LoadLevelAdditiveAsync(sceneName);
float lastProgress = -1;
while (!asyncOperation.isDone)
{
if (lastProgress != asyncOperation.progress)
{
EventAggregator.Publish(new SceneLoaderEvent()
{
State = SceneState.Instantiating,
Name = sceneName,
Progress = asyncOperation.progress
});
lastProgress = asyncOperation.progress;
}
yield return new WaitForSeconds(0.1f);
}
}
public static bool IsKernelLoaded
{
get { return _isKernelLoaded; }
set { _isKernelLoaded = value; }
}
public static uFrameKernel Instance { get; set; }
public static IUFrameContainer Container
{
get
{
if (_container == null)
{
_container = new UFrameContainer();
_container.RegisterInstance<IUFrameContainer>(_container);
_container.RegisterInstance<IEventAggregator>(EventAggregator);
}
return _container;
}
}
public static IEventAggregator EventAggregator
{
get { return _eventAggregator ?? (_eventAggregator = new EventAggregator()); }
set { _eventAggregator = value; }
}
public List<ISystemLoader> SystemLoaders
{
get { return _systemLoaders ?? (_systemLoaders = new List<ISystemLoader>()); }
}
public List<ISystemService> Services
{
get { return _services ?? (_services = new List<ISystemService>()); }
}
private void Awake()
{
if (Instance != null)
{
throw new Exception("Loading Kernel twice is not a good practice!");
}
else
{
Instance = this;
//if (this.gameObject.GetComponent<MainThreadDispatcher>() == null)
// this.gameObject.AddComponent<MainThreadDispatcher>();
DontDestroyOnLoad(gameObject);
StartCoroutine(Startup());
}
}
private IEnumerator Startup()
{
var attachedSystemLoaders =
gameObject.GetComponentsInChildren(typeof (ISystemLoader)).OfType<ISystemLoader>();
foreach (var systemLoader in attachedSystemLoaders)
{
this.Publish(new SystemLoaderEvent() {State = SystemState.Loading, Loader = systemLoader});
systemLoader.Container = Container;
systemLoader.EventAggregator = EventAggregator;
systemLoader.Load();
SystemLoaders.Add(systemLoader);
this.Publish(new SystemLoaderEvent() {State = SystemState.Loaded, Loader = systemLoader});
}
var attachedServices = gameObject.GetComponentsInChildren(typeof (SystemServiceMonoBehavior))
.OfType<SystemServiceMonoBehavior>()
.Where(_ => _.enabled)
.ToArray();
foreach (var service in attachedServices)
{
Container.RegisterService(service);
Services.Add(service);
}
Container.InjectAll();
var allServices = Container.ResolveAll<ISystemService>().ToArray();
foreach (var service in allServices)
{
this.Publish(new ServiceLoaderEvent() {State = ServiceState.Loading, Service = service});
service.Setup();
var setupAsync = service.SetupAsync();
if(setupAsync!= null) yield return StartCoroutine(setupAsync);
this.Publish(new ServiceLoaderEvent() {State = ServiceState.Loaded, Service = service});
}
this.Publish(new SystemsLoadedEvent()
{
Kernel = this
});
_isKernelLoaded = true;
this.Publish(new KernelLoadedEvent()
{
Kernel = this
});
yield return new WaitForEndOfFrame(); //Ensure that everything is bound
yield return new WaitForEndOfFrame();
this.Publish(new GameReadyEvent());
}
public void OnDestroy()
{
_container = null;
IsKernelLoaded = false;
Services.Clear();
SystemLoaders.Clear();
EventAggregator = null;
Instance = null;
}
public void ResetKernel()
{
DestroyImmediate(Instance.gameObject);
_container = null;
IsKernelLoaded = false;
Services.Clear();
SystemLoaders.Clear();
EventAggregator = null;
Instance = null;
}
public static void DestroyKernel(string levelToLoad = null)
{
Instance.ResetKernel();
if (levelToLoad != null)
Application.LoadLevel(levelToLoad);
}
}
public class SystemsLoadedEvent
{
public uFrameKernel Kernel;
}
/// <summary>
///
/// </summary>
[uFrameEvent("Kernel Loaded")]
public class KernelLoadedEvent
{
public uFrameKernel Kernel;
}
/// <summary>
///
/// </summary>
[uFrameEvent("Game Ready")]
public class GameReadyEvent
{
}
/// <summary>
///
/// </summary>
public class LoadSceneCommand
{
public string SceneName { get; set; }
public ISceneSettings Settings { get; set; }
public bool RestrictToSingleScene { get; set; }
}
public class UnloadSceneCommand
{
public string SceneName { get; set; }
}
public class SystemLoaderEvent
{
public SystemState State { get; set; }
public ISystemLoader Loader { get; set; }
}
public class ServiceLoaderEvent
{
public ServiceState State { get; set; }
public ISystemService Service { get; set; }
}
public class SceneLoaderEvent
{
public SceneState State { get; set; }
public IScene SceneRoot { get; set; }
public float Progress { get; set; }
public string ProgressMessage { get; set; }
public string Name { get; set; }
}
public enum SceneState
{
Loading,
Update,
Loaded,
Unloading,
Unloaded,
Instantiating,
Instantiated,
Destructed
}
public enum ServiceState
{
Loading,
Loaded,
Unloaded,
}
public enum SystemState
{
Loading,
Loaded,
Unloaded,
}
public static class uFrameKernelExtensions
{
public static void RegisterService(this IUFrameContainer container, ISystemService service)
{
container.RegisterInstance<ISystemService>(service, service.GetType().Name);
//container.RegisterInstance(typeof(TService), service, false);
container.RegisterInstance(service.GetType(), service);
}
public static void RegisterService<TService>(this IUFrameContainer container, ISystemService service)
{
container.RegisterInstance<ISystemService>(service, service.GetType().Name);
container.RegisterInstance(typeof (TService), service);
}
public static void RegisterSceneLoader(this IUFrameContainer container, ISceneLoader sceneLoader)
{
container.RegisterInstance<ISceneLoader>(sceneLoader, sceneLoader.GetType().Name, false);
//container.RegisterInstance(typeof(TService), service, false);
container.RegisterInstance(sceneLoader.GetType(), sceneLoader, false);
}
public static void Publish(this uFrameKernel mvvmKernel, object evt)
{
uFrameKernel.EventAggregator.Publish(evt);
}
public static IObservable<T> OnEvent<T>(this uFrameKernel mvvmKernel)
{
return uFrameKernel.EventAggregator.GetEvent<T>();
}
}
}