Jump to content

Unity.Multiplayer/rfc/0002-generator-spike

From MindAttic Docs

RFC 0002: Generator spike (UM-T4)

Decision: GO. A Roslyn source generator can write the NGO RPC stubs and NetworkVariables. UM-T11 builds the real generator. The fallback (hand-written stubs in the generator's shape, never reflection) is not needed. UM-T10 still hand-writes its stubs in that shape, as RFC 0001 planned, and UM-T11 replaces them.

Question

Can a Roslyn IIncrementalGenerator turn a C# 9 extended partial method into an NGO [Rpc(SendTo.Server)] stub, and a marked field into a NetworkVariable, in a way that NGO's IL post-processor (ILPP) accepts and that still runs in an IL2CPP Linux Dedicated Server build? If not, UM-T11 falls back to hand-written stubs.

Setup

The throwaway project is spikes/GeneratorSpike/. It uses Unity 6000.4.3f1, NGO 2.11.0 and Transport 2.7.2, the same versions as package.json.

Piece What it is
Generator/ MindAttic.Spike.Generators: an IIncrementalGenerator, netstandard2.0, Microsoft.CodeAnalysis.CSharp 4.3.1, LangVersion 9
Unity/Assets/Plugins/Generators/MindAttic.Spike.Generators.dll.meta labels: [RoslynAnalyzer], every platform unticked (Any and Editor enabled: 0, all standalone targets excluded)
Unity/Assets/Spike/Scripts/SpikeEntity.cs The game-side code: [ToServer] public partial void Ping(int amount);, its handler void OnPing(int amount, ulong sender), and [Synced] int _total;. It contains no NGO plumbing.
Unity/Assets/Spike/Tests/SpikeRpcTests.cs A NetcodeIntegrationTest PlayMode test: a host and 1 client in one process
Unity/Assets/Spike/Scripts/SpikeBootstrap.cs A command-line driver for the standalone players (-spikeMode server|host|client)
Unity/Assets/Spike/Editor/SpikeBuild.cs Batch-mode entry points: scene/prefab setup, the IL2CPP Linux server build, the Mono Windows player build
run-spike.ps1 Runs every step below in order and exits non-zero at the first failure

The generator wrote this for SpikeEntity. Unity does not save generated sources to disk, so this copy was captured by running the same generator through CSharpGeneratorDriver:

// <auto-generated>MindAttic UM-T4 spike generator</auto-generated>
#pragma warning disable
namespace MindAttic.Spike
{
    partial class SpikeEntity
    {
        public partial void Ping(int amount)
        {
            PingRpc(amount);
        }

        [global::Unity.Netcode.Rpc(global::Unity.Netcode.SendTo.Server)]
        private void PingRpc(int amount, global::Unity.Netcode.RpcParams rpcParams = default)
        {
            OnPing(amount, rpcParams.Receive.SenderClientId);
        }

        private global::Unity.Netcode.NetworkVariable<int> __TotalNetVar = new global::Unity.Netcode.NetworkVariable<int>();
        public global::Unity.Netcode.NetworkVariable<int> TotalVariable { get { return __TotalNetVar; } }
        public int Total { get { return __TotalNetVar.Value; } set { __TotalNetVar.Value = value; } }
    }
}

Results (2026-10-20, all on the owner's Windows 11 machine)

Every check passed. Each step passed when run by hand, and run-spike.ps1 then passed end to end from a clean build.

# Check Evidence Result
1 Unity loads the generator from a RoslynAnalyzer-labelled DLL and compiles the extended partial method MindAttic.Spike.Game compiles. Without the generator it would stop with CS8795 (partial method has no implementation). ✅
2 NGO ILPP processes the generated RPC and NetworkVariable Library/ScriptAssemblies/MindAttic.Spike.Game.dll contains __rpc_handler_1094476955 and __initializeVariables, which NGO's NetworkBehaviourILPP emits ✅
3 A host and 1 client exchange it (in one process) SpikeRpcTests.GeneratedServerRpcAndNetworkVariableRoundTrip passed (1/1): the client called Ping(7) and Ping(5), the server's OnPing ran twice with sender = client 1, Total = 12 replicated back, and OnPing never ran on the client ✅
4 A host and 1 client exchange it (two Mono Windows player processes over UDP) The host logged Ping(7) from client 1 -> Total=7, the client logged PASS client: Total=7 replicated, and both exited 0 ✅
5 An IL2CPP Linux Dedicated Server build runs it StandaloneLinux64, subtarget Server, IL2CPP: the build succeeded (about 77 s) and il2cppOutput holds the RPC handler. Run in ubuntu:22.04 under Docker with UDP 7777 published, joined by the Windows Mono client: the server logged platform=LinuxServer backend=IL2CPP … Ping(7) from client 1 -> Total=7 … PASS server, and both sides exited 0 ✅

Findings UM-T8, UM-T11 and UC-T13 inherit

  • Generator shape works as designed. Write the partial body that calls <Name>Rpc, plus [Rpc(SendTo.<Target>)] void <Name>Rpc(args, RpcParams rpcParams = default) that calls On<Name>(args, sender). ILPP needs the RPC name to end in Rpc. The trailing RpcParams parameter gives the sender id for free (rpcParams.Receive.SenderClientId).
  • The partial implementation must repeat the definition's modifiers exactly. The spike copies them from the declaring syntax.
  • Generated NetworkVariable fields need no special handling. ILPP initializes them like hand-written ones. The spike kept the user's [Synced] field as a marker only (CS0169 suppressed). UM-T11 should either make the analyzer reject direct use of that field (already planned) or turn the marker into a property-style declaration.
  • The NGO test helpers live in the Unity.Netcode.Runtime.Tests assembly in NGO 2.11.0. There is no Unity.Netcode.TestHelpers.Runtime assembly in this version, although the package's own asmdef still lists that name. A PlayMode test asmdef references Unity.Netcode.Runtime.Tests and needs "testables": ["com.unity.netcode.gameobjects"] in the project manifest. This applies to UM-T8's test setup.
  • IL2CPP Linux from a Windows editor needs two toolchain packages in the building project's manifest: com.unity.toolchain.win-x86_64-linux 1.1.0 (the 6000.3+ toolchain) and com.unity.sysroot.linux-x86_64 2.0.10. Without them the build fails with "No Toolchain found for host platform". These belong to whichever project builds the server (UnityCursory, CI), not to the package. UM-T15's "Build Dedicated Server (Linux)" menu should check for them and say so clearly.
  • A Mono client and an IL2CPP server interoperate. The ILPP-generated RPC hashes match across backends and platforms.
  • Running the Linux server locally needs Docker's Linux engine. The owner's Docker Desktop defaults to Windows containers, so run-spike.ps1 switches to the Linux engine for the server step and back to Windows afterwards.
  • Not tested here; checked in UM-T11: the generator placed inside the package (Runtime/Generators/, under the adapter's asmdef) instead of under Assets/. Unity applies an analyzer in an asmdef's folder to that assembly and to the assemblies that reference it. UM-T11 confirmed it: the PlayMode test assembly (MindAttic.Unity.Multiplayer.Tests, which references the adapter) compiles and passes with its TestAvatar plumbing written only by the in-package generator.

Reproduce

pwsh -File spikes/GeneratorSpike/run-spike.ps1          # or powershell.exe; -SkipDocker skips the Linux run

Logs and the test results XML are written to spikes/GeneratorSpike/logs/, which git ignores. The spike is throwaway, and nothing in the package depends on it.

From Unity.Multiplayer/docs/rfc/0002-generator-spike.md via MindAttic.Docs; synced automatically, so edit the repo, not this page.