src/saveload.cpp
changeset 8935 e9bef9cc4e84
parent 8934 f46812d21fe6
child 8965 29a591456a2f
equal deleted inserted replaced
8934:f46812d21fe6 8935:e9bef9cc4e84
    74 } _sl;
    74 } _sl;
    75 
    75 
    76 
    76 
    77 enum NeedLengthValues {NL_NONE = 0, NL_WANTLENGTH = 1, NL_CALCLENGTH = 2};
    77 enum NeedLengthValues {NL_NONE = 0, NL_WANTLENGTH = 1, NL_CALCLENGTH = 2};
    78 
    78 
       
    79 /** Error handler, calls longjmp to simulate an exception.
       
    80  * @todo this was used to have a central place to handle errors, but it is
       
    81  * pretty ugly, and seriously interferes with any multithreaded approaches */
       
    82 static void NORETURN SlError(StringID string, const char *extra_msg = NULL)
       
    83 {
       
    84 	_sl.error_str = string;
       
    85 	free(_sl.extra_msg);
       
    86 	_sl.extra_msg = (extra_msg == NULL) ? NULL : strdup(extra_msg);
       
    87 	throw std::exception();
       
    88 }
       
    89 
    79 /**
    90 /**
    80  * Fill the input buffer by reading from the file with the given reader
    91  * Fill the input buffer by reading from the file with the given reader
    81  */
    92  */
    82 static void SlReadFill()
    93 static void SlReadFill()
    83 {
    94 {
    84 	uint len = _sl.read_bytes();
    95 	uint len = _sl.read_bytes();
    85 	assert(len != 0);
    96 	if (len == 0) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, "Unexpected end of chunk");
    86 
    97 
    87 	_sl.bufp = _sl.buf;
    98 	_sl.bufp = _sl.buf;
    88 	_sl.bufe = _sl.buf + len;
    99 	_sl.bufe = _sl.buf + len;
    89 	_sl.offs_base += len;
   100 	_sl.offs_base += len;
    90 }
   101 }
   133 
   144 
   134 	/* All the data from the buffer has been written away, rewind to the beginning
   145 	/* All the data from the buffer has been written away, rewind to the beginning
   135 	 * to start reading in more data */
   146 	 * to start reading in more data */
   136 	_sl.bufp = _sl.buf;
   147 	_sl.bufp = _sl.buf;
   137 	_sl.bufe = _sl.buf + _sl.bufsize;
   148 	_sl.bufe = _sl.buf + _sl.bufsize;
   138 }
       
   139 
       
   140 /** Error handler, calls longjmp to simulate an exception.
       
   141  * @todo this was used to have a central place to handle errors, but it is
       
   142  * pretty ugly, and seriously interferes with any multithreaded approaches */
       
   143 static void NORETURN SlError(StringID string, const char *extra_msg = NULL)
       
   144 {
       
   145 	_sl.error_str = string;
       
   146 	free(_sl.extra_msg);
       
   147 	_sl.extra_msg = (extra_msg == NULL) ? NULL : strdup(extra_msg);
       
   148 	throw std::exception();
       
   149 }
   149 }
   150 
   150 
   151 /** Read in a single byte from file. If the temporary buffer is full,
   151 /** Read in a single byte from file. If the temporary buffer is full,
   152  * flush it to its final destination
   152  * flush it to its final destination
   153  * @return return the read byte from file
   153  * @return return the read byte from file
  1522 
  1522 
  1523 		{
  1523 		{
  1524 			uint i;
  1524 			uint i;
  1525 			uint count = 1 << Savegame_POOL_BLOCK_SIZE_BITS;
  1525 			uint count = 1 << Savegame_POOL_BLOCK_SIZE_BITS;
  1526 
  1526 
  1527 			assert(_ts.count == _sl.offs_base);
  1527 			if (_ts.count != _sl.offs_base) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, "Unexpected size of chunk");
  1528 			for (i = 0; i != _Savegame_pool.GetBlockCount() - 1; i++) {
  1528 			for (i = 0; i != _Savegame_pool.GetBlockCount() - 1; i++) {
  1529 				_sl.buf = _Savegame_pool.blocks[i];
  1529 				_sl.buf = _Savegame_pool.blocks[i];
  1530 				fmt->writer(count);
  1530 				fmt->writer(count);
  1531 			}
  1531 			}
  1532 
  1532 
  1535 			_sl.buf = _Savegame_pool.blocks[i];
  1535 			_sl.buf = _Savegame_pool.blocks[i];
  1536 			fmt->writer(_ts.count - (i * count));
  1536 			fmt->writer(_ts.count - (i * count));
  1537 		}
  1537 		}
  1538 
  1538 
  1539 		fmt->uninit_write();
  1539 		fmt->uninit_write();
  1540 		assert(_ts.count == _sl.offs_base);
  1540 		if (_ts.count != _sl.offs_base) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, "Unexpected size of chunk");
  1541 		GetSavegameFormat("memory")->uninit_write(); // clean the memorypool
  1541 		GetSavegameFormat("memory")->uninit_write(); // clean the memorypool
  1542 		fclose(_sl.fh);
  1542 		fclose(_sl.fh);
  1543 
  1543 
  1544 		if (threaded) OTTD_SendThreadMessage(MSG_OTTD_SAVETHREAD_DONE);
  1544 		if (threaded) OTTD_SendThreadMessage(MSG_OTTD_SAVETHREAD_DONE);
  1545 
  1545