* Loads and runs the given Lua script. * The emulator MUST be paused for this function to be * called. Otherwise, all frame boundary assumptions go out the window. * * Returns true on success, false on failure. */
| 6386 | * Returns true on success, false on failure. |
| 6387 | */ |
| 6388 | int FCEU_LoadLuaCode(const char *filename, const char *arg) |
| 6389 | { |
| 6390 | if (!DemandLua()) |
| 6391 | { |
| 6392 | return 0; |
| 6393 | } |
| 6394 | |
| 6395 | if (filename != luaScriptName) |
| 6396 | { |
| 6397 | if (luaScriptName) free(luaScriptName); |
| 6398 | luaScriptName = strdup(filename); |
| 6399 | } |
| 6400 | |
| 6401 | std::string getfilepath = filename; |
| 6402 | |
| 6403 | getfilepath = getfilepath.substr(0,getfilepath.find_last_of("/\\") + 1); |
| 6404 | |
| 6405 | if ( SetCurrentDir(getfilepath.c_str()) != 0 ) |
| 6406 | { |
| 6407 | FCEU_printf("Warning: Failed chdir failed to set current dir to: %s\n", getfilepath.c_str() ); |
| 6408 | } |
| 6409 | |
| 6410 | //stop any lua we might already have had running |
| 6411 | FCEU_LuaStop(); |
| 6412 | |
| 6413 | //Reinit the error count |
| 6414 | luaexiterrorcount = 8; |
| 6415 | |
| 6416 | if (!L) { |
| 6417 | |
| 6418 | L = lua_open(); |
| 6419 | luaL_openlibs(L); |
| 6420 | #if defined( __WIN_DRIVER__) && !defined(NEED_MINGW_HACKS) |
| 6421 | iuplua_open(L); |
| 6422 | iupcontrolslua_open(L); |
| 6423 | luaopen_winapi(L); |
| 6424 | imlua_open(L); |
| 6425 | cdlua_open(L); |
| 6426 | cdluaim_open(L); |
| 6427 | |
| 6428 | //luasocket - yeah, have to open this in a weird way |
| 6429 | lua_pushcfunction(L,luaopen_socket_core); |
| 6430 | lua_setglobal(L,"tmp"); |
| 6431 | luaL_dostring(L, "package.preload[\"socket.core\"] = _G.tmp"); |
| 6432 | lua_pushcfunction(L,luaopen_mime_core); |
| 6433 | lua_setglobal(L,"tmp"); |
| 6434 | luaL_dostring(L, "package.preload[\"mime.core\"] = _G.tmp"); |
| 6435 | #endif |
| 6436 | |
| 6437 | luaL_register(L, "emu", emulib); // added for better cross-emulator compatibility |
| 6438 | luaL_register(L, "FCEU", emulib); // kept for backward compatibility |
| 6439 | luaL_register(L, "memory", memorylib); |
| 6440 | luaL_register(L, "ppu", ppulib); |
| 6441 | luaL_register(L, "rom", romlib); |
| 6442 | luaL_register(L, "joypad", joypadlib); |
| 6443 | luaL_register(L, "zapper", zapperlib); |
| 6444 | luaL_register(L, "input", inputlib); |
| 6445 | lua_settop(L, 0); // clean the stack, because each call to luaL_register leaves a table on top (eventually overflows) |
no test coverage detected