Skip to content

AOT Source Generator

genar edited this page Oct 3, 2023 · 1 revision

Arch itself in its current state is mostly native AOT compatible. However, it still requires some boilerplate-code, especially the registration of components. That's where this extension comes into play.

Automatic AOT-friendly registration

You could register all your components by yourself. But that's a lot of work. AOT.SourceGenerator got you, just mark your components with a [Component] attribute like this:

[Component]
public struct Velocity{ ... }

[Component]
public struct Transform{ ... }

And it will generate a registration-module like this:

namespace Arch.AOT.SourceGenerator
{
   internal static class GeneratedComponentRegistry
   {
      [ModuleInitializer]
      public static void Initialize()
      {
	  ComponentRegistry.Add(new ComponentType(ComponentRegistry.Size + 1, typeof(Velocity), Unsafe.SizeOf<Velocity>(), false);
          ArrayRegistry.Add<Velocity>();
          
          ComponentRegistry.Add(new ComponentType(ComponentRegistry.Size + 1, typeof(Transform), Unsafe.SizeOf<Transform>(), false);
          ArrayRegistry.Add<Transform>();
      }
   }
}

Notice the [ModuleInitializer]? The generated class will automatically be called upon application start to register the components. Pretty cool, right? :)

Clone this wiki locally