Skip to content
This repository was archived by the owner on Sep 11, 2023. It is now read-only.

Commit fd4a8cb

Browse files
committed
Debugger: Added "test runner" headless mode to run Lua scripts w/o UI
1 parent 1e47897 commit fd4a8cb

File tree

12 files changed

+190
-64
lines changed

12 files changed

+190
-64
lines changed

Core/Console.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,9 +360,10 @@ void Console::ResetComponents(bool softReset)
360360
_controlManager->UpdateInputState();
361361
}
362362

363-
void Console::Stop()
363+
void Console::Stop(int stopCode)
364364
{
365365
_stop = true;
366+
_stopCode = stopCode;
366367

367368
shared_ptr<Debugger> debugger = _debugger;
368369
if(debugger) {
@@ -510,6 +511,7 @@ void Console::Run()
510511
}
511512
} catch(const std::runtime_error &ex) {
512513
crashed = true;
514+
_stopCode = -1;
513515
MessageManager::DisplayMessage("Error", "GameCrash", ex.what());
514516
}
515517

@@ -726,6 +728,11 @@ void Console::SetNextFrameOverclockStatus(bool disabled)
726728
Instance->_disableOcNextFrame = disabled;
727729
}
728730

731+
int32_t Console::GetStopCode()
732+
{
733+
return _stopCode;
734+
}
735+
729736
HdPackData* Console::GetHdData()
730737
{
731738
return Instance->_hdData.get();

Core/Console.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class Console
5858

5959
bool _stop = false;
6060
bool _running = false;
61+
int32_t _stopCode = 0;
6162

6263
bool _disableOcNextFrame = false;
6364

@@ -77,7 +78,9 @@ class Console
7778
void SaveBatteries();
7879

7980
void Run();
80-
void Stop();
81+
void Stop(int stopCode = 0);
82+
83+
int32_t GetStopCode();
8184

8285
void RunSingleFrame();
8386
bool UpdateHdPackMode();

Core/LuaApi.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ int LuaApi::GetLibrary(lua_State *lua)
8181
{ "log", LuaApi::Log },
8282
{ "displayMessage", LuaApi::DisplayMessage },
8383
{ "reset", LuaApi::Reset },
84+
{ "stop", LuaApi::Stop },
8485
{ "breakExecution", LuaApi::Break },
8586
{ "resume", LuaApi::Resume },
8687
{ "execute", LuaApi::Execute },
@@ -464,6 +465,15 @@ int LuaApi::Reset(lua_State *lua)
464465
return l.ReturnCount();
465466
}
466467

468+
int LuaApi::Stop(lua_State *lua)
469+
{
470+
LuaCallHelper l(lua);
471+
int32_t stopCode = l.ReadInteger(0);
472+
checkminparams(0);
473+
Console::GetInstance()->Stop(stopCode);
474+
return l.ReturnCount();
475+
}
476+
467477
int LuaApi::Break(lua_State *lua)
468478
{
469479
LuaCallHelper l(lua);

Core/LuaApi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class LuaApi
4141
static int DisplayMessage(lua_State *lua);
4242

4343
static int Reset(lua_State *lua);
44+
static int Stop(lua_State *lua);
4445
static int Break(lua_State *lua);
4546
static int Resume(lua_State *lua);
4647
static int Execute(lua_State *lua);

GUI.NET/CommandLineHelper.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace Mesen.GUI
9+
{
10+
class CommandLineHelper
11+
{
12+
public static List<string> PreprocessCommandLineArguments(string[] args, bool toLower)
13+
{
14+
var switches = new List<string>();
15+
for(int i = 0; i < args.Length; i++) {
16+
if(args[i] != null) {
17+
string arg = args[i].Trim();
18+
if(arg.StartsWith("--")) {
19+
arg = "/" + arg.Substring(2);
20+
} else if(arg.StartsWith("-")) {
21+
arg = "/" + arg.Substring(1);
22+
}
23+
24+
if(toLower) {
25+
arg = arg.ToLowerInvariant();
26+
}
27+
switches.Add(arg);
28+
}
29+
}
30+
return switches;
31+
}
32+
33+
public static void GetRomPathFromCommandLine(List<string> switches, out string romPath, out List<string> luaScriptsToLoad)
34+
{
35+
Func<string, bool, string> getValidPath = (string path, bool forLua) => {
36+
path = path.Trim();
37+
if(path.ToLower().EndsWith(".lua") == forLua) {
38+
try {
39+
if(!File.Exists(path)) {
40+
//Try loading file as a relative path to the folder Mesen was started from
41+
path = Path.Combine(Program.OriginalFolder, path);
42+
}
43+
if(File.Exists(path)) {
44+
return path;
45+
}
46+
} catch { }
47+
}
48+
return null;
49+
};
50+
51+
//Check if any Lua scripts were specified
52+
luaScriptsToLoad = new List<string>();
53+
foreach(string arg in switches) {
54+
string path = getValidPath(arg, true);
55+
if(path != null) {
56+
luaScriptsToLoad.Add(path);
57+
}
58+
}
59+
60+
romPath = null;
61+
foreach(string arg in switches) {
62+
string path = getValidPath(arg, false);
63+
if(path != null) {
64+
romPath = path;
65+
break;
66+
}
67+
}
68+
}
69+
}
70+
}

GUI.NET/Debugger/DebugWindowManager.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,11 @@ public static void CleanupDebugger()
101101
if(_openedWindows.Count == 0) {
102102
//All windows have been closed, disable debugger
103103
DebugWorkspaceManager.Clear();
104-
InteropEmu.DebugRelease();
104+
105+
Task.Run(() => {
106+
//Run this in another thread to avoid deadlocks when this is called within a notification handler
107+
InteropEmu.DebugRelease();
108+
});
105109
}
106110
}
107111

GUI.NET/Forms/frmMain.cs

Lines changed: 9 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -82,28 +82,7 @@ public frmMain(string[] args)
8282
this.Resize += ResizeRecentGames;
8383
this.FormClosed += (s, e) => Application.RemoveMessageFilter(this);
8484
}
85-
86-
public List<string> PreprocessCommandLineArguments(string[] args, bool toLower)
87-
{
88-
var switches = new List<string>();
89-
for(int i = 0; i < args.Length; i++) {
90-
if(args[i] != null) {
91-
string arg = args[i].Trim();
92-
if(arg.StartsWith("--")) {
93-
arg = "/" + arg.Substring(2);
94-
} else if(arg.StartsWith("-")) {
95-
arg = "/" + arg.Substring(1);
96-
}
97-
98-
if(toLower) {
99-
arg = arg.ToLowerInvariant();
100-
}
101-
switches.Add(arg);
102-
}
103-
}
104-
return switches;
105-
}
106-
85+
10786
public void ProcessCommandLineArguments(List<string> switches, bool forStartup)
10887
{
10988
if(forStartup) {
@@ -121,42 +100,13 @@ public void ProcessCommandLineArguments(List<string> switches, bool forStartup)
121100

122101
public void LoadGameFromCommandLine(List<string> switches)
123102
{
124-
Func<string, bool, string> getValidPath = (string path, bool forLua) => {
125-
path = path.Trim();
126-
if(path.ToLower().EndsWith(".lua") == forLua) {
127-
try {
128-
if(!File.Exists(path)) {
129-
//Try loading file as a relative path to the folder Mesen was started from
130-
path = Path.Combine(Program.OriginalFolder, path);
131-
}
132-
if(File.Exists(path)) {
133-
return path;
134-
}
135-
} catch { }
136-
}
137-
return null;
138-
};
103+
string romPath;
104+
CommandLineHelper.GetRomPathFromCommandLine(switches, out romPath, out _luaScriptsToLoad);
139105

140-
//Check if any Lua scripts were specified
141-
_luaScriptsToLoad = new List<string>();
142-
foreach(string arg in switches) {
143-
string path = getValidPath(arg, true);
144-
if(path != null) {
145-
_luaScriptsToLoad.Add(path);
146-
}
147-
}
148-
149-
bool fileLoaded = false;
150-
foreach(string arg in switches) {
151-
string path = getValidPath(arg, false);
152-
if(path != null) {
153-
this.LoadFile(path);
154-
fileLoaded = true;
155-
break;
156-
}
157-
}
106+
if(romPath != null) {
107+
this.LoadFile(romPath);
108+
} else {
158109

159-
if(!fileLoaded) {
160110
if(_emuThread == null) {
161111
//When no ROM is loaded, only process Lua scripts if a ROM was specified as a command line param
162112
_luaScriptsToLoad.Clear();
@@ -196,7 +146,7 @@ protected override void OnLoad(EventArgs e)
196146

197147
InteropEmu.ScreenSize originalSize = InteropEmu.GetScreenSize(false);
198148
VideoInfo.ApplyConfig();
199-
this.ProcessCommandLineArguments(this.PreprocessCommandLineArguments(_commandLineArgs, true), true);
149+
this.ProcessCommandLineArguments(CommandLineHelper.PreprocessCommandLineArguments(_commandLineArgs, true), true);
200150
VideoInfo.ApplyConfig();
201151
InteropEmu.ScreenSize newSize = InteropEmu.GetScreenSize(false);
202152
if(originalSize.Width != newSize.Width || originalSize.Height != newSize.Height) {
@@ -267,7 +217,7 @@ protected override void OnShown(EventArgs e)
267217
this.Size = ConfigManager.Config.WindowSize.Value;
268218
}
269219

270-
this.LoadGameFromCommandLine(this.PreprocessCommandLineArguments(_commandLineArgs, false));
220+
this.LoadGameFromCommandLine(CommandLineHelper.PreprocessCommandLineArguments(_commandLineArgs, false));
271221

272222
this.menuStrip.VisibleChanged += new System.EventHandler(this.menuStrip_VisibleChanged);
273223
this.UpdateRendererLocation();
@@ -281,7 +231,7 @@ protected override void OnShown(EventArgs e)
281231
//This is needed when DPI display settings is not set to 100%
282232
_enableResize = true;
283233

284-
ProcessFullscreenSwitch(this.PreprocessCommandLineArguments(_commandLineArgs, true));
234+
ProcessFullscreenSwitch(CommandLineHelper.PreprocessCommandLineArguments(_commandLineArgs, true));
285235
}
286236

287237
protected override void OnClosing(CancelEventArgs e)

GUI.NET/GUI.NET.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@
238238
</Reference>
239239
</ItemGroup>
240240
<ItemGroup>
241+
<Compile Include="CommandLineHelper.cs" />
241242
<Compile Include="Config\DebuggerShortcutsConfig.cs" />
242243
<Compile Include="Config\MovieRecordInfo.cs" />
243244
<Compile Include="Config\ConfigAttributes.cs" />
@@ -1166,6 +1167,7 @@
11661167
<Compile Include="ResourceManager.cs" />
11671168
<Compile Include="RuntimeChecker.cs" />
11681169
<Compile Include="SingleInstance.cs" />
1170+
<Compile Include="TestRunner.cs" />
11691171
<None Include="Resources\SwitchView.png" />
11701172
<None Include="Resources\SelectAll.png" />
11711173
<None Include="Resources\Paste.png" />

GUI.NET/InteropEmu.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class InteropEmu
2727
[DllImport(DLLPath)] public static extern void SetFullscreenMode([MarshalAs(UnmanagedType.I1)]bool fullscreen, IntPtr windowHandle, UInt32 monitorWidth, UInt32 monitorHeight);
2828

2929
[DllImport(DLLPath)] [return: MarshalAs(UnmanagedType.I1)] public static extern bool IsRunning();
30+
[DllImport(DLLPath)] public static extern Int32 GetStopCode();
3031

3132
[DllImport(DLLPath)] public static extern void LoadROM([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(UTF8Marshaler))]string filename, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(UTF8Marshaler))]string patchFile);
3233
[DllImport(DLLPath)] public static extern void AddKnownGameFolder([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(UTF8Marshaler))]string folder);

GUI.NET/Program.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ private static void Main(string[] args)
129129
return;
130130
}
131131

132+
if(CommandLineHelper.PreprocessCommandLineArguments(args, true).Contains("/testrunner")) {
133+
Environment.ExitCode = TestRunner.Run(args);
134+
return;
135+
}
136+
132137
using(SingleInstance singleInstance = new SingleInstance()) {
133138
if(singleInstance.FirstInstance || !ConfigManager.Config.PreferenceInfo.SingleInstance) {
134139
frmMain frmMain = new frmMain(args);
@@ -137,8 +142,8 @@ private static void Main(string[] args)
137142
singleInstance.ArgumentsReceived += (object sender, ArgumentsReceivedEventArgs e) => {
138143
if(frmMain.IsHandleCreated) {
139144
frmMain.BeginInvoke((MethodInvoker)(() => {
140-
frmMain.ProcessCommandLineArguments(frmMain.PreprocessCommandLineArguments(e.Args, true), false);
141-
frmMain.LoadGameFromCommandLine(frmMain.PreprocessCommandLineArguments(e.Args, false));
145+
frmMain.ProcessCommandLineArguments(CommandLineHelper.PreprocessCommandLineArguments(e.Args, true), false);
146+
frmMain.LoadGameFromCommandLine(CommandLineHelper.PreprocessCommandLineArguments(e.Args, false));
142147
}));
143148
}
144149
};

0 commit comments

Comments
 (0)