From 2fb562de19abcd3cbc7cc24ba4b907e2a1a0a44f Mon Sep 17 00:00:00 2001 From: "Valeriano A.R" Date: Tue, 5 Nov 2019 02:20:19 +0100 Subject: [PATCH] Save configuration between executions. --- VAR.ScreenAutomation/Code/Configuration.cs | 159 ++++++++++++++++++ .../FrmScreenAutomation.Designer.cs | 1 + VAR.ScreenAutomation/FrmScreenAutomation.cs | 31 +++- .../VAR.ScreenAutomation.csproj | 1 + 4 files changed, 190 insertions(+), 2 deletions(-) create mode 100644 VAR.ScreenAutomation/Code/Configuration.cs diff --git a/VAR.ScreenAutomation/Code/Configuration.cs b/VAR.ScreenAutomation/Code/Configuration.cs new file mode 100644 index 0000000..38d3421 --- /dev/null +++ b/VAR.ScreenAutomation/Code/Configuration.cs @@ -0,0 +1,159 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; + +namespace VAR.ScreenAutomation.Code +{ + public class Configuration + { + private Dictionary _configItems = new Dictionary(); + + private string _name = null; + + public Configuration(string name = null) + { + _name = name; + } + + private static string GetConfigFileName(string name = null) + { + string location = System.Reflection.Assembly.GetEntryAssembly().Location; + string path = Path.GetDirectoryName(location); + string filenameWithoutExtension = Path.GetFileNameWithoutExtension(location); + string configFile; + if (string.IsNullOrEmpty(name)) + { + configFile = string.Format("{0}/{1}.cfg", path, filenameWithoutExtension); + } + else + { + configFile = string.Format("{0}/{1}_{2}.cfg", path, filenameWithoutExtension, name); + } + return configFile; + } + + private static string[] GetConfigurationLines(string name = null) + { + string configFile = GetConfigFileName(name); + string[] config; + if (File.Exists(configFile) == false) + { + config = new string[0]; + } + else + { + config = File.ReadAllLines(configFile); + } + return config; + } + + public void Load() + { + _configItems.Clear(); + string[] configLines = GetConfigurationLines(_name); + foreach (string configLine in configLines) + { + int idxSplit = configLine.IndexOf('|'); + if (idxSplit < 0) { continue; } + string configName = configLine.Substring(0, idxSplit); + string configData = configLine.Substring(idxSplit + 1); + + if (_configItems.ContainsKey(configName)) + { + _configItems[configName] = configData; + } + else + { + _configItems.Add(configName, configData); + } + } + } + + public void Save() + { + StringBuilder sbConfig = new StringBuilder(); + foreach (KeyValuePair pair in _configItems) + { + sbConfig.AppendFormat("{0}|{1}\n", pair.Key, pair.Value); + } + string configFileName = GetConfigFileName(_name); + File.WriteAllText(configFileName, sbConfig.ToString()); + } + + public string Get(string key, string defaultValue) + { + if (_configItems == null) { return defaultValue; } + if (_configItems.ContainsKey(key)) + { + return _configItems[key]; + } + return defaultValue; + } + + public int Get(string key, int defaultValue) + { + if (_configItems == null) { return defaultValue; } + if (_configItems.ContainsKey(key)) + { + if (int.TryParse(_configItems[key], out int value)) + { + return value; + } + return defaultValue; + } + return defaultValue; + } + + public bool Get(string key, bool defaultValue) + { + if (_configItems == null) { return defaultValue; } + if (_configItems.ContainsKey(key)) + { + string value = _configItems[key]; + return (value == "true"); + } + return defaultValue; + } + + public void Set(string key, string value) + { + if (_configItems == null) { return; } + if (_configItems.ContainsKey(key)) + { + _configItems[key] = value; + } + else + { + _configItems.Add(key, value); + } + } + + public void Set(string key, int value) + { + if (_configItems == null) { return; } + if (_configItems.ContainsKey(key)) + { + _configItems[key] = Convert.ToString(value); + } + else + { + _configItems.Add(key, Convert.ToString(value)); + } + } + + public void Set(string key, bool value) + { + if (_configItems == null) { return; } + if (_configItems.ContainsKey(key)) + { + _configItems[key] = value ? "true" : "false"; + } + else + { + _configItems.Add(key, value ? "true" : "false"); + } + } + + } +} diff --git a/VAR.ScreenAutomation/FrmScreenAutomation.Designer.cs b/VAR.ScreenAutomation/FrmScreenAutomation.Designer.cs index 9b2f5f8..b970596 100644 --- a/VAR.ScreenAutomation/FrmScreenAutomation.Designer.cs +++ b/VAR.ScreenAutomation/FrmScreenAutomation.Designer.cs @@ -183,6 +183,7 @@ this.Margin = new System.Windows.Forms.Padding(4); this.Name = "FrmScreenAutomation"; this.Text = "ScreenAutomation"; + this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.FrmScreenAutomation_FormClosing); this.Load += new System.EventHandler(this.FrmScreenAutomation_Load); ((System.ComponentModel.ISupportInitialize)(this.picCapturer)).EndInit(); this.splitMain.Panel1.ResumeLayout(false); diff --git a/VAR.ScreenAutomation/FrmScreenAutomation.cs b/VAR.ScreenAutomation/FrmScreenAutomation.cs index 2697599..eb2585b 100644 --- a/VAR.ScreenAutomation/FrmScreenAutomation.cs +++ b/VAR.ScreenAutomation/FrmScreenAutomation.cs @@ -24,18 +24,31 @@ namespace VAR.ScreenAutomation private void FrmScreenAutomation_Load(object sender, EventArgs e) { + var configuration = new Configuration(); + configuration.Load(); + Top = configuration.Get("Top", Top); + Left = configuration.Get("Left", Left); + Width = configuration.Get("Width", Width); + Height = configuration.Get("Height", Height); + splitMain.SplitterDistance = configuration.Get("splitMain.SplitterDistance", splitMain.SplitterDistance); + splitOutput.SplitterDistance = configuration.Get("splitOutput.SplitterDistance", splitOutput.SplitterDistance); + SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.OptimizedDoubleBuffer, true); SetStyle(ControlStyles.SupportsTransparentBackColor, true); TransparencyKey = Color.LimeGreen; picCapturer.BackColor = Color.LimeGreen; - ddlAutomationBot.Items.AddRange(AutomationBotFactory.GetAllAutomationBots()); - ddlAutomationBot.SelectedIndex = 0; + ddlAutomationBot.SelectedItem = configuration.Get("ddlAutomationBot", (string)ddlAutomationBot.SelectedItem); + if (ddlAutomationBot.SelectedIndex < 0) + { + ddlAutomationBot.SelectedIndex = 0; + } _automationBot = AutomationBotFactory.CreateFromName((string)ddlAutomationBot.SelectedItem); _automationBot?.Init(ctrOutput); + numFPS.Value = configuration.Get("numFPS", (int)numFPS.Value); if (components == null) { components = new Container(); } timTicker = new Timer(components) @@ -49,6 +62,20 @@ namespace VAR.ScreenAutomation WindowHandling.WindowSetTopLevel(this); } + private void FrmScreenAutomation_FormClosing(object sender, FormClosingEventArgs e) + { + var configuration = new Configuration(); + configuration.Set("Top", Top); + configuration.Set("Left", Left); + configuration.Set("Width", Width); + configuration.Set("Height", Height); + configuration.Set("splitMain.SplitterDistance", splitMain.SplitterDistance); + configuration.Set("splitOutput.SplitterDistance", splitOutput.SplitterDistance); + configuration.Set("ddlAutomationBot", (string)ddlAutomationBot.SelectedItem); + configuration.Set("numFPS", (int)numFPS.Value); + configuration.Save(); + } + private void TimTicker_Tick(object sender, EventArgs e) { timTicker.Enabled = false; diff --git a/VAR.ScreenAutomation/VAR.ScreenAutomation.csproj b/VAR.ScreenAutomation/VAR.ScreenAutomation.csproj index 91ef47e..36975ae 100644 --- a/VAR.ScreenAutomation/VAR.ScreenAutomation.csproj +++ b/VAR.ScreenAutomation/VAR.ScreenAutomation.csproj @@ -52,6 +52,7 @@ +