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. 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 tomap.listerThings.ThingsOfDef.v8: fixed aCollection was modifiedcrash during un-drafting and cleared a stuckIsThinkingflag.v11–v14: relocated crops/solar panels outdoors and cleared staleNoRoofgrid flags.v15: stopped infiniteBedRestloops 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.
Добавить комментарий