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 Diagnostic window. #655

Merged
merged 1 commit into from
Apr 12, 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
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ namespace VContainer.Diagnostics
{
public static class DiagnositcsContext
{
static readonly Dictionary<string, DiagnosticsCollector> collectors
= new Dictionary<string, DiagnosticsCollector>();
static readonly Dictionary<int, DiagnosticsCollector> collectors
= new Dictionary<int, DiagnosticsCollector>();

public static event Action<IObjectResolver> OnContainerBuilt;

public static DiagnosticsCollector GetCollector(string name)
public static DiagnosticsCollector GetCollector(int instanceId, string name)
{
lock (collectors)
{
if (!collectors.TryGetValue(name, out var collector))
if (!collectors.TryGetValue(instanceId, out var collector))
{
collector = new DiagnosticsCollector(name);
collectors.Add(name, collector);
collector = new DiagnosticsCollector($"${name}/{instanceId}");
collectors.Add(instanceId, collector);
}
return collector;
}
Expand Down Expand Up @@ -52,5 +52,13 @@ internal static DiagnosticsInfo FindByRegistration(Registration registration)
{
return GetDiagnosticsInfos().FirstOrDefault(x => x.ResolveInfo.Registration == registration);
}

public static void RemoveCollector(int instanceId)
{
lock (collectors)
{
collectors.Remove(instanceId);
}
}
}
}
18 changes: 16 additions & 2 deletions VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ void IDisposable.Dispose()
}
}

int instanceId;

[SerializeField]
public ParentReference parentReference;

Expand All @@ -65,6 +67,10 @@ static LifetimeScope Create(IInstaller installer = null)
var gameObject = new GameObject("LifetimeScope");
gameObject.SetActive(false);
var newScope = gameObject.AddComponent<LifetimeScope>();
if (VContainerSettings.DiagnosticsEnabled)
{
newScope.instanceId = gameObject.GetInstanceID();
}
if (installer != null)
{
newScope.localExtraInstallers.Add(installer);
Expand Down Expand Up @@ -168,6 +174,10 @@ public void DisposeCore()
Container?.Dispose();
Container = null;
CancelAwake(this);
if (VContainerSettings.DiagnosticsEnabled)
{
DiagnositcsContext.RemoveCollector(instanceId);
}
}

public void Build()
Expand All @@ -188,7 +198,7 @@ public void Build()
{
builder.RegisterBuildCallback(SetContainer);
builder.ApplicationOrigin = this;
builder.Diagnostics = VContainerSettings.DiagnosticsEnabled ? DiagnositcsContext.GetCollector(name) : null;
builder.Diagnostics = VContainerSettings.DiagnosticsEnabled ? DiagnositcsContext.GetCollector(instanceId, name) : null;
InstallTo(builder);
});
}
Expand All @@ -197,7 +207,7 @@ public void Build()
var builder = new ContainerBuilder
{
ApplicationOrigin = this,
Diagnostics = VContainerSettings.DiagnosticsEnabled ? DiagnositcsContext.GetCollector(name) : null,
Diagnostics = VContainerSettings.DiagnosticsEnabled ? DiagnositcsContext.GetCollector(instanceId, name) : null,
};
builder.RegisterBuildCallback(SetContainer);
InstallTo(builder);
Expand Down Expand Up @@ -227,6 +237,10 @@ public TScope CreateChild<TScope>(IInstaller installer = null)
childGameObject.transform.SetParent(transform, false);
}
var child = childGameObject.AddComponent<TScope>();
if (VContainerSettings.DiagnosticsEnabled)
{
child.instanceId = childGameObject.GetInstanceID();
}
if (installer != null)
{
child.localExtraInstallers.Add(installer);
Expand Down
Loading