protected override void InitSimulation(int seed, LevelInfo level, PlayerInfo[] players, Slot[] slots)
        {
            Assembly levelAssembly = Assembly.Load(level.Type.AssemblyFile);
            Type     levelType     = levelAssembly.GetType(level.Type.TypeName);

            SimulationContext context = new SimulationContext(
                ExtensionLoader.DefaultTypeResolver,
                ExtensionLoader.ExtensionSettings);

            simulation = Activator.CreateInstance(levelType, context) as Level;

            // Player erzeugen
            LevelSlot[] levelSlots = new LevelSlot[AntMe.Level.MAX_SLOTS];
            for (int i = 0; i < AntMe.Level.MAX_SLOTS; i++)
            {
                // Skipp, falls nicht vorhanden
                if (players[i] == null)
                {
                    continue;
                }

                Assembly playerAssembly = Assembly.Load(players[i].Type.AssemblyFile);
                Type     factoryType    = playerAssembly.GetType(players[i].Type.TypeName);

                // Identify Name
                var playerAttributes = factoryType.GetCustomAttributes(typeof(FactoryAttribute), true);
                if (playerAttributes.Length != 1)
                {
                    throw new Exception("Player does not have the right number of Player Attributes");
                }

                FactoryAttribute playerAttribute = playerAttributes[0] as FactoryAttribute;

                // Find the right Mapping
                var mappingAttributes = playerAttribute.GetType().
                                        GetCustomAttributes(typeof(FactoryAttributeMappingAttribute), false);
                if (mappingAttributes.Length != 1)
                {
                    throw new Exception("Player Attribute has no valid Property Mapping Attribute");
                }

                FactoryAttributeMappingAttribute mappingAttribute = mappingAttributes[0] as FactoryAttributeMappingAttribute;

                // Werte auslesen
                string name = playerAttribute.GetType().GetProperty(mappingAttribute.NameProperty).
                              GetValue(playerAttribute, null) as string;

                levelSlots[i] = new LevelSlot()
                {
                    FactoryType = factoryType,
                    Name        = name,
                    Color       = slots[i].ColorKey,
                    Team        = slots[i].Team
                };
            }

            // Level initialisieren
            simulation.Init(seed, levelSlots);
        }
Beispiel #2
0
        /// <summary>
        /// Liest alle notwendigen Informationen aus dem gegebenen Typ aus und
        /// liefert das gesammelte PlayerInfo.
        /// </summary>
        /// <param name="type">Type</param>
        /// <returns>Infos über den Player</returns>
        private static PlayerInfo AnalysePlayerType(Type type)
        {
            FactoryAttribute[] playerAttributes =
                (FactoryAttribute[])type.GetCustomAttributes(typeof(FactoryAttribute), true);

            // Kein oder zu viele Description Attributes
            if (playerAttributes.Length != 1)
            {
                throw new NotSupportedException(
                          string.Format("The Class '{0}' ({1}) has no valid PlayerAttribute",
                                        type.FullName,
                                        type.Assembly.FullName));
            }

            // Property Mapping
            FactoryAttribute playerAttribute = playerAttributes[0];
            var mappings = playerAttribute.GetType().GetCustomAttributes(typeof(FactoryAttributeMappingAttribute), true);

            if (mappings.Length != 1)
            {
                throw new NotSupportedException("The Player Attribute has no valid Property Mapping");
            }

            var  mapping    = mappings[0] as FactoryAttributeMappingAttribute;
            Type playerType = playerAttribute.GetType();

            // Map Name
            PropertyInfo nameProperty = playerType.GetProperty(mapping.NameProperty);

            if (nameProperty == null)
            {
                throw new NotSupportedException("The Name Property from Player Attribute Mapping does not exist");
            }
            string name = (string)nameProperty.GetValue(playerAttribute, null);

            if (string.IsNullOrEmpty(name))
            {
                throw new NotSupportedException("The Name of a Player can't be Empty");
            }

            // Map Author
            PropertyInfo authorProperty = playerType.GetProperty(mapping.AuthorProperty);

            if (authorProperty == null)
            {
                throw new NotSupportedException("The Author Property from Player Attribute Mapping does not exist");
            }
            string author = (string)authorProperty.GetValue(playerAttribute, null);

            if (author == null)
            {
                author = string.Empty;
            }

            PlayerInfo playerInfo = new PlayerInfo()
            {
                Guid   = type.GUID,
                Type   = TypeInfo.FromType(type),
                Name   = name,
                Author = author
            };

            // Faction finden
            // TODO: Richtiger lookup
            playerInfo.FactionType = "";

            // TODO: Stats anhängen
            // if (PlayerStatistics.ContainsKey(playerInfo.

            return(playerInfo);
        }