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 #ifndef SWORD1_RESMAN_H
24 #define SWORD1_RESMAN_H
25 
26 #include "sword1/memman.h"
27 #include "common/file.h"
28 #include "sword1/sworddefs.h"
29 #include "common/endian.h"
30 
31 namespace Sword1 {
32 
33 #define MAX_LABEL_SIZE (31+1)
34 
35 #if defined(__PSP__)
36 #define MAX_OPEN_CLUS 4 // the PSP can't have more than 8 files open simultaneously
37                         // since we also need filehandles for music and sometimes savegames
38                         // set the maximum number of open clusters to 4.
39 #else
40 #define MAX_OPEN_CLUS 8 // don't open more than 8 files at once
41 #endif
42 
43 struct Grp {
44 	uint32 noRes;
45 	MemHandle *resHandle;
46 	uint32 *offset;
47 	uint32 *length;
48 };
49 
50 struct Clu {
51 	uint32 refCount;
52 	Common::File *file;
53 	char label[MAX_LABEL_SIZE];
54 	uint32 noGrp;
55 	Grp *grp;
56 	Clu *nextOpen;
57 };
58 
59 struct Prj {
60 	uint32 noClu;
61 	Clu *clu;
62 };
63 
64 class ResMan {
65 public:
66 	ResMan(const char *fileName, bool isMacFile);
67 	~ResMan();
68 	void flush();
69 	void resClose(uint32 id);
70 	void resOpen(uint32 id);
71 	void *fetchRes(uint32 id);
72 	void dumpRes(uint32 id);
73 	void *openFetchRes(uint32 id);
74 	void *cptResOpen(uint32 id);
75 	Header *lockScript(uint32 scrID);
76 	void unlockScript(uint32 scrID);
77 	FrameHeader *fetchFrame(void *resourceData, uint32 frameNo);
78 
getUint16(uint16 value)79 	uint16 getUint16(uint16 value) {
80 		return (_isBigEndian) ? FROM_BE_16(value) : FROM_LE_16(value);
81 	}
getUint32(uint32 value)82 	uint32 getUint32(uint32 value) {
83 		return (_isBigEndian) ? FROM_BE_32(value) : FROM_LE_32(value);
84 	}
getLEUint16(uint16 value)85 	uint16 getLEUint16(uint16 value) {
86 		return FROM_LE_16(value);
87 	}
getLEUint32(uint32 value)88 	uint32 getLEUint32(uint32 value) {
89 		return FROM_LE_32(value);
90 	}
readUint16(const void * ptr)91 	uint16 readUint16(const void *ptr) {
92 		return (_isBigEndian) ? READ_BE_UINT16(ptr) : READ_LE_UINT16(ptr);
93 	}
readUint32(const void * ptr)94 	uint32 readUint32(const void *ptr) {
95 		return (_isBigEndian) ? READ_BE_UINT32(ptr) : READ_LE_UINT32(ptr);
96 	}
readLEUint32(const void * ptr)97 	uint32 readLEUint32(const void *ptr) {
98 		return READ_LE_UINT32(ptr);
99 	}
toUint16(uint16 value)100 	uint16 toUint16(uint16 value) {
101 		return (_isBigEndian) ? TO_BE_16(value) : TO_LE_16(value);
102 	}
toUint32(uint32 value)103 	uint32 toUint32(uint32 value) {
104 		return (_isBigEndian) ? TO_BE_32(value) : TO_LE_32(value);
105 	}
106 
107 
108 private:
109 	uint32     resLength(uint32 id);
110 	MemHandle *resHandle(uint32 id);
111 	uint32     resOffset(uint32 id);
112 	Common::File      *resFile(uint32 id);
113 
114 	void openCptResourceBigEndian(uint32 id);
115 	void openScriptResourceBigEndian(uint32 id);
116 	void openCptResourceLittleEndian(uint32 id);
117 	void openScriptResourceLittleEndian(uint32 id);
118 
119 	void loadCluDescript(const char *fileName);
120 	void freeCluDescript();
121 	Prj _prj;
122 	MemMan *_memMan;
123 	static const uint32 _scriptList[TOTAL_SECTIONS];    //a table of resource tags
124 	static uint32 _srIdList[29];
125 	Clu *_openCluStart, *_openCluEnd;
126 	int  _openClus;
127 	bool _isBigEndian;
128 };
129 
130 } // End of namespace Sword1
131 
132 #endif //RESMAN_H
133