/** * @brief Runs the rescue script if players are stuck. */ static void land_stranded (void) { char *buf; size_t bufsize; const char *file = "dat/rescue.lua"; /* Nothing to do if there's no rescue script. */ if (!ndata_exists(file)) return; if (rescue_env == LUA_NOREF) { rescue_env = nlua_newEnv(1); nlua_loadStandard( rescue_env ); nlua_loadTk( rescue_env ); buf = ndata_read( file, &bufsize ); if (nlua_dobufenv(rescue_env, buf, bufsize, file) != 0) { WARN( _("Error loading file: %s\n" "%s\n" "Most likely Lua file has improper syntax, please check"), file, lua_tostring(naevL,-1)); free(buf); return; } free(buf); } /* Run Lua. */ nlua_getenv(rescue_env,"rescue"); if (nlua_pcall(rescue_env, 0, 0)) { /* error has occurred */ WARN( _("Rescue: 'rescue' : '%s'"), lua_tostring(naevL,-1)); lua_pop(naevL,1); } }
/** * @brief Runs the rescue script if players are stuck. */ static void land_stranded (void) { char *buf; uint32_t bufsize; const char *file = "dat/rescue.lua"; int errf; lua_State *L; /* Nothing to do if there's no rescue script. */ if (!ndata_exists(file)) return; if (rescue_L == NULL) { rescue_L = nlua_newState(); nlua_loadStandard( rescue_L, 0 ); nlua_loadTk( rescue_L ); L = rescue_L; buf = ndata_read( file, &bufsize ); if (luaL_dobuffer(L, buf, bufsize, file) != 0) { WARN("Error loading file: %s\n" "%s\n" "Most likely Lua file has improper syntax, please check", file, lua_tostring(L,-1)); free(buf); return; } free(buf); } else L = rescue_L; #if DEBUGGING lua_pushcfunction(L, nlua_errTrace); errf = -2; #else /* DEBUGGING */ errf = 0; #endif /* DEBUGGING */ /* Run Lua. */ lua_getglobal(L,"rescue"); if (lua_pcall(L, 0, 0, errf)) { /* error has occurred */ WARN("Rescue: 'rescue' : '%s'", lua_tostring(L,-1)); lua_pop(L,1); } #if DEBUGGING lua_pop(L,1); #endif }
/** * @brief Loads specially modified basic stuff. * * @param L Lua State to load the basic stuff into. * @return 0 on success. */ int nlua_loadBasic( lua_State* L ) { int i; const char *override[] = { /* unsafe functions */ "collectgarbage", "dofile", "getfenv", "getmetatable", "load", "loadfile", "loadstring", "rawequal", "rawget", "rawset", "setfenv", /*"setmetatable",*/ "END" }; luaL_openlibs(L); /* replace non-safe functions */ for (i=0; strcmp(override[i],"END")!=0; i++) { lua_pushnil(L); lua_setglobal(L, override[i]); } /* Override print to print in the console. */ lua_register(L, "print", cli_print); lua_register(L, "warn", cli_warn); /* add our own */ lua_register(L, "include", nlua_packfileLoader); return 0; } /** * @brief include( string module ) * * Loads a module into the current Lua state from inside the data file. * * @param module Name of the module to load. * @return An error string on error. */ static int nlua_packfileLoader( lua_State* L ) { const char *filename; char *path_filename; char *buf; int len; uint32_t bufsize; /* Get parameters. */ filename = luaL_checkstring(L,1); /* Check to see if already included. */ lua_getglobal( L, "_include" ); /* t */ if (!lua_isnil(L,-1)) { lua_getfield(L,-1,filename); /* t, f */ /* Already included. */ if (!lua_isnil(L,-1)) { lua_pop(L,2); /* */ return 0; } lua_pop(L,2); /* */ } /* Must create new _include table. */ else { lua_newtable(L); /* t */ lua_setglobal(L, "_include"); /* */ } /* Try to locate the data directly */ buf = NULL; if (ndata_exists( filename )) buf = ndata_read( filename, &bufsize ); /* If failed to load or doesn't exist try again with INCLUDE_PATH prefix. */ if (buf == NULL) { /* Try to locate the data in the data path */ len = strlen(LUA_INCLUDE_PATH)+strlen(filename)+2; path_filename = malloc( len ); nsnprintf( path_filename, len, "%s%s", LUA_INCLUDE_PATH, filename ); if (ndata_exists( path_filename )) buf = ndata_read( path_filename, &bufsize ); free( path_filename ); } /* Must have buf by now. */ if (buf == NULL) { lua_pushfstring(L, "%s not found in ndata.", filename); return 1; } /* run the buffer */ if (luaL_dobuffer(L, buf, bufsize, filename) != 0) { /* will push the current error from the dobuffer */ lua_error(L); return 1; } /* Mark as loaded. */ lua_getglobal(L, "_include"); /* t */ lua_pushboolean(L, 1); /* t b */ lua_setfield(L, -2, filename); /* t */ lua_pop(L, 1); /* cleanup, success */ free(buf); return 0; } /** * @brief Loads the standard Naev Lua API. * * Loads the modules: * - naev * - var * - space * - planet * - system * - jumps * - time * - player * - pilot * - rnd * - diff * - faction * - vec2 * - outfit * - commodity * * Only is missing: * - misn * - tk * - hook * - music * * @param L Lua State to load modules into. * @param readonly Load as readonly (good for sandboxing). * @return 0 on success. */ int nlua_loadStandard( lua_State *L, int readonly ) { int r; r = 0; r |= nlua_loadBasic(L); r |= nlua_loadNaev(L); r |= nlua_loadVar(L,readonly); r |= nlua_loadSpace(L,readonly); /* systems, planets, jumps */ r |= nlua_loadTime(L,readonly); r |= nlua_loadPlayer(L,readonly); r |= nlua_loadPilot(L,readonly); r |= nlua_loadRnd(L); r |= nlua_loadDiff(L,readonly); r |= nlua_loadFaction(L,readonly); r |= nlua_loadVector(L); r |= nlua_loadOutfit(L,readonly); r |= nlua_loadCommodity(L,readonly); r |= nlua_loadNews(L,readonly); return r; }