SuperTuxKart
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
FileManager Class Reference

class handling files and paths More...

#include <file_manager.hpp>

Inheritance diagram for FileManager:
Inheritance graph
[legend]

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)
 
XMLNodecreateXMLTree (const std::string &filename)
 Reads in a XML file and converts it into a XMLNode tree.
 
XMLNodecreateXMLTreeFromString (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< TextureSearchPathm_texture_search_path
 
std::vector< std::string > m_model_search_path
 
std::vector< std::string > m_music_search_path
 

Static Private Attributes

static std::vector< std::string > m_root_dirs
 The list of all root directories.
 
static std::string m_stdout_filename = "stdout.log"
 Name of stdout file.
 
static std::string m_stdout_dir
 Directory of stdout file.
 

Detailed Description

class handling files and paths

Member Enumeration Documentation

◆ AssetType

The various asset types (and directories) STK might request.

The last entry ASSET_COUNT specifies the number of entries.

Constructor & Destructor Documentation

◆ FileManager()

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.

Member Function Documentation

◆ addRootDirs()

void FileManager::addRootDirs ( const std::string &  roots)
static

Adds paths to the list of stk root directories.

Parameters
rootsA ":" separated string of directories to add.

◆ checkAndCreateAddonsDir()

void FileManager::checkAndCreateAddonsDir ( )
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.

◆ checkAndCreateCachedTexturesDir()

void FileManager::checkAndCreateCachedTexturesDir ( )
private

Creates the directories for cached textures.

This will set m_cached_textures_dir with the appropriate path.

◆ checkAndCreateConfigDir()

void FileManager::checkAndCreateConfigDir ( )
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.

◆ checkAndCreateDirectory()

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.

Returns
True if the directory exists or could be created, false otherwise.

◆ checkAndCreateDirectoryP()

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.

Returns
True if the directory exists or could be created, false otherwise.

◆ checkAndCreateDirForAddons()

void FileManager::checkAndCreateDirForAddons ( const std::string &  dir)

Creates a directory for an addon.

Parameters
addons_nameName of the directory to create.
addons_typeThe type, which is used as a subdirectory. E.g.: 'karts' (m_addons_dir/karts/name will be created).

◆ checkAndCreateGPDir()

void FileManager::checkAndCreateGPDir ( )
private

Creates the directories for user-defined grand prix.

This will set m_gp_dir with the appropriate path.

◆ checkAndCreateLinuxDir()

std::string FileManager::checkAndCreateLinuxDir ( const char *  env_name,
const char *  dir_name,
const char *  fallback1,
const char *  fallback2 = NULL 
)
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.

Parameters
env_nameName of the environment variable to test first.
dir_nameName of the directory to create
fallback1Subdirectory under $HOME to use if the environment variable is not defined or can not be created.
fallback2Subdirectory under $HOME to use if the environment variable and fallback1 are not defined or can not be created.

◆ checkAndCreateReplayDir()

void FileManager::checkAndCreateReplayDir ( )
private

Creates the directories for replay recorded.

This will set m_replay_dir with the appropriate path.

◆ checkAndCreateScreenshotDir()

void FileManager::checkAndCreateScreenshotDir ( )
private

Creates the directories for screenshots.

This will set m_screenshot_dir with the appropriate path.

◆ copyFile()

bool FileManager::copyFile ( const std::string &  source,
const std::string &  dest 
)

Copies the file source to dest.

Parameters
sourceThe file to read.
destThe new filename.
Returns
True if the copy was successful, false otherwise.

◆ createXMLTree()

XMLNode * FileManager::createXMLTree ( const std::string &  filename)

Reads in a XML file and converts it into a XMLNode tree.

Parameters
filenameName of the XML file to read.

◆ createXMLTreeFromString()

XMLNode * FileManager::createXMLTreeFromString ( const std::string &  content)

Reads in XML from a string and converts it into a XMLNode tree.

Parameters
contentthe string containing the XML content.

◆ discoverPaths()

void FileManager::discoverPaths ( )
private

Detects where the assets are stored.

Now search for the path to all needed subdirectories.

◆ fileIsNewer()

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.

◆ findFile() [1/2]

bool FileManager::findFile ( std::string &  full_path,
const std::string &  file_name,
const std::vector< std::string > &  search_path 
) const
private

Tries to find the specified file in any of the given search paths.

Parameters
full_pathOn return contains the full path of the file, or "" if the file is not found.
file_nameThe name of the file to look for.
search_pathThe list of paths to search for the file.
Returns
True if the file is found, false otherwise.

◆ findFile() [2/2]

bool FileManager::findFile ( std::string &  full_path,
const std::string &  file_name,
const std::vector< TextureSearchPath > &  search_path 
) const
private

Tries to find the specified file in any of the given search paths.

Parameters
full_pathOn return contains the full path of the file, or "" if the file is not found.
file_nameThe name of the file to look for.
search_pathThe list of paths to search for the file.
Returns
True if the file is found, false otherwise.

◆ getAddonsFile()

std::string FileManager::getAddonsFile ( const std::string &  name)

Returns the full path of a file in the addons directory.

Parameters
nameName of the file.

◆ getAsset() [1/2]

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).

Parameters
typeType of the asset class.
nameName of the file to search.
Returns
Full path to the file.

◆ getAsset() [2/2]

std::string FileManager::getAsset ( const std::string &  name) const

Searches in all root directories for the specified file.

Parameters
nameName of the file to find.
Returns
Full path of the file, or "" if not found.

◆ getShader()

std::string FileManager::getShader ( const std::string &  name) const
inline

Returns the full path to a shader (this function could be modified later to allow track-specific shaders).

Parameters
nameName of the shader.

◆ init()

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).

◆ isDirectory()

bool FileManager::isDirectory ( const std::string &  path)
static

Returns true if the given name is a directory.

Parameters
pathFile name to test.

◆ listFiles()

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.

Parameters
resultA reference to a std::vector<std::string> which will hold all files in a directory. The vector will be cleared.
dirThe director for which to get the directory listing.
make_full_pathIf set to true, all listed files will be full paths.

◆ moveDirectoryInto()

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.

◆ pushModelSearchPath()

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.

◆ pushTextureSearchPath()

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.

◆ redirectOutput()

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.

◆ removeDirectory()

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).

Parameters
nameDirectory name to remove.
returnTrue if removal was successful.

◆ removeFile()

bool FileManager::removeFile ( const std::string &  name) const

Removes the specified file.

Returns
True if successful, or false if the file is not a regular file or can not be removed.

◆ searchModel()

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.

Parameters
file_nameFile name to search for.

◆ searchMusic()

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.

Parameters
file_nameFile name to search for.

◆ searchTexture()

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).

Parameters
file_nameName of the texture file to search.
Returns
The full path for the texture, or "" if the texture was not found.

◆ setStdoutDir()

void FileManager::setStdoutDir ( const std::string &  dir)
static

Sets the directory for the stdout log file.

Parameters
dirDirectory to use

◆ setStdoutName()

void FileManager::setStdoutName ( const std::string &  filename)
static

Sets the name for the stdout log file.

Parameters
filenameFilename to use (relative to the user config dir).

The documentation for this class was generated from the following files: