saveload.c
changeset 3043 b916dbc4e514
parent 2958 ac0a9673b522
child 3044 fbaef2401476
equal deleted inserted replaced
3042:227101cb98ca 3043:b916dbc4e514
   289 
   289 
   290 		switch (_sl.block_mode) {
   290 		switch (_sl.block_mode) {
   291 		case CH_SPARSE_ARRAY: index = SlReadSparseIndex(); break;
   291 		case CH_SPARSE_ARRAY: index = SlReadSparseIndex(); break;
   292 		case CH_ARRAY:        index = _sl.array_index++; break;
   292 		case CH_ARRAY:        index = _sl.array_index++; break;
   293 		default:
   293 		default:
   294 			DEBUG(misc, 0) ("SlIterateArray: error");
   294 			DEBUG(misc, 0) ("[Sl] SlIterateArray: error");
   295 			return -1; // error
   295 			return -1; // error
   296 		}
   296 		}
   297 
   297 
   298 		if (length != 0) return index;
   298 		if (length != 0) return index;
   299 	}
   299 	}
   718 static void SlSaveChunk(const ChunkHandler *ch)
   718 static void SlSaveChunk(const ChunkHandler *ch)
   719 {
   719 {
   720 	ChunkSaveLoadProc *proc = ch->save_proc;
   720 	ChunkSaveLoadProc *proc = ch->save_proc;
   721 
   721 
   722 	SlWriteUint32(ch->id);
   722 	SlWriteUint32(ch->id);
       
   723 	DEBUG(misc, 1) ("[Sl] Saving chunk %c%c%c%c", ch->id >> 24, ch->id >> 16, ch->id >> 8, ch->id);
   723 
   724 
   724 	if (ch->flags & CH_AUTO_LENGTH) {
   725 	if (ch->flags & CH_AUTO_LENGTH) {
   725 		// Need to calculate the length. Solve that by calling SlAutoLength in the save_proc.
   726 		// Need to calculate the length. Solve that by calling SlAutoLength in the save_proc.
   726 		_tmp_proc_1 = proc;
   727 		_tmp_proc_1 = proc;
   727 		proc = SlStubSaveProc;
   728 		proc = SlStubSaveProc;
   795 {
   796 {
   796 	uint32 id;
   797 	uint32 id;
   797 	const ChunkHandler *ch;
   798 	const ChunkHandler *ch;
   798 
   799 
   799 	for (id = SlReadUint32(); id != 0; id = SlReadUint32()) {
   800 	for (id = SlReadUint32(); id != 0; id = SlReadUint32()) {
   800 		DEBUG(misc, 1) ("Loading chunk %c%c%c%c", id >> 24, id>>16, id>>8, id);
   801 		DEBUG(misc, 1) ("[Sl] Loading chunk %c%c%c%c", id >> 24, id >> 16, id >> 8, id);
   801 
   802 
   802 		ch = SlFindChunkHandler(id);
   803 		ch = SlFindChunkHandler(id);
   803 		if (ch == NULL) SlError("found unknown tag in savegame (sync error)");
   804 		if (ch == NULL) SlError("found unknown tag in savegame (sync error)");
   804 		SlLoadChunk(ch);
   805 		SlLoadChunk(ch);
   805 	}
   806 	}
  1379 		return SL_OK;
  1380 		return SL_OK;
  1380 	}
  1381 	}
  1381 
  1382 
  1382 	_sl.fh = fopen(filename, (mode == SL_SAVE) ? "wb" : "rb");
  1383 	_sl.fh = fopen(filename, (mode == SL_SAVE) ? "wb" : "rb");
  1383 	if (_sl.fh == NULL) {
  1384 	if (_sl.fh == NULL) {
  1384 		DEBUG(misc, 0) ("Cannot open savegame for saving/loading.");
  1385 		DEBUG(misc, 0) ("[Sl] Cannot open savegame for saving/loading.");
  1385 		return SL_ERROR;
  1386 		return SL_ERROR;
  1386 	}
  1387 	}
  1387 
  1388 
  1388 	_sl.bufe = _sl.bufp = NULL;
  1389 	_sl.bufe = _sl.bufp = NULL;
  1389 	_sl.offs_base = 0;
  1390 	_sl.offs_base = 0;
  1421 		fmt = GetSavegameFormat("memory"); // write to memory
  1422 		fmt = GetSavegameFormat("memory"); // write to memory
  1422 
  1423 
  1423 		_sl.write_bytes = fmt->writer;
  1424 		_sl.write_bytes = fmt->writer;
  1424 		_sl.excpt_uninit = fmt->uninit_write;
  1425 		_sl.excpt_uninit = fmt->uninit_write;
  1425 		if (!fmt->init_write()) {
  1426 		if (!fmt->init_write()) {
  1426 			DEBUG(misc, 0) ("Initializing writer %s failed.", fmt->name);
  1427 			DEBUG(misc, 0) ("[Sl] Initializing writer %s failed.", fmt->name);
  1427 			return AbortSaveLoad();
  1428 			return AbortSaveLoad();
  1428 		}
  1429 		}
  1429 
  1430 
  1430 		_sl_version = SAVEGAME_VERSION;
  1431 		_sl_version = SAVEGAME_VERSION;
  1431 
  1432 
  1433 		SlSaveChunks();
  1434 		SlSaveChunks();
  1434 		SlWriteFill(); // flush the save buffer
  1435 		SlWriteFill(); // flush the save buffer
  1435 
  1436 
  1436 		if (_network_server ||
  1437 		if (_network_server ||
  1437 					(save_thread = OTTDCreateThread(&SaveFileToDisk, (void*)"")) == NULL) {
  1438 					(save_thread = OTTDCreateThread(&SaveFileToDisk, (void*)"")) == NULL) {
  1438 			DEBUG(misc, 1) ("cannot create savegame thread, reverting to single-threaded mode...");
  1439 			DEBUG(misc, 1) ("[Sl] Cannot create savegame thread, reverting to single-threaded mode...");
  1439 			SaveFileToDisk(NULL);
  1440 			SaveFileToDisk(NULL);
  1440 		}
  1441 		}
  1441 
  1442 
  1442 	} else { /* LOAD game */
  1443 	} else { /* LOAD game */
  1443 		assert(mode == SL_LOAD);
  1444 		assert(mode == SL_LOAD);
  1444 
  1445 
  1445 		if (fread(hdr, sizeof(hdr), 1, _sl.fh) != 1) {
  1446 		if (fread(hdr, sizeof(hdr), 1, _sl.fh) != 1) {
  1446 			DEBUG(misc, 0) ("Cannot read savegame header, aborting.");
  1447 			DEBUG(misc, 0) ("[Sl] Cannot read savegame header, aborting.");
  1447 			return AbortSaveLoad();
  1448 			return AbortSaveLoad();
  1448 		}
  1449 		}
  1449 
  1450 
  1450 		// see if we have any loader for this type.
  1451 		// see if we have any loader for this type.
  1451 		for (fmt = _saveload_formats; ; fmt++) {
  1452 		for (fmt = _saveload_formats; ; fmt++) {
  1452 			/* No loader found, treat as version 0 and use LZO format */
  1453 			/* No loader found, treat as version 0 and use LZO format */
  1453 			if (fmt == endof(_saveload_formats)) {
  1454 			if (fmt == endof(_saveload_formats)) {
  1454 				DEBUG(misc, 0) ("Unknown savegame type, trying to load it as the buggy format.");
  1455 				DEBUG(misc, 0) ("[Sl] Unknown savegame type, trying to load it as the buggy format.");
  1455 				rewind(_sl.fh);
  1456 				rewind(_sl.fh);
  1456 				_sl_version = version = 0;
  1457 				_sl_version = version = 0;
  1457 				_sl_minor_version = 0;
  1458 				_sl_minor_version = 0;
  1458 				fmt = _saveload_formats + 1; // LZO
  1459 				fmt = _saveload_formats + 1; // LZO
  1459 				break;
  1460 				break;
  1466 				 *  in versions before that (4 cases) which can't be removed easy.
  1467 				 *  in versions before that (4 cases) which can't be removed easy.
  1467 				 *  Therefor it is loaded, but never saved (or, it saves a 0 in any scenario).
  1468 				 *  Therefor it is loaded, but never saved (or, it saves a 0 in any scenario).
  1468 				 *  So never EVER use this minor version again. -- TrueLight -- 22-11-2005 */
  1469 				 *  So never EVER use this minor version again. -- TrueLight -- 22-11-2005 */
  1469 				_sl_minor_version = (TO_BE32(hdr[1]) >> 8) & 0xFF;
  1470 				_sl_minor_version = (TO_BE32(hdr[1]) >> 8) & 0xFF;
  1470 
  1471 
  1471 				DEBUG(misc, 1)("[Savegame] Loading savegame version %d\n", _sl_version);
  1472 				DEBUG(misc, 1)("[Sl] Loading savegame version %d", _sl_version);
  1472 
  1473 
  1473 				/* Is the version higher than the current? */
  1474 				/* Is the version higher than the current? */
  1474 				if (_sl_version > SAVEGAME_VERSION) {
  1475 				if (_sl_version > SAVEGAME_VERSION) {
  1475 					DEBUG(misc, 0) ("Savegame version invalid.");
  1476 					DEBUG(misc, 0) ("[Sl] Savegame version invalid.");
  1476 					return AbortSaveLoad();
  1477 					return AbortSaveLoad();
  1477 				}
  1478 				}
  1478 				break;
  1479 				break;
  1479 			}
  1480 			}
  1480 		}
  1481 		}
  1487 			ShowInfoF("Loader for '%s' is not available.", fmt->name);
  1488 			ShowInfoF("Loader for '%s' is not available.", fmt->name);
  1488 			return AbortSaveLoad();
  1489 			return AbortSaveLoad();
  1489 		}
  1490 		}
  1490 
  1491 
  1491 		if (!fmt->init_read()) {
  1492 		if (!fmt->init_read()) {
  1492 			DEBUG(misc, 0) ("Initializing loader %s failed.", fmt->name);
  1493 			DEBUG(misc, 0) ("[Sl] Initializing loader %s failed.", fmt->name);
  1493 			return AbortSaveLoad();
  1494 			return AbortSaveLoad();
  1494 		}
  1495 		}
  1495 
  1496 
  1496 		/* Old maps were hardcoded to 256x256 and thus did not contain
  1497 		/* Old maps were hardcoded to 256x256 and thus did not contain
  1497 		 * any mapsize information. Pre-initialize to 256x256 to not to
  1498 		 * any mapsize information. Pre-initialize to 256x256 to not to