static void LuaInit() { LuaObject<PropertiedObject>::RegisterClass(); LuaObject<Body>::RegisterClass(); LuaObject<Ship>::RegisterClass(); LuaObject<SpaceStation>::RegisterClass(); LuaObject<Planet>::RegisterClass(); LuaObject<Star>::RegisterClass(); LuaObject<Player>::RegisterClass(); LuaObject<Missile>::RegisterClass(); LuaObject<CargoBody>::RegisterClass(); LuaObject<ModelBody>::RegisterClass(); LuaObject<StarSystem>::RegisterClass(); LuaObject<SystemPath>::RegisterClass(); LuaObject<SystemBody>::RegisterClass(); LuaObject<Random>::RegisterClass(); LuaObject<Faction>::RegisterClass(); Pi::luaSerializer = new LuaSerializer(); Pi::luaTimer = new LuaTimer(); LuaObject<LuaSerializer>::RegisterClass(); LuaObject<LuaTimer>::RegisterClass(); LuaConstants::Register(Lua::manager->GetLuaState()); LuaLang::Register(); LuaEngine::Register(); LuaFileSystem::Register(); LuaGame::Register(); LuaComms::Register(); LuaFormat::Register(); LuaSpace::Register(); LuaShipDef::Register(); LuaMusic::Register(); LuaDev::Register(); LuaConsole::Register(); // XXX sigh UI::Lua::Init(); GameUI::Lua::Init(); SceneGraph::Lua::Init(); // XXX load everything. for now, just modules lua_State *l = Lua::manager->GetLuaState(); pi_lua_dofile(l, "libs/autoload.lua"); pi_lua_dofile_recursive(l, "ui"); pi_lua_dofile_recursive(l, "modules"); Pi::luaNameGen = new LuaNameGen(Lua::manager); }
static void LuaInit() { LuaBody::RegisterClass(); LuaShip::RegisterClass(); LuaSpaceStation::RegisterClass(); LuaPlanet::RegisterClass(); LuaStar::RegisterClass(); LuaPlayer::RegisterClass(); LuaCargoBody::RegisterClass(); LuaStarSystem::RegisterClass(); LuaSystemPath::RegisterClass(); LuaSystemBody::RegisterClass(); LuaShipType::RegisterClass(); LuaEquipType::RegisterClass(); LuaRand::RegisterClass(); LuaFaction::RegisterClass(); LuaObject<LuaChatForm>::RegisterClass(); Pi::luaSerializer = new LuaSerializer(); Pi::luaTimer = new LuaTimer(); LuaObject<LuaSerializer>::RegisterClass(); LuaObject<LuaTimer>::RegisterClass(); LuaConstants::Register(Lua::manager->GetLuaState()); LuaLang::Register(); LuaEngine::Register(); LuaFileSystem::Register(); LuaGame::Register(); LuaComms::Register(); LuaFormat::Register(); LuaSpace::Register(); LuaMusic::Register(); LuaDev::Register(); LuaConsole::Register(); // XXX sigh UI::Lua::Init(); GameUI::Lua::Init(); // XXX load everything. for now, just modules lua_State *l = Lua::manager->GetLuaState(); pi_lua_dofile_recursive(l, "libs"); pi_lua_dofile_recursive(l, "ui"); pi_lua_dofile_recursive(l, "modules"); Pi::luaNameGen = new LuaNameGen(Lua::manager); }
// XXX compatibility int pi_load_lua(lua_State *l) { const std::string path = luaL_checkstring(l, 1); FileSystem::FileInfo info = FileSystem::gameDataFiles.Lookup(path); lua_getglobal(l, "CurrentDirectory"); std::string currentDir = luaL_optstring(l, -1, ""); lua_pop(l, 1); if (info.IsDir()) { pi_lua_dofile_recursive(l, path); } else if (info.IsFile() && (path.size() > 4) && (path.substr(path.size() - 4) == ".lua")) { pi_lua_dofile(l, path); } else if (info.IsFile()) { return luaL_error(l, "load_lua('%s') called on a file without a .lua extension", path.c_str()); } else if (!info.Exists()) { return luaL_error(l, "load_lua('%s') called on a path that doesn't exist", path.c_str()); } else { return luaL_error(l, "load_lua('%s') called on a path that doesn't refer to a valid file", path.c_str()); } if (currentDir.empty()) lua_pushnil(l); else lua_pushlstring(l, currentDir.c_str(), currentDir.size()); lua_setglobal(l, "CurrentDirectory"); return 0; }
void pi_lua_dofile_recursive(lua_State *l, const std::string &basepath) { LUA_DEBUG_START(l); for (FileSystem::FileEnumerator files(FileSystem::gameDataFiles, basepath, FileSystem::FileEnumerator::IncludeDirs); !files.Finished(); files.Next()) { const FileSystem::FileInfo &info = files.Current(); const std::string &fpath = info.GetPath(); if (info.IsDir()) { pi_lua_dofile_recursive(l, fpath); } else { assert(info.IsFile()); if ((fpath.size() > 4) && (fpath.substr(fpath.size() - 4) == ".lua")) { // XXX kill CurrentDirectory lua_pushstring(l, basepath.empty() ? "." : basepath.c_str()); lua_setglobal(l, "CurrentDirectory"); RefCountedPtr<FileSystem::FileData> code = info.Read(); pi_lua_dofile(l, *code); } } LUA_DEBUG_CHECK(l, 0); } LUA_DEBUG_END(l, 0); }
void CustomSystem::Init() { lua_State *L = luaL_newstate(); LUA_DEBUG_START(L); pi_lua_open_standard_base(L); LuaVector::Register(L); LuaFixed::Register(L); LuaConstants::Register(L); // create a shortcut f = fixed.new lua_getglobal(L, LuaFixed::LibName); lua_getfield(L, -1, "new"); assert(lua_iscfunction(L, -1)); lua_setglobal(L, "f"); lua_pop(L, 1); // pop the fixed table // provide shortcut vector constructor: v = vector.new lua_getglobal(L, LuaVector::LibName); lua_getfield(L, -1, "new"); assert(lua_iscfunction(L, -1)); lua_setglobal(L, "v"); lua_pop(L, 1); // pop the vector table LUA_DEBUG_CHECK(L, 0); RegisterCustomSystemsAPI(L); LUA_DEBUG_CHECK(L, 0); pi_lua_dofile_recursive(L, "systems"); LUA_DEBUG_END(L, 0); lua_close(L); }
void Game::InitLua() { LuaObject<PropertiedObject>::RegisterClass(); LuaConstants::Register(Lua::manager->GetLuaState()); LuaLang::Register(); p3::LuaEngine::Register(); p3::LuaGame::Register(); LuaFileSystem::Register(); LuaConsole::Register(); UI::Lua::Init(); lua_State* l = Lua::manager->GetLuaState(); pi_lua_dofile(l, "libs/autoload.lua"); pi_lua_dofile_recursive(l, "ui"); pi_lua_dofile_recursive(l, "modules"); }
//static void Faction::Init() { lua_State *L = luaL_newstate(); LUA_DEBUG_START(L); pi_lua_open_standard_base(L); LuaConstants::Register(L); LUA_DEBUG_CHECK(L, 0); RegisterFactionsAPI(L); LUA_DEBUG_CHECK(L, 0); pi_lua_dofile_recursive(L, "factions"); LUA_DEBUG_END(L, 0); lua_close(L); printf("Number of factions added: " SIZET_FMT "\n", s_factions.size()); StarSystem::ShrinkCache(); // clear the star system cache of anything we used for faction generation }
void FactionsDatabase::Init() { lua_State *L = luaL_newstate(); LUA_DEBUG_START(L); pi_lua_open_standard_base(L); LuaConstants::Register(L); LUA_DEBUG_CHECK(L, 0); RegisterFactionsAPI(L); LUA_DEBUG_CHECK(L, 0); pi_lua_dofile_recursive(L, "factions"); LUA_DEBUG_END(L, 0); lua_close(L); Output("Number of factions added: " SIZET_FMT "\n", m_factions.size()); ClearHomeSectors(); Pi::FlushCaches(); // clear caches of anything we used for faction generation while (!m_missingFactionsMap.empty()) { const std::string& factionName = m_missingFactionsMap.begin()->first; std::list<CustomSystem*>& csl = m_missingFactionsMap.begin()->second; while (!csl.empty()) { CustomSystem* cs = csl.front(); // FIXME: How to signal missing faction? fprintf(stderr, "Custom system %s referenced unknown faction %s\n", cs->name.c_str(), factionName.c_str()); csl.pop_front(); } m_missingFactionsMap.erase(m_missingFactionsMap.begin()); } m_initialized = true; SetHomeSectors(); }
void pi_lua_dofile_recursive(lua_State *l, const std::string &basepath) { DIR *dir; struct dirent *entry; std::string path; struct stat info; // putting directory contents into sorted order so order of // model definition is consistent std::set<std::string> entries; LUA_DEBUG_START(l); // XXX CurrentDirectory stuff has to go lua_getglobal(l, "CurrentDirectory"); std::string save_dir = luaL_checkstring(l, -1); lua_pop(l, 1); lua_pushstring(l, basepath.c_str()); lua_setglobal(l, "CurrentDirectory"); if ((dir = opendir(basepath.c_str())) == NULL) { fprintf(stderr, "opendir: couldn't open directory '%s': %s\n", basepath.c_str(), strerror(errno)); return; } while ((entry = readdir(dir)) != NULL) { if (entry->d_name[0] != '.') { entries.insert(entry->d_name); } } closedir(dir); for (std::set<std::string>::iterator i = entries.begin(); i!=entries.end(); ++i) { const std::string &name = *i; path = basepath + "/" + name; if (stat(path.c_str(), &info) != 0) { fprintf(stderr, "stat: couldn't get info for '%s': %s\n", path.c_str(), strerror(errno)); continue; } if (S_ISDIR(info.st_mode)) { pi_lua_dofile_recursive(l, path.c_str()); continue; } if ( name.size() >= 4 && name.find(".lua") == name.size() - 4 ) { // XXX panic stuff can be removed once the global lua is used everywhere lua_pushcfunction(l, pi_lua_panic); if (luaL_loadfile(l, path.c_str())) { pi_lua_panic(l); } else { lua_pcall(l, 0, 0, -2); } lua_pop(l, 1); } } lua_pushstring(l, save_dir.c_str()); lua_setglobal(l, "CurrentDirectory"); LUA_DEBUG_END(l, 0); }
// XXX compatibility int pi_load_lua(lua_State *l) { const char *path = luaL_checkstring(l, 1); pi_lua_dofile_recursive(l, path); return 0; }