private void deserializeState(string path)
{
// We should only deserialize at the beginning of the 10 state turnphase because
// that's when we serialized
while (_turnPhase != 0)
{
try
{
ProcessTurn();
}
catch (GameEngineException e)
{
// The game will throw exceptions like StateTimeOut, Blacklist, etc during process turn
// Since we are serializing, ignore them all, we will hit them again when we report again
ErrorLog.LogHandledException(e);
}
}
BinaryFormatter b = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
try
{
_worldWidth = (int) b.Deserialize(stream);
_worldHeight = (int) b.Deserialize(stream);
_maxPlants = (int) b.Deserialize(stream);
_maxAnimals = (int) b.Deserialize(stream);
// Since we may be changing our settings to non-optimal settings, tell the engine to recalculate
// them next time
_reloadSettings = true;
_plantCount = (int) b.Deserialize(stream);
_animalCount = (int) b.Deserialize(stream);
CurrentVector = (WorldVector) b.Deserialize(stream);
Scheduler.DeserializeOrganisms(stream);
PopulationData.BeginTick(CurrentVector.State.TickNumber, CurrentVector.State.StateGuid);
foreach (OrganismState state in CurrentVector.State.Organisms)
{
PopulationData.CountOrganism(((Species) state.Species).Name, PopulationChangeReason.Initial);
}
PopulationData.EndTick(CurrentVector.State.TickNumber);
}
catch (Exception e)
{
// If we hit a problem, reset everything
_plantCount = 0;
_animalCount = 0;
CurrentVector = null;
// The scheduler should reset itself if it encounters an exception, so we don't need
// to do anything for it here
Trace.WriteLine("State could not be deserialized from " + path + ": " + e.Message + "\r\n" +
e.StackTrace);
throw;
}
finally
{
stream.Close();
}
}