/// <summary> /// Creates a new instance of the species class initialized with /// the given CLR Type pulling various fields out of the Type's /// attributes. /// </summary> /// <param name="clrType">The CLR Type to initialize this species.</param> protected Species(Type clrType) { MarkingColor = KnownColor.Black; _typeName = clrType.AssemblyQualifiedName; _assemblyFullName = clrType.Assembly.FullName; // Cache attributes because it is expensive to load them var energyAttribute = (MaximumEnergyPointsAttribute) Attribute.GetCustomAttribute(clrType, typeof(MaximumEnergyPointsAttribute)); if (energyAttribute == null) { throw new AttributeRequiredException("MaximumEnergyPointsAttribute"); } _maximumEnergyPerUnitRadius = energyAttribute.MaximumEnergyPerUnitRadius; var matureSizeAttribute = (MatureSizeAttribute)Attribute.GetCustomAttribute(clrType, typeof(MatureSizeAttribute)); if (matureSizeAttribute == null) { throw new AttributeRequiredException("MatureSizeAttribute"); } MatureRadius = matureSizeAttribute.MatureRadius; var markingColorAttribute = (MarkingColorAttribute)Attribute.GetCustomAttribute(clrType, typeof(MarkingColorAttribute)); if (markingColorAttribute != null) { MarkingColor = markingColorAttribute.MarkingColor; } var authInfo = (AuthorInformationAttribute) Attribute.GetCustomAttribute(clrType.Assembly, typeof(AuthorInformationAttribute)); if (authInfo == null || authInfo.AuthorName.Length == 0) { throw new AttributeRequiredException("AuthorInformationAttribute"); } AuthorName = authInfo.AuthorName; AuthorEmail = authInfo.AuthorEmail; // The assembly name is the name of the organism Name = PrivateAssemblyCache.GetAssemblyShortName(clrType.Assembly.FullName); }
/// <summary> /// Constructs a new game engine. /// </summary> /// <param name="dataPath">The path to save game directory.</param> /// <param name="useNetwork">Controls the usage of the network engine.</param> /// <param name="deserializeState">Controls if the state is deserialized or not.</param> /// <param name="fileName">The path to the state file.</param> /// <param name="reportData">Determines if data should be reported.</param> /// <param name="leds">Provides a listing of game leds that can be used.</param> /// <param name="trackLastRun">Controls whether the PAC keeps track of the last run creature for blacklisting.</param> private GameEngine(string dataPath, bool useNetwork, bool deserializeState, string fileName, bool reportData, TerrariumLed[] leds, bool trackLastRun) { _ledIndicators = leds; _currentStateFileName = fileName; // test to make sure we're not violating any constraints by current // physics settings in the engine. EngineSettings.EngineSettingsAsserts(); // Calculate quanta and worldsize if we haven't done so yet if (_reloadSettings) CalculateWorldSize(); _pac = new PrivateAssemblyCache(dataPath, fileName, true, trackLastRun); // Reset the appdomain policy since we changed the location of the organism dlls // This must be done before any animals are loaded in any way. Make sure this call stays // as soon as possible AppDomain.CurrentDomain.SetAppDomainPolicy(SecurityUtils.MakePolicyLevel(_pac.AssemblyDirectory)); _usingNetwork = useNetwork; _populationData = new PopulationData(reportData, leds[(int) LedIndicators.ReportWebService]); // Should only happen if an exception prevented a previous attempt to start a game if (AppMgr.CurrentScheduler != null) { AppMgr.DestroyScheduler(); } // Create a scheduler that manages giving the creatures timeslices _scheduler = AppMgr.CreateSameDomainScheduler(this); _scheduler.Quantum = _organismQuanta; if (useNetwork) { // Required to start up the network listeners _networkEngine = new NetworkEngine(); } WorldState currentState; Boolean successfulDeserialization = false; if (deserializeState && File.Exists(fileName)) { try { if (_pac.LastRun.Length != 0) { // The process was killed while an organism was being run, blacklist it // Since this potentially means the animal hung the game. _pac.BlacklistAssemblies(new string[] {_pac.LastRun}); } this.deserializeState(fileName); currentState = CurrentVector.State; _scheduler.CurrentState = currentState; _scheduler.CompleteOrganismDeserialization(); successfulDeserialization = true; } catch (Exception e) { ErrorLog.LogHandledException(e); } } if (successfulDeserialization) return; // Set up initial world state currentState = new WorldState(GridWidth, GridHeight); currentState.TickNumber = 0; currentState.StateGuid = Guid.NewGuid(); currentState.Teleporter = new Teleporter(AnimalCount/EngineSettings.NumberOfAnimalsPerTeleporter); currentState.MakeImmutable(); WorldVector vector = new WorldVector(currentState); CurrentVector = vector; _scheduler.CurrentState = currentState; }
// Try to load a game private void LoadGame() { try { if (screenSaverMode != ScreenSaverMode.Run) { if (GameConfig.WebRoot.Length == 0) { ShowPropertiesDialog("Server"); } //if (GameConfig.UserEmail.Length == 0) //{ // ShowPropertiesDialog("Registration"); //} } this.Cursor = Cursors.WaitCursor; if (_options.BlackListCheck) { // We have been launched and asked to check for blacklisted species before // we start up. Call the server and ask. SpeciesService service = new SpeciesService(); service.Url = GameConfig.WebRoot + "/Species/AddSpecies.asmx"; service.Timeout = 60000; string[] blacklist = null; try { blacklist = service.GetBlacklistedSpecies(); if (blacklist != null) { PrivateAssemblyCache pac = new PrivateAssemblyCache(SpecialUserAppDataPath, ecosystemStateFileName, false, false); pac.BlacklistAssemblies(blacklist); } } catch (Exception e) { Debug.WriteLine("Problem blacklisting animals on startup"); ErrorLog.LogHandledException(e); } engineStateText += "Terrarium had to sanitize your ecosystem because it contained some bad animals.\r\n"; } switch (screenSaverMode) { case ScreenSaverMode.NoScreenSaver: // run ecosystem GameEngine.LoadEcosystemGame(SpecialUserAppDataPath, ecosystemStateFileName, developerPanel.Leds); break; case ScreenSaverMode.Run: // run the screensaver GameEngine.LoadEcosystemGame(SpecialUserAppDataPath, ecosystemStateFileName, developerPanel.Leds); break; case ScreenSaverMode.RunLoadTerrarium: GameEngine.LoadTerrariumGame(Path.GetDirectoryName(_gamePath), _gamePath, developerPanel.Leds); break; case ScreenSaverMode.RunNewTerrarium: GameEngine.LoadTerrariumGame(Path.GetDirectoryName(_gamePath), _gamePath, developerPanel.Leds); break; } } catch (SocketException e) { ErrorLog.LogHandledException(e); MessageBox.Show("A version of the Terrarium is already running. If you are connected via Terminal Server you can shut down any instances of Terrarium by using the Task Manager."); Application.Exit(); } catch (Exception e) { if (screenSaverMode == ScreenSaverMode.NoScreenSaver || screenSaverMode == ScreenSaverMode.Run) { // Only assert in ecosystem mode because it should never fail Debug.Assert(false, "Problem loading ecosystem: " + e.ToString()); } MessageBox.Show(this, "Can't load game: " + e.Message, "Error Loading Game", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); CloseTerrarium(false); return; } NewGameLoaded(); }