aboutsummaryrefslogtreecommitdiff
path: root/backends/ps2/savefile.cpp
blob: 78def68d7b2f5a7f6fad4c450412caa2cc76c011 (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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2005 The ScummVM project
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header$
 *
 */

#include "backends/ps2/savefile.h"
#include "backends/ps2/Gs2dScreen.h"

#include <tamtypes.h>
#include <kernel.h>
#include <sifrpc.h>
#include <loadfile.h>
#include <fileio.h>
#include <malloc.h>
#include <ucl/ucl.h>
#include "scummsys.h"

class StdioSaveFile : public SaveFile {
public:
	StdioSaveFile(const char *filename, bool saveOrLoad);
	virtual ~StdioSaveFile();
	virtual bool isOpen() const;
	virtual uint32 read(void *buf, uint32 cnt);
	virtual uint32 write(const void *buf, uint32 cnt);
private:
	FILE *_fh;
	bool _saving;
};

class UclSaveFile : public SaveFile {
public:
	UclSaveFile(const char *filename, bool saveOrLoad, Gs2dScreen *screen);
	virtual ~UclSaveFile();
	virtual bool isOpen() const;
	virtual uint32 read(void *buf, uint32 cnt);
	virtual uint32 write(const void *buf, uint32 cnt);
private:
	Gs2dScreen *_screen;
	FILE *_fh;
	bool _saving;
	uint8 *_buf;
	uint32 _bufSize, _bufPos;
};

#define ARRAY_ENTRIES	16
static mcTable mcDir[ARRAY_ENTRIES] __attribute__((aligned(64)));
static int     mcEntries;
static bool    mcNeedUpdate = true;

Ps2SaveFileManager::Ps2SaveFileManager(const char *path, SaveMode mode, Gs2dScreen *screen) {
	_screen = screen;
	if (mcInit(MC_TYPE_MC) < 0) {
		printf("Can't init libmc!\n");
		SleepThread();
	}
	
	if (path)
		strcpy(_savePath, path);
	else
		_savePath[0] = '\0';

	_mode = mode;
	if (mode == TO_HOST) {
		printf("saving to host:/\n");
	} else if (mode == TO_MC) {
		int mcType, mcFree, mcFormat, res;
		mcGetInfo(0, 0, &mcType, &mcFree, &mcFormat);
		mcSync(0, NULL, &res);
		if ((res == 0) || (res == -1)) // mc okay
			printf("MC okay, result = %d. Type %d, Free %d, Format %d\n", res, mcType, mcFree, mcFormat);
		else
			printf("MC failed, not present or not formatted, code %d\n", res);
	} else {
		printf("HDD not implemented yet\n");
		SleepThread();
	}

	checkMainDirectory();
}

Ps2SaveFileManager::~Ps2SaveFileManager(void) {
}

void Ps2SaveFileManager::checkMainDirectory(void) {
	// verify that the main directory (scummvm config + icon) exists
	int ret;
	mcGetDir(0, 0, "/ScummVM/*", 0, ARRAY_ENTRIES, mcDir);
	mcSync(0, NULL, &ret);
	printf("/ScummVM/* res = %d\n", ret);
	if (ret <= 0) { // assume directory doesn't exist
		printf("Dir doesn't exist\n");
		fioMkdir("mc0:ScummVM");
		FILE *outf = fopen("mc0:ScummVM/scummvm.icn", "wb");
		if (outf) {
			uint16 icoSize;
			uint16 *icoBuf = decompressIconData(&icoSize);
			fwrite(icoBuf, 2, icoSize, outf);
			fclose(outf);
			printf(".icn written\n");
			free(icoBuf);

			setupIcon("mc0:ScummVM/icon.sys", "scummvm.icn", "ScummVM", "Configuration");
		} else
			printf("unable to write icon data\n");
	}
}

SaveFile *Ps2SaveFileManager::openSavefile(const char *filename, bool saveOrLoad) {
	char *defaultExt = "SAVE";
	char nameBase[256];
	strcpy(nameBase, filename);
	char *ext = strchr(nameBase, '.');
	if (ext) {	
		*ext = '\0';
		ext++;
		if (!*ext)
			ext = defaultExt;
	} else
		ext = defaultExt;

	if (_mode == TO_HOST) {
		char hostName[256];
		sprintf(hostName, "%s%s", _savePath, filename);
		SaveFile *res = new StdioSaveFile(hostName, saveOrLoad);
		if (!res->isOpen()) {
			printf("unable to open savefile %s for %s\n", hostName, saveOrLoad ? "saving" : "loading");
			delete res;
			return NULL;
		}
		printf("Savefile %s opened for %s\n", hostName, saveOrLoad ? "saving" : "loading");
		return res;
	} else if (_mode == TO_MC) {
		_screen->wantAnim(true);
		int mcType, mcFree, mcFormat, mcResult;
		mcGetInfo(0, 0, &mcType, &mcFree, &mcFormat);
		mcSync(0, NULL, &mcResult);
		if (mcResult == -1) // memory card was exchanged
			mcNeedUpdate = true;
		else if (mcResult != 0) {
			printf("Memory card is not ready\n");
			return NULL;
		}

		char dirStr[256];
		sprintf(dirStr, "/ScummVM-%s/*", nameBase);
		if (saveOrLoad) { // saving
			mcGetDir(0, 0, dirStr, 0, ARRAY_ENTRIES, mcDir);
			mcSync(0, NULL, &mcEntries);
			mcNeedUpdate = true;
			if (mcEntries <= 0) { // directory is empty or doesn't exist.
				sprintf(dirStr, "mc0:ScummVM-%s", nameBase);
				printf("Creating directory %s\n", dirStr);
				if (mcEntries < 0) { // directory doesn't exist
					if (fioMkdir(dirStr) < 0) {
						printf("unable to create directory %s\n", dirStr);
						_screen->wantAnim(false);
						return NULL; // unable to create directory
					}
				}
				char icoSysDest[256], saveDesc[256];
				sprintf(icoSysDest, "%s/icon.sys", dirStr);
				strcpy(saveDesc, nameBase);
				if ((saveDesc[0] >= 'a') && (saveDesc[0] <= 'z'))
					saveDesc[0] += 'A' - 'a'; 
				setupIcon(icoSysDest, "../ScummVM/scummvm.icn", saveDesc, "Savegames");
			}
		} else {
			// scumm engine tries to open hundreds of files to search for savegames.
			if (mcNeedUpdate) {
				mcGetDir(0, 0, dirStr, 0, ARRAY_ENTRIES, mcDir);
				mcSync(0, NULL, &mcEntries);
				mcNeedUpdate = false;
			}
			bool fileExists = false;
			char searchName[32];
			sprintf(searchName, "%s.bin", ext);
			for (int cnt = 0; (cnt < mcEntries) && !fileExists; cnt++)
				if (strcmp(searchName, (char*)mcDir[cnt].name) == 0)
					fileExists = true;

			if (!fileExists) {
				_screen->wantAnim(false);
				return NULL;
			}
		}
		sprintf(dirStr, "mc0:ScummVM-%s/%s.bin", nameBase, ext);
		SaveFile *file = new UclSaveFile(dirStr, saveOrLoad, _screen);
		if (!file->isOpen()) {
			printf("unable to open savefile %s for %s\n", dirStr, saveOrLoad ? "saving" : "loading");
            delete file;
			_screen->wantAnim(false);
			return NULL;
		}
		return file;
	} else {
		printf("HDD not implemented yet\n");
		return NULL;
	}
}

void Ps2SaveFileManager::listSavefiles(const char * /* prefix */, bool *marks, int num) {
	memset(marks, true, num * sizeof(bool));
}

const char *Ps2SaveFileManager::getSavePath(void) const {
	return _savePath;
}

void Ps2SaveFileManager::setSavePath(const char *path) {
	strcpy(_savePath, path);
}

bool Ps2SaveFileManager::setupIcon(const char *dest, const char *ico, const char *descr1, const char *descr2) {
	mcIcon icon_sys;
	memset(&icon_sys, 0, sizeof(mcIcon));
	memcpy(icon_sys.head, "PS2D", 4);
	char title[256];
	sprintf(title, "%s\n%s", descr1, descr2);
	strcpy_sjis((short*)&(icon_sys.title), title);
	icon_sys.nlOffset = strlen(descr1) + 1;
	icon_sys.trans = 0x10;
	memcpy(icon_sys.bgCol, _bgcolor, sizeof(_bgcolor));
	memcpy(icon_sys.lightDir, _lightdir, sizeof(_lightdir));
	memcpy(icon_sys.lightCol, _lightcol, sizeof(_lightcol));
	memcpy(icon_sys.lightAmbient, _ambient, sizeof(_ambient));
	strcpy((char*)icon_sys.view, ico);
	strcpy((char*)icon_sys.copy, ico);
	strcpy((char*)icon_sys.del, ico);

	FILE *outf = fopen(dest, "wb");
	if (outf) {
		fwrite(&icon_sys, 1, sizeof(icon_sys), outf);
		fclose(outf);
		return true;
	} else
		return false;
}

uint16 *Ps2SaveFileManager::decompressIconData(uint16 *size) {
	uint16 inPos = 1;
	uint16 *rleData = (uint16*)_rleIcoData;
	uint16 resSize = rleData[0];
	uint16 *resData = (uint16*)malloc(resSize * sizeof(uint16*));
	uint16 outPos = 0;
	while (outPos < resSize) {
		uint16 len = rleData[inPos++];
		while (len--)
			resData[outPos++] = 0x7FFF;
		len = rleData[inPos++];
		while (len--)
			resData[outPos++] = rleData[inPos++];
	}
	*size = resSize;
	assert(outPos == resSize);
	return resData;
}


StdioSaveFile::StdioSaveFile(const char *filename, bool saveOrLoad)  { 
	_fh = ::fopen(filename, (saveOrLoad? "wb" : "rb")); 
	_saving = saveOrLoad;
}

StdioSaveFile::~StdioSaveFile(void)  { 
	if (_fh)
		::fclose(_fh);
}

bool StdioSaveFile::isOpen(void) const {
	return _fh != NULL;
}

uint32 StdioSaveFile::read(void *buf, uint32 cnt) {
	assert(!_saving);
	return ::fread(buf, 1, cnt, _fh);
}

uint32 StdioSaveFile::write(const void *buf, uint32 cnt) {
	assert(_saving);
	return ::fwrite(buf, 1, cnt, _fh);
}

UclSaveFile::UclSaveFile(const char *filename, bool saveOrLoad, Gs2dScreen *screen) {
	_fh = ::fopen(filename, (saveOrLoad? "wb" : "rb")); 
	_saving = saveOrLoad;
	_bufPos = 0;
	_screen = screen;
	if (_fh) {
		if (_saving) {
			_buf = (uint8*)malloc(65536);
			_bufSize = 65536;
		} else {
			uint32 srcSize = ::fsize(_fh);
			uint8 *srcBuf = (uint8*)malloc(srcSize);
			int res = ::fread(srcBuf, 1, srcSize, _fh);
			assert(res == srcSize);

			uint32 resLen = _bufSize = *(uint32*)srcBuf;
			_buf = (uint8*)malloc(_bufSize + 2048);
			res = ucl_nrv2e_decompress_8(srcBuf + 4, srcSize - 4, _buf, &resLen, NULL);
			if ((res < 0) || (resLen != _bufSize)) {
				printf("Unable to decompress file %s (%d -> %d) error code %d\n", filename, srcSize, _bufSize, res);
                free(_buf);
				_buf = NULL;
				_bufSize = 0;
			}
			::fclose(_fh);
			_fh = NULL;
			free(srcBuf);
		}
	} else {
		printf("Savefile %s doesn't exist\n", filename);
		_buf = NULL;
	}
}

UclSaveFile::~UclSaveFile(void) {
	if (_saving) {
		uint8 *compBuf = (uint8*)malloc(_bufPos * 2);
		uint32 compSize = _bufPos * 2;
		int res = ucl_nrv2e_99_compress(_buf, _bufPos, compBuf, &compSize, NULL, 10, NULL, NULL);
		if (res >= 0) {
			fwrite(&_bufPos, 1, 4, _fh);
			fwrite(compBuf, 1, compSize, _fh);
		} else {
            printf("unable to compress %d bytes of savedata, errorcode %d\n", _bufPos, res);
		}
        free(compBuf);
	}
	if (_buf)
		free(_buf);
	if (_fh)
		::fclose(_fh);
	_screen->wantAnim(false);
}

bool UclSaveFile::isOpen(void) const {
	return (_buf != NULL);
}

uint32 UclSaveFile::read(void *buf, uint32 cnt) {
	assert(!_saving);
	uint32 numBytes = (cnt > _bufSize - _bufPos) ? (_bufSize - _bufPos) : cnt;
	memcpy(buf, _buf + _bufPos, numBytes);
	_bufPos += numBytes;
	return numBytes;
}

uint32 UclSaveFile::write(const void *buf, uint32 cnt) {
	assert(_saving);
	if (_bufSize - _bufPos < cnt) {
		_bufSize += (cnt > 65536) ? cnt : 65536;
		_buf = (uint8*)realloc(_buf, _bufSize);
	}
	memcpy(_buf + _bufPos, buf, cnt);
	_bufPos += cnt;
	return cnt;
}