friflo / Friflo.Engine.ECS

C# ECS šŸ”„ high-performance
https://friflo.gitbook.io/friflo.engine.ecs
GNU Lesser General Public License v3.0
148 stars 9 forks source link

Failed to create EntityStore in runtime compiled code #17

Open inmny opened 1 month ago

inmny commented 1 month ago

I tried to use your framework(netstandard 2.1) in a unity mod. The mod is compiled and loaded at runtime.

The mod is compiled with "allow unsafe code" on Windows.

Framework version:

Dependencies version:

Log:

[Error  : Unity Log] [NML]: The type initializer for 'Static' threw an exception.
[Error  : Unity Log] [NML]:   at Friflo.Engine.ECS.EntityStoreBase..ctor () [0x00022] in <2760b6615674441eb080f0fb778cfc66>:0
  at Friflo.Engine.ECS.EntityStore..ctor (Friflo.Engine.ECS.PidType pidType) [0x00000] in <2760b6615674441eb080f0fb778cfc66>:0
  at Friflo.Engine.ECS.EntityStore..ctor () [0x00000] in <2760b6615674441eb080f0fb778cfc66>:0
  at Cultiway.ModClass.OnModLoad () [0x00001] in Source\ModClass.cs:13

Code:

using Friflo.Engine.ECS;
using NeoModLoader.api;
using UnityEngine;

namespace Cultiway
{
    internal class ModClass : BasicMod<ModClass>
    {
        protected override void OnModLoad()
        {
            var world = new EntityStore();
            world.CreateEntity<Velocity>(new Velocity() { val = new(1, 1, 1) });
        }
    }

    struct Velocity : IComponent
    {
        public Vector3 val;
    }
}

What should I do for more detailed information?

friflo commented 1 month ago

To get more info about the TypeInitializationException add a try / catch and log its InnerException property. The log should contain the exception class Name, the Message and StackTrace. In case its InnerException is not null it should also be logged.

inmny commented 1 month ago

Log code:

LogInfo($"{Type.GetType("UnityEngine.Application, UnityEngine")}");
try
{
    _ = new EntityStore();
}
catch (Exception e)
{
    do
    {
        LogInfo($"Name: {e.GetType().Name}\nMessage: {e.Message}\nStackTrack: {e.StackTrace}");
        e = e.InnerException;
    } while (e != null);
}

Log:

[Info   : Unity Log] [NML]: UnityEngine.Application
[Info   : Unity Log] [NML]: Name: TypeInitializationException
Message: The type initializer for 'Static' threw an exception.
StackTrack:   at Friflo.Engine.ECS.EntityStoreBase..ctor () [0x00022] in <2760b6615674441eb080f0fb778cfc66>:0
  at Friflo.Engine.ECS.EntityStore..ctor (Friflo.Engine.ECS.PidType pidType) [0x00000] in <2760b6615674441eb080f0fb778cfc66>:0
  at Friflo.Engine.ECS.EntityStore..ctor () [0x00000] in <2760b6615674441eb080f0fb778cfc66>:0
  at Cultiway.ModClass.OnModLoad () [0x00002] in Source\ModClass.cs:18
[Info   : Unity Log] [NML]: Name: TypeLoadException
Message: Could not resolve type with token 010000aa (from typeref, class/assembly System.Runtime.CompilerServices.RuntimeFeature, netstandard, Version=2.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51)
StackTrack:   at Friflo.Engine.ECS.SchemaUtils.RegisterSchemaTypes (Friflo.Json.Fliox.Mapper.TypeStore typeStore) [0x00000] in <2760b6615674441eb080f0fb778cfc66>:0
  at Friflo.Engine.ECS.EntityStoreBase+Static..cctor () [0x0000a] in <2760b6615674441eb080f0fb778cfc66>:0

It seems that it is running on NativeAOT. But I am sure that it is in unity runtime.

inmny commented 1 month ago

I find its problem: It tries to load "RuntimeFeature" type which is only provided by "System.Runtime" when call SchemaUtils.RegisterComponentTypesByReflection no matter whether it is used.