Skip to content

Commit 774b237

Browse files
committed
API for processable installers & minor refactor changes
1 parent e5118fd commit 774b237

12 files changed

Lines changed: 230 additions & 36 deletions

Assets/Package/Plugins/Zenject/Runtime/Signals/Zenject-Signals.csproj.meta

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Collections.Generic;
2+
using UnityEngine;
3+
using Zenject;
4+
5+
namespace BroWar.Injection
6+
{
7+
using BroWar.Common;
8+
9+
[AddComponentMenu("BroWar/Injection/Context Installer")]
10+
public class ContextInstaller : MonoInstaller<ContextInstaller>
11+
{
12+
[SerializeReference, ReferencePicker(TypeGrouping = TypeGrouping.ByFlatName), ReorderableList(elementLabel: "Installer")]
13+
private ISubInstaller[] installers;
14+
15+
protected virtual void HandleInstallers()
16+
{
17+
var count = installers?.Length ?? 0;
18+
for (var i = 0; i < count; i++)
19+
{
20+
var installer = installers[i];
21+
if (installer == null)
22+
{
23+
LogHandler.Log($"[Injection][{name}] Installer at '{i}' is invalid.", LogType.Warning);
24+
continue;
25+
}
26+
27+
installer.Install(Container);
28+
}
29+
}
30+
31+
public override void InstallBindings()
32+
{
33+
HandleInstallers();
34+
}
35+
36+
public IReadOnlyList<ISubInstaller> Installers => installers;
37+
}
38+
}
File renamed without changes.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using UnityEngine;
2+
using Zenject;
3+
4+
namespace BroWar.Injection
5+
{
6+
using BroWar.Common;
7+
8+
/// <summary>
9+
/// Installer that can be exposed to a parent context, e.g. from <see cref="GameObjectContext"/> to <see cref="SceneContext"/>.
10+
/// </summary>
11+
public abstract class ExposableInstaller : ISubInstaller
12+
{
13+
[SerializeField, Tooltip("If true parent container will be used for binding." +
14+
" This is useful when we want to expose references outside our context.")]
15+
private bool bindToParent;
16+
17+
private DiContainer GetParentContainer(DiContainer target)
18+
{
19+
var parents = target.ParentContainers;
20+
if (parents == null || parents.Length == 0)
21+
{
22+
return null;
23+
}
24+
25+
return parents[0];
26+
}
27+
28+
protected abstract void OnInstall(DiContainer container);
29+
30+
public void Install(DiContainer container)
31+
{
32+
TargetContainer = container;
33+
ParentContainer = GetParentContainer(container);
34+
if (BindToParent)
35+
{
36+
if (ParentContainer != null)
37+
{
38+
container = ParentContainer;
39+
}
40+
else
41+
{
42+
LogHandler.Log("[Game] Cannot find parent container.", LogType.Warning);
43+
}
44+
}
45+
46+
OnInstall(container);
47+
}
48+
49+
protected bool BindToParent
50+
{
51+
get => bindToParent;
52+
set => bindToParent = value;
53+
}
54+
55+
/// <summary>
56+
/// Default <see cref="DiContainer"/> associated to "our" context.
57+
/// </summary>
58+
protected DiContainer TargetContainer
59+
{
60+
get; private set;
61+
}
62+
63+
/// <summary>
64+
/// Parent <see cref="DiContainer"/> associated with higher context
65+
/// (e.g. <see cref="SceneContext"/> if installer is placed within <see cref="GameObjectContext"/>).
66+
/// </summary>
67+
protected DiContainer ParentContainer
68+
{
69+
get; private set;
70+
}
71+
}
72+
}

Assets/Package/Runtime/IMinorInstaller.cs.meta renamed to Assets/Package/Runtime/ExposableInstaller.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Package/Runtime/GeneralInstaller.cs

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace BroWar.Injection
44
{
5-
public interface IMinorInstaller
5+
public interface ISubInstaller
66
{
77
void Install(DiContainer container);
88
}

Assets/Package/Runtime/ISubInstaller.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace BroWar.Injection
2+
{
3+
public interface ISubProcessor<T>
4+
{
5+
/// <summary>
6+
/// Called right before the installation process.
7+
/// </summary>
8+
/// <param name="data">Data used to initialize the context installer.</param>
9+
void OnBeginInstallation(T data);
10+
/// <summary>
11+
/// Called right after the installation process.
12+
/// </summary>
13+
/// <param name="data">Data used to initialize the context installer.</param>
14+
void OnCloseInstallation(T data);
15+
}
16+
}

Assets/Package/Runtime/ISubProcessor.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)