-
Notifications
You must be signed in to change notification settings - Fork 7
/
InstallButton.cs
384 lines (356 loc) · 14 KB
/
InstallButton.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
using Playnite;
using Playnite.SDK;
using Playnite.SDK.Plugins;
using Playnite.SDK.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using InstallButton;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Management.Automation;
using System.Management;
using API = Playnite.SDK.API;
namespace InstallButton
{
public class InstallButton : GenericPlugin
{
private static readonly ILogger logger = LogManager.GetLogger();
public InstallButtonSettingsViewModel settings { get; set; }
public override Guid Id { get; } = Guid.Parse("fd5887bb-2da2-4044-bea3-9896aea2f5b8");
public InstallButton(IPlayniteAPI api) : base(api)
{
settings = new InstallButtonSettingsViewModel(this);
Properties = new GenericPluginProperties
{
HasSettings = true
};
}
public override ISettings GetSettings(bool firstRunSettings)
{
return settings;
}
public override UserControl GetSettingsView(bool firstRunSettings)
{
return new InstallButtonSettingsView();
}
public override IEnumerable<InstallController> GetInstallActions(GetInstallActionsArgs args)
{
if (args.Game.PluginId != Guid.Empty)
{
yield break;
}
yield return new LocalInstallController(args.Game, this);
}
public override IEnumerable<UninstallController> GetUninstallActions(GetUninstallActionsArgs args)
{
if (args.Game.PluginId != Guid.Empty)
{
yield break;
}
yield return new LocalUninstallController(args.Game, this);
}
public void GameSelect(Game selectedGame)
{
string gameExe = API.Instance.Dialogs.SelectFile("Game Executable|*.exe").Replace(selectedGame.Name, "{Name}");
if (!String.IsNullOrEmpty(gameExe))
{
GameAction action = new GameAction();
GameAction uaction = new GameAction();
string installDir = Path.GetDirectoryName(gameExe);
string[] idFiles = Directory.GetFiles(installDir, "*unins*", SearchOption.AllDirectories);
string finalu = "";
foreach (string idFile in idFiles)
{
if (idFile.ToLower().Contains("uninstall.bat"))
{
finalu = idFile;
break;
}
else if (idFile.ToLower().Contains("uninstall.exe"))
{
finalu = idFile;
break;
}
else if (idFile.ToLower().Contains("unins000.exe"))
{
finalu = idFile;
break;
}
}
if (finalu == "")
{
finalu = API.Instance.Dialogs.SelectFile("Uninstall Executable|*.exe");
}
try
{
action.Type = GameActionType.File;
if (finalu != "")
{
uaction.Type = GameActionType.File;
}
}
catch (Exception ex)
{
logger.Error(ex.ToString());
}
action.Path = Path.GetFileName(gameExe);
action.WorkingDir = "{InstallDir}";
action.Name = "Play";
action.TrackingMode = TrackingMode.Default;
action.IsPlayAction = true;
if (finalu != "")
{
uaction.Path = Path.GetFileName(finalu);
uaction.WorkingDir = Path.GetDirectoryName(finalu);
uaction.Name = "Uninstall";
uaction.IsPlayAction = false;
}
if (selectedGame.GameActions == null)
{
selectedGame.GameActions = new System.Collections.ObjectModel.ObservableCollection<GameAction>();
}
try
{
selectedGame.GameActions.AddMissing(action);
if (finalu != "")
{
selectedGame.GameActions.AddMissing(uaction);
}
}
catch (Exception ex)
{
logger.Error(ex.ToString());
API.Instance.Dialogs.ShowErrorMessage("There was an error creating the Game Action. Please check the Playnite log for details.", "Action Failed");
return;
}
try
{
List<GameAction> gameActions = selectedGame.GameActions.ToList();
foreach (GameAction g in gameActions)
{
if (g.Name == "Play")
{
selectedGame.IsInstalled = true;
selectedGame.InstallDirectory = Path.GetDirectoryName(gameExe);
}
}
}
catch (Exception ex)
{
logger.Error(ex.ToString());
API.Instance.Dialogs.ShowErrorMessage("There was an error marking the game as installed. Please check the Playnite log for details.", "Action Failed");
return;
}
API.Instance.Database.Games.Update(selectedGame);
}
this.Dispose();
}
public void GameInstaller(Game game)
{
Game selectedGame = game;
string gameImagePath = null;
string gameInstallArgs = null;
List<string> driveList = new List<string>();
List<string> driveList2 = new List<string>();
string command = null;
string driveLetter = null;
if (settings.Settings.UseActions)
{
try
{
List<GameAction> gameActions = selectedGame.GameActions.ToList();
try
{
foreach (GameAction g in gameActions)
{
if (g.Name == "Install")
{
gameImagePath = API.Instance.ExpandGameVariables(selectedGame, g).Path.Replace(": ", " - ");
gameInstallArgs = API.Instance.ExpandGameVariables(selectedGame, g).Arguments.Replace(": ", " - ");
}
}
}
catch (Exception ex)
{
logger.Error(ex.ToString());
}
}
catch (Exception ex)
{
logger.Error(ex.ToString());
}
}
else
{
try
{
{
var gameRoms = selectedGame.Roms.ToList();
gameImagePath = gameRoms[0].Path;
}
}
catch (Exception ex)
{
logger.Error(ex.ToString());
}
}
if (String.IsNullOrEmpty(gameImagePath))
{
var response = MessageBox.Show("The installation path is empty.\nDo you want to specify the location of the installation media?", "No Installation Path", MessageBoxButton.YesNo);
if (response == MessageBoxResult.Yes)
{
gameImagePath = API.Instance.Dialogs.SelectFolder();
}
}
else
{
if (Path.GetFileName(gameImagePath).EndsWith(".iso") || Path.GetFileName(gameImagePath).EndsWith(".ISO"))
{
foreach (var drive in DriveInfo.GetDrives())
{
driveList.Add(drive.Name);
}
PowerShell mountedDisk = PowerShell.Create();
{
mountedDisk.AddCommand("Mount-DiskImage");
mountedDisk.AddArgument(gameImagePath);
mountedDisk.Invoke();
}
foreach (var drive in DriveInfo.GetDrives())
{
driveList2.Add(drive.Name);
}
foreach (var i in driveList2)
{
if (driveList.Contains(i))
{
continue;
}
else
{
command = i + "\\Setup.exe";
driveLetter = i;
}
}
}
else if (Path.GetFileName(gameImagePath).EndsWith(".exe"))
{
command = gameImagePath;
}
else if (!Path.HasExtension(gameImagePath))
{
if (!Directory.Exists(gameImagePath))
{
API.Instance.Dialogs.ShowErrorMessage("The file/folder specified in the installation path does not exist.", "Invalid Path");
return;
}
string setupFile = Path.Combine(gameImagePath, "setup.exe");
if (File.Exists(setupFile))
{
command = setupFile;
}
else
{
String[] Files = Directory.GetFiles(gameImagePath, "*.exe");
if (Files.Count() > 1)
{
MessageBoxResult result = MessageBox.Show("More than 1 .exe in folder. Would you like to select the appropriate .exe?", "Too many programs", MessageBoxButton.YesNo);
if (result == MessageBoxResult.Yes)
{
command = API.Instance.Dialogs.SelectFile("Installer|*.exe");
}
else
{
return;
}
}
else if (Files.Count().ToString() == "0")
{
API.Instance.Dialogs.ShowErrorMessage("No executables found in folder. Check Rom Path.", "No Executables.");
return;
}
else
{
command = Files[0];
}
}
}
else
{
API.Instance.Dialogs.ShowErrorMessage("The provided Rom file has an invalid extension. Please provide valid iso/exe/directory.", "Invalid Executable/ISO.");
return;
}
}
if (!File.Exists(command))
{
MessageBoxResult result = MessageBox.Show("Setup.exe was not found in your ISO. Would you like to select the appropriate .exe?", "Setup.exe not found", MessageBoxButton.YesNo);
if (result == MessageBoxResult.Yes)
{
command = API.Instance.Dialogs.SelectFile("Installer|*.exe");
}
else
{
GameSelect(selectedGame);
}
}
try
{
using (Process p = new Process())
{
String dpath = "";
p.StartInfo.FileName = command;
p.StartInfo.UseShellExecute = true;
if (gameInstallArgs != null)
{
p.StartInfo.Arguments = gameInstallArgs;
}
if (driveLetter != null)
{
dpath = driveLetter;
}
else if (Path.HasExtension(gameImagePath))
{
dpath = Path.GetDirectoryName(gameImagePath);
}
else
{
dpath = gameImagePath;
}
p.StartInfo.WorkingDirectory = dpath;
p.StartInfo.Verb = "runas";
p.Start();
p.WaitForExit();
}
}
catch
{
return;
}
if (gameImagePath.EndsWith(".iso"))
{
PowerShell dismountDisk = PowerShell.Create();
{
dismountDisk.AddCommand("Dismount-DiskImage");
dismountDisk.AddArgument(gameImagePath);
dismountDisk.Invoke();
}
}
GameSelect(selectedGame);
}
public void GameUninstaller(Game game)
{
Game selectedGame = game;
if (selectedGame != null)
{
string installDir = selectedGame.InstallDirectory;
}
}
}
}