Fix retained ADV text in menu layouts

This commit is contained in:
gamer147
2026-07-27 13:29:17 -04:00
parent 8bc60d89ad
commit b2f1b4a45e
11 changed files with 446 additions and 70 deletions

View File

@@ -33,6 +33,37 @@ public class AdvTextOpsTests
Assert.Equal((13, 1, 1, "speaker"), Assert.Single(host.SurfaceStrings));
}
[Fact]
public void ShowTextPublishesDynamicStringOperandsAsAdjacentLiveRuns()
{
var table = OpcodeTableJson.Load(Paths.OpcodesJson);
var script = ScriptAssembler.Assemble(table, "DYNAMIC_ADV_TEXT",
new List<(int, Operand[])>
{
(0x70, new[] { new Operand(0, 1), new Operand(0, 640), new Operand(0, 160),
new Operand(0, 0), new Operand(0, 430) }),
(0x79, new[] { new Operand(0, 1), new Operand(0, 100), new Operand(0, 47) }),
(0x71, new[] { new Operand(0, 1) }),
(0x1b5, new[] { new Operand(0, 0) }),
(0x6e, new[] { new Operand(0, 0), new Operand(2, 0) }),
(0x6e, new[] { new Operand(0, 0), new Operand(5, 0x279) }),
(0x6e, new[] { new Operand(0, 0), new Operand(2, 1) }),
(0x2, Array.Empty<Operand>()),
}, new[] { "「", "!」" });
var host = new RecordingHost();
var vm = new VirtualMachine(script, table, host);
vm.GlobalStrings[0x279] = "リリィ";
vm.Run();
Assert.Equal(new[] { "「", "リリィ", "!」" },
host.LiveTextRuns.Select(emitted => emitted.Run.Text));
Assert.All(host.LiveTextRuns,
emitted => Assert.Equal((0, 430, 100, 47),
(emitted.Run.Layout.OriginX, emitted.Run.Layout.OriginY,
emitted.Run.Layout.CursorX, emitted.Run.Layout.CursorY)));
}
[Fact]
public void LayoutResetRestoresConfiguredCursorAndPreservesConfiguredBounds()
{
@@ -60,6 +91,121 @@ public class AdvTextOpsTests
vm.TextHistory.GetLayoutSnapshot(1));
}
[Fact]
public void SuppressedUiTextPublishesRetainedRunsImmediatelyAndAdvancesLines()
{
var table = OpcodeTableJson.Load(Paths.OpcodesJson);
static Operand I(long value) => new(0, value);
static Operand L(int index) => new(9, index);
static Operand S(int index) => new(2, index);
var script = ScriptAssembler.Assemble(table, "UI_RETAINED_TEXT",
new List<(int, Operand[])>
{
(0x1bb, new[] { I(0) }),
(0x70, new[] { I(7), I(500), I(180), I(0), I(0) }),
(0x79, new[] { I(7), I(53), I(10) }),
(0x71, new[] { I(7) }),
(0x75, new[] { I(16) }),
(0x8b, new[] { I(9) }),
(0x7f, new[] { L(0) }),
(0x1b5, new[] { I(0) }),
(0x198, new[] { I(7), I(275), I(90) }),
(0x6e, new[] { I(0), S(0) }),
(0x6f, new[] { I(0) }),
(0x6e, new[] { I(0), S(1) }),
(0x6f, new[] { I(0) }),
(0x198, new[] { I(7), I(275), I(135) }),
(0x6e, new[] { I(0), S(2) }),
(0x6f, new[] { I(0) }),
(0x6e, new[] { I(0), S(3) }),
(0x6f, new[] { I(0) }),
(0x198, new[] { I(7), I(275), I(180) }),
(0x6e, new[] { I(0), S(4) }),
(0x6f, new[] { I(0) }),
(0x6e, new[] { I(0), S(5) }),
(0x6f, new[] { I(0) }),
(0x1b5, new[] { L(0) }),
(0x1bb, new[] { I(1) }),
(0x2, Array.Empty<Operand>()),
}, new[] { "row1a", "row1b", "row2a", "row2b", "row3a", "row3b" });
var host = new RecordingHost();
var vm = new VirtualMachine(script, table, host);
vm.Run();
Assert.Equal("exit", vm.HaltReason);
Assert.Equal(50, host.MessageGlyphDelayMilliseconds);
Assert.Empty(vm.TextHistory.Entries);
Assert.Empty(vm.TextHistory.Records);
Assert.All(host.LiveTextRuns, emitted => Assert.Equal(0, emitted.GlyphDelayMilliseconds));
Assert.Equal(
new[]
{
(275, 90, 53, 10),
(275, 90, 53, 35),
(275, 135, 53, 60),
(275, 135, 53, 85),
(275, 180, 53, 110),
(275, 180, 53, 135),
},
host.LiveTextRuns.Select(emitted =>
(emitted.Run.Layout.OriginX, emitted.Run.Layout.OriginY,
emitted.Run.Layout.CursorX, emitted.Run.Layout.CursorY)));
Assert.Equal(new[] { "row1a", "row1b", "row2a", "row2b", "row3a", "row3b" },
host.LiveTextRuns.Select(emitted => emitted.Run.Text));
}
[Fact]
public void RealMamesResearchDescriptionUsesImmediateRetainedTextPath()
{
var table = OpcodeTableJson.Load(Paths.OpcodesJson);
var scripts = Sys4ScriptProvider.Load(table);
var mames = scripts.RequireByName("MAMES.BIN");
static Operand I(long value) => new(0, value);
var caller = ScriptAssembler.Assemble(table, "MAMES_RESEARCH_CALLER",
new List<(int, Operand[])>
{
(0x1bb, new[] { I(0) }),
(0x70, new[] { I(7), I(500), I(180), I(0), I(0) }),
(0x79, new[] { I(7), I(53), I(10) }),
(0x71, new[] { I(7) }),
(0x75, new[] { I(16) }),
(0x8b, new[] { I(9) }),
(0x198, new[] { I(7), I(275), I(90) }),
(0x3, new[] { I(mames.PackedId) }),
(0x1bb, new[] { I(1) }),
(0x2, Array.Empty<Operand>()),
}, Array.Empty<string>());
var host = new RecordingHost();
var vm = new VirtualMachine(caller, table, host, provider: scripts);
vm.Globals[0x1560e7] = 7;
vm.Run();
Assert.Equal("exit", vm.HaltReason);
Assert.Equal(50, host.MessageGlyphDelayMilliseconds);
Assert.Empty(vm.TextHistory.Records);
Assert.Collection(host.LiveTextRuns,
first =>
{
Assert.Equal("錬成の研究をして知識を高める", first.Run.Text);
Assert.Equal(0, first.GlyphDelayMilliseconds);
Assert.True(first.Run.BelongsToScript("MAMES_RESEARCH_CALLER"));
Assert.True(first.Run.BelongsToScript("MAMES.BIN"));
Assert.Equal((275, 90, 53, 10),
(first.Run.Layout.OriginX, first.Run.Layout.OriginY,
first.Run.Layout.CursorX, first.Run.Layout.CursorY));
},
second =>
{
Assert.Equal("錬成LVの熟練度が上昇", second.Run.Text);
Assert.Equal(0, second.GlyphDelayMilliseconds);
Assert.Equal((275, 90, 53, 35),
(second.Run.Layout.OriginX, second.Run.Layout.OriginY,
second.Run.Layout.CursorX, second.Run.Layout.CursorY));
});
}
[Fact]
public void System4BootstrapReplaysAllResetCursorAndBoundsConfigurations()
{

View File

@@ -57,11 +57,20 @@ internal class RecordingHost : IHost
public readonly List<string> Warnings = new();
public readonly List<long> CursorResources = new();
public readonly List<bool> AdvPagePresentationSuspended = new();
public readonly List<(AdvLiveTextRun Run, int GlyphDelayMilliseconds)> LiveTextRuns = new();
public readonly Dictionary<int, RgbaImage> SurfacePixels = new();
public int MessageGlyphDelayMilliseconds { get; private set; } = 50;
public int CursorClearCount;
public int SceneContextResets;
public void ReportWarning(string message) => Warnings.Add(message);
public void ShowText(int offset, string text) => Lines.Add((offset, text));
public void ShowText(AdvLiveTextRun run, int glyphDelayMilliseconds)
{
Lines.Add((run.SourceOffset, run.Text));
LiveTextRuns.Add((run, glyphDelayMilliseconds));
}
public void SetMessageGlyphDelayMilliseconds(int milliseconds)
=> MessageGlyphDelayMilliseconds = milliseconds;
public RgbaImage? CaptureSurfacePixels(int slot)
=> SurfacePixels.TryGetValue(slot, out var image) ? image : null;
public bool ReplaceSurfacePixels(int slot, RgbaImage image)

View File

@@ -36,6 +36,10 @@ public interface IHost
void EnterScriptContext(string scriptName) { }
void ExitScriptContext() { }
void ShowText(int offset, string text);
void ShowText(AdvLiveTextRun run, int glyphDelayMilliseconds)
=> ShowText(run.SourceOffset, run.Text);
int MessageGlyphDelayMilliseconds => 50;
void SetMessageGlyphDelayMilliseconds(int milliseconds) { }
// Native ADV text subsystem: op 0x7a updates the selected layout's last 20-byte cursor record;
// op 0x204 rasterizes a string into a numbered surface before 0x1fb binds that surface.
void SetAdvTextCursor(int layoutSlot, int x, int y) { }

View File

@@ -44,6 +44,26 @@ public readonly record struct AdvTextLayoutSnapshot(
int Right,
int Bottom);
/// <summary>
/// One live show-text run with the layout and raster state captured when the opcode executes.
/// Unlike <see cref="AdvTextHistoryRecord"/>, this presentation record exists even while History
/// backlog recording is suppressed.
/// </summary>
public sealed record AdvLiveTextRun(
int SourceOffset,
AdvTextLayoutSnapshot Layout,
AdvTextStyle Style,
string Text,
IReadOnlyList<string> ScriptStack)
{
public bool BelongsToScript(string scriptName)
{
foreach (string frame in ScriptStack)
if (string.Equals(frame, scriptName, StringComparison.OrdinalIgnoreCase)) return true;
return false;
}
}
/// <summary>
/// One semantic counterpart of AGE's 0x48-byte retained text record. Metadata uses
/// <see cref="Value"/> plus <see cref="AuxValue"/> as value/type; voice uses them as id/argument.
@@ -157,6 +177,19 @@ public sealed class AdvTextHistory
layout.OriginY = y;
}
/// <summary>
/// Advance to the next horizontal text line. Native resets x to the layout's configured reset cursor
/// and adds the primary font's pixel height plus the current extra line spacing to y.
/// </summary>
public void EndLine(int requestedSlot, AdvTextStyle style)
{
int slot = ResolveLayout(requestedSlot);
var layout = GetOrCreateLayout(slot);
int fontHeight = style.PrimaryFontSize > 0 ? style.PrimaryFontSize : 24;
layout.CursorX = layout.ResetCursorX;
layout.CursorY += fontHeight + style.LineSpacing;
}
public AdvTextLayoutSnapshot GetLayoutSnapshot(int requestedSlot)
=> SnapshotLayout(ResolveLayout(requestedSlot));

View File

@@ -82,6 +82,7 @@ public sealed class VirtualMachine
private volatile bool _advSkipServiceEnabled;
private bool _advReadSkipState;
private AdvTextStyle _advTextStyle = AdvTextStyle.Default;
private int _messageGlyphDelayMilliseconds;
private readonly Dictionary<string, int> _valueSwitchTargets = new(StringComparer.Ordinal);
// Native EngineCtx owns 11 lazily allocated integer FIFOs at +0x55130. ATSEEK/MVSEEK use
// slot zero as their packed-coordinate flood-fill worklist; op 0x132 replaces a slot.
@@ -125,6 +126,10 @@ public sealed class VirtualMachine
{
get { lock (_interactiveLock) return _rawInputFrame != null; }
}
public string? RawInputCallbackScriptName
{
get { lock (_interactiveLock) return _rawInputFrame?.Script.Name; }
}
public AdvTextHistory TextHistory { get; }
/// <summary>The currently executing recursive script frame and stack, or null outside VM execution.</summary>
@@ -151,6 +156,7 @@ public sealed class VirtualMachine
_sink = sink ?? NullTraceSink.Instance; TextHistory = textHistory ?? new AdvTextHistory();
_sharedProfile = sharedProfile ?? new SharedProfile();
_nativeDatStore = nativeDatStore;
_messageGlyphDelayMilliseconds = System.Math.Max(0, host.MessageGlyphDelayMilliseconds);
_accumulatedPlaySeconds = _sharedProfile.AccumulatedPlaySeconds;
_sessionStartTimestamp = System.Diagnostics.Stopwatch.GetTimestamp();
}
@@ -1808,15 +1814,19 @@ public sealed class VirtualMachine
RefreshAdvReadSkipState();
foreach (var o in a)
{
if (o.Type != T_STR) continue;
int off = (int)o.Value;
if (!IsStr(o)) continue;
int off = o.Type == T_STR ? (int)o.Value : ins.Offset;
_cur.EmitSeen.TryGetValue(off, out var c); c++; _cur.EmitSeen[off] = c;
if (c > _o.EmitCap) { HaltReason = $"LOOP:line@0x{off:x}×{c}"; return HALT; }
string text = _cur.Script.GetString(off);
string text = ReadStr(o);
Emitted.Add((off, text, _cur.Script.Name));
int layoutSlot = a.Count > 0 ? (int)Read(a[0]) : 0;
string[] scriptStack;
lock (_debugControlLock) scriptStack = _activeFrameNames.ToArray();
var liveRun = new AdvLiveTextRun(
off, TextHistory.GetLayoutSnapshot(layoutSlot), _advTextStyle, text, scriptStack);
TextHistory.AppendText(layoutSlot, off, text, _advTextStyle);
_host.ShowText(off, text);
_host.ShowText(liveRun, _messageGlyphDelayMilliseconds);
}
return pc + 1;
case "define-adv-text-layout": // 0x70: configure layout and begin a logical retained group
@@ -2235,10 +2245,20 @@ public sealed class VirtualMachine
case "get-message-window-alpha": // 0x131: host profile/config seam; default host currently supplies 0
case "u00415F70":
Write(a[0], _host.MessageWindowAlphaSetting); return pc + 1;
case "get-message-glyph-delay": // 0x7f
case "u00414C60":
Write(a[0], _messageGlyphDelayMilliseconds); return pc + 1;
case "set-message-glyph-delay": // 0x1b5
case "u0041B5F0":
_messageGlyphDelayMilliseconds = System.Math.Max(0, (int)Read(a[0]));
_host.SetMessageGlyphDelayMilliseconds(_messageGlyphDelayMilliseconds);
return pc + 1;
case "u00415BF0":
case "reset-message-skip-input": // 0x101 clears transient input/run bits, not op 0x88 state
return pc + 1;
case "end-text-line": case "set-font":
case "end-text-line":
TextHistory.EndLine((int)Read(a[0]), _advTextStyle); return pc + 1;
case "set-font":
case "comment": case "display-furigana": case "dev_ukn":
return pc + 1;
case "create-texture": // 0x1f8 (slot)(w)(h) — allocate a blank surface at the slot