src/saveload.cpp
branchcustombridgeheads
changeset 5650 aefc131bf5ce
parent 5649 55c8267c933f
child 5919 2b58160d667d
equal deleted inserted replaced
5649:55c8267c933f 5650:aefc131bf5ce
    28 #include "saveload.h"
    28 #include "saveload.h"
    29 #include "network/network.h"
    29 #include "network/network.h"
    30 #include "variables.h"
    30 #include "variables.h"
    31 #include <setjmp.h>
    31 #include <setjmp.h>
    32 
    32 
    33 const uint16 SAVEGAME_VERSION = 43;
    33 extern const uint16 SAVEGAME_VERSION = 43;
    34 uint16 _sl_version;       /// the major savegame version identifier
    34 uint16 _sl_version;       /// the major savegame version identifier
    35 byte   _sl_minor_version; /// the minor savegame version, DO NOT USE!
    35 byte   _sl_minor_version; /// the minor savegame version, DO NOT USE!
    36 
    36 
    37 typedef void WriterProc(uint len);
    37 typedef void WriterProc(uint len);
    38 typedef uint ReaderProc(void);
    38 typedef uint ReaderProc(void);
   509 	switch (GetVarMemType(conv)) {
   509 	switch (GetVarMemType(conv)) {
   510 		default: NOT_REACHED();
   510 		default: NOT_REACHED();
   511 		case SLE_VAR_STR:
   511 		case SLE_VAR_STR:
   512 		case SLE_VAR_STRQ:
   512 		case SLE_VAR_STRQ:
   513 			str = *(const char**)ptr;
   513 			str = *(const char**)ptr;
   514 			len = -1;
   514 			len = SIZE_MAX;
   515 			break;
   515 			break;
   516 		case SLE_VAR_STRB:
   516 		case SLE_VAR_STRB:
   517 		case SLE_VAR_STRBQ:
   517 		case SLE_VAR_STRBQ:
   518 			str = (const char*)ptr;
   518 			str = (const char*)ptr;
   519 			len = length;
   519 			len = length;
   536 	if (_sl.save) { /* SAVE string */
   536 	if (_sl.save) { /* SAVE string */
   537 		switch (GetVarMemType(conv)) {
   537 		switch (GetVarMemType(conv)) {
   538 			default: NOT_REACHED();
   538 			default: NOT_REACHED();
   539 			case SLE_VAR_STRB:
   539 			case SLE_VAR_STRB:
   540 			case SLE_VAR_STRBQ:
   540 			case SLE_VAR_STRBQ:
   541 				len = SlCalcNetStringLen(ptr, length);
   541 				len = SlCalcNetStringLen((char*)ptr, length);
   542 				break;
   542 				break;
   543 			case SLE_VAR_STR:
   543 			case SLE_VAR_STR:
   544 			case SLE_VAR_STRQ:
   544 			case SLE_VAR_STRQ:
   545 				ptr = *(char**)ptr;
   545 				ptr = *(char**)ptr;
   546 				len = SlCalcNetStringLen(ptr, -1);
   546 				len = SlCalcNetStringLen((char*)ptr, SIZE_MAX);
   547 				break;
   547 				break;
   548 		}
   548 		}
   549 
   549 
   550 		SlWriteArrayLength(len);
   550 		SlWriteArrayLength(len);
   551 		SlCopyBytes(ptr, len);
   551 		SlCopyBytes(ptr, len);
   566 				}
   566 				}
   567 				break;
   567 				break;
   568 			case SLE_VAR_STR:
   568 			case SLE_VAR_STR:
   569 			case SLE_VAR_STRQ: /* Malloc'd string, free previous incarnation, and allocate */
   569 			case SLE_VAR_STRQ: /* Malloc'd string, free previous incarnation, and allocate */
   570 				free(*(char**)ptr);
   570 				free(*(char**)ptr);
   571 				*(char**)ptr = malloc(len + 1); // terminating '\0'
   571 				*(char**)ptr = (char*)malloc(len + 1); // terminating '\0'
   572 				ptr = *(char**)ptr;
   572 				ptr = *(char**)ptr;
   573 				SlCopyBytes(ptr, len);
   573 				SlCopyBytes(ptr, len);
   574 				break;
   574 				break;
   575 		}
   575 		}
   576 
   576 
   712 		switch (sld->cmd) {
   712 		switch (sld->cmd) {
   713 		case SL_VAR: SlSaveLoadConv(ptr, conv); break;
   713 		case SL_VAR: SlSaveLoadConv(ptr, conv); break;
   714 		case SL_REF: /* Reference variable, translate */
   714 		case SL_REF: /* Reference variable, translate */
   715 			/// @todo XXX - another artificial limitof 65K elements of pointers?
   715 			/// @todo XXX - another artificial limitof 65K elements of pointers?
   716 			if (_sl.save) { // XXX - read/write pointer as uint16? What is with higher indeces?
   716 			if (_sl.save) { // XXX - read/write pointer as uint16? What is with higher indeces?
   717 				SlWriteUint16(ReferenceToInt(*(void**)ptr, conv));
   717 				SlWriteUint16(ReferenceToInt(*(void**)ptr, (SLRefType)conv));
   718 			} else {
   718 			} else {
   719 				*(void**)ptr = IntToReference(SlReadUint16(), conv);
   719 				*(void**)ptr = IntToReference(SlReadUint16(), (SLRefType)conv);
   720 			}
   720 			}
   721 			break;
   721 			break;
   722 		case SL_ARR: SlArray(ptr, sld->length, conv); break;
   722 		case SL_ARR: SlArray(ptr, sld->length, conv); break;
   723 		case SL_STR: SlString(ptr, sld->length, conv); break;
   723 		case SL_STR: SlString(ptr, sld->length, conv); break;
   724 		default: NOT_REACHED();
   724 		default: NOT_REACHED();
  1395 /** Update the gui accordingly when starting saving
  1395 /** Update the gui accordingly when starting saving
  1396  * and set locks on saveload. Also turn off fast-forward cause with that
  1396  * and set locks on saveload. Also turn off fast-forward cause with that
  1397  * saving takes Aaaaages */
  1397  * saving takes Aaaaages */
  1398 void SaveFileStart(void)
  1398 void SaveFileStart(void)
  1399 {
  1399 {
  1400 	_ts.ff_state = _fast_forward;
  1400 	_ts.ff_state = (_fast_forward != 0);
  1401 	_fast_forward = false;
  1401 	_fast_forward = false;
  1402 	if (_cursor.sprite == SPR_CURSOR_MOUSE) SetMouseCursor(SPR_CURSOR_ZZZ);
  1402 	if (_cursor.sprite == SPR_CURSOR_MOUSE) SetMouseCursor(SPR_CURSOR_ZZZ);
  1403 
  1403 
  1404 	SendWindowMessage(WC_STATUS_BAR, 0, true, 0, 0);
  1404 	SendWindowMessage(WC_STATUS_BAR, 0, true, 0, 0);
  1405 	_ts.saveinprogress = true;
  1405 	_ts.saveinprogress = true;
  1523 		return SL_ERROR;
  1523 		return SL_ERROR;
  1524 	}
  1524 	}
  1525 
  1525 
  1526 	_sl.bufe = _sl.bufp = NULL;
  1526 	_sl.bufe = _sl.bufp = NULL;
  1527 	_sl.offs_base = 0;
  1527 	_sl.offs_base = 0;
  1528 	_sl.save = mode;
  1528 	_sl.save = (mode != 0);
  1529 	_sl.includes = _desc_includes;
  1529 	_sl.includes = _desc_includes;
  1530 	_sl.chs = _chunk_handlers;
  1530 	_sl.chs = _chunk_handlers;
  1531 
  1531 
  1532 	/* XXX - Setup setjmp error handler if an error occurs anywhere deep during
  1532 	/* XXX - Setup setjmp error handler if an error occurs anywhere deep during
  1533 	 * loading/saving execute a longjmp() and continue execution here */
  1533 	 * loading/saving execute a longjmp() and continue execution here */