void SpaceStationType::Init() { assert(s_lua == 0); if (s_lua != 0) return; s_lua = luaL_newstate(); lua_State *L = s_lua; LUA_DEBUG_START(L); pi_lua_open_standard_base(L); LuaVector::Register(L); LUA_DEBUG_CHECK(L, 0); lua_register(L, "define_orbital_station", define_orbital_station); lua_register(L, "define_surface_station", define_surface_station); namespace fs = FileSystem; for (fs::FileEnumerator files(fs::gameDataFiles, "stations", fs::FileEnumerator::Recurse); !files.Finished(); files.Next()) { const fs::FileInfo &info = files.Current(); if (ends_with_ci(info.GetPath(), ".lua")) { const std::string name = info.GetName(); s_currentStationFile = name.substr(0, name.size()-4); pi_lua_dofile(L, info.GetPath()); s_currentStationFile.clear(); } } LUA_DEBUG_END(L, 0); }
/*static*/ void SpaceStationType::Init() { static bool isInitted = false; if (isInitted) return; isInitted = true; // load all station definitions namespace fs = FileSystem; for (fs::FileEnumerator files(fs::gameDataFiles, "stations", 0); !files.Finished(); files.Next()) { const fs::FileInfo &info = files.Current(); if (ends_with_ci(info.GetPath(), ".json")) { const std::string id(info.GetName().substr(0, info.GetName().size() - 5)); try { SpaceStationType st = SpaceStationType(id, info.GetPath()); switch (st.dockMethod) { case SURFACE: surfaceTypes.push_back(st); break; case ORBITAL: orbitalTypes.push_back(st); break; } } catch (StationTypeLoadError) { // TODO: Actual error handling would be nice. Error("Error while loading Space Station data (check stdout/output.txt).\n"); } } } }
void ModManager::Init() { FileSystem::userFiles.MakeDirectory("mods"); for (FileSystem::FileEnumerator files(FileSystem::userFiles, "mods", 0); !files.Finished(); files.Next()) { const FileSystem::FileInfo &info = files.Current(); const std::string &zipPath = info.GetPath(); if (ends_with_ci(zipPath, ".zip")) { Output("adding mod: %s\n", zipPath.c_str()); FileSystem::gameDataFiles.PrependSource(new FileSystem::FileSourceZip(FileSystem::userFiles, zipPath)); } } }
std::vector<std::string> Resource::GetAvailableLanguages(const std::string &resourceName) { std::vector<std::string> languages; for (FileSystem::FileEnumerator files(FileSystem::gameDataFiles, "lang/" + resourceName); !files.Finished(); files.Next()) { assert(files.Current().IsFile()); const std::string &path = files.Current().GetPath(); if (ends_with_ci(path, ".json")) { const std::string name = files.Current().GetName(); languages.push_back(name.substr(0, name.size() - 5)); } } return languages; }
/*static*/ void SpaceStationType::Init() { static bool isInitted = false; if (isInitted) return; isInitted = true; // load all station definitions namespace fs = FileSystem; for (fs::FileEnumerator files(fs::gameDataFiles, "stations", 0); !files.Finished(); files.Next()) { const fs::FileInfo &info = files.Current(); if (ends_with_ci(info.GetPath(), ".json")) { const std::string id(info.GetName().substr(0, info.GetName().size()-5)); SpaceStationType st = SpaceStationType(id, info.GetPath()); switch (st.dockMethod) { case SURFACE: surfaceTypes.push_back(st); break; case ORBITAL: orbitalTypes.push_back(st); break; } } } }
void ShipType::Init() { static bool isInitted = false; if (isInitted) return; isInitted = true; // load all ship definitions namespace fs = FileSystem; for (fs::FileEnumerator files(fs::gameDataFiles, "ships", fs::FileEnumerator::Recurse); !files.Finished(); files.Next()) { const fs::FileInfo &info = files.Current(); if (ends_with_ci(info.GetPath(), ".json")) { const std::string id(info.GetName().substr(0, info.GetName().size()-5)); ShipType st = ShipType(id, info.GetPath()); types.insert(std::make_pair(st.id, st)); // assign the names to the various lists switch( st.tag ) { case TAG_SHIP: player_ships.push_back(id); break; case TAG_STATIC_SHIP: static_ships.push_back(id); break; case TAG_MISSILE: missile_ships.push_back(id); break; break; case TAG_NONE: default: break; } } } #if ALLOW_LUA_SHIP_DEF lua_State *l = luaL_newstate(); LUA_DEBUG_START(l); luaL_requiref(l, "_G", &luaopen_base, 1); luaL_requiref(l, LUA_DBLIBNAME, &luaopen_debug, 1); luaL_requiref(l, LUA_MATHLIBNAME, &luaopen_math, 1); lua_pop(l, 3); LuaConstants::Register(l); LuaVector::Register(l); LUA_DEBUG_CHECK(l, 0); // 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 library table LUA_DEBUG_CHECK(l, 0); // register ship definition functions lua_register(l, "define_ship", define_ship); lua_register(l, "define_static_ship", define_static_ship); lua_register(l, "define_missile", define_missile); LUA_DEBUG_CHECK(l, 0); // load all ship definitions namespace fs = FileSystem; for (fs::FileEnumerator files(fs::gameDataFiles, "ships", fs::FileEnumerator::Recurse); !files.Finished(); files.Next()) { const fs::FileInfo &info = files.Current(); if (ends_with_ci(info.GetPath(), ".lua")) { const std::string name = info.GetName(); s_currentShipFile = name.substr(0, name.size() - 4); if (ShipType::types.find(s_currentShipFile) == ShipType::types.end()) { pi_lua_dofile(l, info.GetPath()); s_currentShipFile.clear(); } } } LUA_DEBUG_END(l, 0); lua_close(l); #endif //remove unbuyable ships from player ship list ShipType::player_ships.erase( std::remove_if(ShipType::player_ships.begin(), ShipType::player_ships.end(), ShipIsUnbuyable), ShipType::player_ships.end()); if (ShipType::player_ships.empty()) Error("No playable ships have been defined! The game cannot run."); }
void ShipType::Init() { static bool isInitted = false; if (isInitted) return; isInitted = true; lua_State *l = luaL_newstate(); LUA_DEBUG_START(l); luaL_requiref(l, "_G", &luaopen_base, 1); luaL_requiref(l, LUA_DBLIBNAME, &luaopen_debug, 1); luaL_requiref(l, LUA_MATHLIBNAME, &luaopen_math, 1); lua_pop(l, 3); LuaConstants::Register(l); LuaVector::Register(l); LUA_DEBUG_CHECK(l, 0); // 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 library table LUA_DEBUG_CHECK(l, 0); // register ship definition functions lua_register(l, "define_ship", define_ship); lua_register(l, "define_static_ship", define_static_ship); lua_register(l, "define_missile", define_missile); LUA_DEBUG_CHECK(l, 0); // load all ship definitions namespace fs = FileSystem; for (fs::FileEnumerator files(fs::gameDataFiles, "ships", fs::FileEnumerator::Recurse); !files.Finished(); files.Next()) { const fs::FileInfo &info = files.Current(); if (ends_with_ci(info.GetPath(), ".lua")) { const std::string name = info.GetName(); s_currentShipFile = name.substr(0, name.size()-4); pi_lua_dofile(l, info.GetPath()); s_currentShipFile.clear(); } } LUA_DEBUG_END(l, 0); lua_close(l); //remove unbuyable ships from player ship list ShipType::player_ships.erase( std::remove_if(ShipType::player_ships.begin(), ShipType::player_ships.end(), ShipIsUnbuyable), ShipType::player_ships.end()); if (ShipType::player_ships.empty()) Error("No playable ships have been defined! The game cannot run."); //collect ships that can fit atmospheric shields for (std::vector<ShipType::Id>::const_iterator it = ShipType::player_ships.begin(); it != ShipType::player_ships.end(); ++it) { const ShipType &ship = ShipType::types[*it]; if (ship.equipSlotCapacity[Equip::SLOT_ATMOSHIELD] != 0) ShipType::playable_atmospheric_ships.push_back(*it); } if (ShipType::playable_atmospheric_ships.empty()) Error("No ships can fit atmospheric shields! The game cannot run."); }
void TextureBuilder::PrepareSurface() { if (m_prepared) return; if (!m_surface && !m_filename.empty()) { std::string filename = m_filename; std::transform(filename.begin(), filename.end(), filename.begin(), ::tolower); if (ends_with_ci(filename, ".dds")) { LoadDDS(); } else { LoadSurface(); } } TextureFormat targetTextureFormat; unsigned int virtualWidth, actualWidth, virtualHeight, actualHeight, numberOfMipMaps = 0, numberOfImages = 1; if( m_surface ) { SDL_PixelFormat *targetPixelFormat; bool needConvert = !GetTargetFormat(m_surface->format, &targetTextureFormat, &targetPixelFormat, m_forceRGBA); if (needConvert) { if(m_textureType == TEXTURE_2D) { SDL_Surface *s = SDL_ConvertSurface(m_surface.Get(), targetPixelFormat, SDL_SWSURFACE); m_surface = SDLSurfacePtr::WrapNew(s); } else if(m_textureType == TEXTURE_CUBE_MAP) { assert(m_cubemap.size() == 6); for(unsigned int i = 0; i < 6; ++i) { SDL_Surface *s = SDL_ConvertSurface(m_cubemap[i].Get(), targetPixelFormat, SDL_SWSURFACE); m_cubemap[i] = SDLSurfacePtr::WrapNew(s); } } else { // Unknown texture type assert(0); } } virtualWidth = actualWidth = m_surface->w; virtualHeight = actualHeight = m_surface->h; if (m_potExtend) { // extend to power-of-two if necessary actualWidth = ceil_pow2(m_surface->w); actualHeight = ceil_pow2(m_surface->h); if (actualWidth != virtualWidth || actualHeight != virtualHeight) { if(m_textureType == TEXTURE_2D) { SDL_Surface *s = SDL_CreateRGBSurface(SDL_SWSURFACE, actualWidth, actualHeight, targetPixelFormat->BitsPerPixel, targetPixelFormat->Rmask, targetPixelFormat->Gmask, targetPixelFormat->Bmask, targetPixelFormat->Amask); SDL_SetSurfaceBlendMode(m_surface.Get(), SDL_BLENDMODE_NONE); SDL_BlitSurface(m_surface.Get(), 0, s, 0); m_surface = SDLSurfacePtr::WrapNew(s); } else if(m_textureType == TEXTURE_CUBE_MAP) { assert(m_cubemap.size() == 6); for(unsigned int i = 0; i < 6; ++i) { SDL_Surface *s = SDL_CreateRGBSurface(SDL_SWSURFACE, actualWidth, actualHeight, targetPixelFormat->BitsPerPixel, targetPixelFormat->Rmask, targetPixelFormat->Gmask, targetPixelFormat->Bmask, targetPixelFormat->Amask); SDL_SetSurfaceBlendMode(m_cubemap[i].Get(), SDL_BLENDMODE_NONE); SDL_BlitSurface(m_cubemap[i].Get(), 0, s, 0); m_cubemap[i] = SDLSurfacePtr::WrapNew(s); } } else { assert(0); } } } else if (! m_filename.empty()) { // power-of-two check unsigned long width = ceil_pow2(m_surface->w); unsigned long height = ceil_pow2(m_surface->h); if (width != virtualWidth || height != virtualHeight) Output("WARNING: texture '%s' is not power-of-two and may not display correctly\n", m_filename.c_str()); } } else { switch(m_dds.GetTextureFormat()) { case PicoDDS::FORMAT_DXT1: targetTextureFormat = TEXTURE_DXT1; break; case PicoDDS::FORMAT_DXT5: targetTextureFormat = TEXTURE_DXT5; break; default: Output("ERROR: DDS texture with invalid format '%s' (only DXT1 and DXT5 are supported)\n", m_filename.c_str()); assert(false); return; } virtualWidth = actualWidth = m_dds.imgdata_.width; virtualHeight = actualHeight = m_dds.imgdata_.height; numberOfMipMaps = m_dds.imgdata_.numMipMaps; numberOfImages = m_dds.imgdata_.numImages; if(m_textureType == TEXTURE_CUBE_MAP) { // Cube map must be fully defined (6 images) to be used correctly assert(numberOfImages == 6); } } m_descriptor = TextureDescriptor( targetTextureFormat, vector2f(actualWidth,actualHeight), vector2f(float(virtualWidth)/float(actualWidth),float(virtualHeight)/float(actualHeight)), m_sampleMode, m_generateMipmaps, m_compressTextures, numberOfMipMaps, m_textureType); m_prepared = true; }
int main(int argc, char** argv) { #ifdef PIONEER_PROFILER Profiler::detect( argc, argv ); #endif RunMode mode = MODE_MODELCOMPILER; if (argc > 1) { const char switchchar = argv[1][0]; if (!(switchchar == '-' || switchchar == '/')) { mode = MODE_USAGE_ERROR; goto start; } const std::string modeopt(std::string(argv[1]).substr(1)); if (modeopt == "compile" || modeopt == "c") { mode = MODE_MODELCOMPILER; goto start; } if (modeopt == "batch" || modeopt == "b") { mode = MODE_MODELBATCHEXPORT; goto start; } if (modeopt == "version" || modeopt == "v") { mode = MODE_VERSION; goto start; } if (modeopt == "help" || modeopt == "h" || modeopt == "?") { mode = MODE_USAGE; goto start; } mode = MODE_USAGE_ERROR; } start: // Init here since we'll need it for both batch and RunCompiler modes. FileSystem::Init(); // what mode are we in? switch (mode) { case MODE_MODELCOMPILER: { std::string modelName; if (argc > 2) { modelName = argv[2]; SetupRenderer(); RunCompiler(modelName, s_dummyPath, false); } break; } case MODE_MODELBATCHEXPORT: { // determine if we're meant to be writing these in the source directory bool isInPlace = false; if (argc > 2) { std::string arg2 = argv[2]; isInPlace = (arg2 == "inplace" || arg2 == "true"); } // find all of the models std::vector<std::pair<std::string, std::string>> list_model; FileSystem::FileSource &fileSource = FileSystem::gameDataFiles; for (FileSystem::FileEnumerator files(fileSource, "models", FileSystem::FileEnumerator::Recurse); !files.Finished(); files.Next()) { const FileSystem::FileInfo &info = files.Current(); const std::string &fpath = info.GetPath(); //check it's the expected type if (info.IsFile()) { if (ends_with_ci(fpath, ".model")) { // store the path for ".model" files list_model.push_back( std::make_pair(info.GetName().substr(0, info.GetName().size()-6), fpath) ); } } } SetupRenderer(); for (auto &modelName : list_model) { RunCompiler(modelName.first, modelName.second, isInPlace); } break; } case MODE_VERSION: { std::string version(PIONEER_VERSION); if (strlen(PIONEER_EXTRAVERSION)) version += " (" PIONEER_EXTRAVERSION ")"; Output("modelcompiler %s\n", version.c_str()); break; } case MODE_USAGE_ERROR: Output("modelcompiler: unknown mode %s\n", argv[1]); // fall through case MODE_USAGE: Output( "usage: modelcompiler [mode] [options...]\n" "available modes:\n" " -compile [-c] model compiler\n" " -batch [-b] batch mode output into users home/Pioneer directory\n" " -batch inplace [-b inplace] batch mode output into the source folder\n" " -version [-v] show version\n" " -help [-h,-?] this help\n" ); break; } return 0; }