saveload.c
changeset 5568 75f13d7bfaed
parent 5296 6a4aaa66eed3
child 5573 afa6f92a71fd
equal deleted inserted replaced
5567:2af6b4aa2833 5568:75f13d7bfaed
   309 
   309 
   310 		switch (_sl.block_mode) {
   310 		switch (_sl.block_mode) {
   311 		case CH_SPARSE_ARRAY: index = (int)SlReadSparseIndex(); break;
   311 		case CH_SPARSE_ARRAY: index = (int)SlReadSparseIndex(); break;
   312 		case CH_ARRAY:        index = _sl.array_index++; break;
   312 		case CH_ARRAY:        index = _sl.array_index++; break;
   313 		default:
   313 		default:
   314 			DEBUG(misc, 0) ("[Sl] SlIterateArray: error");
   314 			DEBUG(sl, 0, "SlIterateArray error");
   315 			return -1; // error
   315 			return -1; // error
   316 		}
   316 		}
   317 
   317 
   318 		if (length != 0) return index;
   318 		if (length != 0) return index;
   319 	}
   319 	}
   555 		switch (GetVarMemType(conv)) {
   555 		switch (GetVarMemType(conv)) {
   556 			default: NOT_REACHED();
   556 			default: NOT_REACHED();
   557 			case SLE_VAR_STRB:
   557 			case SLE_VAR_STRB:
   558 			case SLE_VAR_STRBQ:
   558 			case SLE_VAR_STRBQ:
   559 				if (len >= length) {
   559 				if (len >= length) {
   560 					DEBUG(misc, 0) ("[Sl] String length in savegame is bigger than buffer, truncating");
   560 					DEBUG(sl, 1, "String length in savegame is bigger than buffer, truncating");
   561 					SlCopyBytes(ptr, length);
   561 					SlCopyBytes(ptr, length);
   562 					SlSkipBytes(len - length);
   562 					SlSkipBytes(len - length);
   563 					len = length - 1;
   563 					len = length - 1;
   564 				} else {
   564 				} else {
   565 					SlCopyBytes(ptr, len);
   565 					SlCopyBytes(ptr, len);
   861 static void SlSaveChunk(const ChunkHandler *ch)
   861 static void SlSaveChunk(const ChunkHandler *ch)
   862 {
   862 {
   863 	ChunkSaveLoadProc *proc = ch->save_proc;
   863 	ChunkSaveLoadProc *proc = ch->save_proc;
   864 
   864 
   865 	SlWriteUint32(ch->id);
   865 	SlWriteUint32(ch->id);
   866 	DEBUG(misc, 1) ("[Sl] Saving chunk %c%c%c%c", ch->id >> 24, ch->id >> 16, ch->id >> 8, ch->id);
   866 	DEBUG(sl, 2, "Saving chunk %c%c%c%c", ch->id >> 24, ch->id >> 16, ch->id >> 8, ch->id);
   867 
   867 
   868 	if (ch->flags & CH_AUTO_LENGTH) {
   868 	if (ch->flags & CH_AUTO_LENGTH) {
   869 		// Need to calculate the length. Solve that by calling SlAutoLength in the save_proc.
   869 		// Need to calculate the length. Solve that by calling SlAutoLength in the save_proc.
   870 		_tmp_proc_1 = proc;
   870 		_tmp_proc_1 = proc;
   871 		proc = SlStubSaveProc;
   871 		proc = SlStubSaveProc;
   939 {
   939 {
   940 	uint32 id;
   940 	uint32 id;
   941 	const ChunkHandler *ch;
   941 	const ChunkHandler *ch;
   942 
   942 
   943 	for (id = SlReadUint32(); id != 0; id = SlReadUint32()) {
   943 	for (id = SlReadUint32(); id != 0; id = SlReadUint32()) {
   944 		DEBUG(misc, 1) ("[Sl] Loading chunk %c%c%c%c", id >> 24, id >> 16, id >> 8, id);
   944 		DEBUG(sl, 2, "Loading chunk %c%c%c%c", id >> 24, id >> 16, id >> 8, id);
   945 
   945 
   946 		ch = SlFindChunkHandler(id);
   946 		ch = SlFindChunkHandler(id);
   947 		if (ch == NULL) SlError("found unknown tag in savegame (sync error)");
   947 		if (ch == NULL) SlError("found unknown tag in savegame (sync error)");
   948 		SlLoadChunk(ch);
   948 		SlLoadChunk(ch);
   949 	}
   949 	}
  1518 		return SL_OK;
  1518 		return SL_OK;
  1519 	}
  1519 	}
  1520 
  1520 
  1521 	_sl.fh = (mode == SL_SAVE) ? fopen(filename, "wb") : fopen(filename, "rb");
  1521 	_sl.fh = (mode == SL_SAVE) ? fopen(filename, "wb") : fopen(filename, "rb");
  1522 	if (_sl.fh == NULL) {
  1522 	if (_sl.fh == NULL) {
  1523 		DEBUG(misc, 0) ("[Sl] Cannot open savegame for saving/loading.");
  1523 		DEBUG(sl, 0, "Cannot open savegame '%s' for saving/loading.", filename);
  1524 		return SL_ERROR;
  1524 		return SL_ERROR;
  1525 	}
  1525 	}
  1526 
  1526 
  1527 	_sl.bufe = _sl.bufp = NULL;
  1527 	_sl.bufe = _sl.bufp = NULL;
  1528 	_sl.offs_base = 0;
  1528 	_sl.offs_base = 0;
  1554 		fmt = GetSavegameFormat("memory"); // write to memory
  1554 		fmt = GetSavegameFormat("memory"); // write to memory
  1555 
  1555 
  1556 		_sl.write_bytes = fmt->writer;
  1556 		_sl.write_bytes = fmt->writer;
  1557 		_sl.excpt_uninit = fmt->uninit_write;
  1557 		_sl.excpt_uninit = fmt->uninit_write;
  1558 		if (!fmt->init_write()) {
  1558 		if (!fmt->init_write()) {
  1559 			DEBUG(misc, 0) ("[Sl] Initializing writer %s failed.", fmt->name);
  1559 			DEBUG(sl, 0, "Initializing writer '%s' failed.", fmt->name);
  1560 			return AbortSaveLoad();
  1560 			return AbortSaveLoad();
  1561 		}
  1561 		}
  1562 
  1562 
  1563 		_sl_version = SAVEGAME_VERSION;
  1563 		_sl_version = SAVEGAME_VERSION;
  1564 
  1564 
  1567 		SlWriteFill(); // flush the save buffer
  1567 		SlWriteFill(); // flush the save buffer
  1568 
  1568 
  1569 		SaveFileStart();
  1569 		SaveFileStart();
  1570 		if (_network_server ||
  1570 		if (_network_server ||
  1571 					(save_thread = OTTDCreateThread(&SaveFileToDisk, (void*)"")) == NULL) {
  1571 					(save_thread = OTTDCreateThread(&SaveFileToDisk, (void*)"")) == NULL) {
  1572 			DEBUG(misc, 1) ("[Sl] Cannot create savegame thread, reverting to single-threaded mode...");
  1572 			DEBUG(sl, 1, "Cannot create savegame thread, reverting to single-threaded mode...");
  1573 			SaveFileToDisk(NULL);
  1573 			SaveFileToDisk(NULL);
  1574 			SaveFileDone();
  1574 			SaveFileDone();
  1575 		}
  1575 		}
  1576 
  1576 
  1577 	} else { /* LOAD game */
  1577 	} else { /* LOAD game */
  1578 		assert(mode == SL_LOAD);
  1578 		assert(mode == SL_LOAD);
  1579 
  1579 
  1580 		if (fread(hdr, sizeof(hdr), 1, _sl.fh) != 1) {
  1580 		if (fread(hdr, sizeof(hdr), 1, _sl.fh) != 1) {
  1581 			DEBUG(misc, 0) ("[Sl] Cannot read savegame header, aborting.");
  1581 			DEBUG(sl, 0, "Cannot read savegame header, aborting");
  1582 			return AbortSaveLoad();
  1582 			return AbortSaveLoad();
  1583 		}
  1583 		}
  1584 
  1584 
  1585 		// see if we have any loader for this type.
  1585 		// see if we have any loader for this type.
  1586 		for (fmt = _saveload_formats; ; fmt++) {
  1586 		for (fmt = _saveload_formats; ; fmt++) {
  1587 			/* No loader found, treat as version 0 and use LZO format */
  1587 			/* No loader found, treat as version 0 and use LZO format */
  1588 			if (fmt == endof(_saveload_formats)) {
  1588 			if (fmt == endof(_saveload_formats)) {
  1589 				DEBUG(misc, 0) ("[Sl] Unknown savegame type, trying to load it as the buggy format.");
  1589 				DEBUG(sl, 0, "Unknown savegame type, trying to load it as the buggy format");
  1590 				rewind(_sl.fh);
  1590 				rewind(_sl.fh);
  1591 				_sl_version = 0;
  1591 				_sl_version = 0;
  1592 				_sl_minor_version = 0;
  1592 				_sl_minor_version = 0;
  1593 				fmt = _saveload_formats + 1; // LZO
  1593 				fmt = _saveload_formats + 1; // LZO
  1594 				break;
  1594 				break;
  1601 				 *  in versions before that (4 cases) which can't be removed easy.
  1601 				 *  in versions before that (4 cases) which can't be removed easy.
  1602 				 *  Therefor it is loaded, but never saved (or, it saves a 0 in any scenario).
  1602 				 *  Therefor it is loaded, but never saved (or, it saves a 0 in any scenario).
  1603 				 *  So never EVER use this minor version again. -- TrueLight -- 22-11-2005 */
  1603 				 *  So never EVER use this minor version again. -- TrueLight -- 22-11-2005 */
  1604 				_sl_minor_version = (TO_BE32(hdr[1]) >> 8) & 0xFF;
  1604 				_sl_minor_version = (TO_BE32(hdr[1]) >> 8) & 0xFF;
  1605 
  1605 
  1606 				DEBUG(misc, 1)("[Sl] Loading savegame version %d", _sl_version);
  1606 				DEBUG(sl, 1, "Loading savegame version %d", _sl_version);
  1607 
  1607 
  1608 				/* Is the version higher than the current? */
  1608 				/* Is the version higher than the current? */
  1609 				if (_sl_version > SAVEGAME_VERSION) {
  1609 				if (_sl_version > SAVEGAME_VERSION) {
  1610 					DEBUG(misc, 0) ("[Sl] Savegame version invalid.");
  1610 					DEBUG(sl, 0, "Savegame version invalid");
  1611 					return AbortSaveLoad();
  1611 					return AbortSaveLoad();
  1612 				}
  1612 				}
  1613 				break;
  1613 				break;
  1614 			}
  1614 			}
  1615 		}
  1615 		}
  1622 			ShowInfoF("Loader for '%s' is not available.", fmt->name);
  1622 			ShowInfoF("Loader for '%s' is not available.", fmt->name);
  1623 			return AbortSaveLoad();
  1623 			return AbortSaveLoad();
  1624 		}
  1624 		}
  1625 
  1625 
  1626 		if (!fmt->init_read()) {
  1626 		if (!fmt->init_read()) {
  1627 			DEBUG(misc, 0) ("[Sl] Initializing loader %s failed.", fmt->name);
  1627 			DEBUG(sl, 0, "Initializing loader '%s' failed", fmt->name);
  1628 			return AbortSaveLoad();
  1628 			return AbortSaveLoad();
  1629 		}
  1629 		}
  1630 
  1630 
  1631 		/* Old maps were hardcoded to 256x256 and thus did not contain
  1631 		/* Old maps were hardcoded to 256x256 and thus did not contain
  1632 		 * any mapsize information. Pre-initialize to 256x256 to not to
  1632 		 * any mapsize information. Pre-initialize to 256x256 to not to
  1667 	FILE *f;
  1667 	FILE *f;
  1668 	int mode = SL_OLD_LOAD;
  1668 	int mode = SL_OLD_LOAD;
  1669 
  1669 
  1670 	f = fopen(file, "rb");
  1670 	f = fopen(file, "rb");
  1671 	if (fread(&hdr, sizeof(hdr), 1, f) != 1) {
  1671 	if (fread(&hdr, sizeof(hdr), 1, f) != 1) {
  1672 		DEBUG(misc, 0) ("[Sl] Savegame is obsolete or invalid format");
  1672 		DEBUG(sl, 0, "Savegame is obsolete or invalid format");
  1673 		mode = SL_LOAD; // don't try to get filename, just show name as it is written
  1673 		mode = SL_LOAD; // don't try to get filename, just show name as it is written
  1674 	} else {
  1674 	} else {
  1675 		// see if we have any loader for this type.
  1675 		// see if we have any loader for this type.
  1676 		for (fmt = _saveload_formats; fmt != endof(_saveload_formats); fmt++) {
  1676 		for (fmt = _saveload_formats; fmt != endof(_saveload_formats); fmt++) {
  1677 			if (fmt->tag == hdr) {
  1677 			if (fmt->tag == hdr) {