Fix Sky Mower tumbling and the hitches on a big farm

- Mowbot keeps its heading apart from the drawn pose: a flyer's lean and sway
  were fed back each frame and rolled it over.
- Drone pads looked through every loose bundle, testing every belt end, for
  each idle drone every frame (43 ms on a 1500-bundle farm): distance first,
  and a 1.5 s pause after finding nothing.
- Saves are written, laid out and compressed off the main thread; backups go
  in the same queue (an autosave froze the game ~50 ms every 30 s).
- Robots tick on staggered frames.
- -demoperf N profiles save slot N; -demobots logs the Sky Mower's tilt.

On the player's save: frames over 25 ms went from 144 to 0 in 40 s, worst
frame 74 ms -> 22 ms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
NiemandKG
2026-11-09 19:04:36 +06:00
parent 27d075103b
commit 80271aea72
9 changed files with 251 additions and 37 deletions

View File

@ -99,14 +99,41 @@ namespace GrassCutter
// ───────────────────────── writing ─────────────────────────
public static void Write(string json, byte[] map)
// the disk work of a save runs off the main thread, one write after another: a big farm's mow map is over ten
// megabytes, and writing it in the frame froze the game every half minute
static System.Threading.Tasks.Task s_io = System.Threading.Tasks.Task.CompletedTask;
static readonly object s_ioGate = new object();
public static void Write(string json, byte[] map) => Write(() => json, () => map);
/// <summary>The save, its text and its map put together off the main thread (JsonUtility works on any thread).</summary>
public static void Write(Func<string> json, Func<byte[]> map)
{
var dir = Folder(Current);
Directory.CreateDirectory(dir);
Put(Path.Combine(dir, GameFile), Encoding.UTF8.GetBytes(json));
Put(Path.Combine(dir, MapFile), map);
lock (s_ioGate)
s_io = s_io.ContinueWith(_ =>
{
try
{
Directory.CreateDirectory(dir);
Put(Path.Combine(dir, GameFile), Encoding.UTF8.GetBytes(json()));
Put(Path.Combine(dir, MapFile), map());
}
catch (Exception e) { Debug.LogWarning("[Save] couldn't write " + dir + ": " + e.Message); }
});
}
/// <summary>Waits for a save still being written: before anything reads, copies or deletes the files, and on quitting.</summary>
public static void Flush()
{
System.Threading.Tasks.Task t;
lock (s_ioGate) t = s_io;
try { t.Wait(); } catch (AggregateException e) { Debug.LogWarning("[Save] " + e.InnerException?.Message); }
}
[RuntimeInitializeOnLoadMethod]
static void FlushOnQuit() => Application.quitting += Flush;
/// <summary>Written whole or not at all: a crash halfway leaves the last save as it was.</summary>
static void Put(string path, byte[] data)
{
@ -119,18 +146,31 @@ namespace GrassCutter
/// <summary>The current save goes over the oldest of its backups.</summary>
public static void Backup()
{
// in the same queue as the writes, after the save just asked for: the copy is of that save, and nothing waits
var dir = Folder(Current);
var copies = new string[Backups];
for (int n = 0; n < Backups; n++) copies[n] = Copy(Current, n);
lock (s_ioGate)
s_io = s_io.ContinueWith(_ =>
{
try { CopyToBackup(dir, copies); }
catch (Exception e) { Debug.LogWarning("[Save] couldn't back up " + dir + ": " + e.Message); }
});
}
static void CopyToBackup(string dir, string[] copies)
{
var game = Path.Combine(dir, GameFile);
if (!File.Exists(game)) return;
int pick = 0;
var oldest = DateTime.MaxValue;
for (int n = 0; n < Backups; n++)
for (int n = 0; n < copies.Length; n++)
{
var path = Path.Combine(Copy(Current, n), GameFile);
var path = Path.Combine(copies[n], GameFile);
var when = File.Exists(path) ? File.GetLastWriteTimeUtc(path) : DateTime.MinValue;
if (when < oldest) { oldest = when; pick = n; }
}
var to = Copy(Current, pick);
var to = copies[pick];
Directory.CreateDirectory(to);
File.Copy(game, Path.Combine(to, GameFile), true);
File.SetLastWriteTimeUtc(Path.Combine(to, GameFile), DateTime.UtcNow);
@ -170,6 +210,7 @@ namespace GrassCutter
/// <summary>The save to load, or null for a new game. A bad one is replaced from its backups first.</summary>
public static string Read()
{
Flush();
Migrate();
var dir = Folder(Current);
var json = Text(Path.Combine(dir, GameFile));
@ -194,6 +235,7 @@ namespace GrassCutter
public static SlotInfo Info(int slot)
{
Flush();
var path = Path.Combine(Folder(slot), GameFile);
var h = Parse(Text(path));
if (h == null) return new SlotInfo { used = File.Exists(path) };
@ -202,6 +244,7 @@ namespace GrassCutter
public static void Delete(int slot)
{
Flush();
if (Directory.Exists(Folder(slot))) Directory.Delete(Folder(slot), true);
}