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

UniTask integration #100

Merged
merged 10 commits into from
Jan 31, 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
5 changes: 3 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ jobs:
- name: Run Tests
shell: bash
run: xvfb-run --error-file=/dev/stdout --auto-servernum --server-args='-screen 0 1024x768x24' /opt/Unity/Editor/Unity -batchmode -logFile /dev/stdout -projectPath ./VContainer -runTests -testResults $GITHUB_WORKSPACE/VContainer/tmp/TestResults.xml -testPlatform playmode -buildTarget StandaloneLinux64 -scriptbackend=${{ matrix.scriptBackend }} -nographics -silent-crashes
- uses: actions/upload-artifact@v1
- uses: actions/upload-artifact@v2
if: always()
with:
name: Test results
name: TestResults.xml
path: ./VContainer/tmp/TestResults.xml
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ The extra fast DI (Dependency Injection) library running on Unity Game Engine.
- Flexible scoping
- Application can freely create nested Lifetime Scope with any async way for you like.
- Pre IL Code generation optimization mode
- UniTask Integration
- ECS Integration *beta*

## Performance
Expand All @@ -30,8 +31,6 @@ The extra fast DI (Dependency Injection) library running on Unity Game Engine.

### GC Alloc Result Example



![](./website/static/img/gc_alloc_profiler_result.png)

![](./website/static/img/screenshot_profiler_vcontainer.png)
Expand Down Expand Up @@ -232,6 +231,24 @@ using (LifetimeScope.Enqueue(builder =>
}
```

## UniTask

```csharp
public class FooController : IAsyncStartable
{
public async UniTask StartAsync(CancellationToken cancellation)
{
await LoadSomethingAsync(cancellation);
await ...
...
}
}
```

```csharp
builder.RegisterEntryPoint<FooController>(Lifetime.Scoped);
```

## Documentation

Visit [vcontainer.hadashikick.jp](https://vcontainer.hadashikick.jp) to view the full documentation.
Expand Down
3 changes: 3 additions & 0 deletions VContainer/Assets/VContainer/Runtime/Interfaces.meta

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

12 changes: 12 additions & 0 deletions VContainer/Assets/VContainer/Runtime/Interfaces/IAsyncStartable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#if VCONTAINER_UNITASK_INTEGRATION
using System.Threading;
using Cysharp.Threading.Tasks;

namespace VContainer.Unity
{
public interface IAsyncStartable
{
UniTask StartAsync(CancellationToken cancellation);
}
}
#endif

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace VContainer.Unity
{
public interface IFixedTickable
{
void FixedTick();
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace VContainer.Unity
{
public interface IInitializable
{
void Initialize();
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace VContainer.Unity
{
public interface ILateTickable
{
void LateTick();
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace VContainer.Unity
{
public interface IPostFixedTickable
{
void PostFixedTick();
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace VContainer.Unity
{
public interface IPostInitializable
{
void PostInitialize();
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace VContainer.Unity
{
public interface IPostLateTickable
{
void PostLateTick();
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace VContainer.Unity
{
public interface IPostStartable
{
void PostStart();
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace VContainer.Unity
{
public interface IPostTickable
{
void PostTick();
}
}

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

7 changes: 7 additions & 0 deletions VContainer/Assets/VContainer/Runtime/Interfaces/IStartable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace VContainer.Unity
{
public interface IStartable
{
void Start();
}
}

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

7 changes: 7 additions & 0 deletions VContainer/Assets/VContainer/Runtime/Interfaces/ITickable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace VContainer.Unity
{
public interface ITickable
{
void Tick();
}
}

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

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using UnityEngine;
using VContainer.Internal;

#if VCONTAINER_ECS_INTEGRATION
using Unity.Entities;
#endif
Expand All @@ -20,6 +21,9 @@ public EntryPointsBuilder(IContainerBuilder containerBuilder, Lifetime lifetime)

public RegistrationBuilder Add<T>()
=> containerBuilder.Register<T>(lifetime).AsImplementedInterfaces();

public void OnException(Action<Exception> exceptionHandler)
=> containerBuilder.RegisterEntryPointExceptionHandler(exceptionHandler);
}

public readonly struct ComponentsBuilder
Expand Down Expand Up @@ -57,6 +61,13 @@ public static void UseEntryPoints(
configuration(entryPoints);
}

public static void RegisterEntryPointExceptionHandler(
this IContainerBuilder builder,
Action<Exception> exceptionHandler)
{
builder.RegisterInstance(new EntryPointExceptionHandler(exceptionHandler));
}

public static void UseComponents(this IContainerBuilder builder, Action<ComponentsBuilder> configuration)
{
var components = new ComponentsBuilder(builder);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;

namespace VContainer.Unity
{
sealed class EntryPointExceptionHandler
{
readonly Action<Exception> handler;

public EntryPointExceptionHandler(Action<Exception> handler)
{
this.handler = handler;
}

public void Publish(Exception ex)
{
handler.Invoke(ex);
}
}
}

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

47 changes: 35 additions & 12 deletions VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ protected virtual void Configure(IContainerBuilder builder) { }

public void Dispose()
{
disposable.Dispose();
Container?.Dispose();
Container = null;

if (this != null)
Destroy(gameObject);
}
Expand Down Expand Up @@ -288,86 +292,105 @@ void ActivateEntryPoints()
{
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);
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);
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);
var loopItem = new StartableLoopItem(startables, exceptionHandler);
disposable.Add(loopItem);
PlayerLoopHelper.Dispatch(PlayerLoopTiming.Startup, loopItem);
}

var postStartable = Container.Resolve<IReadOnlyList<IPostStartable>>();
if (postInitializables.Count > 0)
var postStartables = Container.Resolve<IReadOnlyList<IPostStartable>>();
if (postStartables.Count > 0)
{
var loopItem = new PostStartableLoopItem(postStartable);
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);
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);
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);
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);
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);
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);
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>>();

Expand Down
Loading