// license:BSD-3-Clause // copyright-holders:Miodrag Milanovic /*************************************************************************** luaengine.h Controls execution of the core MAME system. ***************************************************************************/ #ifndef MAME_FRONTEND_MAME_LUAENGINE_H #define MAME_FRONTEND_MAME_LUAENGINE_H #pragma once #include #include #include #include #include #include #include #include #ifdef MAME_DEBUG #define SOL_ALL_SAFETIES_ON 1 #else #define SOL_SAFE_USERTYPE 1 #endif #include "sol/sol.hpp" struct lua_State; class lua_engine { public: // helper structures template struct devenum; template struct simple_list_wrapper; template struct tag_object_ptr_map; template using standard_tag_object_ptr_map = tag_object_ptr_map > >; template struct immutable_container_helper; template struct immutable_collection_helper; template struct immutable_sequence_helper; // construction/destruction lua_engine(); ~lua_engine(); void initialize(); sol::load_result load_script(std::string const &filename); sol::load_result load_string(std::string const &value); sol::environment make_environment(); bool frame_hook(); std::optional menu_populate(const std::string &menu, std::vector > &menu_list, std::string &flags); std::pair > menu_callback(const std::string &menu, int index, const std::string &event); void set_machine(running_machine *machine); std::vector &get_menu() { return m_menu; } void attach_notifiers(); void on_frame_done(); void on_sound_update(); void on_periodic(); bool on_missing_mandatory_image(const std::string &instance_name); void on_machine_before_load_settings(); template bool call_plugin(const std::string &name, T &&in, U &out) { bool ret = false; sol::object outobj = call_plugin(name, sol::make_object(sol(), std::forward(in))); if (outobj.is()) { out = outobj.as(); ret = true; } return ret; } template bool call_plugin(const std::string &name, T &&in, std::vector &out) { bool ret = false; sol::object outobj = call_plugin(name, sol::make_object(sol(), std::forward(in))); if (outobj.is()) { for (auto &entry : outobj.as()) { if (entry.second.template is()) { out.push_back(entry.second.template as()); ret = true; } } } return ret; } // this can also check if a returned table contains type T template bool call_plugin_check(const std::string &name, U &&in, bool table = false) { bool ret = false; sol::object outobj = call_plugin(name, sol::make_object(sol(), std::forward(in))); if (outobj.is() && !table) ret = true; else if (outobj.is() && table) { // check just one entry, checking the whole thing shouldn't be necessary as this only supports homogeneous tables if (outobj.as().begin().operator*().second.template is()) ret = true; } return ret; } template void call_plugin_set(const std::string &name, T &&in) { call_plugin(name, sol::make_object(sol(), std::forward(in))); } sol::state_view &sol() const { return *m_sol_state; } template static std::decay_t > invoke(Func &&func, Params&&... args) { g_profiler.start(PROFILER_LUA); try { auto result = func(std::forward(args)...); g_profiler.stop(); return result; } catch (...) { g_profiler.stop(); throw; } } private: template class enum_parser; class buffer_helper; struct addr_space; class tap_helper; class addr_space_change_notif; class symbol_table_wrapper; class expression_wrapper; struct save_item { void *base; unsigned int size; unsigned int count; unsigned int valcount; unsigned int blockcount; unsigned int stride; }; struct context { context() { busy = false; yield = false; } std::string result; std::condition_variable sync; bool busy; bool yield; }; // internal state lua_State *m_lua_state; std::unique_ptr m_sol_state; running_machine *m_machine; std::vector m_menu; template auto make_simple_callback_setter(void (T::*setter)(delegate &&), D &&dflt, const char *name, const char *desc); running_machine &machine() const { return *m_machine; } void on_machine_prestart(); void on_machine_start(); void on_machine_stop(); void on_machine_pause(); void on_machine_resume(); void on_machine_frame(); void resume(int nparam); void register_function(sol::function func, const char *id); template size_t enumerate_functions(const char *id, T &&callback); bool execute_function(const char *id); sol::object call_plugin(const std::string &name, sol::object in); void close(); void initialize_debug(sol::table &emu); void initialize_input(sol::table &emu); void initialize_memory(sol::table &emu); void initialize_render(sol::table &emu); }; #endif // MAME_FRONTEND_MAME_LUAENGINE_H