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  * Additional copyright for this file:
8  * Copyright (C) 1994-1998 Revolution Software Ltd.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23  */
24 
25 #ifndef	SWORD2_RESMAN_H
26 #define	SWORD2_RESMAN_H
27 
28 namespace Common {
29 class File;
30 }
31 
32 #define MAX_MEM_CACHE (8 * 1024 * 1024) // we keep up to 8 megs of resource data files in memory
33 #define	MAX_res_files 20
34 
35 namespace Sword2 {
36 
37 class Sword2Engine;
38 
39 struct Resource {
40 	byte *ptr;
41 	uint32 size;
42 	uint32 refCount;
43 	Resource *next, *prev;
44 };
45 
46 struct ResourceFile {
47 	char fileName[20];
48 	int32 numEntries;
49 	uint32 *entryTab;
50 	uint8 cd;
51 };
52 
53 class ResourceManager {
54 private:
55 	Common::File *openCluFile(uint16 fileNum);
56 	void readCluIndex(uint16 fileNum, Common::File *file);
57 	void removeFromCacheList(Resource *res);
58 	void addToCacheList(Resource *res);
59 	void checkMemUsage();
60 
61 	Sword2Engine *_vm;
62 
63 	int _curCD;
64 	uint32 _totalResFiles;
65 	uint32 _totalClusters;
66 
67 	// Gode generated res-id to res number/rel number conversion table
68 
69 	uint16 *_resConvTable;
70 	ResourceFile _resFiles[MAX_res_files];
71 	Resource *_resList;
72 
73 	Resource *_cacheStart, *_cacheEnd;
74 	uint32 _usedMem; // amount of used memory in bytes
75 
76 public:
77 	ResourceManager(Sword2Engine *vm);	// read in the config file
78 	~ResourceManager();
79 
80 	bool init();
81 
getNumResFiles()82 	uint32 getNumResFiles() { return _totalResFiles; }
getNumClusters()83 	uint32 getNumClusters() { return _totalClusters; }
getResFiles()84 	ResourceFile *getResFiles() { return _resFiles; }
getResList()85 	Resource *getResList() { return _resList; }
86 
87 	byte *openResource(uint32 res, bool dump = false);
88 	void closeResource(uint32 res);
89 
90 	bool checkValid(uint32 res);
91 	uint32 fetchLen(uint32 res);
92 	uint8 fetchType(byte *ptr);
fetchType(uint32 res)93 	uint8 fetchType(uint32 res) {
94 		byte *ptr = openResource(res);
95 		uint8 type = fetchType(ptr);
96 		closeResource(res);
97 
98 		return type;
99 	}
100 
101 	byte *fetchName(uint32 res, byte *buf = NULL) {
102 		static byte tempbuf[NAME_LEN];
103 
104 		if (!buf)
105 			buf = tempbuf;
106 
107 		byte *ptr = openResource(res);
108 		memcpy(buf, ptr + 10, NAME_LEN);
109 		closeResource(res);
110 
111 		return buf;
112 	}
113 
fetchName(byte * ptr)114 	byte *fetchName(byte *ptr) {
115 		return ptr + 10;
116 	}
117 
118 	// Prompts the user for the specified CD.
119 	void askForCD(int cd);
120 
setCD(int cd)121 	void setCD(int cd) {
122 		if (cd)
123 			_curCD = cd;
124 	}
125 
getCD()126 	int getCD() {
127 		return _curCD;
128 	}
129 
130 	void remove(int res);
131 	void removeAll();
132 
133 	// ----console commands
134 
135 	void killAll(bool wantInfo);
136 	void killAllObjects(bool wantInfo);
137 };
138 
139 } // End of namespace Sword2
140 
141 #endif
142