-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskManager.cs
More file actions
171 lines (133 loc) · 5.75 KB
/
TaskManager.cs
File metadata and controls
171 lines (133 loc) · 5.75 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
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using Playnite.SDK.Models;
namespace GameTaskPlugin
{
public class TaskManager
{
private readonly Logger logger;
private readonly string cacheFolder;
private readonly string pendingFile;
private readonly string deleteFile;
public TaskManager(Logger logger, string pluginDataPath)
{
this.logger = logger;
cacheFolder = Path.Combine(pluginDataPath, "Cache");
Directory.CreateDirectory(cacheFolder);
pendingFile = Path.Combine(cacheFolder, "PendingTasks.txt");
deleteFile = Path.Combine(cacheFolder, "DeleteTasks.txt");
if (!File.Exists(pendingFile)) File.WriteAllText(pendingFile, string.Empty);
if (!File.Exists(deleteFile)) File.WriteAllText(deleteFile, string.Empty);
}
// =========================================================
// Pending Tasks
// =========================================================
public void ResetPendingFile()
{
File.WriteAllText(pendingFile, string.Empty);
logger.Log("PendingTasks reset.");
}
public void AddPendingTask(Game game, string ignoredActionName, string resolvedExeOverride = null)
{
if (game == null) return;
string resolvedExe = resolvedExeOverride;
if (string.IsNullOrWhiteSpace(resolvedExe))
{
var action = GetValidAction(game, ignoredActionName);
if (action == null)
{
logger.Log($"ERROR: No valid action found for: {game.Name}");
return;
}
resolvedExe = ResolveExecutable(game, action);
logger.Log($"Original Action.Path: {action.Path}");
}
logger.Log($"Resolved Path: {resolvedExe}");
if (string.IsNullOrWhiteSpace(resolvedExe))
{
logger.Log($"ERROR: Resolved EXE empty: {game.Name}");
return;
}
if (!File.Exists(resolvedExe))
{
logger.Log($"SKIP exe not found: {game.Name} -> {resolvedExe}");
return;
}
string entry = $"{game.Name}|{resolvedExe}";
// Remove any existing entry for this game before adding updated one
var existing = File.ReadAllLines(pendingFile)
.Where(l => !l.StartsWith(game.Name + "|", StringComparison.OrdinalIgnoreCase))
.ToList();
existing.Add(entry);
File.WriteAllLines(pendingFile, existing);
logger.Log($"Pending task added: {game.Name}");
}
public void RemovePendingEntry(Game game)
{
if (game == null || !File.Exists(pendingFile)) return;
var lines = File.ReadAllLines(pendingFile)
.Where(l => !l.StartsWith(game.Name + "|", StringComparison.OrdinalIgnoreCase))
.ToArray();
File.WriteAllLines(pendingFile, lines);
logger.Log($"Pending entry removed: {game.Name}");
}
public List<(string GameName, string ExePath)> GetPendingTasks()
{
var result = new List<(string, string)>();
if (!File.Exists(pendingFile)) return result;
foreach (var line in File.ReadAllLines(pendingFile))
{
if (string.IsNullOrWhiteSpace(line)) continue;
var parts = line.Split('|');
if (parts.Length < 2) continue;
result.Add((parts[0], parts[1]));
}
return result;
}
// =========================================================
// Delete Tasks
// =========================================================
public void ResetDeleteFile()
{
File.WriteAllText(deleteFile, string.Empty);
logger.Log("DeleteTasks reset.");
}
public void AddDeleteTask(Game game)
{
if (game == null) return;
string taskName = $"GameTask_v1_{Utils.MakeSafeTaskName(game.Name)}";
var existing = File.ReadAllLines(deleteFile);
if (!existing.Any(l => l.Equals(taskName, StringComparison.OrdinalIgnoreCase)))
{
File.AppendAllLines(deleteFile, new[] { taskName });
logger.Log($"Delete task added: {taskName}");
}
}
public List<string> GetDeleteTasks()
{
return File.Exists(deleteFile) ? File.ReadAllLines(deleteFile).ToList() : new List<string>();
}
// =========================================================
// Internal Helpers
// =========================================================
private GameAction GetValidAction(Game game, string ignoredActionName)
{
if (game.GameActions == null) return null;
return game.GameActions.FirstOrDefault(a => a != null && a.Name != ignoredActionName && !string.IsNullOrWhiteSpace(a.Path));
}
public string ResolveExecutable(Game game, GameAction action)
{
if (action == null) return null;
string path = action.Path?.Trim();
if (string.IsNullOrWhiteSpace(path)) return null;
path = path.Trim('"');
if (path.Contains("{InstallDir}") && !string.IsNullOrWhiteSpace(game.InstallDirectory))
path = path.Replace("{InstallDir}", game.InstallDirectory);
if (!Path.IsPathRooted(path) && !string.IsNullOrWhiteSpace(game.InstallDirectory))
path = Path.Combine(game.InstallDirectory, path);
return path;
}
}
}