/// <summary>
        /// Loads all Custom Asteroids settings. The class is in a consistent state in the event of
        /// an exception.
        /// </summary>
        static AsteroidManager()
        {
            try {
                curOptions  = Options.load();
                allowedPops = PopulationLoader.load();
                knownFrames = ReferenceLoader.load();

                Debug.Log("[CustomAsteroids]: "
                          + Localizer.Format("#autoLOC_CustomAsteroids_LogLoadedStatus",
                                             allowedPops.getTotalRate()));
            } catch {
                // Ensure the contents of AsteroidManager are predictable even in the event of an
                // exception. Though an exception thrown by a static constructor is basically
                // unrecoverable...
                curOptions  = null;
                allowedPops = null;
                knownFrames = null;
                throw;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Factory method obtaining Custom Asteroids settings from KSP config state.
        /// </summary>
        ///
        /// <returns>A newly constructed PopulationLoader object containing a full list
        /// of all valid asteroid groups in asteroid config files.</returns>
        ///
        /// <exception cref="TypeInitializationException">Thrown if the PopulationLoader
        /// object could not be constructed. The program is in a consistent state in the event of
        /// an exception.</exception>
        internal static PopulationLoader load()
        {
            try {
                // Start with an empty population list
                PopulationLoader allPops = new PopulationLoader();

                // Search for populations in all config files
                UrlDir.UrlConfig [] configList = GameDatabase.Instance.GetConfigs("AsteroidSets");
                foreach (UrlDir.UrlConfig curSet in configList)
                {
                    foreach (ConfigNode curNode in curSet.config.nodes)
                    {
#if DEBUG
                        Debug.Log("[CustomAsteroids]: "
                                  + Localizer.Format("#autoLOC_CustomAsteroids_LogConfig", curNode));
#endif
                        try {
                            AsteroidSet pop = null;
                            switch (curNode.name)
                            {
                            case "ASTEROIDGROUP":
                                pop = new Population();
                                break;

                            case "INTERCEPT":
                                pop = new Flyby();
                                break;

                            case "DEFAULT":
#pragma warning disable 0618 // DefaultAsteroids is deprecated
                                pop = new DefaultAsteroids();
#pragma warning restore 0618
                                break;
                                // silently ignore any other nodes present
                            }
                            if (pop != null)
                            {
                                ConfigNode.LoadObjectFromConfig(pop, curNode);
                                allPops.asteroidPops.Add(pop);
                            }
                        } catch (Exception e) {
                            var nodeName = curNode.GetValue("name");
                            var error    = Localizer.Format(
                                "#autoLOC_CustomAsteroids_ErrorLoadGroup", nodeName);
                            Debug.LogError($"[CustomAsteroids]: " + error);
                            Debug.LogException(e);
                            Util.errorToPlayer(e, error);
                        }   // Attempt to parse remaining populations
                    }
                }

#if DEBUG
                foreach (AsteroidSet x in allPops.asteroidPops)
                {
                    Debug.Log("[CustomAsteroids]: "
                              + Localizer.Format("#autoLOC_CustomAsteroids_LogLoadGroup", x));
                }
#endif

                if (allPops.asteroidPops.Count == 0)
                {
                    Debug.LogWarning("[CustomAsteroids]: "
                                     + Localizer.Format("#autoLOC_CustomAsteroids_ErrorNoConfig1"));
                    ScreenMessages.PostScreenMessage(
                        Localizer.Format("#autoLOC_CustomAsteroids_ErrorNoConfig1") + "\n"
                        + Localizer.Format("#autoLOC_CustomAsteroids_ErrorNoConfig2"),
                        10.0f, ScreenMessageStyle.UPPER_CENTER);
                }

                return(allPops);
            } catch (Exception e) {
                throw new TypeInitializationException(
                          "Starstrider42.CustomAsteroids.PopulationLoader",
                          e);
            }
        }