MCPcopy Create free account
hub / github.com/WilliamLCobb/iNDS / CallExitFunction

Function CallExitFunction

desmume/src/lua-engine.cpp:5208–5298  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5206}
5207
5208void CallExitFunction(int uid)
5209{
5210 LuaContextInfo& info = *luaContextInfo[uid];
5211 lua_State* L = info.L;
5212
5213 if(!L)
5214 return;
5215
5216 dontworry(info);
5217
5218 // first call the registered exit function if there is one
5219 if(!info.ranExit)
5220 {
5221 info.ranExit = true;
5222
5223#ifdef USE_INFO_STACK
5224 infoStack.insert(infoStack.begin(), &info);
5225 struct Scope { ~Scope(){ infoStack.erase(infoStack.begin()); } } scope;
5226#endif
5227
5228 //lua_settop(L, 0);
5229 lua_getfield(L, LUA_REGISTRYINDEX, luaCallIDStrings[LUACALL_BEFOREEXIT]);
5230
5231 int errorcode = 0;
5232 if (lua_isfunction(L, -1))
5233 {
5234 bool wasRunning = info.running;
5235 info.running = true;
5236 RefreshScriptSpeedStatus();
5237
5238 bool wasPanic = info.panic;
5239 info.panic = false; // otherwise we could barely do anything in the exit function
5240
5241 errorcode = lua_pcall(L, 0, 0, 0);
5242
5243 info.panic |= wasPanic; // restore panic
5244
5245 info.running = wasRunning;
5246 RefreshScriptSpeedStatus();
5247 }
5248
5249 // save persisted variable info after the exit function runs (even if it crashed)
5250 {
5251 // gather the final value of the variables we're supposed to persist
5252 LuaSaveData newExitData;
5253 {
5254 int numPersistVars = info.persistVars.size();
5255 for(int i = 0; i < numPersistVars; i++)
5256 {
5257 const char* varName = info.persistVars[i].c_str();
5258 lua_getfield(L, LUA_GLOBALSINDEX, varName);
5259 int type = lua_type(L,-1);
5260 unsigned int varNameCRC = crc32(0, (const unsigned char*)varName, strlen(varName));
5261 newExitData.SaveRecordPartial(uid, varNameCRC, -1);
5262 lua_pop(L,1);
5263 }
5264 }
5265

Callers 1

StopLuaScriptFunction · 0.85

Calls 9

dontworryFunction · 0.85
RefreshScriptSpeedStatusFunction · 0.85
HandleCallbackErrorFunction · 0.85
insertMethod · 0.80
c_strMethod · 0.80
SaveRecordPartialMethod · 0.80
ExportRecordsMethod · 0.80
sizeMethod · 0.45

Tested by

no test coverage detected