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
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (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, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 //=============================================================================
24 //
25 // Functions related to constructing game and script paths.
26 //
27 // TODO: We need some kind of a "file manager" which deals with opening files
28 // in defined set of directories. To ensure that rest of the engine code does
29 // not work with explicit paths or creates directories on its own.
30 //
31 //=============================================================================
32 
33 #ifndef AGS_ENGINE_AC_PATH_HELPER_H
34 #define AGS_ENGINE_AC_PATH_HELPER_H
35 
36 #include "ags/shared/util/path.h"
37 
38 namespace AGS3 {
39 
40 using AGS::Shared::String;
41 
42 // Filepath tokens, which are replaced by platform-specific directory names
43 extern const char *UserSavedgamesRootToken;
44 extern const char *GameSavedgamesDirToken;
45 extern const char *GameDataDirToken;
46 extern const char *DefaultConfigFileName;
47 
48 // Subsitutes illegal characters with '_'. This function uses illegal chars array
49 // specific to current platform.
50 void FixupFilename(char *filename);
51 // Tests the input path, if it's an absolute path then returns it unchanged;
52 // if it's a relative path then resolves it into absolute, using install dir as a base.
53 String PathFromInstallDir(const String &path);
54 
55 // FSLocation describes a file system location defined by two parts:
56 // a secure path that engine does not own, and sub-path that it owns.
57 // The meaning of this is that engine is only allowed to create
58 // sub-path subdirectories, and only if secure path exists.
59 struct FSLocation {
60 	String BaseDir; // base directory, which we assume already exists; not our responsibility
61 	String SubDir;  // sub-directory, relative to BaseDir
62 	String FullDir; // full path to location
FSLocationFSLocation63 	FSLocation() {}
FSLocationFSLocation64 	FSLocation(const String &base) : BaseDir(base), FullDir(base) {
65 	}
FSLocationFSLocation66 	FSLocation(const String &base, const String &subdir)
67 		: BaseDir(base), SubDir(subdir),
68 		FullDir(AGS::Shared::Path::ConcatPaths(base, subdir)) {
69 	}
70 };
71 // Makes sure that given system location is available, makes directories if have to (and if it's allowed to)
72 // Returns full file path on success, empty string on failure.
73 String PreparePathForWriting(const FSLocation &fsloc, const String &filename);
74 
75 // Following functions calculate paths to directories according to game setup
76 // Returns the directory where global user config is to be found
77 FSLocation GetGlobalUserConfigDir();
78 // Returns the directory where this game's user config is to be found
79 FSLocation GetGameUserConfigDir();
80 // Returns the directory where this game's shared app files are to be found
81 FSLocation GetGameAppDataDir();
82 // Returns the directory where this game's saves and user data are to be found
83 FSLocation GetGameUserDataDir();
84 
85 // ResolvedPath describes an actual location pointed by a user path (e.g. from script)
86 struct ResolvedPath {
87 	FSLocation Loc;  // location (directory)
88 	String FullPath; // full path, including filename
89 	String AltPath;  // alternative read-only full path, for backwards compatibility
90 	ResolvedPath() = default;
91 	ResolvedPath(const String & file, const String & alt = "")
FullPathResolvedPath92 		: FullPath(file), AltPath(alt) {
93 	}
94 	ResolvedPath(const FSLocation & loc, const String & file, const String & alt = "")
LocResolvedPath95 		: Loc(loc), FullPath(AGS::Shared::Path::ConcatPaths(loc.FullDir, file)), AltPath(alt) {
96 	}
97 };
98 // Resolves a file path provided by user (e.g. script) into actual file path,
99 // by substituting special keywords with actual platform-specific directory names.
100 // Fills in ResolvedPath object on success.
101 // Returns 'true' on success, and 'false' if either path is impossible to resolve
102 // or if the file path is forbidden to be accessed in current situation.
103 bool ResolveScriptPath(const String &sc_path, bool read_only, ResolvedPath &rp);
104 // Resolves a user file path for writing, and makes sure all the sub-directories are
105 // created along the actual path.
106 // Returns 'true' on success, and 'false' if either path is impossible to resolve,
107 // forbidden for writing, or if failed to create any subdirectories.
108 bool ResolveWritePathAndCreateDirs(const String &sc_path, ResolvedPath &rp);
109 
110 } // namespace AGS3
111 
112 #endif
113