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 #include "common/savefile.h"
24 
25 #include "sludge/allfiles.h"
26 #include "sludge/backdrop.h"
27 #include "sludge/bg_effects.h"
28 #include "sludge/cursors.h"
29 #include "sludge/event.h"
30 #include "sludge/floor.h"
31 #include "sludge/fonttext.h"
32 #include "sludge/function.h"
33 #include "sludge/graphics.h"
34 #include "sludge/language.h"
35 #include "sludge/loadsave.h"
36 #include "sludge/moreio.h"
37 #include "sludge/newfatal.h"
38 #include "sludge/objtypes.h"
39 #include "sludge/people.h"
40 #include "sludge/region.h"
41 #include "sludge/savedata.h"
42 #include "sludge/sludge.h"
43 #include "sludge/sludger.h"
44 #include "sludge/sound.h"
45 #include "sludge/sprites.h"
46 #include "sludge/statusba.h"
47 #include "sludge/speech.h"
48 #include "sludge/utf8.h"
49 #include "sludge/variable.h"
50 #include "sludge/version.h"
51 #include "sludge/zbuffer.h"
52 
53 namespace Sludge {
54 
55 //----------------------------------------------------------------------
56 // From elsewhere
57 //----------------------------------------------------------------------
58 
59 extern LoadedFunction *saverFunc;					// In function.cpp
60 extern LoadedFunction *allRunningFunctions;         // In sludger.cpp
61 extern int numGlobals;                              // In sludger.cpp
62 extern Variable *globalVars;                        // In sludger.cpp
63 extern FILETIME fileTime;                           // In sludger.cpp
64 extern bool allowAnyFilename;
65 
66 //----------------------------------------------------------------------
67 // Save everything
68 //----------------------------------------------------------------------
69 
handleSaveLoad()70 bool handleSaveLoad() {
71 	if (!g_sludge->loadNow.empty()) {
72 		if (g_sludge->loadNow[0] == ':') {
73 			saveGame(g_sludge->loadNow.c_str() + 1);
74 			saverFunc->reg.setVariable(SVT_INT, 1);
75 		} else {
76 			if (!loadGame(g_sludge->loadNow))
77 				return false;
78 		}
79 		g_sludge->loadNow.clear();
80 	}
81 	return true;
82 }
83 
saveGame(const Common::String & fname)84 bool saveGame(const Common::String &fname) {
85 	Common::OutSaveFile *fp = g_system->getSavefileManager()->openForSaving(fname);
86 
87 	if (fp == NULL)
88 		return false;
89 
90 	fp->writeString("SLUDSA");
91 	fp->writeByte(0);
92 	fp->writeByte(0);
93 	fp->writeByte(MAJOR_VERSION);
94 	fp->writeByte(MINOR_VERSION);
95 
96 	if (!g_sludge->_gfxMan->saveThumbnail(fp))
97 		return false;
98 
99 	fp->write(&fileTime, sizeof(FILETIME));
100 
101 	// DON'T ADD ANYTHING NEW BEFORE THIS POINT!
102 
103 	fp->writeByte(allowAnyFilename);
104 	fp->writeByte(false); // deprecated captureAllKeys
105 	fp->writeByte(true);
106 	g_sludge->_txtMan->saveFont(fp);
107 
108 	// Save backdrop
109 	g_sludge->_gfxMan->saveBackdrop(fp);
110 
111 	// Save event handlers
112 	g_sludge->_evtMan->saveHandlers(fp);
113 
114 	// Save regions
115 	g_sludge->_regionMan->saveRegions(fp);
116 
117 	g_sludge->_cursorMan->saveCursor(fp);
118 
119 	// Save functions
120 	LoadedFunction *thisFunction = allRunningFunctions;
121 	int countFunctions = 0;
122 	while (thisFunction) {
123 		countFunctions++;
124 		thisFunction = thisFunction->next;
125 	}
126 	fp->writeUint16BE(countFunctions);
127 
128 	thisFunction = allRunningFunctions;
129 	while (thisFunction) {
130 		saveFunction(thisFunction, fp);
131 		thisFunction = thisFunction->next;
132 	}
133 
134 	for (int a = 0; a < numGlobals; a++) {
135 		globalVars[a].save(fp);
136 	}
137 
138 	g_sludge->_peopleMan->savePeople(fp);
139 
140 	g_sludge->_floorMan->save(fp);
141 
142 	g_sludge->_gfxMan->saveZBuffer(fp);
143 	g_sludge->_gfxMan->saveLightMap(fp);
144 
145 	g_sludge->_speechMan->save(fp);
146 	saveStatusBars(fp);
147 	g_sludge->_soundMan->saveSounds(fp);
148 
149 	fp->writeUint16BE(CustomSaveHelper::_saveEncoding);
150 
151 	blur_saveSettings(fp);
152 
153 	g_sludge->_gfxMan->saveColors(fp);
154 
155 	g_sludge->_gfxMan->saveParallax(fp);
156 	fp->writeByte(0);
157 
158 	g_sludge->_languageMan->saveLanguageSetting(fp);
159 
160 	g_sludge->_gfxMan->saveSnapshot(fp);
161 
162 	fp->flush();
163 	fp->finalize();
164 	delete fp;
165 
166 	clearStackLib();
167 	return true;
168 }
169 
170 //----------------------------------------------------------------------
171 // Load everything
172 //----------------------------------------------------------------------
173 
174 int ssgVersion;
175 
loadGame(const Common::String & fname)176 bool loadGame(const Common::String &fname) {
177 	Common::InSaveFile *fp = g_system->getSavefileManager()->openForLoading(fname);
178 	FILETIME savedGameTime;
179 
180 	while (allRunningFunctions)
181 		finishFunction(allRunningFunctions);
182 
183 	if (fp == NULL)
184 		return false;
185 
186 	bool headerBad = false;
187 	if (fp->readByte() != 'S')
188 		headerBad = true;
189 	if (fp->readByte() != 'L')
190 		headerBad = true;
191 	if (fp->readByte() != 'U')
192 		headerBad = true;
193 	if (fp->readByte() != 'D')
194 		headerBad = true;
195 	if (fp->readByte() != 'S')
196 		headerBad = true;
197 	if (fp->readByte() != 'A')
198 		headerBad = true;
199 	if (headerBad) {
200 		fatal(ERROR_GAME_LOAD_NO, fname);
201 		return NULL;
202 	}
203 	char c;
204 	c = fp->readByte();
205 	while ((c = fp->readByte()))
206 		;
207 
208 	int majVersion = fp->readByte();
209 	int minVersion = fp->readByte();
210 	ssgVersion = VERSION(majVersion, minVersion);
211 
212 	if (ssgVersion >= VERSION(1, 4)) {
213 		if (!g_sludge->_gfxMan->skipThumbnail(fp))
214 			return fatal(ERROR_GAME_LOAD_CORRUPT, fname);
215 	}
216 
217 	uint32 bytes_read = fp->read(&savedGameTime, sizeof(FILETIME));
218 	if (bytes_read != sizeof(FILETIME) && fp->err()) {
219 		warning("Reading error in loadGame.");
220 	}
221 
222 	if (savedGameTime.dwLowDateTime != fileTime.dwLowDateTime || savedGameTime.dwHighDateTime != fileTime.dwHighDateTime) {
223 		return fatal(ERROR_GAME_LOAD_WRONG, fname);
224 	}
225 
226 	// DON'T ADD ANYTHING NEW BEFORE THIS POINT!
227 
228 	if (ssgVersion >= VERSION(1, 4)) {
229 		allowAnyFilename = fp->readByte();
230 	}
231 	fp->readByte(); // deprecated captureAllKeys
232 	fp->readByte(); // updateDisplay (part of movie playing)
233 
234 	g_sludge->_txtMan->loadFont(ssgVersion, fp);
235 
236 	g_sludge->_regionMan->kill();
237 
238 	g_sludge->_gfxMan->loadBackdrop(ssgVersion, fp);
239 
240 	g_sludge->_evtMan->loadHandlers(fp);
241 
242 	g_sludge->_regionMan->loadRegions(fp);
243 
244 	if (!g_sludge->_cursorMan->loadCursor(fp)) {
245 		return false;
246 	}
247 
248 	LoadedFunction *rFunc;
249 	LoadedFunction **buildList = &allRunningFunctions;
250 
251 	int countFunctions = fp->readUint16BE();
252 	while (countFunctions--) {
253 		rFunc = loadFunction(fp);
254 		rFunc->next = NULL;
255 		(*buildList) = rFunc;
256 		buildList = &(rFunc->next);
257 	}
258 
259 	for (int a = 0; a < numGlobals; a++) {
260 		globalVars[a].unlinkVar();
261 		globalVars[a].load(fp);
262 	}
263 
264 	g_sludge->_peopleMan->loadPeople(fp);
265 
266 	if (!g_sludge->_floorMan->load(fp)) {
267 		return false;
268 	}
269 
270 	if (!g_sludge->_gfxMan->loadZBuffer(fp))
271 		return false;
272 
273 	if (!g_sludge->_gfxMan->loadLightMap(ssgVersion, fp)) {
274 		return false;
275 	}
276 
277 	g_sludge->_speechMan->load(fp);
278 	loadStatusBars(fp);
279 	g_sludge->_soundMan->loadSounds(fp);
280 
281 	CustomSaveHelper::_saveEncoding = fp->readUint16BE();
282 
283 	if (ssgVersion >= VERSION(1, 6)) {
284 		if (ssgVersion < VERSION(2, 0)) {
285 			// aaLoad
286 			fp->readByte();
287 			fp->readFloatLE();
288 			fp->readFloatLE();
289 		}
290 
291 		blur_loadSettings(fp);
292 	}
293 
294 	if (ssgVersion >= VERSION(1, 3)) {
295 		g_sludge->_gfxMan->loadColors(fp);
296 
297 		// Read parallax layers
298 		while (fp->readByte()) {
299 			int im = fp->readUint16BE();
300 			int fx = fp->readUint16BE();
301 			int fy = fp->readUint16BE();
302 
303 			if (!g_sludge->_gfxMan->loadParallax(im, fx, fy))
304 				return false;
305 		}
306 
307 		g_sludge->_languageMan->loadLanguageSetting(fp);
308 	}
309 
310 	g_sludge->_gfxMan->nosnapshot();
311 	if (ssgVersion >= VERSION(1, 4)) {
312 		if (fp->readByte()) {
313 			if (!g_sludge->_gfxMan->restoreSnapshot(fp))
314 				return false;
315 		}
316 	}
317 
318 	delete fp;
319 
320 	clearStackLib();
321 	return true;
322 }
323 
324 } // End of namespace Sludge
325