VContainer icon indicating copy to clipboard operation
VContainer copied to clipboard

ECS (beta) not working with Entity package 1.0.10

Open NeistH2o opened this issue 2 years ago • 2 comments

I updated my project to unity 2022.3.2 and the entity package to 1.0.10 and it seems that the method AddSystemManaged needs the concrete type of systems instead of ComponentSystemBase at the line here: https://github.com/hadashiA/VContainer/blob/af7bd4ecabaf6ea3f9e58ecc4352dd5296e5ef29/VContainer/Assets/VContainer/Runtime/Unity/InstanceProviders/SystemInstanceProvider.cs#L42

Everything still compiles because the methods reads the type with a generic parameter but I get this exception at runtime: (The system being registered is not generic or abstract, it's a concrete SystemGroup)

ArgumentException: Unknown Type:`Unity.Entities.ComponentSystemBase` All ComponentType must be known at compile time. For generic components, each concrete type must be registered with [RegisterGenericComponentType].

Unity.Entities.TypeManager.ManagedException (System.Type type) (at ./Library/PackageCache/[email protected]/Unity.Entities/Types/TypeManager.cs:1706)
Unity.Entities.TypeManager.ManagedException[T] () (at ./Library/PackageCache/[email protected]/Unity.Entities/Types/TypeManager.cs:1687)
Unity.Entities.TypeManager.GetSystemTypeIndex[T] () (at ./Library/PackageCache/[email protected]/Unity.Entities/Types/TypeManagerSystems.cs:1016)
Unity.Entities.World.AddSystemManaged[T] (T system) (at ./Library/PackageCache/[email protected]/Unity.Entities/World.cs:930)
VContainer.Unity.SystemInstanceProvider.SpawnInstance (VContainer.IObjectResolver resolver) (at Assets/Plugins/VContainer/Runtime/Unity/InstanceProviders/SystemInstanceProvider.cs:42)
VContainer.Registration.SpawnInstance (VContainer.IObjectResolver resolver) (at Assets/Plugins/VContainer/Runtime/Registration.cs:33)
VContainer.Container.ResolveCore (VContainer.Registration registration) (at Assets/Plugins/VContainer/Runtime/Container.cs:248)
VContainer.Container.Resolve (VContainer.Registration registration) (at Assets/Plugins/VContainer/Runtime/Container.cs:209)
VContainer.Container.Resolve (System.Type type) (at Assets/Plugins/VContainer/Runtime/Container.cs:197)
VContainer.IObjectResolverExtensions.ResolveOrParameter (VContainer.IObjectResolver resolver, System.Type parameterType, System.String parameterName, System.Collections.Generic.IReadOnlyList`1[T] parameters) (at Assets/Plugins/VContainer/Runtime/IObjectResolverExtensions.cs:35)
VContainer.Internal.ReflectionInjector.CreateInstance (VContainer.IObjectResolver resolver, System.Collections.Generic.IReadOnlyList`1[T] parameters) (at Assets/Plugins/VContainer/Runtime/Internal/ReflectionInjector.cs:40)
VContainer.Unity.SystemInstanceProvider.SpawnInstance (VContainer.IObjectResolver resolver) (at Assets/Plugins/VContainer/Runtime/Unity/InstanceProviders/SystemInstanceProvider.cs:40)
VContainer.Registration.SpawnInstance (VContainer.IObjectResolver resolver) (at Assets/Plugins/VContainer/Runtime/Registration.cs:33)
VContainer.Container.ResolveCore (VContainer.Registration registration) (at Assets/Plugins/VContainer/Runtime/Container.cs:248)
VContainer.Container.Resolve (VContainer.Registration registration) (at Assets/Plugins/VContainer/Runtime/Container.cs:209)
VContainer.Container.Resolve (System.Type type) (at Assets/Plugins/VContainer/Runtime/Container.cs:197)
VContainer.IObjectResolverExtensions.ResolveOrParameter (VContainer.IObjectResolver resolver, System.Type parameterType, System.String parameterName, System.Collections.Generic.IReadOnlyList`1[T] parameters) (at Assets/Plugins/VContainer/Runtime/IObjectResolverExtensions.cs:35)
VContainer.Internal.ReflectionInjector.CreateInstance (VContainer.IObjectResolver resolver, System.Collections.Generic.IReadOnlyList`1[T] parameters) (at Assets/Plugins/VContainer/Runtime/Internal/ReflectionInjector.cs:40)
VContainer.Internal.InstanceProvider.SpawnInstance (VContainer.IObjectResolver resolver) (at Assets/Plugins/VContainer/Runtime/Internal/InstanceProviders/InstanceProvider.cs:21)
VContainer.Registration.SpawnInstance (VContainer.IObjectResolver resolver) (at Assets/Plugins/VContainer/Runtime/Registration.cs:33)
VContainer.Container+<>c__DisplayClass12_0.<.ctor>b__1 () (at Assets/Plugins/VContainer/Runtime/Container.cs:186)
System.Lazy`1[T].ViaFactory (System.Threading.LazyThreadSafetyMode mode) (at <605bf8b31fcb444b85176da963870aa7>:0)
System.Lazy`1[T].ExecutionAndPublication (System.LazyHelper executionAndPublication, System.Boolean useDefaultConstructor) (at <605bf8b31fcb444b85176da963870aa7>:0)
System.Lazy`1[T].CreateValue () (at <605bf8b31fcb444b85176da963870aa7>:0)
System.Lazy`1[T].get_Value () (at <605bf8b31fcb444b85176da963870aa7>:0)
VContainer.Container.ResolveCore (VContainer.Registration registration) (at Assets/Plugins/VContainer/Runtime/Container.cs:238)
VContainer.Container.Resolve (VContainer.Registration registration) (at Assets/Plugins/VContainer/Runtime/Container.cs:209)
VContainer.ScopedContainer.ResolveCore (VContainer.Registration registration) (at Assets/Plugins/VContainer/Runtime/Container.cs:124)
VContainer.ScopedContainer.Resolve (VContainer.Registration registration) (at Assets/Plugins/VContainer/Runtime/Container.cs:85)
VContainer.Internal.CollectionInstanceProvider.SpawnInstance (VContainer.IObjectResolver resolver, System.Collections.Generic.IReadOnlyList`1[T] registrations) (at Assets/Plugins/VContainer/Runtime/Internal/InstanceProviders/CollectionInstanceProvider.cs:73)
VContainer.Internal.ContainerLocalInstanceProvider.SpawnInstance (VContainer.IObjectResolver resolver) (at Assets/Plugins/VContainer/Runtime/Internal/InstanceProviders/ContainerLocalInstanceProvider.cs:24)
VContainer.Registration.SpawnInstance (VContainer.IObjectResolver resolver) (at Assets/Plugins/VContainer/Runtime/Registration.cs:33)
VContainer.ScopedContainer+<>c__DisplayClass17_0.<.ctor>b__1 () (at Assets/Plugins/VContainer/Runtime/Container.cs:71)
System.Lazy`1[T].ViaFactory (System.Threading.LazyThreadSafetyMode mode) (at <605bf8b31fcb444b85176da963870aa7>:0)
System.Lazy`1[T].ExecutionAndPublication (System.LazyHelper executionAndPublication, System.Boolean useDefaultConstructor) (at <605bf8b31fcb444b85176da963870aa7>:0)
System.Lazy`1[T].CreateValue () (at <605bf8b31fcb444b85176da963870aa7>:0)
System.Lazy`1[T].get_Value () (at <605bf8b31fcb444b85176da963870aa7>:0)
VContainer.ScopedContainer.CreateTrackedInstance (VContainer.Registration registration) (at Assets/Plugins/VContainer/Runtime/Container.cs:144)
VContainer.ScopedContainer.ResolveCore (VContainer.Registration registration) (at Assets/Plugins/VContainer/Runtime/Container.cs:132)
VContainer.ScopedContainer.Resolve (VContainer.Registration registration) (at Assets/Plugins/VContainer/Runtime/Container.cs:85)
VContainer.ScopedContainer.Resolve (System.Type type) (at Assets/Plugins/VContainer/Runtime/Container.cs:76)
VContainer.IObjectResolverExtensions.Resolve[T] (VContainer.IObjectResolver resolver) (at Assets/Plugins/VContainer/Runtime/IObjectResolverExtensions.cs:10)
VContainer.Unity.EntryPointDispatcher.Dispatch () (at Assets/Plugins/VContainer/Runtime/Unity/EntryPointDispatcher.cs:34)
VContainer.Unity.EntryPointsBuilder+<>c.<EnsureDispatcherRegistered>b__0_0 (VContainer.IObjectResolver container) (at Assets/Plugins/VContainer/Runtime/Unity/ContainerBuilderUnityExtensions.cs:20)
VContainer.ContainerBuilder.EmitCallbacks (VContainer.IObjectResolver container) (at Assets/Plugins/VContainer/Runtime/ContainerBuilder.cs:156)
VContainer.ContainerBuilder.Build () (at Assets/Plugins/VContainer/Runtime/ContainerBuilder.cs:111)
VContainer.Unity.LifetimeScope.Build () (at Assets/Plugins/VContainer/Runtime/Unity/LifetimeScope.cs:198)
RAS.H2o.ServerLifetimeScope.StartAsync () (at Assets/H2o/Head/server/Runtime/FlowControl/Scopes/ServerLifetimeScope.cs:194)
UnityEngine.Debug:LogException(Exception)

NeistH2o avatar Jun 16 '23 03:06 NeistH2o

I was able to make the SystemInstanceProvider SpawnInstance method work with reflection, probably a dirty fix but at least it confirms the cause:

        public object SpawnInstance(IObjectResolver resolver)
        {
            if (world is null)
                world = GetWorld(resolver);

            if (instance is null)
            {
                instance = (ComponentSystemBase)injector.CreateInstance(resolver, customParameters);
#if VCONTAINER_ECS_INTEGRATION_1_0
                var concreteSystemType = instance.GetType();
                var genericMethodInfo = typeof(World).GetMethod(nameof(World.AddSystemManaged), BindingFlags.Public | BindingFlags.Instance);
                var concreteMethodInfo = genericMethodInfo!.MakeGenericMethod(new Type[] { concreteSystemType });

                concreteMethodInfo.Invoke(world, new object[]{ instance });
                // world.AddSystemManaged(instance);
#else
                world.AddSystem(instance);
#endif

        [...]
        }

NeistH2o avatar Jun 16 '23 04:06 NeistH2o

Sorry for late response.. Some fixes have been merged, is the same problem still there? https://github.com/hadashiA/VContainer/pull/467

hadashiA avatar Jan 23 '24 06:01 hadashiA