Exemple #1
0
        /// <summary>
        /// Liest alle notwendigen Informationen aus dem gegebenen Typ aus und
        /// liefert das gesammelte CampaignInfo.
        /// </summary>
        /// <param name="type">Type</param>
        /// <returns>Infos über die Campaign</returns>
        private static CampaignInfo AnalyseCampaignType(Type type)
        {
            CampaignDescriptionAttribute[] descriptionAttributes =
                (CampaignDescriptionAttribute[])type.GetCustomAttributes(
                    typeof(CampaignDescriptionAttribute), false);

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

            Campaign campaign = Activator.CreateInstance(type) as Campaign;

            // Config setzen, falls vorhanden
            CampaignStatistics stats = null;

            if (campaignStatistics.TryGetValue(campaign.Guid, out stats))
            {
                campaign.Settings = stats.Settings;
            }

            CampaignInfo campaignInfo = new CampaignInfo();

            campaignInfo.Guid                 = campaign.Guid;
            campaignInfo.Name                 = campaign.Name;
            campaignInfo.Description          = campaign.Description;
            campaignInfo.Picture              = campaign.Picture;
            campaignInfo.Type                 = TypeInfo.FromType(type);
            campaignInfo.DescriptionAttribute = descriptionAttributes[0];

            // List all unlocked Levels
            foreach (var level in campaign.GetUnlockedLevels())
            {
                LevelInfo info = AnalyseLevelType(level);

                // Sicherstellen, dass Level-Anforderungen passen
                if (info.LevelDescription.MinPlayerCount > 1)
                {
                    throw new Exception("Level is not playable alone");
                }

                campaignInfo.Levels.Add(info);
            }

            // Stats anhängen
            if (!campaignStatistics.ContainsKey(campaign.Guid))
            {
                CampaignStatistics statistics = new CampaignStatistics()
                {
                    Guid     = campaign.Guid,
                    Settings = campaign.Settings
                };
                campaignStatistics.Add(campaign.Guid, statistics);
            }
            campaignInfo.Statistics = campaignStatistics[campaign.Guid];

            return(campaignInfo);
        }
Exemple #2
0
        /// <summary>
        /// Scans the given Assembly for potential stuff like Campaigns, Levels and Players.
        /// </summary>
        /// <param name="assembly">Assembly to search in</param>
        /// <param name="campaign">Scan for new Campaigns</param>
        /// <param name="level">Scan for new Levels</param>
        /// <param name="player">Scan for new Players</param>
        /// <returns>Scan Results</returns>
        internal static LoaderInfo AnalyseAssembly(Assembly assembly, bool level, bool campaign, bool player)
        {
            LoaderInfo loaderInfo = new LoaderInfo();
            bool       isStatic   = false;

            foreach (var type in assembly.GetTypes())
            {
                // Static-Flag abfragen
                // TODO: Abhängigkeiten in andere Assemblies müssen hier auch aufgegriffen werden
                var members = type.GetMembers(BindingFlags.Static);
                isStatic |= members.Length > 0;

                if (type.IsClass &&
                    type.IsPublic &&
                    !type.IsAbstract)
                {
                    // Found Level
                    if (level && type.IsSubclassOf(typeof(Level)))
                    {
                        try
                        {
                            LevelInfo levelInfo = AnalyseLevelType(type);
                            loaderInfo.Levels.Add(levelInfo);
                        }
                        catch (Exception ex)
                        {
                            loaderInfo.Errors.Add(ex);
                        }
                    }

                    // Found Campaign
                    if (campaign && type.IsSubclassOf(typeof(Campaign)))
                    {
                        try
                        {
                            CampaignInfo campaignInfo = AnalyseCampaignType(type);
                            loaderInfo.Campaigns.Add(campaignInfo);
                        }
                        catch (Exception ex)
                        {
                            loaderInfo.Errors.Add(ex);
                        }
                    }

                    // Found Player (Ignorieren, falls Faction-Liste null)
                    if (player && type.GetCustomAttributes(typeof(FactoryAttribute), true).Length > 0)
                    {
                        try
                        {
                            PlayerInfo playerInfo = AnalysePlayerType(type);
                            loaderInfo.Players.Add(playerInfo);
                        }
                        catch (Exception ex)
                        {
                            loaderInfo.Errors.Add(ex);
                        }
                    }

                    // Found Extender
                    if (type.GetInterface("IExtender") != null)
                    {
                        Type[] interfaces = type.GetInterfaces();
                    }
                }
            }

            // Static Flag nachtragen
            foreach (var item in loaderInfo.Players)
            {
                item.IsStatic = isStatic;
            }

            return(loaderInfo);
        }