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 MADE_DATABASE_H
24 #define MADE_DATABASE_H
25 
26 #include "common/hashmap.h"
27 
28 namespace Common {
29 class SeekableReadStream;
30 class WriteStream;
31 class String;
32 }
33 
34 namespace Made {
35 
36 class MadeEngine;
37 
38 class Object {
39 public:
40 
41 	Object();
42 	virtual ~Object();
43 
44 	virtual int load(Common::SeekableReadStream &source) = 0;
45 	virtual int load(byte *source) = 0;
46 	virtual int save(Common::WriteStream &dest) = 0;
47 	virtual uint16 getFlags() = 0;
48 	virtual uint16 getClass() = 0;
49 	virtual uint16 getSize() = 0;
50 	virtual byte getCount1() = 0;
51 	virtual byte getCount2() = 0;
52 	virtual byte *getData() = 0;
53 	virtual bool isConstant() = 0;
54 
55 	const char *getString();
56 	void setString(const char *str);
57 
58 	bool isObject();
59 	bool isVector();
60 
61 	int16 getVectorSize();
62 	int16 getVectorItem(int16 index);
63 	void setVectorItem(int16 index, int16 value);
64 
65 	void dump(const Common::String &filename);
66 
67 protected:
68 	bool _freeData;
69 	uint16 _objSize;
70 	byte *_objData;
71 };
72 
73 class ObjectV2 : public Object {
74 public:
75 	int load(Common::SeekableReadStream &source);
76 	int load(byte *source);
77 	int save(Common::WriteStream &dest);
78 	uint16 getFlags();
79 	uint16 getClass();
80 	uint16 getSize();
81 	byte getCount1();
82 	byte getCount2();
83 	byte *getData();
84 
isConstant()85 	bool isConstant() {
86 		return false;
87 	}
88 };
89 
90 class ObjectV1 : public ObjectV2 {
91 public:
92 	int load(Common::SeekableReadStream &source);
93 };
94 
95 class ObjectV3 : public Object {
96 public:
97 	int load(Common::SeekableReadStream &source);
98 	int load(byte *source);
99 	int save(Common::WriteStream &dest);
100 	uint16 getFlags();
101 	uint16 getClass();
102 	uint16 getSize();
103 	byte getCount1();
104 	byte getCount2();
105 	byte *getData();
106 
isConstant()107 	bool isConstant() {
108 		return !(getFlags() & 1);
109 	}
110 
111 };
112 
113 class GameDatabase {
114 public:
115 
116 	GameDatabase(MadeEngine *vm);
117 	virtual ~GameDatabase();
118 
119 	void open(const char *filename);
120 	void openFromRed(const char *redFilename, const char *filename);
121 
122 	void reload();
123 
getObject(int16 index)124 	Object *getObject(int16 index) const {
125 		if (index >= 1)
126 			return _objects[index - 1];
127 		else
128 			return NULL;
129 	}
130 
getObjectCount()131 	uint getObjectCount() const { return _objects.size(); }
132 
getMainCodeObjectIndex()133 	int16 getMainCodeObjectIndex() const { return _mainCodeObjectIndex; }
134 
135 	int16 getVar(int16 index);
136 	void setVar(int16 index, int16 value);
137 
138 	const char *getObjectString(int16 index);
139 	void setObjectString(int16 index, const char *str);
140 
141 	virtual int16 *findObjectProperty(int16 objectIndex, int16 propertyId, int16 &propertyFlag) = 0;
142 	int16 *findObjectPropertyCached(int16 objectIndex, int16 propertyId, int16 &propertyFlag);
143 	virtual const char *getString(uint16 offset) = 0;
144 	virtual bool getSavegameDescription(const char *filename, Common::String &description, int16 version) = 0;
145 	virtual int16 savegame(const char *filename, const char *description, int16 version) = 0;
146 	virtual int16 loadgame(const char *filename, int16 version) = 0;
147 
148 	int16 getObjectProperty(int16 objectIndex, int16 propertyId);
149 	int16 setObjectProperty(int16 objectIndex, int16 propertyId, int16 value);
150 
151 	void dumpObject(int16 index);
152 
153 protected:
154 	typedef Common::HashMap<uint32, int16 *> ObjectPropertyCacheMap;
155 	MadeEngine *_vm;
156 	Common::Array<Object *> _objects;
157 	ObjectPropertyCacheMap _objectPropertyCache;
158 	byte *_gameState;
159 	uint32 _gameStateSize;
160 	int16 _mainCodeObjectIndex;
161 	bool _isRedSource;
162 	Common::String _filename, _redFilename;
163 	virtual void load(Common::SeekableReadStream &sourceS) = 0;
164 	virtual void reloadFromStream(Common::SeekableReadStream &sourceS) = 0;
165 };
166 
167 class GameDatabaseV2 : public GameDatabase {
168 public:
169 	GameDatabaseV2(MadeEngine *vm);
170 	~GameDatabaseV2();
171 	int16 *findObjectProperty(int16 objectIndex, int16 propertyId, int16 &propertyFlag);
172 	const char *getString(uint16 offset);
173 	bool getSavegameDescription(const char *filename, Common::String &description, int16 version);
174 	int16 savegame(const char *filename, const char *description, int16 version);
175 	int16 loadgame(const char *filename, int16 version);
176 protected:
177 	char *_gameText;
178 	void load(Common::SeekableReadStream &sourceS);
179 	void reloadFromStream(Common::SeekableReadStream &sourceS);
180 };
181 
182 class GameDatabaseV3 : public GameDatabase {
183 public:
184 	GameDatabaseV3(MadeEngine *vm);
185 	int16 *findObjectProperty(int16 objectIndex, int16 propertyId, int16 &propertyFlag);
186 	const char *getString(uint16 offset);
187 	bool getSavegameDescription(const char *filename, Common::String &description, int16 version);
188 	int16 savegame(const char *filename, const char *description, int16 version);
189 	int16 loadgame(const char *filename, int16 version);
190 protected:
191 	char *_gameText;
192 	uint32 _gameStateOffs;
193 	void load(Common::SeekableReadStream &sourceS);
194 	void reloadFromStream(Common::SeekableReadStream &sourceS);
195 };
196 
197 } // End of namespace Made
198 
199 #endif /* MADE_H */
200