Files
Workspace/Godot/Tests/CodeGenFixture/Program.cs
T
cneicy 88ba3cdc48
Validate ShrinkSDK Workspace / unity (push) Failing after 3m18s
feat: add Godot C# compatibility and shared codegen weaving
2026-09-05 02:37:43 +08:00

80 lines
3.2 KiB
C#

#nullable enable
using System;
using System.Linq;
using ShrinkCommand;
using ShrinkEventBus;
using ShrinkNetwork;
using ShrinkNetwork.Integration;
using ShrinkSDK.Runtime;
var assembly = typeof(Program).Assembly;
var marker = assembly.GetCustomAttributes(typeof(ShrinkCodeGenWovenAttribute), false)
.Cast<ShrinkCodeGenWovenAttribute>()
.SingleOrDefault() ?? throw new InvalidOperationException("CodeGen marker is missing.");
if (marker.WeaverVersion != "0.1.0" || !Guid.TryParse(marker.InputMvid, out _))
throw new InvalidOperationException("CodeGen marker is invalid.");
if (!typeof(InstanceSubscriber).GetInterfaces().Contains(typeof(IShrinkGeneratedSubscriber)))
throw new InvalidOperationException("Generated subscriber interface is missing.");
using var binding = EventBus.Attach(new InstanceSubscriber());
EventBus.Post(new FixtureEvent());
if (InstanceSubscriber.Calls != 1 || StaticSubscriber.Calls != 1)
throw new InvalidOperationException($"Generated EventBus bindings failed: instance={InstanceSubscriber.Calls}, static={StaticSubscriber.Calls}.");
if (assembly.GetCustomAttributes(typeof(ShrinkCommandStaticRegistryAttribute), false).Length != 1)
throw new InvalidOperationException("Command registry metadata is missing.");
if (assembly.GetCustomAttributes(typeof(ShrinkNetworkMessageRegistryAttribute), false).Length != 1)
throw new InvalidOperationException("Network message registry metadata is missing.");
if (assembly.GetCustomAttributes(typeof(ShrinkNetworkStaticSubscriberRegistryAttribute), false).Length != 1)
throw new InvalidOperationException("Network subscriber registry metadata is missing.");
var bridgeRegistrations = (System.Collections.IEnumerable)(typeof(ShrinkNetworkEventRegistry)
.GetMethod("Snapshot", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static)!
.Invoke(null, null) ?? throw new InvalidOperationException("Network EventBus registry snapshot failed."));
if (!bridgeRegistrations.Cast<object>().Any(item => item.GetType().GetProperty("EventType")?.GetValue(item) as Type == typeof(FixtureNetworkEvent)))
throw new InvalidOperationException("Generated Network/EventBus binding is missing.");
Console.WriteLine($"PASS CodeGen {marker.WeaverVersion}: EventBus + Command + Network + Network/EventBus registries");
public readonly struct FixtureEvent : IShrinkEvent { }
[ShrinkEventSubscriber]
public sealed class InstanceSubscriber
{
public static int Calls;
[ShrinkSubscribe]
private void OnEvent(FixtureEvent value) => Calls++;
}
[ShrinkEventSubscriber]
public static class StaticSubscriber
{
public static int Calls;
[ShrinkSubscribe]
private static void OnEvent(FixtureEvent value) => Calls++;
}
[ShrinkCommandSubscriber]
public static class FixtureCommands
{
[ShrinkCommand("fixture/ping")]
public static void Ping() { }
}
[ShrinkNetworkMessage(41001, "fixture/ping")]
public sealed class FixtureMessage : IShrinkNetworkMessage { }
[ShrinkNetworkEvent]
[ShrinkNetworkMessage(41002, "fixture/event")]
public sealed class FixtureNetworkEvent : IShrinkEvent, IShrinkNetworkMessage { }
[ShrinkNetworkSubscriber]
public static class FixtureNetworkHandlers
{
[ShrinkNetworkSubscribe]
public static void Handle(FixtureMessage message) { }
}