From 9110176e0096cc5455f2427aa09c1170836d7b9d Mon Sep 17 00:00:00 2001 From: wheremyfoodat <44909372+wheremyfoodat@users.noreply.github.com> Date: Thu, 22 Feb 2024 17:18:00 +0200 Subject: [PATCH 1/2] Bind LoadROM to Lua --- src/lua.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/lua.cpp b/src/lua.cpp index ab1896e5..4328f1ae 100644 --- a/src/lua.cpp +++ b/src/lua.cpp @@ -159,6 +159,22 @@ static int resetThunk(lua_State* L) { return 0; } +static int loadROMThunk(lua_State* L) { + // Path argument is invalid, report that loading failed and exit + if (lua_type(L, -1) != LUA_TSTRING) { + lua_pushboolean(L, 0); + return 1; + } + + size_t pathLength; + const char* const r = lua_tolstring(L, -1, &pathLength); + + const auto path = std::filesystem::path(std::string(r, pathLength)); + // Load ROM and reply if it succeeded or not + lua_pushboolean(L, LuaManager::g_emulator->loadROM(path) ? 1 : 0); + return 1; +} + // clang-format off static constexpr luaL_Reg functions[] = { { "__read8", read8Thunk }, @@ -173,6 +189,7 @@ static constexpr luaL_Reg functions[] = { { "__pause", pauseThunk}, { "__resume", resumeThunk}, { "__reset", resetThunk}, + { "__loadROM", loadROMThunk}, { nullptr, nullptr }, }; // clang-format on @@ -200,6 +217,7 @@ void LuaManager::initializeThunks() { pause = function() GLOBALS.__pause() end, resume = function() GLOBALS.__resume() end, reset = function() GLOBALS.__reset() end, + loadROM = function(path) return GLOBALS.__loadROM(path) end, Frame = __Frame, } From 85161b08eafc7b51216c78f10647a7700fe6386b Mon Sep 17 00:00:00 2001 From: wheremyfoodat <44909372+wheremyfoodat@users.noreply.github.com> Date: Thu, 22 Feb 2024 17:19:33 +0200 Subject: [PATCH 2/2] Update lua.cpp --- src/lua.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lua.cpp b/src/lua.cpp index 4328f1ae..1cc3a8d6 100644 --- a/src/lua.cpp +++ b/src/lua.cpp @@ -167,9 +167,9 @@ static int loadROMThunk(lua_State* L) { } size_t pathLength; - const char* const r = lua_tolstring(L, -1, &pathLength); + const char* const str = lua_tolstring(L, -1, &pathLength); - const auto path = std::filesystem::path(std::string(r, pathLength)); + const auto path = std::filesystem::path(std::string(str, pathLength)); // Load ROM and reply if it succeeded or not lua_pushboolean(L, LuaManager::g_emulator->loadROM(path) ? 1 : 0); return 1;