File: asset_manager.h

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (160 lines) | stat: -rw-r--r-- 6,349 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

//=============================================================================
//
// Asset manager class for reading and writing game resources.
//-----------------------------------------------------------------------------
//
// The code is based on CLIB32, by Chris Jones (1998-99), DJGPP implementation
// of the CLIB reader.
//
//-----------------------------------------------------------------------------
// TODO: consider replace/merge with PhysFS library in the future.
//
// TODO: support streams that work on a file subsection, limited by size,
// to avoid having to return an asset size separately from a stream.
// TODO: return stream as smart pointer.
//
//=============================================================================

#ifndef AGS_SHARED_CORE_ASSET_MANAGER_H
#define AGS_SHARED_CORE_ASSET_MANAGER_H

#include "common/stream.h"
#include "common/std/functional.h"
#include "common/std/memory.h"
#include "ags/shared/core/asset.h"
#include "ags/shared/util/file.h" // TODO: extract filestream mode constants or introduce generic ones

namespace AGS3 {
namespace AGS {
namespace Shared {

class Stream;
struct MultiFileLib;

enum AssetSearchPriority {
	kAssetPriorityDir,
	kAssetPriorityLib
};

enum AssetError {
	kAssetNoError = 0,
	kAssetErrNoLibFile = -1, // library file not found or can't be read
	kAssetErrLibParse = -2, // bad library file format or read error
	kAssetErrNoManager = -6, // asset manager not initialized
};

/**
 * AssetPath combines asset name and optional library filter, that serves to narrow down the search
 */
struct AssetPath {
	String Name;
	String Filter;

	AssetPath(const String &name = "", const String &filter = "") : Name(name), Filter(filter) {
	}
};

class AssetManager {
public:
	AssetManager() = default;
	~AssetManager() {
		RemoveAllLibraries();
	}

	// Test if given file is main data file
	static bool         IsDataFile(const String &data_file);
	// Read data file table of contents into provided struct
	static AssetError   ReadDataFileTOC(const String &data_file, AssetLibInfo &lib);

	// Sets asset search priority (in which order manager will search available locations)
	void         SetSearchPriority(AssetSearchPriority priority);
	// Gets current asset search priority
	AssetSearchPriority GetSearchPriority() const;

	// Add library location to the list of asset locations
	AssetError   AddLibrary(const String &path, const AssetLibInfo **lib = nullptr);
	// Add library location, specifying comma-separated list of filters;
	// if library was already added before, this method will overwrite the filters only
	AssetError   AddLibrary(const String &path, const String &filters, const AssetLibInfo **lib = nullptr);
	// Remove library location from the list of asset locations
	void         RemoveLibrary(const String &path);
	// Removes all libraries
	void         RemoveAllLibraries();

	size_t       GetLibraryCount() const;
	const AssetLibInfo *GetLibraryInfo(size_t index) const;
	// Tells whether asset exists in any of the registered search locations
	bool         DoesAssetExist(const String &asset_name, const String &filter = "") const;
	inline bool  DoesAssetExist(const AssetPath &apath) const {
		return DoesAssetExist(apath.Name, apath.Filter);
	}
	// Searches in all the registered locations and collects a list of
    // assets using given wildcard pattern
    void FindAssets(std::vector<String> &assets, const String &wildcard,
		const String &filter = "") const;
	// Open asset stream in the given work mode; returns null if asset is not found or cannot be opened
	// This method only searches in libraries that do not have any defined filters
	Stream *OpenAsset(const String &asset_name) const;
	// Open asset stream, providing a single filter to search in matching libraries
	Stream *OpenAsset(const String &asset_name, const String &filter) const;
	inline Stream *OpenAsset(const AssetPath &apath) const {
		return OpenAsset(apath.Name, apath.Filter);
	}
	// Open asset stream in the given work mode; returns null if asset is not found or cannot be opened
	// This method only searches in libraries that do not have any defined filters
	Common::SeekableReadStream *OpenAssetStream(const String &asset_name) const;
	// Open asset stream, providing a single filter to search in matching libraries
	Common::SeekableReadStream *OpenAssetStream(const String &asset_name, const String &filter) const;

private:
	// AssetLibEx combines library info with extended internal data required for the manager
	struct AssetLibEx : AssetLibInfo {
		std::vector<String> Filters; // asset filters this library is matching to
		std::vector<String> RealLibFiles; // fixed up library filenames

		bool TestFilter(const String &filter) const;
	};

	// Loads library and registers its contents into the cache
	AssetError  RegisterAssetLib(const String &path, AssetLibEx *&lib);

	// Tries to find asset in the given location, and then opens a stream for reading
	Stream *OpenAssetFromLib(const AssetLibEx *lib, const String &asset_name) const;
	Stream *OpenAssetFromDir(const AssetLibEx *lib, const String &asset_name) const;

	std::vector<AssetLibEx *> _libs;
	std::vector<AssetLibEx *> _activeLibs;

	AssetSearchPriority _libsPriority = kAssetPriorityDir;
	// Sorting function, depends on priority setting
	bool (*_libsSorter)(const AssetLibInfo *, const AssetLibInfo *);
};

String GetAssetErrorText(AssetError err);

} // namespace Shared
} // namespace AGS
} // namespace AGS3

#endif