Initial commit: Unity WordConnect project
This commit is contained in:
@ -0,0 +1,44 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace VContainer.Unity
|
||||
{
|
||||
sealed class ExistingComponentProvider : IInstanceProvider
|
||||
{
|
||||
readonly object instance;
|
||||
readonly IInjector injector;
|
||||
readonly IReadOnlyList<IInjectParameter> customParameters;
|
||||
readonly bool dontDestroyOnLoad;
|
||||
|
||||
public ExistingComponentProvider(
|
||||
object instance,
|
||||
IInjector injector,
|
||||
IReadOnlyList<IInjectParameter> customParameters,
|
||||
bool dontDestroyOnLoad = false)
|
||||
{
|
||||
this.instance = instance;
|
||||
this.customParameters = customParameters;
|
||||
this.injector = injector;
|
||||
this.dontDestroyOnLoad = dontDestroyOnLoad;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public object SpawnInstance(IObjectResolver resolver)
|
||||
{
|
||||
injector.Inject(instance, resolver, customParameters);
|
||||
if (dontDestroyOnLoad)
|
||||
{
|
||||
if (instance is UnityEngine.Object component)
|
||||
{
|
||||
UnityEngine.Object.DontDestroyOnLoad(component);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new VContainerException(instance.GetType(),
|
||||
$"Cannot apply `DontDestroyOnLoad`. {instance.GetType().Name} is not a UnityEngine.Object");
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3eaf1bb53ea4494484290e95207cc8f3
|
||||
timeCreated: 1620020530
|
||||
@ -0,0 +1,72 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
using VContainer.Internal;
|
||||
|
||||
namespace VContainer.Unity
|
||||
{
|
||||
sealed class FindComponentProvider : IInstanceProvider
|
||||
{
|
||||
readonly Type componentType;
|
||||
readonly IReadOnlyList<IInjectParameter> customParameters;
|
||||
ComponentDestination destination;
|
||||
Scene scene;
|
||||
|
||||
public FindComponentProvider(
|
||||
Type componentType,
|
||||
IReadOnlyList<IInjectParameter> customParameters,
|
||||
in Scene scene,
|
||||
in ComponentDestination destination)
|
||||
{
|
||||
this.componentType = componentType;
|
||||
this.customParameters = customParameters;
|
||||
this.scene = scene;
|
||||
this.destination = destination;
|
||||
}
|
||||
|
||||
public object SpawnInstance(IObjectResolver resolver)
|
||||
{
|
||||
var component = default(Component);
|
||||
|
||||
var parent = destination.GetParent(resolver);
|
||||
if (parent != null)
|
||||
{
|
||||
component = parent.GetComponentInChildren(componentType, true);
|
||||
if (component == null)
|
||||
{
|
||||
throw new VContainerException(componentType, $"{componentType} is not in the parent {parent.name} : {this}");
|
||||
}
|
||||
}
|
||||
else if (scene.IsValid())
|
||||
{
|
||||
using (ListPool<GameObject>.Get(out var gameObjectBuffer))
|
||||
{
|
||||
scene.GetRootGameObjects(gameObjectBuffer);
|
||||
foreach (var gameObject in gameObjectBuffer)
|
||||
{
|
||||
component = gameObject.GetComponentInChildren(componentType, true);
|
||||
if (component != null) break;
|
||||
}
|
||||
}
|
||||
if (component == null)
|
||||
{
|
||||
throw new VContainerException(componentType, $"{componentType} is not in this scene {scene.path} : {this}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new VContainerException(componentType, $"Invalid Component find target {this}");
|
||||
}
|
||||
|
||||
if (component is MonoBehaviour monoBehaviour)
|
||||
{
|
||||
var injector = InjectorCache.GetOrBuild(monoBehaviour.GetType());
|
||||
injector.Inject(monoBehaviour, resolver, customParameters);
|
||||
}
|
||||
|
||||
destination.ApplyDontDestroyOnLoadIfNeeded(component);
|
||||
return component;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8feb76212733400e80e88bc385685993
|
||||
timeCreated: 1619932417
|
||||
@ -0,0 +1,51 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace VContainer.Unity
|
||||
{
|
||||
sealed class NewGameObjectProvider : IInstanceProvider
|
||||
{
|
||||
readonly Type componentType;
|
||||
readonly IInjector injector;
|
||||
readonly IReadOnlyList<IInjectParameter> customParameters;
|
||||
readonly string newGameObjectName;
|
||||
ComponentDestination destination;
|
||||
|
||||
public NewGameObjectProvider(
|
||||
Type componentType,
|
||||
IInjector injector,
|
||||
IReadOnlyList<IInjectParameter> customParameters,
|
||||
in ComponentDestination destination,
|
||||
string newGameObjectName = null)
|
||||
{
|
||||
this.componentType = componentType;
|
||||
this.customParameters = customParameters;
|
||||
this.injector = injector;
|
||||
this.destination = destination;
|
||||
this.newGameObjectName = newGameObjectName;
|
||||
}
|
||||
|
||||
public object SpawnInstance(IObjectResolver resolver)
|
||||
{
|
||||
var name = string.IsNullOrEmpty(newGameObjectName)
|
||||
? componentType.Name
|
||||
: newGameObjectName;
|
||||
var gameObject = new GameObject(name);
|
||||
gameObject.SetActive(false);
|
||||
|
||||
var parent = destination.GetParent(resolver);
|
||||
if (parent != null)
|
||||
{
|
||||
gameObject.transform.SetParent(parent);
|
||||
}
|
||||
var component = gameObject.AddComponent(componentType);
|
||||
|
||||
injector.Inject(component, resolver, customParameters);
|
||||
destination.ApplyDontDestroyOnLoadIfNeeded(component);
|
||||
|
||||
component.gameObject.SetActive(true);
|
||||
return component;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f18c3e4b45d349ca8f1d388dca5bc8b8
|
||||
timeCreated: 1619933144
|
||||
@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace VContainer.Unity
|
||||
{
|
||||
sealed class PrefabComponentProvider : IInstanceProvider
|
||||
{
|
||||
readonly IInjector injector;
|
||||
readonly IReadOnlyList<IInjectParameter> customParameters;
|
||||
readonly Func<IObjectResolver, Component> prefabFinder;
|
||||
ComponentDestination destination;
|
||||
|
||||
public PrefabComponentProvider(
|
||||
Func<IObjectResolver, Component> prefabFinder,
|
||||
IInjector injector,
|
||||
IReadOnlyList<IInjectParameter> customParameters,
|
||||
in ComponentDestination destination)
|
||||
{
|
||||
this.injector = injector;
|
||||
this.customParameters = customParameters;
|
||||
this.prefabFinder = prefabFinder;
|
||||
this.destination = destination;
|
||||
}
|
||||
|
||||
public object SpawnInstance(IObjectResolver resolver)
|
||||
{
|
||||
var prefab = prefabFinder(resolver);
|
||||
var parent = destination.GetParent(resolver);
|
||||
|
||||
var wasActive = prefab.gameObject.activeSelf;
|
||||
using (new ObjectResolverUnityExtensions.PrefabDirtyScope(prefab.gameObject))
|
||||
{
|
||||
if (wasActive)
|
||||
{
|
||||
prefab.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
var component = parent != null
|
||||
? UnityEngine.Object.Instantiate(prefab, parent)
|
||||
: UnityEngine.Object.Instantiate(prefab);
|
||||
|
||||
if (VContainerSettings.Instance != null && VContainerSettings.Instance.RemoveClonePostfix)
|
||||
component.name = prefab.name;
|
||||
|
||||
try
|
||||
{
|
||||
injector.Inject(component, resolver, customParameters);
|
||||
destination.ApplyDontDestroyOnLoadIfNeeded(component);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (wasActive)
|
||||
{
|
||||
prefab.gameObject.SetActive(true);
|
||||
component.gameObject.SetActive(true);
|
||||
}
|
||||
}
|
||||
return component;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 51a9e9ed532b41898ab9e5f82ea2781f
|
||||
timeCreated: 1619932830
|
||||
@ -0,0 +1,81 @@
|
||||
#if VCONTAINER_ECS_INTEGRATION
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Unity.Entities;
|
||||
|
||||
namespace VContainer.Unity
|
||||
{
|
||||
public sealed class SystemInstanceProvider<T> : IInstanceProvider where T : ComponentSystemBase
|
||||
{
|
||||
readonly Type systemType;
|
||||
readonly IInjector injector;
|
||||
readonly IReadOnlyList<IInjectParameter> customParameters;
|
||||
readonly string worldName;
|
||||
readonly Type systemGroupType;
|
||||
|
||||
World world;
|
||||
T instance;
|
||||
|
||||
public SystemInstanceProvider(
|
||||
Type systemType,
|
||||
string worldName,
|
||||
Type systemGroupType,
|
||||
IInjector injector,
|
||||
IReadOnlyList<IInjectParameter> customParameters)
|
||||
{
|
||||
this.systemType = systemType;
|
||||
this.worldName = worldName;
|
||||
this.systemGroupType = systemGroupType;
|
||||
this.injector = injector;
|
||||
this.customParameters = customParameters;
|
||||
}
|
||||
|
||||
public object SpawnInstance(IObjectResolver resolver)
|
||||
{
|
||||
if (world is null)
|
||||
world = GetWorld(resolver);
|
||||
|
||||
if (instance is null)
|
||||
{
|
||||
instance = (T) injector.CreateInstance(resolver, customParameters);
|
||||
#if UNITY_2022_2_OR_NEWER
|
||||
world.AddSystemManaged(instance);
|
||||
#else
|
||||
world.AddSystem(instance);
|
||||
#endif
|
||||
|
||||
if (systemGroupType != null)
|
||||
{
|
||||
#if UNITY_2022_2_OR_NEWER
|
||||
var systemGroup = (ComponentSystemGroup)world.GetOrCreateSystemManaged(systemGroupType);
|
||||
#else
|
||||
var systemGroup = (ComponentSystemGroup)world.GetOrCreateSystem(systemGroupType);
|
||||
#endif
|
||||
systemGroup.AddSystemToUpdateList(instance);
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
#if UNITY_2022_2_OR_NEWER
|
||||
return world.GetExistingSystemManaged(systemType);
|
||||
#else
|
||||
return world.GetExistingSystem(systemType);
|
||||
#endif
|
||||
}
|
||||
|
||||
World GetWorld(IObjectResolver resolver)
|
||||
{
|
||||
if (worldName is null && World.DefaultGameObjectInjectionWorld != null)
|
||||
return World.DefaultGameObjectInjectionWorld;
|
||||
|
||||
var worlds = resolver.Resolve<IEnumerable<World>>();
|
||||
foreach (var world in worlds)
|
||||
{
|
||||
if (world.Name == worldName)
|
||||
return world;
|
||||
}
|
||||
throw new VContainerException(systemType, $"World `{worldName}` is not registered");
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: baa43b4b3a9a4d1895436d98f4c539e4
|
||||
timeCreated: 1595083942
|
||||
@ -0,0 +1,71 @@
|
||||
#if VCONTAINER_ECS_INTEGRATION && UNITY_2022_2_OR_NEWER
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Unity.Entities;
|
||||
|
||||
namespace VContainer.Unity
|
||||
{
|
||||
public sealed class UnmanagedSystemInstanceProvider : IInstanceProvider
|
||||
{
|
||||
readonly Type systemType;
|
||||
readonly IInjector injector;
|
||||
readonly IReadOnlyList<IInjectParameter> customParameters;
|
||||
readonly string worldName;
|
||||
readonly Type systemGroupType;
|
||||
|
||||
private World world;
|
||||
private UnmanagedSystemReference instance;
|
||||
|
||||
public UnmanagedSystemInstanceProvider(
|
||||
Type systemType,
|
||||
string worldName,
|
||||
Type systemGroupType,
|
||||
IInjector injector,
|
||||
IReadOnlyList<IInjectParameter> customParameters)
|
||||
{
|
||||
this.systemType = systemType;
|
||||
this.worldName = worldName;
|
||||
this.systemGroupType = systemGroupType;
|
||||
this.injector = injector;
|
||||
this.customParameters = customParameters;
|
||||
}
|
||||
|
||||
public object SpawnInstance(IObjectResolver resolver)
|
||||
{
|
||||
if (world is null)
|
||||
world = GetWorld(resolver);
|
||||
|
||||
if (instance is null)
|
||||
{
|
||||
SystemHandle handle = world.GetOrCreateSystem(systemType);
|
||||
injector.Inject(handle, resolver, customParameters);
|
||||
|
||||
if (systemGroupType is not null)
|
||||
{
|
||||
var systemGroup = (ComponentSystemGroup) world.GetOrCreateSystemManaged(systemGroupType);
|
||||
systemGroup.AddSystemToUpdateList(handle);
|
||||
}
|
||||
Type refType = typeof(UnmanagedSystemReference<>);
|
||||
Type target = refType.MakeGenericType(systemType);
|
||||
instance = (UnmanagedSystemReference)Activator.CreateInstance(target, handle, world);
|
||||
return instance;
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
private World GetWorld(IObjectResolver resolver)
|
||||
{
|
||||
if (worldName is null && World.DefaultGameObjectInjectionWorld != null)
|
||||
return World.DefaultGameObjectInjectionWorld;
|
||||
|
||||
var worlds = resolver.Resolve<IEnumerable<World>>();
|
||||
foreach (World w in worlds)
|
||||
{
|
||||
if (w.Name == worldName)
|
||||
return w;
|
||||
}
|
||||
throw new VContainerException(systemType, $"World `{worldName}` is not Created");
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c3e078788acd49f292b76c0a778b1a45
|
||||
timeCreated: 1671719884
|
||||
@ -0,0 +1,48 @@
|
||||
#if VCONTAINER_ECS_INTEGRATION
|
||||
using System;
|
||||
using Unity.Entities;
|
||||
using UnityEngine.LowLevel;
|
||||
|
||||
namespace VContainer.Unity
|
||||
{
|
||||
public sealed class WorldInstanceProvider : IInstanceProvider
|
||||
{
|
||||
readonly string name;
|
||||
readonly Action<World> initialization;
|
||||
|
||||
public WorldInstanceProvider(string name, Action<World> initialization = null)
|
||||
{
|
||||
this.name = name;
|
||||
this.initialization = initialization;
|
||||
}
|
||||
|
||||
public object SpawnInstance(IObjectResolver resolver)
|
||||
{
|
||||
var world = new World(name);
|
||||
if (initialization != null)
|
||||
{
|
||||
initialization(world);
|
||||
}
|
||||
else
|
||||
{
|
||||
#if UNITY_2022_2_OR_NEWER
|
||||
world.CreateSystemManaged<InitializationSystemGroup>();
|
||||
world.CreateSystemManaged<SimulationSystemGroup>();
|
||||
world.CreateSystemManaged<PresentationSystemGroup>();
|
||||
|
||||
ScriptBehaviourUpdateOrder.RemoveWorldFromCurrentPlayerLoop(world);
|
||||
ScriptBehaviourUpdateOrder.AppendWorldToCurrentPlayerLoop(world);
|
||||
#else
|
||||
world.CreateSystem<InitializationSystemGroup>();
|
||||
world.CreateSystem<SimulationSystemGroup>();
|
||||
world.CreateSystem<PresentationSystemGroup>();
|
||||
|
||||
ScriptBehaviourUpdateOrder.RemoveWorldFromCurrentPlayerLoop(world);
|
||||
ScriptBehaviourUpdateOrder.AddWorldToCurrentPlayerLoop(world);
|
||||
#endif
|
||||
}
|
||||
return world;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c152c2c135444258b6b6bc730bdb1f64
|
||||
timeCreated: 1595059160
|
||||
Reference in New Issue
Block a user