Replace AutomationBotFactory with one using BaseFactory<>

This commit is contained in:
2022-04-09 03:25:25 +02:00
parent 44d6531bda
commit d0fc1235a9
7 changed files with 20 additions and 73 deletions

View File

@@ -1,66 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using VAR.ScreenAutomation.Bots;
using VAR.ScreenAutomation.Interfaces;
namespace VAR.ScreenAutomation.Code
{
public static class AutomationBotFactory
{
private static Dictionary<string, Type> _dictAutomationBots;
private static Dictionary<string, Type> GetDict()
{
if (_dictAutomationBots != null)
{
return _dictAutomationBots;
}
Type iAutomationBot = typeof(IAutomationBot);
IEnumerable<Type> automationBotTypes = AppDomain.CurrentDomain
.GetAssemblies()
.SelectMany(x => x.GetTypes())
.Where(x =>
x.IsAbstract == false &&
x.IsInterface == false &&
iAutomationBot.IsAssignableFrom(x) &&
true);
_dictAutomationBots = automationBotTypes.ToDictionary(t =>
{
IAutomationBot automationBot =
System.Runtime.Serialization.FormatterServices.GetUninitializedObject(t) as IAutomationBot;
return automationBot?.Name ?? t.Name;
});
return _dictAutomationBots;
}
public static object[] GetAllAutomationBots()
{
Dictionary<string, Type> dict = GetDict();
string[] allAutomationBots = dict.Select(p => p.Key).ToArray();
return allAutomationBots.ToArray<object>();
}
public static IAutomationBot CreateFromName(string name)
{
Dictionary<string, Type> dict = GetDict();
if (string.IsNullOrEmpty(name))
{
return new DummyBot();
}
if (dict.ContainsKey(name) == false)
{
throw new NotImplementedException($"Can't create IAutomationBot with this name: {name}");
}
Type proxyCmdExecutorType = dict[name];
IAutomationBot automationBot = Activator.CreateInstance(proxyCmdExecutorType) as IAutomationBot;
return automationBot;
}
}
}

View File

@@ -0,0 +1,8 @@
using VAR.Toolbox.Code.Bots;
namespace VAR.Toolbox.Code
{
public class AutomationBotFactory: BaseFactory<IAutomationBot>
{
}
}

View File

@@ -1,7 +1,7 @@
using System.Drawing;
using VAR.ScreenAutomation.Interfaces;
namespace VAR.ScreenAutomation.Bots
namespace VAR.Toolbox.Code.Bots
{
public class DummyBot : IAutomationBot
{

View File

@@ -1,10 +1,10 @@
using System.Drawing;
using VAR.ScreenAutomation.Interfaces;
namespace VAR.ScreenAutomation.Interfaces
namespace VAR.Toolbox.Code.Bots
{
public interface IAutomationBot
public interface IAutomationBot: INamed
{
string Name { get; }
IConfiguration GetDefaultConfiguration();
void Init(VAR.Toolbox.Code.IOutputHandler output, IConfiguration config);
Bitmap Process(Bitmap bmpInput, VAR.Toolbox.Code.IOutputHandler output);

View File

@@ -6,7 +6,7 @@ using System.Text;
using VAR.ScreenAutomation.Code;
using VAR.ScreenAutomation.Interfaces;
namespace VAR.ScreenAutomation.Bots
namespace VAR.Toolbox.Code.Bots
{
public class TetrisBot : IAutomationBot
{

View File

@@ -1,9 +1,14 @@
using System;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using VAR.ScreenAutomation.Code;
using VAR.ScreenAutomation.Interfaces;
using VAR.Toolbox.Code;
using VAR.Toolbox.Code.Bots;
using Mouse = VAR.ScreenAutomation.Code.Mouse;
using Screenshoter = VAR.ScreenAutomation.Code.Screenshoter;
// ReSharper disable LocalizableElement
@@ -52,7 +57,7 @@ namespace VAR.ScreenAutomation
TransparencyKey = Color.LimeGreen;
picCapturer.BackColor = Color.LimeGreen;
ddlAutomationBot.Items.AddRange(AutomationBotFactory.GetAllAutomationBots());
ddlAutomationBot.Items.AddRange(AutomationBotFactory.GetNames().ToArray<object>());
ddlAutomationBot.SelectedItem =
configuration.Get("ddlAutomationBot", (string)ddlAutomationBot.SelectedItem);
if (ddlAutomationBot.SelectedIndex < 0)

View File

@@ -74,8 +74,8 @@
<Compile Include="..\VAR.Json\VAR.Json\ParserContext.cs">
<Link>Code\Json\ParserContext.cs</Link>
</Compile>
<Compile Include="Code\AutomationBotFactory.cs" />
<Compile Include="Code\BaseFactory.cs" />
<Compile Include="Code\Bots\AutomationBotFactory.cs" />
<Compile Include="Code\Bots\DummyBot.cs" />
<Compile Include="Code\Bots\IAutomationBot.cs" />
<Compile Include="Code\Bots\Mouse.cs" />