From 95309f461e5274151c97830945ba69d49cccef12 Mon Sep 17 00:00:00 2001 From: cracyc Date: Sat, 16 Apr 2016 08:36:09 -0500 Subject: plugins/cheat: load multiple files and fix load failure (nw) --- src/emu/luaengine.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 59 insertions(+), 3 deletions(-) (limited to 'src/emu/luaengine.cpp') diff --git a/src/emu/luaengine.cpp b/src/emu/luaengine.cpp index 08482eccd5c..331d2b1372e 100644 --- a/src/emu/luaengine.cpp +++ b/src/emu/luaengine.cpp @@ -1761,25 +1761,67 @@ int lua_engine::compile_with_env(const char *env, const char *script) return ref; } +template +Tout lua_engine::run(const char *env, int ref, Tin in) +{ + Tout ret; + lua_settop(m_lua_state, 0); + luabridge::Stack::push(m_lua_state, in); + run_internal(env, ref); + ret = luabridge::Stack::get(m_lua_state, 1); + lua_pop(m_lua_state, 1); + return ret; +} + +template +Tout lua_engine::run(const char *env, int ref) +{ + Tout ret; + lua_settop(m_lua_state, 0); + lua_pushnil(m_lua_state); + run_internal(env, ref); + ret = luabridge::Stack::get(m_lua_state, 1); + lua_pop(m_lua_state, 1); + return ret; +} + +template +void lua_engine::run(const char *env, int ref, Tin in) +{ + lua_settop(m_lua_state, 0); + luabridge::Stack::push(m_lua_state, in); + run_internal(env, ref); + lua_pop(m_lua_state, 1); +} + void lua_engine::run(const char *env, int ref) { - std::string field = std::string("env_").append(env); lua_settop(m_lua_state, 0); + lua_pushnil(m_lua_state); + run_internal(env, ref); + lua_pop(m_lua_state, 1); +} + +void lua_engine::run_internal(const char *env, int ref) +{ + std::string field = std::string("env_").append(env); lua_getfield(m_lua_state, LUA_REGISTRYINDEX, field.c_str()); if(lua_istable(m_lua_state, -1)) { lua_rawgeti(m_lua_state, -1, ref); if(lua_isfunction(m_lua_state, -1)) { - if(int error = lua_pcall(m_lua_state, 0, 0, 0) != LUA_OK) + lua_pushvalue(m_lua_state, -3); + if(int error = lua_pcall(m_lua_state, 1, 1, 0) != LUA_OK) { if(error == LUA_ERRRUN) printf("%s\n", lua_tostring(m_lua_state, -1)); lua_pop(m_lua_state, 1); + lua_pushnil(m_lua_state); } } - lua_pop(m_lua_state, 1); } + lua_replace(m_lua_state, 1); lua_pop(m_lua_state, 1); } void lua_engine::menu_populate(std::string &menu, std::vector &menu_list) @@ -2088,6 +2130,7 @@ void lua_engine::initialize() .addFunction ("cheat", &running_machine::cheat) .addFunction ("memory", &running_machine::memory) .addFunction ("options", &running_machine::options) + .addFunction ("outputs", &running_machine::output) .addProperty ("devices", &lua_engine::l_machine_get_devices) .addProperty ("screens", &lua_engine::l_machine_get_screens) .addProperty ("images", &lua_engine::l_machine_get_images) @@ -2344,6 +2387,14 @@ void lua_engine::initialize() .addCFunction ("write_i64", &lua_memory_region::l_region_write) .addCFunction ("write_u64", &lua_memory_region::l_region_write) .endClass() + .beginClass ("output") + .addFunction ("set_value", &output_manager::set_value) + .addFunction ("set_indexed_value", &output_manager::set_indexed_value) + .addFunction ("get_value", &output_manager::get_value) + .addFunction ("get_indexed_value", &output_manager::get_indexed_value) + .addFunction ("name_to_id", &output_manager::name_to_id) + .addFunction ("id_to_name", &output_manager::id_to_name) + .endClass() .deriveClass ("region") .addProperty ("size", &memory_region::bytes) .endClass() @@ -2367,11 +2418,16 @@ void lua_engine::initialize() .beginClass ("lua_file") .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 .deriveClass ("file") .addConstructor () .addFunction ("open", static_cast(&emu_file::open)) + .addFunction ("open_next", &emu_file::open_next) .addFunction ("seek", &emu_file::seek) .addFunction ("size", &emu_file::size) + .addFunction ("filename", &emu_file::filename) + .addFunction ("fullpath", &emu_file::fullpath) .endClass() .beginClass ("item") .addConstructor () -- cgit v1.2.3-70-g09d2