Skip to content

Commit

Permalink
Merge pull request #722 from hadashiA/ku/fix-null
Browse files Browse the repository at this point in the history
Fix null error in duplication registration
  • Loading branch information
hadashiA authored Nov 16, 2024
2 parents 32202fd + 9285e17 commit d7c2ed6
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 156 deletions.
2 changes: 1 addition & 1 deletion VContainer/Assets/VContainer/Runtime/Registry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static Registry Build(Registration[] registrations)

static void AddToBuildBuffer(IDictionary<Type, Registration> buf, Type service, Registration registration)
{
if (buf.TryGetValue(service, out var exists))
if (buf.TryGetValue(service, out var exists) && exists != null)
{
CollectionInstanceProvider collection;
if (buf.TryGetValue(RuntimeTypeCache.EnumerableTypeOf(service), out var found) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,37 +27,37 @@ public object SpawnInstance(IObjectResolver resolver)
{
var prefab = prefabFinder(resolver);
var parent = destination.GetParent(resolver);

var wasActive = prefab.gameObject.activeSelf;
using var dirtyScope = new ObjectResolverUnityExtensions.PrefabDirtyScope(prefab.gameObject);

if (wasActive)
using (new ObjectResolverUnityExtensions.PrefabDirtyScope(prefab.gameObject))
{
prefab.gameObject.SetActive(false);
}

var component = parent != null
? UnityEngine.Object.Instantiate(prefab, parent)
: UnityEngine.Object.Instantiate(prefab);
if (wasActive)
{
prefab.gameObject.SetActive(false);
}

if (VContainerSettings.Instance != null && VContainerSettings.Instance.RemoveClonePostfix)
component.name = prefab.name;
var component = parent != null
? UnityEngine.Object.Instantiate(prefab, parent)
: UnityEngine.Object.Instantiate(prefab);

try
{
injector.Inject(component, resolver, customParameters);
destination.ApplyDontDestroyOnLoadIfNeeded(component);
}
finally
{
if (wasActive)
if (VContainerSettings.Instance != null && VContainerSettings.Instance.RemoveClonePostfix)
component.name = prefab.name;

try
{
prefab.gameObject.SetActive(true);
component.gameObject.SetActive(true);
injector.Inject(component, resolver, customParameters);
destination.ApplyDontDestroyOnLoadIfNeeded(component);
}
finally
{
if (wasActive)
{
prefab.gameObject.SetActive(true);
component.gameObject.SetActive(true);
}
}
return component;
}

return component;
}
}
}
35 changes: 18 additions & 17 deletions VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,24 +263,25 @@ public TScope CreateChildFromPrefab<TScope>(TScope prefab, IInstaller installer
where TScope : LifetimeScope
{
var wasActive = prefab.gameObject.activeSelf;
using var dirtyScope = new ObjectResolverUnityExtensions.PrefabDirtyScope(prefab.gameObject);

if (wasActive)
{
prefab.gameObject.SetActive(false);
}
var child = Instantiate(prefab, transform, false);
if (installer != null)
{
child.localExtraInstallers.Add(installer);
}
child.parentReference.Object = this;
if (wasActive)
using (new ObjectResolverUnityExtensions.PrefabDirtyScope(prefab.gameObject))
{
prefab.gameObject.SetActive(true);
child.gameObject.SetActive(true);
if (wasActive)
{
prefab.gameObject.SetActive(false);
}
var child = Instantiate(prefab, transform, false);
if (installer != null)
{
child.localExtraInstallers.Add(installer);
}
child.parentReference.Object = this;
if (wasActive)
{
prefab.gameObject.SetActive(true);
child.gameObject.SetActive(true);
}
return child;
}
return child;
}

public TScope CreateChildFromPrefab<TScope>(TScope prefab, Action<IContainerBuilder> installation)
Expand Down Expand Up @@ -317,7 +318,7 @@ LifetimeScope GetRuntimeParent()

if (parentReference.Object != null)
return parentReference.Object;

// Find via implementation
var implParent = FindParent();
if (implParent != null)
Expand Down
Loading

0 comments on commit d7c2ed6

Please sign in to comment.