src/saveload.cpp
changeset 10252 8d87c2f342cb
parent 10250 e64ee7484b43
child 10284 4f4086cd9585
equal deleted inserted replaced
10251:b41d7d8c873b 10252:8d87c2f342cb
    83 {
    83 {
    84 	_sl.error_str = string;
    84 	_sl.error_str = string;
    85 	free(_sl.extra_msg);
    85 	free(_sl.extra_msg);
    86 	_sl.extra_msg = (extra_msg == NULL) ? NULL : strdup(extra_msg);
    86 	_sl.extra_msg = (extra_msg == NULL) ? NULL : strdup(extra_msg);
    87 	throw std::exception();
    87 	throw std::exception();
       
    88 }
       
    89 
       
    90 typedef void (*AsyncSaveFinishProc)();
       
    91 static AsyncSaveFinishProc _async_save_finish = NULL;
       
    92 
       
    93 /**
       
    94  * Called by save thread to tell we finished saving.
       
    95  */
       
    96 static void SetAsyncSaveFinish(AsyncSaveFinishProc proc)
       
    97 {
       
    98 	if (_exit_game) return;
       
    99 	while (_async_save_finish != NULL) CSleep(10);
       
   100 
       
   101 	_async_save_finish = proc;
       
   102 }
       
   103 
       
   104 /**
       
   105  * Handle async save finishes.
       
   106  */
       
   107 void ProcessAsyncSaveFinish()
       
   108 {
       
   109 	if (_async_save_finish == NULL) return;
       
   110 
       
   111 	_async_save_finish();
       
   112 
       
   113 	_async_save_finish = NULL;
    88 }
   114 }
    89 
   115 
    90 /**
   116 /**
    91  * Fill the input buffer by reading from the file with the given reader
   117  * Fill the input buffer by reading from the file with the given reader
    92  */
   118  */
  1455 }
  1481 }
  1456 
  1482 
  1457 /** Update the gui accordingly when starting saving
  1483 /** Update the gui accordingly when starting saving
  1458  * and set locks on saveload. Also turn off fast-forward cause with that
  1484  * and set locks on saveload. Also turn off fast-forward cause with that
  1459  * saving takes Aaaaages */
  1485  * saving takes Aaaaages */
  1460 void SaveFileStart()
  1486 static void SaveFileStart()
  1461 {
  1487 {
  1462 	_ts.ff_state = _fast_forward;
  1488 	_ts.ff_state = _fast_forward;
  1463 	_fast_forward = 0;
  1489 	_fast_forward = 0;
  1464 	if (_cursor.sprite == SPR_CURSOR_MOUSE) SetMouseCursor(SPR_CURSOR_ZZZ, PAL_NONE);
  1490 	if (_cursor.sprite == SPR_CURSOR_MOUSE) SetMouseCursor(SPR_CURSOR_ZZZ, PAL_NONE);
  1465 
  1491 
  1467 	_ts.saveinprogress = true;
  1493 	_ts.saveinprogress = true;
  1468 }
  1494 }
  1469 
  1495 
  1470 /** Update the gui accordingly when saving is done and release locks
  1496 /** Update the gui accordingly when saving is done and release locks
  1471  * on saveload */
  1497  * on saveload */
  1472 void SaveFileDone()
  1498 static void SaveFileDone()
  1473 {
  1499 {
  1474 	_fast_forward = _ts.ff_state;
  1500 	_fast_forward = _ts.ff_state;
  1475 	if (_cursor.sprite == SPR_CURSOR_ZZZ) SetMouseCursor(SPR_CURSOR_MOUSE, PAL_NONE);
  1501 	if (_cursor.sprite == SPR_CURSOR_ZZZ) SetMouseCursor(SPR_CURSOR_MOUSE, PAL_NONE);
  1476 
  1502 
  1477 	SendWindowMessage(WC_STATUS_BAR, 0, false, 0, 0);
  1503 	SendWindowMessage(WC_STATUS_BAR, 0, false, 0, 0);
  1494 	GetString(err_str, _sl.save ? STR_4007_GAME_SAVE_FAILED : STR_4009_GAME_LOAD_FAILED, lastof(err_str));
  1520 	GetString(err_str, _sl.save ? STR_4007_GAME_SAVE_FAILED : STR_4009_GAME_LOAD_FAILED, lastof(err_str));
  1495 	return err_str;
  1521 	return err_str;
  1496 }
  1522 }
  1497 
  1523 
  1498 /** Show a gui message when saving has failed */
  1524 /** Show a gui message when saving has failed */
  1499 void SaveFileError()
  1525 static void SaveFileError()
  1500 {
  1526 {
  1501 	SetDParamStr(0, GetSaveLoadErrorString());
  1527 	SetDParamStr(0, GetSaveLoadErrorString());
  1502 	ShowErrorMessage(STR_012D, STR_NULL, 0, 0);
  1528 	ShowErrorMessage(STR_012D, STR_NULL, 0, 0);
  1503 	SaveFileDone();
  1529 	SaveFileDone();
  1504 }
  1530 }
  1543 		fmt->uninit_write();
  1569 		fmt->uninit_write();
  1544 		if (_ts.count != _sl.offs_base) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, "Unexpected size of chunk");
  1570 		if (_ts.count != _sl.offs_base) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, "Unexpected size of chunk");
  1545 		GetSavegameFormat("memory")->uninit_write(); // clean the memorypool
  1571 		GetSavegameFormat("memory")->uninit_write(); // clean the memorypool
  1546 		fclose(_sl.fh);
  1572 		fclose(_sl.fh);
  1547 
  1573 
  1548 		if (threaded) OTTD_SendThreadMessage(MSG_OTTD_SAVETHREAD_DONE);
  1574 		if (threaded) SetAsyncSaveFinish(SaveFileDone);
  1549 
  1575 
  1550 		return SL_OK;
  1576 		return SL_OK;
  1551 	}
  1577 	}
  1552 	catch (...) {
  1578 	catch (...) {
  1553 		AbortSaveLoad();
  1579 		AbortSaveLoad();
  1555 
  1581 
  1556 		ShowInfo(GetSaveLoadErrorString());
  1582 		ShowInfo(GetSaveLoadErrorString());
  1557 		fprintf(stderr, GetSaveLoadErrorString());
  1583 		fprintf(stderr, GetSaveLoadErrorString());
  1558 
  1584 
  1559 		if (threaded) {
  1585 		if (threaded) {
  1560 			OTTD_SendThreadMessage(MSG_OTTD_SAVETHREAD_ERROR);
  1586 			SetAsyncSaveFinish(SaveFileError);
  1561 		} else {
  1587 		} else {
  1562 			SaveFileError();
  1588 			SaveFileError();
  1563 		}
  1589 		}
  1564 		return SL_ERROR;
  1590 		return SL_ERROR;
  1565 	}
  1591 	}