Friflo.Engine.ECS icon indicating copy to clipboard operation
Friflo.Engine.ECS copied to clipboard

Failed to create EntityStore in runtime compiled code

Open inmny opened this issue 1 year ago • 3 comments

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:

  • 3.0.0-preview.13
  • 3.0.0-preview.10
  • 2.2.0

Dependencies version:

  • Friflo.Json.Burst 1.0.2
  • Friflo.Json.Fliox 1.0.2
  • Friflo.Json.Fliox.Annotation 1.0.2
  • System.Runtime.CompilerServices.Unsafe 5.0.2/6.0.0

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?

inmny avatar Sep 24 '24 03:09 inmny

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.

friflo avatar Sep 24 '24 09:09 friflo

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 avatar Sep 24 '24 13:09 inmny

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.

inmny avatar Sep 24 '24 13:09 inmny