////////////// // Save a mod to the cache void CCache::SaveMod(const std::string& file1, const SmartPointer<CGameScript> & mod) { if(mod.get() == NULL) { errors << "SaveMod: tried to safe NULL gamescript" << endl; return; } if(!mod->isLoaded()) { errors << "SaveMod: tried to safe non-loaded gamescript" << endl; return; } // dont save gus mods if(mod->gusEngineUsed()) return; std::string file = file1; stringlwr(file); ScopedLock lock(mutex); if( ModCache.find(file) != ModCache.end() ) // Error - already in cache { errors << "Error: mod already in cache - memleak: " << file << endl; return; } struct stat st; StatFile(file1, &st); ModCache[file] = ModItem_t( mod, getCurrentTime(), st.st_mtime ); }
// TODO: move this to console (it was only nec. via chat because we didn't had the console globally before) // HINT: This is called atm from CClientNetEngine::SendText(). // HINT: This is just a hack to do some testing in lobby or whatever. // WARNING: These stuff is not intended to be stable, it's only for testing! // HINT: Don't rely on this. If we allow the console later somehow in the lobby, // this debug stuff will probably move there. bool HandleDebugCommand(const std::string& text) { if(text.size() >= 3 && text.substr(0,3) == "///") { cClient->getChatbox()->AddText("DEBUG COMMAND", tLX->clNotice, TXT_NOTICE, tLX->currentTime); std::string cmd = text.substr(3); stringlwr(cmd); if(cmd == "reconnect") { notes << "DEBUG CMD: reconnect local client to " << cClient->getServerAddress() << endl; cClient->Connect(cClient->getServerAddress()); } else if(cmd == "msgbox") { Menu_MessageBox("Test", "This is a very long text, a very long text, a very long text, a very long text, " "a very long text, a very long text, a very long text, a very long text, a very long text, " "a very long text, a very long text, a very long text, a very long text, a very long text, " "a very long text, a very long text, a very long text, a very long text, a very long text.\n" "Yes really, this text is very long, very long, very long, very long, very long, " "very very long, very very long, very very long, very very long, very very long.", DeprecatedGUI::LMB_OK); } else if(cmd == "register") { cServer->RegisterServer(); } else notes << "DEBUG CMD unknown" << endl; return true; } return false; }
////////////// // Save a map to the cache void CCache::SaveMap(const std::string& file1, CMap *map) { { ScopedLock lock(mutex); if (map == NULL) return; std::string file = file1; stringlwr(file); if( MapCache.find(file) != MapCache.end() ) // Error - already in cache { errors << "Error: map already in cache: " << file << endl; return; } // Copy the map to the cache (not just the pointer because map changes during the game) CMap *cached_map = new CMap; if (cached_map == NULL) return; if (!cached_map->NewFrom(map)) return; struct stat st; StatFile(file1, &st); MapCache[file] = MapItem_t( SmartPointer<CMap>(cached_map), getCurrentTime(), st.st_mtime ); } ClearExtraEntries(); // Cache can get very big when browsing through levels - clear it here }
bool CMusic::load(const std::string &musicfile) { mpPlayer.reset(); if(musicfile == "") return false; const SDL_AudioSpec &audioSpec = g_pSound->getAudioSpec(); if(audioSpec.format != 0) { std::string extension = GetFileExtension(musicfile); g_pSound->pauseAudio(); stringlwr(extension); if( extension == "imf" ) { mpPlayer.reset( new CIMFPlayer ); if(!mpPlayer->loadMusicFromFile(musicfile)) { return false; } } else if( extension == "ogg" ) { #if defined(OGG) || defined(TREMOR) mpPlayer.reset( new COGGPlayer ); mpPlayer->loadMusicFromFile(musicfile); #else gLogging.ftextOut("Music Manager: Neither OGG bor TREMOR-Support are enabled! Please use another build<br>"); g_pSound->resumeAudio(); return false; #endif } if(!mpPlayer->open(true)) { mpPlayer.reset(); gLogging.textOut(PURPLE,"Music Manager: File could not be opened: \"%s\". File is damaged or something is wrong with your soundcard!<br>", musicfile.c_str()); g_pSound->resumeAudio(); return false; } g_pSound->resumeAudio(); return true; } else { gLogging.textOut(PURPLE,"Music Manager: I would like to open the music for you. But your Soundcard seems to be disabled!!<br>"); } gLogging.textOut(PURPLE,"Music Manager: Got a tune to play"); return false; }
////////////// // Get an image from the cache SmartPointer<SDL_Surface> CCache::GetImage__unsafe(const std::string& file1) { std::string file = file1; stringlwr(file); ImageCache_t::iterator item = ImageCache.find(file); if(item != ImageCache.end()) { item->second.fSaveTime = getCurrentTime(); return item->second.bmpSurf; } return NULL; }
////////////// // Get a sound sample from the cache SmartPointer<SoundSample> CCache::GetSound(const std::string& file1) { ScopedLock lock(mutex); std::string file = file1; stringlwr(file); SoundCache_t::iterator item = SoundCache.find(file); if(item != SoundCache.end()) { item->second.fSaveTime = getCurrentTime(); return item->second.sndSample; }; return NULL; }
////////////// // Save an image to the cache void CCache::SaveImage__unsafe(const std::string& file1, const SmartPointer<SDL_Surface> & img) { if (img.get() == NULL) return; std::string file = file1; stringlwr(file); if( ImageCache.find(file) != ImageCache.end() ) // Error - already in cache { errors << "Error: image already in cache - memleak: " << file << endl; return; } //notes << "CCache::SaveImage(): " << img << " " << file << endl; ImageCache[file] = ImageItem_t(img, getCurrentTime(), 0); }
////////////// // Save a sound sample to the cache void CCache::SaveSound(const std::string& file1, const SmartPointer<SoundSample> & smp) { ScopedLock lock(mutex); if (smp.get() == NULL) return; std::string file = file1; stringlwr(file); if( SoundCache.find(file) != SoundCache.end() ) { errors << "Error: sound already in cache - memleak: " << file << endl; return; }; SoundCache[file] = SoundItem_t( smp, getCurrentTime(), 0 ); }
/** * \brief This function tries to read a value from string, given that * there is any type of number that comes. * \param input a string of which we want to get the numerical value * \param output the integer that might be detected. * \param width optionally it can read width * \return if the number could be read true, otherwise false */ bool CPatcher::readIntValueAndWidth(const std::string &input, unsigned long int &output, size_t &width) { if(strStartsWith(input, "$") or strCaseStartsWith(input, "0x")) { std::string line = input; // it is a hexadecimal number if(strStartsWith(line, "$")) { std::string buf; line.erase(0,1); buf = "0x"+line; line = buf; } stringlwr(line); if(line.size() <=4 ) { width = 1; } else { if(line.find("w")) { width = 2; } else { width = 4; } } // now everything is hexadecimal with the proper format sscanf( line.c_str(), "%lx", &output ); return true; } // Try to read a decimal number if( (output = atoi(input)) != 0) return true; if(input == "00" || input == "0") return true; return false; }
void VideoPostProcessor::init() { notes << "VideoPostProcessor initialisation ... "; std::string vppName = tLXOptions->sVideoPostProcessor; TrimSpaces(vppName); stringlwr(vppName); if(vppName == "stretchhalf") instance = new StretchHalfPostProc(); else if(vppName == "scale2x") instance = new Scale2XPostProc(); else if(vppName == "dummy") instance = new DummyVideoPostProc(); else { if(vppName != "") notes << "\"" << tLXOptions->sVideoPostProcessor << "\" unknown; "; // notes << "none used, drawing directly on screen" << endl; //instance = &voidVideoPostProcessor; instance = new DummyVideoPostProc(); } }
////////////// // Get a mod from the cache SmartPointer<CGameScript> CCache::GetMod(const std::string& file1) { ScopedLock lock(mutex); std::string file = file1; stringlwr(file); ModCache_t::iterator item = ModCache.find(file); if(item != ModCache.end()) { // If the file has changed, don't consider it as found and erase it from the cache struct stat st; StatFile(item->first, &st); if (item->second.iFileTimeStamp != (Uint64)st.st_mtime) { item->second.tMod = NULL; ModCache.erase(item); return NULL; } item->second.fSaveTime = getCurrentTime(); return item->second.tMod; }; return NULL; }
// Helper functions std::string strlwr(const std::string& s) { std::string res = s; stringlwr(res); return res; }
///////////////////// // Converts the buffer to lower case void StringBuf::toLower() { stringlwr(sStr); }