/// <summary> /// ISerializable constructor /// </summary> /// <param name="info"></param> /// <param name="c"></param> private OrganismWrapper(SerializationInfo info, StreamingContext c) { try { var t = Type.GetType((string) info.GetValue("OrganismType", typeof (string))); _org = (Organism) Activator.CreateInstance(t); } catch (Exception e) { ErrorLog.LogHandledException(e); // Something failed when we tried to deserialize. This usually means that the // animal has been blacklisted and thus their assembly has been nulled out. // Replace with an instance of TerrariumOrganism so we don't abort the whole deserialization process. // TerrariumOrganism will simply throw an exception when we try to give it a time slice. _org = (Organism) Activator.CreateInstance(typeof (TerrariumOrganism)); return; } // Give the animal a chance to deserialize itself (_org).InternalOrganismDeserialize(new MemoryStream((byte[]) info.GetValue("OrganismInfo", typeof (byte[])))); if (_org is Animal) { ((Animal) _org).InternalAnimalDeserialize( new MemoryStream((byte[]) info.GetValue("AnimalInfo", typeof (byte[])))); _org.SerializedStream = new MemoryStream((byte[]) info.GetValue("UserInfo", typeof (byte[]))); } else { ((Plant) _org).InternalPlantDeserialize( new MemoryStream((byte[]) info.GetValue("PlantInfo", typeof (byte[])))); _org.SerializedStream = new MemoryStream((byte[]) info.GetValue("UserInfo", typeof (byte[]))); } }
/// <summary> /// Sets the world boundary for a specific creature. /// </summary> /// <param name="organism">The organism the boundary is for.</param> /// <param name="id">The Unique ID of the organism.</param> internal static void SetWorldBoundary(Organism organism, string id) { if (organism is Animal) { organism.SetWorldBoundary(new AnimalWorldBoundary(organism, id)); } else { organism.SetWorldBoundary(new PlantWorldBoundary(organism, id)); } }
/// <summary> /// Creates a new animal world boundary for a given animal /// </summary> /// <param name="animal">The actual class representing the creature.</param> /// <param name="ID">The unique ID of the creature.</param> internal AnimalWorldBoundary(Organism animal, string ID) : base(animal, ID) { }
// This is our last line of defense to deal with animals that try to hang the game (deadlock). // (the first line of defense is ThreadAborting the thread with our timer, see description in ActivateBug()). // Thus, it must have robust code that can always fail in some graceful way, and should blacklist // any animal that hangs. Because blacklisting an animal is very drastic, we go through great pains // to do it fairly, which means that we want to ensure that the animal is getting actual time to run // in the OS kernel, and the elapsed time isn't simply because the system is starving its thread // or something. If the animal got plenty of kernel time and didn't come back, we restart the game // and blacklist them permanently. If they aren't getting kernel time but it still taking way too long // we simply restart the game. // Add an organism to get timesliced. public void Add(Organism org, string id) { // We should only be adding organisms at the beginning Debug.Assert(_organismsActivated == 0); // Reconstruct its world boundary OrganismWorldBoundary.SetWorldBoundary(org, id); if (_organisms.ContainsKey(id)) { throw new OrganismAlreadyExistsException(); } _organisms.Add(new OrganismWrapper(org)); }
public OrganismWrapper(Organism org) { _org = org; }
/// <summary> /// Initializes a new world boundary given the original organism and their ID. /// </summary> /// <param name="organism">The organism used to init this world boundary.</param> /// <param name="ID">The Unique ID of the organism.</param> internal OrganismWorldBoundary(Organism organism, string ID) { this.organism = organism; this.organismID = ID; }
/// <summary> /// Creates a new plant world boundary given the owning /// plant and the plant's unique ID. /// </summary> /// <param name="plant">The plant used to initialize the world boundary.</param> /// <param name="id">The plant's Unique ID</param> internal PlantWorldBoundary(Organism plant, string id) : base(plant, id) { }