SuperTuxKart
file_manager.hpp
1 //
2 // SuperTuxKart - a fun racing game with go-kart
3 // Copyright (C) 2004-2015 Steve Baker <sjbaker1@airmail.net>
4 // Copyright (C) 2008-2015 Steve Baker, Joerg Henrichs
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 3
9 // of the License, or (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 
20 #ifndef HEADER_FILE_MANAGER_HPP
21 #define HEADER_FILE_MANAGER_HPP
22 
28 #include <mutex>
29 #include <string>
30 #include <vector>
31 #include <set>
32 
33 #include <irrString.h>
34 #include <IFileSystem.h>
35 namespace irr { class IrrlichtDevice; }
36 using namespace irr;
37 
38 #include "io/xml_node.hpp"
39 #include "utils/no_copy.hpp"
40 
42 {
43  std::string m_texture_search_path;
44  std::string m_container_id;
45 
46  TextureSearchPath(std::string path, std::string container_id) :
47  m_texture_search_path(path), m_container_id(container_id)
48  {
49  }
50 };
51 
56 class FileManager : public NoCopy
57 {
58 public:
61  enum AssetType {ASSET_MIN,
62  CHALLENGE=ASSET_MIN,
63  GFX, GRANDPRIX, GUI_ICON, GUI_SCREEN, GUI_DIALOG,
64  REPLAY, SHADER, SKIN, TTF, TRANSLATION, BUILTIN_ASSETS=TRANSLATION,
65  LIBRARY, MODEL, MUSIC, SFX, TEXTURE, SCRIPT, ASSET_MAX = SCRIPT,
66  ASSET_COUNT};
67 
68 private:
69  mutable std::mutex m_file_system_lock;
70 
72  std::vector< std::string > m_subdir_name;
73 
75  io::IFileSystem *m_file_system;
76 
78  std::string m_user_config_dir;
79 
81  std::string m_addons_dir;
82 
84  static std::vector<std::string> m_root_dirs;
85 
87  static std::string m_stdout_filename;
88 
90  static std::string m_stdout_dir;
91 
93  std::string m_screenshot_dir;
94 
96  std::string m_replay_dir;
97 
99  std::string m_cached_textures_dir;
100 
102  std::string m_gp_dir;
103 
106 
109 
110  std::vector<TextureSearchPath> m_texture_search_path;
111 
112  std::vector<std::string>
113  m_model_search_path,
114  m_music_search_path;
115  bool findFile(std::string& full_path,
116  const std::string& fname,
117  const std::vector<std::string>& search_path)
118  const;
119  bool findFile(std::string& full_path,
120  const std::string& fname,
121  const std::vector<TextureSearchPath>& search_path)
122  const;
123  void makePath(std::string& path, const std::string& dir,
124  const std::string& fname) const;
125  io::path createAbsoluteFilename(const std::string &f);
126  void checkAndCreateConfigDir();
127  void checkAndCreateAddonsDir();
128  void checkAndCreateScreenshotDir();
129  void checkAndCreateReplayDir();
130  void checkAndCreateCachedTexturesDir();
131  void checkAndCreateGPDir();
132  void discoverPaths();
133  void addAssetsSearchPath();
134  void resetSubdir();
135 #if !defined(WIN32) && !defined(__APPLE__)
136  std::string checkAndCreateLinuxDir(const char *env_name,
137  const char *dir_name,
138  const char *fallback1,
139  const char *fallback2=NULL);
140 #endif
141 
142 public:
143  FileManager();
144  ~FileManager();
145  void init();
146  void reinitAfterDownloadAssets();
147  static void addRootDirs(const std::string &roots);
148  static void setStdoutName(const std::string &name);
149  static void setStdoutDir(const std::string &dir);
150  io::IXMLReader *createXMLReader(const std::string &filename);
151  XMLNode *createXMLTree(const std::string &filename);
152  XMLNode *createXMLTreeFromString(const std::string & content);
153 
154  std::string getScreenshotDir() const;
155  std::string getReplayDir() const;
156  std::string getCachedTexturesDir() const;
157  std::string getGPDir() const;
158  bool checkAndCreateDirectory(const std::string &path);
159  bool checkAndCreateDirectoryP(const std::string &path);
160  const std::string &getAddonsDir() const;
161  std::string getAddonsFile(const std::string &name);
162  void checkAndCreateDirForAddons(const std::string &dir);
163  bool isDirectory(const std::string &path) const;
164  bool removeFile(const std::string &name) const;
165  bool removeDirectory(const std::string &name) const;
166  // ------------------------------------------------------------------------
167  bool moveDirectoryInto(std::string source, std::string target);
168  // ------------------------------------------------------------------------
169  bool copyFile(const std::string &source, const std::string &dest);
170  std::vector<std::string>getMusicDirs() const;
171  std::string getAssetChecked(AssetType type, const std::string& name,
172  bool abort_on_error=false) const;
173  std::string getAsset(AssetType type, const std::string &name) const;
174  std::string getAsset(const std::string &name) const;
175  // ------------------------------------------------------------------------
177  std::string getAssetDirectory(AssetType type) const
178  {
179  return m_subdir_name[type];
180  }
181  // ------------------------------------------------------------------------
182  std::string searchMusic(const std::string& file_name) const;
183  // ------------------------------------------------------------------------
184  std::string searchModel(const std::string& file_name) const;
185  std::string searchTexture(const std::string& fname) const;
186  std::string getUserConfigFile(const std::string& fname) const;
187  bool fileExists(const std::string& path) const;
188  // ------------------------------------------------------------------------
191  bool fileExists(const char *prefix, const std::string& path) const
192  {
193  return fileExists(std::string(prefix) + path);
194  }
195  // ------------------------------------------------------------------------
196  bool searchTextureContainerId(std::string& container_id,
197  const std::string& file_name) const;
198  // ------------------------------------------------------------------------
200  static const std::string& getStdoutName() { return m_stdout_filename; }
201  // ------------------------------------------------------------------------
202  void listFiles (std::set<std::string>& result,
203  const std::string& dir,
204  bool make_full_path=false) const;
205 
206 
207  void pushTextureSearchPath(const std::string& path, const std::string& container_id);
208  void pushModelSearchPath(const std::string& path);
209  void popTextureSearchPath();
210  void popModelSearchPath();
211  void popMusicSearchPath();
212  void redirectOutput();
213 
214  bool fileIsNewer(const std::string& f1, const std::string& f2) const;
215  // ------------------------------------------------------------------------
216  const std::string& getUserConfigDir() const { return m_user_config_dir; }
217  // ------------------------------------------------------------------------
219  irr::io::IFileSystem* getFileSystem() { return m_file_system; }
220  // ------------------------------------------------------------------------
223  void pushMusicSearchPath(const std::string& path)
224  {
225  m_music_search_path.push_back(path);
226  } // pushMusicSearchPath
227  // ------------------------------------------------------------------------
232  std::string getShader(const std::string &name) const
233  {
234  return getAsset(SHADER, name);
235 
236  } // getShader
237 
238  std::string getShadersDir() const
239  {
240  return m_subdir_name[SHADER];
241  }
242  // ------------------------------------------------------------------------
243  const std::string& getSTKAssetsDownloadDir() const
244  { return m_stk_assets_download_dir; }
245  // ------------------------------------------------------------------------
246  const std::string& getCertBundleLocation() const
247  { return m_cert_bundle_location; }
248 
249 }; // FileManager
250 
251 extern FileManager* file_manager;
252 
253 #endif
Definition: file_manager.hpp:41
std::string m_cached_textures_dir
Directory where resized textures are cached.
Definition: file_manager.hpp:99
std::string m_stk_assets_download_dir
Mobile stk specific to download stk-assets in the first.
Definition: file_manager.hpp:108
std::vector< std::string > m_subdir_name
The names of the various subdirectories of the asset types.
Definition: file_manager.hpp:72
class handling files and paths
Definition: file_manager.hpp:56
std::string m_replay_dir
Directory to store replays in.
Definition: file_manager.hpp:96
AssetType
The various asset types (and directories) STK might request.
Definition: file_manager.hpp:61
io::IFileSystem * m_file_system
Handle to irrlicht's file systems.
Definition: file_manager.hpp:75
bool fileExists(const char *prefix, const std::string &path) const
Convenience function to save some typing in the file manager constructor.
Definition: file_manager.hpp:191
static std::string m_stdout_filename
Name of stdout file.
Definition: file_manager.hpp:87
static std::string m_stdout_dir
Directory of stdout file.
Definition: file_manager.hpp:90
std::string m_gp_dir
Directory where user-defined grand prix are stored.
Definition: file_manager.hpp:102
std::string m_user_config_dir
Directory where user config files are stored.
Definition: file_manager.hpp:78
std::string m_addons_dir
Directory where addons are stored.
Definition: file_manager.hpp:81
Utility class, you can inherit from this class to disallow the assignment operator and copy construct...
Definition: no_copy.hpp:25
std::string m_screenshot_dir
Directory to store screenshots in.
Definition: file_manager.hpp:93
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 shad...
Definition: file_manager.hpp:232
std::string getAssetDirectory(AssetType type) const
Returns the directory of an asset.
Definition: file_manager.hpp:177
utility class used to parse XML files
Definition: xml_node.hpp:47
static const std::string & getStdoutName()
Returns the name of the stdout file for log messages.
Definition: file_manager.hpp:200
irr::io::IFileSystem * getFileSystem()
Returns the irrlicht file system.
Definition: file_manager.hpp:219
static std::vector< std::string > m_root_dirs
The list of all root directories.
Definition: file_manager.hpp:84
std::string m_cert_bundle_location
Location of the certificate bundle.
Definition: file_manager.hpp:105
void pushMusicSearchPath(const std::string &path)
Adds a directory to the music search path (or stack).
Definition: file_manager.hpp:223