New dynamic factories for TextCoders and ProxyCmdExecutors.

This commit is contained in:
2019-05-26 18:53:32 +02:00
parent e72b3ca600
commit c5b76ee077
16 changed files with 135 additions and 89 deletions

View File

@@ -1,26 +0,0 @@
using System;
namespace VAR.Toolbox.Code
{
public class ProxyCmdExecutorFactory
{
public static IProxyCmdExecutor CreateFromConfig(string config)
{
if (string.IsNullOrEmpty(config) || config.StartsWith("Dummy:"))
{
return new ProxyCmdExecutorDummy();
}
if (config.StartsWith("SqlServer:"))
{
string configSqlServer = config.Substring("SqlServer:".Length);
return new ProxyCmdExecutorThroughSQLServer(configSqlServer);
}
if (config.StartsWith("WMIC:"))
{
string configWMIC = config.Substring("WMIC:".Length);
return new ProxyCmdExecutorWMIC(configWMIC);
}
throw new NotImplementedException(string.Format("Cant create IProxyCmdExecutor with this config: {0}", config));
}
}
}

View File

@@ -1,7 +1,9 @@
namespace VAR.Toolbox.Code namespace VAR.Toolbox.Code.ProxyCmdExecutors
{ {
public interface IProxyCmdExecutor public interface IProxyCmdExecutor
{ {
string Name { get; }
bool ExecuteCmd(string cmd, IOutputHandler outputHandler); bool ExecuteCmd(string cmd, IOutputHandler outputHandler);
} }
} }

View File

@@ -1,7 +1,11 @@
namespace VAR.Toolbox.Code namespace VAR.Toolbox.Code.ProxyCmdExecutors
{ {
public class ProxyCmdExecutorDummy : IProxyCmdExecutor public class ProxyCmdExecutorDummy : IProxyCmdExecutor
{ {
public string Name { get { return "Dummy"; } }
public ProxyCmdExecutorDummy(string config) { }
public bool ExecuteCmd(string cmdString, IOutputHandler outputHandler) public bool ExecuteCmd(string cmdString, IOutputHandler outputHandler)
{ {
outputHandler.OutputLine(string.Format("DummyExecution: {0}", cmdString)); outputHandler.OutputLine(string.Format("DummyExecution: {0}", cmdString));

View File

@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace VAR.Toolbox.Code.ProxyCmdExecutors
{
public class ProxyCmdExecutorFactory
{
private static Dictionary<string, Type> _dictProxyCmdExecutors = null;
private static Dictionary<string, Type> GetDict()
{
if (_dictProxyCmdExecutors != null)
{
return _dictProxyCmdExecutors;
}
Type iTextCoder = typeof(IProxyCmdExecutor);
IEnumerable<Type> toolFormTypes = ReflectionUtils.GetTypesOfInterface(iTextCoder);
_dictProxyCmdExecutors = toolFormTypes.ToDictionary(t =>
{
IProxyCmdExecutor proxyCmdExecutor = System.Runtime.Serialization.FormatterServices.GetUninitializedObject(t) as IProxyCmdExecutor;
return proxyCmdExecutor.Name;
});
return _dictProxyCmdExecutors;
}
public static IProxyCmdExecutor CreateFromConfig(string config)
{
Dictionary<string, Type> dict = GetDict();
int indexOfColon = config.IndexOf(':');
string name = config.Substring(0, indexOfColon < 0 ? config.Length : indexOfColon);
string nextConfig = config.Substring(indexOfColon + 1);
if (string.IsNullOrEmpty(name))
{
return new ProxyCmdExecutorDummy(string.Empty);
}
if (dict.ContainsKey(name) == false)
{
throw new NotImplementedException(string.Format("Cant create IProxyCmdExecutor with this config: {0}", config));
}
Type proxyCmdExecutorType = dict[name];
IProxyCmdExecutor proxyCmdExecutor = Activator.CreateInstance(proxyCmdExecutorType, new object[] { nextConfig }) as IProxyCmdExecutor;
return proxyCmdExecutor;
}
}
}

View File

@@ -1,10 +1,12 @@
using System; using System;
using System.Data.SqlClient; using System.Data.SqlClient;
namespace VAR.Toolbox.Code namespace VAR.Toolbox.Code.ProxyCmdExecutors
{ {
public class ProxyCmdExecutorThroughSQLServer : IProxyCmdExecutor public class ProxyCmdExecutorThroughSQLServer : IProxyCmdExecutor
{ {
public string Name { get { return "SqlServer"; } }
private readonly string _connectionString = null; private readonly string _connectionString = null;
public ProxyCmdExecutorThroughSQLServer(string connectionString) public ProxyCmdExecutorThroughSQLServer(string connectionString)

View File

@@ -1,9 +1,11 @@
using System.Diagnostics; using System.Diagnostics;
namespace VAR.Toolbox.Code namespace VAR.Toolbox.Code.ProxyCmdExecutors
{ {
public class ProxyCmdExecutorWMIC : IProxyCmdExecutor public class ProxyCmdExecutorWMIC : IProxyCmdExecutor
{ {
public string Name { get { return "WMIC"; } }
private string _configWMIC; private string _configWMIC;
public ProxyCmdExecutorWMIC(string configWMIC) public ProxyCmdExecutorWMIC(string configWMIC)

View File

@@ -1,38 +0,0 @@
using System;
namespace VAR.Toolbox.Code
{
public class TextCoderFactory
{
public static string[] GetSupportedCoders()
{
return new string[] {
TextCoderBase64ToAscii.Name,
TextCoderBase64ToUtf8.Name,
TextCoderHexToUtf8.Name,
TextCoderHexToAscii.Name,
};
}
public static ITextCoder CreateFromName(string name)
{
if (name == TextCoderBase64ToAscii.Name)
{
return new TextCoderBase64ToAscii();
}
if (name == TextCoderBase64ToUtf8.Name)
{
return new TextCoderBase64ToUtf8();
}
if (name == TextCoderHexToUtf8.Name)
{
return new TextCoderHexToUtf8();
}
if (name == TextCoderHexToAscii.Name)
{
return new TextCoderHexToAscii();
}
throw new NotImplementedException(string.Format("Cant create ITextCoder with this name: {0}", name));
}
}
}

View File

@@ -1,7 +1,9 @@
namespace VAR.Toolbox.Code namespace VAR.Toolbox.Code.TextCoders
{ {
public interface ITextCoder public interface ITextCoder
{ {
string Name { get; }
bool NeedsKey { get; } bool NeedsKey { get; }
string Encode(string input, string key); string Encode(string input, string key);

View File

@@ -1,11 +1,11 @@
using System; using System;
using System.Text; using System.Text;
namespace VAR.Toolbox.Code namespace VAR.Toolbox.Code.TextCoders
{ {
public class TextCoderBase64ToAscii : ITextCoder public class TextCoderBase64ToAscii : ITextCoder
{ {
public const string Name = "Base64ToAscii"; public string Name { get { return "Base64ToAscii"; } }
public bool NeedsKey { get { return false; } } public bool NeedsKey { get { return false; } }

View File

@@ -1,11 +1,11 @@
using System; using System;
using System.Text; using System.Text;
namespace VAR.Toolbox.Code namespace VAR.Toolbox.Code.TextCoders
{ {
public class TextCoderBase64ToUtf8 : ITextCoder public class TextCoderBase64ToUtf8 : ITextCoder
{ {
public const string Name = "Base64ToUtf8"; public string Name { get { return "Base64ToUtf8"; } }
public bool NeedsKey { get { return false; } } public bool NeedsKey { get { return false; } }

View File

@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace VAR.Toolbox.Code.TextCoders
{
public class TextCoderFactory
{
private static Dictionary<string, Type> _dictTextCoderTypes = null;
private static Dictionary<string, Type> GetDict()
{
if (_dictTextCoderTypes != null)
{
return _dictTextCoderTypes;
}
Type iTextCoder = typeof(ITextCoder);
IEnumerable<Type> toolFormTypes = ReflectionUtils.GetTypesOfInterface(iTextCoder);
_dictTextCoderTypes = toolFormTypes.ToDictionary(t =>
{
ITextCoder textCoder = System.Runtime.Serialization.FormatterServices.GetUninitializedObject(t) as ITextCoder;
return textCoder.Name;
});
return _dictTextCoderTypes;
}
public static string[] GetSupportedCoders()
{
Dictionary<string, Type> dict = GetDict();
return dict.Select(p => p.Key).ToArray();
}
public static ITextCoder CreateFromName(string name)
{
Dictionary<string, Type> dict = GetDict();
if (dict.ContainsKey(name) == false)
{
throw new NotImplementedException(string.Format("Cant create ITextCoder with this name: {0}", name));
}
Type textCoder = dict[name];
return Activator.CreateInstance(textCoder) as ITextCoder;
}
}
}

View File

@@ -1,10 +1,10 @@
using System.Text; using System.Text;
namespace VAR.Toolbox.Code namespace VAR.Toolbox.Code.TextCoders
{ {
public class TextCoderHexToAscii : ITextCoder public class TextCoderHexToAscii : ITextCoder
{ {
public const string Name = "HexToAscii"; public string Name { get { return "HexToAscii"; } }
public bool NeedsKey { get { return false; } } public bool NeedsKey { get { return false; } }

View File

@@ -1,10 +1,10 @@
using System.Text; using System.Text;
namespace VAR.Toolbox.Code namespace VAR.Toolbox.Code.TextCoders
{ {
public class TextCoderHexToUtf8 : ITextCoder public class TextCoderHexToUtf8 : ITextCoder
{ {
public const string Name = "HexToUtf8"; public string Name { get { return "HexToUtf8"; } }
public bool NeedsKey { get { return false; } } public bool NeedsKey { get { return false; } }

View File

@@ -1,6 +1,6 @@
using System; using System;
using System.Windows.Forms; using System.Windows.Forms;
using VAR.Toolbox.Code; using VAR.Toolbox.Code.TextCoders;
namespace VAR.Toolbox.UI namespace VAR.Toolbox.UI
{ {

View File

@@ -5,6 +5,7 @@ using System.Collections.Generic;
using System.Threading; using System.Threading;
using System.Windows.Forms; using System.Windows.Forms;
using VAR.Toolbox.Code; using VAR.Toolbox.Code;
using VAR.Toolbox.Code.ProxyCmdExecutors;
namespace VAR.Toolbox.UI namespace VAR.Toolbox.UI
{ {

View File

@@ -74,10 +74,10 @@
<Link>Code\Json\ParserContext.cs</Link> <Link>Code\Json\ParserContext.cs</Link>
</Compile> </Compile>
<Compile Include="Code\HexUtils.cs" /> <Compile Include="Code\HexUtils.cs" />
<Compile Include="Code\ProxyCmdExecutorWMIC.cs" /> <Compile Include="Code\ProxyCmdExecutors\ProxyCmdExecutorWMIC.cs" />
<Compile Include="Code\ReflectionUtils.cs" /> <Compile Include="Code\ReflectionUtils.cs" />
<Compile Include="Code\TextCoderBase64Utf8.cs" /> <Compile Include="Code\TextCoders\TextCoderBase64Utf8.cs" />
<Compile Include="Code\TextCoderBase64Ascii.cs" /> <Compile Include="Code\TextCoders\TextCoderBase64Ascii.cs" />
<Compile Include="Code\DirectShow\CameraControlProperty.cs" /> <Compile Include="Code\DirectShow\CameraControlProperty.cs" />
<Compile Include="Code\DirectShow\IAMCameraControl.cs" /> <Compile Include="Code\DirectShow\IAMCameraControl.cs" />
<Compile Include="Code\DirectShow\IAMCrossbar.cs" /> <Compile Include="Code\DirectShow\IAMCrossbar.cs" />
@@ -108,18 +108,18 @@
<Compile Include="Code\DirectShow\Tools.cs" /> <Compile Include="Code\DirectShow\Tools.cs" />
<Compile Include="Code\DirectShow\Uuids.cs" /> <Compile Include="Code\DirectShow\Uuids.cs" />
<Compile Include="Code\Windows\GDI32.cs" /> <Compile Include="Code\Windows\GDI32.cs" />
<Compile Include="Code\ITextCoder.cs" /> <Compile Include="Code\TextCoders\ITextCoder.cs" />
<Compile Include="Code\IOutputHandler.cs" /> <Compile Include="Code\IOutputHandler.cs" />
<Compile Include="Code\IProxyCmdExecutor.cs" /> <Compile Include="Code\ProxyCmdExecutors\IProxyCmdExecutor.cs" />
<Compile Include="Code\Logger.cs" /> <Compile Include="Code\Logger.cs" />
<Compile Include="Code\Mouse.cs" /> <Compile Include="Code\Mouse.cs" />
<Compile Include="Code\ProxyCmdExecutorFactory.cs" /> <Compile Include="Code\ProxyCmdExecutors\ProxyCmdExecutorFactory.cs" />
<Compile Include="Code\ProxyCmdExecutorDummy.cs" /> <Compile Include="Code\ProxyCmdExecutors\ProxyCmdExecutorDummy.cs" />
<Compile Include="Code\ProxyCmdExecutorThroughSQLServer.cs" /> <Compile Include="Code\ProxyCmdExecutors\ProxyCmdExecutorThroughSQLServer.cs" />
<Compile Include="Code\Screenshooter.cs" /> <Compile Include="Code\Screenshooter.cs" />
<Compile Include="Code\TextCoderFactory.cs" /> <Compile Include="Code\TextCoders\TextCoderFactory.cs" />
<Compile Include="Code\TextCoderHexToAscii.cs" /> <Compile Include="Code\TextCoders\TextCoderHexToAscii.cs" />
<Compile Include="Code\TextCoderHexToUtf8.cs" /> <Compile Include="Code\TextCoders\TextCoderHexToUtf8.cs" />
<Compile Include="Code\Windows\User32.cs" /> <Compile Include="Code\Windows\User32.cs" />
<Compile Include="Code\Webcam.cs" /> <Compile Include="Code\Webcam.cs" />
<Compile Include="Code\Windows\Win32.cs" /> <Compile Include="Code\Windows\Win32.cs" />
@@ -214,6 +214,7 @@
<Content Include="Images\toolbox.svg" /> <Content Include="Images\toolbox.svg" />
<Content Include="Toolbox.ico" /> <Content Include="Toolbox.ico" />
</ItemGroup> </ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets. Other similar extension points exist, see Microsoft.Common.targets.