From 511515b9fa253781c12be0365761060e82d8426b Mon Sep 17 00:00:00 2001 From: Alessandro Proto Date: Fri, 27 Jan 2023 19:31:29 +0100 Subject: [PATCH] Added panic screen back --- Capy64/Runtime/InputEmitter.cs | 1 + Capy64/Runtime/LuaState.cs | 9 +++++ Capy64/Runtime/PanicScreen.cs | 57 ++++++++++++++++++++++++++++++++ Capy64/Runtime/RuntimeManager.cs | 21 ++++++++++-- 4 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 Capy64/Runtime/PanicScreen.cs diff --git a/Capy64/Runtime/InputEmitter.cs b/Capy64/Runtime/InputEmitter.cs index 21d9519..78baa9f 100644 --- a/Capy64/Runtime/InputEmitter.cs +++ b/Capy64/Runtime/InputEmitter.cs @@ -75,6 +75,7 @@ internal class InputEmitter if (heldReboot >= rebootDelay) { heldReboot = 0; + RuntimeManager.ResetPanic(); RuntimeManager.Reboot(); } } diff --git a/Capy64/Runtime/LuaState.cs b/Capy64/Runtime/LuaState.cs index 05870a8..3160430 100644 --- a/Capy64/Runtime/LuaState.cs +++ b/Capy64/Runtime/LuaState.cs @@ -135,9 +135,18 @@ public class LuaState : IDisposable string stacktrace = Thread.OptString(-1, null); var builder = new StringBuilder(); + builder.AppendLine(status switch + { + LuaStatus.ErrSyntax => "Syntax error", + LuaStatus.ErrMem => "Out of memory", + LuaStatus.ErrErr => "Error", + LuaStatus.ErrRun => "Runtime error", + _ => status.ToString() + }); builder.AppendLine(error); if (!string.IsNullOrWhiteSpace(stacktrace)) { + builder.AppendLine(); builder.Append(stacktrace); } diff --git a/Capy64/Runtime/PanicScreen.cs b/Capy64/Runtime/PanicScreen.cs new file mode 100644 index 0000000..aa3e880 --- /dev/null +++ b/Capy64/Runtime/PanicScreen.cs @@ -0,0 +1,57 @@ +using Capy64.Core; +using Capy64.Runtime.Libraries; +using Microsoft.Xna.Framework; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Capy64.Runtime; + +public class PanicScreen +{ + public static Color ForegroundColor = Color.White; + public static Color BackgroundColor = new Color(0, 51, 187); + + public static void Render(string error, string details = null) + { + Term.ForegroundColor = ForegroundColor; + Term.BackgroundColor = BackgroundColor; + Term.SetCursorBlink(false); + Term.SetSize(51, 19); + Term.Clear(); + + var title = " Capy64 "; + var halfX = (Term.Width / 2) + 1; + Term.SetCursorPosition(halfX - (title.Length / 2), 2); + Term.ForegroundColor = BackgroundColor; + Term.BackgroundColor = ForegroundColor; + Term.Write(title); + + Term.ForegroundColor = ForegroundColor; + Term.BackgroundColor = BackgroundColor; + Term.SetCursorPosition(1, 4); + Print(error + '\n'); + + if (details is not null) + { + Print(details); + } + Term.SetCursorPosition(1, 19); + Print("Hold CTRL + ALT + INSERT to reboot."); + } + + private static void Print(string txt) + { + foreach (var ch in txt) + { + Term.Write(ch.ToString()); + if (Term.CursorPosition.X >= Term.Width || ch == '\n') + { + Term.SetCursorPosition(1, (int)Term.CursorPosition.Y + 1); + } + } + Term.SetCursorPosition(1, (int)Term.CursorPosition.Y + 1); + } +} diff --git a/Capy64/Runtime/RuntimeManager.cs b/Capy64/Runtime/RuntimeManager.cs index 67fbc1f..cc62bdf 100644 --- a/Capy64/Runtime/RuntimeManager.cs +++ b/Capy64/Runtime/RuntimeManager.cs @@ -20,6 +20,7 @@ internal class RuntimeManager : IPlugin private int step = 0; private static bool close = false; + private static bool inPanic = false; private static IGame _game; public RuntimeManager(IGame game) @@ -59,10 +60,26 @@ internal class RuntimeManager : IPlugin private void Resume() { - if (close || !luaState.ProcessQueue()) + if (inPanic) return; + try { - Start(); + if (close || !luaState.ProcessQueue()) + { + Start(); + } } + catch (LuaException e) + { + Console.Error.WriteLine(e); + inPanic = true; + close = true; + PanicScreen.Render(e.Message); + } + } + + public static void ResetPanic() + { + inPanic = false; } private void InitBIOS()