aboutsummaryrefslogtreecommitdiff
path: root/engines/hopkins/saveload.cpp
blob: 427e2d9fcebd59efd55b5ce3d9d1c0cc54670145 (plain)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/* 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 2
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "common/system.h"
#include "common/savefile.h"
#include "graphics/surface.h"
#include "graphics/scaler.h"
#include "graphics/thumbnail.h"
#include "hopkins/saveload.h"
#include "hopkins/files.h"
#include "hopkins/globals.h"
#include "hopkins/graphics.h"
#include "hopkins/hopkins.h"

namespace Hopkins {

const char *SAVEGAME_STR = "HOPKINS";
#define SAVEGAME_STR_SIZE 13

void SaveLoadManager::setParent(HopkinsEngine *vm) {
	_vm = vm;
}

bool SaveLoadManager::bsave(const Common::String &file, const void *buf, size_t n) {
	Common::OutSaveFile *f = g_system->getSavefileManager()->openForSaving(file);

	if (f) {
		size_t bytesWritten = f->write(buf, n);
		f->finalize();
		delete f;

		return bytesWritten == n;
	} else 
		return false;
}

// Save File
bool SaveLoadManager::SAUVE_FICHIER(const Common::String &file, const void *buf, size_t n) {
	return bsave(file, buf, n);
}

void SaveLoadManager::initSaves() {
	Common::String dataFilename = "HISCORE.DAT";
	byte data[100];
	Common::fill(&data[0], &data[100], 0);
	
	SAUVE_FICHIER(dataFilename, data, 100);
}

void SaveLoadManager::bload(const Common::String &file, byte *buf) {
	Common::InSaveFile *f = g_system->getSavefileManager()->openForLoading(file);
	if (f == NULL)
		error("Error openinig file - %s", file.c_str());

	int32 filesize = f->size();
	f->read(buf, filesize);
	delete f;
}

bool SaveLoadManager::readSavegameHeader(Common::InSaveFile *in, hopkinsSavegameHeader &header) {
	char saveIdentBuffer[SAVEGAME_STR_SIZE + 1];
	header.thumbnail = NULL;

	// Validate the header Id
	in->read(saveIdentBuffer, SAVEGAME_STR_SIZE + 1);
	if (strncmp(saveIdentBuffer, SAVEGAME_STR, SAVEGAME_STR_SIZE))
		return false;

	header.version = in->readByte();
	if (header.version > HOPKINS_SAVEGAME_VERSION)
		return false;

	// Read in the string
	header.saveName.clear();
	char ch;
	while ((ch = (char)in->readByte()) != '\0') header.saveName += ch;

	// Get the thumbnail
	header.thumbnail = Graphics::loadThumbnail(*in);
	if (!header.thumbnail)
		return false;

	// Read in save date/time
	header.saveYear = in->readSint16LE();
	header.saveMonth = in->readSint16LE();
	header.saveDay = in->readSint16LE();
	header.saveHour = in->readSint16LE();
	header.saveMinutes = in->readSint16LE();
	header.totalFrames = in->readUint32LE();

	return true;
}

void SaveLoadManager::writeSavegameHeader(Common::OutSaveFile *out, hopkinsSavegameHeader &header) {
	// Write out a savegame header
	out->write(SAVEGAME_STR, SAVEGAME_STR_SIZE + 1);

	out->writeByte(HOPKINS_SAVEGAME_VERSION);

	// Write savegame name
	out->write(header.saveName.c_str(), header.saveName.size() + 1);

	// Create a thumbnail and save it
	Graphics::Surface *thumb = new Graphics::Surface();
	createThumbnail(thumb);
	Graphics::saveThumbnail(*out, *thumb);
	thumb->free();
	delete thumb;

	// Write out the save date/time
	TimeDate td;
	g_system->getTimeAndDate(td);
	out->writeSint16LE(td.tm_year + 1900);
	out->writeSint16LE(td.tm_mon + 1);
	out->writeSint16LE(td.tm_mday);
	out->writeSint16LE(td.tm_hour);
	out->writeSint16LE(td.tm_min);
	out->writeUint32LE(_vm->_eventsManager._gameCounter);
}

Common::Error SaveLoadManager::save(int slot, const Common::String &saveName) {
	// Try and create the save file
	Common::OutSaveFile *saveFile = g_system->getSavefileManager()->openForSaving(
		_vm->generateSaveName(slot));
	if (!saveFile)
		return Common::kCreatingFileFailed;

	// Set up the serializer
	Common::Serializer serializer(NULL, saveFile);

	// Write out the savegame header
	hopkinsSavegameHeader header;
	header.saveName = saveName;
	header.version = HOPKINS_SAVEGAME_VERSION;
	writeSavegameHeader(saveFile, header);

	// Write out the savegame data
	syncSavegameData(serializer);

	// Save file complete
	saveFile->finalize();
	delete saveFile;

	return Common::kNoError;
}

Common::Error SaveLoadManager::restore(int slot) {
	// Try and open the save file for reading
	Common::InSaveFile *saveFile = g_system->getSavefileManager()->openForLoading(
		_vm->generateSaveName(slot));
	if (!saveFile)
		return Common::kReadingFailed;

	// Set up the serializer
	Common::Serializer serializer(saveFile, NULL);

	// Read in the savegame header
	hopkinsSavegameHeader header;
	readSavegameHeader(saveFile, header);
	if (header.thumbnail)
		header.thumbnail->free();
	delete header.thumbnail;

	// Read in the savegame data
	syncSavegameData(serializer);

	// Loading save file complete
	delete saveFile;

	return Common::kNoError;
}

bool SaveLoadManager::readSavegameHeader(int slot, hopkinsSavegameHeader &header) {
	// Try and open the save file for reading
	Common::InSaveFile *saveFile = g_system->getSavefileManager()->openForLoading(
		g_vm->generateSaveName(slot));
	if (!saveFile)
		return false;

	bool result = readSavegameHeader(saveFile, header);
	delete saveFile;
	return result;
}

#define REDUCE_AMOUNT 80

void SaveLoadManager::createThumbnail(Graphics::Surface *s) {
	int w = _vm->_graphicsManager.Reel_Reduc(SCREEN_WIDTH, REDUCE_AMOUNT);
	int h = _vm->_graphicsManager.Reel_Reduc(SCREEN_HEIGHT - 40, REDUCE_AMOUNT); 

	s->create(w, h, Graphics::PixelFormat(2, 5, 6, 5, 0, 11, 5, 0, 0));	

	_vm->_graphicsManager.Reduc_Ecran(_vm->_graphicsManager.VESA_BUFFER, (byte *)s->pixels, 
		_vm->_eventsManager.start_x, 20, SCREEN_WIDTH, SCREEN_HEIGHT - 40, 80);
	_vm->_graphicsManager.INIT_TABLE(45, 80, _vm->_graphicsManager.Palette);
	_vm->_graphicsManager.Trans_bloc2((byte *)s->pixels, _vm->_graphicsManager.TABLE_COUL, 11136);
}

void SaveLoadManager::syncSavegameData(Common::Serializer &s) {
	s.syncBytes(&_vm->_globals.SAUVEGARDE->data[0], 0x802);
	syncSauvegarde1(s, _vm->_globals.SAUVEGARDE->field360);
	syncSauvegarde1(s, _vm->_globals.SAUVEGARDE->field370);
	syncSauvegarde1(s, _vm->_globals.SAUVEGARDE->field380);

	for (int i = 0; i < 35; ++i)
		s.syncAsSint16LE(_vm->_globals.SAUVEGARDE->inventory[i]);
}

void SaveLoadManager::syncSauvegarde1(Common::Serializer &s, Sauvegarde1 &item) {
	s.syncAsSint16LE(item.field0);
	s.syncAsSint16LE(item.field1);
	s.syncAsSint16LE(item.field2);
	s.syncAsSint16LE(item.field3);
	s.syncAsSint16LE(item.field4);
}

} // End of namespace Hopkins