saveload.c
changeset 2285 410dedcf46d1
parent 2283 defd5403139a
child 2286 733dbf6b4545
equal deleted inserted replaced
2284:c1bd2d5e8544 2285:410dedcf46d1
    19 #include "openttd.h"
    19 #include "openttd.h"
    20 #include "debug.h"
    20 #include "debug.h"
    21 #include "functions.h"
    21 #include "functions.h"
    22 #include "vehicle.h"
    22 #include "vehicle.h"
    23 #include "station.h"
    23 #include "station.h"
       
    24 #include "thread.h"
    24 #include "town.h"
    25 #include "town.h"
    25 #include "player.h"
    26 #include "player.h"
    26 #include "saveload.h"
    27 #include "saveload.h"
    27 #include "variables.h"
    28 #include "variables.h"
    28 
    29 
  1232 }
  1233 }
  1233 
  1234 
  1234 /** We have written the whole game into memory, _save_pool, now find
  1235 /** We have written the whole game into memory, _save_pool, now find
  1235  * and appropiate compressor and start writing to file.
  1236  * and appropiate compressor and start writing to file.
  1236  */
  1237  */
  1237 static bool SaveFileToDisk(void *ptr)
  1238 static void SaveFileToDisk(void* arg)
  1238 {
  1239 {
  1239 	const SaveLoadFormat *fmt = GetSavegameFormat(_savegame_format);
  1240 	const SaveLoadFormat *fmt = GetSavegameFormat(_savegame_format);
  1240 	/* XXX - backup _sl.buf cause it is used internally by the writer
  1241 	/* XXX - backup _sl.buf cause it is used internally by the writer
  1241 	 * and we update it for our own purposes */
  1242 	 * and we update it for our own purposes */
  1242 	static byte *tmp = NULL;
  1243 	static byte *tmp = NULL;
  1255 
  1256 
  1256 		ShowInfoF("Save game failed: %s.", _sl.excpt_msg);
  1257 		ShowInfoF("Save game failed: %s.", _sl.excpt_msg);
  1257 		ShowErrorMessage(STR_4007_GAME_SAVE_FAILED, STR_NULL, 0, 0);
  1258 		ShowErrorMessage(STR_4007_GAME_SAVE_FAILED, STR_NULL, 0, 0);
  1258 
  1259 
  1259 		SaveFileDone();
  1260 		SaveFileDone();
  1260 		return false;
  1261 		return;
  1261 	}
  1262 	}
  1262 
  1263 
  1263 	/* We have written our stuff to memory, now write it to file! */
  1264 	/* We have written our stuff to memory, now write it to file! */
  1264 	hdr[0] = fmt->tag;
  1265 	hdr[0] = fmt->tag;
  1265 	hdr[1] = TO_BE32((SAVEGAME_MAJOR_VERSION << 16) + (SAVEGAME_MINOR_VERSION << 8));
  1266 	hdr[1] = TO_BE32((SAVEGAME_MAJOR_VERSION << 16) + (SAVEGAME_MINOR_VERSION << 8));
  1290 	assert(_ts.count == _sl.offs_base);
  1291 	assert(_ts.count == _sl.offs_base);
  1291 	GetSavegameFormat("memory")->uninit_write(); // clean the memorypool
  1292 	GetSavegameFormat("memory")->uninit_write(); // clean the memorypool
  1292 	fclose(_sl.fh);
  1293 	fclose(_sl.fh);
  1293 
  1294 
  1294 	SaveFileDone();
  1295 	SaveFileDone();
  1295 	return true;
  1296 }
  1296 }
  1297 
       
  1298 
       
  1299 static Thread* save_thread;
       
  1300 
       
  1301 void WaitTillSaved(void)
       
  1302 {
       
  1303 	OTTDJoinThread(save_thread);
       
  1304 	save_thread = NULL;
       
  1305 }
       
  1306 
  1297 
  1307 
  1298 /**
  1308 /**
  1299  * Main Save or Load function where the high-level saveload functions are
  1309  * Main Save or Load function where the high-level saveload functions are
  1300  * handled. It opens the savegame, selects format and checks versions
  1310  * handled. It opens the savegame, selects format and checks versions
  1301  * @param filename The name of the savegame being created/loaded
  1311  * @param filename The name of the savegame being created/loaded
  1309   uint version;
  1319   uint version;
  1310 
  1320 
  1311 	/* An instance of saving is already active, so wait until it is done */
  1321 	/* An instance of saving is already active, so wait until it is done */
  1312 	if (_ts.saveinprogress) {
  1322 	if (_ts.saveinprogress) {
  1313 		if (!_do_autosave) ShowErrorMessage(_error_message, STR_SAVE_STILL_IN_PROGRESS, 0, 0);
  1323 		if (!_do_autosave) ShowErrorMessage(_error_message, STR_SAVE_STILL_IN_PROGRESS, 0, 0);
  1314 		JoinOTTDThread(); // synchronize and wait until save is finished to continue
  1324 		WaitTillSaved();
  1315 		// nonsense to do an autosave while we were still saving our game, so skip it
  1325 		// nonsense to do an autosave while we were still saving our game, so skip it
  1316 		if (_do_autosave) return SL_OK;
  1326 		if (_do_autosave) return SL_OK;
  1317 	}
  1327 	}
  1318 
  1328 
  1319   /* Load a TTDLX or TTDPatch game */
  1329   /* Load a TTDLX or TTDPatch game */
  1377 		BeforeSaveGame();
  1387 		BeforeSaveGame();
  1378 		SlSaveChunks();
  1388 		SlSaveChunks();
  1379 		SlWriteFill(); // flush the save buffer
  1389 		SlWriteFill(); // flush the save buffer
  1380 
  1390 
  1381 		/* Write to file */
  1391 		/* Write to file */
  1382 		if (_network_server || !CreateOTTDThread(&SaveFileToDisk, NULL)) {
  1392 		if (_network_server ||
       
  1393 				(save_thread = OTTDCreateThread(&SaveFileToDisk, NULL)) == NULL) {
  1383 			DEBUG(misc, 1) ("cannot create savegame thread, reverting to single-threaded mode...");
  1394 			DEBUG(misc, 1) ("cannot create savegame thread, reverting to single-threaded mode...");
  1384 			SaveFileToDisk(NULL);
  1395 			SaveFileToDisk(NULL);
  1385 		}
  1396 		}
  1386 
  1397 
  1387 	} else { /* LOAD game */
  1398 	} else { /* LOAD game */