| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
#include "save.h" |
| 10 |
|
| 11 |
#include "current.h" |
| 12 |
#include "lists.h" |
| 13 |
#include "readline.h" |
| 14 |
#include "options.h" |
| 15 |
#include "syserr.h" |
| 16 |
#include "instance.h" |
| 17 |
#include "memory.h" |
| 18 |
#include "output.h" |
| 19 |
#include "args.h" |
| 20 |
#include "score.h" |
| 21 |
#include "event.h" |
| 22 |
#include "msg.h" |
| 23 |
|
| 24 |
#ifndef HAVE_GLK |
| 25 |
static char saveFileName[256]; |
| 26 |
typedef FILE *AFILE; |
| 27 |
#else |
| 28 |
#include "glk.h" |
| 29 |
#define MAP_STDIO_TO_GLK |
| 30 |
#include "glkio.h" |
| 31 |
typedef strid_t AFILE; |
| 32 |
#endif |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
static void saveStrings(AFILE saveFile) { |
| 37 |
|
| 38 |
StringInitEntry *initEntry; |
| 39 |
|
| 40 |
if (header->stringInitTable != 0) |
| 41 |
for (initEntry = (StringInitEntry *)pointerTo(header->stringInitTable); |
| 42 |
!isEndOfArray(initEntry); initEntry++) { |
| 43 |
char *attr = (char *)getInstanceStringAttribute(initEntry->instanceCode, initEntry->attributeCode); |
| 44 |
Aint length = strlen(attr) + 1; |
| 45 |
fwrite((void *)&length, sizeof(length), 1, saveFile); |
| 46 |
fwrite((void *)attr, 1, length, saveFile); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
static void saveSets(AFILE saveFile) { |
| 53 |
SetInitEntry *initEntry; |
| 54 |
|
| 55 |
if (header->setInitTable != 0) |
| 56 |
for (initEntry = (SetInitEntry *)pointerTo(header->setInitTable); |
| 57 |
!isEndOfArray(initEntry); initEntry++) { |
| 58 |
Set *attr = (Set *)getInstanceSetAttribute(initEntry->instanceCode, initEntry->attributeCode); |
| 59 |
fwrite((void *)&attr->size, sizeof(attr->size), 1, saveFile); |
| 60 |
fwrite((void *)attr->members, sizeof(attr->members[0]), attr->size, saveFile); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
static void saveGameInfo(AFILE saveFile) { |
| 67 |
fwrite((void *)"ASAV", 1, 4, saveFile); |
| 68 |
fwrite((void *)&header->version, 1, sizeof(Aword), saveFile); |
| 69 |
fwrite((void *)adventureName, 1, strlen(adventureName)+1, saveFile); |
| 70 |
fwrite((void *)&header->uid, 1, sizeof(Aword), saveFile); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
static void saveAdmin(AFILE saveFile) { |
| 76 |
fwrite((void *)&admin[1], sizeof(AdminEntry), header->instanceMax, saveFile); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
static void saveAttributeArea(AFILE saveFile) { |
| 82 |
fwrite((void*)attributes, header->attributesAreaSize, sizeof(Aword), saveFile); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
static void saveEventQueue(AFILE saveFile) { |
| 88 |
fwrite((void *)&eventQueueTop, sizeof(eventQueueTop), 1, saveFile); |
| 89 |
fwrite((void *)&eventQueue[0], sizeof(eventQueue[0]), eventQueueTop, saveFile); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
static void saveCurrentValues(AFILE saveFile) { |
| 95 |
fwrite((void *)¤t, sizeof(current), 1, saveFile); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
protected void saveScores(AFILE saveFile) { |
| 101 |
fwrite((void *)scores, sizeof(Aword), header->scoreCount, saveFile); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
protected void saveGame(AFILE saveFile) { |
| 107 |
|
| 108 |
saveGameInfo(saveFile); |
| 109 |
|
| 110 |
|
| 111 |
saveCurrentValues(saveFile); |
| 112 |
|
| 113 |
saveAttributeArea(saveFile); |
| 114 |
saveAdmin(saveFile); |
| 115 |
|
| 116 |
saveEventQueue(saveFile); |
| 117 |
|
| 118 |
saveScores(saveFile); |
| 119 |
|
| 120 |
saveStrings(saveFile); |
| 121 |
saveSets(saveFile); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
void save(void) |
| 127 |
{ |
| 128 |
#ifdef HAVE_GLK |
| 129 |
frefid_t saveFileRef; |
| 130 |
strid_t saveFile; |
| 131 |
saveFileRef = glk_fileref_create_by_prompt(fileusage_SavedGame, filemode_Write, 0); |
| 132 |
if (saveFileRef == NULL) |
| 133 |
error(M_SAVEFAILED); |
| 134 |
saveFile = glk_stream_open_file(saveFileRef, filemode_Write, 0); |
| 135 |
|
| 136 |
#else |
| 137 |
FILE *saveFile; |
| 138 |
char str[1000]; |
| 139 |
|
| 140 |
current.location = where(HERO, DIRECT); |
| 141 |
|
| 142 |
if (saveFileName[0] == '\0') { |
| 143 |
strcpy(saveFileName, adventureName); |
| 144 |
strcat(saveFileName, ".sav"); |
| 145 |
} |
| 146 |
printMessage(M_SAVEWHERE); |
| 147 |
sprintf(str, "(%s) : ", saveFileName); |
| 148 |
output(str); |
| 149 |
#ifdef USE_READLINE |
| 150 |
readline(str); |
| 151 |
#else |
| 152 |
gets(str); |
| 153 |
#endif |
| 154 |
if (str[0] == '\0') |
| 155 |
strcpy(str, saveFileName); |
| 156 |
col = 1; |
| 157 |
if ((saveFile = fopen(str, READ_MODE)) != NULL) |
| 158 |
|
| 159 |
if (!regressionTestOption) { |
| 160 |
|
| 161 |
if (!confirm(M_SAVEOVERWRITE)) |
| 162 |
abortPlayerCommand(); |
| 163 |
} |
| 164 |
strcpy(saveFileName, str); |
| 165 |
if ((saveFile = fopen(saveFileName, WRITE_MODE)) == NULL) |
| 166 |
error(M_SAVEFAILED); |
| 167 |
#endif |
| 168 |
|
| 169 |
saveGame(saveFile); |
| 170 |
|
| 171 |
fclose(saveFile); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
static void restoreStrings(AFILE saveFile) { |
| 177 |
StringInitEntry *initEntry; |
| 178 |
|
| 179 |
if (header->stringInitTable != 0) |
| 180 |
for (initEntry = (StringInitEntry *)pointerTo(header->stringInitTable); |
| 181 |
!isEndOfArray(initEntry); initEntry++) { |
| 182 |
int rc; |
| 183 |
(void)rc; |
| 184 |
Aint length; |
| 185 |
char *string; |
| 186 |
rc = fread((void *)&length, sizeof(Aint), 1, saveFile); |
| 187 |
string = allocate(length+1); |
| 188 |
rc = fread((void *)string, 1, length, saveFile); |
| 189 |
setInstanceAttribute(initEntry->instanceCode, initEntry->attributeCode, toAptr(string)); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
static void restoreSets(AFILE saveFile) { |
| 196 |
SetInitEntry *initEntry; |
| 197 |
|
| 198 |
if (header->setInitTable != 0) |
| 199 |
for (initEntry = (SetInitEntry *)pointerTo(header->setInitTable); |
| 200 |
!isEndOfArray(initEntry); initEntry++) { |
| 201 |
Aint setSize; |
| 202 |
Set *set; |
| 203 |
int rc; |
| 204 |
(void)rc; |
| 205 |
|
| 206 |
rc = fread((void *)&setSize, sizeof(setSize), 1, saveFile); |
| 207 |
set = newSet(setSize); |
| 208 |
for (int i = 0; i < setSize; i++) { |
| 209 |
Aword member; |
| 210 |
rc = fread((void *)&member, sizeof(member), 1, saveFile); |
| 211 |
addToSet(set, member); |
| 212 |
} |
| 213 |
setInstanceAttribute(initEntry->instanceCode, initEntry->attributeCode, toAptr(set)); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
protected void restoreScores(AFILE saveFile) { |
| 220 |
int rc; |
| 221 |
(void)rc; |
| 222 |
rc = fread((void *)scores, sizeof(Aword), header->scoreCount, saveFile); |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
static void restoreEventQueue(AFILE saveFile) { |
| 228 |
int rc; |
| 229 |
(void)rc; |
| 230 |
|
| 231 |
rc = fread((void *)&eventQueueTop, sizeof(eventQueueTop), 1, saveFile); |
| 232 |
if (eventQueueTop > eventQueueSize) { |
| 233 |
deallocate(eventQueue); |
| 234 |
eventQueue = allocate(eventQueueTop*sizeof(eventQueue[0])); |
| 235 |
} |
| 236 |
rc = fread((void *)&eventQueue[0], sizeof(eventQueue[0]), eventQueueTop, saveFile); |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
static void restoreAdmin(AFILE saveFile) { |
| 242 |
|
| 243 |
int rc; |
| 244 |
(void)rc; |
| 245 |
|
| 246 |
for (int i = 1; i <= header->instanceMax; i++) { |
| 247 |
AttributeEntry *currentAttributesArea = admin[i].attributes; |
| 248 |
rc = fread((void *)&admin[i], sizeof(AdminEntry), 1, saveFile); |
| 249 |
admin[i].attributes = currentAttributesArea; |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
static void restoreAttributeArea(AFILE saveFile) { |
| 256 |
int rc; |
| 257 |
(void)rc; |
| 258 |
|
| 259 |
rc = fread((void *)attributes, header->attributesAreaSize, sizeof(Aword), saveFile); |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
static void restoreCurrentValues(AFILE saveFile) { |
| 265 |
int rc; |
| 266 |
(void)rc; |
| 267 |
|
| 268 |
rc = fread((void *)¤t, sizeof(current), 1, saveFile); |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
static void verifyGameId(AFILE saveFile) { |
| 274 |
Aword savedUid; |
| 275 |
int rc; |
| 276 |
(void)rc; |
| 277 |
|
| 278 |
rc = fread((void *)&savedUid, sizeof(Aword), 1, saveFile); |
| 279 |
if (!ignoreErrorOption && savedUid != header->uid) |
| 280 |
error(M_SAVEVERS); |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
static void verifyGameName(AFILE saveFile) { |
| 286 |
char savedName[256]; |
| 287 |
int i = 0; |
| 288 |
|
| 289 |
while ((savedName[i++] = fgetc(saveFile)) != '\0'); |
| 290 |
if (strcmp(savedName, adventureName) != 0) |
| 291 |
error(M_SAVENAME); |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
static void verifyCompilerVersion(AFILE saveFile) { |
| 297 |
char savedVersion[4]; |
| 298 |
int rc; |
| 299 |
(void)rc; |
| 300 |
|
| 301 |
rc = fread((void *)&savedVersion, sizeof(Aword), 1, saveFile); |
| 302 |
if (!ignoreErrorOption && strncmp(savedVersion, header->version, 4)) |
| 303 |
error(M_SAVEVERS); |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
static void verifySaveFile(AFILE saveFile) { |
| 309 |
char string[256]; |
| 310 |
int rc; |
| 311 |
(void)rc; |
| 312 |
|
| 313 |
rc = fread((void *)&string, 1, 4, saveFile); |
| 314 |
string[4] = '\0'; |
| 315 |
if (strcmp(string, "ASAV") != 0) |
| 316 |
error(M_NOTASAVEFILE); |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
protected void restoreGame(AFILE saveFile) |
| 322 |
{ |
| 323 |
if (saveFile == NULL) syserr("'restoreGame()' from a null fileref"); |
| 324 |
|
| 325 |
verifySaveFile(saveFile); |
| 326 |
|
| 327 |
|
| 328 |
verifyCompilerVersion(saveFile); |
| 329 |
|
| 330 |
|
| 331 |
verifyGameName(saveFile); |
| 332 |
|
| 333 |
|
| 334 |
verifyGameId(saveFile); |
| 335 |
|
| 336 |
restoreCurrentValues(saveFile); |
| 337 |
restoreAttributeArea(saveFile); |
| 338 |
restoreAdmin(saveFile); |
| 339 |
restoreEventQueue(saveFile); |
| 340 |
restoreScores(saveFile); |
| 341 |
restoreStrings(saveFile); |
| 342 |
restoreSets(saveFile); |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
void restore(void) |
| 348 |
{ |
| 349 |
#ifdef HAVE_GLK |
| 350 |
frefid_t saveFileRef; |
| 351 |
strid_t saveFile; |
| 352 |
saveFileRef = glk_fileref_create_by_prompt(fileusage_SavedGame, filemode_Read, 0); |
| 353 |
if (saveFileRef == NULL) return; |
| 354 |
saveFile = glk_stream_open_file(saveFileRef, filemode_Read, 0); |
| 355 |
if (saveFile == NULL) return; |
| 356 |
|
| 357 |
#else |
| 358 |
char str[1000]; |
| 359 |
FILE *saveFile; |
| 360 |
|
| 361 |
current.location = where(HERO, DIRECT); |
| 362 |
|
| 363 |
if (saveFileName[0] == '\0') { |
| 364 |
strcpy(saveFileName, adventureName); |
| 365 |
strcat(saveFileName, ".sav"); |
| 366 |
} |
| 367 |
printMessage(M_RESTOREFROM); |
| 368 |
sprintf(str, "(%s) : ", saveFileName); |
| 369 |
output(str); |
| 370 |
#ifdef USE_READLINE |
| 371 |
readline(str); |
| 372 |
#else |
| 373 |
gets(str); |
| 374 |
#endif |
| 375 |
|
| 376 |
col = 1; |
| 377 |
if (str[0] == '\0') { |
| 378 |
strcpy(str, saveFileName); |
| 379 |
} |
| 380 |
if ((saveFile = fopen(str, READ_MODE)) == NULL) |
| 381 |
error(M_SAVEMISSING); |
| 382 |
strcpy(saveFileName, str); |
| 383 |
|
| 384 |
#endif |
| 385 |
|
| 386 |
restoreGame(saveFile); |
| 387 |
|
| 388 |
fclose(saveFile); |
| 389 |
} |