Example #1
0
/**
 * @brief Initialize the music Lua control system.
 *
 *    @return 0 on success.
 */
static int music_luaInit (void)
{
   char *buf;
   uint32_t bufsize;

   if (music_disabled)
      return 0;

   if (music_lua != NULL)
      music_luaQuit();

   music_lua = nlua_newState();
   nlua_loadBasic(music_lua);
   nlua_loadStandard(music_lua,1);
   nlua_loadMusic(music_lua,0); /* write it */

   /* load the actual Lua music code */
   buf = ndata_read( MUSIC_LUA_PATH, &bufsize );
   if (luaL_dobuffer(music_lua, buf, bufsize, MUSIC_LUA_PATH) != 0) {
      ERR("Error loading music file: %s\n"
          "%s\n"
          "Most likely Lua file has improper syntax, please check",
            MUSIC_LUA_PATH, lua_tostring(music_lua,-1) );
      return -1;
   }
   free(buf);

   return 0;
}
Example #2
0
File: console.c Project: pegue/naev
/**
 * @brief Initializes the CLI environment.
 */
int cli_init (void)
{
   /* Already loaded. */
   if (cli_state != NULL)
      return 0;

   /* Calculate size. */
   cli_width  = SCREEN_W - 100;
   cli_height = SCREEN_H - 100;

   /* Create the state. */
   cli_state = nlua_newState();
   nlua_loadBasic( cli_state );
   nlua_loadStandard( cli_state, 0 );
   nlua_loadCLI( cli_state );
   luaL_register( cli_state, "_G", cli_methods );
   lua_settop( cli_state, 0 );

   /* Set the font. */
   cli_font = malloc( sizeof(glFont) );
   gl_fontInit( cli_font, "dat/mono.ttf", CONSOLE_FONT_SIZE );

   /* Clear the buffer. */
   memset( cli_buffer, 0, sizeof(cli_buffer) );

   /* Put a friendly message at first. */
   cli_addMessage( "Welcome to the Lua console!" );
   cli_addMessage( "" );

   return 0;
}
Example #3
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 #4
0
/**
 * @brief Initializes a mission.
 *
 *    @param mission Mission to initialize.
 *    @param misn Data to use.
 *    @param genid 1 if should generate id, 0 otherwise.
 *    @param create 1 if should run create function, 0 otherwise.
 *    @param[out] id ID of the newly created mission.
 *    @return 0 on success.
 */
static int mission_init( Mission* mission, MissionData* misn, int genid, int create, unsigned int *id )
{
   char *buf;
   uint32_t bufsize;
   int ret;

   /* clear the mission */
   memset( mission, 0, sizeof(Mission) );

   /* Create id if needed. */
   mission->id    = (genid) ? mission_genID() : 0;
   if (id != NULL)
      *id         = mission->id;
   mission->data  = misn;
   if (create) {
      mission->title = strdup(misn->name);
      mission->desc  = strdup("No description.");
   }

   /* init Lua */
   mission->L = nlua_newState();
   if (mission->L == NULL) {
      WARN("Unable to create a new Lua state.");
      return -1;
   }
   nlua_loadBasic( mission->L ); /* pairs and such */
   misn_loadLibs( mission->L ); /* load our custom libraries */

   /* load the file */
   buf = ndata_read( misn->lua, &bufsize );
   if (buf == NULL) {
      WARN("Mission '%s' Lua script not found.", misn->lua );
      return -1;
   }
   if (luaL_dobuffer(mission->L, buf, bufsize, misn->lua) != 0) {
      WARN("Error loading mission file: %s\n"
          "%s\n"
          "Most likely Lua file has improper syntax, please check",
            misn->lua, lua_tostring(mission->L,-1));
      free(buf);
      return -1;
   }
   free(buf);

   /* run create function */
   if (create) {
      /* Failed to create. */
      ret = misn_run( mission, "create");
      if (ret) {
         mission_cleanup(mission);
         return ret;
      }
   }

   return 0;
}
Example #5
0
/**
 * @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;
}