aboutsummaryrefslogtreecommitdiff
path: root/backends/platform/dc/vmsave.cpp
blob: 05b459f40fa86ba9a9291405896f040a44b7356b (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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/* 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.
 *
 * $URL$
 * $Id$
 *
 */

#include <common/scummsys.h>
#include "engines/engine.h"
#include "dc.h"
#include "icon.h"
#include <common/savefile.h>
#include <gui/GuiManager.h>
#include <gui/message.h>
#include <backends/saves/compressed/compressed-saves.h>


// Savegame can not be bigger than this
#define MAX_SAVE_SIZE (128*1024)


enum vmsaveResult {
  VMSAVE_OK,
  VMSAVE_NOVM,
  VMSAVE_NOSPACE,
  VMSAVE_WRITEERROR,
};


static int lastvm=-1;

static void displaySaveResult(vmsaveResult res)
{
  char buf[1024];

  switch(res) {
  case VMSAVE_OK:
    sprintf(buf, "Game saved on unit %c%d", 'A'+(lastvm/6), lastvm%6);
    break;
  case VMSAVE_NOVM:
    strcpy(buf, "No memory card present!");
    break;
  case VMSAVE_NOSPACE:
    strcpy(buf, "Not enough space available!");
    break;
  case VMSAVE_WRITEERROR:
    strcpy(buf, "Write error!!!");
    break;
  default:
    strcpy(buf, "Unknown error!!!");
    break;
  }

  GUI::MessageDialog dialog(buf);
  dialog.runModal();
}

static vmsaveResult trySave(const char *gamename, const char *data, int size,
			    const char *filename, class Icon &icon, int vm)
{
  struct vmsinfo info;
  struct superblock super;
  struct vms_file file;
  struct vms_file_header header;
  struct timestamp tstamp;
  struct tm tm;
  time_t t;
  unsigned char iconbuffer[512+32];

  if (!vmsfs_check_unit(vm, 0, &info))
    return VMSAVE_NOVM;
  if (!vmsfs_get_superblock(&info, &super))
    return VMSAVE_NOVM;
  int free_cnt = vmsfs_count_free(&super);
  if (vmsfs_open_file(&super, filename, &file))
    free_cnt += file.blks;
  if (((128+512+size+511)>>9) > free_cnt)
    return VMSAVE_NOSPACE;

  memset(&header, 0, sizeof(header));
  strncpy(header.shortdesc, "ScummVM savegame", 16);
  strncpy(header.longdesc, gamename, 32);
  strncpy(header.id, "ScummVM", 16);
  icon.create_vmicon(iconbuffer);
  header.numicons = 1;
  memcpy(header.palette, iconbuffer, sizeof(header.palette));
  time(&t);
  tm = *localtime(&t);
  tstamp.year = tm.tm_year+1900;
  tstamp.month = tm.tm_mon+1;
  tstamp.day = tm.tm_mday;
  tstamp.hour = tm.tm_hour;
  tstamp.minute = tm.tm_min;
  tstamp.second = tm.tm_sec;
  tstamp.wkday = (tm.tm_wday+6)%7;

  vmsfs_beep(&info, 1);

  vmsfs_errno = 0;
  if (!vmsfs_create_file(&super, filename, &header,
			iconbuffer+sizeof(header.palette), NULL,
			data, size, &tstamp)) {
    fprintf(stderr, "%s\n", vmsfs_describe_error());
    vmsfs_beep(&info, 0);
    return VMSAVE_WRITEERROR;
  }

  vmsfs_beep(&info, 0);
  return VMSAVE_OK;
}

static bool tryLoad(char *&buffer, int &size, const char *filename, int vm)
{
  struct vmsinfo info;
  struct superblock super;
  struct vms_file file;
  struct vms_file_header header;
  struct timestamp tstamp;
  struct tm tm;
  time_t t;

  if (!vmsfs_check_unit(vm, 0, &info))
    return false;
  if (!vmsfs_get_superblock(&info, &super))
    return false;
  if (!vmsfs_open_file(&super, filename, &file))
    return false;

  buffer = new char[size = file.size];

  if (vmsfs_read_file(&file, (unsigned char *)buffer, size))
    return true;

  delete[] buffer;
  buffer = NULL;
  return false;
}

static bool tryDelete(const char *filename, int vm)
{
  struct vmsinfo info;
  struct superblock super;

  if (!vmsfs_check_unit(vm, 0, &info))
    return false;
  if (!vmsfs_get_superblock(&info, &super))
    return false;

  if (!vmsfs_delete_file(&super, filename))
    return false;

  return true;
}

static bool matches(const char *glob, const char *name)
{
  while(*glob)
    if(*glob == '*') {
      while(*glob == '*')
	glob++;
      do {
	if((*name == *glob || *glob == '?') &&
	   matches(glob, name))
	  return true;
      } while(*name++);
      return false;
    } else if(!*name)
      return false;
    else if(*glob == '?' || *glob == *name) {
      glob++;
      name++;
    }
    else
      return false;
  return !*name;
}

static void tryList(const char *glob, int vm, Common::StringList &list)
{
  struct vmsinfo info;
  struct superblock super;
  struct dir_iterator iter;
  struct dir_entry de;

  if (!vmsfs_check_unit(vm, 0, &info))
    return;
  if (!vmsfs_get_superblock(&info, &super))
    return;
  vmsfs_open_dir(&super, &iter);
  while (vmsfs_next_dir_entry(&iter, &de))
    if (de.entry[0]) {
      char buf[16];
      strncpy(buf, (char *)de.entry+4, 12);
      buf[12] = 0;
      if (matches(glob, buf))
	list.push_back(buf);
    }
}

vmsaveResult writeSaveGame(const char *gamename, const char *data, int size,
			   const char *filename, class Icon &icon)
{
  vmsaveResult r, res = VMSAVE_NOVM;

  if (lastvm >= 0 &&
     (res = trySave(gamename, data, size, filename, icon, lastvm)) == VMSAVE_OK)
    return res;

  for (int i=0; i<24; i++)
    if ((r = trySave(gamename, data, size, filename, icon, i)) == VMSAVE_OK) {
      lastvm = i;
      return r;
    } else if (r > res)
      res = r;

  return res;
}

bool readSaveGame(char *&buffer, int &size, const char *filename)
{
  if (lastvm >= 0 &&
     tryLoad(buffer, size, filename, lastvm))
    return true;

  for (int i=0; i<24; i++)
    if (tryLoad(buffer, size, filename, i)) {
      lastvm = i;
      return true;
    }

  return false;
}

bool deleteSaveGame(const char *filename)
{
  if (lastvm >= 0 &&
     tryDelete(filename, lastvm))
    return true;

  for (int i=0; i<24; i++)
    if (tryDelete(filename, i)) {
      lastvm = i;
      return true;
    }

  return false;
}


class InVMSave : public Common::InSaveFile {
private:
  char *buffer;
  int _pos, _size;
  bool _eos;

  uint32 read(void *buf, uint32 cnt);
  bool skip(uint32 offset);
  bool seek(int32 offs, int whence);

public:
  InVMSave()
    : _pos(0), buffer(NULL), _eos(false)
  { }

  ~InVMSave()
  {
    if (buffer != NULL)
      delete[] buffer;
  }

  bool eos() const { return _eos; }
  void clearErr() { _eos = false; }
  int32 pos() const { return _pos; }
  int32 size() const { return _size; }

  bool readSaveGame(const char *filename)
  { return ::readSaveGame(buffer, _size, filename); }
};

class OutVMSave : public Common::OutSaveFile {
private:
  char *buffer;
  int pos, size, committed;
  char filename[16];
  bool iofailed;

public:
  uint32 write(const void *buf, uint32 cnt);

  OutVMSave(const char *_filename)
    : pos(0), committed(-1), iofailed(false)
  {
    strncpy(filename, _filename, 16);
    buffer = new char[size = MAX_SAVE_SIZE];
  }

  ~OutVMSave();

  bool err() const { return iofailed; }
  void clearErr() { iofailed = false; }
  void finalize();
};

class VMSaveManager : public Common::SaveFileManager {
public:

  virtual Common::OutSaveFile *openForSaving(const char *filename) {
	return wrapOutSaveFile(new OutVMSave(filename));
  }

  virtual Common::InSaveFile *openForLoading(const char *filename) {
	InVMSave *s = new InVMSave();
	if (s->readSaveGame(filename)) {
	  return wrapInSaveFile(s);
	} else {
	  delete s;
	  return NULL;
	}
  }

  virtual bool removeSavefile(const char *filename) {
	return ::deleteSaveGame(filename);
  }

  virtual Common::StringList VMSaveManager::listSavefiles(const char *glob);
};

void OutVMSave::finalize()
{
  extern const char *gGameName;
  extern Icon icon;

  if (committed >= pos)
    return;

  char *data = buffer;
  int len = pos;

  vmsaveResult r = writeSaveGame(gGameName, data, len, filename, icon);
  committed = pos;
  if (r != VMSAVE_OK)
    iofailed = true;
  displaySaveResult(r);
}

OutVMSave::~OutVMSave()
{
  finalize();
  delete[] buffer;
}

uint32 InVMSave::read(void *buf, uint32 cnt)
{
  int nbyt = cnt;
  if (_pos + nbyt > _size) {
    cnt = (_size - _pos);
    _eos = true;
    nbyt = cnt;
  }
  if (nbyt)
    memcpy(buf, buffer + _pos, nbyt);
  _pos += nbyt;
  return cnt;
}

bool InVMSave::skip(uint32 offset)
{
  int nbyt = offset;
  if (_pos + nbyt > _size)
    nbyt = (_size - _pos);
  _pos += nbyt;
  return true;
}

bool InVMSave::seek(int32 offs, int whence)
{
  switch(whence) {
  case SEEK_SET:
    _pos = offs;
    break;
  case SEEK_CUR:
    _pos += offs;
    break;
  case SEEK_END:
    _pos = _size + offs;
    break;
  }
  if (_pos < 0)
    _pos = 0;
  else if (_pos > _size)
    _pos = _size;
  _eos = false;
  return true;
}

uint32 OutVMSave::write(const void *buf, uint32 cnt)
{
  int nbyt = cnt;
  if (pos + nbyt > size) {
    cnt = (size - pos);
    nbyt = cnt;
  }
  if (nbyt)
    memcpy(buffer + pos, buf, nbyt);
  pos += nbyt;
  return cnt;
}


Common::StringList VMSaveManager::listSavefiles(const char *glob)
{
  Common::StringList list;

  for (int i=0; i<24; i++)
    tryList(glob, i, list);

  return list;
}

Common::SaveFileManager *OSystem_Dreamcast::createSavefileManager()
{
  return new VMSaveManager();
}