Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix null error in duplication registration #722

Merged
merged 3 commits into from
Nov 16, 2024
Merged
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
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
Loading