saveload.c
changeset 2026 567e3bc9af72
parent 1993 09205cf59fb4
child 2041 2210f5c42cc8
equal deleted inserted replaced
2025:6d1723144029 2026:567e3bc9af72
   216 
   216 
   217 		_sl.obj_len = --length;
   217 		_sl.obj_len = --length;
   218 		next_offs = SlGetOffs() + length;
   218 		next_offs = SlGetOffs() + length;
   219 
   219 
   220 		switch (_sl.block_mode) {
   220 		switch (_sl.block_mode) {
   221 		case CH_SPARSE_ARRAY:	index = SlReadSparseIndex(); break;
   221 		case CH_SPARSE_ARRAY: index = SlReadSparseIndex(); break;
   222 		case CH_ARRAY:        index = _sl.array_index++; break;
   222 		case CH_ARRAY:        index = _sl.array_index++; break;
   223 		default:
   223 		default:
   224 			DEBUG(misc, 0) ("SlIterateArray: error");
   224 			DEBUG(misc, 0) ("SlIterateArray: error");
   225 			return -1; // error
   225 			return -1; // error
   226 		}
   226 		}
   324 		}
   324 		}
   325 
   325 
   326 		// Write the value to the file and check if its value is in the desired range
   326 		// Write the value to the file and check if its value is in the desired range
   327 		switch (conv & 0xF) {
   327 		switch (conv & 0xF) {
   328 		case SLE_FILE_I8: assert(x >= -128 && x <= 127);     SlWriteByte(x);break;
   328 		case SLE_FILE_I8: assert(x >= -128 && x <= 127);     SlWriteByte(x);break;
   329 		case SLE_FILE_U8:	assert(x >= 0 && x <= 255);        SlWriteByte(x);break;
   329 		case SLE_FILE_U8: assert(x >= 0 && x <= 255);        SlWriteByte(x);break;
   330 		case SLE_FILE_I16:assert(x >= -32768 && x <= 32767); SlWriteUint16(x);break;
   330 		case SLE_FILE_I16:assert(x >= -32768 && x <= 32767); SlWriteUint16(x);break;
   331 		case SLE_FILE_STRINGID:
   331 		case SLE_FILE_STRINGID:
   332 		case SLE_FILE_U16:assert(x >= 0 && x <= 65535);      SlWriteUint16(x);break;
   332 		case SLE_FILE_U16:assert(x >= 0 && x <= 65535);      SlWriteUint16(x);break;
   333 		case SLE_FILE_I32: case SLE_FILE_U32:                SlWriteUint32((uint32)x);break;
   333 		case SLE_FILE_I32: case SLE_FILE_U32:                SlWriteUint32((uint32)x);break;
   334 		case SLE_FILE_I64: case SLE_FILE_U64:                SlWriteUint64(x);break;
   334 		case SLE_FILE_I64: case SLE_FILE_U64:                SlWriteUint64(x);break;
   463 			}
   463 			}
   464 		} else if (sld->cmd == SL_WRITEBYTE) {
   464 		} else if (sld->cmd == SL_WRITEBYTE) {
   465 			length++; // a byte is logically of size 1
   465 			length++; // a byte is logically of size 1
   466 		} else if (sld->cmd == SL_INCLUDE) {
   466 		} else if (sld->cmd == SL_INCLUDE) {
   467 			length += SlCalcObjLength(NULL, _sl.includes[sld->version_from]);
   467 			length += SlCalcObjLength(NULL, _sl.includes[sld->version_from]);
   468 		}	else
   468 		} else
   469 			assert(sld->cmd == SL_END);
   469 			assert(sld->cmd == SL_END);
   470 	}
   470 	}
   471 	return length;
   471 	return length;
   472 }
   472 }
   473 
   473 
   524 		/* SL_INCLUDE loads common code for a type
   524 		/* SL_INCLUDE loads common code for a type
   525 		 * XXX - variable renaming abuse
   525 		 * XXX - variable renaming abuse
   526 		 * include_index: common code to include from _desc_includes[], abused by sld->version_from */
   526 		 * include_index: common code to include from _desc_includes[], abused by sld->version_from */
   527 		} else if (sld->cmd == SL_INCLUDE) {
   527 		} else if (sld->cmd == SL_INCLUDE) {
   528 			SlObject(ptr, _sl.includes[sld->version_from]);
   528 			SlObject(ptr, _sl.includes[sld->version_from]);
   529 		}	else
   529 		} else
   530 			assert(sld->cmd == SL_END);
   530 			assert(sld->cmd == SL_END);
   531 	}
   531 	}
   532 }
   532 }
   533 
   533 
   534 /** Calculate the length of global variables
   534 /** Calculate the length of global variables
   939 	z->next_in = p;
   939 	z->next_in = p;
   940 	z->avail_in = len;
   940 	z->avail_in = len;
   941 	do {
   941 	do {
   942 		z->next_out = buf;
   942 		z->next_out = buf;
   943 		z->avail_out = sizeof(buf);
   943 		z->avail_out = sizeof(buf);
   944 		r	= deflate(z, mode);
   944 		r = deflate(z, mode);
   945 			// bytes were emitted?
   945 			// bytes were emitted?
   946 		if ((n=sizeof(buf) - z->avail_out) != 0) {
   946 		if ((n=sizeof(buf) - z->avail_out) != 0) {
   947 			if (fwrite(buf, n, 1, _sl.fh) != 1) SlError("file write error");
   947 			if (fwrite(buf, n, 1, _sl.fh) != 1) SlError("file write error");
   948 		}
   948 		}
   949 		if (r == Z_STREAM_END)
   949 		if (r == Z_STREAM_END)
  1435 	int mode = SL_OLD_LOAD;
  1435 	int mode = SL_OLD_LOAD;
  1436 
  1436 
  1437 	f = fopen(file, "rb");
  1437 	f = fopen(file, "rb");
  1438 	if (fread(&hdr, sizeof(hdr), 1, f) != 1) {
  1438 	if (fread(&hdr, sizeof(hdr), 1, f) != 1) {
  1439 		printf("Savegame is obsolete or invalid format.\n");
  1439 		printf("Savegame is obsolete or invalid format.\n");
  1440 		mode = SL_LOAD;	// don't try to get filename, just show name as it is written
  1440 		mode = SL_LOAD; // don't try to get filename, just show name as it is written
  1441 	}
  1441 	}
  1442 	else {
  1442 	else {
  1443 		// see if we have any loader for this type.
  1443 		// see if we have any loader for this type.
  1444 		for (fmt = _saveload_formats; fmt != endof(_saveload_formats); fmt++) {
  1444 		for (fmt = _saveload_formats; fmt != endof(_saveload_formats); fmt++) {
  1445 			if (fmt->tag == hdr) {
  1445 			if (fmt->tag == hdr) {