forked from nefarius/ScpToolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScpForm.cs
489 lines (398 loc) · 16.5 KB
/
ScpForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using System.IO;
using System.Reflection;
using System.ServiceProcess;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using log4net;
using ScpControl.Driver;
using ScpControl.Utilities;
using ScpDriver.Properties;
namespace ScpDriver
{
public partial class ScpForm : Form
{
private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private bool _bthDriverConfigured;
private bool _busDeviceConfigured;
private bool _busDriverConfigured;
private bool _ds3DriverConfigured;
private bool _ds4DriverConfigured;
private Difx _installer;
private bool _reboot;
private Cursor _saved;
private bool _scpServiceConfigured;
private OsType _valid = OsType.Invalid;
public ScpForm()
{
InitializeComponent();
AppDomain.CurrentDomain.UnhandledException +=
(sender, args) => { Log.FatalFormat("An unhandled exception occured: {0}", args.ExceptionObject); };
try
{
// get absolute path to XML file
var cfgFile =
Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? string.Empty,
Assembly.GetExecutingAssembly().GetName().Name + ".xml");
// deserialize file content
var cfg = ScpDriver.Deserialize(cfgFile);
// set display options
cbService.Checked = cbService.Visible = cfg.InstallService;
cbBluetooth.Checked = cbBluetooth.Visible = cfg.InstallBluetooth;
cbDS3.Checked = cbDS3.Visible = cfg.InstallDualShock3;
cbBus.Checked = cbBus.Visible = cfg.InstallVirtualBus;
// Don't install Service if Bus not Enabled
if (!cfg.InstallVirtualBus)
cbService.Checked = cbService.Visible = cfg.InstallVirtualBus;
}
catch (Exception ex)
{
Log.ErrorFormat("Couldn't load configuration: {0}", ex);
}
}
private static void Logger(DifxLog Event, int error, string description)
{
switch (Event)
{
case DifxLog.DIFXAPI_ERROR:
Log.Error(description);
break;
case DifxLog.DIFXAPI_INFO:
case DifxLog.DIFXAPI_SUCCESS:
Log.Info(description);
break;
case DifxLog.DIFXAPI_WARNING:
Log.Warn(description);
break;
}
}
private static bool Start(string service)
{
try
{
var sc = new ServiceController(service);
if (sc.Status == ServiceControllerStatus.Stopped)
{
sc.Start();
Thread.Sleep(1000);
return true;
}
}
catch (Exception ex)
{
Log.ErrorFormat("Couldn't start service: {0}", ex);
}
return false;
}
private static bool Stop(string service)
{
try
{
var sc = new ServiceController(service);
if (sc.Status == ServiceControllerStatus.Running)
{
sc.Stop();
Thread.Sleep(1000);
return true;
}
}
catch (InvalidOperationException iopex)
{
if (!(iopex.InnerException is Win32Exception))
{
Log.ErrorFormat("Win32-Exception occured: {0}", iopex);
return false;
}
switch (((Win32Exception) iopex.InnerException).NativeErrorCode)
{
case 1060: // ERROR_SERVICE_DOES_NOT_EXIST
Log.Warn("Service doesn't exist, maybe it was uninstalled before");
break;
default:
Log.ErrorFormat("Win32-Error: {0}", (Win32Exception) iopex.InnerException);
break;
}
}
catch (Exception ex)
{
Log.ErrorFormat("Couldn't stop service: {0}", ex);
}
return false;
}
private void ScpForm_Load(object sender, EventArgs e)
{
Log.InfoFormat("SCP Driver Installer {0} [{1}]", Application.ProductVersion, DateTime.Now);
_installer = Difx.Instance;
_installer.OnLogEvent += Logger;
var info = OsInfoHelper.OsInfo;
_valid = OsInfoHelper.OsParse(info);
Log.InfoFormat("{0} detected", info);
if (_valid == OsType.Invalid)
{
btnInstall.Enabled = false;
btnUninstall.Enabled = false;
Log.Error("Could not find a valid configuration");
}
else
{
btnInstall.Enabled = true;
btnUninstall.Enabled = true;
Log.InfoFormat("Selected {0} configuration", _valid);
}
Icon = Resources.Scp_All;
if (!OsInfoHelper.IsVc2013Installed)
{
MessageBox.Show(Resources.ScpForm_VcppMissingText, Resources.ScpForm_VcppMissingHead,
MessageBoxButtons.OK, MessageBoxIcon.Error);
Close();
}
}
private async void btnInstall_Click(object sender, EventArgs e)
{
#region Pre-Installation
_saved = Cursor;
Cursor = Cursors.WaitCursor;
btnInstall.Enabled = false;
btnUninstall.Enabled = false;
btnExit.Enabled = false;
_busDeviceConfigured = false;
_busDriverConfigured = false;
_ds3DriverConfigured = false;
_bthDriverConfigured = false;
_scpServiceConfigured = false;
pbRunning.Style = ProgressBarStyle.Marquee;
var forceInstall = cbForce.Checked;
var installBus = cbBus.Checked;
var installBth = cbBluetooth.Checked;
var installDs3 = cbDS3.Checked;
var installDs4 = cbDs4.Checked;
var installService = cbService.Checked;
#endregion
#region Installation
await Task.Run(() =>
{
string devPath = string.Empty, instanceId = string.Empty;
try
{
uint result = 0;
var flags = DifxFlags.DRIVER_PACKAGE_ONLY_IF_DEVICE_PRESENT;
if (forceInstall)
flags |= DifxFlags.DRIVER_PACKAGE_FORCE;
if (installBus)
{
if (!Devcon.Find(Settings.Default.Ds3BusClassGuid, ref devPath, ref instanceId))
{
if (Devcon.Create("System", new Guid("{4D36E97D-E325-11CE-BFC1-08002BE10318}"),
"root\\ScpVBus\0\0"))
{
Logger(DifxLog.DIFXAPI_SUCCESS, 0, "Virtual Bus Created");
_busDeviceConfigured = true;
}
}
bool rebootRequired;
result = _installer.Install(Path.Combine(Settings.Default.InfFilePath, @"ScpVBus.inf"), flags,
out rebootRequired);
_reboot |= rebootRequired;
if (result == 0) _busDriverConfigured = true;
}
if (installBth)
{
Invoke(
(MethodInvoker) delegate { result = DriverInstaller.InstallBluetoothDongles(Handle, forceInstall); });
if (result > 0) _bthDriverConfigured = true;
}
if (installDs3)
{
Invoke(
(MethodInvoker)
delegate { result = DriverInstaller.InstallDualShock3Controllers(Handle, forceInstall); });
if (result > 0) _ds3DriverConfigured = true;
}
if (installDs4)
{
Invoke(
(MethodInvoker)
delegate { result = DriverInstaller.InstallDualShock4Controllers(Handle, forceInstall); });
if (result > 0) _ds4DriverConfigured = true;
}
if (installService)
{
IDictionary state = new Hashtable();
var service =
new AssemblyInstaller(Directory.GetCurrentDirectory() + @"\ScpService.exe", null);
state.Clear();
service.UseNewContext = true;
service.Install(state);
service.Commit(state);
if (Start(Settings.Default.ScpServiceName))
Logger(DifxLog.DIFXAPI_INFO, 0, Settings.Default.ScpServiceName + " Started.");
else _reboot = true;
_scpServiceConfigured = true;
}
}
catch (Win32Exception w32Ex)
{
switch (w32Ex.NativeErrorCode)
{
case 1073: // ERROR_SERVICE_EXISTS
Log.WarnFormat("Service already exists, skipping installation...");
break;
default:
Log.ErrorFormat("Win32-Error during installation: {0}", w32Ex);
break;
}
}
catch (Exception ex)
{
Log.ErrorFormat("Error during installation: {0}", ex);
}
});
#endregion
#region Post-Installation
pbRunning.Style = ProgressBarStyle.Continuous;
btnInstall.Enabled = true;
btnUninstall.Enabled = true;
btnExit.Enabled = true;
Cursor = _saved;
Log.Info("Install Succeeded.");
if (_reboot)
Log.InfoFormat("[Reboot Required]");
Log.Info("-- Install Summary --");
if (_scpServiceConfigured)
Log.Info("SCP DS3 Service installed");
if (_busDeviceConfigured)
Log.Info("Bus Device installed");
if (_busDriverConfigured)
Log.Info("Bus Driver installed");
if (_ds3DriverConfigured)
Log.Info("DS3 USB Driver installed");
if (_bthDriverConfigured)
Log.Info("Bluetooth Driver installed");
if (_ds4DriverConfigured)
Log.Info("DS4 USB Driver installed");
#endregion
}
private async void btnUninstall_Click(object sender, EventArgs e)
{
#region Pre-Uninstallation
_saved = Cursor;
Cursor = Cursors.WaitCursor;
btnInstall.Enabled = false;
btnUninstall.Enabled = false;
btnExit.Enabled = false;
_busDeviceConfigured = false;
_busDriverConfigured = false;
_ds3DriverConfigured = false;
_bthDriverConfigured = false;
_scpServiceConfigured = false;
pbRunning.Style = ProgressBarStyle.Marquee;
#endregion
#region Uninstallation
await Task.Run(() =>
{
string devPath = string.Empty, instanceId = string.Empty;
try
{
var rebootRequired = false;
if (cbService.Checked)
{
IDictionary state = new Hashtable();
var service =
new AssemblyInstaller(Directory.GetCurrentDirectory() + @"\ScpService.exe", null);
state.Clear();
service.UseNewContext = true;
if (Stop(Settings.Default.ScpServiceName))
{
Logger(DifxLog.DIFXAPI_INFO, 0, Settings.Default.ScpServiceName + " Stopped.");
}
service.Uninstall(state);
_scpServiceConfigured = true;
}
if (cbBluetooth.Checked)
{
DriverInstaller.UninstallBluetoothDongles(ref rebootRequired);
_reboot |= rebootRequired;
}
if (cbDS3.Checked)
{
DriverInstaller.UninstallDualShock3Controllers(ref rebootRequired);
_reboot |= rebootRequired;
}
if (cbDs4.Checked)
{
DriverInstaller.UninstallDualShock4Controllers(ref rebootRequired);
_reboot |= rebootRequired;
}
if (cbBus.Checked && Devcon.Find(Settings.Default.Ds3BusClassGuid, ref devPath, ref instanceId))
{
if (Devcon.Remove(Settings.Default.Ds3BusClassGuid, devPath, instanceId))
{
Logger(DifxLog.DIFXAPI_SUCCESS, 0, "Virtual Bus Removed");
_busDeviceConfigured = true;
_installer.Uninstall(Path.Combine(Settings.Default.InfFilePath, @"ScpVBus.inf"),
DifxFlags.DRIVER_PACKAGE_DELETE_FILES,
out rebootRequired);
_reboot |= rebootRequired;
}
else
{
Logger(DifxLog.DIFXAPI_ERROR, 0, "Virtual Bus Removal Failure");
}
}
}
catch (InstallException instex)
{
if (!(instex.InnerException is Win32Exception))
{
Log.ErrorFormat("Error during uninstallation: {0}", instex);
return;
}
switch (((Win32Exception) instex.InnerException).NativeErrorCode)
{
case 1060: // ERROR_SERVICE_DOES_NOT_EXIST
Log.Warn("Service doesn't exist, maybe it was uninstalled before");
break;
default:
Log.ErrorFormat("Win32-Error during uninstallation: {0}",
(Win32Exception) instex.InnerException);
break;
}
}
catch (Exception ex)
{
Log.ErrorFormat("Error during uninstallation: {0}", ex);
}
});
#endregion
#region Post-Uninstallation
pbRunning.Style = ProgressBarStyle.Continuous;
btnInstall.Enabled = true;
btnUninstall.Enabled = true;
btnExit.Enabled = true;
Cursor = _saved;
Log.Info("Uninstall Succeeded.");
if (_reboot)
Log.Info(" [Reboot Required]");
Log.Info("-- Uninstall Summary --");
if (_scpServiceConfigured)
Log.Info("SCP DS3 Service uninstalled");
if (_busDeviceConfigured)
Log.Info("Bus Device uninstalled");
if (_busDriverConfigured)
Log.Info("Bus Driver uninstalled");
if (_ds3DriverConfigured)
Log.Info("DS3 USB Driver uninstalled");
if (_bthDriverConfigured)
Log.Info("Bluetooth Driver uninstalled");
#endregion
}
private void btnExit_Click(object sender, EventArgs e)
{
Close();
}
}
}