コード例 #1
0
ファイル: LuaData.cpp プロジェクト: dreamsxin/solarus
/**
 * \brief Imports a Lua data file from the filesystem to this object.
 *
 * Unlike import_from_quest_file(), this function acts on a regular file on the
 * filesystem, independently of any notion of quest search path.
 *
 * \param[in] file_name Path of the file to load.
 * The file must be encoded in UTF-8.
 * \return \c true in case of success, \c false if the file could not be loaded.
 */
bool LuaData::import_from_file(const std::string& file_name) {

  lua_State* l = luaL_newstate();
  if (luaL_loadfile(l, file_name.c_str()) != 0) {
    Debug::error(std::string("Failed to load data file '") + file_name + "': " + lua_tostring(l, -1));
    lua_pop(l, 1);
    return false;
  }

  bool success = import_from_lua(l);
  lua_close(l);
  return success;
}
コード例 #2
0
ファイル: LuaData.cpp プロジェクト: stavrossk/solarus
/**
 * \brief Imports a Lua data file from memory to this object.
 * \param[in] buffer A memory area with the content of a data file
 * encoded in UTF-8.
 * \return \c true in case of success, \c false if the file could not be loaded.
 */
bool LuaData::import_from_buffer(const std::string& buffer) {

  // Read the file.
  lua_State* l = luaL_newstate();
  if (luaL_loadbuffer(l, buffer.data(), buffer.size(), "data file") != 0) {
    Debug::error(std::string("Failed to load data file: ") + lua_tostring(l, -1));
    lua_pop(l, 1);
    return false;
  }

  bool success = import_from_lua(l);
  lua_close(l);
  return success;
}