Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
Refactored several files, and made loosely coupled code.
  • Loading branch information
kingnakama committed Oct 1, 2020
1 parent cfa6d79 commit 528ecca
Show file tree
Hide file tree
Showing 112 changed files with 33,018 additions and 310 deletions.
10 changes: 6 additions & 4 deletions App.config
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>

<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
Expand All @@ -18,7 +19,8 @@
<bindingRedirect oldVersion="0.0.0.0-4.1.4.0" newVersion="4.1.4.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a"
culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.6.0" newVersion="4.0.6.0" />
</dependentAssembly>
<dependentAssembly>
Expand Down
23 changes: 14 additions & 9 deletions App.xaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
<Application x:Class="NClicker.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:NClicker"
StartupUri="MainWindow.xaml">
<Application
x:Class="NClicker.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:NClicker"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Dark.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.Lime.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />
<ResourceDictionary
Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Dark.xaml" />
<ResourceDictionary
Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<ResourceDictionary
Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.Lime.xaml" />
<ResourceDictionary
Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Expand Down
6 changes: 4 additions & 2 deletions Constants.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
using System;
using NClicker.Models;
using System;
using System.Runtime.InteropServices;
using NClicker.Models;

namespace NClicker
{
public static class Constants
{
public const string PaypalUrl = "https://paypal.me/nakamaa";

public static readonly bool IsNetCore = RuntimeInformation.FrameworkDescription
.StartsWith(".NET Core", StringComparison.OrdinalIgnoreCase);

public static RunConfiguration DefaultConfig => new RunConfiguration
{
Seconds = 1,
Expand Down
19 changes: 9 additions & 10 deletions ContainerModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,23 @@ public class ContainerModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<IStorage>()
.InstancePerLifetimeScope()
.PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies);
//Storages
builder.Register(c => new PresetStorage()).SingleInstance();
builder.Register(c => new PresetStorage()).As<IPresetStorage>().SingleInstance();

//Services
builder.Register(c => new PresetService()).SingleInstance();
builder.Register(c => new PresetService(c.Resolve<IPresetStorage>()))
.As<IPresetService>().SingleInstance();
builder.Register(c => new MouseControllerService(new System.Random()))
.As<IMouseControllerService>().SingleInstance();

builder.Register(c => new MouseControllerService()).SingleInstance();
builder.Register(c => new GlobalKeyboardHook()).SingleInstance();

builder.Register(c => new KeyboardControllerService(
c.Resolve<GlobalKeyboardHook>())).SingleInstance();
builder.Register(c => new KeyboardService(
c.Resolve<GlobalKeyboardHook>())).As<IKeyboardService>().SingleInstance();

builder.Register(c => new MainViewModel(
c.Resolve<PresetStorage>(), c.Resolve<PresetService>(),
c.Resolve<KeyboardControllerService>())).SingleInstance();
c.Resolve<IPresetStorage>(), c.Resolve<IPresetService>(),
c.Resolve<IKeyboardService>())).SingleInstance();
}
}
}
18 changes: 13 additions & 5 deletions Hooks/GlobalKeyboardHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,23 @@ public GlobalKeyboardHook()
{
_windowsHookHandle = IntPtr.Zero;
_user32LibraryHandle = IntPtr.Zero;
_hookProc = LowLevelKeyboardProc; // we must keep alive _hookProc, because GC is not aware about SetWindowsHookEx behaviour.
_hookProc =
LowLevelKeyboardProc; // we must keep alive _hookProc, because GC is not aware about SetWindowsHookEx behaviour.

_user32LibraryHandle = LoadLibrary("User32");
if (_user32LibraryHandle == IntPtr.Zero)
{
int errorCode = Marshal.GetLastWin32Error();
throw new Win32Exception(errorCode, $"Failed to load library 'User32.dll'. Error {errorCode}: {new Win32Exception(Marshal.GetLastWin32Error()).Message}.");
throw new Win32Exception(errorCode,
$"Failed to load library 'User32.dll'. Error {errorCode}: {new Win32Exception(Marshal.GetLastWin32Error()).Message}.");
}

_windowsHookHandle = SetWindowsHookEx(WH_KEYBOARD_LL, _hookProc, _user32LibraryHandle, 0);
if (_windowsHookHandle == IntPtr.Zero)
{
int errorCode = Marshal.GetLastWin32Error();
throw new Win32Exception(errorCode, $"Failed to adjust keyboard hooks for '{Process.GetCurrentProcess().ProcessName}'. Error {errorCode}: {new Win32Exception(Marshal.GetLastWin32Error()).Message}.");
throw new Win32Exception(errorCode,
$"Failed to adjust keyboard hooks for '{Process.GetCurrentProcess().ProcessName}'. Error {errorCode}: {new Win32Exception(Marshal.GetLastWin32Error()).Message}.");
}
}

Expand All @@ -40,8 +43,10 @@ private void Dispose(bool disposing)
if (!UnhookWindowsHookEx(_windowsHookHandle))
{
int errorCode = Marshal.GetLastWin32Error();
throw new Win32Exception(errorCode, $"Failed to remove keyboard hooks for '{Process.GetCurrentProcess().ProcessName}'. Error {errorCode}: {new Win32Exception(Marshal.GetLastWin32Error()).Message}.");
throw new Win32Exception(errorCode,
$"Failed to remove keyboard hooks for '{Process.GetCurrentProcess().ProcessName}'. Error {errorCode}: {new Win32Exception(Marshal.GetLastWin32Error()).Message}.");
}

_windowsHookHandle = IntPtr.Zero;

// ReSharper disable once DelegateSubtraction
Expand All @@ -54,8 +59,10 @@ private void Dispose(bool disposing)
if (!FreeLibrary(_user32LibraryHandle)) // reduces reference to library by 1.
{
int errorCode = Marshal.GetLastWin32Error();
throw new Win32Exception(errorCode, $"Failed to unload library 'User32.dll'. Error {errorCode}: {new Win32Exception(Marshal.GetLastWin32Error()).Message}.");
throw new Win32Exception(errorCode,
$"Failed to unload library 'User32.dll'. Error {errorCode}: {new Win32Exception(Marshal.GetLastWin32Error()).Message}.");
}

_user32LibraryHandle = IntPtr.Zero;
}
}
Expand Down Expand Up @@ -183,6 +190,7 @@ public IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam)
handler?.Invoke(this, eventArguments);
fEatKeyStroke = eventArguments.Handled;
}

return fEatKeyStroke ? (IntPtr)1 : CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam);
}
}
Expand Down
Loading

0 comments on commit 528ecca

Please sign in to comment.