public void writeSavedGame( OutputStream out, SavedGameState gameState ) throws IOException {

		if ( gameState.getMysteryList().size() > 0 )
			log.warn( "The original saved game file contained mystery bytes, which will be omitted in the new file" );

		// This should always be 2.
		writeInt( out, 2 );

		writeBool( out, gameState.isDifficultyEasy() );
		writeInt( out, gameState.getTotalShipsDefeated() );
		writeInt( out, gameState.getTotalBeaconsExplored() );
		writeInt( out, gameState.getTotalScrapCollected() );
		writeInt( out, gameState.getTotalCrewHired() );

		writeString( out, gameState.getPlayerShipName() );
		writeString( out, gameState.getPlayerShipBlueprintId() );

		// Redundant 1-based sector number.
		writeInt( out, gameState.getSectorNumber()+1 );

		writeInt( out, gameState.getHeaderAlpha() );

		writeInt( out, gameState.getStateVars().size() );
		for (Map.Entry<String, Integer> entry : gameState.getStateVars().entrySet()) {
			writeString( out, entry.getKey() );
			writeInt( out, entry.getValue().intValue() );
		}

		writeShip( out, gameState.getPlayerShipState() );

		writeInt( out, gameState.getCargoIdList().size() );
		for (String cargoItemId : gameState.getCargoIdList()) {
			writeString( out, cargoItemId );
		}

		writeInt( out, gameState.getSectorTreeSeed() );
		writeInt( out, gameState.getSectorLayoutSeed() );
		writeInt( out, gameState.getRebelFleetOffset() );
		writeInt( out, gameState.getRebelFleetFudge() );
		writeInt( out, gameState.getRebelPursuitMod() );
		writeBool( out, gameState.areSectorHazardsVisible() );
		writeBool( out, gameState.isRebelFlagshipVisible() );
		writeInt( out, gameState.getRebelFlagshipHop() );
		writeBool( out, gameState.isRebelFlagshipApproaching() );

		writeInt( out, gameState.getSectorList().size() );
		for (Boolean visited : gameState.getSectorList()) {
			writeBool( out, visited.booleanValue() );
		}

		writeInt( out, gameState.getSectorNumber() );
		writeBool( out, gameState.isSectorHiddenCrystalWorlds() );

		writeInt( out, gameState.getBeaconList().size() );
		for (BeaconState beacon : gameState.getBeaconList()) {
			writeBeacon( out, beacon );
		}

		writeInt( out, gameState.getQuestEventMap().size() );
		for (Map.Entry<String, Integer> entry : gameState.getQuestEventMap().entrySet()) {
			writeString( out, entry.getKey() );
			writeInt( out, entry.getValue().intValue() );
		}

		writeInt( out, gameState.getDistantQuestEventList().size() );
		for (String questEventId : gameState.getDistantQuestEventList()) {
			writeString( out, questEventId );
		}

		writeInt( out, gameState.getCurrentBeaconId() );

		ShipState nearbyShip = gameState.getNearbyShipState();
		writeBool( out, (nearbyShip != null) );
		if ( nearbyShip != null ) {
			writeShip( out, nearbyShip );
		}

		writeRebelFlagship( out, gameState.getRebelFlagshipState() );

	}