-
-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #119 from hadashiA/ku/partial-lifetimescope
Make LifetimeScope as patial
- Loading branch information
Showing
7 changed files
with
224 additions
and
176 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.AwakeScheduler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
using UnityEngine.SceneManagement; | ||
|
||
namespace VContainer.Unity | ||
{ | ||
public sealed class VContainerParentTypeReferenceNotFound : Exception | ||
{ | ||
public readonly Type ParentType; | ||
|
||
public VContainerParentTypeReferenceNotFound(Type parentType, string message) | ||
: base(message) | ||
{ | ||
ParentType = parentType; | ||
} | ||
} | ||
|
||
|
||
public partial class LifetimeScope | ||
{ | ||
static readonly List<(LifetimeScope, VContainerParentTypeReferenceNotFound)> WaitingList = | ||
new List<(LifetimeScope, VContainerParentTypeReferenceNotFound)>(); | ||
|
||
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] | ||
static void SubscribeSceneEvents() | ||
{ | ||
SceneManager.sceneLoaded -= ReleaseWaitingList; | ||
SceneManager.sceneLoaded += ReleaseWaitingList; | ||
} | ||
|
||
static void WaitForAwake(LifetimeScope lifetimeScope, VContainerParentTypeReferenceNotFound ex) | ||
{ | ||
WaitingList.Add((lifetimeScope, ex)); | ||
} | ||
|
||
static void CancelWaiting(LifetimeScope lifetimeScope) | ||
{ | ||
for (var i = WaitingList.Count - 1; i >= 0; i--) | ||
{ | ||
if (WaitingList[i].Item1 == lifetimeScope) | ||
{ | ||
WaitingList.RemoveAt(i); | ||
} | ||
} | ||
} | ||
|
||
static void ReleaseWaitingList(Scene scene, LoadSceneMode mode) | ||
{ | ||
for (var i = WaitingList.Count - 1; i >= 0; i--) | ||
{ | ||
var (waiting, ex) = WaitingList[i]; | ||
if (waiting.gameObject.scene == scene) | ||
{ | ||
WaitingList.RemoveAt(i); | ||
waiting.Awake(); // Re-throw if parent not found. | ||
} | ||
} | ||
} | ||
|
||
static void ReleaseWaitingListFrom(LifetimeScope awakedParent) | ||
{ | ||
if (WaitingList.Count < 0) return; | ||
|
||
var type = awakedParent.GetType(); | ||
|
||
for (var i = WaitingList.Count - 1; i >= 0; i--) | ||
{ | ||
var (waiting, ex) = WaitingList[i]; | ||
if (ex.ParentType == type) | ||
{ | ||
waiting.parentReference.Object = awakedParent; | ||
WaitingList.RemoveAt(i); | ||
waiting.Awake(); | ||
} | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.AwakeScheduler.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
122 changes: 122 additions & 0 deletions
122
VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.Dispatcher.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace VContainer.Unity | ||
{ | ||
public partial class LifetimeScope | ||
{ | ||
void DispatchEntryPoints() | ||
{ | ||
PlayerLoopHelper.Initialize(); | ||
|
||
EntryPointExceptionHandler exceptionHandler = null; | ||
try | ||
{ | ||
exceptionHandler = Container.Resolve<EntryPointExceptionHandler>(); | ||
} | ||
catch (VContainerException ex) when (ex.InvalidType == typeof(EntryPointExceptionHandler)) | ||
{ | ||
} | ||
|
||
var initializables = Container.Resolve<IReadOnlyList<IInitializable>>(); | ||
if (initializables.Count > 0) | ||
{ | ||
var loopItem = new InitializationLoopItem(initializables, exceptionHandler); | ||
disposable.Add(loopItem); | ||
PlayerLoopHelper.Dispatch(PlayerLoopTiming.Initialization, loopItem); | ||
} | ||
|
||
var postInitializables = Container.Resolve<IReadOnlyList<IPostInitializable>>(); | ||
if (postInitializables.Count > 0) | ||
{ | ||
var loopItem = new PostInitializationLoopItem(postInitializables, exceptionHandler); | ||
disposable.Add(loopItem); | ||
PlayerLoopHelper.Dispatch(PlayerLoopTiming.PostInitialization, loopItem); | ||
} | ||
|
||
var startables = Container.Resolve<IReadOnlyList<IStartable>>(); | ||
if (startables.Count > 0) | ||
{ | ||
var loopItem = new StartableLoopItem(startables, exceptionHandler); | ||
disposable.Add(loopItem); | ||
PlayerLoopHelper.Dispatch(PlayerLoopTiming.Startup, loopItem); | ||
} | ||
|
||
var postStartables = Container.Resolve<IReadOnlyList<IPostStartable>>(); | ||
if (postStartables.Count > 0) | ||
{ | ||
var loopItem = new PostStartableLoopItem(postStartables, exceptionHandler); | ||
disposable.Add(loopItem); | ||
PlayerLoopHelper.Dispatch(PlayerLoopTiming.PostStartup, loopItem); | ||
} | ||
|
||
var fixedTickables = Container.Resolve<IReadOnlyList<IFixedTickable>>(); | ||
if (fixedTickables.Count > 0) | ||
{ | ||
var loopItem = new FixedTickableLoopItem(fixedTickables, exceptionHandler); | ||
disposable.Add(loopItem); | ||
PlayerLoopHelper.Dispatch(PlayerLoopTiming.FixedUpdate, loopItem); | ||
} | ||
|
||
var postFixedTickables = Container.Resolve<IReadOnlyList<IPostFixedTickable>>(); | ||
if (postFixedTickables.Count > 0) | ||
{ | ||
var loopItem = new PostFixedTickableLoopItem(postFixedTickables, exceptionHandler); | ||
disposable.Add(loopItem); | ||
PlayerLoopHelper.Dispatch(PlayerLoopTiming.PostFixedUpdate, loopItem); | ||
} | ||
|
||
var tickables = Container.Resolve<IReadOnlyList<ITickable>>(); | ||
if (tickables.Count > 0) | ||
{ | ||
var loopItem = new TickableLoopItem(tickables, exceptionHandler); | ||
disposable.Add(loopItem); | ||
PlayerLoopHelper.Dispatch(PlayerLoopTiming.Update, loopItem); | ||
} | ||
|
||
var postTickables = Container.Resolve<IReadOnlyList<IPostTickable>>(); | ||
if (postTickables.Count > 0) | ||
{ | ||
var loopItem = new PostTickableLoopItem(postTickables, exceptionHandler); | ||
disposable.Add(loopItem); | ||
PlayerLoopHelper.Dispatch(PlayerLoopTiming.PostUpdate, loopItem); | ||
} | ||
|
||
var lateTickables = Container.Resolve<IReadOnlyList<ILateTickable>>(); | ||
if (lateTickables.Count > 0) | ||
{ | ||
var loopItem = new LateTickableLoopItem(lateTickables, exceptionHandler); | ||
disposable.Add(loopItem); | ||
PlayerLoopHelper.Dispatch(PlayerLoopTiming.LateUpdate, loopItem); | ||
} | ||
|
||
var postLateTickables = Container.Resolve<IReadOnlyList<IPostLateTickable>>(); | ||
if (postLateTickables.Count > 0) | ||
{ | ||
var loopItem = new PostLateTickableLoopItem(postLateTickables, exceptionHandler); | ||
disposable.Add(loopItem); | ||
PlayerLoopHelper.Dispatch(PlayerLoopTiming.PostLateUpdate, loopItem); | ||
} | ||
|
||
#if VCONTAINER_UNITASK_INTEGRATION | ||
var asyncStartables = Container.Resolve<IReadOnlyList<IAsyncStartable>>(); | ||
if (asyncStartables.Count > 0) | ||
{ | ||
var loopItem = new AsyncStartableLoopItem(asyncStartables, exceptionHandler); | ||
disposable.Add(loopItem); | ||
PlayerLoopHelper.Dispatch(PlayerLoopTiming.Startup, loopItem); | ||
} | ||
#endif | ||
|
||
#if VCONTAINER_ECS_INTEGRATION | ||
Container.Resolve<IEnumerable<ComponentSystemBase>>(); | ||
|
||
var worldHelpers = Container.Resolve<IEnumerable<WorldConfigurationHelper>>(); | ||
foreach (var x in worldHelpers) | ||
{ | ||
x.SortSystems(); | ||
} | ||
#endif | ||
} | ||
|
||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.Dispatcher.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
21c04ac
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: