| 4986 | } |
| 4987 | |
| 4988 | void RunLuaScriptFile(int uid, const char* filenameCStr) |
| 4989 | { |
| 4990 | if(luaContextInfo.find(uid) == luaContextInfo.end()) |
| 4991 | return; |
| 4992 | StopLuaScript(uid); |
| 4993 | |
| 4994 | LuaContextInfo& info = *luaContextInfo[uid]; |
| 4995 | |
| 4996 | #ifdef USE_INFO_STACK |
| 4997 | infoStack.insert(infoStack.begin(), &info); |
| 4998 | struct Scope { ~Scope(){ infoStack.erase(infoStack.begin()); } } scope; // doing it like this makes sure that the info stack gets cleaned up even if an exception is thrown |
| 4999 | #endif |
| 5000 | |
| 5001 | info.nextFilename = filenameCStr; |
| 5002 | |
| 5003 | // TODO: store script's current directory into LuaContextInfo |
| 5004 | static char dirnameCStr[MAX_PATH]; |
| 5005 | strcpy(dirnameCStr, filenameCStr); |
| 5006 | TrimFilenameFromPath(dirnameCStr); |
| 5007 | chdir(dirnameCStr); |
| 5008 | |
| 5009 | if(info.running) |
| 5010 | { |
| 5011 | // it's a little complicated, but... the call to luaL_dofile below |
| 5012 | // could call a C function that calls this very function again |
| 5013 | // additionally, if that happened then the above call to StopLuaScript |
| 5014 | // probably couldn't stop the script yet, so instead of continuing, |
| 5015 | // we'll set a flag that tells the first call of this function to loop again |
| 5016 | // when the script is able to stop safely |
| 5017 | info.restart = true; |
| 5018 | return; |
| 5019 | } |
| 5020 | |
| 5021 | do |
| 5022 | { |
| 5023 | std::string filename = info.nextFilename; |
| 5024 | |
| 5025 | lua_State* L = lua_open(); |
| 5026 | #ifndef USE_INFO_STACK |
| 5027 | luaStateToContextMap[L] = &info; |
| 5028 | #endif |
| 5029 | luaStateToUIDMap[L] = uid; |
| 5030 | ResetInfo(info); |
| 5031 | info.L = L; |
| 5032 | info.guiFuncsNeedDeferring = true; |
| 5033 | info.lastFilename = filename; |
| 5034 | |
| 5035 | SetSaveKey(info, FilenameFromPath(filename.c_str())); |
| 5036 | info.dataSaveLoadKeySet = false; |
| 5037 | |
| 5038 | registerLibs(L); |
| 5039 | |
| 5040 | // register a function to periodically check for inactivity |
| 5041 | lua_sethook(L, LuaRescueHook, LUA_MASKCOUNT, HOOKCOUNT); |
| 5042 | |
| 5043 | // deferred evaluation table |
| 5044 | lua_newtable(L); |
| 5045 | lua_setfield(L, LUA_REGISTRYINDEX, deferredGUIIDString); |
no test coverage detected