2021-12-28 22:25:16 +00:00
|
|
|
using System.Text;
|
|
|
|
using Microsoft.CodeAnalysis;
|
|
|
|
using Microsoft.CodeAnalysis.Text;
|
|
|
|
|
|
|
|
namespace Godot.SourceGenerators
|
|
|
|
{
|
|
|
|
[Generator]
|
|
|
|
public class GodotPluginsInitializerGenerator : ISourceGenerator
|
|
|
|
{
|
|
|
|
public void Initialize(GeneratorInitializationContext context)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2022-02-27 20:57:52 +00:00
|
|
|
[UnmanagedCallersOnly(EntryPoint = ""godotsharp_game_main_init"")]
|
2022-08-29 15:37:49 +00:00
|
|
|
private static godot_bool InitializeFromGameProject(IntPtr godotDllHandle, IntPtr outManagedCallbacks,
|
|
|
|
IntPtr unmanagedCallbacks, int unmanagedCallbacksSize)
|
2021-12-28 22:25:16 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2022-02-27 20:57:52 +00:00
|
|
|
DllImportResolver dllImportResolver = new GodotDllImportResolver(godotDllHandle).OnResolveDllImport;
|
|
|
|
|
2022-12-07 15:16:51 +00:00
|
|
|
var coreApiAssembly = typeof(global::Godot.GodotObject).Assembly;
|
2021-12-28 22:25:16 +00:00
|
|
|
|
2022-02-27 20:57:52 +00:00
|
|
|
NativeLibrary.SetDllImportResolver(coreApiAssembly, dllImportResolver);
|
2021-12-28 22:25:16 +00:00
|
|
|
|
2022-08-29 15:37:49 +00:00
|
|
|
NativeFuncs.Initialize(unmanagedCallbacks, unmanagedCallbacksSize);
|
|
|
|
|
2021-12-28 22:25:16 +00:00
|
|
|
ManagedCallbacks.Create(outManagedCallbacks);
|
|
|
|
|
2022-12-07 15:16:51 +00:00
|
|
|
ScriptManagerBridge.LookupScriptsInAssembly(typeof(global::GodotPlugins.Game.Main).Assembly);
|
2021-12-28 22:25:16 +00:00
|
|
|
|
|
|
|
return godot_bool.True;
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2023-01-31 20:56:20 +00:00
|
|
|
global::System.Console.Error.WriteLine(e);
|
2021-12-28 22:25:16 +00:00
|
|
|
return false.ToGodotBool();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
";
|
|
|
|
|
2022-10-22 21:13:52 +00:00
|
|
|
context.AddSource("GodotPlugins.Game.generated",
|
2021-12-28 22:25:16 +00:00
|
|
|
SourceText.From(source, Encoding.UTF8));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|