CEGUI::Window* Widget::loadMainSheet(const std::string& filename, const std::string& prefix) { assert(mWindowManager && "You must call init() before you can call any other methods."); mPrefix = prefix; std::string finalFileName(mGuiManager->getLayoutDir() + filename); try { mMainWindow = mWindowManager->loadLayoutFromFile(finalFileName); } catch (const std::exception& ex) { S_LOG_FAILURE("Error when loading from " << filename << "." << ex); throw ex; } catch (...) { S_LOG_FAILURE("Unknown error when loading from " << filename << "."); throw; } mMainWindow->setName(prefix); mOriginalWindowAlpha = mMainWindow->getAlpha(); getMainSheet()->addChild(mMainWindow); BIND_CEGUI_EVENT(mMainWindow, CEGUI::FrameWindow::EventActivated, Widget::MainWindow_Activated); BIND_CEGUI_EVENT(mMainWindow, CEGUI::FrameWindow::EventDeactivated, Widget::MainWindow_Deactivated); //we want to catch all click events, so we'll listen for the mouse button down event BIND_CEGUI_EVENT(mMainWindow, CEGUI::Window::EventMouseButtonDown, Widget::MainWindow_MouseButtonDown); if (mMainWindow->isVisible()) { onEventFirstTimeShown(); } else { //Set it up to listen for the first time the window is shown. BIND_CEGUI_EVENT(mMainWindow, CEGUI::Window::EventShown, Widget::MainWindow_Shown); } return mMainWindow; }
CEGUI::Window* LayoutHelper::loadLayout(const std::string& layoutFile, CEGUI::String& prefix) { CEGUI::WindowManager& windowManager = CEGUI::WindowManager::getSingleton(); std::string finalFileName(GUIManager::getSingleton().getLayoutDir() + layoutFile); return windowManager.loadLayoutFromFile(finalFileName); }
bool ModuleMap::cacheIsUpToDate(uint32 mapID, MC2String mapFileName, uint32 loadMapRequestType, byte zoomlevel){ MC2String finalFileName( ModuleMap::getCacheFilename(mapID, loadMapRequestType, zoomlevel) ); if ( File::fileExist( finalFileName )){ struct stat cacheInfo; if (stat(finalFileName.c_str(), &cacheInfo) != 0){ mc2log << error << "Could not read file times for: " << finalFileName << endl; MC2_ASSERT(false); } time_t cacheModTime = cacheInfo.st_mtime; struct stat mapInfo; MC2String realMapFileName = DataBufferCreator::getMapOrIdxPath(mapFileName); if (stat(realMapFileName.c_str(), &mapInfo) != 0){ mc2log << error << "Could not read file time for: " << realMapFileName << endl; MC2_ASSERT(false); } time_t mapModTime = mapInfo.st_mtime; mc2dbg << "[ModuleMap]: Comparing file times for " << finalFileName << " and " << realMapFileName << endl; if ( cacheModTime > mapModTime ){ mc2dbg << info << "[ModuleMap]: Cache file " << finalFileName << " is up to date, not saving." << endl; // This cache file is up to date. return true; } } return false; }
bool ModuleMap::saveCachedMap(DataBuffer& mapBuffer, const GenericMap* theMap, uint32 loadMapRequestType, uint32 mapVersion, uint32 generatorVersion, bool update, byte zoomlevel) { uint32 mapID = theMap->getMapID(); MC2String finalFileName( ModuleMap::getCacheFilename(mapID, loadMapRequestType, zoomlevel) ); if ( update && cacheIsUpToDate(theMap->getMapID(), theMap->getFilename(), loadMapRequestType, zoomlevel) ){ return true; } const char* fileName = finalFileName.c_str(); MC2String tmpDir = ModuleMap::getCachePath(); if ( tmpDir[0] == '\0' ) { mc2log << info << "[ModuleMap]: No cachepath set - will not save " << "map 0x" << hex << mapID << dec << endl; return false; } else { mc2log << info << "[ModuleMap]: Will try to save " << MC2CITE(fileName) << endl; } char tempTemplate[1024]; sprintf(tempTemplate, "%scachingXXXXXX", tmpDir.c_str()); int tmpDesc = mkstemp(tempTemplate); MC2String tempName = MC2String(tempTemplate); // Remove the old file if there is one ( to make space for new map ) mc2dbg << "[ModuleMap]: Removing " << MC2CITE(fileName) << " if it exists" << endl; unlink(fileName); int headerSize; byte* header = makeHeader( headerSize, mapID, mapVersion, generatorVersion); FILE* writeFile = NULL; if ( tmpDesc < 0 ) { mc2dbg << "[ModuleMap]: Could not make tempfile" << endl; writeFile = NULL; return false; } else { writeFile = fdopen(tmpDesc, "w"); if ( header ) { if ( fwrite(header, headerSize, 1, writeFile) != 1 ) { mc2dbg << "[ModuleMap]: Could not write header: " << strerror(errno) << endl; fclose(writeFile); unlink(tempName.c_str()); writeFile = NULL; return false; } } else { mc2log << "[ModuleMap]: Header is NULL" << endl; return false; } } delete [] header; MC2_ASSERT( writeFile != NULL ); // Now the header should be written. if ( fwrite(mapBuffer.getBufferAddress(), mapBuffer.getBufferSize(), 1, writeFile) != 1 ) { fclose(writeFile); unlink(tempName.c_str()); writeFile = NULL; mc2dbg << "[ModuleMap]: Could not write map data" << endl; return false; } else { rename(tempName.c_str(), fileName); chmod (fileName, (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) ); fclose(writeFile); mc2log << info << "[ModuleMap]: Map 0x" << hex << mapID << dec << " saved." << endl; return true; } }