-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiagnosticsViewModel.cs
More file actions
217 lines (188 loc) · 8.49 KB
/
DiagnosticsViewModel.cs
File metadata and controls
217 lines (188 loc) · 8.49 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
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows.Input;
using Playnite.SDK;
using Playnite.SDK.Models;
namespace GameTaskPlugin
{
// =========================================================
// Row model
// =========================================================
public class GameDiagnosticRow : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string name = null)
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
public Game Game { get; set; }
public string Name { get; set; }
public string TaskStatus { get; set; }
public string ExeName { get; set; }
public string FocusStatus { get; set; }
public string CustomPath { get; set; }
public string IssueHint { get; set; }
public bool HasIssue { get; set; }
public bool HasNoAction { get; set; }
public ICommand RepairCommand { get; set; }
public ICommand FixExeCommand { get; set; }
public ICommand DisableCommand { get; set; }
}
// =========================================================
// ViewModel
// =========================================================
public class DiagnosticsViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string name = null)
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
private readonly IPlayniteAPI api;
private readonly GameTaskPlugin plugin;
private readonly PathManager pathManager;
private readonly TaskManager taskManager;
private readonly string actionName;
private ObservableCollection<GameDiagnosticRow> games;
public ObservableCollection<GameDiagnosticRow> Games
{
get => games;
set { games = value; OnPropertyChanged(); }
}
private string summary;
public string Summary
{
get => summary;
set { summary = value; OnPropertyChanged(); }
}
private bool isLoading;
public bool IsLoading
{
get => isLoading;
set { isLoading = value; OnPropertyChanged(); }
}
public ICommand FixAllCommand { get; }
public ICommand RepairAllCommand { get; }
public ICommand RefreshCommand { get; }
public DiagnosticsViewModel(
IPlayniteAPI api,
GameTaskPlugin plugin,
PathManager pathManager,
TaskManager taskManager,
string actionName)
{
this.api = api;
this.plugin = plugin;
this.pathManager = pathManager;
this.taskManager = taskManager;
this.actionName = actionName;
FixAllCommand = new RelayCommand(_ => plugin.InvokeFixAllUnknownExecutables());
RepairAllCommand = new RelayCommand(_ => { plugin.InvokeRepairAll(); Refresh(); });
RefreshCommand = new RelayCommand(_ => Refresh());
Refresh();
}
public void Refresh()
{
Summary = "Loading...";
IsLoading = true;
Games = new ObservableCollection<GameDiagnosticRow>();
var taggedGames = api.Database.Games
.Where(plugin.HasGameTaskTagPublic)
.OrderBy(g => g.Name)
.ToList();
// Run TaskExists checks in background — each schtasks call takes ~200ms
System.Threading.Tasks.Task.Run(() =>
{
var rows = new System.Collections.Generic.List<GameDiagnosticRow>();
foreach (var game in taggedGames)
{
string customExe = pathManager.GetCustomPath(game);
string resolvedExe = plugin.ResolveExePathPublic(game);
string exeFileName = string.IsNullOrWhiteSpace(resolvedExe)
? "❌ Unknown"
: Path.GetFileName(resolvedExe);
bool taskExists = TaskExists(game);
bool hasExe = !string.IsNullOrWhiteSpace(resolvedExe);
bool hasNoAction = plugin.HasNoGameAction(game);
bool hasIssue = !taskExists || !hasExe;
string issueHint = "";
if (!taskExists && !hasExe)
issueHint = hasNoAction
? "No GameAction — add a Play action in Game Details → Actions"
: "Task missing and exe unknown — use Fix Exe";
else if (!taskExists)
issueHint = "Task missing — use Repair";
else if (!hasExe)
issueHint = hasNoAction
? "No GameAction — add a Play action in Game Details → Actions"
: "Exe unknown — use Fix Exe";
var row = new GameDiagnosticRow
{
Game = game,
Name = game.Name,
TaskStatus = taskExists ? "✅ Created" : "❌ Missing",
ExeName = exeFileName,
FocusStatus = hasExe ? "✅ Active" : "❌ Inactive",
CustomPath = string.IsNullOrWhiteSpace(customExe) ? "" : customExe,
IssueHint = issueHint,
HasIssue = hasIssue,
HasNoAction = hasNoAction
};
var capturedGame = game;
row.RepairCommand = new RelayCommand(_ => { plugin.InvokeRepairGame(capturedGame); Refresh(); });
row.FixExeCommand = new RelayCommand(_ => { plugin.InvokeFixExecutablePath(capturedGame); Refresh(); },
_ => !hasNoAction);
row.DisableCommand = new RelayCommand(_ => { plugin.InvokeDisableGame(capturedGame); Refresh(); });
rows.Add(row);
}
System.Windows.Application.Current.Dispatcher.Invoke(() =>
{
Games = new ObservableCollection<GameDiagnosticRow>(rows);
IsLoading = false;
int total = rows.Count;
int issues = rows.Count(r => r.HasIssue);
Summary = issues == 0
? $"{total} game(s) — all OK ✅"
: $"{total} game(s) — {issues} with issues (shown in red)";
});
});
}
private bool TaskExists(Game game)
{
string taskName = TaskManager.GetTaskName(game);
try
{
using var proc = new Process();
proc.StartInfo.FileName = "schtasks.exe";
proc.StartInfo.Arguments = $"/query /tn \"\\GameTask\\{taskName}\"";
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.UseShellExecute = false;
proc.Start();
proc.WaitForExit();
return proc.ExitCode == 0;
}
catch { return false; }
}
}
// =========================================================
// Simple RelayCommand
// =========================================================
public class RelayCommand : ICommand
{
private readonly Action<object> execute;
private readonly Func<object, bool> canExecute;
public RelayCommand(Action<object> execute, Func<object, bool> canExecute = null)
{
this.execute = execute;
this.canExecute = canExecute;
}
public event EventHandler CanExecuteChanged
{
add { System.Windows.Input.CommandManager.RequerySuggested += value; }
remove { System.Windows.Input.CommandManager.RequerySuggested -= value; }
}
public bool CanExecute(object parameter) => canExecute?.Invoke(parameter) ?? true;
public void Execute(object parameter) => execute(parameter);
}
}