/** Creates the directories for the addons data. This will set m_addons_dir * with the appropriate path, and also create the subdirectories in this * directory. */ void FileManager::checkAndCreateAddonsDir() { #if defined(WIN32) || defined(__CYGWIN__) m_addons_dir = m_user_config_dir+"addons/"; #elif defined(__APPLE__) m_addons_dir = getenv("HOME"); m_addons_dir += "/Library/Application Support/SuperTuxKart/Addons/"; #else m_addons_dir = checkAndCreateLinuxDir("XDG_DATA_HOME", "supertuxkart", ".local/share", ".stkaddons"); m_addons_dir += "addons/"; #endif if(!checkAndCreateDirectory(m_addons_dir)) { Log::error("FileManager", "Can not create add-ons dir '%s', " "falling back to '.'.", m_addons_dir.c_str()); m_addons_dir = "./"; } if (!checkAndCreateDirectory(m_addons_dir + "icons/")) { Log::error("FileManager", "Failed to create add-ons icon dir at '%s'.", (m_addons_dir + "icons/").c_str()); } if (!checkAndCreateDirectory(m_addons_dir + "tmp/")) { Log::error("FileManager", "Failed to create add-ons tmp dir at '%s'.", (m_addons_dir + "tmp/").c_str()); } } // checkAndCreateAddonsDir
/** If the directory specified in path does not exist, it is created * recursively (mkdir -p style). * \params path Directory to test. * \return True if the directory exists or could be created, false otherwise. */ bool FileManager::checkAndCreateDirectoryP(const std::string &path) { // irrlicht apparently returns true for files and directory // (using access/_access internally): if(m_file_system->existFile(io::path(path.c_str()))) return true; Log::info("[FileManager]", "Creating directory(ies) '%s'", path.c_str()); std::vector<std::string> split = StringUtils::split(path,'/'); std::string current_path = ""; for (unsigned int i=0; i<split.size(); i++) { current_path += split[i] + "/"; Log::info("[FileManager]", "Checking for: '%s", current_path.c_str()); if (!m_file_system->existFile(io::path(current_path.c_str()))) { if (!checkAndCreateDirectory(current_path)) { Log::error("[FileManager]", "Can't create dir '%s'", current_path.c_str()); break; } } } bool error = checkAndCreateDirectory(path); return error; } // checkAndCreateDirectory
bool FileManager::checkAndCreateDirectoryP(const std::string &path) { std::cout << "creating...:" << path << std::endl; // irrlicht apparently returns true for files and directory // (using access/_access internally): if(m_file_system->existFile(io::path(path.c_str()))) return true; std::vector<std::string> split = StringUtils::split(path,'/'); std::string current_path =""; for(unsigned int i=0; i<split.size(); i++) { current_path += split[i] + "/"; std::cout << "Checking for: " << current_path << std::endl; if(m_file_system->existFile(io::path(current_path.c_str()))) std::cout << "The directory exist." << std::endl; else { if(!checkAndCreateDirectory(current_path)) { fprintf(stderr, "Can't create dir '%s'", current_path.c_str()); break; } } } bool error = checkAndCreateDirectory(path); return error; } // checkAndCreateDirectory
void FileManager::checkAndCreateDirForAddons(std::string addons_name, std::string addons_type) { bool success = checkAndCreateDirectory(getAddonsDir() + "/data/" + addons_type); if(!success) std::cout << "There is a problem with the addons dir." << std::endl; checkAndCreateDirectory(getAddonsDir() + "/data/" + addons_type + addons_name); }
void FileManager::checkAndCreateAddonsDir() { #if defined(WIN32) //TODO #elif defined(__APPLE__) m_addons_dir = getenv("HOME"); m_addons_dir += "/Library/Application Support/SuperTuxKart"; #else // Remaining unix variants. Use the new standards for config directory // i.e. either XDG_CONFIG_HOME or $HOME/.config if (getenv("XDG_DATA_HOME")!=NULL){ m_addons_dir = getenv("XDG_DATA_HOME"); } else if (!getenv("HOME")) { std::cerr << "No home directory, this should NOT happen - trying '.addons' for addons files!\n"; m_addons_dir = "stkaddons"; } else { m_addons_dir = getenv("HOME"); m_addons_dir += "/.local/share"; if(!checkAndCreateDirectory(m_config_dir)) { // If $HOME/.config can not be created: fprintf(stderr, "Can't create dir '%s', falling back to use '%s'.\n", m_config_dir.c_str(), getenv("HOME")); m_addons_dir = getenv("HOME"); m_addons_dir += "."; } } const std::string CONFIGDIR("supertuxkart"); m_addons_dir += "/"; m_addons_dir += CONFIGDIR; #endif if(!checkAndCreateDirectory(m_addons_dir)) { fprintf(stderr, "Can not create add-ons dir '%s', falling back to '.'.\n", m_addons_dir.c_str()); m_config_dir = "."; } else { //we hope that there will be no problem since we created the other dir if (!checkAndCreateDirectory(m_addons_dir + "/data/")) { fprintf(stderr, "Failed to create add-ons data dir at '%s'\n", (m_addons_dir + "/data/").c_str()); } } return; } // checkAndCreateAddonsDir
void OsmTileClusterRenderer::renderOsmTileCluster( int const clusterX, int const clusterY ) { qDebug() << objectName() << "rendering clusterX:" << clusterX << ", clusterY:" << clusterY; int tilesRenderedCount = 0; QTime t; t.start(); int const tileX1 = clusterX * m_clusterEdgeLengthTiles; int const tileX2 = tileX1 + m_clusterEdgeLengthTiles; int const tileY1 = clusterY * m_clusterEdgeLengthTiles; int const tileY2 = tileY1 + m_clusterEdgeLengthTiles; for ( int tileX = tileX1; tileX < tileX2; ++tileX ) { QDir const tileDirectory = checkAndCreateDirectory( tileX ); for ( int tileY = tileY1; tileY < tileY2; ++tileY ) { QImage const osmTile = renderOsmTile( tileX, tileY ); // hack if ( osmTile.isNull() ) continue; QString const filename = tileDirectory.path() + QString( "/%1.png" ).arg( tileY ); bool const saved = osmTile.save( filename ); if ( saved ) ++tilesRenderedCount; else qFatal("Unable to save tile '%s'.", filename.toStdString().c_str() ); } } int const durationMs = t.elapsed(); qDebug() << objectName() << "clusterX:" <<clusterX << ", clusterY:" << clusterY << "rendered:" << tilesRenderedCount << "tiles in" << durationMs << "ms =>" << static_cast<double>( tilesRenderedCount ) * 1000.0 / static_cast<double>( durationMs ) << "tiles/s"; emit clusterRendered( this ); }
/** Creates the directories for screenshots. This will set m_screenshot_dir * with the appropriate path. */ void FileManager::checkAndCreateScreenshotDir() { #if defined(WIN32) || defined(__CYGWIN__) m_screenshot_dir = m_user_config_dir+"screenshots/"; #elif defined(__APPLE__) m_screenshot_dir = getenv("HOME"); m_screenshot_dir += "/Library/Application Support/SuperTuxKart/Screenshots/"; #else m_screenshot_dir = checkAndCreateLinuxDir("XDG_CACHE_HOME", "supertuxkart", ".cache/", "."); m_screenshot_dir += "screenshots/"; #endif if(!checkAndCreateDirectory(m_screenshot_dir)) { Log::error("FileManager", "Can not create screenshot directory '%s', " "falling back to '.'.", m_screenshot_dir.c_str()); m_screenshot_dir = "."; } } // checkAndCreateScreenshotDir
/** Creates the directories for user-defined grand prix. This will set m_gp_dir * with the appropriate path. */ void FileManager::checkAndCreateGPDir() { #if defined(WIN32) || defined(__CYGWIN__) m_gp_dir = m_user_config_dir + "grandprix/"; #elif defined(__APPLE__) m_gp_dir = getenv("HOME"); m_gp_dir += "/Library/Application Support/SuperTuxKart/grandprix/"; #else m_gp_dir = checkAndCreateLinuxDir("XDG_DATA_HOME", "supertuxkart", ".local/share", ".supertuxkart"); m_gp_dir += "grandprix/"; #endif if(!checkAndCreateDirectory(m_gp_dir)) { Log::error("FileManager", "Can not create user-defined grand prix directory '%s', " "falling back to '.'.", m_gp_dir.c_str()); m_gp_dir = "."; } } // checkAndCreateGPDir
/** Find a directory to use for remaining unix variants. Use the new standards * for config directory based on XDG_* environment variables, or a * subdirectory under $HOME, trying two different fallbacks. It will also * check if the directory 'dirname' can be created (to avoid problems that * e.g. $env_name is '/', which exists, but can not be written to. * \param env_name Name of the environment variable to test first. * \param dir_name Name of the directory to create * \param fallback1 Subdirectory under $HOME to use if the environment * variable is not defined or can not be created. * \param fallback2 Subdirectory under $HOME to use if the environment * variable and fallback1 are not defined or can not be created. */ std::string FileManager::checkAndCreateLinuxDir(const char *env_name, const char *dir_name, const char *fallback1, const char *fallback2) { bool dir_ok = false; std::string dir; if (getenv(env_name)!=NULL) { dir = getenv(env_name); dir_ok = checkAndCreateDirectory(dir); if(!dir_ok) Log::warn("FileManager", "Cannot create $%s.", env_name); if(dir[dir.size()-1]!='/') dir += "/"; // Do an additional test here, e.g. in case that XDG_DATA_HOME is '/' // and since dir_ok is set, it would not test any of the other options // like $HOME/.local/share dir_ok = checkAndCreateDirectory(dir+dir_name); if(!dir_ok) Log::warn("FileManager", "Cannot create $%s/%s.", dir.c_str(), dir_name); } if(!dir_ok && getenv("HOME")) { // Use ~/.local/share : dir = getenv("HOME"); if(dir.size()>0 && dir[dir.size()-1]!='/') dir += "/"; dir += fallback1; // This will create each individual subdirectory if // dir_name contains "/". dir_ok = checkAndCreateDirectoryP(dir); if(!dir_ok) Log::warn("FileManager", "Cannot create $HOME/%s.", fallback1); } if(!dir_ok && fallback2 && getenv("HOME")) { dir = getenv("HOME"); if(dir.size()>0 && dir[dir.size()-1]!='/') dir += "/"; dir += fallback2; dir_ok = checkAndCreateDirectory(dir); if(!dir_ok) Log::warn("FileManager", "Cannot create $HOME/%s.", fallback2); } if(!dir_ok) { Log::warn("FileManager", "Falling back to use '.'."); dir = "./"; } if(dir.size()>0 && dir[dir.size()-1]!='/') dir += "/"; dir += dir_name; dir_ok = checkAndCreateDirectory(dir); if(!dir_ok) { // If the directory can not be created Log::error("FileManager", "Cannot create directory '%s', " "falling back to use '.'.", dir.c_str()); dir="./"; } if(dir.size()>0 && dir[dir.size()-1]!='/') dir += "/"; return dir; } // checkAndCreateLinuxDir
/** Checks if the config directory exists, and it not, tries to create it. * It will set m_user_config_dir to the path to which user-specific config * files are stored. */ void FileManager::checkAndCreateConfigDir() { if(getenv("SUPERTUXKART_SAVEDIR") && checkAndCreateDirectory(getenv("SUPERTUXKART_SAVEDIR")) ) { m_user_config_dir = getenv("SUPERTUXKART_SAVEDIR"); } else { #if defined(WIN32) || defined(__CYGWIN__) // Try to use the APPDATA directory to store config files and highscore // lists. If not defined, used the current directory. if(getenv("APPDATA")!=NULL) { m_user_config_dir = getenv("APPDATA"); if(!checkAndCreateDirectory(m_user_config_dir)) { Log::error("[FileManager]", "Can't create config dir '%s" ", falling back to '.'.", m_user_config_dir); m_user_config_dir = "."; } } else m_user_config_dir = "."; m_user_config_dir += "/supertuxkart"; #elif defined(__APPLE__) if (getenv("HOME")!=NULL) { m_user_config_dir = getenv("HOME"); } else { Log::error("[FileManager]", "No home directory, this should NOT happen!"); // Fall back to system-wide app data (rather than // user-specific data), but should not happen anyway. m_user_config_dir = ""; } m_user_config_dir += "/Library/Application Support/"; const std::string CONFIGDIR("SuperTuxKart"); m_user_config_dir += CONFIGDIR; #else // Remaining unix variants. Use the new standards for config directory // i.e. either XDG_CONFIG_HOME or $HOME/.config if (getenv("XDG_CONFIG_HOME")!=NULL){ m_user_config_dir = getenv("XDG_CONFIG_HOME"); } else if (!getenv("HOME")) { Log::error("[FileManager]", "No home directory, this should NOT happen " "- trying '.' for config files!"); m_user_config_dir = "."; } else { m_user_config_dir = getenv("HOME"); m_user_config_dir += "/.config"; if(!checkAndCreateDirectory(m_user_config_dir)) { // If $HOME/.config can not be created: Log::error("[FileManager]", "Cannot create directory '%s', falling back to use '%s'", m_user_config_dir.c_str(), getenv("HOME")); m_user_config_dir = getenv("HOME"); } } m_user_config_dir += "/supertuxkart"; #endif } // if(getenv("SUPERTUXKART_SAVEDIR") && checkAndCreateDirectory(...)) if(m_user_config_dir.size()>0 && *m_user_config_dir.rbegin()!='/') m_user_config_dir += "/"; if(!checkAndCreateDirectory(m_user_config_dir)) { Log::warn("FileManager", "Can not create config dir '%s', " "falling back to '.'.", m_user_config_dir.c_str()); m_user_config_dir = "./"; } return; } // checkAndCreateConfigDir
/** Checks if the config directory exists, and it not, tries to create it. */ void FileManager::checkAndCreateConfigDir() { #if defined(WIN32) // Try to use the APPDATA directory to store config files and highscore // lists. If not defined, used the current directory. if(getenv("APPDATA")!=NULL) { m_config_dir = getenv("APPDATA"); if(!checkAndCreateDirectory(m_config_dir)) { fprintf(stderr, "Can't create config dir '%s', falling back to '.'.\n", m_config_dir.c_str()); m_config_dir = "."; } } else m_config_dir = "."; const std::string CONFIGDIR("supertuxkart"); m_config_dir += "/"; m_config_dir += CONFIGDIR; #elif defined(__APPLE__) if (getenv("HOME")!=NULL) { m_config_dir = getenv("HOME"); } else { std::cerr << "No home directory, this should NOT happen!\n"; // Fall back to system-wide app data (rather than user-specific data), but should not happen anyway. m_config_dir = ""; } m_config_dir += "/Library/Application Support/"; const std::string CONFIGDIR("SuperTuxKart"); m_config_dir += CONFIGDIR; # else // Remaining unix variants. Use the new standards for config directory // i.e. either XDG_CONFIG_HOME or $HOME/.config if (getenv("XDG_CONFIG_HOME")!=NULL){ m_config_dir = getenv("XDG_CONFIG_HOME"); } else if (!getenv("HOME")) { std::cerr << "No home directory, this should NOT happen - trying '.' for config files!\n"; m_config_dir = "."; } else { m_config_dir = getenv("HOME"); m_config_dir += "/.config"; if(!checkAndCreateDirectory(m_config_dir)) { // If $HOME/.config can not be created: fprintf(stderr, "Can't create dir '%s', falling back to use '%s'.\n", m_config_dir.c_str(), getenv("HOME")); m_config_dir = getenv("HOME"); m_config_dir += "."; } } const std::string CONFIGDIR("supertuxkart"); m_config_dir += "/"; m_config_dir += CONFIGDIR; #endif if(!checkAndCreateDirectory(m_config_dir)) { fprintf(stderr, "Can not create config dir '%s', falling back to '.'.\n", m_config_dir.c_str()); m_config_dir = "."; } return; } // checkAndCreateConfigDir