aboutsummaryrefslogtreecommitdiff
path: root/engines/glk/alan2/saveload.cpp
blob: 8d2f7a43be6266f256400f5c038db8580ddc80bc (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
/* 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/savefile.h"
#include "glk/alan2/acode.h"
#include "glk/alan2/alan2.h"
#include "glk/alan2/saveload.h"
#include "glk/alan2/types.h"

namespace Glk {
namespace Alan2 {

void SaveLoad::save() {
	int i;
	Common::SaveFileManager *saveFileMan = _vm->getSaveFileManager();
	Common::OutSaveFile *saveFile = nullptr;
	Common::String str;
	AtrElem *atr;

	// TODO
#if 0
	frefid_t fref = glk_fileref_create_by_prompt(fileusage_SavedGame, filemode_Write, 0);
	if (fref == NULL)
		_vm->printError(M_SAVEFAILED);

	strcpy(str, garglk_fileref_get_name(fref));
	glk_fileref_destroy(fref);
#endif

	if (str.empty())
		str = _prevSaveName;

	_vm->col = 1;

	// TODO
#if 0
	if ((savfil = fopen(str, READ_MODE)) != NULL)
		// It already existed
		if (!confirm(M_SAVEOVERWRITE))
			_vm->printError(MSGMAX);            // Return to player without saying anything

	if ((savfil = fopen(str, WRITE_MODE)) == NULL)
		_vm->printError(M_SAVEFAILED);
#endif

	saveFile = saveFileMan->openForSaving(str);

	_prevSaveName = str;

	// Save version of interpreter and name of game
	saveFile->write(_vm->header->vers, sizeof(Aword));
	saveFile->writeString(_gameName);
	saveFile->writeByte('\0');
	// Save current values
	saveFile->write(&_vm->cur, sizeof(_vm->cur));

	// Save actors
	for (i = ACTMIN; i <= ACTMAX; i++) {
		saveFile->writeUint32LE(_actors[i - ACTMIN].loc);
		saveFile->writeUint32LE(_actors[i - ACTMIN].script);
		saveFile->writeUint32LE(_actors[i - ACTMIN].step);
		saveFile->writeUint32LE(_actors[i - ACTMIN].count);
		if (_actors[i - ACTMIN].atrs)
			for (atr = (AtrElem *)addrTo(_actors[i - ACTMIN].atrs); !endOfTable(atr); atr++)
				saveFile->writeUint32LE(atr->val);
	}

	// Save locations
	for (i = LOCMIN; i <= LOCMAX; i++) {
		saveFile->writeUint32LE(_locations[i - LOCMIN].describe);
		if (_locations[i - LOCMIN].atrs)
			for (atr = (AtrElem *)addrTo(_locations[i - LOCMIN].atrs); !endOfTable(atr); atr++)
				saveFile->writeUint32LE(atr->val);
	}

	// Save objects
	for (i = OBJMIN; i <= OBJMAX; i++) {
		saveFile->writeUint32LE(_objects[i - OBJMIN].loc);
		if (_objects[i - OBJMIN].atrs)
			for (atr = (AtrElem *)addrTo(_objects[i - OBJMIN].atrs); !endOfTable(atr); atr++)
				saveFile->writeUint32LE(atr->val);
	}

	// Save the event queue
	_events[*_eventTop].time = 0;        // Mark the top
	for (i = 0; i < *_eventTop + 1; i++)
		saveFile->write(&_events[i], sizeof(_events[0]));

	// Save scores
	for (i = 0; _vm->scores[i] != EOF; i++)
		saveFile->writeUint32LE(_vm->scores[i]);

	delete saveFile;
}

void SaveLoad::restore() {
	int i, tmp;
	Common::SaveFileManager *saveFileMan = _vm->getSaveFileManager();
	Common::InSaveFile *saveFile = nullptr;
	Common::String str;
	AtrElem *atr;
	char savedVersion[4];
	char savedName[256];

	// TODO
#if 0
	frefid_t fref = glk_fileref_create_by_prompt(fileusage_SavedGame, filemode_Read, 0);
	if (fref == NULL)
		_vm->printError(M_SAVEFAILED);

	strcpy(str, garglk_fileref_get_name(fref));
	glk_fileref_destroy(fref);
#endif

	_vm->col = 1;

	if (str.empty())
		str = _prevSaveName;	// Use the name temporarily

	if ((saveFile = saveFileMan->openForLoading(str)) == nullptr) {
		_vm->printError(M_SAVEMISSING);
		return;
	}

	_prevSaveName = str;		// Save it for future use

	saveFile->read(savedVersion, sizeof(Aword));

	// 4f - save file version check doesn't seem to work on PC's!
	if (strncmp(savedVersion, _vm->header->vers, 4)) {
		delete saveFile;
		_vm->printError(M_SAVEVERS);
		return;
	}

	i = 0;

	while ((savedName[i++] = saveFile->readByte()) != '\0');

	if (savedName != _gameName) {
		delete saveFile;
		_vm->printError(M_SAVENAME);
		return;
	}

	// Restore current values
	saveFile->read(&_vm->cur, sizeof(_vm->cur));

	// Restore actors
	for (i = ACTMIN; i <= ACTMAX; i++) {
		_actors[i - ACTMIN].loc = saveFile->readUint32LE();
		_actors[i - ACTMIN].script = saveFile->readUint32LE();
		_actors[i - ACTMIN].step = saveFile->readUint32LE();
		_actors[i - ACTMIN].count = saveFile->readUint32LE();

		if (_actors[i - ACTMIN].atrs)
			for (atr = (AtrElem *)addrTo(_actors[i - ACTMIN].atrs); !endOfTable(atr); atr++)
				atr->val = saveFile->readUint32LE();
	}

	// Restore locations
	for (i = LOCMIN; i <= LOCMAX; i++) {
		_locations[i - LOCMIN].describe = saveFile->readUint32LE();
		if (_locations[i - LOCMIN].atrs)
			for (atr = (AtrElem *)addrTo(_locations[i - LOCMIN].atrs); !endOfTable(atr); atr++)
				atr->val = saveFile->readUint32LE();
	}

	// Restore objects
	for (i = OBJMIN; i <= OBJMAX; i++) {
		_objects[i - OBJMIN].loc = saveFile->readUint32LE();
		if (_objects[i - OBJMIN].atrs)
			for (atr = (AtrElem *)addrTo(_objects[i - OBJMIN].atrs); !endOfTable(atr); atr++)
				atr->val = saveFile->readUint32LE();
	}

	// Restore the event queue
	*_eventTop = 0;
	do {
		saveFile->read(&_events[*_eventTop], sizeof(_events[0]));
		(*_eventTop)++;
	} while (_events[*_eventTop - 1].time != 0);

	(*_eventTop)--;

	// Restore scores
	for (i = 0; _vm->scores[i] != EOF; i++)
		_vm->scores[i] = saveFile->readUint32LE();

	delete saveFile;
}

bool SaveLoad::endOfTable(AtrElem *addr) {
	Aword *x = (Aword *)addr;
	return *x == EOF;
}

} // End of namespace Alan2
} // End of namespace Glk