Skip to content

Commit

Permalink
Merge pull request #67 from Delt06/dev
Browse files Browse the repository at this point in the history
v2.5.3
  • Loading branch information
Delt06 committed Nov 11, 2021
2 parents b889b91 + 8dc8c27 commit cd24709
Show file tree
Hide file tree
Showing 22 changed files with 537 additions and 4 deletions.
3 changes: 3 additions & 0 deletions DIFramework.Lifecycle.csproj.DotSettings
@@ -0,0 +1,3 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=packages_005Ccom_002Edeltation_002Edi_002Dframework_005Cassets/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=packages_005Ccom_002Edeltation_002Edi_002Dframework_005Cassets_005Cdeltation_005Cdiframework_005Cruntime/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
@@ -0,0 +1,49 @@
using System;
using JetBrains.Annotations;
using UnityEngine.Assertions;
using Object = UnityEngine.Object;

namespace DELTation.DIFramework
{
/// <summary>
/// Utility methods for registering dependencies in ContainerBuilders.
/// </summary>
public static class ContainerBuilderExtensions
{
/// <summary>
/// Register the given dependency only if it is not null (it checks for Unity null too).
/// </summary>
/// <param name="containerBuilder">Container builder to register dependency in.</param>
/// <param name="obj">Registered object.</param>
/// <returns>The same container builder.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="containerBuilder" /> is null.</exception>
[NotNull]
public static ContainerBuilder RegisterIfNotNull([NotNull] this ContainerBuilder containerBuilder,
[CanBeNull] object obj)
{
if (containerBuilder == null) throw new ArgumentNullException(nameof(containerBuilder));
if (IsNullOrUnityNull(obj)) return containerBuilder;

Assert.IsNotNull(obj);
return containerBuilder.Register(obj);
}

private static bool IsNullOrUnityNull([CanBeNull] object obj) =>
obj is Object unityObj ? unityObj == null : obj == null;

/// <summary>
/// Try to resolve a dependency globally. If resolved, register it.
/// </summary>
/// <param name="containerBuilder">Container builder to register dependency in.</param>
/// <typeparam name="T">Type of dependency to try to resolve.</typeparam>
/// <returns>The same container builder.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="containerBuilder" /> is null.</exception>
[NotNull]
public static ContainerBuilder TryResolveGloballyAndRegister<T>(
[NotNull] this ContainerBuilder containerBuilder) where T : class
{
if (containerBuilder == null) throw new ArgumentNullException(nameof(containerBuilder));
return Di.TryResolveGlobally(out T service) ? containerBuilder.Register(service) : containerBuilder;
}
}
}

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

@@ -0,0 +1,101 @@
using System;
using System.Collections.Generic;
using DELTation.DIFramework.Containers;
using DELTation.DIFramework.Lifecycle;
using UnityEngine;
using UnityEngine.Assertions;

namespace DELTation.DIFramework
{
/// <summary>
/// Pulls all registered objects from a container and calls lifecycle events on them
/// (<see cref="DIFramework.Lifecycle.IStartable" />, <see cref="DIFramework.Lifecycle.IUpdatable" />,
/// <see cref="DIFramework.Lifecycle.IDestroyable" />).
/// </summary>
// ReSharper disable once ClassWithVirtualMembersNeverInherited.Global
public class ContainerLifecycle : MonoBehaviour
{
[SerializeField] private DependencyContainerBase _container;

private readonly List<IDestroyable> _destroyables = new List<IDestroyable>();
private readonly List<IStartable> _startables = new List<IStartable>();
private readonly List<IUpdatable> _updatables = new List<IUpdatable>();

public DependencyContainerBase Container
{
get => _container;
set
{
if (value == null) throw new ArgumentNullException(nameof(value), "Container cannot be null.");
_container = value;
}
}

protected virtual void Start()
{
AssetThatContainerIsAssigned();
PullObjectsFromContainer();
InvokeStartables();
}

protected virtual void Update()
{
InvokeUpdatables();
}

protected virtual void OnDestroy()
{
InvokeDestroyables();
}

private void AssetThatContainerIsAssigned()
{
Assert.IsNotNull(_container, $"Container has to be assigned ({gameObject.name}).");
}

private void InvokeStartables()
{
// ReSharper disable once ForCanBeConvertedToForeach
for (var index = 0; index < _startables.Count; index++)
{
_startables[index].OnStart();
}
}

private void InvokeUpdatables()
{
// ReSharper disable once ForCanBeConvertedToForeach
for (var index = 0; index < _updatables.Count; index++)
{
_updatables[index].OnUpdate();
}
}

private void InvokeDestroyables()
{
// ReSharper disable once ForCanBeConvertedToForeach
for (var index = 0; index < _destroyables.Count; index++)
{
_destroyables[index].OnDestroy();
}
}

private void PullObjectsFromContainer()
{
var allObjects = new List<object>();
_container.GetAllRegisteredObjects(allObjects);

for (var index = 0; index < allObjects.Count; index++)
{
var obj = allObjects[index];
// ReSharper disable once ConvertIfStatementToSwitchStatement
if (obj is IStartable startable)
_startables.Add(startable);
if (obj is IUpdatable updatable)
_updatables.Add(updatable);
if (obj is IDestroyable destroyable)
_destroyables.Add(destroyable);
}
}
}
}

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

@@ -1,6 +1,8 @@
{
"name": "DIFramework",
"references": [],
"references": [
"GUID:7661adf0dcc98034093743fe3a566d1a"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
Expand Down

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

@@ -0,0 +1,13 @@
{
"name": "DIFramework.Lifecycle",
"references": [],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

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

@@ -0,0 +1,12 @@
namespace DELTation.DIFramework.Lifecycle
{
public interface IDestroyable
{
/// <summary>
/// Executed by
/// <see cref="DELTation.DIFramework.ContainerLifecycle" />
/// at Unity's OnDestroy callback.
/// </summary>
void OnDestroy();
}
}

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

@@ -0,0 +1,12 @@
namespace DELTation.DIFramework.Lifecycle
{
public interface IStartable
{
/// <summary>
/// Executed by
/// <see cref="DELTation.DIFramework.ContainerLifecycle" />
/// at Unity's Start callback.
/// </summary>
void OnStart();
}
}

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

@@ -0,0 +1,12 @@
namespace DELTation.DIFramework.Lifecycle
{
public interface IUpdatable
{
/// <summary>
/// Executed by
/// <see cref="DELTation.DIFramework.ContainerLifecycle" />
/// at Unity's Update callback.
/// </summary>
void OnUpdate();
}
}

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

0 comments on commit cd24709

Please sign in to comment.