Implement diagnostic output opcodes
This commit is contained in:
@@ -2,6 +2,8 @@ namespace Age.Engine.Hosting;
|
||||
public sealed class CaptureHost : IHost
|
||||
{
|
||||
public List<(int Offset, string Text)> Emitted { get; } = new();
|
||||
public List<DiagnosticMessage> Diagnostics { get; } = new();
|
||||
public void ShowDiagnosticMessage(DiagnosticMessage message) => Diagnostics.Add(message);
|
||||
public void ShowText(int offset, string text) => Emitted.Add((offset, text));
|
||||
public void WaitForInput() { }
|
||||
public void Sleep(long duration) { }
|
||||
|
||||
@@ -28,6 +28,9 @@ public readonly record struct SurfaceRectCopy(
|
||||
int SourceSurface, int DestinationSurface, int SourceX, int SourceY,
|
||||
int Width, int Height, int DestinationX, int DestinationY);
|
||||
|
||||
/// <summary>A synchronous AGE-owned diagnostic prompt after native body/context formatting.</summary>
|
||||
public readonly record struct DiagnosticMessage(string Caption, string Text);
|
||||
|
||||
public enum SurfaceBlackFadeDirection
|
||||
{
|
||||
FromBlack,
|
||||
@@ -38,6 +41,9 @@ public interface IHost
|
||||
{
|
||||
/// <summary>Report a recoverable runtime discrepancy while allowing script execution to continue.</summary>
|
||||
void ReportWarning(string message) => System.Console.Error.WriteLine(message);
|
||||
/// <summary>Present a modal diagnostic and return only after the user dismisses it.</summary>
|
||||
void ShowDiagnosticMessage(DiagnosticMessage message)
|
||||
=> System.Console.Error.WriteLine($"{message.Caption}: {message.Text}");
|
||||
// Script context is retained for diagnostics/page location; resource operands are universal packed ids.
|
||||
void EnterScriptContext(string scriptName) { }
|
||||
void ExitScriptContext() { }
|
||||
|
||||
17
engine/Age.Engine/Vm/DiagnosticOutputState.cs
Normal file
17
engine/Age.Engine/Vm/DiagnosticOutputState.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using System.Text;
|
||||
|
||||
namespace Age.Engine.Vm;
|
||||
|
||||
/// <summary>
|
||||
/// EngineCtx-lifetime accumulator used by opcodes 0x1b2 through 0x1b4. GameSession shares this
|
||||
/// state across fresh scene VMs just as native AGE retains the embedded string across script frames.
|
||||
/// </summary>
|
||||
public sealed class DiagnosticOutputState
|
||||
{
|
||||
private readonly StringBuilder _text = new();
|
||||
|
||||
public string PendingText => _text.ToString();
|
||||
|
||||
internal void Append(string value) => _text.Append(value);
|
||||
internal void Clear() => _text.Clear();
|
||||
}
|
||||
@@ -32,6 +32,8 @@ public sealed class GameSession
|
||||
public AudioMixerSettings AudioMixerSettings { get; }
|
||||
/// <summary>The live retained ADV backlog shared by every VM run in this session.</summary>
|
||||
public AdvTextHistory TextHistory { get; } = new();
|
||||
/// <summary>The EngineCtx-lifetime diagnostic accumulator shared across script/scene VMs.</summary>
|
||||
public DiagnosticOutputState DiagnosticOutput { get; } = new();
|
||||
|
||||
public GameSession(SharedProfile? sharedProfile = null, INativeDatStore? nativeDatStore = null,
|
||||
AudioMixerSettings? audioMixerSettings = null)
|
||||
@@ -51,7 +53,7 @@ public sealed class GameSession
|
||||
{
|
||||
var vm = new VirtualMachine(
|
||||
script, table, host, options, provider, sink, TextHistory, SharedProfile, NativeDatStore,
|
||||
AudioMixerSettings);
|
||||
AudioMixerSettings, DiagnosticOutput);
|
||||
foreach (var kv in Globals) vm.Globals[kv.Key] = kv.Value;
|
||||
foreach (var kv in GlobalFloats) vm.GlobalFloats[kv.Key] = kv.Value;
|
||||
foreach (var kv in GlobalStrings) vm.GlobalStrings[kv.Key] = kv.Value;
|
||||
|
||||
@@ -29,9 +29,10 @@ public sealed class VirtualMachine
|
||||
private const int HOTSPOT_RETURN = int.MinValue + 2;
|
||||
private const int ROOT_RELOAD = int.MinValue + 3;
|
||||
private const int SceneEntryCoroutineGate = 0xaba5c;
|
||||
private const int T_IMM = 0, T_STR = 2, T_GINT = 3, T_GFLOAT = 4, T_GSTR = 5, T_GPTR = 6,
|
||||
private const int T_IMM = 0, T_FLOAT = 1, T_STR = 2, T_GINT = 3, T_GFLOAT = 4, T_GSTR = 5, T_GPTR = 6,
|
||||
T_GSTRPTR = 8, T_LINT = 9, T_LFLOAT = 10, T_LSTR = 11, T_LPTR = 12,
|
||||
T_LSTRPTR = 14;
|
||||
private const string DiagnosticCaption = "エラーが発生しました";
|
||||
|
||||
private readonly Script _s;
|
||||
private readonly OpcodeTable _t;
|
||||
@@ -41,6 +42,7 @@ public sealed class VirtualMachine
|
||||
private readonly IScriptProvider? _provider;
|
||||
private readonly SharedProfile _sharedProfile;
|
||||
private readonly AudioMixerSettings _audioMixerSettings;
|
||||
private readonly DiagnosticOutputState _diagnosticOutput;
|
||||
private readonly INativeDatStore? _nativeDatStore;
|
||||
private static readonly bool _diagSetTexture = System.Environment.GetEnvironmentVariable("AGE_DIAG_SETTEX") == "1";
|
||||
private ExecFrame _cur = null!;
|
||||
@@ -108,6 +110,7 @@ public sealed class VirtualMachine
|
||||
public long Steps { get; private set; }
|
||||
public bool AutoMessageEnabled => _autoMessageEnabled;
|
||||
public bool MessageSkipEnabled => _messageSkipEnabled;
|
||||
public string PendingDiagnosticText => _diagnosticOutput.PendingText;
|
||||
/// <summary>
|
||||
/// Zero-based active-frame cutoff selected by opcode 0x1ad, or null when no surviving marker
|
||||
/// exists. A numbered-save serializer consumes this boundary in the full payload slice.
|
||||
@@ -151,7 +154,8 @@ public sealed class VirtualMachine
|
||||
IScriptProvider? provider = null, ITraceSink? sink = null,
|
||||
AdvTextHistory? textHistory = null, SharedProfile? sharedProfile = null,
|
||||
INativeDatStore? nativeDatStore = null,
|
||||
AudioMixerSettings? audioMixerSettings = null)
|
||||
AudioMixerSettings? audioMixerSettings = null,
|
||||
DiagnosticOutputState? diagnosticOutput = null)
|
||||
{
|
||||
_s = s; _t = t; _host = host; _o = o ?? new VmOptions(); _provider = provider;
|
||||
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
||||
@@ -159,6 +163,7 @@ public sealed class VirtualMachine
|
||||
_sink = sink ?? NullTraceSink.Instance; TextHistory = textHistory ?? new AdvTextHistory();
|
||||
_sharedProfile = sharedProfile ?? new SharedProfile();
|
||||
_audioMixerSettings = audioMixerSettings ?? new AudioMixerSettings();
|
||||
_diagnosticOutput = diagnosticOutput ?? new DiagnosticOutputState();
|
||||
_nativeDatStore = nativeDatStore;
|
||||
_messageWindowAlphaSetting = host.MessageWindowAlphaSetting;
|
||||
_messageGlyphDelayMilliseconds = System.Math.Max(0, host.MessageGlyphDelayMilliseconds);
|
||||
@@ -644,6 +649,33 @@ public sealed class VirtualMachine
|
||||
? ReadStr(operand)
|
||||
: unchecked((int)Read(operand)).ToString(System.Globalization.CultureInfo.InvariantCulture);
|
||||
|
||||
private string FormatDiagnosticOperand(Operand operand)
|
||||
{
|
||||
if (IsStr(operand)) return ReadStr(operand);
|
||||
if (operand.Type is T_FLOAT or T_GFLOAT or T_LFLOAT)
|
||||
{
|
||||
float value = BitConverter.Int32BitsToSingle(unchecked((int)Read(operand)));
|
||||
return value.ToString("F6", System.Globalization.CultureInfo.InvariantCulture);
|
||||
}
|
||||
return unchecked((int)Read(operand))
|
||||
.ToString(System.Globalization.CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
private DiagnosticMessage BuildDiagnosticMessage(Instruction instruction)
|
||||
{
|
||||
// Himegari's release AGE initializes both optional debug metadata tables to null and has no
|
||||
// writer for either one. The native formatter consequently emits -1 and "-" here.
|
||||
const int sourceLine = -1;
|
||||
const string commandName = "-";
|
||||
int nativeDepth = Math.Max(0, _depth - 1);
|
||||
string context = string.Format(
|
||||
System.Globalization.CultureInfo.InvariantCulture,
|
||||
"\n\nデバック情報:\nFILE={0} ADDRESS={1:X} LINE={2} COMMAND={3}({4}) DEPTH={5}\n",
|
||||
_cur.Script.Name, instruction.Offset, sourceLine, commandName,
|
||||
instruction.Opcode, nativeDepth);
|
||||
return new DiagnosticMessage(DiagnosticCaption, _diagnosticOutput.PendingText + context);
|
||||
}
|
||||
|
||||
private sealed class RootReloadRequestedException : Exception { }
|
||||
private sealed class NumberedRestoreRequestedException : Exception { }
|
||||
private sealed class ProcessExitRequestedException : Exception { }
|
||||
@@ -1194,6 +1226,19 @@ public sealed class VirtualMachine
|
||||
case "halve-strlen": // 0x1a6: strlen(native encoded bytes) >> 1
|
||||
Write(a[0], NativeStringByteLength(ReadStr(a[1])) >> 1);
|
||||
return pc + 1;
|
||||
case "u00425790": // upstream ABI label
|
||||
case "append-diagnostic-value": // 0x1b2: generic operand text -> EngineCtx accumulator
|
||||
_diagnosticOutput.Append(FormatDiagnosticOperand(a[0]));
|
||||
return pc + 1;
|
||||
case "u004257D0": // upstream ABI label
|
||||
case "append-diagnostic-newline": // 0x1b3: exact native CRLF bytes
|
||||
_diagnosticOutput.Append("\r\n");
|
||||
return pc + 1;
|
||||
case "u004237C0": // upstream ABI label
|
||||
case "show-and-clear-diagnostic": // 0x1b4: synchronous host prompt, then erase
|
||||
_host.ShowDiagnosticMessage(BuildDiagnosticMessage(ins));
|
||||
_diagnosticOutput.Clear();
|
||||
return pc + 1;
|
||||
case "is-catalog-resource-unlocked": // 0x19d
|
||||
Write(a[0], _sharedProfile.IsCatalogResourceUnlocked(Read(a[1])) ? 1 : 0);
|
||||
return pc + 1;
|
||||
|
||||
Reference in New Issue
Block a user