Skip to content

Commit

Permalink
Added ability to pass in a Func<> getter method to choose a prefab dy…
Browse files Browse the repository at this point in the history
…namically when using ByNewPrefabMethod or ByNewPrefabInstaller
  • Loading branch information
svermeulen committed Nov 10, 2019
1 parent 17cdca4 commit e08a8be
Show file tree
Hide file tree
Showing 26 changed files with 315 additions and 18 deletions.
20 changes: 20 additions & 0 deletions README.md
Expand Up @@ -863,6 +863,16 @@ Where:
}
```

Note that instead of passing in a prefab directly, you can also pass in a getter method. For example:

```csharp
Container.Bind<Foo>().FromSubContainerResolve().ByNewPrefabMethod(ChooseFooPrefab, InstallFoo);

UnityEngine.Object ChooseFooPrefab(InjectContext context) {
return FooPrefabs[Random.Range(0, FooPrefabs.Length)];
}
```

1. **ByNewPrefabInstaller** - Initialize subcontainer by instantiating a new prefab. Same as ByNewPrefabMethod, except it initializes the dynamically created GameObjectContext with the given installer rather than a method.

```csharp
Expand All @@ -877,6 +887,16 @@ Where:
}
```

Note that instead of passing in a prefab directly, you can also pass in a getter method. For example:

```csharp
Container.Bind<Foo>().FromSubContainerResolve().ByNewPrefabInstaller<FooInstaller>(ChooseFooPrefab);

UnityEngine.Object ChooseFooPrefab(InjectContext context) {
return FooPrefabs[Random.Range(0, FooPrefabs.Length)];
}
```

1. **ByNewPrefabResourceMethod** - Initialize subcontainer instantiating a new prefab obtained via `Resources.Load`. Note that unlike `ByNewPrefabResource`, this bind method does not require that there be a GameObjectContext attached to the prefab. In this case the GameObjectContext is added dynamically and then run with the given installer method.

```csharp
Expand Down
Expand Up @@ -32,6 +32,20 @@ public IEnumerator TestInstaller()
yield break;
}

[UnityTest]
public IEnumerator TestInstallerGetter()
{
PreInstall();

Container.Bind<Qux>().FromSubContainerResolve()
.ByNewPrefabInstaller<FooInstaller>(_ => FooPrefab).AsCached();

PostInstall();

Assert.IsEqual(Container.Resolve<Qux>().Data, "asdf");
yield break;
}

[UnityTest]
public IEnumerator TestMethod()
{
Expand All @@ -46,6 +60,20 @@ public IEnumerator TestMethod()
yield break;
}

[UnityTest]
public IEnumerator TestMethodGetter()
{
PreInstall();

Container.Bind<Qux>().FromSubContainerResolve()
.ByNewPrefabMethod((context) => FooPrefab, InstallFoo).AsCached();

PostInstall();

Assert.IsEqual(Container.Resolve<Qux>().Data, "asdf");
yield break;
}

[UnityTest]
public IEnumerator TestResourceInstaller()
{
Expand Down
Expand Up @@ -41,6 +41,22 @@ public NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder ByNewGameObject
return new NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder(BindInfo, gameObjectInfo);
}

public NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder ByNewPrefabMethod(
Func<InjectContext, UnityEngine.Object> prefabGetter, Action<DiContainer> installerMethod)
{
var gameObjectInfo = new GameObjectCreationParameters();

ProviderFunc =
(container) => new SubContainerDependencyProvider(
ContractType, SubIdentifier,
new SubContainerCreatorByNewPrefabMethod(
container,
new PrefabProviderCustom(prefabGetter),
gameObjectInfo, installerMethod), false);

return new NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder(BindInfo, gameObjectInfo);
}

public NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder ByNewPrefabMethod(
UnityEngine.Object prefab, Action<DiContainer> installerMethod)
{
Expand Down
Expand Up @@ -41,6 +41,22 @@ public ScopeConcreteIdArgConditionCopyNonLazyBinder ByMethod(Action<DiContainer,
return new NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder(BindInfo, gameObjectInfo);
}

public NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder ByNewPrefabMethod(
Func<InjectContext, UnityEngine.Object> prefabGetter, Action<DiContainer, TParam1> installerMethod)
{
var gameObjectInfo = new GameObjectCreationParameters();

ProviderFunc =
(container) => new SubContainerDependencyProvider(
ContractType, SubIdentifier,
new SubContainerCreatorByNewPrefabMethod<TParam1>(
container,
new PrefabProviderCustom(prefabGetter),
gameObjectInfo, installerMethod), false);

return new NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder(BindInfo, gameObjectInfo);
}

public NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder ByNewPrefabMethod(
UnityEngine.Object prefab, Action<DiContainer, TParam1> installerMethod)
{
Expand Down
Expand Up @@ -48,6 +48,25 @@ public class FactorySubContainerBinder<TParam1, TParam2, TParam3, TParam4, TPara
return new NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder(BindInfo, gameObjectInfo);
}

public NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder ByNewPrefabMethod(
Func<InjectContext, UnityEngine.Object> prefabGetter,
#if !NET_4_6
ModestTree.Util.
#endif
Action<DiContainer, TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7, TParam8, TParam9, TParam10> installerMethod)
{
var gameObjectInfo = new GameObjectCreationParameters();

ProviderFunc =
(container) => new SubContainerDependencyProvider(
ContractType, SubIdentifier,
new SubContainerCreatorByNewPrefabMethod<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7, TParam8, TParam9, TParam10>( container,
new PrefabProviderCustom(prefabGetter),
gameObjectInfo, installerMethod), false);

return new NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder(BindInfo, gameObjectInfo);
}

public NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder ByNewPrefabMethod(
UnityEngine.Object prefab,
#if !NET_4_6
Expand Down
Expand Up @@ -40,6 +40,22 @@ public ScopeConcreteIdArgConditionCopyNonLazyBinder ByMethod(Action<DiContainer,
return new NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder(BindInfo, gameObjectInfo);
}

public NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder ByNewPrefabMethod(
Func<InjectContext, UnityEngine.Object> prefabGetter, Action<DiContainer, TParam1, TParam2> installerMethod)
{
var gameObjectInfo = new GameObjectCreationParameters();

ProviderFunc =
(container) => new SubContainerDependencyProvider(
ContractType, SubIdentifier,
new SubContainerCreatorByNewPrefabMethod<TParam1, TParam2>(
container,
new PrefabProviderCustom(prefabGetter),
gameObjectInfo, installerMethod), false);

return new NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder(BindInfo, gameObjectInfo);
}

public NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder ByNewPrefabMethod(
UnityEngine.Object prefab, Action<DiContainer, TParam1, TParam2> installerMethod)
{
Expand Down
Expand Up @@ -40,6 +40,22 @@ public ScopeConcreteIdArgConditionCopyNonLazyBinder ByMethod(Action<DiContainer,
return new NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder(BindInfo, gameObjectInfo);
}

public NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder ByNewPrefabMethod(
Func<InjectContext, UnityEngine.Object> prefabGetter, Action<DiContainer, TParam1, TParam2, TParam3> installerMethod)
{
var gameObjectInfo = new GameObjectCreationParameters();

ProviderFunc =
(container) => new SubContainerDependencyProvider(
ContractType, SubIdentifier,
new SubContainerCreatorByNewPrefabMethod<TParam1, TParam2, TParam3>(
container,
new PrefabProviderCustom(prefabGetter),
gameObjectInfo, installerMethod), false);

return new NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder(BindInfo, gameObjectInfo);
}

public NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder ByNewPrefabMethod(
UnityEngine.Object prefab, Action<DiContainer, TParam1, TParam2, TParam3> installerMethod)
{
Expand Down
Expand Up @@ -47,6 +47,26 @@ public class FactorySubContainerBinder<TParam1, TParam2, TParam3, TParam4, TCont
return new NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder(BindInfo, gameObjectInfo);
}

public NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder ByNewPrefabMethod(
Func<InjectContext, UnityEngine.Object> prefabGetter,
#if !NET_4_6
ModestTree.Util.
#endif
Action<DiContainer, TParam1, TParam2, TParam3, TParam4> installerMethod)
{
var gameObjectInfo = new GameObjectCreationParameters();

ProviderFunc =
(container) => new SubContainerDependencyProvider(
ContractType, SubIdentifier,
new SubContainerCreatorByNewPrefabMethod<TParam1, TParam2, TParam3, TParam4>(
container,
new PrefabProviderCustom(prefabGetter),
gameObjectInfo, installerMethod), false);

return new NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder(BindInfo, gameObjectInfo);
}

public NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder ByNewPrefabMethod(
UnityEngine.Object prefab,
#if !NET_4_6
Expand Down
Expand Up @@ -47,6 +47,26 @@ public class FactorySubContainerBinder<TParam1, TParam2, TParam3, TParam4, TPara
return new NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder(BindInfo, gameObjectInfo);
}

public NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder ByNewPrefabMethod(
Func<InjectContext, UnityEngine.Object> prefabGetter,
#if !NET_4_6
ModestTree.Util.
#endif
Action<DiContainer, TParam1, TParam2, TParam3, TParam4, TParam5> installerMethod)
{
var gameObjectInfo = new GameObjectCreationParameters();

ProviderFunc =
(container) => new SubContainerDependencyProvider(
ContractType, SubIdentifier,
new SubContainerCreatorByNewPrefabMethod<TParam1, TParam2, TParam3, TParam4, TParam5>(
container,
new PrefabProviderCustom(prefabGetter),
gameObjectInfo, installerMethod), false);

return new NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder(BindInfo, gameObjectInfo);
}

public NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder ByNewPrefabMethod(
UnityEngine.Object prefab,
#if !NET_4_6
Expand Down
Expand Up @@ -47,6 +47,26 @@ public class FactorySubContainerBinder<TParam1, TParam2, TParam3, TParam4, TPara
return new NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder(BindInfo, gameObjectInfo);
}

public NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder ByNewPrefabMethod(
Func<InjectContext, UnityEngine.Object> prefabGetter,
#if !NET_4_6
ModestTree.Util.
#endif
Action<DiContainer, TParam1, TParam2, TParam3, TParam4, TParam5, TParam6> installerMethod)
{
var gameObjectInfo = new GameObjectCreationParameters();

ProviderFunc =
(container) => new SubContainerDependencyProvider(
ContractType, SubIdentifier,
new SubContainerCreatorByNewPrefabMethod<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6>(
container,
new PrefabProviderCustom(prefabGetter),
gameObjectInfo, installerMethod), false);

return new NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder(BindInfo, gameObjectInfo);
}

public NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder ByNewPrefabMethod(
UnityEngine.Object prefab,
#if !NET_4_6
Expand Down
Expand Up @@ -96,6 +96,32 @@ public NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder ByNewGameObject
return new NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder(BindInfo, gameObjectInfo);
}

public NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder ByNewPrefabInstaller<TInstaller>(
Func<InjectContext, UnityEngine.Object> prefabGetter)
where TInstaller : InstallerBase
{
return ByNewPrefabInstaller(prefabGetter, typeof(TInstaller));
}

public NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder ByNewPrefabInstaller(
Func<InjectContext, UnityEngine.Object> prefabGetter, Type installerType)
{
Assert.That(installerType.DerivesFrom<InstallerBase>(),
"Invalid installer type given during bind command. Expected type '{0}' to derive from 'Installer<>'", installerType);

var gameObjectInfo = new GameObjectCreationParameters();

ProviderFunc =
(container) => new SubContainerDependencyProvider(
ContractType, SubIdentifier,
new SubContainerCreatorByNewPrefabInstaller(
container,
new PrefabProviderCustom(prefabGetter),
gameObjectInfo, installerType, BindInfo.Arguments), false);

return new NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder(BindInfo, gameObjectInfo);
}

public NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder ByNewPrefabInstaller<TInstaller>(
UnityEngine.Object prefab)
where TInstaller : InstallerBase
Expand Down
Expand Up @@ -125,6 +125,21 @@ public ScopeConcreteIdArgConditionCopyNonLazyBinder ByInstance(DiContainer subCo
return new NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder(_bindInfo, gameObjectInfo);
}

public NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder ByNewPrefabMethod(
Func<InjectContext, UnityEngine.Object> prefabGetter, Action<DiContainer> installerMethod)
{
var gameObjectInfo = new GameObjectCreationParameters();

SubFinalizer = new SubContainerPrefabBindingFinalizer(
_bindInfo, _subIdentifier, _resolveAll,
(container) => new SubContainerCreatorByNewPrefabMethod(
container,
new PrefabProviderCustom(prefabGetter),
gameObjectInfo, installerMethod));

return new NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder(_bindInfo, gameObjectInfo);
}

public NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder ByNewPrefabMethod(
UnityEngine.Object prefab, Action<DiContainer> installerMethod)
{
Expand Down Expand Up @@ -163,6 +178,31 @@ public NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder ByNewGameObject
return new NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder(_bindInfo, gameObjectInfo);
}

public NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder ByNewPrefabInstaller<TInstaller>(
Func<InjectContext, UnityEngine.Object> prefabGetter)
where TInstaller : InstallerBase
{
return ByNewPrefabInstaller(prefabGetter, typeof(TInstaller));
}

public NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder ByNewPrefabInstaller(
Func<InjectContext, UnityEngine.Object> prefabGetter, Type installerType)
{
Assert.That(installerType.DerivesFrom<InstallerBase>(),
"Invalid installer type given during bind command. Expected type '{0}' to derive from 'Installer<>'", installerType);

var gameObjectInfo = new GameObjectCreationParameters();

SubFinalizer = new SubContainerPrefabBindingFinalizer(
_bindInfo, _subIdentifier, _resolveAll,
(container) => new SubContainerCreatorByNewPrefabInstaller(
container,
new PrefabProviderCustom(prefabGetter),
gameObjectInfo, installerType, _bindInfo.Arguments));

return new NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder(_bindInfo, gameObjectInfo);
}

public NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder ByNewPrefabInstaller<TInstaller>(
UnityEngine.Object prefab)
where TInstaller : InstallerBase
Expand Down
Expand Up @@ -54,7 +54,7 @@ public Type GetInstanceType(InjectContext context)
var match = gameObject.GetComponentInChildren(_componentType, true);

Assert.IsNotNull(match, "Could not find component with type '{0}' on prefab '{1}'",
_componentType, _prefabInstantiator.GetPrefab().name);
_componentType, _prefabInstantiator.GetPrefab(context).name);

buffer.Add(match);
return;
Expand All @@ -64,7 +64,7 @@ public Type GetInstanceType(InjectContext context)

Assert.That(allComponents.Length >= 1,
"Expected to find at least one component with type '{0}' on prefab '{1}'",
_componentType, _prefabInstantiator.GetPrefab().name);
_componentType, _prefabInstantiator.GetPrefab(context).name);

buffer.AllocFreeAddRange(allComponents);
}
Expand Down
Expand Up @@ -25,7 +25,7 @@ GameObjectCreationParameters GameObjectCreationParameters

GameObject Instantiate(InjectContext context, List<TypeValuePair> args, out Action injectAction);

UnityEngine.Object GetPrefab();
UnityEngine.Object GetPrefab(InjectContext context);
}
}

Expand Down

0 comments on commit e08a8be

Please sign in to comment.