2020-07-26 14:07:13 +08:00
using System ;
using System.Collections.Generic ;
using System.Diagnostics ;
2020-11-06 21:59:27 -08:00
using System.IO ;
2020-07-26 14:07:13 +08:00
using System.Linq ;
2020-11-06 21:59:27 -08:00
using System.Reflection ;
using System.Text.RegularExpressions ;
using System.Threading ;
2020-09-13 04:29:59 +08:00
using OpenTabletDriver.Plugin ;
using OpenTabletDriver.Plugin.Attributes ;
2020-07-26 14:07:13 +08:00
2020-12-13 04:59:00 +08:00
namespace VoiDPlugins.Tool
2020-07-26 14:07:13 +08:00
{
2020-11-06 21:59:27 -08:00
[PluginName("OEM Kill"), SupportedPlatform(PluginPlatform.Windows)]
2020-12-13 04:59:00 +08:00
public class OemKill : ITool
2020-07-26 14:07:13 +08:00
{
public bool Initialize ( )
2020-11-06 21:59:27 -08:00
{
Terminate ( ) ;
return true ;
}
[Action("Driver", "Kill OEM Drivers"), ToolTip("Terminate known OEM Drivers")]
public static void Terminate ( )
2020-07-26 14:07:13 +08:00
{
var processList = Process . GetProcesses ( ) ;
var oemProcesses = from process in processList
2020-11-06 21:59:27 -08:00
where OemData . OemProcesses . Contains ( process . ProcessName )
2020-07-26 14:07:13 +08:00
select process ;
if ( oemProcesses . ToList ( ) . Count = = 0 )
{
2020-08-28 03:16:16 +08:00
Log . Write ( "OemKill" , "No oem process found" ) ;
2020-11-06 21:59:27 -08:00
return ;
2020-07-26 14:07:13 +08:00
}
2020-10-15 20:35:53 +08:00
foreach ( var process in oemProcesses )
2020-07-26 14:07:13 +08:00
{
2020-10-15 20:35:53 +08:00
try
2020-07-29 05:47:56 +08:00
{
2020-07-26 14:07:13 +08:00
process . Kill ( ) ;
2020-08-28 03:16:16 +08:00
Log . Write ( "OemKill" , "Killing " + process . ProcessName ) ;
2020-07-29 05:47:56 +08:00
}
2020-10-15 20:35:53 +08:00
catch ( Exception e )
{
Log . Write ( "OemKill" , "Failed. Reason: " + e . Message , LogLevel . Error ) ;
2020-11-06 21:59:27 -08:00
return ;
2020-10-15 20:35:53 +08:00
}
2020-07-26 14:07:13 +08:00
}
2020-08-28 03:16:16 +08:00
Log . Write ( "OemKill" , "Oem process killed successfully" ) ;
2020-07-26 14:07:13 +08:00
}
2020-11-06 21:59:27 -08:00
[Action("Driver", "Restore Driver Defaults"), ToolTip("Fix issues with remnant drivers from Huion and Gaomon")]
public static void RestoreDriver ( )
{
RunRestore ( false ) ;
}
[Action("Driver", "Simulate 'Restore Driver Defaults'"), ToolTip("Simulates driver restoration without actually doing any driver related action, and opens assets folder for inspection")]
public static void SimulateRestoreDriver ( )
{
RunRestore ( true ) ;
}
private static void RunRestore ( bool simulate )
{
var location = Path . GetDirectoryName ( Assembly . GetExecutingAssembly ( ) . Location ) ;
var assets = Path . Join ( location , "assets" ) ;
if ( ! Directory . Exists ( assets ) )
{
Directory . CreateDirectory ( assets ) ;
}
RunEnum ( assets , out var enumOutput ) ;
List < IndexedInf > oemDrivers = new List < IndexedInf > ( ) ;
for ( int i = 0 ; i < enumOutput . Length ; i + + )
{
var infMatch = inf . Match ( enumOutput [ i ] ) ;
if ( infMatch . Success )
{
var infName = infMatch . Groups [ 1 ] . Value ;
var driverName = driver . Match ( enumOutput [ i + 1 ] ) . Groups [ 1 ] . Value ;
if ( OemData . OemDrivers . Contains ( driverName ) )
{
Log . Write ( "OemKill" , $"Found '{driverName}' with driver '{infName}'" ) ;
oemDrivers . Add ( new IndexedInf ( i , driverName , infName ) ) ;
}
}
}
if ( oemDrivers . Count = = 0 )
{
Log . Write ( "OemKill" , "No remnant driver found" ) ;
if ( simulate )
{
Log . Write ( "OemKill" , $"You may now inspect generated scripts on: '{assets}'" ) ;
var startInfo = new ProcessStartInfo ( "explorer" , $"{assets}" ) ;
Process . Start ( startInfo ) ;
}
return ;
}
var restoreHelperScript = Path . Join ( assets , "restore.bat" ) ;
using ( var restoreHelperStream = File . CreateText ( restoreHelperScript ) )
{
restoreHelperStream . WriteLine ( "@echo off" ) ;
restoreHelperStream . WriteLine ( "echo WARNING! OTD will be closed after you press any key." ) ;
restoreHelperStream . WriteLine ( "pause" ) ;
restoreHelperStream . WriteLine ( "taskkill /F /IM OpenTabletDriver.UX.Wpf.exe" ) ;
restoreHelperStream . WriteLine ( "taskkill /F /IM OpenTabletDriver.Daemon.exe" ) ;
restoreHelperStream . WriteLine ( "echo Script auto-generated by OemKill OTD plugin" ) ;
foreach ( var oemDriver in oemDrivers )
{
restoreHelperStream . WriteLine ( $"echo Uninstalling '{oemDriver.name}' with driver '{oemDriver.inf}'..." ) ;
restoreHelperStream . WriteLine ( $"pnputil -f -d {oemDriver.inf}" ) ;
}
restoreHelperStream . WriteLine ( "echo Installing default driver..." ) ;
restoreHelperStream . WriteLine ( $"pnputil -i -a %SystemRoot%/INF/input.inf" ) ;
restoreHelperStream . WriteLine ( "echo Driver restore done! You may now start OTD again" ) ;
}
if ( simulate )
{
Log . Write ( "OemKill" , $"You may now inspect generated scripts on: '{assets}'" ) ;
var startInfo = new ProcessStartInfo ( "explorer" , $"{assets}" ) ;
Process . Start ( startInfo ) ;
}
else
{
Terminate ( ) ;
Call ( restoreHelperScript , true ) ;
}
}
private static void RunEnum ( string assetsLocation , out string [ ] output )
{
var enumHelperScript = Path . Join ( assetsLocation , "enumerator.bat" ) ;
var enumHelperOutput = Path . Join ( assetsLocation , "drivers.txt" ) ;
using ( var enumHelperStream = File . CreateText ( enumHelperScript ) )
{
enumHelperStream . WriteLine ( ":: Script auto-generated by OemKill OTD plugin" ) ;
enumHelperStream . WriteLine ( $"pnputil -e > \" { enumHelperOutput } \ "" ) ;
}
Log . Write ( "OemKill" , "Enumerating oem driver installations" ) ;
Call ( enumHelperScript ) ;
while ( ! File . Exists ( enumHelperOutput ) )
{
Thread . Sleep ( 100 ) ;
}
bool read = false ;
while ( ! read )
{
try
{
output = File . ReadAllLines ( enumHelperOutput ) ;
read = true ;
return ;
} catch { }
}
output = null ;
}
private static void Call ( string file , bool asAdmin = false )
2020-07-26 14:07:13 +08:00
{
2020-11-06 21:59:27 -08:00
Log . Write ( "OemKill" , $" Calling script: '{file}'" ) ;
var info = new ProcessStartInfo ( "powershell" )
{
CreateNoWindow = true ,
Arguments = asAdmin ? $"-c start -verb runas {file}" : $"-c start {file}"
} ;
var process = Process . Start ( info ) ;
process . WaitForExit ( ) ;
}
private static readonly Regex inf = new Regex ( ":\\s*(.*\\.inf)$" , RegexOptions . Compiled ) ;
private static readonly Regex driver = new Regex ( @":\s*(.*)$" , RegexOptions . Compiled ) ;
2020-07-26 14:07:13 +08:00
public void Dispose ( ) { }
}
}