Debugger: Lua - Added getScriptDataFolder function

This commit is contained in:
Souryo 2017-10-07 15:16:14 -04:00
parent 4245561034
commit 11a06926ff
13 changed files with 63 additions and 36 deletions

View file

@ -1048,28 +1048,23 @@ void Debugger::SetInputOverride(uint8_t port, uint32_t state)
_inputOverride[port] = state; _inputOverride[port] = state;
} }
int Debugger::LoadScript(string content, int32_t scriptId) int Debugger::LoadScript(string name, string content, int32_t scriptId)
{ {
DebugBreakHelper helper(this); DebugBreakHelper helper(this);
if(scriptId < 0) { if(scriptId < 0) {
shared_ptr<ScriptHost> script(new ScriptHost(_nextScriptId++)); shared_ptr<ScriptHost> script(new ScriptHost(_nextScriptId++));
script->LoadScript(content, this); script->LoadScript(name, content, this);
_scripts.push_back(script); _scripts.push_back(script);
_hasScript = true; _hasScript = true;
return script->GetScriptId(); return script->GetScriptId();
} else { } else {
if(content.empty()) { auto result = std::find_if(_scripts.begin(), _scripts.end(), [=](shared_ptr<ScriptHost> &script) {
RemoveScript(scriptId); return script->GetScriptId() == scriptId;
return 0; });
} else { if(result != _scripts.end()) {
auto result = std::find_if(_scripts.begin(), _scripts.end(), [=](shared_ptr<ScriptHost> &script) { (*result)->LoadScript(name, content, this);
return script->GetScriptId() == scriptId; return scriptId;
});
if(result != _scripts.end()) {
(*result)->LoadScript(content, this);
return scriptId;
}
} }
} }

View file

@ -225,7 +225,7 @@ public:
static uint32_t GetInputOverride(uint8_t port); static uint32_t GetInputOverride(uint8_t port);
void SetInputOverride(uint8_t port, uint32_t state); void SetInputOverride(uint8_t port, uint32_t state);
int32_t LoadScript(string content, int32_t scriptId); int32_t LoadScript(string name, string content, int32_t scriptId);
void RemoveScript(int32_t scriptId); void RemoveScript(int32_t scriptId);
const char* GetScriptLog(int32_t scriptId); const char* GetScriptLog(int32_t scriptId);

View file

@ -1,6 +1,7 @@
#include "stdafx.h" #include "stdafx.h"
#include "LuaApi.h" #include "LuaApi.h"
#include "../Utilities/HexUtilities.h" #include "../Utilities/HexUtilities.h"
#include "../Utilities/FolderUtilities.h"
#include "../Lua/lua.hpp" #include "../Lua/lua.hpp"
#include "LuaCallHelper.h" #include "LuaCallHelper.h"
#include "Debugger.h" #include "Debugger.h"
@ -92,6 +93,7 @@ int LuaApi::GetLibrary(lua_State *lua)
{ "clearCheats", LuaApi::ClearCheats }, { "clearCheats", LuaApi::ClearCheats },
{ "setState", LuaApi::SetState }, { "setState", LuaApi::SetState },
{ "getState", LuaApi::GetState }, { "getState", LuaApi::GetState },
{ "getScriptDataFolder", LuaApi::GetScriptDataFolder },
{ NULL,NULL } { NULL,NULL }
}; };
@ -599,6 +601,18 @@ int LuaApi::ClearCheats(lua_State *lua)
return l.ReturnCount(); return l.ReturnCount();
} }
int LuaApi::GetScriptDataFolder(lua_State *lua)
{
LuaCallHelper l(lua);
checkparams();
string baseFolder = FolderUtilities::CombinePath(FolderUtilities::GetHomeFolder(), "LuaScriptData");
FolderUtilities::CreateFolder(baseFolder);
string scriptFolder = FolderUtilities::CombinePath(baseFolder, FolderUtilities::GetFilename(_context->GetScriptName(), false));
FolderUtilities::CreateFolder(scriptFolder);
l.Return(scriptFolder);
return l.ReturnCount();
}
int LuaApi::SetState(lua_State *lua) int LuaApi::SetState(lua_State *lua)
{ {
LuaCallHelper l(lua); LuaCallHelper l(lua);

View file

@ -65,6 +65,8 @@ public:
static int AddCheat(lua_State *lua); static int AddCheat(lua_State *lua);
static int ClearCheats(lua_State *lua); static int ClearCheats(lua_State *lua);
static int GetScriptDataFolder(lua_State * lua);
static int SetState(lua_State *lua); static int SetState(lua_State *lua);
static int GetState(lua_State *lua); static int GetState(lua_State *lua);

View file

@ -16,8 +16,10 @@ LuaScriptingContext::~LuaScriptingContext()
} }
} }
bool LuaScriptingContext::LoadScript(string scriptContent, Debugger* debugger) bool LuaScriptingContext::LoadScript(string scriptName, string scriptContent, Debugger* debugger)
{ {
_scriptName = scriptName;
int iErr = 0; int iErr = 0;
_lua = luaL_newstate(); _lua = luaL_newstate();
LuaApi::RegisterDebugger(debugger); LuaApi::RegisterDebugger(debugger);
@ -26,7 +28,7 @@ bool LuaScriptingContext::LoadScript(string scriptContent, Debugger* debugger)
luaL_openlibs(_lua); luaL_openlibs(_lua);
luaL_requiref(_lua, "emu", LuaApi::GetLibrary, 1); luaL_requiref(_lua, "emu", LuaApi::GetLibrary, 1);
Log("Loading script..."); Log("Loading script...");
if((iErr = luaL_loadstring(_lua, scriptContent.c_str())) == 0) { if((iErr = luaL_loadbufferx(_lua, scriptContent.c_str(), scriptContent.size(), ("@" + scriptName).c_str(), nullptr)) == 0) {
if((iErr = lua_pcall(_lua, 0, LUA_MULTRET, 0)) == 0) { if((iErr = lua_pcall(_lua, 0, LUA_MULTRET, 0)) == 0) {
//Script loaded properly //Script loaded properly
Log("Script loaded successfully."); Log("Script loaded successfully.");

View file

@ -18,5 +18,5 @@ public:
LuaScriptingContext(); LuaScriptingContext();
~LuaScriptingContext(); ~LuaScriptingContext();
bool LoadScript(string scriptContent, Debugger* debugger); bool LoadScript(string scriptName, string scriptContent, Debugger* debugger);
}; };

View file

@ -18,15 +18,11 @@ const char* ScriptHost::GetLog()
return _context ? _context->GetLog() : ""; return _context ? _context->GetLog() : "";
} }
bool ScriptHost::LoadScript(string scriptContent, Debugger* debugger) bool ScriptHost::LoadScript(string scriptName, string scriptContent, Debugger* debugger)
{ {
_context.reset(); _context.reset(new LuaScriptingContext());
if(scriptContent.size() > 0) { if(!_context->LoadScript(scriptName, scriptContent, debugger)) {
_context.reset(new LuaScriptingContext()); return false;
if(!_context->LoadScript(scriptContent, debugger)) {
return false;
}
} }
return true; return true;
} }

View file

@ -17,7 +17,7 @@ public:
int GetScriptId(); int GetScriptId();
const char* GetLog(); const char* GetLog();
bool LoadScript(string scriptContent, Debugger* debugger); bool LoadScript(string scriptName, string scriptContent, Debugger* debugger);
void ProcessCpuOperation(uint16_t addr, uint8_t &value, MemoryOperationType type); void ProcessCpuOperation(uint16_t addr, uint8_t &value, MemoryOperationType type);
void ProcessPpuOperation(uint16_t addr, uint8_t &value, MemoryOperationType type); void ProcessPpuOperation(uint16_t addr, uint8_t &value, MemoryOperationType type);

View file

@ -26,6 +26,11 @@ const char* ScriptingContext::GetLog()
return _log.c_str(); return _log.c_str();
} }
string ScriptingContext::GetScriptName()
{
return _scriptName;
}
void ScriptingContext::CallMemoryCallback(uint16_t addr, uint8_t &value, CallbackType type) void ScriptingContext::CallMemoryCallback(uint16_t addr, uint8_t &value, CallbackType type)
{ {
_inExecOpEvent = type == CallbackType::CpuExec; _inExecOpEvent = type == CallbackType::CpuExec;

View file

@ -32,6 +32,8 @@ private:
int32_t _loadSlot = -1; int32_t _loadSlot = -1;
protected: protected:
string _scriptName;
vector<int> _callbacks[5][0x10000]; vector<int> _callbacks[5][0x10000];
vector<int> _eventCallbacks[7]; vector<int> _eventCallbacks[7];
@ -39,11 +41,13 @@ protected:
virtual int InternalCallEventCallback(EventType type) = 0; virtual int InternalCallEventCallback(EventType type) = 0;
public: public:
virtual bool LoadScript(string scriptContent, Debugger* debugger) = 0; virtual bool LoadScript(string scriptName, string scriptContent, Debugger* debugger) = 0;
void Log(string message); void Log(string message);
const char* GetLog(); const char* GetLog();
string GetScriptName();
void RequestSaveState(int slot); void RequestSaveState(int slot);
bool RequestLoadState(int slot); bool RequestLoadState(int slot);
void SaveState(); void SaveState();

View file

@ -216,13 +216,25 @@ namespace Mesen.GUI.Debugger
mnuRecentScripts.Enabled = mnuRecentScripts.DropDownItems.Count > 0; mnuRecentScripts.Enabled = mnuRecentScripts.DropDownItems.Count > 0;
} }
private string ScriptName
{
get
{
if(_filePath != null) {
return Path.GetFileName(_filePath);
} else {
return "unnamed.lua";
}
}
}
private void RunScript() private void RunScript()
{ {
if(_filePath != null && mnuSaveBeforeRun.Checked && txtScriptContent.UndoEnabled) { if(_filePath != null && mnuSaveBeforeRun.Checked && txtScriptContent.UndoEnabled) {
txtScriptContent.SaveToFile(_filePath, Encoding.UTF8); txtScriptContent.SaveToFile(_filePath, Encoding.UTF8);
} }
_scriptId = InteropEmu.DebugLoadScript(txtScriptContent.Text, _scriptId); _scriptId = InteropEmu.DebugLoadScript(ScriptName, txtScriptContent.Text, _scriptId);
if(_scriptId < 0) { if(_scriptId < 0) {
MessageBox.Show("Error while loading script."); MessageBox.Show("Error while loading script.");
} else { } else {
@ -233,12 +245,9 @@ namespace Mesen.GUI.Debugger
private void StopScript() private void StopScript()
{ {
if(_scriptId >= 0) { if(_scriptId >= 0) {
if(InteropEmu.DebugLoadScript(string.Empty, _scriptId) == 0) { InteropEmu.DebugRemoveScript(_scriptId);
lblScriptActive.Visible = false; lblScriptActive.Visible = false;
_scriptId = -1; _scriptId = -1;
} else {
MessageBox.Show("Error while stopping script.");
}
} }
} }

View file

@ -207,7 +207,7 @@ namespace Mesen.GUI
[DllImport(DLLPath)] public static extern void DebugSetMemoryValue(DebugMemoryType type, UInt32 address, byte value); [DllImport(DLLPath)] public static extern void DebugSetMemoryValue(DebugMemoryType type, UInt32 address, byte value);
[DllImport(DLLPath)] public static extern void DebugSetInputOverride(Int32 port, Int32 state); [DllImport(DLLPath)] public static extern void DebugSetInputOverride(Int32 port, Int32 state);
[DllImport(DLLPath)] public static extern Int32 DebugLoadScript([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(UTF8Marshaler))]string content, Int32 scriptId = -1); [DllImport(DLLPath)] public static extern Int32 DebugLoadScript([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(UTF8Marshaler))]string name, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(UTF8Marshaler))]string content, Int32 scriptId = -1);
[DllImport(DLLPath)] public static extern void DebugRemoveScript(Int32 scriptId); [DllImport(DLLPath)] public static extern void DebugRemoveScript(Int32 scriptId);
[DllImport(DLLPath, EntryPoint = "DebugGetScriptLog")] private static extern IntPtr DebugGetScriptLogWrapper(Int32 scriptId); [DllImport(DLLPath, EntryPoint = "DebugGetScriptLog")] private static extern IntPtr DebugGetScriptLogWrapper(Int32 scriptId);
public static string DebugGetScriptLog(Int32 scriptId) { return PtrToStringUtf8(InteropEmu.DebugGetScriptLogWrapper(scriptId)).Replace("\n", Environment.NewLine); } public static string DebugGetScriptLog(Int32 scriptId) { return PtrToStringUtf8(InteropEmu.DebugGetScriptLogWrapper(scriptId)).Replace("\n", Environment.NewLine); }

View file

@ -111,7 +111,7 @@ extern "C"
DllExport void __stdcall DebugSetInputOverride(uint32_t port, uint32_t state) { GetDebugger()->SetInputOverride(port, state); } DllExport void __stdcall DebugSetInputOverride(uint32_t port, uint32_t state) { GetDebugger()->SetInputOverride(port, state); }
DllExport int32_t __stdcall DebugLoadScript(char* content, int32_t scriptId) { return GetDebugger()->LoadScript(content, scriptId); } DllExport int32_t __stdcall DebugLoadScript(char* name, char* content, int32_t scriptId) { return GetDebugger()->LoadScript(name, content, scriptId); }
DllExport void __stdcall DebugRemoveScript(int32_t scriptId) { GetDebugger()->RemoveScript(scriptId); } DllExport void __stdcall DebugRemoveScript(int32_t scriptId) { GetDebugger()->RemoveScript(scriptId); }
DllExport const char* __stdcall DebugGetScriptLog(int32_t scriptId) { return GetDebugger()->GetScriptLog(scriptId); } DllExport const char* __stdcall DebugGetScriptLog(int32_t scriptId) { return GetDebugger()->GetScriptLog(scriptId); }
}; };