Save configuration between executions.
This commit is contained in:
159
VAR.ScreenAutomation/Code/Configuration.cs
Normal file
159
VAR.ScreenAutomation/Code/Configuration.cs
Normal file
@@ -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<string, string> _configItems = new Dictionary<string, string>();
|
||||
|
||||
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<string, string> 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");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -52,6 +52,7 @@
|
||||
<Compile Include="Bots\DummyBot.cs" />
|
||||
<Compile Include="Bots\TetrisBot.cs" />
|
||||
<Compile Include="Code\AutomationBotFactory.cs" />
|
||||
<Compile Include="Code\Configuration.cs" />
|
||||
<Compile Include="Code\Mouse.cs" />
|
||||
<Compile Include="Code\Screenshoter.cs" />
|
||||
<Compile Include="Code\WindowHandling.cs" />
|
||||
|
||||
Reference in New Issue
Block a user