diff --git a/Core/Debugger.cpp b/Core/Debugger.cpp index 633752e0..ad352bc1 100644 --- a/Core/Debugger.cpp +++ b/Core/Debugger.cpp @@ -1048,28 +1048,23 @@ void Debugger::SetInputOverride(uint8_t port, uint32_t 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); if(scriptId < 0) { shared_ptr script(new ScriptHost(_nextScriptId++)); - script->LoadScript(content, this); + script->LoadScript(name, content, this); _scripts.push_back(script); _hasScript = true; return script->GetScriptId(); } else { - if(content.empty()) { - RemoveScript(scriptId); - return 0; - } else { - auto result = std::find_if(_scripts.begin(), _scripts.end(), [=](shared_ptr &script) { - return script->GetScriptId() == scriptId; - }); - if(result != _scripts.end()) { - (*result)->LoadScript(content, this); - return scriptId; - } + auto result = std::find_if(_scripts.begin(), _scripts.end(), [=](shared_ptr &script) { + return script->GetScriptId() == scriptId; + }); + if(result != _scripts.end()) { + (*result)->LoadScript(name, content, this); + return scriptId; } } diff --git a/Core/Debugger.h b/Core/Debugger.h index e32682a5..9ad968ec 100644 --- a/Core/Debugger.h +++ b/Core/Debugger.h @@ -225,7 +225,7 @@ public: static uint32_t GetInputOverride(uint8_t port); 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); const char* GetScriptLog(int32_t scriptId); diff --git a/Core/LuaApi.cpp b/Core/LuaApi.cpp index 2e6a489c..cc15a776 100644 --- a/Core/LuaApi.cpp +++ b/Core/LuaApi.cpp @@ -1,6 +1,7 @@ #include "stdafx.h" #include "LuaApi.h" #include "../Utilities/HexUtilities.h" +#include "../Utilities/FolderUtilities.h" #include "../Lua/lua.hpp" #include "LuaCallHelper.h" #include "Debugger.h" @@ -92,6 +93,7 @@ int LuaApi::GetLibrary(lua_State *lua) { "clearCheats", LuaApi::ClearCheats }, { "setState", LuaApi::SetState }, { "getState", LuaApi::GetState }, + { "getScriptDataFolder", LuaApi::GetScriptDataFolder }, { NULL,NULL } }; @@ -599,6 +601,18 @@ int LuaApi::ClearCheats(lua_State *lua) 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) { LuaCallHelper l(lua); diff --git a/Core/LuaApi.h b/Core/LuaApi.h index 0887da89..15858bfe 100644 --- a/Core/LuaApi.h +++ b/Core/LuaApi.h @@ -65,6 +65,8 @@ public: static int AddCheat(lua_State *lua); static int ClearCheats(lua_State *lua); + static int GetScriptDataFolder(lua_State * lua); + static int SetState(lua_State *lua); static int GetState(lua_State *lua); diff --git a/Core/LuaScriptingContext.cpp b/Core/LuaScriptingContext.cpp index f89fd7ce..68feb984 100644 --- a/Core/LuaScriptingContext.cpp +++ b/Core/LuaScriptingContext.cpp @@ -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; _lua = luaL_newstate(); LuaApi::RegisterDebugger(debugger); @@ -26,7 +28,7 @@ bool LuaScriptingContext::LoadScript(string scriptContent, Debugger* debugger) luaL_openlibs(_lua); luaL_requiref(_lua, "emu", LuaApi::GetLibrary, 1); 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) { //Script loaded properly Log("Script loaded successfully."); diff --git a/Core/LuaScriptingContext.h b/Core/LuaScriptingContext.h index 00d147f5..3e24ae5e 100644 --- a/Core/LuaScriptingContext.h +++ b/Core/LuaScriptingContext.h @@ -18,5 +18,5 @@ public: LuaScriptingContext(); ~LuaScriptingContext(); - bool LoadScript(string scriptContent, Debugger* debugger); + bool LoadScript(string scriptName, string scriptContent, Debugger* debugger); }; diff --git a/Core/ScriptHost.cpp b/Core/ScriptHost.cpp index da67415f..fa40ebb6 100644 --- a/Core/ScriptHost.cpp +++ b/Core/ScriptHost.cpp @@ -18,15 +18,11 @@ const char* ScriptHost::GetLog() return _context ? _context->GetLog() : ""; } -bool ScriptHost::LoadScript(string scriptContent, Debugger* debugger) +bool ScriptHost::LoadScript(string scriptName, string scriptContent, Debugger* debugger) { - _context.reset(); - if(scriptContent.size() > 0) { - _context.reset(new LuaScriptingContext()); - - if(!_context->LoadScript(scriptContent, debugger)) { - return false; - } + _context.reset(new LuaScriptingContext()); + if(!_context->LoadScript(scriptName, scriptContent, debugger)) { + return false; } return true; } diff --git a/Core/ScriptHost.h b/Core/ScriptHost.h index feac6a2c..e87a970d 100644 --- a/Core/ScriptHost.h +++ b/Core/ScriptHost.h @@ -17,7 +17,7 @@ public: int GetScriptId(); 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 ProcessPpuOperation(uint16_t addr, uint8_t &value, MemoryOperationType type); diff --git a/Core/ScriptingContext.cpp b/Core/ScriptingContext.cpp index 14604314..84af20d5 100644 --- a/Core/ScriptingContext.cpp +++ b/Core/ScriptingContext.cpp @@ -26,6 +26,11 @@ const char* ScriptingContext::GetLog() return _log.c_str(); } +string ScriptingContext::GetScriptName() +{ + return _scriptName; +} + void ScriptingContext::CallMemoryCallback(uint16_t addr, uint8_t &value, CallbackType type) { _inExecOpEvent = type == CallbackType::CpuExec; diff --git a/Core/ScriptingContext.h b/Core/ScriptingContext.h index a9cba77c..3c86d080 100644 --- a/Core/ScriptingContext.h +++ b/Core/ScriptingContext.h @@ -32,6 +32,8 @@ private: int32_t _loadSlot = -1; protected: + string _scriptName; + vector _callbacks[5][0x10000]; vector _eventCallbacks[7]; @@ -39,11 +41,13 @@ protected: virtual int InternalCallEventCallback(EventType type) = 0; public: - virtual bool LoadScript(string scriptContent, Debugger* debugger) = 0; + virtual bool LoadScript(string scriptName, string scriptContent, Debugger* debugger) = 0; void Log(string message); const char* GetLog(); + string GetScriptName(); + void RequestSaveState(int slot); bool RequestLoadState(int slot); void SaveState(); diff --git a/GUI.NET/Debugger/frmScript.cs b/GUI.NET/Debugger/frmScript.cs index 30245950..f58404bd 100644 --- a/GUI.NET/Debugger/frmScript.cs +++ b/GUI.NET/Debugger/frmScript.cs @@ -216,13 +216,25 @@ namespace Mesen.GUI.Debugger mnuRecentScripts.Enabled = mnuRecentScripts.DropDownItems.Count > 0; } + private string ScriptName + { + get + { + if(_filePath != null) { + return Path.GetFileName(_filePath); + } else { + return "unnamed.lua"; + } + } + } + private void RunScript() { if(_filePath != null && mnuSaveBeforeRun.Checked && txtScriptContent.UndoEnabled) { txtScriptContent.SaveToFile(_filePath, Encoding.UTF8); } - _scriptId = InteropEmu.DebugLoadScript(txtScriptContent.Text, _scriptId); + _scriptId = InteropEmu.DebugLoadScript(ScriptName, txtScriptContent.Text, _scriptId); if(_scriptId < 0) { MessageBox.Show("Error while loading script."); } else { @@ -233,12 +245,9 @@ namespace Mesen.GUI.Debugger private void StopScript() { if(_scriptId >= 0) { - if(InteropEmu.DebugLoadScript(string.Empty, _scriptId) == 0) { - lblScriptActive.Visible = false; - _scriptId = -1; - } else { - MessageBox.Show("Error while stopping script."); - } + InteropEmu.DebugRemoveScript(_scriptId); + lblScriptActive.Visible = false; + _scriptId = -1; } } diff --git a/GUI.NET/InteropEmu.cs b/GUI.NET/InteropEmu.cs index 35eb2251..8fd21574 100644 --- a/GUI.NET/InteropEmu.cs +++ b/GUI.NET/InteropEmu.cs @@ -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 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, EntryPoint = "DebugGetScriptLog")] private static extern IntPtr DebugGetScriptLogWrapper(Int32 scriptId); public static string DebugGetScriptLog(Int32 scriptId) { return PtrToStringUtf8(InteropEmu.DebugGetScriptLogWrapper(scriptId)).Replace("\n", Environment.NewLine); } diff --git a/InteropDLL/DebugWrapper.cpp b/InteropDLL/DebugWrapper.cpp index 73cfb060..4f5e48f9 100644 --- a/InteropDLL/DebugWrapper.cpp +++ b/InteropDLL/DebugWrapper.cpp @@ -111,7 +111,7 @@ extern "C" 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 const char* __stdcall DebugGetScriptLog(int32_t scriptId) { return GetDebugger()->GetScriptLog(scriptId); } };