Skip to content

Commit 01a6e76

Browse files
committed
chore: 优化 Option 目录检索
1 parent f3891e4 commit 01a6e76

14 files changed

Lines changed: 155 additions & 96 deletions

ChuChartManager/Controllers/CourseController.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -312,12 +312,8 @@ public ActionResult DeleteCourse([FromQuery] int id, [FromQuery] string assetDir
312312
}
313313
}
314314

315-
var optionRoot = Path.Combine(StaticSettings.GamePath, "bin", "option");
316-
if (!Directory.Exists(optionRoot)) yield break;
317-
318-
foreach (var optDir in Directory.EnumerateDirectories(optionRoot).OrderBy(d => d))
315+
foreach (var (dirName, optDir) in OptionPathResolver.EnumerateOptionDirectories(StaticSettings.GamePath))
319316
{
320-
var dirName = Path.GetFileName(optDir);
321317
if (source != null && source != dirName) continue;
322318

323319
var courseDir = Path.Combine(optDir, "course");
@@ -337,7 +333,11 @@ public ActionResult DeleteCourse([FromQuery] int id, [FromQuery] string assetDir
337333
if (assetDir == "A000")
338334
courseRoot = Path.Combine(StaticSettings.GamePath, "data", "A000", "course");
339335
else
340-
courseRoot = Path.Combine(StaticSettings.GamePath, "bin", "option", assetDir, "course");
336+
{
337+
var optionPath = OptionPathResolver.ResolveExisting(StaticSettings.GamePath, assetDir);
338+
if (optionPath == null) return null;
339+
courseRoot = Path.Combine(optionPath, "course");
340+
}
341341

342342
if (!Directory.Exists(courseRoot))
343343
return null;
@@ -439,10 +439,7 @@ private static void AppendCourseMusicInfo(XmlDocument doc, XmlElement parent, Cr
439439
if (Directory.Exists(dataPath))
440440
return dataPath;
441441

442-
var optionPath = Path.Combine(StaticSettings.GamePath, "bin", "option", dirName);
443-
if (Directory.Exists(optionPath))
444-
return optionPath;
445-
442+
var optionPath = OptionPathResolver.ResolveWritePath(StaticSettings.GamePath, dirName);
446443
Directory.CreateDirectory(optionPath);
447444
return optionPath;
448445
}

ChuChartManager/Controllers/CustomResourceController.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,11 @@ public ActionResult DeleteMusic([FromQuery] int id, [FromQuery] string assetDir)
132132
if (assetDir == "A000")
133133
musicRoot = Path.Combine(StaticSettings.GamePath, "data", "A000", "music");
134134
else
135-
musicRoot = Path.Combine(StaticSettings.GamePath, "bin", "option", assetDir, "music");
135+
{
136+
var optionPath = OptionPathResolver.ResolveExisting(StaticSettings.GamePath, assetDir);
137+
if (optionPath == null) return NotFound();
138+
musicRoot = Path.Combine(optionPath, "music");
139+
}
136140

137141
if (!Directory.Exists(musicRoot)) return NotFound();
138142

@@ -238,12 +242,8 @@ public ActionResult GetResourcePreview([FromQuery] string type, [FromQuery] int
238242
yield return (d, "A000");
239243
}
240244

241-
var optionRoot = Path.Combine(StaticSettings.GamePath, "bin", "option");
242-
if (!Directory.Exists(optionRoot)) yield break;
243-
244-
foreach (var optDir in Directory.EnumerateDirectories(optionRoot).OrderBy(d => d))
245+
foreach (var (dirName, optDir) in OptionPathResolver.EnumerateOptionDirectories(StaticSettings.GamePath))
245246
{
246-
var dirName = Path.GetFileName(optDir);
247247
if (source != null && source != dirName) continue;
248248

249249
var resDir = Path.Combine(optDir, type);
@@ -262,7 +262,11 @@ public ActionResult GetResourcePreview([FromQuery] string type, [FromQuery] int
262262
if (assetDir == "A000")
263263
resRoot = Path.Combine(StaticSettings.GamePath, "data", "A000", typeDef.type);
264264
else
265-
resRoot = Path.Combine(StaticSettings.GamePath, "bin", "option", assetDir, typeDef.type);
265+
{
266+
var optionPath = OptionPathResolver.ResolveExisting(StaticSettings.GamePath, assetDir);
267+
if (optionPath == null) return null;
268+
resRoot = Path.Combine(optionPath, typeDef.type);
269+
}
266270

267271
if (!Directory.Exists(resRoot)) return null;
268272

@@ -1080,12 +1084,7 @@ public ActionResult AddCharaVariant([FromBody] AddCharaVariantDto dto)
10801084
if (Directory.Exists(dataPath))
10811085
return dataPath;
10821086

1083-
// bin/option 目录下的 opt(如 A001)
1084-
var optionPath = Path.Combine(StaticSettings.GamePath, "bin", "option", dirName);
1085-
if (Directory.Exists(optionPath))
1086-
return optionPath;
1087-
1088-
// 如果都不存在,在 bin/option 下创建
1087+
var optionPath = OptionPathResolver.ResolveWritePath(StaticSettings.GamePath, dirName);
10891088
Directory.CreateDirectory(optionPath);
10901089
return optionPath;
10911090
}

ChuChartManager/Controllers/EmoteController.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,8 @@ public ActionResult GetEmoteWebGLData([FromQuery] string filePath)
179179
yield return (d, "A000");
180180
}
181181

182-
var optionRoot = Path.Combine(StaticSettings.GamePath, "bin", "option");
183-
if (!Directory.Exists(optionRoot)) yield break;
184-
185-
foreach (var optDir in Directory.EnumerateDirectories(optionRoot).OrderBy(d => d))
182+
foreach (var (dirName, optDir) in OptionPathResolver.EnumerateOptionDirectories(StaticSettings.GamePath))
186183
{
187-
var dirName = Path.GetFileName(optDir);
188184
if (source != null && source != "A000" && source != dirName) continue;
189185

190186
var resDir = Path.Combine(optDir, type);

ChuChartManager/Controllers/EventController.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -837,12 +837,8 @@ private static void BuildDefaultSubstances(XmlDocument doc, XmlElement subst)
837837
yield return (d, "A000");
838838
}
839839

840-
var optionRoot = Path.Combine(StaticSettings.GamePath, "bin", "option");
841-
if (!Directory.Exists(optionRoot)) yield break;
842-
843-
foreach (var optDir in Directory.EnumerateDirectories(optionRoot).OrderBy(d => d))
840+
foreach (var (dirName, optDir) in OptionPathResolver.EnumerateOptionDirectories(StaticSettings.GamePath))
844841
{
845-
var dirName = Path.GetFileName(optDir);
846842
if (source != null && source != "A000" && source != dirName) continue;
847843

848844
var resDir = Path.Combine(optDir, type);
@@ -863,7 +859,11 @@ private static void BuildDefaultSubstances(XmlDocument doc, XmlElement subst)
863859
if (assetDir == "A000")
864860
resRoot = Path.Combine(StaticSettings.GamePath, "data", "A000", type);
865861
else
866-
resRoot = Path.Combine(StaticSettings.GamePath, "bin", "option", assetDir, type);
862+
{
863+
var optionPath = OptionPathResolver.ResolveExisting(StaticSettings.GamePath, assetDir);
864+
if (optionPath == null) return null;
865+
resRoot = Path.Combine(optionPath, type);
866+
}
867867

868868
if (Directory.Exists(resRoot))
869869
{
@@ -908,7 +908,7 @@ private static void BuildDefaultSubstances(XmlDocument doc, XmlElement subst)
908908
{
909909
if (string.IsNullOrEmpty(StaticSettings.GamePath)) return null;
910910
if (dirName == "A000") return Path.Combine(StaticSettings.GamePath, "data", "A000");
911-
return Path.Combine(StaticSettings.GamePath, "bin", "option", dirName);
911+
return OptionPathResolver.ResolveWritePath(StaticSettings.GamePath, dirName);
912912
}
913913

914914
private static void SetNodeText(XmlNode parent, string xpath, string value)

ChuChartManager/Controllers/LoginBonusController.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -299,12 +299,8 @@ public ActionResult DeletePreset([FromQuery] int id, [FromQuery] string assetDir
299299
yield return (d, "A000");
300300
}
301301

302-
var optionRoot = Path.Combine(StaticSettings.GamePath, "bin", "option");
303-
if (!Directory.Exists(optionRoot)) yield break;
304-
305-
foreach (var optDir in Directory.EnumerateDirectories(optionRoot).OrderBy(d => d))
302+
foreach (var (dirName, optDir) in OptionPathResolver.EnumerateOptionDirectories(StaticSettings.GamePath))
306303
{
307-
var dirName = Path.GetFileName(optDir);
308304
if (source != null && source != "A000" && source != dirName) continue;
309305

310306
var resDir = Path.Combine(optDir, type);
@@ -325,7 +321,11 @@ public ActionResult DeletePreset([FromQuery] int id, [FromQuery] string assetDir
325321
if (assetDir == "A000")
326322
resRoot = Path.Combine(StaticSettings.GamePath, "data", "A000", type);
327323
else
328-
resRoot = Path.Combine(StaticSettings.GamePath, "bin", "option", assetDir, type);
324+
{
325+
var optionPath = OptionPathResolver.ResolveExisting(StaticSettings.GamePath, assetDir);
326+
if (optionPath == null) return null;
327+
resRoot = Path.Combine(optionPath, type);
328+
}
329329

330330
if (Directory.Exists(resRoot))
331331
{
@@ -370,7 +370,7 @@ public ActionResult DeletePreset([FromQuery] int id, [FromQuery] string assetDir
370370
{
371371
if (string.IsNullOrEmpty(StaticSettings.GamePath)) return null;
372372
if (dirName == "A000") return Path.Combine(StaticSettings.GamePath, "data", "A000");
373-
return Path.Combine(StaticSettings.GamePath, "bin", "option", dirName);
373+
return OptionPathResolver.ResolveWritePath(StaticSettings.GamePath, dirName);
374374
}
375375

376376
private static void SetNodeText(XmlNode parent, string xpath, string value)

ChuChartManager/Controllers/MusicController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,8 +1087,8 @@ public ActionResult ImportMusicExecute(
10871087
var dataPath = Path.Combine(StaticSettings.GamePath, "data", dirName);
10881088
if (Directory.Exists(dataPath)) return dataPath;
10891089

1090-
var optionPath = Path.Combine(StaticSettings.GamePath, "bin", "option", dirName);
1091-
if (Directory.Exists(optionPath)) return optionPath;
1090+
var optionPath = OptionPathResolver.ResolveExisting(StaticSettings.GamePath, dirName);
1091+
if (optionPath != null) return optionPath;
10921092

10931093
return null;
10941094
}

ChuChartManager/Controllers/OptionController.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ public ActionResult<List<OptionDirInfo>> GetOptionDirs()
2323
var isCustom = false;
2424
if (source != "A000")
2525
{
26-
var markPath = Path.Combine(StaticSettings.GamePath, "bin", "option", source, "CustomChartsMark.txt");
26+
var optionPath = scanner.OptionPaths.TryGetValue(source, out var discoveredPath)
27+
? discoveredPath
28+
: OptionPathResolver.ResolveExisting(StaticSettings.GamePath, source);
29+
var markPath = optionPath == null ? "" : Path.Combine(optionPath, "CustomChartsMark.txt");
2730
isCustom = System.IO.File.Exists(markPath);
2831
}
2932
var version = ReadOptVersion(source);
@@ -41,8 +44,8 @@ public ActionResult ToggleCustomMark([FromBody] string dirName)
4144
if (dirName == "A000")
4245
return BadRequest("不能修改基础包标记");
4346

44-
var optionRoot = Path.Combine(StaticSettings.GamePath, "bin", "option");
45-
var markPath = Path.Combine(optionRoot, dirName, "CustomChartsMark.txt");
47+
var optionPath = OptionPathResolver.ResolveWritePath(StaticSettings.GamePath, dirName);
48+
var markPath = Path.Combine(optionPath, "CustomChartsMark.txt");
4649

4750
if (System.IO.File.Exists(markPath))
4851
System.IO.File.Delete(markPath);
@@ -93,12 +96,10 @@ public ActionResult CreateOptionDir([FromBody] string dirName)
9396
if (string.IsNullOrWhiteSpace(dirName))
9497
return BadRequest("目录名不能为空");
9598

96-
var optionRoot = Path.Combine(StaticSettings.GamePath, "bin", "option");
97-
var target = Path.Combine(optionRoot, dirName);
98-
99-
if (Directory.Exists(target))
99+
if (OptionPathResolver.ResolveExisting(StaticSettings.GamePath, dirName) != null)
100100
return BadRequest("目录已存在");
101101

102+
var target = Path.Combine(StaticSettings.GamePath, "bin", "option", dirName);
102103
Directory.CreateDirectory(target);
103104
Directory.CreateDirectory(Path.Combine(target, "music"));
104105

@@ -119,10 +120,9 @@ public ActionResult DeleteOptionDir([FromBody] string dirName)
119120
if (dirName == "A000")
120121
return BadRequest("不能删除基础包");
121122

122-
var optionRoot = Path.Combine(StaticSettings.GamePath, "bin", "option");
123-
var target = Path.Combine(optionRoot, dirName);
123+
var target = OptionPathResolver.ResolveExisting(StaticSettings.GamePath, dirName);
124124

125-
if (!Directory.Exists(target))
125+
if (target == null || !Directory.Exists(target))
126126
return NotFound("目录不存在");
127127

128128
Directory.Delete(target, true);
@@ -162,7 +162,7 @@ public ActionResult ImportLocalOptionDir()
162162
var optionRoot = Path.Combine(StaticSettings.GamePath, "bin", "option");
163163
var dest = Path.Combine(optionRoot, dirName);
164164

165-
if (Directory.Exists(dest))
165+
if (OptionPathResolver.ResolveExisting(StaticSettings.GamePath, dirName) != null)
166166
{
167167
dirName = $"{dirName}_{DateTime.Now:yyyyMMddHHmmss}";
168168
dest = Path.Combine(optionRoot, dirName);
@@ -196,8 +196,8 @@ public ActionResult RescanOptions()
196196
return null;
197197
var dataPath = Path.Combine(StaticSettings.GamePath, "data", dirName);
198198
if (Directory.Exists(dataPath)) return dataPath;
199-
var optionPath = Path.Combine(StaticSettings.GamePath, "bin", "option", dirName);
200-
if (Directory.Exists(optionPath)) return optionPath;
199+
var optionPath = OptionPathResolver.ResolveExisting(StaticSettings.GamePath, dirName);
200+
if (optionPath != null) return optionPath;
201201
return null;
202202
}
203203

ChuChartManager/Controllers/ReleaseTagController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ public ActionResult AddReleaseTag([FromBody] AddReleaseTagRequest req)
4242
if (string.IsNullOrWhiteSpace(req.AssetDir)) return BadRequest("Opt 不能为空");
4343
if (req.AssetDir == "A000") return BadRequest("不能在 A000 创建自定义版本标签");
4444

45-
var optionDir = Path.Combine(gamePath, "bin", "option", req.AssetDir);
46-
if (!Directory.Exists(optionDir)) return BadRequest("Opt 不存在");
45+
var optionDir = OptionPathResolver.ResolveExisting(gamePath, req.AssetDir);
46+
if (optionDir == null) return BadRequest("Opt 不存在");
4747

4848
var existing = ReleaseTagXml.ScanAll(gamePath);
4949
if (existing.Any(x => x.Id == req.Id)) return BadRequest($"ID {req.Id} 已存在");

ChuChartManager/Controllers/StageController.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -223,12 +223,8 @@ public ActionResult DeleteStage([FromQuery] int id, [FromQuery] string assetDir)
223223
yield return (d, "A000");
224224
}
225225

226-
var optionRoot = Path.Combine(StaticSettings.GamePath, "bin", "option");
227-
if (!Directory.Exists(optionRoot)) yield break;
228-
229-
foreach (var optDir in Directory.EnumerateDirectories(optionRoot).OrderBy(d => d))
226+
foreach (var (dirName, optDir) in OptionPathResolver.EnumerateOptionDirectories(StaticSettings.GamePath))
230227
{
231-
var dirName = Path.GetFileName(optDir);
232228
if (source != null && source != dirName) continue;
233229
var resDir = Path.Combine(optDir, type);
234230
if (!Directory.Exists(resDir)) continue;
@@ -245,7 +241,11 @@ public ActionResult DeleteStage([FromQuery] int id, [FromQuery] string assetDir)
245241
if (assetDir == "A000")
246242
resRoot = Path.Combine(StaticSettings.GamePath, "data", "A000", type);
247243
else
248-
resRoot = Path.Combine(StaticSettings.GamePath, "bin", "option", assetDir, type);
244+
{
245+
var optionPath = OptionPathResolver.ResolveExisting(StaticSettings.GamePath, assetDir);
246+
if (optionPath == null) return null;
247+
resRoot = Path.Combine(optionPath, type);
248+
}
249249

250250
if (!Directory.Exists(resRoot)) return null;
251251

@@ -271,8 +271,7 @@ public ActionResult DeleteStage([FromQuery] int id, [FromQuery] string assetDir)
271271
if (string.IsNullOrEmpty(StaticSettings.GamePath) || string.IsNullOrWhiteSpace(dirName)) return null;
272272
var dataPath = Path.Combine(StaticSettings.GamePath, "data", dirName);
273273
if (Directory.Exists(dataPath)) return dataPath;
274-
var optionPath = Path.Combine(StaticSettings.GamePath, "bin", "option", dirName);
275-
if (Directory.Exists(optionPath)) return optionPath;
274+
var optionPath = OptionPathResolver.ResolveWritePath(StaticSettings.GamePath, dirName);
276275
Directory.CreateDirectory(optionPath);
277276
return optionPath;
278277
}

ChuChartManager/Models/GenreSortXml.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class GenreSortXml
88

99
public string GamePath { get; }
1010
public string AssetDir { get; }
11-
public string FilePath => Path.Combine(GamePath, "bin", "option", AssetDir, "music", "GenreSort.xml");
11+
public string FilePath => Path.Combine(OptionPathResolver.ResolveWritePath(GamePath, AssetDir), "music", "GenreSort.xml");
1212

1313
private GenreSortXml(string gamePath, string assetDir, XmlDocument xmlDoc)
1414
{
@@ -19,7 +19,7 @@ private GenreSortXml(string gamePath, string assetDir, XmlDocument xmlDoc)
1919

2020
public static GenreSortXml LoadOrCreate(string gamePath, string assetDir)
2121
{
22-
var path = Path.Combine(gamePath, "bin", "option", assetDir, "music", "GenreSort.xml");
22+
var path = Path.Combine(OptionPathResolver.ResolveWritePath(gamePath, assetDir), "music", "GenreSort.xml");
2323
var doc = new XmlDocument();
2424
if (File.Exists(path))
2525
{
@@ -80,18 +80,16 @@ private static XmlDocument CreateBaseDocument(string gamePath, string targetPath
8080
private static IEnumerable<string> GetBaseCandidates(string gamePath)
8181
{
8282
yield return Path.Combine(gamePath, "data", "A000", "music", "GenreSort.xml");
83-
yield return Path.Combine(gamePath, "bin", "option", "A001", "music", "GenreSort.xml");
83+
var optionPath = OptionPathResolver.ResolveExisting(gamePath, "A001");
84+
if (optionPath != null)
85+
yield return Path.Combine(optionPath, "music", "GenreSort.xml");
8486
}
8587

8688
public static List<GenreSortXml> ScanAll(string gamePath)
8789
{
88-
var optionRoot = Path.Combine(gamePath, "bin", "option");
89-
if (!Directory.Exists(optionRoot)) return [];
90-
9190
var result = new List<GenreSortXml>();
92-
foreach (var optDir in Directory.EnumerateDirectories(optionRoot).OrderBy(d => d))
91+
foreach (var (assetDir, optDir) in OptionPathResolver.EnumerateOptionDirectories(gamePath))
9392
{
94-
var assetDir = Path.GetFileName(optDir);
9593
var path = Path.Combine(optDir, "music", "GenreSort.xml");
9694
if (!File.Exists(path)) continue;
9795
try

0 commit comments

Comments
 (0)