Source Generators
Source generators are a feature of the Roslyn compiler platform, which is used by C#. They allow developers to generate additional source code during the compilation process. This can be useful for a variety of tasks, such as automatically generating boilerplate code, ensuring compile-time checks on certain conditions, or even creating domain-specific languages within C#.
Here's a basic overview of where source generators fit in the build process:
Initialization: When the compilation process starts, the Roslyn compiler initializes all registered source generators. This is where a source generator can set up any initial state or register callbacks for later stages. For example, in the BlackboardSourceGenerator, the Initialize method registers a syntax receiver to collect certain syntax nodes for later processing.
Generation: After the initial parsing and before the actual compilation of user code into IL (Intermediate Language), the compiler invokes the Execute method of each source generator. At this point, the generator can analyze the code, and based on its logic, generate additional C# source files. These generated files are then compiled along with the user's original source files. In the BlackboardSourceGenerator, the Generate method analyzes collected syntax nodes and generates source files based on them.
Compilation: The generated source files are compiled along with the original source files into the final assembly. Errors or warnings from the source generator can be reported during this phase and will appear alongside other compilation messages.
Output: The output of the compilation process includes the generated source files (which can sometimes be viewed in the IDE for debugging purposes), the compiled assembly, and any diagnostics (errors or warnings) produced during compilation.
Assembly Definitions
Source generators are used to generate the code for the blackboard. This allows you to define the data in a simple way, and have the code generated for you. Source generators in Unity check if they apply to any class "within their reach". Allowing the Blackboard.Generators.dll
access to the complete project will slow down compilation. To mitigate this the source generation dll is placed into a specific assembly definition. This way the source generation only applies to the classes that are within the assembly definition.
Therefore it is important to place the Blackboard.Generators.dll
in the same assembly definition as the classes that use the blackboard, or place any new blackboard classes in the existing assembly definition.
Last updated