Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make LifetimeScope as patial #119

Merged
merged 2 commits into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

122 changes: 122 additions & 0 deletions VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.Dispatcher.cs
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
}

}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading