Example #1
0
File: news.c Project: Arakash/naev
/**
 * @brief Initializes the news.
 *
 *    @return 0 on success.
 */
int news_init (void)
{
   lua_State *L;
   char *buf;
   uint32_t bufsize;

   /* Already initialized. */
   if (news_state != NULL)
      return 0;

   /* Create the state. */
   news_state = nlua_newState();
   L = news_state;

   /* Load the libraries. */
   nlua_loadBasic(L);
   nlua_load(L,luaopen_string);
   nlua_loadStandard(L, 1);

   /* Load the news file. */
   buf = ndata_read( LUA_NEWS, &bufsize );
   if (luaL_dobuffer(news_state, buf, bufsize, LUA_NEWS) != 0) {
      WARN("Failed to load news file: %s\n"
           "%s\n"
           "Most likely Lua file has improper syntax, please check",
            LUA_NEWS, lua_tostring(L,-1));
      return -1;
   }
   free(buf);

   return 0;
}
Example #2
0
File: nlua.c Project: pegue/naev
/**
 * @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"
   };


   nlua_load(L,luaopen_base); /* open base. */

   /* replace non-safe functions */
   for (i=0; strcmp(override[i],"END")!=0; i++) {
      lua_pushnil(L);
      lua_setglobal(L, override[i]);
   }

   nlua_load(L,luaopen_math); /* open math. */
   nlua_load(L,luaopen_table); /* open table. */
   nlua_load(L, luaopen_string); /* open string. */

   /* 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 *buf;
   uint32_t bufsize;

   filename = luaL_checkstring(L,1);

   /* try to locate the data */
   buf = ndata_read( filename, &bufsize );
   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;
   }

   /* cleanup, success */
   free(buf);
   return 0;
}


/**
 * @brief Loads the standard NAEV Lua API.
 *
 * Loads the modules:
 *  - naev
 *  - space
 *    - planet
 *    - system
 *  - var
 *  - pilot
 *  - time
 *  - player
 *  - diff
 *  - faction
 *  - vec2
 *
 * 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 |= lua_loadNaev(L);
   r |= lua_loadVar(L,readonly);
   r |= lua_loadSpace(L,readonly); /* planet, system */
   r |= lua_loadTime(L,readonly);
   r |= lua_loadPlayer(L,readonly);
   r |= lua_loadPilot(L,readonly);
   r |= lua_loadRnd(L);
   r |= lua_loadDiff(L,readonly);
   r |= lua_loadFaction(L,readonly);
   r |= lua_loadVector(L);

   return r;
}