-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameTaskPlugin.cs
More file actions
497 lines (407 loc) · 16.4 KB
/
GameTaskPlugin.cs
File metadata and controls
497 lines (407 loc) · 16.4 KB
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
490
491
492
493
494
495
496
497
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Playnite.SDK;
using Playnite.SDK.Events;
using Playnite.SDK.Models;
using Playnite.SDK.Plugins;
namespace GameTaskPlugin
{
public class GameTaskPlugin : GenericPlugin
{
public override Guid Id { get; } =
Guid.Parse("d6d798db-6a1f-4c6e-9b1d-000000000001");
private const string TagName = "[GT] UAC-skip";
private const string ActionName = "Play Without UAC";
private readonly IPlayniteAPI api;
private readonly string pluginDataPath;
private readonly Logger logger;
private readonly TaskManager taskManager;
private readonly LauncherManager launcherManager;
private readonly ActionManager actionManager;
private readonly HiddenLauncherManager hiddenLauncherManager;
private readonly NotificationManager notificationManager;
private readonly TrackerManager trackerManager;
private readonly PathManager pathManager;
public GameTaskPlugin(IPlayniteAPI api) : base(api)
{
this.api = api;
Properties = new GenericPluginProperties
{
HasSettings = false
};
pluginDataPath = GetPluginUserDataPath();
Directory.CreateDirectory(pluginDataPath);
logger = new Logger(pluginDataPath);
taskManager = new TaskManager(logger, pluginDataPath);
launcherManager = new LauncherManager(logger, pluginDataPath);
actionManager = new ActionManager(logger, launcherManager);
hiddenLauncherManager = new HiddenLauncherManager(logger, pluginDataPath);
trackerManager = new TrackerManager(logger);
pathManager = new PathManager(logger, pluginDataPath, taskManager);
notificationManager = new NotificationManager(api, RunPendingTasks, pluginDataPath);
logger.Log("GameTask plugin started.");
}
public override void OnApplicationStarted(OnApplicationStartedEventArgs args)
{
base.OnApplicationStarted(args);
ScanLibrary();
notificationManager.ShowPendingNotification();
}
// =====================================================
// LIBRARY SCAN
// =====================================================
private void ScanLibrary()
{
taskManager.ResetPendingFile();
foreach (var game in api.Database.Games)
{
if (!HasGameTaskTag(game))
continue;
RepairGameTask(game);
}
}
// =====================================================
// REPAIR
// =====================================================
private void RepairGameTask(Game game)
{
launcherManager.CreateOrUpdateLauncher(game);
actionManager.CreateOrUpdatePlayAction(game, api);
if (!TaskExists(game))
taskManager.AddPendingTask(game, ActionName);
ValidateExecutable(game);
}
// =====================================================
// VALIDATE EXECUTABLE
// Checks both the custom path (PathManager) and the
// Playnite action path. If neither resolves to a valid
// file, shows an error notification so the user can fix it.
// =====================================================
private void ValidateExecutable(Game game)
{
// First try the custom path saved by the user
string customExe = pathManager.GetCustomPath(game);
if (!string.IsNullOrWhiteSpace(customExe) && File.Exists(customExe))
{
notificationManager.RemoveExecutableFixNotification(game);
return;
}
// Fall back to resolving from the Playnite action
var action =
game.GameActions?.FirstOrDefault(a =>
a != null &&
a.Name != ActionName &&
!string.IsNullOrWhiteSpace(a.Path));
if (action == null)
return;
string exePath = pathManager.GetExecutablePath(game, action);
if (string.IsNullOrWhiteSpace(exePath) || !File.Exists(exePath))
{
logger.Log($"Tracking not configured, EXE not found: {game.Name}");
notificationManager.ShowExecutableFixNotification(
game,
() => FixExecutablePath(game));
}
else
{
notificationManager.RemoveExecutableFixNotification(game);
}
}
// =====================================================
// FIX EXECUTABLE PATH
// Opens a file dialog for the user to select the .exe,
// saves it via PathManager, forces the pending task to
// be re-queued with the correct path, and shows a
// confirmation message.
// =====================================================
private void FixExecutablePath(Game game)
{
bool fixedPath = pathManager.PromptForExecutable(game);
if (!fixedPath) return;
logger.Log($"Executable manually fixed: {game.Name}");
notificationManager.RemoveExecutableFixNotification(game);
string customExe = pathManager.GetCustomPath(game);
// Force re-queue with the correct exe, bypassing TaskExists()
taskManager.RemovePendingEntry(game);
taskManager.AddPendingTask(game, ActionName, customExe);
launcherManager.CreateOrUpdateLauncher(game);
actionManager.CreateOrUpdatePlayAction(game, api);
notificationManager.ShowPendingNotification();
logger.Log($"Pending task queued after executable fix: {game.Name}");
api.Dialogs.ShowMessage(
$"Executable configured for \"{game.Name}\".\n\nClick \"Create Pending Tasks\" in the GameTask menu (or the notification) to register the Windows task with elevated rights.",
"GameTask");
}
// =====================================================
// REMOVE CUSTOM PATH
// Clears the manually saved exe override for a game.
// After clearing, ValidateExecutable runs again so the
// notification reappears if the automatic path is also missing.
// =====================================================
private void RemoveCustomPath(Game game)
{
pathManager.RemoveCustomPath(game);
logger.Log($"Custom path removed: {game.Name}");
ValidateExecutable(game);
api.Dialogs.ShowMessage(
$"Custom executable path removed for \"{game.Name}\".\n\nGameTask will now try to detect the executable automatically.",
"GameTask");
}
// =====================================================
// TAG HELPERS
// =====================================================
private bool HasGameTaskTag(Game game)
{
if (game.Tags != null && game.Tags.Any(t => t != null && t.Name == TagName))
return true;
if (game.TagIds == null)
return false;
foreach (var tagId in game.TagIds)
{
var tag = api.Database.Tags.Get(tagId);
if (tag != null && tag.Name == TagName)
return true;
}
return false;
}
private Tag GetOrCreateGameTaskTag()
{
var existing = api.Database.Tags.FirstOrDefault(t => t.Name == TagName);
if (existing != null) return existing;
var tag = new Tag(TagName);
api.Database.Tags.Add(tag);
logger.Log("Tag created: " + TagName);
return tag;
}
// =====================================================
// ENABLE / DISABLE
// =====================================================
private void EnableGameTask(IEnumerable<Game> games)
{
var tag = GetOrCreateGameTaskTag();
foreach (var game in games)
{
if (game.TagIds == null)
game.TagIds = new List<Guid>();
if (!game.TagIds.Contains(tag.Id))
{
game.TagIds.Add(tag.Id);
api.Database.Games.Update(game);
}
RepairGameTask(game);
logger.Log($"GameTask enabled: {game.Name}");
}
notificationManager.ShowPendingNotification();
}
private void DisableGameTask(IEnumerable<Game> games)
{
taskManager.ResetDeleteFile();
foreach (var game in games)
{
var tag = api.Database.Tags.FirstOrDefault(t => t.Name == TagName);
if (tag != null && game.TagIds != null && game.TagIds.Contains(tag.Id))
game.TagIds.Remove(tag.Id);
actionManager.RemovePlayAction(game, api);
launcherManager.RemoveLauncher(game);
taskManager.RemovePendingEntry(game);
taskManager.AddDeleteTask(game);
notificationManager.RemoveExecutableFixNotification(game);
api.Database.Games.Update(game);
logger.Log($"GameTask disabled: {game.Name}");
}
RunDeleteTasks();
}
// =====================================================
// REBUILD / REPAIR
// =====================================================
private void RebuildSelected(IEnumerable<Game> games)
{
taskManager.ResetDeleteFile();
foreach (var game in games)
{
actionManager.RemovePlayAction(game, api);
launcherManager.RemoveLauncher(game);
taskManager.RemovePendingEntry(game);
taskManager.AddDeleteTask(game);
launcherManager.CreateOrUpdateLauncher(game);
actionManager.CreateOrUpdatePlayAction(game, api);
taskManager.AddPendingTask(game, ActionName);
ValidateExecutable(game);
logger.Log($"GameTask rebuilt: {game.Name}");
}
RunDeleteTasks();
notificationManager.ShowPendingNotification();
}
private void RepairSelected(IEnumerable<Game> games)
{
foreach (var game in games)
{
RepairGameTask(game);
logger.Log($"GameTask repaired: {game.Name}");
}
notificationManager.ShowPendingNotification();
}
// =====================================================
// TASK EXISTS CHECK
// =====================================================
private bool TaskExists(Game game)
{
string taskName = $"GameTask_v1_{Utils.MakeSafeTaskName(game.Name)}";
try
{
using var process = new Process();
process.StartInfo.FileName = "schtasks.exe";
process.StartInfo.Arguments = $"/query /tn \"\\GameTask\\{taskName}\"";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.Start();
process.WaitForExit();
return process.ExitCode == 0;
}
catch
{
return false;
}
}
// =====================================================
// RUN HELPERS (elevated via wscript runas)
// =====================================================
private void RunPendingTasks()
{
try
{
logger.Log("Requesting elevated task creation...");
Process.Start(new ProcessStartInfo
{
FileName = "wscript.exe",
Arguments = $"\"{hiddenLauncherManager.GetCreateLauncherPath()}\"",
Verb = "runas",
UseShellExecute = true
});
}
catch (Exception ex)
{
logger.Log($"ERROR running create helper: {ex.Message}");
}
}
private void RunDeleteTasks()
{
try
{
logger.Log("Requesting elevated task cleanup...");
Process.Start(new ProcessStartInfo
{
FileName = "wscript.exe",
Arguments = $"\"{hiddenLauncherManager.GetDeleteLauncherPath()}\"",
Verb = "runas",
UseShellExecute = true
});
}
catch (Exception ex)
{
logger.Log($"ERROR running delete helper: {ex.Message}");
}
}
// =====================================================
// UTILITY ACTIONS
// =====================================================
private void OpenDataFolder()
{
try
{
Process.Start(pluginDataPath);
}
catch (Exception ex)
{
logger.Log($"ERROR opening data folder: {ex.Message}");
}
}
private void OpenTaskScheduler()
{
try
{
Process.Start(new ProcessStartInfo
{
FileName = "taskschd.msc",
UseShellExecute = true
});
logger.Log("Task Scheduler opened.");
}
catch (Exception ex)
{
logger.Log($"ERROR opening Task Scheduler: {ex.Message}");
}
}
// =====================================================
// GAME MENU ITEMS
// =====================================================
public override IEnumerable<GameMenuItem> GetGameMenuItems(GetGameMenuItemsArgs args)
{
yield return new GameMenuItem
{
MenuSection = "GameTask",
Description = "Enable GameTask",
Action = menuArgs => EnableGameTask(menuArgs.Games)
};
yield return new GameMenuItem
{
MenuSection = "GameTask",
Description = "Disable GameTask",
Action = menuArgs => DisableGameTask(menuArgs.Games)
};
yield return new GameMenuItem
{
MenuSection = "GameTask",
Description = "Create Pending Tasks",
Action = menuArgs => RunPendingTasks()
};
yield return new GameMenuItem
{
MenuSection = "GameTask",
Description = "Rebuild Selected",
Action = menuArgs => RebuildSelected(menuArgs.Games)
};
yield return new GameMenuItem
{
MenuSection = "GameTask",
Description = "Repair Selected",
Action = menuArgs => RepairSelected(menuArgs.Games)
};
yield return new GameMenuItem
{
MenuSection = "GameTask",
Description = "Fix Executable Path",
Action = menuArgs =>
{
foreach (var game in menuArgs.Games)
FixExecutablePath(game);
}
};
yield return new GameMenuItem
{
MenuSection = "GameTask",
Description = "Remove Custom Executable Path",
Action = menuArgs =>
{
foreach (var game in menuArgs.Games)
RemoveCustomPath(game);
}
};
yield return new GameMenuItem
{
MenuSection = "GameTask",
Description = "Open Data Folder",
Action = menuArgs => OpenDataFolder()
};
yield return new GameMenuItem
{
MenuSection = "GameTask",
Description = "Open Task Scheduler",
Action = menuArgs => OpenTaskScheduler()
};
}
}
}