diff options
-rw-r--r-- | plugins/cheat/init.lua | 1 | ||||
-rw-r--r-- | plugins/cheatfind/init.lua | 12 | ||||
-rw-r--r-- | src/emu/luaengine.cpp | 60 | ||||
-rw-r--r-- | src/emu/luaengine.h | 10 |
4 files changed, 50 insertions, 33 deletions
diff --git a/plugins/cheat/init.lua b/plugins/cheat/init.lua index ea6f4bba3fb..b77ee97fc46 100644 --- a/plugins/cheat/init.lua +++ b/plugins/cheat/init.lua @@ -327,6 +327,7 @@ function cheat.startplugin() end local function menu_callback(index, event) + manager:machine():popmessage() if index > #cheats and event == "select" then index = index - #cheats if index == 2 then diff --git a/plugins/cheatfind/init.lua b/plugins/cheatfind/init.lua index c72fc95edac..69fd9346800 100644 --- a/plugins/cheatfind/init.lua +++ b/plugins/cheatfind/init.lua @@ -243,8 +243,8 @@ function cheatfind.startplugin() matchsel = 1 menu_blocks = {} - table = cheat.getspaces() - for tag, list in pairs(table) do + local space_table = cheat.getspaces() + for tag, list in pairs(space_table) do if list.program then local ram = {} for num, entry in pairs(list.program.map) do @@ -257,8 +257,8 @@ function cheatfind.startplugin() end end end - table = cheat.getram() - for tag, ram in pairs(table) do + space_table = cheat.getram() + for tag, ram in pairs(space_table) do devtable[#devtable + 1] = { tag = tag, space = ram.dev, ram = {{ offset = 0, size = ram.size }} } end end @@ -484,10 +484,12 @@ function cheatfind.startplugin() _G.ce.inject(cheat) end else + local filename = string.format("%s_%08x_cheat.json", emu.romname(), match.addr) local json = require("json") - local file = io.open(string.format("%s_%08x_cheat.json", emu.romname(), match.addr), "w") + local file = io.open(filename, "w") file:write(json.stringify(cheat)) file:close() + manager:machine():popmessage("Cheat written to " .. filename) end end end diff --git a/src/emu/luaengine.cpp b/src/emu/luaengine.cpp index 8e1ddbf8d93..34b78ba2890 100644 --- a/src/emu/luaengine.cpp +++ b/src/emu/luaengine.cpp @@ -1720,33 +1720,35 @@ lua_engine::~lua_engine() close(); } -int lua_engine::compile_with_env(const char *env, const char *script) +int lua_engine::compile_with_env(const char *envname, const char *script, const char *env) { - std::string field = std::string("env_").append(env); + std::string field = std::string("env_").append(envname); + int error; lua_settop(m_lua_state, 0); lua_getfield(m_lua_state, LUA_REGISTRYINDEX, field.c_str()); if(!lua_istable(m_lua_state, -1)) { - emu_file file(m_machine->manager().options().plugins_path(), OPEN_FLAG_READ); - // optionally load a script to prepare the environment - if(file.open(env, ".lua") != osd_file::error::NONE) + lua_newtable(m_lua_state); + lua_setfield(m_lua_state, LUA_REGISTRYINDEX, field.c_str()); + lua_getfield(m_lua_state, LUA_REGISTRYINDEX, field.c_str()); + } + + // optionally load a string to prepare the environment + if(env) + { + error = luaL_loadstring(m_lua_state, env); + lua_pushvalue(m_lua_state, -2); + if((error != LUA_OK) || ((error = lua_pcall(m_lua_state, 1, 0, 0)) != LUA_OK)) { - int error = luaL_loadfile(m_lua_state, file.fullpath()); - if(error || (error = lua_pcall(m_lua_state, 0, 0, 0) != LUA_OK)) - { - if(error == LUA_ERRRUN) - printf("%s\n", lua_tostring(m_lua_state, -1)); - lua_pop(m_lua_state, 1); - } + if((error == LUA_ERRSYNTAX) || (error == LUA_ERRRUN)) + printf("%s\n", lua_tostring(m_lua_state, -1)); + lua_pop(m_lua_state, 1); } - if(!lua_istable(m_lua_state, -1)) - lua_newtable(m_lua_state); - lua_setfield(m_lua_state, LUA_REGISTRYINDEX, field.c_str()); lua_getfield(m_lua_state, LUA_REGISTRYINDEX, field.c_str()); } - if(int error = luaL_loadstring(m_lua_state, script) != LUA_OK) + if((error = luaL_loadstring(m_lua_state, script)) != LUA_OK) { if(error == LUA_ERRSYNTAX) printf("%s\n", lua_tostring(m_lua_state, -1)); @@ -1801,6 +1803,8 @@ void lua_engine::run(const char *env, int ref) run_internal(env, ref); lua_pop(m_lua_state, 1); } +// create specialization so luabridge doesn't have to be included everywhere +template int lua_engine::run<int>(const char *env, int ref); void lua_engine::run_internal(const char *env, int ref) { @@ -1812,7 +1816,8 @@ void lua_engine::run_internal(const char *env, int ref) if(lua_isfunction(m_lua_state, -1)) { lua_pushvalue(m_lua_state, -3); - if(int error = lua_pcall(m_lua_state, 1, 1, 0) != LUA_OK) + int error; + if((error = lua_pcall(m_lua_state, 1, 1, 0)) != LUA_OK) { if(error == LUA_ERRRUN) printf("%s\n", lua_tostring(m_lua_state, -1)); @@ -1824,6 +1829,7 @@ void lua_engine::run_internal(const char *env, int ref) lua_replace(m_lua_state, 1); lua_pop(m_lua_state, 1); } + void lua_engine::menu_populate(std::string &menu, std::vector<menu_item> &menu_list) { std::string field = "menu_pop_" + menu; @@ -1835,7 +1841,8 @@ void lua_engine::menu_populate(std::string &menu, std::vector<menu_item> &menu_l lua_pop(m_lua_state, 1); return; } - if(int error = lua_pcall(m_lua_state, 0, 1, 0) != LUA_OK) + int error; + if((error = lua_pcall(m_lua_state, 0, 1, 0)) != LUA_OK) { if(error == LUA_ERRRUN) printf("%s\n", lua_tostring(m_lua_state, -1)); @@ -1881,7 +1888,8 @@ bool lua_engine::menu_callback(std::string &menu, int index, std::string event) { lua_pushinteger(m_lua_state, index); lua_pushstring(m_lua_state, event.c_str()); - if(int error = lua_pcall(m_lua_state, 2, 1, 0)) + int error; + if((error = lua_pcall(m_lua_state, 2, 1, 0)) != LUA_OK) { if(error == 2) printf("%s\n", lua_tostring(m_lua_state, -1)); @@ -1922,7 +1930,8 @@ void lua_engine::execute_function(const char *id) { if (lua_isfunction(m_lua_state, -1)) { - if(int error = lua_pcall(m_lua_state, 0, 0, 0)) + int error; + if((error = lua_pcall(m_lua_state, 0, 0, 0)) != LUA_OK) { if(error == 2) printf("%s\n", lua_tostring(m_lua_state, -1)); @@ -2062,8 +2071,10 @@ void lua_engine::attach_notifiers() int lua_engine::lua_machine::l_popmessage(lua_State *L) { running_machine *m = luabridge::Stack<running_machine *>::get(L, 1); - luaL_argcheck(L, lua_isstring(L, 2), 2, "message (string) expected"); - m->popmessage("%s", luaL_checkstring(L, 2)); + if(!lua_isstring(L, 2)) + m->popmessage(); + else + m->popmessage("%s", luaL_checkstring(L, 2)); return 0; } @@ -2245,6 +2256,9 @@ void lua_engine::initialize() .addFunction ("tag", &ioport_port::tag) .addFunction ("active", &ioport_port::active) .addFunction ("live", &ioport_port::live) + .addFunction ("read", &ioport_port::read) + .addFunction ("write", &ioport_port::write) + .addFunction ("field", &ioport_port::field) .addProperty <luabridge::LuaRef, void> ("fields", &lua_engine::l_ioports_port_get_fields) .endClass() .beginClass <ioport_field> ("ioport_field") @@ -2466,7 +2480,7 @@ void lua_engine::initialize() .addCFunction ("read", &lua_emu_file::l_emu_file_read) .endClass() // make sure there's a reference to the emu_file search path in your script as long as you need it - // otherwise it might be garbage collected as emu_file don't copy the string + // otherwise it might be garbage collected as emu_file doesn't copy the string .deriveClass <emu_file, lua_emu_file> ("file") .addConstructor <void (*)(const char *, UINT32)> () .addFunction ("open", static_cast<osd_file::error (emu_file::*)(const char *)>(&emu_file::open)) diff --git a/src/emu/luaengine.h b/src/emu/luaengine.h index 3b4c85ce939..d8a538c9a42 100644 --- a/src/emu/luaengine.h +++ b/src/emu/luaengine.h @@ -63,11 +63,11 @@ public: void attach_notifiers(); void on_frame_done(); - int compile_with_env(const char *env, const char *script); - template <typename Tout, typename Tin> Tout run(const char *env, int ref, Tin in); - template <typename Tout> Tout run(const char *env, int ref); - template <typename Tin> void run(const char *env, int ref, Tin in); - void run(const char *env, int ref); + int compile_with_env(const char *envname, const char *script, const char *env = nullptr); + template <typename Tout, typename Tin> Tout run(const char *envname, int ref, Tin in); + template <typename Tout> Tout run(const char *envname, int ref); + template <typename Tin> void run(const char *envname, int ref, Tin in); + void run(const char *envname, int ref); private: struct hook { lua_State *L; |