ScummVM API documentation
path_helper.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 //=============================================================================
23 //
24 // Functions related to constructing game and script paths.
25 //
26 // TODO: We need some kind of a "file manager" which deals with opening files
27 // in defined set of directories. To ensure that rest of the engine code does
28 // not work with explicit paths or creates directories on its own.
29 //
30 //=============================================================================
31 
32 #ifndef AGS_ENGINE_AC_PATH_HELPER_H
33 #define AGS_ENGINE_AC_PATH_HELPER_H
34 
35 #include "ags/shared/util/path.h"
36 
37 namespace AGS3 {
38 
39 using AGS::Shared::String;
40 
41 // Filepath tokens, which are replaced by platform-specific directory names
42 extern const char *UserSavedgamesRootToken;
43 extern const char *GameSavedgamesDirToken;
44 extern const char *GameDataDirToken;
45 extern const char *DefaultConfigFileName;
46 
47 // Subsitutes illegal characters with '_'. This function uses illegal chars array
48 // specific to current platform.
49 void FixupFilename(char *filename);
50 
51 // FSLocation describes a file system location defined by two parts:
52 // a secure path that engine does not own, and sub-path that it owns.
53 // The meaning of this is that engine is only allowed to create
54 // sub-path subdirectories, and only if secure path exists.
55 struct FSLocation {
56  String BaseDir; // base directory, which we assume already exists; not our responsibility
57  String SubDir; // sub-directory, relative to BaseDir
58  String FullDir; // full path to location
59  FSLocation() {}
60  FSLocation(const String &base) : BaseDir(base), FullDir(base) {
61  }
62  FSLocation(const String &base, const String &subdir)
63  : BaseDir(base), SubDir(subdir),
64  FullDir(AGS::Shared::Path::ConcatPaths(base, subdir)) {
65  }
66  inline bool IsValid() const {
67  return !FullDir.IsEmpty();
68  }
69  // Concats the given path to the existing full dir
70  inline FSLocation Concat(const String &path) const {
71  return FSLocation(BaseDir, AGS::Shared::Path::ConcatPaths(FullDir, path));
72  }
73  // Sets full path as a relative to the existing base dir
74  inline FSLocation Rebase(const String &path) const {
75  return FSLocation(BaseDir, AGS::Shared::Path::ConcatPaths(BaseDir, path));
76  }
77 };
78 // Tests the input path, if it's an absolute path then returns it unchanged;
79 // if it's a relative path then resolves it into absolute, using install dir as a base.
80 String PathFromInstallDir(const String &path);
81 FSLocation PathFromInstallDir(const FSLocation &fsloc);
82 // Makes sure that given system location is available, makes directories if have to (and if it's allowed to)
83 // Returns full file path on success, empty string on failure.
84 String PreparePathForWriting(const FSLocation &fsloc, const String &filename);
85 
86 // Following functions calculate paths to directories according to game setup
87 // Returns the directory where global user config is to be found
88 FSLocation GetGlobalUserConfigDir();
89 // Returns the directory where this game's user config is to be found
90 FSLocation GetGameUserConfigDir();
91 // Returns the directory where this game's shared app files are to be found
92 FSLocation GetGameAppDataDir();
93 // Returns the directory where this game's saves and user data are to be found
94 FSLocation GetGameUserDataDir();
95 
96 // ResolvedPath describes an actual location pointed by a user path (e.g. from script)
97 struct ResolvedPath {
98  FSLocation Loc; // location (directory)
99  String FullPath; // full path, including filename
100  String AltPath; // alternative read-only full path, for backwards compatibility
101  bool AssetMgr = false; // file is to be accessed through the asset manager
102  ResolvedPath() = default;
103  ResolvedPath(const String & file, const String & alt = "")
104  : FullPath(file), AltPath(alt) {
105  }
106  ResolvedPath(const FSLocation & loc, const String & file, const String & alt = "")
107  : Loc(loc), FullPath(AGS::Shared::Path::ConcatPaths(loc.FullDir, file)), AltPath(alt) {
108  }
109 };
110 // Resolves a file path provided by user (e.g. script) into actual file path,
111 // by substituting special keywords with actual platform-specific directory names.
112 // Fills in ResolvedPath object on success.
113 // Returns 'true' on success, and 'false' if either path is impossible to resolve
114 // or if the file path is forbidden to be accessed in current situation.
115 bool ResolveScriptPath(const String &sc_path, bool read_only, ResolvedPath &rp);
116 // Resolves a user file path for writing, and makes sure all the sub-directories are
117 // created along the actual path.
118 // Returns 'true' on success, and 'false' if either path is impossible to resolve,
119 // forbidden for writing, or if failed to create any subdirectories.
120 bool ResolveWritePathAndCreateDirs(const String &sc_path, ResolvedPath &rp);
121 // Creates all necessary subdirectories inside the safe parent location.
122 bool CreateFSDirs(const FSLocation &fs);
123 
124 } // namespace AGS3
125 
126 #endif
Definition: path_helper.h:55
Definition: path_helper.h:97
Definition: string.h:62
Definition: ags.h:40