-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPluginSettings.cs
More file actions
225 lines (188 loc) · 8.03 KB
/
PluginSettings.cs
File metadata and controls
225 lines (188 loc) · 8.03 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Playnite.SDK;
using Playnite.SDK.Plugins;
namespace GameTaskPlugin
{
// =========================================================
// MODEL
// =========================================================
public class PluginSettings : ObservableObject
{
private bool bringWindowToForeground = true;
private bool detectOrphanTasks = true;
private bool detectCorruptedTasks = true;
private bool lowPerformanceMode = false;
private int guardSeconds = 20;
public bool BringWindowToForeground
{
get => bringWindowToForeground;
set => SetValue(ref bringWindowToForeground, value);
}
public bool DetectOrphanTasks
{
get => detectOrphanTasks;
set => SetValue(ref detectOrphanTasks, value);
}
public bool DetectCorruptedTasks
{
get => detectCorruptedTasks;
set => SetValue(ref detectCorruptedTasks, value);
}
public bool LowPerformanceMode
{
get => lowPerformanceMode;
set => SetValue(ref lowPerformanceMode, value);
}
/// <summary>How long FocusGuard keeps the game in the foreground after launch.</summary>
public int GuardSeconds
{
get => guardSeconds;
set => SetValue(ref guardSeconds, Math.Max(5, Math.Min(120, value)));
}
private int cooldownSeconds = 3;
/// <summary>Minimum seconds between two "Create Pending Tasks" calls.</summary>
public int CooldownSeconds
{
get => cooldownSeconds;
set => SetValue(ref cooldownSeconds, Math.Max(1, Math.Min(30, value)));
}
// =========================================================
// FocusGuard parameters — derived from LowPerformanceMode
// =========================================================
/// <summary>Max ms to wait for the game process to appear.</summary>
public int FocusProcessTimeoutMs => LowPerformanceMode ? 120_000 : 60_000;
/// <summary>Max ms to wait for the game window handle to appear.</summary>
public int FocusWindowTimeoutMs => LowPerformanceMode ? 60_000 : 30_000;
/// <summary>Number of aggressive foreground pushes right after window appears.</summary>
public int FocusEarlyPushCount => LowPerformanceMode ? 8 : 4;
/// <summary>Interval in ms between early pushes.</summary>
public int FocusEarlyPushInterval => LowPerformanceMode ? 250 : 300;
}
// =========================================================
// SETTINGS PROVIDER
// =========================================================
public class GameTaskSettings : ISettings
{
private readonly GameTaskPlugin plugin;
private readonly SettingsManager settingsManager;
public PluginSettings Settings => settingsManager.Current;
private PluginSettings snapshot;
public GameTaskSettings(GameTaskPlugin plugin, SettingsManager settingsManager)
{
this.plugin = plugin;
this.settingsManager = settingsManager;
}
public void BeginEdit()
{
snapshot = new PluginSettings
{
BringWindowToForeground = Settings.BringWindowToForeground,
DetectOrphanTasks = Settings.DetectOrphanTasks,
DetectCorruptedTasks = Settings.DetectCorruptedTasks,
LowPerformanceMode = Settings.LowPerformanceMode,
GuardSeconds = Settings.GuardSeconds,
CooldownSeconds = Settings.CooldownSeconds
};
}
public void CancelEdit()
{
Settings.BringWindowToForeground = snapshot.BringWindowToForeground;
Settings.DetectOrphanTasks = snapshot.DetectOrphanTasks;
Settings.DetectCorruptedTasks = snapshot.DetectCorruptedTasks;
Settings.LowPerformanceMode = snapshot.LowPerformanceMode;
Settings.GuardSeconds = snapshot.GuardSeconds;
Settings.CooldownSeconds = snapshot.CooldownSeconds;
}
public void EndEdit() => settingsManager.Save();
public bool VerifySettings(out List<string> errors)
{
errors = new List<string>();
return true;
}
}
// =========================================================
// PERSISTENCE — simple key=value format, no external deps
// =========================================================
public class SettingsManager
{
private readonly Logger logger;
private readonly string settingsFile;
private PluginSettings current;
public PluginSettings Current => current;
public SettingsManager(Logger logger, string pluginDataPath)
{
this.logger = logger;
string configFolder = Path.Combine(pluginDataPath, "Config");
Directory.CreateDirectory(configFolder);
settingsFile = Path.Combine(configFolder, "Settings.ini");
Load();
}
private void Load()
{
current = new PluginSettings();
try
{
if (!File.Exists(settingsFile))
{
Save();
return;
}
foreach (var line in File.ReadAllLines(settingsFile, Encoding.UTF8))
{
if (string.IsNullOrWhiteSpace(line) || line.StartsWith("#")) continue;
var parts = line.Split('=');
if (parts.Length != 2) continue;
string key = parts[0].Trim();
string value = parts[1].Trim();
switch (key)
{
case "BringWindowToForeground":
current.BringWindowToForeground = value == "true"; break;
case "DetectOrphanTasks":
current.DetectOrphanTasks = value == "true"; break;
case "DetectCorruptedTasks":
current.DetectCorruptedTasks = value == "true"; break;
case "LowPerformanceMode":
current.LowPerformanceMode = value == "true"; break;
case "GuardSeconds":
if (int.TryParse(value, out int gs)) current.GuardSeconds = gs; break;
case "CooldownSeconds":
if (int.TryParse(value, out int cs)) current.CooldownSeconds = cs; break;
}
}
logger.Log("Settings loaded.");
}
catch (Exception ex)
{
logger.Log($"ERROR loading settings: {ex.Message}");
current = new PluginSettings();
}
}
public void Save()
{
try
{
var lines = new[]
{
"# GameTask Settings",
$"BringWindowToForeground={BoolToStr(current.BringWindowToForeground)}",
$"DetectOrphanTasks={BoolToStr(current.DetectOrphanTasks)}",
$"DetectCorruptedTasks={BoolToStr(current.DetectCorruptedTasks)}",
$"LowPerformanceMode={BoolToStr(current.LowPerformanceMode)}",
$"GuardSeconds={current.GuardSeconds}",
$"CooldownSeconds={current.CooldownSeconds}"
};
File.WriteAllLines(settingsFile, lines, Encoding.UTF8);
logger.Log("Settings saved.");
}
catch (Exception ex)
{
logger.Log($"ERROR saving settings: {ex.Message}");
}
}
private static string BoolToStr(bool value) => value ? "true" : "false";
}
}