Beispiel #1
0
        /// <summary>
        /// Called when the component is activated
        /// </summary>
        void Start()
        {
            // Get all types who extend a Tweaker Interface and add them to the storage
            AssemblyLoader.loadedAssemblies.TypeOperation(type =>
            {
                if (typeof(IPQSModTweaker).IsAssignableFrom(type) && !type.IsAbstract)
                {
                    IPQSModTweaker tweaker = (IPQSModTweaker)Activator.CreateInstance(type);
                    PQSModTweakers.Add(tweaker);

                    // Get the config
                    String configNodeName = tweaker.GetConfig();
                    if (!ConfigCache.ContainsKey(configNodeName))
                    {
                        ConfigNode config = GameDatabase.Instance.GetConfigs(configNodeName)[0].config;
                        ConfigCache.Add(configNodeName, config);
                    }
                }
                if (typeof(ICelestialBodyTweaker).IsAssignableFrom(type) && !type.IsAbstract)
                {
                    ICelestialBodyTweaker tweaker = (ICelestialBodyTweaker)Activator.CreateInstance(type);
                    CBTweakers.Add(tweaker);

                    // Get the config
                    String configNodeName = tweaker.GetConfig();
                    if (!ConfigCache.ContainsKey(configNodeName))
                    {
                        ConfigNode config = GameDatabase.Instance.GetConfigs(configNodeName)[0].config;
                        ConfigCache.Add(configNodeName, config);
                    }
                }
                if (typeof(IPQSTweaker).IsAssignableFrom(type) && !type.IsAbstract)
                {
                    IPQSTweaker tweaker = (IPQSTweaker)Activator.CreateInstance(type);
                    PQSTweakers.Add(tweaker);

                    // Get the config
                    String configNodeName = tweaker.GetConfig();
                    if (!ConfigCache.ContainsKey(configNodeName))
                    {
                        ConfigNode config = GameDatabase.Instance.GetConfigs(configNodeName)[0].config;
                        ConfigCache.Add(configNodeName, config);
                    }
                }
            });

            // Get the blacklist
            bodyBlacklist = GameDatabase.Instance.GetConfigs("PD_BODY_BLACKLIST")[0].config;

            // Register the callback for manipulating the system
            GameEvents.onGameSceneSwitchRequested.Add(OnGameSceneSwitchRequested);
            GameEvents.onLevelWasLoaded.Add(OnLevelWasLoaded);
        }
Beispiel #2
0
        /// <summary>
        /// Gets called when the users switches from one game scene to another one.
        /// </summary>
        void OnGameSceneSwitchRequested(GameEvents.FromToAction <GameScenes, GameScenes> action)
        {
            // Are we loading a game?
            if (action.from == GameScenes.MAINMENU && action.to == GameScenes.SPACECENTER)
            {
                // Get a sorted list of bodies
                List <CelestialBody> bodies = Utility.GetSortedBodies();

                // Tweak it!
                for (Int32 j = 0; j < bodies.Count; j++)
                {
                    // Get the Body
                    CelestialBody body = bodies[j];

                    // Is this body blacklisted?
                    if (bodyBlacklist != null)
                    {
                        if (bodyBlacklist.GetValues("blacklist").Any(b => body.bodyName == b))
                        {
                            continue;
                        }
                    }

                    // Was the body edited?
                    Boolean edited = false;

                    // Tweak the PQS itself
                    for (Int32 i = 0; i < PQSTweakers.Count; i++)
                    {
                        // Tweaker
                        IPQSTweaker tweaker = PQSTweakers[i];

                        // Check the config
                        ConfigNode config = ConfigCache[tweaker.GetConfig()];

                        // Is the tweak group enabled?
                        if (!config.HasValue("enabled"))
                        {
                            continue;
                        }
                        if (!Boolean.TryParse(config.GetValue("enabled"), out Boolean isEnabled) || !isEnabled)
                        {
                            continue;
                        }

                        // Is the tweak itself enabled?
                        String setting = tweaker.GetSetting();
                        if (setting != null)
                        {
                            if (!config.HasValue(setting))
                            {
                                continue;
                            }
                            if (!Boolean.TryParse(config.GetValue(setting), out isEnabled) || !isEnabled)
                            {
                                continue;
                            }
                        }

                        // Tweak it
                        if (tweaker.Tweak(body, body.pqsController))
                        {
                            edited = true;
                        }
                    }

                    // Get the PQSMods
                    PQSMod[] mods = body.GetComponentsInChildren <PQSMod>(true);

                    for (Int32 i = 0; i < PQSModTweakers.Count; i++)
                    {
                        // Tweaker
                        IPQSModTweaker tweaker = PQSModTweakers[i];

                        // Check the config
                        ConfigNode config = ConfigCache[tweaker.GetConfig()];

                        // Is the tweak group enabled?
                        if (!config.HasValue("enabled"))
                        {
                            continue;
                        }
                        if (!Boolean.TryParse(config.GetValue("enabled"), out Boolean isEnabled) || !isEnabled)
                        {
                            continue;
                        }

                        // Is the tweak itself enabled?
                        String setting = tweaker.GetSetting();
                        if (setting != null)
                        {
                            if (!config.HasValue(setting))
                            {
                                continue;
                            }
                            if (!Boolean.TryParse(config.GetValue(setting), out isEnabled) || !isEnabled)
                            {
                                continue;
                            }
                        }

                        // Tweak them
                        foreach (PQSMod mod in mods)
                        {
                            if (tweaker.Tweak(body, mod))
                            {
                                edited = true;
                                mod.OnSetup();
                            }
                        }
                    }

                    // The body was edited, we should update it's scaled space
                    if (edited && scaledSpaceUpdate.All(b => b.name != body.name))
                    {
                        scaledSpaceUpdate.Add(body);
                    }
                }

                // Tweak Celestial Bodies
                for (Int32 i = 0; i < CBTweakers.Count; i++)
                {
                    // Tweaker
                    ICelestialBodyTweaker tweaker = CBTweakers[i];

                    // Check the config
                    ConfigNode config = ConfigCache[tweaker.GetConfig()];

                    // Is the tweak group enabled?
                    if (!config.HasValue("enabled"))
                    {
                        continue;
                    }
                    if (!Boolean.TryParse(config.GetValue("enabled"), out Boolean isEnabled) || !isEnabled)
                    {
                        continue;
                    }

                    // Is the tweak itself enabled?
                    String setting = tweaker.GetSetting();
                    if (setting != null)
                    {
                        if (!config.HasValue(setting))
                        {
                            continue;
                        }
                        if (!Boolean.TryParse(config.GetValue(setting), out isEnabled) || !isEnabled)
                        {
                            continue;
                        }
                    }

                    // Tweak it!
                    for (Int32 j = 0; j < bodies.Count; j++)
                    {
                        // Get the Body
                        CelestialBody body = bodies[j];

                        // Tweak it
                        tweaker.Tweak(body);
                    }
                }
            }

            // Are we leaving the game?
            if (action.to == GameScenes.MAINMENU)
            {
                if (PSystemManager.Instance?.localBodies == null)
                {
                    return;
                }

                // Reset the random
                RandomProvider.Reset();

                // Kill the PQS so it definitly rebuilds when we load a game
                for (Int32 i = 0; i < PSystemManager.Instance.localBodies.Count; i++)
                {
                    CelestialBody body = PSystemManager.Instance.localBodies[i];
                    body.pqsController?.ResetSphere();
                }
            }
        }