SuperTuxKart
|
class handling files and paths More...
#include <file_manager.hpp>
Public Types | |
enum | AssetType { ASSET_MIN , CHALLENGE =ASSET_MIN , GFX , GRANDPRIX , GUI_ICON , GUI_SCREEN , GUI_DIALOG , REPLAY , SHADER , SKIN , TTF , TRANSLATION , BUILTIN_ASSETS =TRANSLATION , LIBRARY , MODEL , MUSIC , SFX , TEXTURE , SCRIPT , ASSET_MAX = SCRIPT , ASSET_COUNT } |
The various asset types (and directories) STK might request. More... | |
Public Member Functions | |
FileManager () | |
The constructor of the file manager creates an irrlicht file system and detects paths for the user config file and assets base directory (data). | |
void | init () |
This function is used to initialise the file-manager after reading in the user configuration data. | |
void | reinitAfterDownloadAssets () |
io::IXMLReader * | createXMLReader (const std::string &filename) |
XMLNode * | createXMLTree (const std::string &filename) |
Reads in a XML file and converts it into a XMLNode tree. | |
XMLNode * | createXMLTreeFromString (const std::string &content) |
Reads in XML from a string and converts it into a XMLNode tree. | |
std::string | getScreenshotDir () const |
Returns the directory in which screenshots should be stored. | |
std::string | getReplayDir () const |
Returns the directory in which replay file should be stored. | |
std::string | getCachedTexturesDir () const |
Returns the directory in which resized textures should be cached. | |
std::string | getGPDir () const |
Returns the directory in which user-defined grand prix should be stored. | |
bool | checkAndCreateDirectory (const std::string &path) |
If the directory specified in path does not exist, it is created. | |
bool | checkAndCreateDirectoryP (const std::string &path) |
If the directory specified in path does not exist, it is created recursively (mkdir -p style). | |
const std::string & | getAddonsDir () const |
Returns the directory for addon files. | |
std::string | getAddonsFile (const std::string &name) |
Returns the full path of a file in the addons directory. | |
void | checkAndCreateDirForAddons (const std::string &dir) |
Creates a directory for an addon. | |
bool | removeFile (const std::string &name) const |
Removes the specified file. | |
bool | removeDirectory (const std::string &name) const |
Removes a directory (including all files contained). | |
bool | moveDirectoryInto (std::string source, std::string target) |
Move the source directory into the target directory location. | |
bool | copyFile (const std::string &source, const std::string &dest) |
Copies the file source to dest. | |
std::vector< std::string > | getMusicDirs () const |
Returns the list of all directories in which music files are searched. | |
std::string | getAssetChecked (AssetType type, const std::string &name, bool abort_on_error=false) const |
std::string | getAsset (AssetType type, const std::string &name) const |
Returns the full path of a file of the given asset class. | |
std::string | getAsset (const std::string &name) const |
Searches in all root directories for the specified file. | |
std::string | getAssetDirectory (AssetType type) const |
Returns the directory of an asset. | |
std::string | searchMusic (const std::string &file_name) const |
Returns the full path of a music file by searching all music search paths. | |
std::string | searchModel (const std::string &file_name) const |
Returns the full path of a model file by searching all model search paths. | |
std::string | searchTexture (const std::string &fname) const |
Returns the full path of a texture file name by searching in all directories currently in the texture search path. | |
std::string | getUserConfigFile (const std::string &fname) const |
Returns the full path of the config directory. | |
bool | fileExists (const std::string &path) const |
Returns true if the specified file exists. | |
bool | fileExists (const char *prefix, const std::string &path) const |
Convenience function to save some typing in the file manager constructor. | |
bool | searchTextureContainerId (std::string &container_id, const std::string &file_name) const |
void | listFiles (std::set< std::string > &result, const std::string &dir, bool make_full_path=false) const |
Returns a list of files in a given directory. | |
void | pushTextureSearchPath (const std::string &path, const std::string &container_id) |
Adds a texture search path to the list of texture search paths. | |
void | pushModelSearchPath (const std::string &path) |
Adds a model search path to the list of model search paths. | |
void | popTextureSearchPath () |
Removes the last added texture search path from the list of paths. | |
void | popModelSearchPath () |
Removes the last added model search path from the list of paths. | |
void | popMusicSearchPath () |
Removes the last added directory from the music search path. | |
void | redirectOutput () |
Redirects output to go into files in the user's config directory instead of to the console. | |
bool | fileIsNewer (const std::string &f1, const std::string &f2) const |
Returns true if the first file is newer than the second. | |
const std::string & | getUserConfigDir () const |
irr::io::IFileSystem * | getFileSystem () |
Returns the irrlicht file system. | |
void | pushMusicSearchPath (const std::string &path) |
Adds a directory to the music search path (or stack). | |
std::string | getShader (const std::string &name) const |
Returns the full path to a shader (this function could be modified later to allow track-specific shaders). | |
std::string | getShadersDir () const |
const std::string & | getSTKAssetsDownloadDir () const |
const std::string & | getCertBundleLocation () const |
Static Public Member Functions | |
static void | addRootDirs (const std::string &roots) |
Adds paths to the list of stk root directories. | |
static void | setStdoutName (const std::string &name) |
Sets the name for the stdout log file. | |
static void | setStdoutDir (const std::string &dir) |
Sets the directory for the stdout log file. | |
static bool | isDirectory (const std::string &path) |
Returns true if the given name is a directory. | |
static const std::string & | getStdoutName () |
Returns the name of the stdout file for log messages. | |
Private Member Functions | |
bool | findFile (std::string &full_path, const std::string &fname, const std::vector< std::string > &search_path) const |
Tries to find the specified file in any of the given search paths. | |
bool | findFile (std::string &full_path, const std::string &fname, const std::vector< TextureSearchPath > &search_path) const |
Tries to find the specified file in any of the given search paths. | |
void | makePath (std::string &path, const std::string &dir, const std::string &fname) const |
io::path | createAbsoluteFilename (const std::string &f) |
In order to add and later remove paths we have to specify the absolute filename (and replace '\' with '/' on windows). | |
void | checkAndCreateConfigDir () |
Checks if the config directory exists, and it not, tries to create it. | |
void | checkAndCreateAddonsDir () |
Creates the directories for the addons data. | |
void | checkAndCreateScreenshotDir () |
Creates the directories for screenshots. | |
void | checkAndCreateReplayDir () |
Creates the directories for replay recorded. | |
void | checkAndCreateCachedTexturesDir () |
Creates the directories for cached textures. | |
void | checkAndCreateGPDir () |
Creates the directories for user-defined grand prix. | |
void | discoverPaths () |
Detects where the assets are stored. | |
void | addAssetsSearchPath () |
void | resetSubdir () |
Reset subdirectories to initial state, for example after download assets. | |
std::string | checkAndCreateLinuxDir (const char *env_name, const char *dir_name, const char *fallback1, const char *fallback2=NULL) |
Find a directory to use for remaining unix variants. | |
Private Attributes | |
std::vector< std::string > | m_subdir_name |
The names of the various subdirectories of the asset types. | |
io::IFileSystem * | m_file_system |
Handle to irrlicht's file systems. | |
std::string | m_user_config_dir |
Directory where user config files are stored. | |
std::string | m_addons_dir |
Directory where addons are stored. | |
std::string | m_screenshot_dir |
Directory to store screenshots in. | |
std::string | m_replay_dir |
Directory to store replays in. | |
std::string | m_cached_textures_dir |
Directory where resized textures are cached. | |
std::string | m_gp_dir |
Directory where user-defined grand prix are stored. | |
std::string | m_cert_bundle_location |
Location of the certificate bundle. | |
std::string | m_stk_assets_download_dir |
Mobile stk specific to download stk-assets in the first. | |
std::vector< TextureSearchPath > | m_texture_search_path |
std::vector< std::string > | m_model_search_path |
std::vector< std::string > | m_music_search_path |
class handling files and paths
The various asset types (and directories) STK might request.
The last entry ASSET_COUNT specifies the number of entries.
FileManager::FileManager | ( | ) |
The constructor of the file manager creates an irrlicht file system and detects paths for the user config file and assets base directory (data).
A second initialisation is done later once (see init()), once the user config file is read. This is necessary since part of discoverPaths depend on artist debug mode.
|
static |
Adds paths to the list of stk root directories.
roots | A ":" separated string of directories to add. |
|
private |
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.
|
private |
Creates the directories for cached textures.
This will set m_cached_textures_dir with the appropriate path.
|
private |
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.
bool FileManager::checkAndCreateDirectory | ( | const std::string & | path | ) |
If the directory specified in path does not exist, it is created.
This function does not support recursive operations, so if a directory "a/b" is tested, and "a" does not exist, this function will fail. \params path Directory to test.
bool FileManager::checkAndCreateDirectoryP | ( | const std::string & | path | ) |
If the directory specified in path does not exist, it is created recursively (mkdir -p style).
\params path Directory to test.
void FileManager::checkAndCreateDirForAddons | ( | const std::string & | dir | ) |
Creates a directory for an addon.
addons_name | Name of the directory to create. |
addons_type | The type, which is used as a subdirectory. E.g.: 'karts' (m_addons_dir/karts/name will be created). |
|
private |
Creates the directories for user-defined grand prix.
This will set m_gp_dir with the appropriate path.
|
private |
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.
env_name | Name of the environment variable to test first. |
dir_name | Name of the directory to create |
fallback1 | Subdirectory under $HOME to use if the environment variable is not defined or can not be created. |
fallback2 | Subdirectory under $HOME to use if the environment variable and fallback1 are not defined or can not be created. |
|
private |
Creates the directories for replay recorded.
This will set m_replay_dir with the appropriate path.
|
private |
Creates the directories for screenshots.
This will set m_screenshot_dir with the appropriate path.
bool FileManager::copyFile | ( | const std::string & | source, |
const std::string & | dest | ||
) |
Copies the file source to dest.
source | The file to read. |
dest | The new filename. |
XMLNode * FileManager::createXMLTree | ( | const std::string & | filename | ) |
Reads in a XML file and converts it into a XMLNode tree.
filename | Name of the XML file to read. |
XMLNode * FileManager::createXMLTreeFromString | ( | const std::string & | content | ) |
Reads in XML from a string and converts it into a XMLNode tree.
content | the string containing the XML content. |
|
private |
Detects where the assets are stored.
Now search for the path to all needed subdirectories.
bool FileManager::fileIsNewer | ( | const std::string & | f1, |
const std::string & | f2 | ||
) | const |
Returns true if the first file is newer than the second.
The comparison is based on the modification time of the two files.
|
private |
Tries to find the specified file in any of the given search paths.
full_path | On return contains the full path of the file, or "" if the file is not found. |
file_name | The name of the file to look for. |
search_path | The list of paths to search for the file. |
|
private |
Tries to find the specified file in any of the given search paths.
full_path | On return contains the full path of the file, or "" if the file is not found. |
file_name | The name of the file to look for. |
search_path | The list of paths to search for the file. |
std::string FileManager::getAddonsFile | ( | const std::string & | name | ) |
Returns the full path of a file in the addons directory.
name | Name of the file. |
std::string FileManager::getAsset | ( | FileManager::AssetType | type, |
const std::string & | name | ||
) | const |
Returns the full path of a file of the given asset class.
It is not checked if the file actually exists (use getAssetChecked() instead if checking is needed).
type | Type of the asset class. |
name | Name of the file to search. |
std::string FileManager::getAsset | ( | const std::string & | name | ) | const |
Searches in all root directories for the specified file.
name | Name of the file to find. |
|
inline |
Returns the full path to a shader (this function could be modified later to allow track-specific shaders).
name | Name of the shader. |
void FileManager::init | ( | ) |
This function is used to initialise the file-manager after reading in the user configuration data.
Esp. discovering the paths of all assets depends on the user config file (artist debug mode).
|
static |
Returns true if the given name is a directory.
path | File name to test. |
void FileManager::listFiles | ( | std::set< std::string > & | result, |
const std::string & | dir, | ||
bool | make_full_path = false |
||
) | const |
Returns a list of files in a given directory.
result | A reference to a std::vector<std::string> which will hold all files in a directory. The vector will be cleared. |
dir | The director for which to get the directory listing. |
make_full_path | If set to true, all listed files will be full paths. |
bool FileManager::moveDirectoryInto | ( | std::string | source, |
std::string | target | ||
) |
Move the source directory into the target directory location.
The target directory must be on the same drive as the source.
void FileManager::pushModelSearchPath | ( | const std::string & | path | ) |
Adds a model search path to the list of model search paths.
This path will be searched before any other existing paths.
void FileManager::pushTextureSearchPath | ( | const std::string & | path, |
const std::string & | container_id | ||
) |
Adds a texture search path to the list of texture search paths.
This path will be searched before any other existing paths.
void FileManager::redirectOutput | ( | ) |
Redirects output to go into files in the user's config directory instead of to the console.
It keeps backup copies of previous stdout files (3 atm), which can help to diagnose problems caused by a previous crash.
bool FileManager::removeDirectory | ( | const std::string & | name | ) | const |
Removes a directory (including all files contained).
The function could easily recursively delete further subdirectories, but this is commented out atm (to limit the amount of damage in case of a bug).
name | Directory name to remove. |
return | True if removal was successful. |
bool FileManager::removeFile | ( | const std::string & | name | ) | const |
Removes the specified file.
std::string FileManager::searchModel | ( | const std::string & | file_name | ) | const |
Returns the full path of a model file by searching all model search paths.
It throws an exception if the file is not found.
file_name | File name to search for. |
std::string FileManager::searchMusic | ( | const std::string & | file_name | ) | const |
Returns the full path of a music file by searching all music search paths.
It throws an exception if the file is not found.
file_name | File name to search for. |
std::string FileManager::searchTexture | ( | const std::string & | file_name | ) | const |
Returns the full path of a texture file name by searching in all directories currently in the texture search path.
The difference to a call getAsset(TEXTURE,...) is that the latter will only return textures from .../textures, while the searchTexture will also search e.g. in kart or track directories (depending on what is currently being loaded).
file_name | Name of the texture file to search. |
|
static |
Sets the directory for the stdout log file.
dir | Directory to use |
|
static |
Sets the name for the stdout log file.
filename | Filename to use (relative to the user config dir). |