Wire asset resolution into a live Godot render. VM executes set-texture -> ResourceMap resolves (scene,resId) -> files[section_base+resId] across all archives -> pre-converted BMP -> composite behind the dialogue. The full-screen event-CG layer (EV052*) renders end-to-end from the executed bytecode. - Age.Engine/Sys4/ResourceMap.cs: Resolve + BMP TexturePath; Paths: asset JSONs - GodotAdvHost: create/set/draw-texture -> TextureRect in a _stage layer - IHost.DrawTexture + VM dispatch extended with dst x/y (draw-texture args 7/8) - project.godot 800x600; convert_agf.py all-archive + --scene batch - engine 8/8, C# --selftest still byte-matches vm0 trace (VM behaviour unchanged) Known limitations (next chunk = graphics geometry/blend subsystem): - sprites + BG* via the CG-load subroutine get garbage dst/size — native ops stubbed (0x208 get-texture-size + sprite position/anim chain) - AE* fades draw opaque/instant (no alpha); no chromakey - slot model approximates the game's immediate-mode blit-onto-slot-0 canvas - AGF pre-converted to BMP offline (runtime decoder deferred) See docs/phase-a-slice-plan.md (A2b section). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
139 lines
5.7 KiB
C#
139 lines
5.7 KiB
C#
using System.Collections.Generic;
|
|
using System.Text.Json;
|
|
using System.Threading.Tasks;
|
|
using Godot;
|
|
using Age.Engine.Sys4;
|
|
using Age.Engine.Vm;
|
|
|
|
public partial class Main : Godot.Control
|
|
{
|
|
private Control _stage = null!; // texture layer (behind the text)
|
|
private readonly Dictionary<int, TextureRect> _slots = new();
|
|
private Label _text = null!;
|
|
private Label _status = null!;
|
|
private VirtualMachine _vm = null!;
|
|
private GodotAdvHost _host = null!;
|
|
private volatile bool _done;
|
|
private bool _ended;
|
|
private bool _selftest;
|
|
|
|
public override void _Ready()
|
|
{
|
|
// texture stage, added first so it draws BEHIND the dialogue text
|
|
_stage = new Control();
|
|
_stage.SetAnchorsAndOffsetsPreset(LayoutPreset.FullRect);
|
|
_stage.MouseFilter = MouseFilterEnum.Ignore;
|
|
AddChild(_stage);
|
|
|
|
_text = new Label { AutowrapMode = TextServer.AutowrapMode.WordSmart };
|
|
_text.SetAnchorsAndOffsetsPreset(LayoutPreset.FullRect);
|
|
_text.OffsetLeft = 40; _text.OffsetTop = 40; _text.OffsetRight = -40; _text.OffsetBottom = -80;
|
|
AddChild(_text);
|
|
_status = new Label();
|
|
_status.SetAnchorsAndOffsetsPreset(LayoutPreset.BottomWide);
|
|
_status.OffsetLeft = 40; _status.OffsetTop = -60;
|
|
AddChild(_status);
|
|
|
|
// Best-effort CJK font so the visual isn't tofu (headless self-test doesn't depend on it).
|
|
foreach (var fp in new[] { "C:/Windows/Fonts/YuGothM.ttc", "C:/Windows/Fonts/YuGothR.ttc",
|
|
"C:/Windows/Fonts/msgothic.ttc", "C:/Windows/Fonts/meiryo.ttc" })
|
|
{
|
|
if (!System.IO.File.Exists(fp)) continue;
|
|
try
|
|
{
|
|
var ff = new FontFile { Data = System.IO.File.ReadAllBytes(fp) };
|
|
_text.AddThemeFontOverride("font", ff);
|
|
_status.AddThemeFontOverride("font", ff);
|
|
_text.AddThemeFontSizeOverride("font_size", 22);
|
|
break;
|
|
}
|
|
catch { /* fall back to the default font */ }
|
|
}
|
|
|
|
_selftest = System.Array.IndexOf(OS.GetCmdlineUserArgs(), "--selftest") >= 0;
|
|
|
|
var table = OpcodeTableJson.Load(Paths.OpcodesJson);
|
|
var script = Sys4Loader.Load(Paths.Scripts()["SC0000.BIN"], table);
|
|
_host = new GodotAdvHost(this, ResourceMap.Load(), "SC0000");
|
|
_vm = new VirtualMachine(script, table, _host);
|
|
_ = Task.Run(() => { _vm.Run(); _done = true; });
|
|
|
|
if (_selftest)
|
|
_ = Task.Run(async () => { while (!_done) { if (_host.IsWaiting) _host.SignalInput(); await Task.Delay(1); } });
|
|
}
|
|
|
|
public override void _Process(double delta)
|
|
{
|
|
if (_done && !_ended)
|
|
{
|
|
_ended = true;
|
|
ShowEnd();
|
|
if (_selftest) RunSelfTest();
|
|
}
|
|
}
|
|
|
|
// _Input (not _UnhandledInput): the root Control consumes mouse clicks as GUI input before they
|
|
// reach _UnhandledInput, so clicks were swallowed while keyboard ui_accept still got through.
|
|
public override void _Input(InputEvent e)
|
|
{
|
|
if (_selftest) return;
|
|
if (e.IsActionPressed("ui_accept") ||
|
|
(e is InputEventMouseButton mb && mb.Pressed && mb.ButtonIndex == MouseButton.Left))
|
|
_host.SignalInput();
|
|
}
|
|
|
|
public override void _ExitTree() { _host?.SignalInput(); }
|
|
|
|
// ---- UI methods invoked on the main thread via CallDeferred ----
|
|
// Composite a resolved texture into a slot at (x,y) sized (w,h). One TextureRect per slot,
|
|
// layered in draw order (backgrounds are drawn before sprites, so they sit behind).
|
|
public void DrawSlot(int slot, string bmpPath, int x, int y, int w, int h)
|
|
{
|
|
var img = new Image();
|
|
var err = img.LoadBmpFromBuffer(System.IO.File.ReadAllBytes(bmpPath));
|
|
if (err != Error.Ok) { GD.Print($"BMP load failed {bmpPath}: {err}"); return; }
|
|
if (!_slots.TryGetValue(slot, out var tr))
|
|
{
|
|
tr = new TextureRect
|
|
{
|
|
ExpandMode = TextureRect.ExpandModeEnum.IgnoreSize,
|
|
StretchMode = TextureRect.StretchModeEnum.Scale,
|
|
MouseFilter = MouseFilterEnum.Ignore,
|
|
};
|
|
_stage.AddChild(tr);
|
|
_slots[slot] = tr;
|
|
}
|
|
tr.Texture = ImageTexture.CreateFromImage(img);
|
|
tr.Position = new Vector2(x, y);
|
|
tr.Size = new Vector2(w > 0 ? w : img.GetWidth(), h > 0 ? h : img.GetHeight());
|
|
tr.Visible = true;
|
|
}
|
|
|
|
public void AppendLine(string text) => _text.Text += text + "\n";
|
|
public void PageBreak() => _status.Text = "▼ click / Enter";
|
|
public void ClearPage() { _text.Text = ""; _status.Text = ""; }
|
|
public void ShowEnd() => _status.Text = "— end —";
|
|
|
|
private void RunSelfTest()
|
|
{
|
|
var expected = LoadExpected("SC0000.BIN");
|
|
var actual = _host.Captured.ConvertAll(c => c.Offset);
|
|
bool ok = expected != null && actual.Count == expected.Count;
|
|
for (int i = 0; ok && i < actual.Count; i++) ok = actual[i] == expected![i];
|
|
if (ok) GD.Print($"SELFTEST OK: {actual.Count} lines match vm0 trace");
|
|
else GD.Print($"SELFTEST FAIL: cs={actual.Count} expected={(expected?.Count.ToString() ?? "n/a")}");
|
|
GetTree().Quit(ok ? 0 : 1);
|
|
}
|
|
|
|
private static List<int>? LoadExpected(string scene)
|
|
{
|
|
string p = System.IO.Path.Combine(Paths.Build, "vm0-trace.json");
|
|
if (!System.IO.File.Exists(p)) return null;
|
|
using var doc = JsonDocument.Parse(System.IO.File.ReadAllText(p));
|
|
if (!doc.RootElement.TryGetProperty(scene, out var e)) return null;
|
|
var list = new List<int>();
|
|
foreach (var x in e.GetProperty("offsets").EnumerateArray()) list.Add(x.GetInt32());
|
|
return list;
|
|
}
|
|
}
|