From 3c4b750b1edbc00675ea68dcc7a4d8cafeb3b9b9 Mon Sep 17 00:00:00 2001 From: SpoinkyNL Date: Thu, 10 Mar 2016 09:23:29 +0100 Subject: [PATCH] NamedPipe for Division WIP --- Artemis/Artemis/App.xaml.cs | 36 ++------------ Artemis/Artemis/Artemis.csproj | 12 +++-- Artemis/Artemis/ArtemisBootstrapper.cs | 11 +++-- .../AmbientLightning}/ScreenCapture.cs | 2 +- .../AudioVisualizer/AudioVisualizerModel.cs | 2 +- .../Utilities}/FftEventArgs.cs | 10 ++-- .../Utilities}/SampleAggregator.cs | 8 ++-- Artemis/Artemis/Utilities/GeneralHelpers.cs | 48 +++++++++++++++++++ .../Utilities/LogitechDll/DllManager.cs | 41 ++++++++-------- .../LogitechDll/LogitechNamedPipe.cs | 23 +++++++++ Artemis/Artemis/packages.config | 1 + 11 files changed, 121 insertions(+), 73 deletions(-) rename Artemis/Artemis/{Utilities => Modules/Effects/AmbientLightning}/ScreenCapture.cs (96%) rename Artemis/Artemis/{Utilities/Audio => Modules/Effects/AudioVisualizer/Utilities}/FftEventArgs.cs (82%) rename Artemis/Artemis/{Utilities/Audio => Modules/Effects/AudioVisualizer/Utilities}/SampleAggregator.cs (96%) create mode 100644 Artemis/Artemis/Utilities/GeneralHelpers.cs create mode 100644 Artemis/Artemis/Utilities/LogitechDll/LogitechNamedPipe.cs diff --git a/Artemis/Artemis/App.xaml.cs b/Artemis/Artemis/App.xaml.cs index 4dc135af8..04e288836 100644 --- a/Artemis/Artemis/App.xaml.cs +++ b/Artemis/Artemis/App.xaml.cs @@ -1,9 +1,7 @@ using System; -using System.Diagnostics; -using System.Reflection; -using System.Security.Principal; using System.Windows; using System.Windows.Threading; +using Artemis.Utilities; using WpfExceptionViewer; namespace Artemis @@ -15,42 +13,14 @@ namespace Artemis { public App() { - if (!IsRunAsAdministrator()) - { - var processInfo = new ProcessStartInfo(Assembly.GetExecutingAssembly().CodeBase); - - // The following properties run the new process as administrator - processInfo.UseShellExecute = true; - processInfo.Verb = "runas"; - - // Start the new process - try - { - Process.Start(processInfo); - } - catch (Exception) - { - // The user did not allow the application to run as administrator - MessageBox.Show("Sorry, this application must be run as Administrator."); - } - - // Shut down the current process - Environment.Exit(0); - } + if (!GeneralHelpers.IsRunAsAdministrator()) + GeneralHelpers.RunAsAdministrator(); InitializeComponent(); } public bool DoHandle { get; set; } - private bool IsRunAsAdministrator() - { - var wi = WindowsIdentity.GetCurrent(); - var wp = new WindowsPrincipal(wi); - - return wp.IsInRole(WindowsBuiltInRole.Administrator); - } - private void Application_Startup(object sender, StartupEventArgs e) { AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; diff --git a/Artemis/Artemis/Artemis.csproj b/Artemis/Artemis/Artemis.csproj index de223736e..099e59535 100644 --- a/Artemis/Artemis/Artemis.csproj +++ b/Artemis/Artemis/Artemis.csproj @@ -163,6 +163,10 @@ False + + ..\packages\NamedPipeWrapper.1.4.0\lib\net40\NamedPipeWrapper.dll + True + ..\packages\NAudio.1.7.3\lib\net35\NAudio.dll True @@ -271,12 +275,15 @@ AmbientLightningEffectView.xaml + AudioVisualization.settings True True + + @@ -346,21 +353,20 @@ True Offsets.settings - - + + - diff --git a/Artemis/Artemis/ArtemisBootstrapper.cs b/Artemis/Artemis/ArtemisBootstrapper.cs index 934a41e27..29dffcc6b 100644 --- a/Artemis/Artemis/ArtemisBootstrapper.cs +++ b/Artemis/Artemis/ArtemisBootstrapper.cs @@ -1,14 +1,13 @@ -using System; -using System.Diagnostics; +using System.Diagnostics; using System.Linq; using System.Windows; using System.Windows.Forms; +using Artemis.Utilities; using Artemis.Utilities.LogitechDll; using Artemis.ViewModels; using Autofac; using Caliburn.Micro; using Caliburn.Micro.Autofac; -using Microsoft.Win32; using Application = System.Windows.Application; using MessageBox = System.Windows.Forms.MessageBox; @@ -18,8 +17,10 @@ namespace Artemis { public ArtemisBootstrapper() { - //CheckDuplicateInstances(); - DllManager.RestoreDll(); + CheckDuplicateInstances(); + if (DllManager.RestoreDll()) + GeneralHelpers.RunAsAdministrator(); + Initialize(); } diff --git a/Artemis/Artemis/Utilities/ScreenCapture.cs b/Artemis/Artemis/Modules/Effects/AmbientLightning/ScreenCapture.cs similarity index 96% rename from Artemis/Artemis/Utilities/ScreenCapture.cs rename to Artemis/Artemis/Modules/Effects/AmbientLightning/ScreenCapture.cs index 0bbc29614..7b3b32c93 100644 --- a/Artemis/Artemis/Utilities/ScreenCapture.cs +++ b/Artemis/Artemis/Modules/Effects/AmbientLightning/ScreenCapture.cs @@ -13,7 +13,7 @@ using MapFlags = SharpDX.DXGI.MapFlags; using Resource = SharpDX.DXGI.Resource; using ResultCode = SharpDX.DXGI.ResultCode; -namespace Artemis.Utilities +namespace Artemis.Modules.Effects.AmbientLightning { internal class ScreenCapture : IDisposable { diff --git a/Artemis/Artemis/Modules/Effects/AudioVisualizer/AudioVisualizerModel.cs b/Artemis/Artemis/Modules/Effects/AudioVisualizer/AudioVisualizerModel.cs index 3452c7c04..754cb4310 100644 --- a/Artemis/Artemis/Modules/Effects/AudioVisualizer/AudioVisualizerModel.cs +++ b/Artemis/Artemis/Modules/Effects/AudioVisualizer/AudioVisualizerModel.cs @@ -5,8 +5,8 @@ using System.Drawing.Drawing2D; using System.Linq; using Artemis.Managers; using Artemis.Models; +using Artemis.Modules.Effects.AudioVisualizer.Utilities; using Artemis.Utilities; -using Artemis.Utilities.Audio; using Artemis.Utilities.Keyboard; using NAudio.CoreAudioApi; using NAudio.Wave; diff --git a/Artemis/Artemis/Utilities/Audio/FftEventArgs.cs b/Artemis/Artemis/Modules/Effects/AudioVisualizer/Utilities/FftEventArgs.cs similarity index 82% rename from Artemis/Artemis/Utilities/Audio/FftEventArgs.cs rename to Artemis/Artemis/Modules/Effects/AudioVisualizer/Utilities/FftEventArgs.cs index de4a23650..69faaf373 100644 --- a/Artemis/Artemis/Utilities/Audio/FftEventArgs.cs +++ b/Artemis/Artemis/Modules/Effects/AudioVisualizer/Utilities/FftEventArgs.cs @@ -1,8 +1,8 @@ -using System; -using System.Diagnostics; -using NAudio.Dsp; - -namespace Artemis.Utilities.Audio +using System; +using System.Diagnostics; +using NAudio.Dsp; + +namespace Artemis.Modules.Effects.AudioVisualizer.Utilities { public class FftEventArgs : EventArgs { diff --git a/Artemis/Artemis/Utilities/Audio/SampleAggregator.cs b/Artemis/Artemis/Modules/Effects/AudioVisualizer/Utilities/SampleAggregator.cs similarity index 96% rename from Artemis/Artemis/Utilities/Audio/SampleAggregator.cs rename to Artemis/Artemis/Modules/Effects/AudioVisualizer/Utilities/SampleAggregator.cs index 89833485f..ce68f4034 100644 --- a/Artemis/Artemis/Utilities/Audio/SampleAggregator.cs +++ b/Artemis/Artemis/Modules/Effects/AudioVisualizer/Utilities/SampleAggregator.cs @@ -1,7 +1,7 @@ -using System; -using NAudio.Dsp; - -namespace Artemis.Utilities.Audio +using System; +using NAudio.Dsp; + +namespace Artemis.Modules.Effects.AudioVisualizer.Utilities { // The Complex and FFT are here! public class SampleAggregator diff --git a/Artemis/Artemis/Utilities/GeneralHelpers.cs b/Artemis/Artemis/Utilities/GeneralHelpers.cs new file mode 100644 index 000000000..b83ed3751 --- /dev/null +++ b/Artemis/Artemis/Utilities/GeneralHelpers.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Reflection; +using System.Security.Principal; +using System.Text; +using System.Threading.Tasks; +using System.Windows; + +namespace Artemis.Utilities +{ + public static class GeneralHelpers + { + public static void RunAsAdministrator() + { + var processInfo = new ProcessStartInfo(Assembly.GetExecutingAssembly().CodeBase) + { + UseShellExecute = true, + Verb = "runas" + }; + + // The following properties run the new process as administrator + + // Start the new process + try + { + Process.Start(processInfo); + } + catch (Exception) + { + // The user did not allow the application to run as administrator + MessageBox.Show("Sorry, this application must be run as Administrator."); + } + + // Shut down the current process + Environment.Exit(0); + } + + public static bool IsRunAsAdministrator() + { + var wi = WindowsIdentity.GetCurrent(); + var wp = new WindowsPrincipal(wi); + + return wp.IsInRole(WindowsBuiltInRole.Administrator); + } + } +} diff --git a/Artemis/Artemis/Utilities/LogitechDll/DllManager.cs b/Artemis/Artemis/Utilities/LogitechDll/DllManager.cs index af0782908..1485c2272 100644 --- a/Artemis/Artemis/Utilities/LogitechDll/DllManager.cs +++ b/Artemis/Artemis/Utilities/LogitechDll/DllManager.cs @@ -6,16 +6,20 @@ namespace Artemis.Utilities.LogitechDll { internal static class DllManager { - public static void RestoreDll() + private const string LogitechPath = @"C:\Program Files\Logitech Gaming Software\SDK\LED\x64\"; + + public static bool RestoreDll() { - if (!BackupAvailable()) - return; + if (!File.Exists(LogitechPath + @"\LogitechLed.dll.bak")) + return false; // Get rid of our own DLL - File.Delete(@"C:\Program Files\Logitech Gaming Software\SDK\LED\x64\LogitechLed.dll"); + File.Delete(LogitechPath + @"\LogitechLed.dll"); // Restore the backup - File.Move(@"C:\Program Files\Logitech Gaming Software\SDK\LED\x64\LogitechLed.dll.bak", - @"C:\Program Files\Logitech Gaming Software\SDK\LED\x64\LogitechLed.dll"); + File.Move(LogitechPath + @"\LogitechLed.dll.bak", + LogitechPath + @"\LogitechLed.dll"); + + return true; } public static void PlaceDll() @@ -24,19 +28,19 @@ namespace Artemis.Utilities.LogitechDll return; // Create directory structure, just in case - Directory.CreateDirectory(@"C:\Program Files\Logitech Gaming Software\SDK\LED\x64"); + Directory.CreateDirectory(LogitechPath + @""); // Remove old backups if they are there - if (BackupAvailable()) - File.Delete(@"C:\Program Files\Logitech Gaming Software\SDK\LED\x64\LogitechLed.dll.bak"); + if (File.Exists(LogitechPath + @"\LogitechLed.dll.bak")) + File.Delete(LogitechPath + @"\LogitechLed.dll.bak"); // Backup the existing DLL - if (File.Exists(@"C:\Program Files\Logitech Gaming Software\SDK\LED\x64\LogitechLed.dll")) - File.Move(@"C:\Program Files\Logitech Gaming Software\SDK\LED\x64\LogitechLed.dll", - @"C:\Program Files\Logitech Gaming Software\SDK\LED\x64\LogitechLed.dll.bak"); + if (File.Exists(LogitechPath + @"\LogitechLed.dll")) + File.Move(LogitechPath + @"\LogitechLed.dll", + LogitechPath + @"\LogitechLed.dll.bak"); // Copy our own DLL in place - File.WriteAllBytes(@"C:\Program Files\Logitech Gaming Software\SDK\LED\x64\LogitechLED.dll", + File.WriteAllBytes(LogitechPath + @"\LogitechLED.dll", Resources.LogitechLED); // If the user doesn't have a Logitech device, the CLSID will be missing @@ -47,17 +51,12 @@ namespace Artemis.Utilities.LogitechDll public static bool DllPlaced() { - if (!Directory.Exists(@"C:\Program Files\Logitech Gaming Software\SDK\LED\x64")) + if (!Directory.Exists(LogitechPath + @"")) return false; if (!RegistryKeyPlaced()) return false; - return File.Exists(@"C:\Program Files\Logitech Gaming Software\SDK\LED\x64\LogitechLed.dll"); - } - - private static bool BackupAvailable() - { - return File.Exists(@"C:\Program Files\Logitech Gaming Software\SDK\LED\x64\LogitechLed.dll.bak"); + return File.Exists(LogitechPath + @"\LogitechLed.dll"); } private static bool RegistryKeyPlaced() @@ -73,7 +72,7 @@ namespace Artemis.Utilities.LogitechDll var key = Registry .LocalMachine.OpenSubKey( @"SOFTWARE\Classes\CLSID\{a6519e67-7632-4375-afdf-caa889744403}\ServerBinary", true); - key?.SetValue(null, @"C:\Program Files\Logitech Gaming Software\SDK\LED\x64\LogitechLed.dll"); + key?.SetValue(null, LogitechPath + @"\LogitechLed.dll"); } } } \ No newline at end of file diff --git a/Artemis/Artemis/Utilities/LogitechDll/LogitechNamedPipe.cs b/Artemis/Artemis/Utilities/LogitechDll/LogitechNamedPipe.cs new file mode 100644 index 000000000..99a2bbfef --- /dev/null +++ b/Artemis/Artemis/Utilities/LogitechDll/LogitechNamedPipe.cs @@ -0,0 +1,23 @@ +using System.Diagnostics; +using NamedPipeWrapper; + +namespace Artemis.Utilities.LogitechDll +{ + public class LogitechNamedPipe + { + public LogitechNamedPipe() + { + LogitechPipe = new NamedPipeServer("ArtemisLogitech"); + + LogitechPipe.ClientMessage += LogitechPipeOnClientMessage; + LogitechPipe.Start(); + } + + public NamedPipeServer LogitechPipe { get; set; } + + private void LogitechPipeOnClientMessage(NamedPipeConnection connection, string message) + { + Debug.WriteLine(message); + } + } +} \ No newline at end of file diff --git a/Artemis/Artemis/packages.config b/Artemis/Artemis/packages.config index 0b67a3431..d5231ab31 100644 --- a/Artemis/Artemis/packages.config +++ b/Artemis/Artemis/packages.config @@ -12,6 +12,7 @@ +