ScummVM API documentation
asset_manager.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// Asset manager class for reading and writing game resources.
25//-----------------------------------------------------------------------------
26//
27// The code is based on CLIB32, by Chris Jones (1998-99), DJGPP implementation
28// of the CLIB reader.
29//
30//-----------------------------------------------------------------------------
31// TODO: consider replace/merge with PhysFS library in the future.
32//
33// TODO: support streams that work on a file subsection, limited by size,
34// to avoid having to return an asset size separately from a stream.
35// TODO: return stream as smart pointer.
36//
37//=============================================================================
38
39#ifndef AGS_SHARED_CORE_ASSET_MANAGER_H
40#define AGS_SHARED_CORE_ASSET_MANAGER_H
41
42#include "common/stream.h"
43#include "common/std/functional.h"
44#include "common/std/memory.h"
45#include "ags/shared/core/asset.h"
46#include "ags/shared/util/file.h" // TODO: extract filestream mode constants or introduce generic ones
47
48namespace AGS3 {
49namespace AGS {
50namespace Shared {
51
52class Stream;
53struct MultiFileLib;
54
55enum AssetSearchPriority {
56 kAssetPriorityDir,
57 kAssetPriorityLib
58};
59
60enum AssetError {
61 kAssetNoError = 0,
62 kAssetErrNoLibFile = -1, // library file not found or can't be read
63 kAssetErrLibParse = -2, // bad library file format or read error
64 kAssetErrNoManager = -6, // asset manager not initialized
65};
66
70struct AssetPath {
71 String Name;
72 String Filter;
73
74 AssetPath(const String &name = "", const String &filter = "") : Name(name), Filter(filter) {
75 }
76};
77
79public:
82 RemoveAllLibraries();
83 }
84
85 // Test if given file is main data file
86 static bool IsDataFile(const String &data_file);
87 // Read data file table of contents into provided struct
88 static AssetError ReadDataFileTOC(const String &data_file, AssetLibInfo &lib);
89
90 // Sets asset search priority (in which order manager will search available locations)
91 void SetSearchPriority(AssetSearchPriority priority);
92 // Gets current asset search priority
93 AssetSearchPriority GetSearchPriority() const;
94
95 // Add library location to the list of asset locations
96 AssetError AddLibrary(const String &path, const AssetLibInfo **lib = nullptr);
97 // Add library location, specifying comma-separated list of filters;
98 // if library was already added before, this method will overwrite the filters only
99 AssetError AddLibrary(const String &path, const String &filters, const AssetLibInfo **lib = nullptr);
100 // Remove library location from the list of asset locations
101 void RemoveLibrary(const String &path);
102 // Removes all libraries
103 void RemoveAllLibraries();
104
105 size_t GetLibraryCount() const;
106 const AssetLibInfo *GetLibraryInfo(size_t index) const;
107 // Tells whether asset exists in any of the registered search locations
108 bool DoesAssetExist(const String &asset_name, const String &filter = "") const;
109 inline bool DoesAssetExist(const AssetPath &apath) const {
110 return DoesAssetExist(apath.Name, apath.Filter);
111 }
112 // Searches in all the registered locations and collects a list of
113 // assets using given wildcard pattern
114 void FindAssets(std::vector<String> &assets, const String &wildcard,
115 const String &filter = "") const;
116 // Open asset stream in the given work mode; returns null if asset is not found or cannot be opened
117 // This method only searches in libraries that do not have any defined filters
118 Stream *OpenAsset(const String &asset_name) const;
119 // Open asset stream, providing a single filter to search in matching libraries
120 Stream *OpenAsset(const String &asset_name, const String &filter) const;
121 inline Stream *OpenAsset(const AssetPath &apath) const {
122 return OpenAsset(apath.Name, apath.Filter);
123 }
124 // Open asset stream in the given work mode; returns null if asset is not found or cannot be opened
125 // This method only searches in libraries that do not have any defined filters
126 Common::SeekableReadStream *OpenAssetStream(const String &asset_name) const;
127 // Open asset stream, providing a single filter to search in matching libraries
128 Common::SeekableReadStream *OpenAssetStream(const String &asset_name, const String &filter) const;
129
130private:
131 // AssetLibEx combines library info with extended internal data required for the manager
132 struct AssetLibEx : AssetLibInfo {
133 std::vector<String> Filters; // asset filters this library is matching to
134 std::vector<String> RealLibFiles; // fixed up library filenames
135
136 bool TestFilter(const String &filter) const;
137 };
138
139 // Loads library and registers its contents into the cache
140 AssetError RegisterAssetLib(const String &path, AssetLibEx *&lib);
141
142 // Tries to find asset in the given location, and then opens a stream for reading
143 Stream *OpenAssetFromLib(const AssetLibEx *lib, const String &asset_name) const;
144 Stream *OpenAssetFromDir(const AssetLibEx *lib, const String &asset_name) const;
145
147 std::vector<AssetLibEx *> _activeLibs;
148
149 AssetSearchPriority _libsPriority = kAssetPriorityDir;
150 // Sorting function, depends on priority setting
151 bool (*_libsSorter)(const AssetLibInfo *, const AssetLibInfo *);
152};
153
154String GetAssetErrorText(AssetError err);
155
156} // namespace Shared
157} // namespace AGS
158} // namespace AGS3
159
160#endif
Definition: asset_manager.h:78
Definition: stream.h:52
Definition: string.h:60
Definition: stream.h:745
Definition: asset.h:50
Definition: asset_manager.h:70