Skip to content
Open
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: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,7 @@ sysinfo.txt
*.apk
#*.unitypackage

.DS_Store
.DS_Store

# Rider
/.idea/
46 changes: 46 additions & 0 deletions Runtime/Core/API/Pin.LoadScene.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// PinInject, Maxwell Keonwoo Kang <[email protected]>, 2022

using System;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace Cathei.PinInject
{
public static partial class Pin
{
internal static ContextConfiguration sceneContextConfig = null;

/// <summary>
/// Load and inject config a Scene.
/// </summary>
public static void LoadScene(string sceneName, LoadSceneMode loadSceneMode, ContextConfiguration config = null)
{
sceneContextConfig = config;
SceneManager.LoadScene(sceneName, loadSceneMode);
}

/// <summary>
/// Load and inject config a Scene.
/// </summary>
/// <remarks>
/// Note: Calling this method in parallel may result in improper injection of the Configuration.
/// </remarks>
public static AsyncOperation LoadSceneAsync(string sceneName, LoadSceneMode loadSceneMode, ContextConfiguration config = null)
{
sceneContextConfig = config;
return SceneManager.LoadSceneAsync(sceneName, loadSceneMode);
}

/// <summary>
/// Load and inject config a Scene.
/// </summary>
/// <remarks>
/// Note: Calling this method in parallel may result in improper injection of the Configuration.
/// </remarks>
public static T LoadSceneAsync<T>(Func<T> loadSceneFunc, ContextConfiguration config = null)
{
sceneContextConfig = config;
return loadSceneFunc();
}
}
}
11 changes: 11 additions & 0 deletions Runtime/Core/API/Pin.LoadScene.cs.meta

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

6 changes: 3 additions & 3 deletions Runtime/Core/API/Pin.SceneManagement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ internal static IDependencyContainer SetUpPersistent(PersistentCompositionRoot c
}
}

internal static void SetUpScene(SceneCompositionRoot compositionRoot)
internal static void SetUpScene(SceneCompositionRoot compositionRoot, ContextConfiguration config = null)
{
var compositionRootObject = compositionRoot.gameObject;
var scene = compositionRootObject.scene;
Expand All @@ -76,7 +76,7 @@ internal static void SetUpScene(SceneCompositionRoot compositionRoot)
var persistentContainer = SetUpPersistent(compositionRoot.parent);

// inject scene first
UnityStrategy.Inject(compositionRootObject, persistentContainer, null);
UnityStrategy.Inject(compositionRootObject, persistentContainer, config);

var sceneContainer = compositionRootObject.GetOrAddContainerComponent();

Expand Down Expand Up @@ -115,4 +115,4 @@ internal static DependencyContainer GetSceneContainer(Scene scene)
return container;
}
}
}
}
2 changes: 1 addition & 1 deletion Runtime/Core/Common/DefaultInjectionStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private void InjectProperties(ReflectionCache reflection, object obj, IDependenc
{
var value = container.Resolve(injectable.Type, injectable.IdGetter(obj));

if (value == null)
if (value == null && !injectable.Optional)
throw new InjectionException($"Type {injectable.Type} on {obj} cannot be resolved");

injectable.Setter(obj, value);
Expand Down
4 changes: 3 additions & 1 deletion Runtime/Core/Reflection/InjectAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ public class InjectAttribute : PreserveAttribute
{
public readonly string Name;
public readonly bool FromMember;
public readonly bool Optional = false;

public InjectAttribute()
public InjectAttribute(bool optional = false)
{
Name = null;
Optional = optional;
}

public InjectAttribute(string name, bool fromMember = false)
Expand Down
15 changes: 15 additions & 0 deletions Runtime/Core/Reflection/InjectOptionalAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// PinInject, Maxwell Keonwoo Kang <[email protected]>, 2022

using System;
using Cathei.PinInject;

namespace Core.Reflection
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class InjectOptionalAttribute : InjectAttribute
{
public InjectOptionalAttribute() : base(optional: true)
{
}
}
}
3 changes: 3 additions & 0 deletions Runtime/Core/Reflection/InjectOptionalAttribute.cs.meta

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

8 changes: 5 additions & 3 deletions Runtime/Core/Reflection/ReflectionCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private ReflectionCache(Type type)

_injectables ??= new List<InjectableProperty>();
_injectables.Add(new InjectableProperty(
prop.PropertyType, IdGetter(type, injectAttr), prop.SetValue));
prop.PropertyType, IdGetter(type, injectAttr), prop.SetValue, injectAttr.Optional));
}

if (resolveAttr != null)
Expand All @@ -85,7 +85,7 @@ private ReflectionCache(Type type)
{
_injectables ??= new List<InjectableProperty>();
_injectables.Add(new InjectableProperty(
field.FieldType, IdGetter(type, injectAttr), field.SetValue));
field.FieldType, IdGetter(type, injectAttr), field.SetValue, injectAttr.Optional));
}

if (resolveAttr != null)
Expand Down Expand Up @@ -127,12 +127,14 @@ public struct InjectableProperty
public readonly Type Type;
public readonly Func<object, string> IdGetter;
public readonly Action<object, object> Setter;
public readonly bool Optional;

public InjectableProperty(Type type, Func<object, string> idGetter, Action<object, object> setter)
public InjectableProperty(Type type, Func<object, string> idGetter, Action<object, object> setter, bool optional)
{
Type = type;
IdGetter = idGetter;
Setter = setter;
Optional = optional;
}
}

Expand Down
2 changes: 1 addition & 1 deletion Runtime/Core/Unity/SceneCompositionRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class SceneCompositionRoot : MonoBehaviour, ICompositionRoot

private void Awake()
{
Pin.SetUpScene(this);
Pin.SetUpScene(this, Pin.sceneContextConfig);
}

private void OnDestroy()
Expand Down