Beispiel #1
0
        /// <summary>
        /// Creates an Instance of simulator. Should be called only from factory.
        /// </summary>
        /// <param name="configuration">configuration</param>
        public Simulator(SimulatorConfiguration configuration)
        {
            // Leere Konfiguration prüfen
            if (configuration == null)
            {
                throw new ArgumentNullException("configuration", Resource.SimulationCoreFactoryConfigIsNull);
            }

            // Copy config
            this.configuration = (SimulatorConfiguration)configuration.Clone();

            // Reload PlayerInfo
            if (this.configuration.Teams != null)
            {
                foreach (TeamInfo team in this.configuration.Teams)
                {
                    if (team.Player != null)
                    {
                        for (int i = 0; i < team.Player.Count; i++)
                        {
                            PlayerInfo player = team.Player[i];
                            if (player is PlayerInfoFiledump)
                            {
                                team.Player[i] =
                                    AiAnalysis.FindPlayerInformation(
                                        ((PlayerInfoFiledump)player).File, player.ClassName);
                            }
                            else if (player is PlayerInfoFilename)
                            {
                                team.Player[i] =
                                    AiAnalysis.FindPlayerInformation(
                                        ((PlayerInfoFilename)player).File, player.ClassName);
                            }
                            else
                            {
                                // TODO: Throw unknown type...
                            }
                            team.Player[i].Guid = player.Guid;
                        }
                    }
                }
            }

            // Regelprüfung der Konfig anwerfen
            configuration.Rulecheck();


            // Init values
            currentLoop  = 0;
            currentRound = 0;
            roundTime    = 0;
            loopTime     = 0;
            totalTime    = 0;

            // Reset
            totalPlayerTime = new Dictionary <Guid, long>();
            for (int i = 0; i < configuration.Teams.Count; i++)
            {
                for (int j = 0; j < configuration.Teams[i].Player.Count; j++)
                {
                    Guid guid = configuration.Teams[i].Player[j].Guid;
                    if (!totalPlayerTime.ContainsKey(guid))
                    {
                        totalPlayerTime.Add(guid, 0);
                    }
                }
            }

            // Set initial state
            state = SimulatorState.Ready;
        }
        /// <summary>
        /// Initialisierung der Simulation
        /// </summary>
        /// <param name="configuration">Simulationskonfiguration</param>
        public void Init(SimulatorConfiguration configuration)
        {
            // unload possible appdomains.
            Unload();

            // load involved ai's
            for (int team = 0; team < configuration.Teams.Count; team++)
            {
                for (int i = 0; i < configuration.Teams[team].Player.Count; i++)
                {
                    // externe Guid-Info speichern
                    Guid guid = configuration.Teams[team].Player[i].Guid;

                    if (configuration.Teams[team].Player[i] is PlayerInfoFiledump)
                    {
                        // use filedump
                        configuration.Teams[team].Player[i] =
                            AiAnalysis.FindPlayerInformation(
                                ((PlayerInfoFiledump)configuration.Teams[team].Player[i]).File,
                                configuration.Teams[team].Player[i].ClassName);
                    }
                    else if (configuration.Teams[team].Player[i] is PlayerInfoFilename)
                    {
                        // use filename
                        configuration.Teams[team].Player[i] =
                            AiAnalysis.FindPlayerInformation(
                                ((PlayerInfoFilename)configuration.Teams[team].Player[i]).File,
                                configuration.Teams[team].Player[i].ClassName);
                    }
                    else
                    {
                        // not supported PlayerInfo-type
                        throw new InvalidOperationException(
                                  Resource.SimulationCoreProxyWrongPlayerInfo);
                    }

                    // Rückspeicherung der externen Guid
                    configuration.Teams[team].Player[i].Guid = guid;
                }
            }

            // setup appDomain
            AppDomainSetup setup = new AppDomainSetup();

            setup.ApplicationBase       = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            setup.ShadowCopyDirectories = AppDomain.CurrentDomain.SetupInformation.ShadowCopyDirectories;
            setup.ShadowCopyFiles       = "false";
            setup.PrivateBinPath        = "";

            // setup some access-rights für appdomain
            PermissionSet rechte = new PermissionSet(PermissionState.None);

            rechte.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));
            rechte.AddPermission(new ReflectionPermission(ReflectionPermissionFlag.RestrictedMemberAccess));

            bool IoAccess  = false;
            bool UiAccess  = false;
            bool DbAccess  = false;
            bool NetAccess = false;

            // allow access to the needed ai-files
            foreach (TeamInfo team in configuration.Teams)
            {
                foreach (PlayerInfo info in team.Player)
                {
                    if (info is PlayerInfoFilename)
                    {
                        rechte.AddPermission(
                            new FileIOPermission(
                                FileIOPermissionAccess.Read |
                                FileIOPermissionAccess.PathDiscovery,
                                ((PlayerInfoFilename)info).File));
                    }
                    if (info.RequestDatabaseAccess)
                    {
                        DbAccess = true;
                    }
                    if (info.RequestFileAccess)
                    {
                        IoAccess = true;
                    }
                    if (info.RequestNetworkAccess)
                    {
                        NetAccess = true;
                    }
                    if (info.RequestUserInterfaceAccess)
                    {
                        UiAccess = true;
                    }
                }
            }

            // Grand special rights
            if (IoAccess)
            {
                if (configuration.AllowFileAccess)
                {
                    rechte.AddPermission(new FileIOPermission(PermissionState.Unrestricted));
                }
                else
                {
                    throw new ConfigurationErrorsException(Resource.SimulationCoreRightsConflictIo);
                }
            }

            if (UiAccess)
            {
                if (configuration.AllowUserinterfaceAccess)
                {
                    rechte.AddPermission(new UIPermission(PermissionState.Unrestricted));
                }
                else
                {
                    throw new ConfigurationErrorsException(Resource.SimulationCoreRightsConflictUi);
                }
            }

            if (DbAccess)
            {
                if (configuration.AllowDatabaseAccess)
                {
                    // TODO: Grand rights
                }
                else
                {
                    throw new ConfigurationErrorsException(Resource.SimulationCoreRightsConflictDb);
                }
            }

            if (NetAccess)
            {
                if (configuration.AllowNetworkAccess)
                {
                    rechte.AddPermission(new System.Net.WebPermission(PermissionState.Unrestricted));
                    rechte.AddPermission(new System.Net.SocketPermission(PermissionState.Unrestricted));
                    rechte.AddPermission(new System.Net.DnsPermission(PermissionState.Unrestricted));
                }
                else
                {
                    throw new ConfigurationErrorsException(Resource.SimulationCoreRightsConflictNet);
                }
            }

            // create appdomain and load simulation-host
            appDomain = AppDomain.CreateDomain("Simulation", AppDomain.CurrentDomain.Evidence, setup, rechte);

            host =
                (SimulatorHost)
                appDomain.CreateInstanceAndUnwrap(
                    Assembly.GetExecutingAssembly().FullName, "AntMe.Simulation.SimulatorHost");

            // initialize host
            if (!host.Init(configuration))
            {
                // got some exceptions - unload appdomain und throw exception
                Exception ex = host.Exception;
                Unload();
                throw ex;
            }
        }