<commit_before>﻿/*
* (C) Copyright 2014, <NAME>
* 
* Kerbal Space Program is Copyright (C) 2013 Squad. See http://kerbalspaceprogram.com/. This
* project is in no way associated with nor endorsed by Squad.
* 
* This code is licensed under the Apache License Version 2.0. See the LICENSE.txt
* file for more information.
*/

using System.Collections.Generic;
using System.Collections;
using System;
using System.IO;
using UnityEngine;

namespace PlanetShine
{

    public sealed class Config
    {
        private static readonly Config instance = new Config();

        private Config(){}

        public static Config Instance
        {
            get 
            {
                return instance; 
            }
        }

        public static string[] qualityLabels = {"Low", "Medium", "High"};
        public static int maxAlbedoLightsQuantity = 4;

        public int quality { get; private set; }
        public bool useVertex = false;
        public int albedoLightsQuantity = 4;
        public float baseAlbedoIntensity = 0.2f;
        public float vacuumLightLevel = 0.02f;
        public float baseGroundAmbient = 0.50f;
        public float groundAmbientOverrideRatio = 0.5f;
        public float minAlbedoFadeAltitude = 0.02f;
        public float maxAlbedoFadeAltitude = 0.10f;
        public float minAmbientFadeAltitude = 0.00f;
        public float maxAmbientFadeAltitude = 0.08f;
        public float albedoRange = 8f;
        public bool debug = false;
        public int updateFrequency = 1;
        public Dictionary<CelestialBody, CelestialBodyInfo> celestialBodyInfos = new Dictionary<CelestialBody, CelestialBodyInfo>();

        public void setQuality(int selectedQuality)
        {
            quality = selectedQuality;
            switch (selectedQuality) {
            case 0:
                albedoLightsQuantity = 1;
                useVertex = true;
                updateFrequency = 5;
                break;
            case 1:
                albedoLightsQuantity = maxAlbedoLightsQuantity;
                useVertex = true;
                updateFrequency = 2;
                break;
            case 2:
                albedoLightsQuantity = maxAlbedoLightsQuantity;
                useVertex = false;
                updateFrequency = 1;
                break;
            default:
                break;
            }
        }
    }


    public class ConfigDefaults
    {
        private ConfigDefaults(){}

        public static float baseAlbedoIntensity = 0.2f;
        public static float vacuumLightLevel = 0.02f;
        public static float baseGroundAmbient = 0.50f;
        public static float groundAmbientOverrideRatio = 0.5f;
        public static float minAlbedoFadeAltitude = 0.02f;
        public static float maxAlbedoFadeAltitude = 0.10f;
        public static float minAmbientFadeAltitude = 0.00f;
        public static float maxAmbientFadeAltitude = 0.08f;
        public static float albedoRange = 8f;
    }

    
    [KSPAddon(KSPAddon.Startup.EveryScene, false)]
    public class ConfigManager : MonoBehaviour
    {
        public static ConfigManager Instance { get; private set; }
        private Config config = Config.Instance;
        private ConfigNode configFile;
        private ConfigNode configFileNode;

        public void Start()
        {
            if (Instance != null)
                Destroy (Instance.gameObject);
            Instance = this;

            LoadSettings ();
        }
            
        public void LoadSettings()
        {
            configFile = ConfigNode.Load(KSPUtil.ApplicationRootPath + "GameData/PlanetShine/Config/Settings.cfg");
            configFileNode = configFile.GetNode("PlanetShine");

            if (bool.Parse (configFileNode.GetValue ("useAreaLight")))
                config.albedoLightsQuantity = Config.maxAlbedoLightsQuantity;
            else
                config.albedoLightsQuantity = 1;

            config.baseAlbedoIntensity = float.Parse(configFileNode.GetValue("baseAlbedoIntensity"));
            config.vacuumLightLevel = float.Parse(configFileNode.GetValue("vacuumLightLevel"));
            config.baseGroundAmbient = float.Parse(configFileNode.GetValue("baseGroundAmbient"));
            config.groundAmbientOverrideRatio = float.Parse(configFileNode.GetValue("groundAmbientOverrideRatio"));
            config.minAlbedoFadeAltitude = float.Parse(configFileNode.GetValue("minAlbedoFadeAltitude"));
            config.maxAlbedoFadeAltitude = float.Parse(configFileNode.GetValue("maxAlbedoFadeAltitude"));
            config.minAmbientFadeAltitude = float.Parse(configFileNode.GetValue("minAmbientFadeAltitude"));
            config.maxAmbientFadeAltitude = float.Parse(configFileNode.GetValue("maxAmbientFadeAltitude"));
            config.albedoRange = float.Parse(configFileNode.GetValue("albedoRange"));
            config.useVertex = bool.Parse(configFileNode.GetValue("useVertex"));
            config.updateFrequency = int.Parse(configFileNode.GetValue("updateFrequency"));
            config.setQuality(int.Parse(configFileNode.GetValue("quality")));

            foreach (ConfigNode bodySettings in GameDatabase.Instance.GetConfigNodes("CelestialBodyColor"))
            {
                CelestialBody body = FlightGlobals.Bodies.Find(n => n.name == bodySettings.GetValue("name"));
                if (FlightGlobals.Bodies.Contains(body))
                {
                    Color color = ConfigNode.ParseColor(bodySettings.GetValue("color"))
                        * float.Parse(bodySettings.GetValue("intensity"));
                    color.r = (color.r / 255f);
                    color.g = (color.g / 255f);
                    color.b = (color.b / 255f);
                    color.a = 1;
                    if (!config.celestialBodyInfos.ContainsKey(body))
                        config.celestialBodyInfos.Add(body, new CelestialBodyInfo
                                                      (
                                                       color,
                                                       float.Parse(bodySettings.GetValue("intensity")),
                                                       float.Parse(bodySettings.GetValue("atmosphereAmbient")),
                                                       float.Parse(bodySettings.GetValue("groundAmbientOverride"))
                                                       ));
                }
            }
        }

        public void SaveSettings()
        {
            configFileNode.SetValue("useAreaLight", (config.albedoLightsQuantity > 1) ? "True" : "False");
            configFileNode.SetValue("baseAlbedoIntensity", config.baseAlbedoIntensity.ToString());
            configFileNode.SetValue("vacuumLightLevel", config.vacuumLightLevel.ToString());
            configFileNode.SetValue("baseGroundAmbient", config.baseGroundAmbient.ToString());
            configFileNode.SetValue("groundAmbientOverrideRatio", config.groundAmbientOverrideRatio.ToString());
            configFileNode.SetValue("minAlbedoFadeAltitude", config.minAlbedoFadeAltitude.ToString());
            configFileNode.SetValue("maxAlbedoFadeAltitude", config.maxAlbedoFadeAltitude.ToString());
            configFileNode.SetValue("minAmbientFadeAltitude", config.minAmbientFadeAltitude.ToString());
            configFileNode.SetValue("maxAmbientFadeAltitude", config.maxAmbientFadeAltitude.ToString());
            configFileNode.SetValue("albedoRange", config.albedoRange.ToString());
            configFileNode.SetValue("useVertex", config.useVertex ? "True" : "False");
            configFileNode.SetValue("updatefrequency", config.updateFrequency.ToString());
            configFileNode.SetValue("quality", config.quality.ToString());
            configFile.Save(KSPUtil.ApplicationRootPath + "GameData/PlanetShine/Config/Settings.cfg");
        }

    }
}

<commit_msg>Make LoadSettings more robust<commit_after>﻿/*
* (C) Copyright 2014, <NAME>
* 
* Kerbal Space Program is Copyright (C) 2013 Squad. See http://kerbalspaceprogram.com/. This
* project is in no way associated with nor endorsed by Squad.
* 
* This code is licensed under the Apache License Version 2.0. See the LICENSE.txt
* file for more information.
*/

using System.Collections.Generic;
using System.Collections;
using System;
using System.IO;
using UnityEngine;

namespace PlanetShine
{

    public sealed class Config
    {
        private static readonly Config instance = new Config();

        private Config(){}

        public static Config Instance
        {
            get 
            {
                return instance; 
            }
        }

        public static string[] qualityLabels = {"Low", "Medium", "High"};
        public static int maxAlbedoLightsQuantity = 4;

        public int quality { get; private set; }
        public bool useVertex = false;
        public int albedoLightsQuantity = 4;
        public float baseAlbedoIntensity = 0.2f;
        public float vacuumLightLevel = 0.02f;
        public float baseGroundAmbient = 0.50f;
        public float groundAmbientOverrideRatio = 0.5f;
        public float minAlbedoFadeAltitude = 0.02f;
        public float maxAlbedoFadeAltitude = 0.10f;
        public float minAmbientFadeAltitude = 0.00f;
        public float maxAmbientFadeAltitude = 0.08f;
        public float albedoRange = 8f;
        public bool debug = false;
        public int updateFrequency = 1;
        public Dictionary<CelestialBody, CelestialBodyInfo> celestialBodyInfos = new Dictionary<CelestialBody, CelestialBodyInfo>();

        public void setQuality(int selectedQuality)
        {
            quality = selectedQuality;
            switch (selectedQuality) {
            case 0:
                albedoLightsQuantity = 1;
                useVertex = true;
                updateFrequency = 5;
                break;
            case 1:
                albedoLightsQuantity = maxAlbedoLightsQuantity;
                useVertex = true;
                updateFrequency = 2;
                break;
            case 2:
                albedoLightsQuantity = maxAlbedoLightsQuantity;
                useVertex = false;
                updateFrequency = 1;
                break;
            default:
                break;
            }
        }
    }


    public class ConfigDefaults
    {
        private ConfigDefaults(){}

        public static float baseAlbedoIntensity = 0.2f;
        public static float vacuumLightLevel = 0.02f;
        public static float baseGroundAmbient = 0.50f;
        public static float groundAmbientOverrideRatio = 0.5f;
        public static float minAlbedoFadeAltitude = 0.02f;
        public static float maxAlbedoFadeAltitude = 0.10f;
        public static float minAmbientFadeAltitude = 0.00f;
        public static float maxAmbientFadeAltitude = 0.08f;
        public static float albedoRange = 8f;
    }

    
    [KSPAddon(KSPAddon.Startup.EveryScene, false)]
    public class ConfigManager : MonoBehaviour
    {
        public static ConfigManager Instance { get; private set; }
        private Config config = Config.Instance;
        private ConfigNode configFile;
        private ConfigNode configFileNode;

        public void Start()
        {
            if (Instance != null)
                Destroy (Instance.gameObject);
            Instance = this;

            LoadSettings ();
        }
            
        public void LoadSettings()
        {
            configFile = ConfigNode.Load(KSPUtil.ApplicationRootPath + "GameData/PlanetShine/Config/Settings.cfg");
            configFileNode = configFile.GetNode("PlanetShine");

            if (bool.Parse (configFileNode.GetValue ("useAreaLight")))
                config.albedoLightsQuantity = Config.maxAlbedoLightsQuantity;
            else
                config.albedoLightsQuantity = 1;

            config.baseAlbedoIntensity = float.Parse(configFileNode.GetValue("baseAlbedoIntensity"));
            config.vacuumLightLevel = float.Parse(configFileNode.GetValue("vacuumLightLevel"));
            config.baseGroundAmbient = float.Parse(configFileNode.GetValue("baseGroundAmbient"));
            config.groundAmbientOverrideRatio = float.Parse(configFileNode.GetValue("groundAmbientOverrideRatio"));
            config.minAlbedoFadeAltitude = float.Parse(configFileNode.GetValue("minAlbedoFadeAltitude"));
            config.maxAlbedoFadeAltitude = float.Parse(configFileNode.GetValue("maxAlbedoFadeAltitude"));
            config.minAmbientFadeAltitude = float.Parse(configFileNode.GetValue("minAmbientFadeAltitude"));
            config.maxAmbientFadeAltitude = float.Parse(configFileNode.GetValue("maxAmbientFadeAltitude"));
            config.albedoRange = float.Parse(configFileNode.GetValue("albedoRange"));
            config.useVertex = bool.Parse(configFileNode.GetValue("useVertex"));
            config.updateFrequency = int.Parse(configFileNode.GetValue("updateFrequency"));
            config.setQuality(int.Parse(configFileNode.GetValue("quality")));

            foreach (ConfigNode bodySettings in GameDatabase.Instance.GetConfigNodes("CelestialBodyColor"))
            {
                try
                {
                    CelestialBody body = FlightGlobals.Bodies.Find(n => n.name == bodySettings.GetValue("name"));
                    if (FlightGlobals.Bodies.Contains(body))
                    {
                        Color color = ConfigNode.ParseColor(bodySettings.GetValue("color"))
                            * float.Parse(bodySettings.GetValue("intensity"));
                        color.r = (color.r / 255f);
                        color.g = (color.g / 255f);
                        color.b = (color.b / 255f);
                        color.a = 1;
                        if (!config.celestialBodyInfos.ContainsKey(body))
                            config.celestialBodyInfos.Add(body, new CelestialBodyInfo
                                                          (
                                                           color,
                                                           float.Parse(bodySettings.GetValue("intensity")),
                                                           float.Parse(bodySettings.GetValue("atmosphereAmbient")),
                                                           float.Parse(bodySettings.GetValue("groundAmbientOverride"))
                                                           ));
                    }
                }
                catch(Exception e)
                {
                    Debug.LogError(String.Format(
                        "[PlanetShine] An exception occured reading CelestialBodyColor node:\n{0}\nThe exception was:\n{1}",
                        bodySettings,
                        e
                    ));
                }
            }
        }

        public void SaveSettings()
        {
            configFileNode.SetValue("useAreaLight", (config.albedoLightsQuantity > 1) ? "True" : "False");
            configFileNode.SetValue("baseAlbedoIntensity", config.baseAlbedoIntensity.ToString());
            configFileNode.SetValue("vacuumLightLevel", config.vacuumLightLevel.ToString());
            configFileNode.SetValue("baseGroundAmbient", config.baseGroundAmbient.ToString());
            configFileNode.SetValue("groundAmbientOverrideRatio", config.groundAmbientOverrideRatio.ToString());
            configFileNode.SetValue("minAlbedoFadeAltitude", config.minAlbedoFadeAltitude.ToString());
            configFileNode.SetValue("maxAlbedoFadeAltitude", config.maxAlbedoFadeAltitude.ToString());
            configFileNode.SetValue("minAmbientFadeAltitude", config.minAmbientFadeAltitude.ToString());
            configFileNode.SetValue("maxAmbientFadeAltitude", config.maxAmbientFadeAltitude.ToString());
            configFileNode.SetValue("albedoRange", config.albedoRange.ToString());
            configFileNode.SetValue("useVertex", config.useVertex ? "True" : "False");
            configFileNode.SetValue("updatefrequency", config.updateFrequency.ToString());
            configFileNode.SetValue("quality", config.quality.ToString());
            configFile.Save(KSPUtil.ApplicationRootPath + "GameData/PlanetShine/Config/Settings.cfg");
        }

    }
}


{'fraction_non_alphanumeric': 0.08458160380196114, 'fraction_numerical': 0.009504902845455092, 'mean_word_length': 1.9164912280701754, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6304333', 'n_tokens_mistral': 4532, 'n_tokens_neox': 4065, 'n_words': 790}