Permalink
Browse files

Resharper code cleanup pass of Capture project

  • Loading branch information...
1 parent 663206d commit 10dcb41a56406e6efb02e96383b3c54466904411 @remcoros remcoros committed Aug 29, 2015
@@ -1,22 +1,20 @@
-namespace Capture
+using System;
+using System.Diagnostics;
+using System.IO;
+using System.Runtime.Remoting;
+using System.Runtime.Remoting.Channels.Ipc;
+using Capture.Hook;
+using Capture.Interface;
+using EasyHook;
+
+namespace Capture
{
- using System;
- using System.Diagnostics;
- using System.IO;
- using System.Runtime.Remoting;
- using System.Runtime.Remoting.Channels.Ipc;
-
- using Capture.Hook;
- using Capture.Interface;
-
- using EasyHook;
-
public class CaptureProcess : IDisposable
{
#region Fields
/// <summary>
- /// Must be null to allow a random channel name to be generated
+ /// Must be null to allow a random channel name to be generated
/// </summary>
private readonly string _channelName;
@@ -31,11 +29,15 @@ public class CaptureProcess : IDisposable
#region Constructors and Destructors
/// <summary>
- /// Prepares capturing in the target process. Note that the process must not already be hooked, and must have a <see cref="Process.MainWindowHandle"/>.
+ /// Prepares capturing in the target process. Note that the process must not already be hooked, and must have a
+ /// <see cref="Process.MainWindowHandle" />.
/// </summary>
/// <param name="process">The process to inject into</param>
- /// <exception cref="ProcessHasNoWindowHandleException">Thrown if the <paramref name="process"/> does not have a window handle. This could mean that the process does not have a UI, or that the process has not yet finished starting.</exception>
- /// <exception cref="ProcessAlreadyHookedException">Thrown if the <paramref name="process"/> is already hooked</exception>
+ /// <exception cref="ProcessHasNoWindowHandleException">
+ /// Thrown if the <paramref name="process" /> does not have a window
+ /// handle. This could mean that the process does not have a UI, or that the process has not yet finished starting.
+ /// </exception>
+ /// <exception cref="ProcessAlreadyHookedException">Thrown if the <paramref name="process" /> is already hooked</exception>
/// <exception cref="InjectionFailedException">Thrown if the injection failed - see the InnerException for more details.</exception>
/// <remarks>The target process will have its main window brought to the foreground after successful injection.</remarks>
public CaptureProcess(Process process, CaptureConfig config, CaptureInterface captureInterface)
@@ -57,7 +59,7 @@ public CaptureProcess(Process process, CaptureConfig config, CaptureInterface ca
//_serverInterface = new CaptureInterface() { ProcessId = process.Id };
// Initialise the IPC server (with our instance of _serverInterface)
- _screenshotServer = RemoteHooking.IpcCreateServer<CaptureInterface>(
+ _screenshotServer = RemoteHooking.IpcCreateServer(
ref _channelName,
WellKnownObjectMode.Singleton,
_serverInterface);
@@ -102,10 +104,7 @@ public CaptureProcess(Process process, CaptureConfig config, CaptureInterface ca
public CaptureInterface CaptureInterface
{
- get
- {
- return _serverInterface;
- }
+ get { return _serverInterface; }
}
public Process Process { get; private set; }
@@ -139,4 +138,4 @@ protected virtual void Dispose(bool disposing)
#endregion
}
-}
+}
View
@@ -1,19 +1,17 @@
-namespace Capture
+using System;
+using System.Collections;
+using System.Runtime.Remoting;
+using System.Runtime.Remoting.Channels;
+using System.Runtime.Remoting.Channels.Ipc;
+using System.Runtime.Serialization.Formatters;
+using System.Threading;
+using System.Threading.Tasks;
+using Capture.Hook;
+using Capture.Interface;
+using EasyHook;
+
+namespace Capture
{
- using System;
- using System.Collections;
- using System.Runtime.Remoting;
- using System.Runtime.Remoting.Channels;
- using System.Runtime.Remoting.Channels.Ipc;
- using System.Runtime.Serialization.Formatters;
- using System.Threading;
- using System.Threading.Tasks;
-
- using Capture.Hook;
- using Capture.Interface;
-
- using EasyHook;
-
public class EntryPoint : IEntryPoint
{
#region Fields
@@ -56,11 +54,11 @@ public class EntryPoint : IEntryPoint
properties["portName"] = channelName + Guid.NewGuid().ToString("N");
// random portName so no conflict with existing channels of channelName
- BinaryServerFormatterSinkProvider binaryProv =
+ var binaryProv =
new BinaryServerFormatterSinkProvider();
binaryProv.TypeFilterLevel = TypeFilterLevel.Full;
- IpcServerChannel _clientServerChannel =
+ var _clientServerChannel =
new IpcServerChannel(properties, binaryProv);
ChannelServices.RegisterChannel(_clientServerChannel, false);
@@ -78,7 +76,7 @@ public class EntryPoint : IEntryPoint
{
// When not using GAC there can be issues with remoting assemblies resolving correctly
// this is a workaround that ensures that the current assembly is correctly associated
- AppDomain currentDomain = AppDomain.CurrentDomain;
+ var currentDomain = AppDomain.CurrentDomain;
currentDomain.AssemblyResolve +=
(sender, args) => { return GetType().Assembly.FullName == args.Name ? GetType().Assembly : null; };
@@ -123,7 +121,6 @@ public class EntryPoint : IEntryPoint
}
finally
{
-
// Dispose of the DXHook so any installed hooks are removed correctly
DisposeDirectXHook();
@@ -167,9 +164,9 @@ private void DisposeDirectXHook()
private bool InitialiseDirectXHook(CaptureConfig config)
{
- Direct3DVersion version = config.Direct3DVersion;
+ var version = config.Direct3DVersion;
- bool isX64Process = RemoteHooking.IsX64Process(RemoteHooking.GetCurrentProcessId());
+ var isX64Process = RemoteHooking.IsX64Process(RemoteHooking.GetCurrentProcessId());
_interface.Message(MessageType.Information, "Remote process is a {0}-bit process.", isX64Process ? "64" : "32");
try
@@ -179,15 +176,18 @@ private bool InitialiseDirectXHook(CaptureConfig config)
// Attempt to determine the correct version based on loaded module.
// In most cases this will work fine, however it is perfectly ok for an application to use a D3D10 device along with D3D11 devices
// so the version might matched might not be the one you want to use
- IntPtr d3D9Loaded = IntPtr.Zero;
- IntPtr d3D10Loaded = IntPtr.Zero;
- IntPtr d3D10_1Loaded = IntPtr.Zero;
- IntPtr d3D11Loaded = IntPtr.Zero;
- IntPtr d3D11_1Loaded = IntPtr.Zero;
-
- int delayTime = 100;
- int retryCount = 0;
- while (d3D9Loaded == IntPtr.Zero && d3D10Loaded == IntPtr.Zero && d3D10_1Loaded == IntPtr.Zero && d3D11Loaded == IntPtr.Zero
+ var d3D9Loaded = IntPtr.Zero;
+ var d3D10Loaded = IntPtr.Zero;
+ var d3D10_1Loaded = IntPtr.Zero;
+ var d3D11Loaded = IntPtr.Zero;
+ var d3D11_1Loaded = IntPtr.Zero;
+
+ var delayTime = 100;
+ var retryCount = 0;
+ while (d3D9Loaded == IntPtr.Zero
+ && d3D10Loaded == IntPtr.Zero
+ && d3D10_1Loaded == IntPtr.Zero
+ && d3D11Loaded == IntPtr.Zero
&& d3D11_1Loaded == IntPtr.Zero)
{
retryCount++;
@@ -240,7 +240,7 @@ private bool InitialiseDirectXHook(CaptureConfig config)
break;
case Direct3DVersion.Direct3D9Simple:
_directXHook = new DXHookD3D9Simple(_interface);
- break; //case Direct3DVersion.Direct3D9Obs:
+ break; //case Direct3DVersion.Direct3D9Obs:
case Direct3DVersion.Direct3D9SharedMem:
_directXHook = new DXHookD3D9SharedMem(_interface);
break;
@@ -278,35 +278,35 @@ private bool InitialiseDirectXHook(CaptureConfig config)
}
/// <summary>
- /// Begin a background thread to check periodically that the host process is still accessible on its IPC channel
+ /// Begin a background thread to check periodically that the host process is still accessible on its IPC channel
/// </summary>
private void StartCheckHostIsAliveThread()
{
_checkAlive = new Task(
() =>
- {
- try
{
- while (Interlocked.Read(ref _stopCheckAlive) == 0)
+ try
{
- Thread.Sleep(1000);
+ while (Interlocked.Read(ref _stopCheckAlive) == 0)
+ {
+ Thread.Sleep(1000);
- // .NET Remoting exceptions will throw RemotingException
- _interface.Ping();
+ // .NET Remoting exceptions will throw RemotingException
+ _interface.Ping();
+ }
}
- }
- catch // We will assume that any exception means that the hooks need to be removed.
- {
- // Signal the Run method so that it can exit
- _runWait.Set();
- }
- });
+ catch // We will assume that any exception means that the hooks need to be removed.
+ {
+ // Signal the Run method so that it can exit
+ _runWait.Set();
+ }
+ });
_checkAlive.Start();
}
/// <summary>
- /// Tell the _checkAlive thread that it can exit if it hasn't already
+ /// Tell the _checkAlive thread that it can exit if it hasn't already
/// </summary>
private void StopCheckHostIsAliveThread()
{
@@ -315,4 +315,4 @@ private void StopCheckHostIsAliveThread()
#endregion
}
-}
+}
View
@@ -1,9 +1,9 @@
-namespace Capture
-{
- using System;
+using System;
+namespace Capture
+{
/// <summary>
- /// Indicates that the provided process does not have a window handle.
+ /// Indicates that the provided process does not have a window handle.
/// </summary>
public class ProcessHasNoWindowHandleException : Exception
{
@@ -40,4 +40,4 @@ public InjectionFailedException(Exception innerException)
#endregion
}
-}
+}
@@ -1,15 +1,13 @@
-namespace Capture.Hook
+using System;
+using System.Collections.Generic;
+using System.Runtime.InteropServices;
+using System.Runtime.Remoting;
+using System.Threading;
+using Capture.Interface;
+using EasyHook;
+
+namespace Capture.Hook
{
- using System;
- using System.Collections.Generic;
- using System.Runtime.InteropServices;
- using System.Runtime.Remoting;
- using System.Threading;
-
- using Capture.Interface;
-
- using EasyHook;
-
internal abstract class BaseDXHook : IDXHook
{
#region Fields
@@ -18,8 +16,6 @@ internal abstract class BaseDXHook : IDXHook
protected List<LocalHook> Hooks = new List<LocalHook>();
- private CaptureConfig _config;
-
private int _processId;
private ScreenshotRequest _request;
@@ -44,30 +40,14 @@ protected BaseDXHook(CaptureInterface ssInterface)
#region Public Properties
- public CaptureConfig Config
- {
- get
- {
- return _config;
- }
- set
- {
- _config = value;
- }
- }
+ public CaptureConfig Config { get; set; }
public CaptureInterface Interface { get; set; }
public ScreenshotRequest Request
{
- get
- {
- return _request;
- }
- set
- {
- Interlocked.Exchange(ref _request, value);
- }
+ get { return _request; }
+ set { Interlocked.Exchange(ref _request, value); }
}
#endregion
@@ -76,10 +56,7 @@ public ScreenshotRequest Request
protected virtual string HookName
{
- get
- {
- return "BaseDXHook";
- }
+ get { return "BaseDXHook"; }
}
protected TimeSpan LastCaptureTime { get; set; }
@@ -140,7 +117,7 @@ protected virtual void Dispose(bool disposing)
foreach (var hook in Hooks)
{
// Lets ensure that no threads will be intercepted again
- hook.ThreadACL.SetInclusiveACL(new int[] { 0 });
+ hook.ThreadACL.SetInclusiveACL(new[] { 0 });
}
Thread.Sleep(100);
@@ -181,10 +158,10 @@ protected IntPtr[] GetVTblAddresses(IntPtr pointer, int numberOfMethods)
protected IntPtr[] GetVTblAddresses(IntPtr pointer, int startIndex, int numberOfMethods)
{
- List<IntPtr> vtblAddresses = new List<IntPtr>();
+ var vtblAddresses = new List<IntPtr>();
- IntPtr vTable = Marshal.ReadIntPtr(pointer);
- for (int i = startIndex; i < startIndex + numberOfMethods; i++)
+ var vTable = Marshal.ReadIntPtr(pointer);
+ for (var i = startIndex; i < startIndex + numberOfMethods; i++)
{
vtblAddresses.Add(GetVTblAddress(vTable, i));
}
@@ -237,4 +214,4 @@ protected void TraceMessage(string message)
#endregion
}
-}
+}
Oops, something went wrong.

0 comments on commit 10dcb41

Please sign in to comment.