GodotPluginsInitializerGenerator.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System.Text;
  2. using Microsoft.CodeAnalysis;
  3. using Microsoft.CodeAnalysis.Text;
  4. namespace Godot.SourceGenerators
  5. {
  6. [Generator]
  7. public class GodotPluginsInitializerGenerator : ISourceGenerator
  8. {
  9. public void Initialize(GeneratorInitializationContext context)
  10. {
  11. }
  12. public void Execute(GeneratorExecutionContext context)
  13. {
  14. if (context.IsGodotToolsProject() || context.IsGodotSourceGeneratorDisabled("GodotPluginsInitializer"))
  15. return;
  16. string source =
  17. @"using System;
  18. using System.Runtime.InteropServices;
  19. using Godot.Bridge;
  20. using Godot.NativeInterop;
  21. namespace GodotPlugins.Game
  22. {
  23. internal static partial class Main
  24. {
  25. [UnmanagedCallersOnly(EntryPoint = ""godotsharp_game_main_init"")]
  26. private static godot_bool InitializeFromGameProject(IntPtr godotDllHandle, IntPtr outManagedCallbacks,
  27. IntPtr unmanagedCallbacks, int unmanagedCallbacksSize)
  28. {
  29. try
  30. {
  31. DllImportResolver dllImportResolver = new GodotDllImportResolver(godotDllHandle).OnResolveDllImport;
  32. var coreApiAssembly = typeof(global::Godot.GodotObject).Assembly;
  33. NativeLibrary.SetDllImportResolver(coreApiAssembly, dllImportResolver);
  34. NativeFuncs.Initialize(unmanagedCallbacks, unmanagedCallbacksSize);
  35. ManagedCallbacks.Create(outManagedCallbacks);
  36. ScriptManagerBridge.LookupScriptsInAssembly(typeof(global::GodotPlugins.Game.Main).Assembly);
  37. return godot_bool.True;
  38. }
  39. catch (Exception e)
  40. {
  41. global::System.Console.Error.WriteLine(e);
  42. return false.ToGodotBool();
  43. }
  44. }
  45. }
  46. }
  47. ";
  48. context.AddSource("GodotPlugins.Game.generated",
  49. SourceText.From(source, Encoding.UTF8));
  50. }
  51. }
  52. }