LancerLab

Zero-Restart C# Hot-Reload in Unity/Mono: Shipping 15 Live Patches Inside a Running Game

⏱️ ~1 min read

Traditional C# modding for Unity/Mono games on Windows hits a frustrating wall: Assembly.LoadFrom() locks the .dll on disk while the game is running. Testing a two-line fix means closing the client, recompiling, and reloading a save—costing 1–3 minutes per iteration. When debugging an autonomous AI agent in RimWorld 1.6 that breaks 20 minutes into a live colony, that feedback loop kills momentum.

1. RimWorld (4s)WriteLiveSnapshotIfDue2. live_briefing.txt~3.8 KB • ASCII 23×233. .NET 8 SDK BuildLancAI.HotPatch.dll (<2s)4. Assembly.Load(byte[])No file lock • OnHotTick
Zero-Restart Unity/Mono C# Hot-Reload Loop (< 4 seconds)

1. A 4-Second «Game → Agent» Telemetry Loop

Inside LiveTelemetryAndHotReload.cs (73 lines), we paired two lightweight loops. First, WriteLiveSnapshotIfDue overwrites a single live_briefing.txt file (~3.8 KB) every 4 seconds with the agent’s status, goal, pawn coordinates, resources, and a 23×23 ASCII map around the base.

In one run, that ASCII snapshot immediately revealed two colonists sealed inside a room extension with no doors; in another, it exposed a roof grid conflict over a solar panel.

2. Lock-Free Reloading via Assembly.Load(byte[])

Instead of LoadFrom, the main mod watches File.GetLastWriteTimeUtc(HotDllPath) on a separate LancAI.HotPatch.dll (compiled with .NET 8.0.428 SDK targeting net472 in < 2s). Whenever it changes, the bytes are loaded into memory with zero Windows file locks:

public static void CheckAndRunHotPatch(Map map, AIControllerGameComponent ai)
{
    if (!File.Exists(HotDllPath)) return;
    DateTime writeTime = File.GetLastWriteTimeUtc(HotDllPath);
    if (writeTime > _lastDllWriteTime)
    {
        byte[] rawBytes = File.ReadAllBytes(HotDllPath);
        _lastDllWriteTime = writeTime;
        Assembly hotAsm = Assembly.Load(rawBytes);
        Type entryType = hotAsm.GetType("RimWorldLancAIMod.HotPatchEntry");
        _hotTickMethod = entryType?.GetMethod("OnHotTick", BindingFlags.Public | BindingFlags.Static);
    }
    if (_hotTickMethod != null)
    {
        string msg = _hotTickMethod.Invoke(null, new object[] { map, ai }) as string;
        if (!string.IsNullOrEmpty(msg)) ai.RecentResults.Insert(0, msg);
    }
}

HotPatchEntry.OnHotTick(map, ai) can execute live fixes on the active map tick and even apply runtime Harmony patches inside the running process.

3. Outcome: 15 Live Patches (v1–v15) Shipped in One Day Without Restarting

  • v1 / v10: completed missing wall segments and cut 2 doors into a sealed room to free trapped colonists mid-session.
  • v4 / v5: added per-colonist build caps (after 13 campfires & 11 beds) and switched steel counting from stockpile-only to map.listerThings.ThingsOfDef.
  • v8: fixed a Collection was modified crash during un-drafting and cleared a stuck IsThinking flag.
  • v11–v14: relocated crops/solar panels outdoors and cleared stale NoRoof grid flags.
  • v15: stopped infinite BedRest loops triggered by minor chronic asthma.

The entire round-trip—from spotting a failure in live_briefing.txt to seeing the fix run inside the live game—took under 4 seconds.

More from the Lab
← Teaching a Local 24B LLM to Play RimWorld on an RTX 3090: Sockets, Bugs, and Macro ControlNotebookLM Site Search & RTX 3090 Auto Events Calendar for KNUBA Library →

Комментарии

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *