Coverage Report

Created: 2025-08-25 06:48

/src/ogre/OgreMain/include/OgreFileSystemLayer.h
Line
Count
Source (jump to first uncovered line)
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-2014 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
#include "OgreHeaderPrefix.h"
34
35
namespace Ogre
36
{
37
    /** Provides methods to find out where the Ogre config files are stored
38
        and where logs and settings files should be written to.
39
40
        In modern multi-user OS, a standard user account will often not
41
        have write access to the path where the application is stored.
42
        In order to still be able to store graphics settings and log
43
        output and for the user to overwrite the default Ogre config files,
44
        this class tries to create a folder inside the user's home directory.
45
      */
46
    class _OgreExport FileSystemLayer : public FileSystemLayerAlloc
47
    {
48
    public:
49
        /** Creates a concrete platform-dependent implementation of FileSystemLayer.
50
         @param subdir
51
         A subdirectory inside the user's path to distinguish between
52
         different Ogre applications.
53
         */
54
        FileSystemLayer(const Ogre::String& subdir)
55
0
        {
56
0
            // determine directories to search for config files
57
0
            getConfigPaths();
58
0
            // prepare write location in user directory
59
0
            prepareUserHome(subdir);
60
0
        }
61
        
62
        /** Search for the given config file in a set of predefined locations
63
64
         The search order is
65
         1. Subdirectory in user Home (see @ref getWritablePath)
66
         2. OS dependent config-paths
67
         3. Current working directory
68
69
         @param filename The config file name (without path)
70
         @return The full path to the config file
71
         */
72
        Ogre::String getConfigFilePath(Ogre::String filename) const
73
0
        {
74
0
            // look for the requested file in several locations:
75
0
            
76
0
            // 1. in the writable path (so user can provide custom files)
77
0
            Ogre::String path = getWritablePath(filename);
78
0
            if (fileExists(path))
79
0
                return path;
80
0
            
81
0
            // 2. in the config file search paths
82
0
            for (const String& cpath : mConfigPaths)
83
0
            {
84
0
                path = cpath + filename;
85
0
                if (fileExists(path))
86
0
                    return path;
87
0
            }
88
0
            
89
0
            // 3. fallback to current working dir
90
0
            return filename;
91
0
        }
92
93
        /** Find a path where the given filename can be written to. This path
94
         will usually be a subdirectory in the user's home directory.
95
         This function should be used for any output like logs and graphics settings.
96
97
         | Platform         | Location |
98
         |------------------|----------|
99
         | Windows          | Documents/$subdir/ |
100
         | Linux            | ~/.cache/$subdir/ |
101
         | OSX              | ~/Library/Application Support/$subdir/ |
102
         | iOS              | NSDocumentDirectory |
103
         | Android / Emscripten | n/a |
104
105
         @param filename Name of the file.
106
         @return The full path to a writable location for the given filename.
107
         */
108
        Ogre::String getWritablePath(const Ogre::String& filename) const
109
0
        {
110
0
            return mHomePath + filename;
111
0
        }
112
        
113
0
        void setConfigPaths(const Ogre::StringVector &paths){
114
0
            mConfigPaths = paths;
115
0
        }
116
        
117
0
        void setHomePath(const Ogre::String &path){
118
0
            mHomePath = path;
119
0
        }
120
        
121
        /** Resolve path inside the application bundle
122
         * on some platforms Ogre is delivered as an application bundle
123
         * this function resolves the given path such that it points inside that bundle
124
         * @param path
125
         * @return path inside the bundle
126
         */
127
        static String resolveBundlePath(String path);
128
129
        /** Create a directory. */
130
        static bool createDirectory(const Ogre::String& name);
131
        /** Delete a directory. Should be empty */
132
        static bool removeDirectory(const Ogre::String& name);
133
        /** Test if the given file exists. */
134
        static bool fileExists(const Ogre::String& path);
135
        /** Delete a file. */
136
        static bool removeFile(const Ogre::String& path);
137
        /** Rename a file. */
138
        static bool renameFile(const Ogre::String& oldpath, const Ogre::String& newpath);
139
140
    private:
141
        Ogre::StringVector mConfigPaths;
142
        Ogre::String mHomePath;
143
        
144
        /** Determine config search paths. */
145
        void getConfigPaths();
146
        
147
        /** Create an Ogre directory and the given subdir in the user's home. */
148
        void prepareUserHome(const Ogre::String& subdir);
149
    };
150
151
}
152
153
#include "OgreHeaderSuffix.h"
154
155
#endif