Skip to content

Commit

Permalink
Merge pull request #103 from hadashiA/ku/map-entrypoint
Browse files Browse the repository at this point in the history
Add MapEntryPoint<>
  • Loading branch information
hadashiA authored Nov 15, 2024
2 parents 51765fb + fb6693d commit e5a8ec9
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 36 deletions.
9 changes: 7 additions & 2 deletions src/VitalRouter.Unity/Assets/Sandbox/Controllers.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
using Sandbox;
using UnityEngine;
using VContainer.Unity;
using VitalRouter;

public class LoggingInterceptor : ICommandInterceptor
Expand Down Expand Up @@ -34,13 +34,18 @@ public ValueTask InvokeAsync<T>(T command, PublishContext context, PublishContin
[Routes]
// [Filter(typeof(LoggingInterceptor))]
[Filter(typeof(AInterceptor))]
public partial class SamplePresenter
public partial class SamplePresenter : IInitializable
{
public SamplePresenter()
{
UnityEngine.Debug.Log("SamplePresenter.ctor");
}

public void Initialize()
{
UnityEngine.Debug.Log("SamplePresenter.Initialize");
}

public UniTask On(CharacterEnterCommand cmd)
{
UnityEngine.Debug.Log("SamplePresenter.ctor");
Expand Down
34 changes: 16 additions & 18 deletions src/VitalRouter.Unity/Assets/Sandbox/SampleLifetimeScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,31 @@ public class SampleLifetimeScope : LifetimeScope
{
protected override void Configure(IContainerBuilder builder)
{
// builder.RegisterEntryPoint<SampleEntryPoint>();

builder.RegisterVitalRouter(routing =>
{
routing.Filters.Add<LoggingInterceptor>();
routing.Map<SamplePresenter>();
routing.Map<RoutingBehaviour>();
routing.MapEntryPoint<SamplePresenter>();
routing.MapComponentOnNewGameObject<RoutingBehaviour>();

// routing.FanOut(childRouter =>
// {
// childRouter.Map<SamplePresenter2>();
// });

routing.FanOut(childRouter =>
{
childRouter.Map<SamplePresenter3>();

childRouter.FanOut(grandChildRouter =>
{
grandChildRouter.Map<SamplePresenter4>();
});

childRouter.FanOut(grandChildRouter =>
{
grandChildRouter.Map<SamplePresenter5>();
});
});
// routing.FanOut(childRouter =>
// {
// childRouter.Map<SamplePresenter3>();
//
// childRouter.FanOut(grandChildRouter =>
// {
// grandChildRouter.Map<SamplePresenter4>();
// });
//
// childRouter.FanOut(grandChildRouter =>
// {
// grandChildRouter.Map<SamplePresenter5>();
// });
// });
});
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/VitalRouter.Unity/Assets/Sandbox/SampleScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ GameObject:
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
m_IsActive: 0
--- !u!4 &1985057628
Transform:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -989,7 +989,7 @@ GameObject:
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 0
m_IsActive: 1
--- !u!4 &2056801797
Transform:
m_ObjectHideFlags: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,20 @@ public RoutingBuilder(IContainerBuilder containerBuilder)
this.containerBuilder = containerBuilder;
}

public RegistrationBuilder Map<T>()
public void Map<T>()
{
RegistrationBuilder registrationBuilder;
if (typeof(UnityEngine.Component).IsAssignableFrom(typeof(T)))
if (!containerBuilder.Exists(typeof(T)))
{
registrationBuilder = containerBuilder.RegisterComponentOnNewGameObject(typeof(T), Lifetime.Singleton);
}
else
{
registrationBuilder = containerBuilder.Register<T>(Lifetime.Singleton);
if (typeof(UnityEngine.Component).IsAssignableFrom(typeof(T)))
{
containerBuilder.RegisterComponentOnNewGameObject(typeof(T), Lifetime.Singleton);
}
else
{
containerBuilder.Register<T>(Lifetime.Singleton);
}
}
MapRoutesInfos.Add(MapRoutesInfo.Analyze(typeof(T)));
return registrationBuilder;
}

public RegistrationBuilder Map<T>(T instance) where T : class
Expand All @@ -65,13 +66,31 @@ public RegistrationBuilder Map<T>(T instance) where T : class
return containerBuilder.RegisterInstance(instance);
}

public RegistrationBuilder MapComponentInHierarchy<T>() where T : UnityEngine.Component
public RegistrationBuilder MapEntryPoint<T>()
{
MapRoutesInfos.Add(MapRoutesInfo.Analyze(typeof(T)));
return containerBuilder.RegisterEntryPoint<T>().AsSelf();
}

public RegistrationBuilder MapComponent<T>(T instance) where T : UnityEngine.Component
{
MapRoutesInfos.Add(MapRoutesInfo.Analyze(typeof(T)));
return containerBuilder.RegisterComponent(instance);
}

public RegistrationBuilder MapComponentOnNewGameObject<T>() where T : UnityEngine.Component
{
MapRoutesInfos.Add(MapRoutesInfo.Analyze(typeof(T)));
return containerBuilder.RegisterComponentOnNewGameObject<T>(Lifetime.Singleton);
}

public ComponentRegistrationBuilder MapComponentInHierarchy<T>() where T : UnityEngine.Component
{
MapRoutesInfos.Add(MapRoutesInfo.Analyze(typeof(T)));
return containerBuilder.RegisterComponentInHierarchy<T>();
}

public RegistrationBuilder MapComponentInNewPrefab<T>(T prefab) where T : UnityEngine.Component
public ComponentRegistrationBuilder MapComponentInNewPrefab<T>(T prefab) where T : UnityEngine.Component
{
MapRoutesInfos.Add(MapRoutesInfo.Analyze(typeof(T)));
return containerBuilder.RegisterComponentInNewPrefab(prefab, Lifetime.Singleton);
Expand Down Expand Up @@ -122,7 +141,7 @@ public static void RegisterVitalRouter(this IContainerBuilder builder, Action<Ro

if (fanOut != null)
{
router.Filter(fanOut);
router.AddFilter(fanOut);
}
});
}
Expand All @@ -149,7 +168,7 @@ static void RegisterVitalRouterRecursive(this IContainerBuilder builder, Router
InvokeMapRoutes(routerInstance, routing, container);
if (fanOut != null)
{
routerInstance.Filter(fanOut);
routerInstance.AddFilter(fanOut);
}
});
}
Expand Down Expand Up @@ -201,7 +220,7 @@ static void InvokeMapRoutes(Router router, RoutingBuilder routing, IObjectResolv
{
foreach (var interceptorType in routing.Filters.Types)
{
router.Filter((ICommandInterceptor)container.Resolve(interceptorType));
router.AddFilter((ICommandInterceptor)container.Resolve(interceptorType));
}

for (var i = 0; i < routing.MapRoutesInfos.Count; i++)
Expand Down

0 comments on commit e5a8ec9

Please sign in to comment.