| 1421 | |
| 1422 | |
| 1423 | void CallRegisteredLuaLoadFunctions(int savestateNumber, const LuaSaveData& saveData) |
| 1424 | { |
| 1425 | //lua_State* L = FCEU_GetLuaState(); |
| 1426 | if(L) |
| 1427 | { |
| 1428 | lua_settop(L, 0); |
| 1429 | lua_getfield(L, LUA_REGISTRYINDEX, luaCallIDStrings[LUACALL_AFTERLOAD]); |
| 1430 | |
| 1431 | if (lua_isfunction(L, -1)) |
| 1432 | { |
| 1433 | #ifdef __WIN_DRIVER__ |
| 1434 | // since the scriptdata can be very expensive to load |
| 1435 | // (e.g. the registered save function returned some huge tables) |
| 1436 | // check the number of parameters the registered load function expects |
| 1437 | // and don't bother loading the parameters it wouldn't receive anyway |
| 1438 | int numParamsExpected = (L->top - 1)->value.gc->cl.l.p->numparams; // NOTE: if this line crashes, that means your Lua headers are out of sync with your Lua lib |
| 1439 | if(numParamsExpected) numParamsExpected--; // minus one for the savestate number we always pass in |
| 1440 | |
| 1441 | int prevGarbage = lua_gc(L, LUA_GCCOUNT, 0); |
| 1442 | |
| 1443 | lua_pushinteger(L, savestateNumber); |
| 1444 | saveData.LoadRecord(L, LUA_DATARECORDKEY, numParamsExpected); |
| 1445 | #else |
| 1446 | int prevGarbage = lua_gc(L, LUA_GCCOUNT, 0); |
| 1447 | |
| 1448 | lua_pushinteger(L, savestateNumber); |
| 1449 | saveData.LoadRecord(L, LUA_DATARECORDKEY, (unsigned int) -1); |
| 1450 | #endif |
| 1451 | |
| 1452 | int n = lua_gettop(L) - 1; |
| 1453 | |
| 1454 | int ret = lua_pcall(L, n, 0, 0); |
| 1455 | if (ret != 0) { |
| 1456 | // This is grounds for trashing the function |
| 1457 | lua_pushnil(L); |
| 1458 | lua_setfield(L, LUA_REGISTRYINDEX, luaCallIDStrings[LUACALL_AFTERLOAD]); |
| 1459 | #ifdef __WIN_DRIVER__ |
| 1460 | MessageBox(hAppWnd, lua_tostring(L, -1), "Lua Error in LOAD function", MB_OK); |
| 1461 | #else |
| 1462 | LuaPrintfToWindowConsole("Lua error in registerload function: %s\n", lua_tostring(L, -1)); |
| 1463 | fprintf(stderr, "Lua error in registerload function: %s\n", lua_tostring(L, -1)); |
| 1464 | #endif |
| 1465 | } |
| 1466 | else |
| 1467 | { |
| 1468 | int newGarbage = lua_gc(L, LUA_GCCOUNT, 0); |
| 1469 | if(newGarbage - prevGarbage > 50) |
| 1470 | { |
| 1471 | // now seems to be a very good time to run the garbage collector |
| 1472 | // it might take a while now but that's better than taking 10 whiles 9 loads from now |
| 1473 | lua_gc(L, LUA_GCCOLLECT, 0); |
| 1474 | } |
| 1475 | } |
| 1476 | } |
| 1477 | else |
| 1478 | { |
| 1479 | lua_pop(L, 1); |
| 1480 | } |
no test coverage detected