bool CTilemap::loadHiresTile( const std::string& filename, const std::string& path ) { std::string fullfilename; // Cycle through possible filename extensions, when more formats are supported for( auto &ext : exts ) { fullfilename = filename + "." + ext; fullfilename = getResourceFilename(fullfilename, path, false); if(!fullfilename.empty()) break; } if(!IsFileAvailable(fullfilename)) return false; if(m_Tilesurface) { SDL_Surface *temp_surface = IMG_Load(GetFullFileName(fullfilename).c_str()); if(temp_surface) { SDL_FreeSurface(m_Tilesurface); m_Tilesurface = temp_surface; return true; } else { g_pLogFile->textOut(RED, "IMG_Load: %s\n", IMG_GetError()); g_pLogFile->textOut(RED, "IMG_Load: CG will ignore those images\n"); } } return false; }
bool GsTilemap::loadHiresTile( const std::string& filename, const std::string& path ) { std::string fullfilename; // Cycle through possible filename extensions, when more formats are supported for( auto &ext : exts ) { fullfilename = filename + "." + ext; fullfilename = getResourceFilename(fullfilename, path, false); if(!fullfilename.empty()) break; } if(!IsFileAvailable(fullfilename)) return false; if(!mTileSurface.empty()) { SDL_Surface *temp_surface = IMG_Load(GetFullFileName(fullfilename).c_str()); if(temp_surface) { GsWeakSurface tempWeak(temp_surface); mTileSurface.createCopy(tempWeak); SDL_FreeSurface(temp_surface); return true; } else { gLogging.textOut(FONTCOLORS::RED, "IMG_Load: %s\n", IMG_GetError()); gLogging.textOut(FONTCOLORS::RED, "IMG_Load: CG will ignore those images\n"); } } return false; }
/////////////////// // Load the weapons restrictions list void CWpnRest::loadList(const std::string& szFilename, const std::string& moddir) { // Shutdown the list first Shutdown(); std::string fn = szFilename; if(!strCaseStartsWith(fn, "cfg/")) { if(fn.size() > 4 && !stringcaseequal(fn.substr(fn.size()-4), ".wps")) fn += ".wps"; if(moddir != "" && IsFileAvailable("cfg/presets/" + moddir + "/" + fn)) fn = "cfg/presets/" + moddir + "/" + fn; else fn = "cfg/presets/" + fn; } FILE *fp = OpenGameFile(fn, "rt"); if( !fp ) return; std::string line; while( !feof(fp) && !ferror(fp) ) { line = ReadUntil(fp, '\n'); std::vector<std::string> exploded = explode(line,","); if (exploded.size() >= 2) addWeapon(exploded[0],from_string<int>(exploded[1])); } fclose(fp); // Sort the list sortList(); }
/** * \brief This function will look for certain files used in the game. It looks on the local dir * and is it didn't find the file, it will look on the global director GLOBAL_DIR. In case * it finds something there, it return that alternate path. * \param filename the filename for which we are looking * \param gamepath if a game started, path to that one * \param essential essential means whether the file is really needed or the game can live without it. * \param can_be_global This means, that the file can be read from the global directory. In some cases it must not. */ std::string getResourceFilename(const std::string &filename, const std::string &gamepath, bool essential, bool can_be_global) { std::string text; std::string vfs_path; if(gamepath != "") { vfs_path = JoinPaths(gamepath, filename); text = "Looking for " + vfs_path + " ... "; // try to look at the local path of the game. if(!IsFileAvailable(vfs_path)) vfs_path = ""; } if(vfs_path == "" && can_be_global) { // if it didn't find the file try looking at the global resources vfs_path = JoinPaths(GLOBAL_DIR, filename); text = "Looking for " + vfs_path + " ... "; if(!IsFileAvailable(vfs_path)) vfs_path = ""; } if( vfs_path == "" ) { if(essential) { text += "missing!\n"; text += "The file " + filename + " was not found. Please provide that file!\n"; } else text.clear(); } else text += "found!\n"; if(!text.empty()) gLogging.textOut(GREEN, text); return vfs_path; }
std::string ProcessLevel(const std::vector<std::string>& params, int sender_id) { // Check the sender if (sender_id < 0 || sender_id >= MAX_WORMS) return "Invalid worm"; // Param check if (params.size() < GetCommand(&ProcessLevel)->iMinParamCount) return "Too few parameters"; // Check that we're in lobby if (cServer->getState() != SVS_LOBBY) return "Cannot change the level while playing"; // Check privileges CWorm *w = cServer->getWorms() + sender_id; CServerConnection *cl = w->getClient(); if (!cl || !cl->getRights()->ChooseLevel) return "You do not have sufficient privileges to change the level"; // Join the parameters into one string std::string level = *params.begin(); for (std::vector<std::string>::const_iterator it = params.begin() + 1; it != params.end(); it++) level += " " + *it; // Check if the level is available if (!IsFileAvailable("levels/" + level, false, false)) return "Level \"" + level + "\" not available"; std::string name = CMap::GetLevelName(level); if (!name.size()) return "The level file is corrupted"; // Set the level gameSettings.overwrite[FT_Map].as<LevelInfo>()->path = level; gameSettings.overwrite[FT_Map].as<LevelInfo>()->name = name; if (!bDedicated) DeprecatedGUI::Menu_Net_HostLobbySetLevel(level); cServer->UpdateGameLobby(); // Notify everybody cServer->SendGlobalText(w->getName() + " changed level to " + name, TXT_NOTICE); return ""; }
bool CFont::loadHiColourFont( const std::string& filename ) { if(!IsFileAvailable(filename)) return false; if(m_FontSurface) { SDL_Surface *temp_surface = SDL_LoadBMP(GetFullFileName(filename).c_str()); if(temp_surface) { SDL_Surface *displaysurface = SDL_ConvertSurface(temp_surface, m_FontSurface->format, m_FontSurface->flags); SDL_BlitSurface(displaysurface, NULL, m_FontSurface, NULL); SDL_FreeSurface(displaysurface); SDL_FreeSurface(temp_surface); return true; } } return false; }
bool CSprite::loadHQSprite( const std::string& filename ) { if(!IsFileAvailable(filename)) return false; if(!mpSurface.empty()) { const std::string fullpath = GetFullFileName(filename); SmartPointer<SDL_Surface> temp_surface = SDL_LoadBMP( fullpath.c_str() ); if(!temp_surface.empty()) { SmartPointer<SDL_Surface> displaysurface = SDL_ConvertSurface(temp_surface.get(), mpSurface->format, mpSurface->flags); readMask(displaysurface.get()); readBBox(displaysurface.get()); SDL_BlitSurface(displaysurface.get(), NULL, mpSurface.get(), NULL); return true; } } return false; }
bool GsBitmap::loadHQBitmap( const std::string& filename ) { if(!IsFileAvailable(filename)) return false; if( mpBitmapSurface ) { SDL_Surface *tempSurface = SDL_LoadBMP(GetFullFileName(filename).c_str()); if(tempSurface) { SDL_Surface *displaysurface = SDL_ConvertSurface(tempSurface, mpBitmapSurface->format, mpBitmapSurface->flags); SDL_BlitSurface(displaysurface, NULL, mpBitmapSurface.get(), NULL); SDL_FreeSurface(displaysurface); SDL_FreeSurface(tempSurface); return true; } } return false; }
bool CSprite::loadHQSprite( const std::string& filename ) { if(!IsFileAvailable(filename)) return false; if(mpSurface) { const std::string fullpath = GetFullFileName(filename); std::unique_ptr<SDL_Surface, SDL_Surface_Deleter> temp_surface(SDL_LoadBMP( fullpath.c_str() )); if(temp_surface) { std::unique_ptr<SDL_Surface, SDL_Surface_Deleter> displaysurface( SDL_ConvertSurface(temp_surface.get(), mpSurface->format, mpSurface->flags)); readMask(displaysurface.get()); readBBox(displaysurface.get()); SDL_BlitSurface(displaysurface.get(), NULL, mpSurface.get(), NULL); return true; } } return false; }
//// // Initialization Routine //// bool CGameLauncher::setupMenu() { m_mustquit = false; mDonePatchSelection = false; m_chosenGame = -1; m_ep1slot = -1; mLauncherDialog.initEmptyBackground(); mSelection = -1; bool gamesDetected = false; // TODO: Put that scanning into a separate so we can show a loading menu // Scan for games... m_DirList.clear(); m_Entries.clear(); gLogging.ftextOut("Game Autodetection Started<br>" ); // Process any custom labels getLabels(); // Scan VFS DIR_ROOT for exe's gamesDetected |= scanExecutables(DIR_ROOT); mGameScanner.setPermilage(100); // Recursivly scan into DIR_GAMES subdir's for exe's gamesDetected |= scanSubDirectories(DIR_GAMES, DEPTH_MAX_GAMES, 200, 900); mpSelList = new CGUITextSelectionList(); // Save any custom labels putLabels(); // Create an empty Bitmap control mLauncherDialog.addControl( new CGUIBitmap(), GsRect<float>(0.51f, 0.07f, 0.48f, 0.48f) ); mCurrentBmp = std::dynamic_pointer_cast< CGUIBitmap > ( mLauncherDialog.getControlList().back() ); mpPrevievBmpVec.resize(m_Entries.size()); std::vector<GameEntry>::iterator it = m_Entries.begin(); unsigned int i=0; for( ; it != m_Entries.end() ; it++ ) { mpSelList->addText(it->name); // And try to add a preview bitmap std::string fullfilename = "preview.bmp"; fullfilename = getResourceFilename(fullfilename, it->path, false); fullfilename = GetFullFileName(fullfilename); if(IsFileAvailable(fullfilename)) { SDL_Surface *pPrimBmp = SDL_LoadBMP(GetFullFileName(fullfilename).c_str()); std::shared_ptr<SDL_Surface> bmpSfcPtr( pPrimBmp ); std::shared_ptr<GsBitmap> pBmp(new GsBitmap(bmpSfcPtr)); mpPrevievBmpVec[i] = pBmp; } i++; } mpSelList->setConfirmButtonEvent(new GMStart()); mpSelList->setBackButtonEvent(new GMQuit()); mLauncherDialog.addControl(new CGUIText("Pick a Game"), GsRect<float>(0.0f, 0.0f, 1.0f, 0.05f)); mLauncherDialog.addControl(new GsButton( "x", new GMQuit() ), GsRect<float>(0.0f, 0.0f, 0.07f, 0.07f) ); mLauncherDialog.addControl(mpSelList, GsRect<float>(0.01f, 0.07f, 0.49f, 0.79f)); mLauncherDialog.addControl(new GsButton( "Start >", new GMStart() ), GsRect<float>(0.65f, 0.865f, 0.3f, 0.07f) ); #ifdef DBFUSION GsButton *fusionShellBtn = new GsButton( "DosFusion Shell >", new GMDBFusionStart() ); GsButton *fusionBtn = new GsButton( "DosFusion! >", new GMDosGameFusionStart() ); if(disallowDBFusion) { fusionShellBtn->enable(false); fusionBtn->enable(false); } mLauncherDialog.addControl( fusionShellBtn, GsRect<float>(0.01f, 0.865f, 0.3f, 0.07f) ); mLauncherDialog.addControl( fusionBtn, GsRect<float>(0.35f, 0.865f, 0.3f, 0.07f) ); #endif #ifdef DOWNLOADER GsButton *downloadBtn = new GsButton( "New Stuff", new GMDownloadDlgOpen() ); mLauncherDialog.addControl( downloadBtn, GsRect<float>(0.35f, 0.865f, 0.3f, 0.07f) ); #endif mpEpisodeText = new CGUIText("Game"); mpVersionText = new CGUIText("Version"); mLauncherDialog.addControl(mpEpisodeText, GsRect<float>(0.5f, 0.75f, 0.5f, 0.05f)); mLauncherDialog.addControl(mpVersionText, GsRect<float>(0.5f, 0.80f, 0.5f, 0.05f)); // This way it goes right to the selection list. mLauncherDialog.setSelection(2); mGameScanner.setPermilage(1000); gLogging.ftextOut("Game Autodetection Finished<br>" ); // Banner. TODO: Create a class for that... CGUIBanner *banner = new CGUIBanner("Commander Genius " CGVERSION "\n" "By Gerstrong,\n" "Hagel,\n" "Tulip,\n" "NY00123,\n" "Pelya,\n" "and the CG Contributors\n"); mLauncherDialog.addControl( banner, GsRect<float>(0.0f, 0.95f, 1.0f, 0.05f) ); if(!gamesDetected) return false; const std::string gameDir = gArgs.getValue("dir"); if(!gameDir.empty()) { int chosenGame = 0; bool found=false; // Check if the given parameter makes one game start. for( GameEntry &entry : m_Entries) { if(entry.path == gameDir) { // found! m_chosenGame = chosenGame; gLogging.textOut("Launching game from directory: \"" + gameDir + "\"\n"); gArgs.removeTag("dir"); setupModsDialog(); // Nothing else to do, break the loop found = true; break; } chosenGame++; } if(!found) { const std::string err = "The game from directory: \"" + gameDir + "\" cannot the launched." + "Maybe it's missing or not compatible. Please check if you can run that through the game launcher.\n"; gLogging.textOut(err); showMessageBox("Given path :\"" + gameDir + "\" unknown.\nPlease check the CGLog File!"); } } return true; }
/** * \brief checks if the file of the chosen slot exits * \param SaveSlot Slot where to check for the file * \return true if it exists, else false */ bool CSaveGameController::Fileexists( int SaveSlot ) { std::string filename = m_savedir + "/cksave"+itoa(SaveSlot)+".ck"+itoa(m_Episode); return IsFileAvailable(filename); }
//////////////////// // Helper function for TakeScreenshot static std::string GetScreenshotFileName(const std::string& scr_path, const std::string& extension) { std::string path = scr_path; // Append a slash if not present if (path[path.size() - 1] != '/' && path[path.size() - 1] != '\\') { path += '/'; } std::string filePrefix = GetDateTimeFilename(); filePrefix += "-"; if( tLX ) { if( tLX->iGameType == GME_LOCAL ) filePrefix += "local"; else if( tLX->iGameType == GME_HOST && cServer ) filePrefix += tLXOptions->sServerName; else if( cClient ) filePrefix += cClient->getServerName(); } // Make filename more fileststem-friendly if( filePrefix.size() > 64 ) filePrefix.resize(64); #define S_LETTER_UPPER "ABCDEFGHIJKLMNOPQRSTUVWXYZ" #define S_LETTER_LOWER "abcdefghijklmnopqrstuvwxyz" #define S_LETTER S_LETTER_UPPER S_LETTER_LOWER #define S_NUMBER "0123456789" #define S_SYMBOL ". -_&+" // No "\\" symbol, no tab. #define S_VALID_FILENAME S_LETTER_UPPER S_LETTER_LOWER S_NUMBER S_SYMBOL while( filePrefix.find_first_not_of(S_VALID_FILENAME) != std::string::npos ) filePrefix[ filePrefix.find_first_not_of(S_VALID_FILENAME) ] = '-'; static const size_t step = 256; // Step; after how many files we check if the filename still exists // We start at range from 1 to step size_t lower_bound = 0; size_t upper_bound = step; std::string fullname(path + GetPicName(filePrefix, upper_bound, extension)); // Find a raw range of where the screenshot filename could be // For example: between lierox1000.png and lierox1256.png while (IsFileAvailable(fullname, false)) { lower_bound = upper_bound; upper_bound += step; fullname = path + GetPicName(filePrefix, upper_bound, extension); } // First file? if (!IsFileAvailable(path + GetPicName(filePrefix, lower_bound, extension))) return path + GetPicName(filePrefix, lower_bound, extension); // Use binary search on the given range to find the exact file name size_t i = (lower_bound + upper_bound) / 2; while (true) { if (IsFileAvailable(path + GetPicName(filePrefix, i, extension), false)) { // If the current (i) filename exists, but the i+1 does not, we're done if (!IsFileAvailable(path + GetPicName(filePrefix, i + 1, extension))) return path + GetPicName(filePrefix, i + 1, extension); else { // The filename is somewhere in the interval (i, upper_bound) lower_bound = i; i = (lower_bound + upper_bound) / 2; } } else { // The filename is somewhere in the interval (lower_bound, i) upper_bound = i; i = (lower_bound + upper_bound) / 2; } } return ""; // Should not happen }
bool gusExistsFile(const std::string& filename) { return IsFileAvailable(filename, false, true); }