From fc5b81014bc2513524dcf04c63b3ff116c03fb61 Mon Sep 17 00:00:00 2001 From: "Valeriano A.R" Date: Sat, 9 Apr 2022 00:55:02 +0200 Subject: [PATCH] Changes recommended by Rider/Resharper --- .../.idea/.gitignore | 13 + .../.idea/encodings.xml | 4 + .../.idea/indexLayout.xml | 8 + .../.idea.VAR.ScreenAutomation/.idea/vcs.xml | 6 + VAR.ScreenAutomation.sln.DotSettings | 60 ++++ VAR.ScreenAutomation/App.config | 4 +- VAR.ScreenAutomation/Bots/DummyBot.cs | 2 +- VAR.ScreenAutomation/Bots/TetrisBot.cs | 333 ++++++++++-------- .../Code/AutomationBotFactory.cs | 17 +- .../Code/FileBackedConfiguration.cs | 40 +-- .../Code/MemoryBackedConfiguration.cs | 25 +- VAR.ScreenAutomation/Code/Mouse.cs | 20 +- VAR.ScreenAutomation/Code/Screenshoter.cs | 20 +- VAR.ScreenAutomation/Code/WindowHandling.cs | 21 +- .../Controls/CtrImageViewer.cs | 16 +- VAR.ScreenAutomation/Controls/CtrOutput.cs | 16 +- .../FrmAutomationBotParams.cs | 36 +- .../FrmAutomationBotParams.resx | 235 ++++++------ VAR.ScreenAutomation/FrmScreenAutomation.cs | 75 ++-- VAR.ScreenAutomation/FrmScreenAutomation.resx | 235 ++++++------ .../Interfaces/IAutomationBot.cs | 2 +- .../Interfaces/IOutputHandler.cs | 2 +- VAR.ScreenAutomation/Program.cs | 2 +- .../Properties/AssemblyInfo.cs | 3 +- .../Properties/Resources.resx | 229 ++++++------ .../Properties/Settings.settings | 8 +- .../VAR.ScreenAutomation.csproj | 220 ++++++------ VAR.ScreenAutomation/app.manifest | 120 +++---- 28 files changed, 982 insertions(+), 790 deletions(-) create mode 100644 .idea/.idea.VAR.ScreenAutomation/.idea/.gitignore create mode 100644 .idea/.idea.VAR.ScreenAutomation/.idea/encodings.xml create mode 100644 .idea/.idea.VAR.ScreenAutomation/.idea/indexLayout.xml create mode 100644 .idea/.idea.VAR.ScreenAutomation/.idea/vcs.xml create mode 100644 VAR.ScreenAutomation.sln.DotSettings diff --git a/.idea/.idea.VAR.ScreenAutomation/.idea/.gitignore b/.idea/.idea.VAR.ScreenAutomation/.idea/.gitignore new file mode 100644 index 0000000..c516e08 --- /dev/null +++ b/.idea/.idea.VAR.ScreenAutomation/.idea/.gitignore @@ -0,0 +1,13 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Rider ignored files +/projectSettingsUpdater.xml +/.idea.VAR.ScreenAutomation.iml +/contentModel.xml +/modules.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/.idea.VAR.ScreenAutomation/.idea/encodings.xml b/.idea/.idea.VAR.ScreenAutomation/.idea/encodings.xml new file mode 100644 index 0000000..df87cf9 --- /dev/null +++ b/.idea/.idea.VAR.ScreenAutomation/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/.idea.VAR.ScreenAutomation/.idea/indexLayout.xml b/.idea/.idea.VAR.ScreenAutomation/.idea/indexLayout.xml new file mode 100644 index 0000000..7b08163 --- /dev/null +++ b/.idea/.idea.VAR.ScreenAutomation/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/.idea.VAR.ScreenAutomation/.idea/vcs.xml b/.idea/.idea.VAR.ScreenAutomation/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/.idea.VAR.ScreenAutomation/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/VAR.ScreenAutomation.sln.DotSettings b/VAR.ScreenAutomation.sln.DotSettings new file mode 100644 index 0000000..33636c2 --- /dev/null +++ b/VAR.ScreenAutomation.sln.DotSettings @@ -0,0 +1,60 @@ + + NotRequired + True + True + TOGETHER_SAME_LINE + True + True + False + True + True + True + OneStep + OneStep + True + UseVarWhenEvident + UseVarWhenEvident + AES + AM + AUX + BPP + CYC + DC + DES + EPM + GDI + ID + IP + RECT + RGB + SCART + SPDIF + SQL + SRCCOPY + TCP + URL + USB + VAR + WMIC + YRYBY + False + <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb_AaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb_AaBb"><ExtraRule Prefix="T" Suffix="" Style="AaBb_AaBb" /></Policy> + <Policy Inspect="True" Prefix="I" Suffix="" Style="AaBb_AaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb_AaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb_AaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb_AaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb_AaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb_AaBb" /> + <Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb_AaBb" /> + <Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb_AaBb" /> + <Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb_AaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb_AaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb_AaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb_AaBb" /> + True + True + True + True + \ No newline at end of file diff --git a/VAR.ScreenAutomation/App.config b/VAR.ScreenAutomation/App.config index 56efbc7..7597626 100644 --- a/VAR.ScreenAutomation/App.config +++ b/VAR.ScreenAutomation/App.config @@ -1,6 +1,6 @@  - - + + \ No newline at end of file diff --git a/VAR.ScreenAutomation/Bots/DummyBot.cs b/VAR.ScreenAutomation/Bots/DummyBot.cs index 7f263f5..300a60b 100644 --- a/VAR.ScreenAutomation/Bots/DummyBot.cs +++ b/VAR.ScreenAutomation/Bots/DummyBot.cs @@ -29,4 +29,4 @@ namespace VAR.ScreenAutomation.Bots return string.Empty; } } -} +} \ No newline at end of file diff --git a/VAR.ScreenAutomation/Bots/TetrisBot.cs b/VAR.ScreenAutomation/Bots/TetrisBot.cs index 3617898..c6b0007 100644 --- a/VAR.ScreenAutomation/Bots/TetrisBot.cs +++ b/VAR.ScreenAutomation/Bots/TetrisBot.cs @@ -17,12 +17,12 @@ namespace VAR.ScreenAutomation.Bots private TetrisGrid _workGrid1; private double[] _columnEvaluation; - private bool _shapeFound = false; + private bool _shapeFound; private int _shapeX; private int _shapeY; private double _bestEvaluation = double.MinValue; - private int _bestXOffset = 0; - private int _bestRotation = 0; + private int _bestXOffset; + private int _bestRotation; public string Name => "Tetris"; @@ -57,7 +57,7 @@ namespace VAR.ScreenAutomation.Bots new TetrisShape(), }; output.Clean(); - output.AddLine(string.Format("TetrisBot: Starting {0}", DateTime.UtcNow.ToString("s"))); + output.AddLine($"TetrisBot: Starting {DateTime.UtcNow:s}"); } public Bitmap Process(Bitmap bmpInput, IOutputHandler output) @@ -74,6 +74,7 @@ namespace VAR.ScreenAutomation.Bots _currentShape[_bestRotation].Drop(_workGrid0, _shapeX + _bestXOffset, _shapeY, TetrisGrid.CellShapeA); _currentShape[0].PutOnGrid(_workGrid0, _shapeX, _shapeY, TetrisGrid.CellShapeB); } + _workGrid0.Draw(bmpInput, 0.75f); _workGrid0.SampleOther(_grid, TetrisGrid.CellSolid, TetrisGrid.CellSolid); _workGrid0.RemoveGround(); @@ -91,7 +92,8 @@ namespace VAR.ScreenAutomation.Bots { for (int x = 0; x < _grid.Width; x++) { - TetrisShape matchedShape = TetrisShape.DefaultShapes.FirstOrDefault(s => s.MatchOnGrid(_workGrid0, x, y)); + TetrisShape matchedShape = + TetrisShape.DefaultShapes.FirstOrDefault(s => s.MatchOnGrid(_workGrid0, x, y)); if (matchedShape != null) { _workGrid1.SampleOther(_workGrid0, TetrisGrid.CellSolid, TetrisGrid.CellSolid); @@ -102,14 +104,16 @@ namespace VAR.ScreenAutomation.Bots _currentShape[0].Copy(matchedShape); for (int i = 1; i < 4; i++) { - _currentShape[i].RotateCW(_currentShape[i - 1]); + _currentShape[i].RotateClockWise(_currentShape[i - 1]); } + _shapeX = x; _shapeY = y; _shapeFound = true; break; } } + if (_shapeFound) { break; } } } @@ -147,9 +151,11 @@ namespace VAR.ScreenAutomation.Bots // Search valid X range int minX = _shapeX; while (minX >= 0 && _columnEvaluation[minX] > double.MinValue) { minX--; } + minX++; int maxX = _shapeX; while (maxX < _grid.Width && _columnEvaluation[maxX] > double.MinValue) { maxX++; } + maxX--; // Apply best value inside valid X range @@ -165,8 +171,8 @@ namespace VAR.ScreenAutomation.Bots } } - private int _shotCooldownFrames = 0; - private int _shotCooldown = 0; + private int _shotCooldownFrames; + private int _shotCooldown; public string ResponseKeys() { @@ -189,9 +195,13 @@ namespace VAR.ScreenAutomation.Bots _shotCooldown = _shotCooldownFrames; if (_bestRotation != 0 && _bestXOffset < 0) { return "{UP}{LEFT}"; } + if (_bestRotation != 0 && _bestXOffset > 0) { return "{UP}{RIGHT}"; } + if (_bestRotation != 0) { return "{UP}"; } + if (_bestXOffset < 0) { return "{LEFT}"; } + if (_bestXOffset > 0) { return "{RIGHT}"; } return string.Empty; @@ -200,11 +210,11 @@ namespace VAR.ScreenAutomation.Bots public class TetrisShape { - public const int ShapeSize = 4; + private const int ShapeSize = 4; - private byte[][] _cells = null; + private readonly byte[][] _cells; - private int _count = 0; + private int _count; public TetrisShape(byte[][] cells = null) { @@ -220,9 +230,11 @@ namespace VAR.ScreenAutomation.Bots for (int j = 0; j < ShapeSize; j++) { if (j >= cells.Length) { break; } + for (int i = 0; i < ShapeSize; i++) { if (i >= cells[j].Length) { break; } + _cells[j][i] = cells[j][i]; if (_cells[j][i] != 0) { _count++; } } @@ -235,124 +247,140 @@ namespace VAR.ScreenAutomation.Bots return _count; } - private static List _defaultShapes = null; + private static List _defaultShapes; public static List DefaultShapes { get { - if (_defaultShapes == null) + return _defaultShapes ?? (_defaultShapes = new List { - _defaultShapes = new List + // I + new TetrisShape(new[] { - // I - new TetrisShape(new byte[][]{ - new byte[]{ 1, 1, 1, 1, }, - }), - new TetrisShape(new byte[][]{ - new byte[]{ 1, }, - new byte[]{ 1, }, - new byte[]{ 1, }, - new byte[]{ 1, }, - }), + new byte[] { 1, 1, 1, 1, }, + }), + new TetrisShape(new[] + { + new byte[] { 1, }, + new byte[] { 1, }, + new byte[] { 1, }, + new byte[] { 1, }, + }), - // J - new TetrisShape(new byte[][]{ - new byte[]{ 1, }, - new byte[]{ 1, 1, 1, }, - }), - new TetrisShape(new byte[][]{ - new byte[]{ 1, 1, }, - new byte[]{ 1, }, - new byte[]{ 1, }, - }), - new TetrisShape(new byte[][]{ - new byte[]{ 1, 1, 1, }, - new byte[]{ 0, 0, 1, }, - }), - new TetrisShape(new byte[][]{ - new byte[]{ 0, 1, }, - new byte[]{ 0, 1, }, - new byte[]{ 1, 1, }, - }), + // J + new TetrisShape(new[] + { + new byte[] { 1, }, + new byte[] { 1, 1, 1, }, + }), + new TetrisShape(new[] + { + new byte[] { 1, 1, }, + new byte[] { 1, }, + new byte[] { 1, }, + }), + new TetrisShape(new[] + { + new byte[] { 1, 1, 1, }, + new byte[] { 0, 0, 1, }, + }), + new TetrisShape(new[] + { + new byte[] { 0, 1, }, + new byte[] { 0, 1, }, + new byte[] { 1, 1, }, + }), - // L - new TetrisShape(new byte[][]{ - new byte[]{ 0, 0, 1, }, - new byte[]{ 1, 1, 1, }, - }), - new TetrisShape(new byte[][]{ - new byte[]{ 1, }, - new byte[]{ 1, }, - new byte[]{ 1, 1, }, - }), - new TetrisShape(new byte[][]{ - new byte[]{ 1, 1, 1, }, - new byte[]{ 1, }, - }), - new TetrisShape(new byte[][]{ - new byte[]{ 1, 1, }, - new byte[]{ 0, 1, }, - new byte[]{ 0, 1, }, - }), + // L + new TetrisShape(new[] + { + new byte[] { 0, 0, 1, }, + new byte[] { 1, 1, 1, }, + }), + new TetrisShape(new[] + { + new byte[] { 1, }, + new byte[] { 1, }, + new byte[] { 1, 1, }, + }), + new TetrisShape(new[] + { + new byte[] { 1, 1, 1, }, + new byte[] { 1, }, + }), + new TetrisShape(new[] + { + new byte[] { 1, 1, }, + new byte[] { 0, 1, }, + new byte[] { 0, 1, }, + }), - // S - new TetrisShape(new byte[][]{ - new byte[]{ 0, 1, 1, }, - new byte[]{ 1, 1, }, - }), - new TetrisShape(new byte[][]{ - new byte[]{ 1, }, - new byte[]{ 1, 1, }, - new byte[]{ 0, 1, }, - }), + // S + new TetrisShape(new[] + { + new byte[] { 0, 1, 1, }, + new byte[] { 1, 1, }, + }), + new TetrisShape(new[] + { + new byte[] { 1, }, + new byte[] { 1, 1, }, + new byte[] { 0, 1, }, + }), - // T - new TetrisShape(new byte[][]{ - new byte[]{ 0, 1, }, - new byte[]{ 1, 1, 1, }, - }), - new TetrisShape(new byte[][]{ - new byte[]{ 1, }, - new byte[]{ 1, 1, }, - new byte[]{ 1, }, - }), - new TetrisShape(new byte[][]{ - new byte[]{ 1, 1, 1, }, - new byte[]{ 0, 1, }, - }), - new TetrisShape(new byte[][]{ - new byte[]{ 0, 1, }, - new byte[]{ 1, 1, }, - new byte[]{ 0, 1, }, - }), + // T + new TetrisShape(new[] + { + new byte[] { 0, 1, }, + new byte[] { 1, 1, 1, }, + }), + new TetrisShape(new[] + { + new byte[] { 1, }, + new byte[] { 1, 1, }, + new byte[] { 1, }, + }), + new TetrisShape(new[] + { + new byte[] { 1, 1, 1, }, + new byte[] { 0, 1, }, + }), + new TetrisShape(new[] + { + new byte[] { 0, 1, }, + new byte[] { 1, 1, }, + new byte[] { 0, 1, }, + }), - // Z - new TetrisShape(new byte[][]{ - new byte[]{ 1, 1, }, - new byte[]{ 0, 1, 1, }, - }), - new TetrisShape(new byte[][]{ - new byte[]{ 0, 1, }, - new byte[]{ 1, 1, }, - new byte[]{ 1, }, - }), + // Z + new TetrisShape(new[] + { + new byte[] { 1, 1, }, + new byte[] { 0, 1, 1, }, + }), + new TetrisShape(new[] + { + new byte[] { 0, 1, }, + new byte[] { 1, 1, }, + new byte[] { 1, }, + }), - // O - new TetrisShape(new byte[][]{ - new byte[]{ 1, 1, }, - new byte[]{ 1, 1, }, - }) - }; - } - return _defaultShapes; + // O + new TetrisShape(new[] + { + new byte[] { 1, 1, }, + new byte[] { 1, 1, }, + }) + }); } } public bool IsValid() { if (_count != 4) { return false; } - bool matchesAnyDefault = DefaultShapes.Any(ts => CompareShape(ts)); + + bool matchesAnyDefault = DefaultShapes.Any(CompareShape); return matchesAnyDefault; } @@ -368,7 +396,7 @@ namespace VAR.ScreenAutomation.Bots } } - public bool CompareShape(TetrisShape shape) + private bool CompareShape(TetrisShape shape) { for (int j = 0; j < ShapeSize; j++) { @@ -380,6 +408,7 @@ namespace VAR.ScreenAutomation.Bots } } } + return true; } @@ -410,6 +439,7 @@ namespace VAR.ScreenAutomation.Bots for (int i = 0; i < ShapeSize; i++) { if (_cells[j][i] == 0) { continue; } + grid.Set(x + i, y + j, value); } } @@ -422,27 +452,31 @@ namespace VAR.ScreenAutomation.Bots for (int i = 0; i < ShapeSize; i++) { if (_cells[j][i] == 0) { continue; } + if (grid.Get(x + i, y + j) != 0) { return true; } } } + return false; } public bool Drop(TetrisGrid grid, int x, int y, byte value) { if (CheckIntersection(grid, x, y)) { return false; } + while (CheckIntersection(grid, x, y + 1) == false) { y++; } + PutOnGrid(grid, x, y, value); return true; } - public bool SearchFirstCell(byte value, out int x, out int y) + private bool SearchFirstCell(byte value, out int x, out int y) { x = -1; y = -1; @@ -456,7 +490,9 @@ namespace VAR.ScreenAutomation.Bots } } } + if (y == -1) { return false; } + for (int i = 0; i < ShapeSize && x == -1; i++) { for (int j = 0; j < ShapeSize && x == -1; j++) @@ -467,11 +503,13 @@ namespace VAR.ScreenAutomation.Bots } } } + if (x == -1) { return false; } + return true; } - public void Offset(int x, int y) + private void Offset(int x, int y) { _count = 0; for (int j = 0; j < ShapeSize; j++) @@ -494,7 +532,7 @@ namespace VAR.ScreenAutomation.Bots } } - public void RotateCW(TetrisShape shape) + public void RotateClockWise(TetrisShape shape) { for (int j = 0; j < ShapeSize; j++) { @@ -503,6 +541,7 @@ namespace VAR.ScreenAutomation.Bots _cells[i][ShapeSize - (j + 1)] = shape._cells[j][i]; } } + _count = shape._count; if (SearchFirstCell(1, out int offsetX, out int offsetY)) @@ -518,15 +557,9 @@ namespace VAR.ScreenAutomation.Bots StringBuilder sbLine = new StringBuilder(); for (int x = 0; x < ShapeSize; x++) { - if (_cells[y][x] == 0) - { - sbLine.Append(".."); - } - else - { - sbLine.Append("[]"); - } + sbLine.Append(_cells[y][x] == 0 ? ".." : "[]"); } + output.AddLine(sbLine.ToString()); } } @@ -554,6 +587,7 @@ namespace VAR.ScreenAutomation.Bots } } } + return true; } } @@ -565,15 +599,15 @@ namespace VAR.ScreenAutomation.Bots public const byte CellShapeA = 2; public const byte CellShapeB = 3; - private int _gridWidth; - private int _gridHeight; + private readonly int _gridWidth; + private readonly int _gridHeight; - public int Width { get { return _gridWidth; } } - public int Height { get { return _gridHeight; } } + public int Width => _gridWidth; + public int Height => _gridHeight; - private byte[][] _grid = null; + private readonly byte[][] _grid; - private int[] _heights = null; + private readonly int[] _heights; public TetrisGrid(int gridWidth, int gridHeight) { @@ -584,27 +618,32 @@ namespace VAR.ScreenAutomation.Bots { _grid[y] = new byte[_gridWidth]; } + _heights = new int[_gridWidth]; } public byte Get(int x, int y) { if (x >= _gridWidth || x < 0) { return 0xFF; } + if (y >= _gridHeight || y < 0) { return 0xFF; } + return _grid[y][x]; } public void Set(int x, int y, byte value) { if (x >= _gridWidth || x < 0) { return; } + if (y >= _gridHeight || y < 0) { return; } + _grid[y][x] = value; } public void SampleFromBitmap(Bitmap bmp) { - float xStep = bmp.Width / _gridWidth; - float yStep = bmp.Height / _gridHeight; + float xStep = bmp.Width / (float)_gridWidth; + float yStep = bmp.Height / (float)_gridHeight; float xOff0 = xStep / 2; float xOff1 = xOff0 / 2; float xOff2 = xOff0 + xOff1; @@ -665,11 +704,14 @@ namespace VAR.ScreenAutomation.Bots } } - public void FloodFill(int x, int y, byte expectedValue, byte fillValue) + private void FloodFill(int x, int y, byte expectedValue, byte fillValue) { if (x >= _gridWidth || x < 0) { return; } + if (y >= _gridHeight || y < 0) { return; } + if (_grid[y][x] != expectedValue) { return; } + _grid[y][x] = fillValue; FloodFill(x - 1, y - 1, expectedValue, fillValue); FloodFill(x - 1, y + 0, expectedValue, fillValue); @@ -681,7 +723,7 @@ namespace VAR.ScreenAutomation.Bots FloodFill(x + 1, y + 1, expectedValue, fillValue); } - public void SampleOther(TetrisGrid grid, byte value, byte setValue = 1) + public void SampleOther(TetrisGrid grid, byte value, byte setValue) { for (int y = 0; y < _gridHeight; y++) { @@ -713,7 +755,9 @@ namespace VAR.ScreenAutomation.Bots } } } + if (y == -1) { return false; } + for (int i = 0; i < _gridWidth && x == -1; i++) { for (int j = 0; j < _gridHeight && x == -1; j++) @@ -724,11 +768,13 @@ namespace VAR.ScreenAutomation.Bots } } } + if (x == -1) { return false; } + return true; } - public bool IsCompleteLine(int y) + private bool IsCompleteLine(int y) { bool complete = true; for (int x = 0; x < _gridWidth; x++) @@ -739,6 +785,7 @@ namespace VAR.ScreenAutomation.Bots break; } } + return complete; } @@ -748,14 +795,16 @@ namespace VAR.ScreenAutomation.Bots double holesWeight = -0.35663, double bumpinessWeight = -0.184483) { - // Calculte aggregate height + // Calculate aggregate height for (int i = 0; i < _gridWidth; i++) { int j = 0; while (j < _gridHeight && _grid[j][i] == CellEmpty) { j++; } + _heights[i] = _gridHeight - j; } - double agregateHeight = _heights.Sum(); + + double aggregateHeight = _heights.Sum(); // Calculate complete lines int completeLines = 0; @@ -783,18 +832,18 @@ namespace VAR.ScreenAutomation.Bots } // Calculate bumpiness - int bumpines = 0; + int bumpiness = 0; for (int i = 1; i < _gridWidth; i++) { - bumpines += Math.Abs(_heights[i] - _heights[i - 1]); + bumpiness += Math.Abs(_heights[i] - _heights[i - 1]); } // Evaluate formula double evaluationValue = - aggregateHeightWeight * agregateHeight + + aggregateHeightWeight * aggregateHeight + completeLinesWeight * completeLines + holesWeight * holes + - bumpinessWeight * bumpines + + bumpinessWeight * bumpiness + 0; return evaluationValue; } @@ -832,13 +881,15 @@ namespace VAR.ScreenAutomation.Bots { br = Brushes.Green; } + if (br == null) { continue; } - g.DrawRectangle(borderPen, (xStep * x) + offX - 1, (yStep * y) + offY - 1, halfXStep + 2, halfYStep + 2); + g.DrawRectangle(borderPen, (xStep * x) + offX - 1, (yStep * y) + offY - 1, halfXStep + 2, + halfYStep + 2); g.FillRectangle(br, (xStep * x) + offX, (yStep * y) + offY, halfXStep, halfYStep); } } } } } -} +} \ No newline at end of file diff --git a/VAR.ScreenAutomation/Code/AutomationBotFactory.cs b/VAR.ScreenAutomation/Code/AutomationBotFactory.cs index 2d4277a..c5ee5cc 100644 --- a/VAR.ScreenAutomation/Code/AutomationBotFactory.cs +++ b/VAR.ScreenAutomation/Code/AutomationBotFactory.cs @@ -6,9 +6,9 @@ using VAR.ScreenAutomation.Interfaces; namespace VAR.ScreenAutomation.Code { - public class AutomationBotFactory + public static class AutomationBotFactory { - private static Dictionary _dictAutomationBots = null; + private static Dictionary _dictAutomationBots; private static Dictionary GetDict() { @@ -28,8 +28,9 @@ namespace VAR.ScreenAutomation.Code true); _dictAutomationBots = automationBotTypes.ToDictionary(t => { - IAutomationBot automationBot = System.Runtime.Serialization.FormatterServices.GetUninitializedObject(t) as IAutomationBot; - return automationBot.Name; + IAutomationBot automationBot = + System.Runtime.Serialization.FormatterServices.GetUninitializedObject(t) as IAutomationBot; + return automationBot?.Name ?? t.Name; }); return _dictAutomationBots; @@ -39,7 +40,7 @@ namespace VAR.ScreenAutomation.Code { Dictionary dict = GetDict(); string[] allAutomationBots = dict.Select(p => p.Key).ToArray(); - return allAutomationBots; + return allAutomationBots.ToArray(); } public static IAutomationBot CreateFromName(string name) @@ -49,10 +50,12 @@ namespace VAR.ScreenAutomation.Code { return new DummyBot(); } + if (dict.ContainsKey(name) == false) { - throw new NotImplementedException(string.Format("Can't create IAutomationBot with this name: {0}", name)); + throw new NotImplementedException($"Can't create IAutomationBot with this name: {name}"); } + Type proxyCmdExecutorType = dict[name]; IAutomationBot automationBot = Activator.CreateInstance(proxyCmdExecutorType) as IAutomationBot; @@ -60,4 +63,4 @@ namespace VAR.ScreenAutomation.Code return automationBot; } } -} +} \ No newline at end of file diff --git a/VAR.ScreenAutomation/Code/FileBackedConfiguration.cs b/VAR.ScreenAutomation/Code/FileBackedConfiguration.cs index a845532..31d092b 100644 --- a/VAR.ScreenAutomation/Code/FileBackedConfiguration.cs +++ b/VAR.ScreenAutomation/Code/FileBackedConfiguration.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.IO; using System.Text; using VAR.ScreenAutomation.Interfaces; @@ -7,9 +8,9 @@ namespace VAR.ScreenAutomation.Code { public class FileBackedConfiguration : IConfiguration { - private MemoryBackedConfiguration _config = new MemoryBackedConfiguration(); + private readonly MemoryBackedConfiguration _config = new MemoryBackedConfiguration(); - private string _name = null; + private readonly string _name; public FileBackedConfiguration(string name = null) { @@ -18,33 +19,22 @@ namespace VAR.ScreenAutomation.Code private static string GetConfigFileName(string name = null) { - string location = System.Reflection.Assembly.GetEntryAssembly().Location; + 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); - } + string configFile = string.IsNullOrEmpty(name) + ? $"{path}/{filenameWithoutExtension}.cfg" + : $"{path}/{filenameWithoutExtension}_{name}.cfg"; 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); - } + string[] config = File.Exists(configFile) == false + ? Array.Empty() + : File.ReadAllLines(configFile); + return config; } @@ -58,11 +48,13 @@ namespace VAR.ScreenAutomation.Code _config.Set(key, other.Get(key, null)); } } + 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); @@ -77,6 +69,7 @@ namespace VAR.ScreenAutomation.Code { sbConfig.AppendFormat("{0}|{1}\n", key, _config.Get(key, string.Empty)); } + string configFileName = GetConfigFileName(_name); File.WriteAllText(configFileName, sbConfig.ToString()); } @@ -120,6 +113,5 @@ namespace VAR.ScreenAutomation.Code { _config.Set(key, value); } - } -} +} \ No newline at end of file diff --git a/VAR.ScreenAutomation/Code/MemoryBackedConfiguration.cs b/VAR.ScreenAutomation/Code/MemoryBackedConfiguration.cs index 140313a..a7ab978 100644 --- a/VAR.ScreenAutomation/Code/MemoryBackedConfiguration.cs +++ b/VAR.ScreenAutomation/Code/MemoryBackedConfiguration.cs @@ -7,12 +7,13 @@ namespace VAR.ScreenAutomation.Code { public class MemoryBackedConfiguration : IConfiguration { - private Dictionary _configItems = new Dictionary(); + private readonly Dictionary _configItems = new Dictionary(); public IEnumerable GetKeys() { - if (_configItems == null) { return new List(); } - return _configItems.Select(p => p.Key); + return _configItems == null + ? new List() + : _configItems.Select(p => p.Key); } public void Clear() @@ -23,41 +24,44 @@ namespace VAR.ScreenAutomation.Code public string Get(string key, string defaultValue) { if (_configItems == null) { return defaultValue; } - if (_configItems.ContainsKey(key)) - { - return _configItems[key]; - } - return defaultValue; + + return _configItems.ContainsKey(key) ? _configItems[key] : 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; @@ -71,6 +75,7 @@ namespace VAR.ScreenAutomation.Code public void Set(string key, int value) { if (_configItems == null) { return; } + if (_configItems.ContainsKey(key)) { _configItems[key] = Convert.ToString(value); @@ -84,6 +89,7 @@ namespace VAR.ScreenAutomation.Code public void Set(string key, bool value) { if (_configItems == null) { return; } + if (_configItems.ContainsKey(key)) { _configItems[key] = value ? "true" : "false"; @@ -93,6 +99,5 @@ namespace VAR.ScreenAutomation.Code _configItems.Add(key, value ? "true" : "false"); } } - } -} +} \ No newline at end of file diff --git a/VAR.ScreenAutomation/Code/Mouse.cs b/VAR.ScreenAutomation/Code/Mouse.cs index 2c8cfcc..0cbe672 100644 --- a/VAR.ScreenAutomation/Code/Mouse.cs +++ b/VAR.ScreenAutomation/Code/Mouse.cs @@ -1,9 +1,11 @@ using System; using System.Runtime.InteropServices; +// ReSharper disable InconsistentNaming + namespace VAR.ScreenAutomation.Code { - public class Mouse + public static class Mouse { public enum MouseButtons { @@ -24,14 +26,17 @@ namespace VAR.ScreenAutomation.Code { input.Data.Mouse.Flags = down ? MOUSEEVENTF_LEFTDOWN : MOUSEEVENTF_LEFTUP; } + if (button == MouseButtons.Middle) { input.Data.Mouse.Flags = down ? MOUSEEVENTF_MIDDLEDOWN : MOUSEEVENTF_MIDDLEUP; } + if (button == MouseButtons.Right) { input.Data.Mouse.Flags = down ? MOUSEEVENTF_RIGHTDOWN : MOUSEEVENTF_RIGHTUP; } + INPUT[] inputs = new INPUT[] { input }; if (SendInput(1, inputs, Marshal.SizeOf(typeof(INPUT))) == 0) throw new Exception(); @@ -73,14 +78,11 @@ namespace VAR.ScreenAutomation.Code [StructLayout(LayoutKind.Explicit)] public struct MOUSEKEYBDHARDWAREINPUT { - [FieldOffset(0)] - public HARDWAREINPUT Hardware; + [FieldOffset(0)] public HARDWAREINPUT Hardware; - [FieldOffset(0)] - public KEYBDINPUT Keyboard; + [FieldOffset(0)] public KEYBDINPUT Keyboard; - [FieldOffset(0)] - public MOUSEINPUT Mouse; + [FieldOffset(0)] public MOUSEINPUT Mouse; } /// @@ -144,7 +146,6 @@ namespace VAR.ScreenAutomation.Code public static extern int SendInput(int nInputs, INPUT[] pInputs, int cbSize); - /// /// Struct representing a point. /// @@ -163,7 +164,6 @@ namespace VAR.ScreenAutomation.Code public static extern bool GetCursorPos(out POINT lpPoint); [DllImport("User32.dll")] - public static extern Boolean SetCursorPos(UInt32 X, UInt32 Y); - + public static extern Boolean SetCursorPos(UInt32 x, UInt32 y); } } \ No newline at end of file diff --git a/VAR.ScreenAutomation/Code/Screenshoter.cs b/VAR.ScreenAutomation/Code/Screenshoter.cs index 127e4ed..8003614 100644 --- a/VAR.ScreenAutomation/Code/Screenshoter.cs +++ b/VAR.ScreenAutomation/Code/Screenshoter.cs @@ -4,17 +4,19 @@ using System.Windows.Forms; namespace VAR.ScreenAutomation.Code { - public class Screenshoter + public static class Screenshoter { public static Bitmap CaptureControl(Control ctrl, Bitmap bmp = null) { if (ctrl == null) { return bmp; } + Point picCapturerOrigin = ctrl.PointToScreen(new Point(0, 0)); bmp = CaptureScreen(bmp, picCapturerOrigin.X, picCapturerOrigin.Y, ctrl.Width, ctrl.Height); return bmp; } - public static Bitmap CaptureScreen(Bitmap bmp = null, int? left = null, int? top = null, int? width = null, int? height = null) + private static Bitmap CaptureScreen(Bitmap bmp = null, int? left = null, int? top = null, int? width = null, + int? height = null) { if (width <= 0 || height <= 0) { return bmp; } @@ -25,9 +27,9 @@ namespace VAR.ScreenAutomation.Code height = height ?? SystemInformation.VirtualScreen.Height; // Create a bitmap of the appropriate size to receive the screenshot. - if (bmp == null || bmp?.Width != width || bmp?.Height != height) + if (bmp == null || bmp.Width != width || bmp.Height != height) { - bmp = new Bitmap(width ?? 0, height ?? 0); + bmp = new Bitmap((int)width, (int)height); } try @@ -35,11 +37,15 @@ namespace VAR.ScreenAutomation.Code // Draw the screenshot into our bitmap. using (Graphics g = Graphics.FromImage(bmp)) { - g.CopyFromScreen(left ?? 0, top ?? 0, 0, 0, bmp.Size); + g.CopyFromScreen((int)left, (int)top, 0, 0, bmp.Size); } } - catch (Exception) { /* Nom Nom Nom */} + catch (Exception) + { + /* Nom Nom Nom */ + } + return bmp; } } -} +} \ No newline at end of file diff --git a/VAR.ScreenAutomation/Code/WindowHandling.cs b/VAR.ScreenAutomation/Code/WindowHandling.cs index bf5364a..fe9f5a4 100644 --- a/VAR.ScreenAutomation/Code/WindowHandling.cs +++ b/VAR.ScreenAutomation/Code/WindowHandling.cs @@ -3,9 +3,11 @@ using System.Diagnostics; using System.Runtime.InteropServices; using System.Windows.Forms; +// ReSharper disable InconsistentNaming + namespace VAR.ScreenAutomation.Code { - public class WindowHandling + public static class WindowHandling { private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1); private static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2); @@ -15,18 +17,15 @@ namespace VAR.ScreenAutomation.Code [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] - private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags); + private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, + uint uFlags); public static void WindowSetTopLevel(Form form, bool top = true) { - if (top) - { - SetWindowPos(form.Handle, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS); - } - else - { - SetWindowPos(form.Handle, HWND_NOTOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS); - } + SetWindowPos(form.Handle, top + ? HWND_TOPMOST + : HWND_NOTOPMOST, + 0, 0, 0, 0, TOPMOST_FLAGS); } public static bool ApplicationIsActivated() @@ -48,4 +47,4 @@ namespace VAR.ScreenAutomation.Code [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern int GetWindowThreadProcessId(IntPtr handle, out int processId); } -} +} \ No newline at end of file diff --git a/VAR.ScreenAutomation/Controls/CtrImageViewer.cs b/VAR.ScreenAutomation/Controls/CtrImageViewer.cs index 361a5eb..082787e 100644 --- a/VAR.ScreenAutomation/Controls/CtrImageViewer.cs +++ b/VAR.ScreenAutomation/Controls/CtrImageViewer.cs @@ -8,7 +8,7 @@ namespace VAR.ScreenAutomation.Controls { #region Declarations - private Image _imageShow = null; + private Image _imageShow; #endregion @@ -16,7 +16,8 @@ namespace VAR.ScreenAutomation.Controls public Image ImageShow { - get { return _imageShow; } + // ReSharper disable once InconsistentlySynchronizedField + get => _imageShow; set { lock (this) @@ -32,6 +33,11 @@ namespace VAR.ScreenAutomation.Controls #region Control life cycle public CtrImageViewer() + { + InitializeComponent(); + } + + private void InitializeComponent() { BackColor = Color.Black; } @@ -59,6 +65,7 @@ namespace VAR.ScreenAutomation.Controls { return; } + lock (_imageShow) { if (graph == null) @@ -71,7 +78,7 @@ namespace VAR.ScreenAutomation.Controls int imgDrawHeight; float imgDrawX = 0; float imgDrawY = 0; - float relation = (float)_imageShow.Width / (float)_imageShow.Height; + float relation = _imageShow.Width / (float)_imageShow.Height; if (relation > 0) { // Imagen mas ancha que alta @@ -110,6 +117,5 @@ namespace VAR.ScreenAutomation.Controls } #endregion - } -} +} \ No newline at end of file diff --git a/VAR.ScreenAutomation/Controls/CtrOutput.cs b/VAR.ScreenAutomation/Controls/CtrOutput.cs index d64ce96..5889580 100644 --- a/VAR.ScreenAutomation/Controls/CtrOutput.cs +++ b/VAR.ScreenAutomation/Controls/CtrOutput.cs @@ -6,6 +6,8 @@ using System.Text; using System.Windows.Forms; using VAR.ScreenAutomation.Interfaces; +// ReSharper disable InconsistentNaming + namespace VAR.ScreenAutomation.Controls { public class CtrOutput : Control, IOutputHandler @@ -18,6 +20,7 @@ namespace VAR.ScreenAutomation.Controls { public string Text { get; set; } public object Data { get; set; } + public override string ToString() { return Text; @@ -37,7 +40,7 @@ namespace VAR.ScreenAutomation.Controls { Dock = DockStyle.Fill, FormattingEnabled = true, - Font = new System.Drawing.Font("Consolas", 9), + Font = new Font("Consolas", 9), BackColor = Color.Black, ForeColor = Color.Gray, SelectionMode = SelectionMode.MultiExtended, @@ -69,6 +72,7 @@ namespace VAR.ScreenAutomation.Controls CopyToClipboard(); return true; } + return base.ProcessCmdKey(ref msg, keyData); } @@ -87,6 +91,7 @@ namespace VAR.ScreenAutomation.Controls { sbText.AppendLine(item.Text); } + if (sbText.Length > 0) { Clipboard.SetText(sbText.ToString()); @@ -106,7 +111,7 @@ namespace VAR.ScreenAutomation.Controls } } - private bool _updated = false; + private bool _updated; private readonly List _pendingOutput = new List(); private void UpdatePosition() @@ -119,6 +124,7 @@ namespace VAR.ScreenAutomation.Controls { _listBox.Items.Add(item); } + _pendingOutput.Clear(); _listBox.ResumeLayout(); @@ -131,7 +137,7 @@ namespace VAR.ScreenAutomation.Controls } [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)] - private static extern IntPtr SendMessage(HandleRef hWnd, Int32 Msg, IntPtr wParam, IntPtr lParam); + private static extern IntPtr SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam); private static void EnableRepaint(HandleRef handle, bool enable) { @@ -168,6 +174,7 @@ namespace VAR.ScreenAutomation.Controls public string GetCurrentText() { if (_listBox.SelectedItems.Count == 0) { return null; } + OutputItem item = (OutputItem)_listBox.SelectedItems[0]; return item?.Text; } @@ -175,8 +182,9 @@ namespace VAR.ScreenAutomation.Controls public object GetCurrentData() { if (_listBox.SelectedItems.Count == 0) { return null; } + OutputItem item = (OutputItem)_listBox.SelectedItems[0]; return item?.Data; } } -} +} \ No newline at end of file diff --git a/VAR.ScreenAutomation/FrmAutomationBotParams.cs b/VAR.ScreenAutomation/FrmAutomationBotParams.cs index 2647ed8..9c3d16c 100644 --- a/VAR.ScreenAutomation/FrmAutomationBotParams.cs +++ b/VAR.ScreenAutomation/FrmAutomationBotParams.cs @@ -9,11 +9,11 @@ namespace VAR.ScreenAutomation { public partial class FrmAutomationBotParams : Form { - private FileBackedConfiguration _config = null; + private readonly FileBackedConfiguration _config; - private BindingList pairs = null; + private BindingList _pairs; - private DataGridView dgvParams = null; + private DataGridView _dgvParams; public FrmAutomationBotParams(FileBackedConfiguration config) { @@ -24,32 +24,34 @@ namespace VAR.ScreenAutomation private void FrmAutomationBotParams_Load(object sender, EventArgs e) { - pairs = new BindingList(); + _pairs = new BindingList(); foreach (string key in _config.GetKeys()) { - pairs.Add(new Pair { Key = key, Value = _config.Get(key, string.Empty), }); + _pairs.Add(new Pair { Key = key, Value = _config.Get(key, string.Empty), }); } - pairs.AddingNew += (s, a) => + + _pairs.AddingNew += (s, a) => { - a.NewObject = new Pair { Parent = pairs }; + a.NewObject = new Pair { Parent = _pairs }; }; - dgvParams = new DataGridView + _dgvParams = new DataGridView { Dock = DockStyle.Fill, - DataSource = pairs + DataSource = _pairs }; - Controls.Add(dgvParams); + Controls.Add(_dgvParams); } private void FrmAutomationBotParams_FormClosing(object sender, FormClosingEventArgs e) { - var parms = new Dictionary(); - foreach (Pair pair in pairs) + foreach (Pair pair in _pairs) { if (string.IsNullOrEmpty(pair.Key)) { continue; } + _config.Set(pair.Key, pair.Value); } + _config.Save(); } @@ -59,23 +61,21 @@ namespace VAR.ScreenAutomation public string Key { get; set; } public string Value { get; set; } - string IDataErrorInfo.Error - { - get { return string.Empty; } - } + string IDataErrorInfo.Error => string.Empty; string IDataErrorInfo.this[string columnName] { get { if (columnName == "Key" && Parent != null && Parent.Any( - x => x.Key == this.Key && !ReferenceEquals(x, this))) + x => x.Key == this.Key && !ReferenceEquals(x, this))) { return "duplicate key"; } + return string.Empty; } } } } -} +} \ No newline at end of file diff --git a/VAR.ScreenAutomation/FrmAutomationBotParams.resx b/VAR.ScreenAutomation/FrmAutomationBotParams.resx index 1af7de1..d5a483d 100644 --- a/VAR.ScreenAutomation/FrmAutomationBotParams.resx +++ b/VAR.ScreenAutomation/FrmAutomationBotParams.resx @@ -1,120 +1,125 @@  - - - - - - - + + + + - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, + PublicKeyToken=b77a5c561934e089 + + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, + PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/VAR.ScreenAutomation/FrmScreenAutomation.cs b/VAR.ScreenAutomation/FrmScreenAutomation.cs index 4f99ba6..d85d534 100644 --- a/VAR.ScreenAutomation/FrmScreenAutomation.cs +++ b/VAR.ScreenAutomation/FrmScreenAutomation.cs @@ -5,23 +5,31 @@ using System.Windows.Forms; using VAR.ScreenAutomation.Code; using VAR.ScreenAutomation.Interfaces; +// ReSharper disable LocalizableElement + namespace VAR.ScreenAutomation { public partial class FrmScreenAutomation : Form { - private bool _running = false; - private IAutomationBot _automationBot = null; + private bool _running; + private IAutomationBot _automationBot; - private Timer timTicker; - private Bitmap bmpScreen = null; + private Timer _timTicker; + private Bitmap _bmpScreen; public FrmScreenAutomation() { - AutoScaleMode = AutoScaleMode.None; - Font = new Font(Font.Name, 8.25f * 96f / CreateGraphics().DpiX, Font.Style, Font.Unit, Font.GdiCharSet, Font.GdiVerticalFont); + PreInitializeComponent(); InitializeComponent(); } + private void PreInitializeComponent() + { + AutoScaleMode = AutoScaleMode.None; + Font = new Font(Font.Name, 8.25f * 96f / CreateGraphics().DpiX, Font.Style, Font.Unit, Font.GdiCharSet, + Font.GdiVerticalFont); + } + private void FrmScreenAutomation_Load(object sender, EventArgs e) { var configuration = new FileBackedConfiguration(); @@ -31,7 +39,8 @@ namespace VAR.ScreenAutomation 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); + splitOutput.SplitterDistance = + configuration.Get("splitOutput.SplitterDistance", splitOutput.SplitterDistance); SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.OptimizedDoubleBuffer, true); @@ -40,7 +49,8 @@ namespace VAR.ScreenAutomation picCapturer.BackColor = Color.LimeGreen; ddlAutomationBot.Items.AddRange(AutomationBotFactory.GetAllAutomationBots()); - ddlAutomationBot.SelectedItem = configuration.Get("ddlAutomationBot", (string)ddlAutomationBot.SelectedItem); + ddlAutomationBot.SelectedItem = + configuration.Get("ddlAutomationBot", (string)ddlAutomationBot.SelectedItem); if (ddlAutomationBot.SelectedIndex < 0) { ddlAutomationBot.SelectedIndex = 0; @@ -52,14 +62,14 @@ namespace VAR.ScreenAutomation chkClick.Checked = configuration.Get("chkClick", chkClick.Checked); if (components == null) { components = new Container(); } - timTicker = new Timer(components) + + _timTicker = new Timer(components) { Interval = Convert.ToInt32(1000 / numFPS.Value), }; - timTicker.Tick += TimTicker_Tick; - timTicker.Enabled = true; - timTicker.Start(); - + _timTicker.Tick += TimTicker_Tick; + _timTicker.Enabled = true; + _timTicker.Start(); } private void FrmScreenAutomation_FormClosing(object sender, FormClosingEventArgs e) @@ -80,14 +90,14 @@ namespace VAR.ScreenAutomation private void TimTicker_Tick(object sender, EventArgs e) { - timTicker.Enabled = false; - timTicker.Stop(); + _timTicker.Enabled = false; + _timTicker.Stop(); - bmpScreen = Screenshoter.CaptureControl(picCapturer, bmpScreen); + _bmpScreen = Screenshoter.CaptureControl(picCapturer, _bmpScreen); if (_automationBot != null) { - bmpScreen = _automationBot.Process(bmpScreen, ctrOutput); + _bmpScreen = _automationBot.Process(_bmpScreen, ctrOutput); if (_running) { string responseKeys = _automationBot.ResponseKeys(); @@ -97,11 +107,12 @@ namespace VAR.ScreenAutomation } } } - picPreview.ImageShow = bmpScreen; - timTicker.Interval = Convert.ToInt32(1000 / numFPS.Value); - timTicker.Enabled = true; - timTicker.Start(); + picPreview.ImageShow = _bmpScreen; + + _timTicker.Interval = Convert.ToInt32(1000 / numFPS.Value); + _timTicker.Enabled = true; + _timTicker.Start(); } private void BtnStartEnd_Click(object sender, EventArgs e) @@ -124,6 +135,7 @@ namespace VAR.ScreenAutomation private void BtnAutomationBotConfig_Click(object sender, EventArgs e) { if (_automationBot == null) { return; } + IConfiguration defaultConfig = _automationBot.GetDefaultConfiguration(); var config = new FileBackedConfiguration(_automationBot.Name); config.Load(defaultConfig); @@ -135,6 +147,7 @@ namespace VAR.ScreenAutomation { WindowHandling.WindowSetTopLevel(this, false); } + frmAutomationBotParameters.ShowDialog(); InitBot(_automationBot.Name); if (_isToplevel) @@ -146,20 +159,22 @@ namespace VAR.ScreenAutomation private void Start() { if (_running) { return; } + _running = true; btnStartEnd.Text = "End"; InitBot((string)ddlAutomationBot.SelectedItem); - if (chkClick.Checked) - { - Point pointCapturerCenter = picCapturer.PointToScreen(new Point(picCapturer.Width / 2, picCapturer.Height / 2)); - Mouse.SetPosition((uint)pointCapturerCenter.X, (uint)pointCapturerCenter.Y); - Mouse.Click(Mouse.MouseButtons.Left); - } + if (!chkClick.Checked) { return; } + + Point pointCapturerCenter = + picCapturer.PointToScreen(new Point(picCapturer.Width / 2, picCapturer.Height / 2)); + Mouse.SetPosition((uint)pointCapturerCenter.X, (uint)pointCapturerCenter.Y); + Mouse.Click(Mouse.MouseButtons.Left); } private void End() { if (_running == false) { return; } + _running = false; btnStartEnd.Text = "Start"; } @@ -177,22 +192,24 @@ namespace VAR.ScreenAutomation WindowSetTopLevel(chkKeepToplevel.Checked); } - private bool _isToplevel = false; + private bool _isToplevel; private void WindowSetTopLevel(bool toplevel) { if (toplevel) { if (_isToplevel) { return; } + WindowHandling.WindowSetTopLevel(this); _isToplevel = true; } else { if (_isToplevel == false) { return; } + WindowHandling.WindowSetTopLevel(this, false); _isToplevel = false; } } } -} +} \ No newline at end of file diff --git a/VAR.ScreenAutomation/FrmScreenAutomation.resx b/VAR.ScreenAutomation/FrmScreenAutomation.resx index 1af7de1..d5a483d 100644 --- a/VAR.ScreenAutomation/FrmScreenAutomation.resx +++ b/VAR.ScreenAutomation/FrmScreenAutomation.resx @@ -1,120 +1,125 @@  - - - - - - - + + + + - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, + PublicKeyToken=b77a5c561934e089 + + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, + PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/VAR.ScreenAutomation/Interfaces/IAutomationBot.cs b/VAR.ScreenAutomation/Interfaces/IAutomationBot.cs index c546956..b4364e4 100644 --- a/VAR.ScreenAutomation/Interfaces/IAutomationBot.cs +++ b/VAR.ScreenAutomation/Interfaces/IAutomationBot.cs @@ -10,4 +10,4 @@ namespace VAR.ScreenAutomation.Interfaces Bitmap Process(Bitmap bmpInput, IOutputHandler output); string ResponseKeys(); } -} +} \ No newline at end of file diff --git a/VAR.ScreenAutomation/Interfaces/IOutputHandler.cs b/VAR.ScreenAutomation/Interfaces/IOutputHandler.cs index b86c242..99002e6 100644 --- a/VAR.ScreenAutomation/Interfaces/IOutputHandler.cs +++ b/VAR.ScreenAutomation/Interfaces/IOutputHandler.cs @@ -5,4 +5,4 @@ void Clean(); void AddLine(string line, object data = null); } -} +} \ No newline at end of file diff --git a/VAR.ScreenAutomation/Program.cs b/VAR.ScreenAutomation/Program.cs index 308f05b..62a2ead 100644 --- a/VAR.ScreenAutomation/Program.cs +++ b/VAR.ScreenAutomation/Program.cs @@ -16,4 +16,4 @@ namespace VAR.ScreenAutomation Application.Run(new FrmScreenAutomation()); } } -} +} \ No newline at end of file diff --git a/VAR.ScreenAutomation/Properties/AssemblyInfo.cs b/VAR.ScreenAutomation/Properties/AssemblyInfo.cs index 1f625a3..0ece480 100644 --- a/VAR.ScreenAutomation/Properties/AssemblyInfo.cs +++ b/VAR.ScreenAutomation/Properties/AssemblyInfo.cs @@ -1,5 +1,4 @@ using System.Reflection; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; // General Information about an assembly is controlled through the following @@ -33,4 +32,4 @@ using System.Runtime.InteropServices; // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] [assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] \ No newline at end of file diff --git a/VAR.ScreenAutomation/Properties/Resources.resx b/VAR.ScreenAutomation/Properties/Resources.resx index af7dbeb..1c875c9 100644 --- a/VAR.ScreenAutomation/Properties/Resources.resx +++ b/VAR.ScreenAutomation/Properties/Resources.resx @@ -1,117 +1,122 @@  - - - - - - + + + - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, + PublicKeyToken=b77a5c561934e089 + + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, + PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/VAR.ScreenAutomation/Properties/Settings.settings b/VAR.ScreenAutomation/Properties/Settings.settings index 3964565..796d34b 100644 --- a/VAR.ScreenAutomation/Properties/Settings.settings +++ b/VAR.ScreenAutomation/Properties/Settings.settings @@ -1,7 +1,7 @@  - - - - + + + + diff --git a/VAR.ScreenAutomation/VAR.ScreenAutomation.csproj b/VAR.ScreenAutomation/VAR.ScreenAutomation.csproj index 16cb54a..905957e 100644 --- a/VAR.ScreenAutomation/VAR.ScreenAutomation.csproj +++ b/VAR.ScreenAutomation/VAR.ScreenAutomation.csproj @@ -1,113 +1,113 @@  - - - Debug - AnyCPU - {E2BE8E2A-3422-42A6-82FA-5E0CCDC42032} - WinExe - VAR.ScreenAutomation - VAR.ScreenAutomation - v4.7.2 - 512 - true - true - - - AnyCPU - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - AnyCPU - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - app.manifest - - - - - - - - - - - - - - - - - - - - - - Form - - - FrmAutomationBotParams.cs - - - - - - - Component - - - Component - - - Form - - - FrmScreenAutomation.cs - - - - - - - FrmAutomationBotParams.cs - - - FrmScreenAutomation.cs - - - ResXFileCodeGenerator - Resources.Designer.cs - Designer - - - True - Resources.resx - - - - SettingsSingleFileGenerator - Settings.Designer.cs - - - True - Settings.settings - True - - - - - - + + + Debug + AnyCPU + {E2BE8E2A-3422-42A6-82FA-5E0CCDC42032} + WinExe + VAR.ScreenAutomation + VAR.ScreenAutomation + v4.7.2 + 512 + true + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + app.manifest + + + + + + + + + + + + + + + + + + + + + + Form + + + FrmAutomationBotParams.cs + + + + + + + Component + + + Component + + + Form + + + FrmScreenAutomation.cs + + + + + + + FrmAutomationBotParams.cs + + + FrmScreenAutomation.cs + + + ResXFileCodeGenerator + Resources.Designer.cs + Designer + + + True + Resources.resx + + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + True + Settings.settings + True + + + + + + \ No newline at end of file diff --git a/VAR.ScreenAutomation/app.manifest b/VAR.ScreenAutomation/app.manifest index 648dc43..d25cb99 100644 --- a/VAR.ScreenAutomation/app.manifest +++ b/VAR.ScreenAutomation/app.manifest @@ -1,76 +1,76 @@  - - - - - + + + + - - - + + + - Specifying requestedExecutionLevel element will disable file and registry virtualization. - Remove this element if your application requires this virtualization for backwards - compatibility. - --> - - - - + + - - - + + - - + + - - + + - - + + - - + + - - + + + + true + - - - - - - true - - - - - + +