Debugger: Lua - Add folder that contains the current script to the folders that scripts can require other Lua scripts from

This commit is contained in:
Sour 2023-12-18 22:04:47 +09:00
parent a640f04f10
commit 9258d1453f
10 changed files with 28 additions and 14 deletions

View file

@ -21,10 +21,10 @@ string ScriptHost::GetLog()
return context ? context->GetLog() : "";
}
bool ScriptHost::LoadScript(string scriptName, string scriptContent, Debugger* debugger)
bool ScriptHost::LoadScript(string scriptName, string path, string scriptContent, Debugger* debugger)
{
_context.reset(new ScriptingContext(debugger));
if(!_context->LoadScript(scriptName, scriptContent, debugger)) {
if(!_context->LoadScript(scriptName, path, scriptContent, debugger)) {
return false;
}
return true;

View file

@ -20,7 +20,7 @@ public:
int GetScriptId();
string GetLog();
bool LoadScript(string scriptName, string scriptContent, Debugger* debugger);
bool LoadScript(string scriptName, string path, string scriptContent, Debugger* debugger);
void RefreshMemoryCallbackFlags() { _context->RefreshMemoryCallbackFlags(); }
void ProcessEvent(EventType eventType, CpuType cpuType);

View file

@ -20,14 +20,14 @@ ScriptManager::~ScriptManager()
_debugger->GetEmulator()->GetScriptHud()->ClearScreen();
}
int ScriptManager::LoadScript(string name, string content, int32_t scriptId)
int ScriptManager::LoadScript(string name, string path, string content, int32_t scriptId)
{
DebugBreakHelper helper(_debugger);
auto lock = _scriptLock.AcquireSafe();
if(scriptId < 0) {
unique_ptr<ScriptHost> script(new ScriptHost(_nextScriptId++));
script->LoadScript(name, content, _debugger);
script->LoadScript(name, path, content, _debugger);
scriptId = script->GetScriptId();
_scripts.push_back(std::move(script));
_hasScript = true;
@ -40,7 +40,7 @@ int ScriptManager::LoadScript(string name, string content, int32_t scriptId)
//Send a ScriptEnded event before reloading the code
(*result)->ProcessEvent(EventType::ScriptEnded, _debugger->GetMainCpuType());
(*result)->LoadScript(name, content, _debugger);
(*result)->LoadScript(name, path, content, _debugger);
RefreshMemoryCallbackFlags();
return scriptId;
}

View file

@ -26,7 +26,7 @@ public:
~ScriptManager();
__forceinline bool HasScript() { return _hasScript; }
int32_t LoadScript(string name, string content, int32_t scriptId);
int32_t LoadScript(string name, string path, string content, int32_t scriptId);
void RemoveScript(int32_t scriptId);
string GetScriptLog(int32_t scriptId);
void ProcessEvent(EventType type, CpuType cpuType);

View file

@ -1,5 +1,6 @@
#include "pch.h"
#include <algorithm>
#include <regex>
#include "Lua/lua.hpp"
#include "Lua/luasocket.hpp"
#include "Debugger/ScriptingContext.h"
@ -50,7 +51,7 @@ ScriptingContext::~ScriptingContext()
}
}
bool ScriptingContext::LoadScript(string scriptName, string scriptContent, Debugger* debugger)
bool ScriptingContext::LoadScript(string scriptName, string path, string scriptContent, Debugger* debugger)
{
_scriptName = scriptName;
@ -78,6 +79,17 @@ bool ScriptingContext::LoadScript(string scriptName, string scriptContent, Debug
lua_pop(_lua, 2);
}
if(allowIoOsAccess) {
//Escape backslashes
std::regex r("\\\\");
path = std::regex_replace(path, r, "\\\\");
//Add path for the current Lua script to package.path to allow
//using require() without specifying an absolute path, etc.
string cmd = "package.path = package.path .. ';" + path + "?.lua'";
luaL_dostring(_lua, cmd.c_str());
}
luaL_requiref(_lua, "emu", LuaApi::GetLibrary, 1);
Log("Loading script...");
if((iErr = luaL_loadbufferx(_lua, scriptContent.c_str(), scriptContent.size(), ("@" + scriptName).c_str(), nullptr)) == 0) {

View file

@ -66,7 +66,7 @@ protected:
public:
ScriptingContext(Debugger* debugger);
~ScriptingContext();
bool LoadScript(string scriptName, string scriptContent, Debugger* debugger);
bool LoadScript(string scriptName, string path, string scriptContent, Debugger* debugger);
void Log(string message);
string GetLog();

View file

@ -181,7 +181,7 @@ extern "C"
DllExport DebugEventInfo __stdcall GetEventViewerEvent(CpuType cpuType, uint16_t scanline, uint16_t cycle) { return WithTool(DebugEventInfo, GetEventManager(cpuType), GetEvent(scanline, cycle)); }
DllExport uint32_t __stdcall TakeEventSnapshot(CpuType cpuType, bool forAutoRefresh) { return WithTool(uint32_t, GetEventManager(cpuType), TakeEventSnapshot(forAutoRefresh)); }
DllExport int32_t __stdcall LoadScript(char* name, char* content, int32_t scriptId) { return WithTool(int32_t, GetScriptManager(), LoadScript(name, content, scriptId)); }
DllExport int32_t __stdcall LoadScript(char* name, char* path, char* content, int32_t scriptId) { return WithTool(int32_t, GetScriptManager(), LoadScript(name, path, content, scriptId)); }
DllExport void __stdcall RemoveScript(int32_t scriptId) { WithToolVoid(GetScriptManager(), RemoveScript(scriptId)); }
DllExport void __stdcall GetScriptLog(int32_t scriptId, char* outScriptLog, uint32_t maxLength)

View file

@ -181,7 +181,8 @@ namespace Mesen.Debugger.ViewModels
await SaveScript();
}
UpdateScriptId(DebugApi.LoadScript(ScriptName.Length == 0 ? "DefaultName" : ScriptName, Code, ScriptId));
string path = (Path.GetDirectoryName(FilePath) ?? Program.OriginalFolder) + Path.DirectorySeparatorChar;
UpdateScriptId(DebugApi.LoadScript(ScriptName.Length == 0 ? "DefaultName" : ScriptName, path, Code, ScriptId));
}
private void UpdateScriptId(int scriptId)
@ -224,7 +225,8 @@ namespace Mesen.Debugger.ViewModels
public void RestartScript()
{
DebugApi.RemoveScript(ScriptId);
UpdateScriptId(DebugApi.LoadScript(ScriptName.Length == 0 ? "DefaultName" : ScriptName, Code, -1));
string path = (Path.GetDirectoryName(FilePath) ?? Program.OriginalFolder) + Path.DirectorySeparatorChar;
UpdateScriptId(DebugApi.LoadScript(ScriptName.Length == 0 ? "DefaultName" : ScriptName, path, Code, -1));
}
private string? InitialFolder

View file

@ -191,7 +191,7 @@ namespace Mesen.Interop
[DllImport(DllPath)] public static extern void SetProgramCounter(CpuType cpuType, UInt32 address);
[DllImport(DllPath)] public static extern UInt32 GetProgramCounter(CpuType cpuType, [MarshalAs(UnmanagedType.I1)] bool getInstPc);
[DllImport(DllPath)] public static extern Int32 LoadScript(string name, [MarshalAs(UnmanagedType.LPUTF8Str)] string content, Int32 scriptId = -1);
[DllImport(DllPath)] public static extern Int32 LoadScript([MarshalAs(UnmanagedType.LPUTF8Str)]string name, [MarshalAs(UnmanagedType.LPUTF8Str)]string path, [MarshalAs(UnmanagedType.LPUTF8Str)] string content, Int32 scriptId = -1);
[DllImport(DllPath)] public static extern void RemoveScript(Int32 scriptId);
[DllImport(DllPath, EntryPoint = "GetScriptLog")] private static extern void GetScriptLogWrapper(Int32 scriptId, IntPtr outScriptLog, Int32 maxLength);

View file

@ -40,7 +40,7 @@ namespace Mesen.Utilities
foreach(string luaScript in commandLineHelper.LuaScriptsToLoad) {
try {
string script = File.ReadAllText(luaScript);
DebugApi.LoadScript(luaScript, script);
DebugApi.LoadScript(luaScript, Path.GetDirectoryName(luaScript) ?? Program.OriginalFolder, script);
} catch { }
}