Permalink
Browse files

Resharper code cleanup pass of HearthCap.Updater and PHash projects

  • Loading branch information...
1 parent b518d7c commit 1271ce0535d8a4cbbc6f7e8e005b61f487903c5e @remcoros remcoros committed Aug 29, 2015
@@ -1,6 +1,7 @@
-<?xml version="1.0" encoding="utf-8" ?>
+<?xml version="1.0" encoding="utf-8"?>
+
<configuration>
- <startup>
+ <startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
@@ -1,19 +1,20 @@
-namespace HearthCap.Updater
-{
- using System;
- using System.IO;
- using System.Windows.Forms;
-
- using NLog;
- using NLog.Config;
- using NLog.Targets;
+using System;
+using System.IO;
+using System.Windows.Forms;
+using NLog;
+using NLog.Config;
+using NLog.Targets;
- class Program
+namespace HearthCap.Updater
+{
+ internal class Program
{
- static void Main(string[] args)
+ private static void Main(string[] args)
{
if (args.Length <= 0)
+ {
return;
+ }
var config = new LoggingConfiguration();
var logfile = new FileTarget();
@@ -31,10 +32,10 @@ static void Main(string[] args)
config.AddTarget("logfile", logfile);
config.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, logfile));
LogManager.Configuration = config;
-
+
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
- Application.Run(new UpdateProgress(args));
+ Application.Run(new UpdateProgress(args));
}
}
}
@@ -1,10 +1,10 @@
using System.Reflection;
-using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
+
[assembly: AssemblyTitle("HearthCap.Updater")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
@@ -17,9 +17,11 @@
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
+
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
+
[assembly: Guid("434fdbc4-c843-4bbd-90e2-f444493029d0")]
// Version information for an assembly consists of the following four values:
@@ -32,5 +34,6 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
+
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
@@ -1,23 +1,21 @@
using System;
+using System.Diagnostics;
+using System.IO;
+using System.IO.Compression;
using System.Linq;
+using System.Runtime.InteropServices;
+using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
+using NLog;
namespace HearthCap.Updater
{
- using System.Diagnostics;
- using System.IO;
- using System.IO.Compression;
- using System.Runtime.InteropServices;
- using System.Threading;
-
- using NLog;
-
public partial class UpdateProgress : Form
{
- private static Logger Log = LogManager.GetCurrentClassLogger();
+ private static readonly Logger Log = LogManager.GetCurrentClassLogger();
- private bool isTesting = false;
+ private readonly bool isTesting;
private readonly string installFile;
@@ -30,40 +28,41 @@ public UpdateProgress()
public UpdateProgress(string[] args)
{
InitializeComponent();
- this.Shown += OnShown;
+ Shown += OnShown;
- this.installFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, args[0]);
+ installFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, args[0]);
if (args.Length >= 2)
{
- this.installPath = args[1];
- if (this.installPath.EndsWith("\""))
+ installPath = args[1];
+ if (installPath.EndsWith("\""))
{
- this.installPath = args[1].Substring(0, args[1].Length - 1);
+ installPath = args[1].Substring(0, args[1].Length - 1);
}
}
else
{
// this.installPath = AppDomain.CurrentDomain.BaseDirectory;
- this.installPath = Directory.GetCurrentDirectory();
+ installPath = Directory.GetCurrentDirectory();
}
- this.isTesting = args.Any(x => x.Contains("--testing"));
+ isTesting = args.Any(x => x.Contains("--testing"));
}
private void OnShown(object sender, EventArgs eventArgs)
{
- Task.Run(() => this.DoUpdate());
+ Task.Run(() => DoUpdate());
}
private void DoUpdate()
{
var success = false;
-
+
try
{
var retrycount = 0;
- while (retrycount < 5 && IsRunning())
+ while (retrycount < 5
+ && IsRunning())
{
Thread.Sleep(1000);
retrycount++;
@@ -84,16 +83,16 @@ private void DoUpdate()
}
else
{
- success = DoUpdate(this.installFile);
- File.Delete(this.installFile);
+ success = DoUpdate(installFile);
+ File.Delete(installFile);
}
Thread.Sleep(500);
var targs = success ? "-updated" : "-updatefailed";
- var pi = new ProcessStartInfo(Path.Combine(this.installPath, "HearthCap.exe"), targs)
- {
- WorkingDirectory = this.installPath
- };
+ var pi = new ProcessStartInfo(Path.Combine(installPath, "HearthCap.exe"), targs)
+ {
+ WorkingDirectory = installPath
+ };
Process.Start(pi);
}
catch (Exception ex)
@@ -102,7 +101,7 @@ private void DoUpdate()
}
finally
{
- this.Hide();
+ Hide();
Application.Exit();
}
}
@@ -113,46 +112,50 @@ private bool DoUpdate(string zipfile)
try
{
// Cleanup temp files
- var dir = new DirectoryInfo(this.installPath);
+ var dir = new DirectoryInfo(installPath);
var tempFiles = dir.GetFiles().Where(
x =>
- x.Extension.ToLower().EndsWith(".pendingoverwrite") ||
- x.Extension.ToLower().EndsWith(".tmp")).ToList();
+ x.Extension.ToLower().EndsWith(".pendingoverwrite") ||
+ x.Extension.ToLower().EndsWith(".tmp")).ToList();
foreach (var tempFile in tempFiles)
{
try
{
Log.Warn("Deleting tempory file: " + tempFile.Name);
File.Delete(tempFile.FullName);
}
- catch { }
+ catch
+ {
+ }
}
// HACK: work-around Capture.dll in use.
// Rename it to .tmp so it will get cleaned on next update
- var fromCapDll = Path.Combine(this.installPath, "Capture.dll");
- var toCapDll = Path.Combine(this.installPath, "Capture.dll.tmp");
+ var fromCapDll = Path.Combine(installPath, "Capture.dll");
+ var toCapDll = Path.Combine(installPath, "Capture.dll.tmp");
File.Move(fromCapDll, toCapDll);
- using(var file = File.OpenRead(zipfile))
- using (var zip = new ZipArchive(file, ZipArchiveMode.Read))
+ using (var file = File.OpenRead(zipfile))
{
- foreach (var entry in zip.Entries)
+ using (var zip = new ZipArchive(file, ZipArchiveMode.Read))
{
- var targetFile = Path.Combine(this.installPath, entry.FullName);
- Log.Debug("Extracting: " + entry.FullName);
- try
- {
- entry.ExtractToFile(targetFile, true);
- }
- catch (IOException ex)
+ foreach (var entry in zip.Entries)
{
- Log.Error(ex);
- Log.Info("Renaming inaccesable file: " + targetFile);
- var tmpTargetFile = targetFile + ".tmp";
- File.Move(targetFile, tmpTargetFile);
+ var targetFile = Path.Combine(installPath, entry.FullName);
Log.Debug("Extracting: " + entry.FullName);
- entry.ExtractToFile(targetFile, true);
+ try
+ {
+ entry.ExtractToFile(targetFile, true);
+ }
+ catch (IOException ex)
+ {
+ Log.Error(ex);
+ Log.Info("Renaming inaccesable file: " + targetFile);
+ var tmpTargetFile = targetFile + ".tmp";
+ File.Move(targetFile, tmpTargetFile);
+ Log.Debug("Extracting: " + entry.FullName);
+ entry.ExtractToFile(targetFile, true);
+ }
}
}
}
@@ -209,6 +212,6 @@ private static bool CheckNotRunning(out bool retry)
}
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
- static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
+ private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
}
}
@@ -1,13 +1,11 @@
-namespace PHash.AForge
-{
- using System;
- using System.Drawing.Imaging;
-
- using global::AForge.Imaging;
- using global::AForge.Imaging.Filters;
-
- using MathNet.Numerics.LinearAlgebra.Double;
+using System;
+using System.Drawing.Imaging;
+using AForge.Imaging;
+using AForge.Imaging.Filters;
+using MathNet.Numerics.LinearAlgebra.Double;
+namespace PHash.AForge
+{
public class AForgePerceptualHash : PerceptualHash
{
private const int dctsize = 8;
@@ -23,25 +21,27 @@ public override ulong Create(BitmapData image)
var data = new DenseMatrix(32, 32);
using (var unmanaged = new UnmanagedImage(image))
- using (var filtered = ExtractChannel.Apply(unmanaged))
{
- Filter.ApplyInPlace(filtered);
-
- using (var imgdata = Resize.Apply(filtered))
+ using (var filtered = ExtractChannel.Apply(unmanaged))
{
- unsafe
+ Filter.ApplyInPlace(filtered);
+
+ using (var imgdata = Resize.Apply(filtered))
{
- byte* src = (byte*)imgdata.ImageData.ToPointer();
- int offset = imgdata.Stride - imgdata.Width;
- int width = imgdata.Width;
- int height = imgdata.Height;
- for (int y = 0; y < height; y++)
+ unsafe
{
- for (int x = 0; x < width; x++, src++)
+ var src = (byte*)imgdata.ImageData.ToPointer();
+ var offset = imgdata.Stride - imgdata.Width;
+ var width = imgdata.Width;
+ var height = imgdata.Height;
+ for (var y = 0; y < height; y++)
{
- data.At(y, x, (float)*src / 255);
+ for (var x = 0; x < width; x++, src++)
+ {
+ data.At(y, x, (float)*src / 255);
+ }
+ src += offset;
}
- src += offset;
}
}
}
@@ -50,10 +50,10 @@ public override ulong Create(BitmapData image)
var dct = DctMatrix.FastDCT(data);
var vals = new double[dctsize * dctsize];
- int valscount = 0;
- for (int r = 1; r <= dctsize; r++)
+ var valscount = 0;
+ for (var r = 1; r <= dctsize; r++)
{
- for (int c = 1; c <= dctsize; c++)
+ for (var c = 1; c <= dctsize; c++)
{
vals[valscount] = dct.At(r, c);
++valscount;
@@ -65,11 +65,11 @@ public override ulong Create(BitmapData image)
Array.Sort(sorted);
var mid = dctsize * dctsize / 2;
- double median = (sorted[mid - 1] + sorted[mid]) / 2d;
+ var median = (sorted[mid - 1] + sorted[mid]) / 2d;
ulong index = 1;
ulong result = 0;
- for (int i = 0; i < dctsize * dctsize; i++)
+ for (var i = 0; i < dctsize * dctsize; i++)
{
if (vals[i] > median)
{
@@ -82,4 +82,4 @@ public override ulong Create(BitmapData image)
return result;
}
}
-}
+}
Oops, something went wrong.

0 comments on commit 1271ce0

Please sign in to comment.