Files @ 41381c24d35a
Branch filter:

Location: ATITD-Tools/Desert-Paint-Lab/Settings.cs - annotation

Jason Maltzen
Now supports all the interface sizes with a setting to select the current interface size. The initial screen size check now displays the detected resolution as a hint. The screen size check / interface size settings can now be updated after launch through File->Preferences. Capturing a reaction now includes a progress bar, and runs in a separate thread instead of silently blocking. The reaction status window under 'Help' now has options to disable ingredients to remove them from the list. NOTE: this also disables/enables those ingredients in the recipe generator as well. The list also updates as new reactions are recorded instead of requiring that it be closed and re-opened to update.
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;

namespace DesertPaintLab
{
    public class Settings
    {
        public Settings()
        {
        }

        private Dictionary<string, string> _settings = new Dictionary<string, string>();

        public void Get(string key, out int value)
        {
            value = 0;
            string valStr;
            if ( _settings.TryGetValue(key.ToLower(), out valStr) )
            {
                Int32.TryParse(valStr, out value);
            }
        }
        public void Get(string key, out bool value)
        {
            value = false;
            string valStr;
            if ( _settings.TryGetValue(key.ToLower(), out valStr) )
            {
                Boolean.TryParse(valStr, out value);
            }
        }
        public void Set(string key, int value)
        {
            _settings[key.ToLower()] = value.ToString();
        }
        public void Set(string key, bool value)
        {
            _settings[key.ToLower()] = value.ToString();
        }

        public void Reset()
        {
            _settings.Clear();
        }

        public void Save(string settingsPath)
        {
            using (StreamWriter writer = new StreamWriter(settingsPath))
            {
                foreach (KeyValuePair<string, string> pair in _settings)
                {
                    writer.WriteLine("{0}={1}", pair.Key, pair.Value);
                }
            }
        }
        
        static Regex optionEntry = new Regex(@"(?<opt>[^#=][^=]*)=(?<optval>.*)$");
        public bool Load(string settingsPath)
        {
            if (System.IO.File.Exists(settingsPath))
            {
                string line;
                Match match;
                using (StreamReader reader = new StreamReader(settingsPath))
                {
                    while ((line = reader.ReadLine()) != null) 
                    {
                        match = optionEntry.Match(line);
                        if (match.Success)
                        {
                            String optName = match.Groups["opt"].Value.ToLower();
                            String optVal = match.Groups["optval"].Value.Trim();
                            if (optName.Equals("debug"))
                            {
                                // convert
                                optName = "enabledebugmenu";
                            }
                            _settings[optName.ToLower()] = optVal;
                        }
                    }
                }
                return true;
            }
            return false;
        }
    }
}