-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModCleanupService.cs
More file actions
248 lines (218 loc) · 9.22 KB
/
Copy pathModCleanupService.cs
File metadata and controls
248 lines (218 loc) · 9.22 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
using Newtonsoft.Json;
using STAGE.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace STAGE
{
public sealed record ModCleanupResult(
int FilesScanned,
int JsonFilesUpdated,
int RemovedAudioTracks,
int RemovedVfxPapOptions,
int RemovedVfxPapMappings,
IReadOnlyList<string> Errors);
public static class ModCleanupService
{
private static readonly string[] AudioExtensions = [".scd"];
private static readonly string[] VfxPapExtensions = [".pap", ".avfx"];
public static ModCleanupResult PurgeMissingReferences()
{
int scanned = 0;
int updated = 0;
int removedAudio = 0;
int removedOptions = 0;
int removedMappings = 0;
var errors = new List<string>();
foreach (string modDirectory in GetCleanupModFolders())
{
if (!Directory.Exists(modDirectory))
continue;
if (PenumbraMeta.TryLoad(modDirectory, out var meta))
{
scanned++;
try
{
bool changed = false;
foreach (var group in meta!.Groups)
{
if (VfxPapGroup.IsAudioPlaylistGroup(group))
{
int before = group.Options.Count;
group.Options = group.Options
.Where(option => ShouldKeepAudioOption(modDirectory, option))
.ToList();
int removed = before - group.Options.Count;
if (removed > 0)
{
changed = true;
removedAudio += removed;
}
}
else if (PurgeMissingVfxPapFromGroup(modDirectory, group, ref removedOptions, ref removedMappings))
{
changed = true;
}
}
if (changed)
{
meta.Save();
PenumbraApi.ScheduleReloadModFolder(modDirectory);
updated++;
}
}
catch (Exception ex)
{
errors.Add($"{Path.GetFileName(modDirectory)}\\meta.json: {ex.Message}");
}
continue;
}
foreach (string groupPath in Directory.GetFiles(modDirectory, "group_*.json")
.OrderBy(path => path, StringComparer.OrdinalIgnoreCase))
{
scanned++;
try
{
string json = File.ReadAllText(groupPath);
var playlist = JsonConvert.DeserializeObject<Playlist>(json);
if (playlist?.Options == null)
continue;
if (VfxPapGroup.IsAudioPlaylistGroup(playlist))
{
int before = playlist.Options.Count;
playlist.Options = playlist.Options
.Where(option => ShouldKeepAudioOption(modDirectory, option))
.ToList();
int removed = before - playlist.Options.Count;
if (removed > 0)
{
WriteGroupJson(groupPath, playlist);
PenumbraApi.ScheduleReloadModFolder(modDirectory);
updated++;
removedAudio += removed;
}
}
else
{
var group = JsonConvert.DeserializeObject<VfxPapGroup>(json);
if (group?.Options == null)
continue;
bool changed = PurgeMissingVfxPapFromGroup(
modDirectory,
group,
ref removedOptions,
ref removedMappings);
if (changed)
{
WriteGroupJson(groupPath, group);
PenumbraApi.ScheduleReloadModFolder(modDirectory);
updated++;
}
}
}
catch (Exception ex)
{
errors.Add($"{Path.GetFileName(groupPath)}: {ex.Message}");
}
}
}
return new ModCleanupResult(
scanned,
updated,
removedAudio,
removedOptions,
removedMappings,
errors);
}
private static bool PurgeMissingVfxPapFromGroup(
string modDirectory,
VfxPapGroup group,
ref int removedOptions,
ref int removedMappings)
{
bool changed = false;
foreach (var option in group.Options.ToList())
{
if (option.Files == null || option.Files.Count == 0)
continue;
var keysToRemove = option.Files
.Where(pair => IsTargetExtension(pair.Key, pair.Value, VfxPapExtensions))
.Where(pair => !MappedFileExists(modDirectory, pair.Value))
.Select(pair => pair.Key)
.ToList();
foreach (string key in keysToRemove)
option.Files.Remove(key);
if (keysToRemove.Count > 0)
{
changed = true;
removedMappings += keysToRemove.Count;
}
if (keysToRemove.Count > 0 && option.Files.Count == 0 && !IsProtectedOption(option))
{
group.Options.Remove(option);
removedOptions++;
}
}
return changed;
}
private static IReadOnlyList<string> GetCleanupModFolders()
{
var folders = Settings.ManagedModFolders.ToList();
string active = Settings.ActiveModFolder;
if (!string.IsNullOrWhiteSpace(active) &&
!folders.Contains(active, StringComparer.OrdinalIgnoreCase))
{
folders.Add(active);
}
return folders
.Where(path => !string.IsNullOrWhiteSpace(path))
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToList();
}
private static bool ShouldKeepAudioOption(string modDirectory, Option option)
{
if (IsProtectedOption(option))
return true;
if (option.Files == null || option.Files.Count == 0)
return true;
if (!option.Files.Any(pair => IsTargetExtension(pair.Key, pair.Value, AudioExtensions)))
return true;
string scdPath = Playlist.GetScdPath(option);
return string.IsNullOrWhiteSpace(scdPath) || MappedFileExists(modDirectory, scdPath);
}
private static bool IsProtectedOption(Option option)
{
return option.Name.Equals("Off", StringComparison.OrdinalIgnoreCase) ||
option.Name.Equals("Default", StringComparison.OrdinalIgnoreCase);
}
private static bool IsTargetExtension(string gamePath, string localPath, IReadOnlyCollection<string> extensions)
{
return extensions.Any(extension =>
gamePath.EndsWith(extension, StringComparison.OrdinalIgnoreCase) ||
localPath.EndsWith(extension, StringComparison.OrdinalIgnoreCase));
}
private static bool MappedFileExists(string modDirectory, string relativePath)
{
if (string.IsNullOrWhiteSpace(relativePath))
return true;
string fullPath = Path.GetFullPath(Path.Combine(modDirectory, ToNativePath(relativePath)));
string modRoot = Path.GetFullPath(modDirectory).TrimEnd(
Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar) + Path.DirectorySeparatorChar;
if (!fullPath.StartsWith(modRoot, StringComparison.OrdinalIgnoreCase))
return true;
return File.Exists(fullPath);
}
private static void WriteGroupJson(string groupPath, object group)
{
string json = JsonConvert.SerializeObject(group, Formatting.Indented);
PenumbraMeta.RequireFormat(Path.GetDirectoryName(groupPath) ?? "", PenumbraModFormat.LegacyGroupFiles);
File.WriteAllText(groupPath, json);
}
private static string ToNativePath(string path)
{
return path.Replace('/', Path.DirectorySeparatorChar)
.Replace('\\', Path.DirectorySeparatorChar);
}
}
}