1 | /* |
---|
2 | ----------------------------------------------------------------------------- |
---|
3 | This source file is part of OGRE |
---|
4 | (Object-oriented Graphics Rendering Engine) |
---|
5 | For the latest info, see http://www.ogre3d.org/ |
---|
6 | |
---|
7 | Copyright (c) 2000-2013 Torus Knot Software Ltd |
---|
8 | |
---|
9 | Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
10 | of this software and associated documentation files (the "Software"), to deal |
---|
11 | in the Software without restriction, including without limitation the rights |
---|
12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
13 | copies of the Software, and to permit persons to whom the Software is |
---|
14 | furnished to do so, subject to the following conditions: |
---|
15 | |
---|
16 | The above copyright notice and this permission notice shall be included in |
---|
17 | all copies or substantial portions of the Software. |
---|
18 | |
---|
19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
22 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
25 | THE SOFTWARE. |
---|
26 | ----------------------------------------------------------------------------- |
---|
27 | */ |
---|
28 | #ifndef __FileSystemLayer_H__ |
---|
29 | #define __FileSystemLayer_H__ |
---|
30 | |
---|
31 | #include "OgrePrerequisites.h" |
---|
32 | #include "OgreStringVector.h" |
---|
33 | |
---|
34 | namespace Ogre |
---|
35 | { |
---|
36 | /** Provides methods to find out where the Ogre config files are stored |
---|
37 | and where logs and settings files should be written to. |
---|
38 | @remarks |
---|
39 | In modern multi-user OS, a standard user account will often not |
---|
40 | have write access to the path where the SampleBrowser is stored. |
---|
41 | In order to still be able to store graphics settings and log |
---|
42 | output and for the user to overwrite the default Ogre config files, |
---|
43 | this class tries to create a folder inside the user's home directory. |
---|
44 | Specialised implementations for each individual platform must |
---|
45 | subclass this abstract interface. |
---|
46 | */ |
---|
47 | |
---|
48 | /** Implementation for the FileSystemLayer interface. */ |
---|
49 | class _OgreExport FileSystemLayer : public FileSystemLayerAlloc |
---|
50 | { |
---|
51 | public: |
---|
52 | /** Creates a concrete platform-dependent implementation of FileSystemLayer. |
---|
53 | @param subdir |
---|
54 | A subdirectory inside the user's path to distinguish between |
---|
55 | different Ogre releases. |
---|
56 | */ |
---|
57 | FileSystemLayer(const Ogre::String& subdir) |
---|
58 | { |
---|
59 | // determine directories to search for config files |
---|
60 | getConfigPaths(); |
---|
61 | // prepare write location in user directory |
---|
62 | prepareUserHome(subdir); |
---|
63 | } |
---|
64 | |
---|
65 | /** Search for the given config file in the user's home path. If it can't |
---|
66 | be found there, the function falls back to the system-wide install |
---|
67 | path for Ogre config files. (Usually the same place where the |
---|
68 | SampleBrowser resides, or a special config path above that path.) |
---|
69 | @param filename |
---|
70 | The config file name (without path) |
---|
71 | @return |
---|
72 | The full path to the config file. |
---|
73 | */ |
---|
74 | const Ogre::String getConfigFilePath(Ogre::String filename) const |
---|
75 | { |
---|
76 | #if OGRE_DEBUG_MODE == 1 && (OGRE_PLATFORM != OGRE_PLATFORM_APPLE && OGRE_PLATFORM != OGRE_PLATFORM_APPLE_IOS) |
---|
77 | // add OGRE_BUILD_SUFFIX (default: "_d") to config file names |
---|
78 | Ogre::String::size_type pos = filename.rfind('.'); |
---|
79 | if (pos != Ogre::String::npos) |
---|
80 | filename = filename.substr(0, pos) + OGRE_BUILD_SUFFIX + filename.substr(pos); |
---|
81 | #endif |
---|
82 | |
---|
83 | // look for the requested file in several locations: |
---|
84 | |
---|
85 | // 1. in the writable path (so user can provide custom files) |
---|
86 | Ogre::String path = getWritablePath(filename); |
---|
87 | if (fileExists(path)) |
---|
88 | return path; |
---|
89 | |
---|
90 | // 2. in the config file search paths |
---|
91 | for (size_t i = 0; i < mConfigPaths.size(); ++i) |
---|
92 | { |
---|
93 | path = mConfigPaths[i] + filename; |
---|
94 | if (fileExists(path)) |
---|
95 | return path; |
---|
96 | } |
---|
97 | |
---|
98 | // 3. fallback to current working dir |
---|
99 | return filename; |
---|
100 | } |
---|
101 | |
---|
102 | /** Find a path where the given filename can be written to. This path |
---|
103 | will usually be in the user's home directory. This function should |
---|
104 | be used for any output like logs and graphics settings. |
---|
105 | @param filename |
---|
106 | Name of the file. |
---|
107 | @return |
---|
108 | The full path to a writable location for the given filename. |
---|
109 | */ |
---|
110 | const Ogre::String getWritablePath(const Ogre::String& filename) const |
---|
111 | { |
---|
112 | return mHomePath + filename; |
---|
113 | } |
---|
114 | |
---|
115 | void setConfigPaths(const Ogre::StringVector &paths){ |
---|
116 | mConfigPaths = paths; |
---|
117 | } |
---|
118 | |
---|
119 | void setHomePath(const Ogre::String &path){ |
---|
120 | mHomePath = path; |
---|
121 | } |
---|
122 | |
---|
123 | /** Create a directory */ |
---|
124 | bool createDirectory(const Ogre::String& name); |
---|
125 | |
---|
126 | private: |
---|
127 | Ogre::StringVector mConfigPaths; |
---|
128 | Ogre::String mHomePath; |
---|
129 | |
---|
130 | /** Determine config search paths. */ |
---|
131 | void getConfigPaths(); |
---|
132 | |
---|
133 | /** Create an Ogre directory and the given subdir in the user's home. */ |
---|
134 | void prepareUserHome(const Ogre::String& subdir); |
---|
135 | |
---|
136 | /** Test if the given file exists. */ |
---|
137 | bool fileExists(const Ogre::String& path) const; |
---|
138 | }; |
---|
139 | |
---|
140 | } |
---|
141 | |
---|
142 | |
---|
143 | #endif |
---|