public void Execute(GeneratorExecutionContext context) { if (context.AreGodotSourceGeneratorsDisabled()) { return; } if (context.IsGodotToolsProject()) { return; } // NOTE: NotNullWhen diagnostics don't work on projects targeting .NET Standard 2.0 // ReSharper disable once ReplaceWithStringIsNullOrEmpty if (!context.TryGetGlobalAnalyzerProperty("GodotProjectDir", out string?godotProjectDir) || godotProjectDir !.Length == 0) { throw new InvalidOperationException("Property 'GodotProjectDir' is null or empty."); } Dictionary <INamedTypeSymbol, IEnumerable <ClassDeclarationSyntax> > godotClasses = context .Compilation.SyntaxTrees .SelectMany(tree => tree.GetRoot().DescendantNodes() .OfType <ClassDeclarationSyntax>() // Ignore inner classes .Where(cds => !cds.IsNested()) .SelectGodotScriptClasses(context.Compilation) // Report and skip non-partial classes .Where(x => { if (x.cds.IsPartial()) { return(true); } Common.ReportNonPartialGodotScriptClass(context, x.cds, x.symbol); return(false); }) ) // Ignore classes whose name is not the same as the file name .Where(x => Path.GetFileNameWithoutExtension(x.cds.SyntaxTree.FilePath) == x.symbol.Name) .GroupBy(x => x.symbol) .ToDictionary(g => g.Key, g => g.Select(x => x.cds)); foreach (var godotClass in godotClasses) { VisitGodotScriptClass(context, godotProjectDir, symbol: godotClass.Key, classDeclarations: godotClass.Value); } if (godotClasses.Count <= 0) { return; } AddScriptTypesAssemblyAttr(context, godotClasses); }
public void Execute(GeneratorExecutionContext context) { if (context.IsGodotToolsProject()) { return; } string source = @"using System; using System.Runtime.InteropServices; using Godot.Bridge; using Godot.NativeInterop; namespace GodotPlugins.Game { internal static partial class Main { [UnmanagedCallersOnly(EntryPoint = ""godotsharp_game_main_init"")] private static godot_bool InitializeFromGameProject(IntPtr godotDllHandle, IntPtr outManagedCallbacks, IntPtr unmanagedCallbacks, int unmanagedCallbacksSize) { try { DllImportResolver dllImportResolver = new GodotDllImportResolver(godotDllHandle).OnResolveDllImport; var coreApiAssembly = typeof(Godot.Object).Assembly; NativeLibrary.SetDllImportResolver(coreApiAssembly, dllImportResolver); NativeFuncs.Initialize(unmanagedCallbacks, unmanagedCallbacksSize); ManagedCallbacks.Create(outManagedCallbacks); ScriptManagerBridge.LookupScriptsInAssembly(typeof(GodotPlugins.Game.Main).Assembly); return godot_bool.True; } catch (Exception e) { Console.Error.WriteLine(e); return false.ToGodotBool(); } } } } "; context.AddSource("GodotPlugins.Game_Generated", SourceText.From(source, Encoding.UTF8)); }