// MIT License - Copyright (c) 2025 wallstop
// Full license text: https://github.com/wallstop/unity-helpers/blob/main/LICENSE
#if VCONTAINER_PRESENT
namespace WallstopStudios.UnityHelpers.Integrations.VContainer
{
using System;
using global::VContainer;
using global::VContainer.Unity;
using WallstopStudios.UnityHelpers.Core.Attributes;
using WallstopStudios.UnityHelpers.Tags;
///
/// Convenience registration helpers for wiring relational component support into a
/// .
///
///
/// Registers the shared as a singleton and schedules a
/// scene-wide entry point that hydrates all relational fields after the container has been built.
/// Optionally wires so future additive scenes receive
/// the same treatment.
///
public static class RelationalComponentsBuilderExtensions
{
///
/// Registers the relational component assigner and scene entry point with the supplied
/// container builder.
///
/// The VContainer builder.
///
/// Optional settings to control how the active scene is scanned (e.g., include inactive
/// objects). When null, is used.
///
///
/// When true registers so additively loaded scenes
/// are hydrated with the same options. Disable when you manage additive scenes manually.
///
///
///
/// using VContainer;
/// using VContainer.Unity;
/// using WallstopStudios.UnityHelpers.Integrations.VContainer;
///
/// public sealed class GameLifetimeScope : LifetimeScope
/// {
/// protected override void Configure(IContainerBuilder builder)
/// {
/// // Basic usage
/// builder.RegisterRelationalComponents();
///
/// // Or customize scanning options
/// builder.RegisterRelationalComponents(
/// new RelationalSceneAssignmentOptions(includeInactive: false)
/// );
/// }
/// }
///
///
public static void RegisterRelationalComponents(
this IContainerBuilder builder,
RelationalSceneAssignmentOptions? options = null,
bool enableAdditiveSceneListener = true
)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
AttributeMetadataCache cacheInstance = AttributeMetadataCache.Instance;
if (cacheInstance != null)
{
builder.RegisterInstance(cacheInstance).AsSelf();
}
RelationalSceneAssignmentOptions resolved =
options ?? RelationalSceneAssignmentOptions.Default;
builder
.Register(Lifetime.Singleton)
.As()
.AsSelf();
builder.RegisterEntryPoint().WithParameter(resolved);
if (enableAdditiveSceneListener)
{
builder.RegisterEntryPoint().WithParameter(resolved);
}
}
}
}
#endif