Skip to content

Commit 0ed43bd

Browse files
committed
refactor: ModConfigService
1 parent 13785ca commit 0ed43bd

5 files changed

Lines changed: 135 additions & 140 deletions

File tree

MaiChartManager/Controllers/Mod/ConfigurationController.cs

Lines changed: 7 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -16,135 +16,19 @@ public class ConfigurationController : ControllerBase
1616
{
1717
private readonly StaticSettings settings;
1818
private readonly ILogger<ConfigurationController> logger;
19-
private readonly MuModService muModService;
20-
private static MuModService? staticMuModService;
19+
private readonly ModConfigService modConfigService;
2120

22-
public ConfigurationController(StaticSettings settings, ILogger<ConfigurationController> logger, MuModService muModService)
21+
public ConfigurationController(StaticSettings settings, ILogger<ConfigurationController> logger, ModConfigService modConfigService)
2322
{
2423
this.settings = settings;
2524
this.logger = logger;
26-
this.muModService = muModService;
27-
staticMuModService = muModService;
28-
}
29-
30-
public class UnsupportedConfigApiVersionException() : Exception(Locale.UnsupportedConfigVersion);
31-
32-
public class ConfigCorruptedException() : Exception(Locale.AquaMaiConfigCorrupted);
33-
34-
public class AquaMaiNotInstalledException() : Exception(Locale.AquaMaiNotInstalled);
35-
36-
public class AquaMaiSignatureVerificationFailedException() : Exception("AquaMaiSignatureVerificationFailed");
37-
38-
[NonAction]
39-
private static void CheckConfigApiVersion(HeadlessConfigInterface configInterface)
40-
{
41-
var currentSupportedApiVersion = new Version(1, 1);
42-
var configApiVersion = new Version(configInterface.ApiVersion);
43-
if (currentSupportedApiVersion.Major != configApiVersion.Major)
44-
{
45-
throw new UnsupportedConfigApiVersionException();
46-
}
47-
48-
if (currentSupportedApiVersion.Minor > configApiVersion.Minor)
49-
{
50-
throw new UnsupportedConfigApiVersionException();
51-
}
52-
}
53-
54-
[NonAction]
55-
public static IConfig GetCurrentAquaMaiConfig(bool forceDefault = false, bool skipSignatureCheck = false)
56-
{
57-
string dllPath;
58-
try
59-
{
60-
dllPath = GetAquaMaiDllPath(staticMuModService);
61-
}
62-
catch (AquaMaiNotInstalledException)
63-
{
64-
throw;
65-
}
66-
67-
var binary = System.IO.File.ReadAllBytes(dllPath);
68-
if (!skipSignatureCheck)
69-
{
70-
var sigResult = AquaMaiSignatureV2.VerifySignature(binary);
71-
if (sigResult.Status != AquaMaiSignatureV2.VerifyStatus.Valid)
72-
{
73-
throw new AquaMaiSignatureVerificationFailedException();
74-
}
75-
}
76-
var configInterface = HeadlessConfigLoader.LoadFromPacked(binary);
77-
var config = configInterface.CreateConfig();
78-
CheckConfigApiVersion(configInterface);
79-
if (System.IO.File.Exists(ModPaths.AquaMaiConfigPath) && !forceDefault)
80-
{
81-
try
82-
{
83-
var view = configInterface.CreateConfigView(System.IO.File.ReadAllText(ModPaths.AquaMaiConfigPath));
84-
var migrationManager = configInterface.GetConfigMigrationManager();
85-
86-
if (migrationManager.GetVersion(view) != migrationManager.LatestVersion)
87-
{
88-
Console.WriteLine("Migrating AquaMai config from {0} to {1}", migrationManager.GetVersion(view), migrationManager.LatestVersion);
89-
view = migrationManager.Migrate(view);
90-
}
91-
92-
var parser = configInterface.GetConfigParser();
93-
parser.Parse(config, view);
94-
StaticSettings.UpdateAssetPathsFromAquaMaiConfig(config);
95-
}
96-
catch (Exception ex)
97-
{
98-
Console.WriteLine("无法加载 AquaMai 配置");
99-
Console.WriteLine(ex);
100-
if (ex.Message.Contains("Could not migrate the config"))
101-
{
102-
// 这个应该是,AquaMai 未安装或需要更新
103-
throw;
104-
}
105-
// 这个的提示是 AquaMai 配置文件损坏
106-
throw new ConfigCorruptedException();
107-
}
108-
}
109-
110-
return config;
111-
}
112-
113-
[NonAction]
114-
private string GetAquaMaiDllPath()
115-
{
116-
return GetAquaMaiDllPath(muModService);
117-
}
118-
119-
[NonAction]
120-
private static string GetAquaMaiDllPath(MuModService? muModService)
121-
{
122-
var muModInstalled = muModService?.IsMuModInstalled() == true;
123-
var aquaMaiInstalled = System.IO.File.Exists(ModPaths.AquaMaiDllInstalledPath);
124-
125-
if (muModInstalled && !aquaMaiInstalled)
126-
{
127-
var cachePath = muModService!.GetResolvedCachePath();
128-
if (!System.IO.File.Exists(cachePath))
129-
{
130-
throw new AquaMaiNotInstalledException();
131-
}
132-
133-
return cachePath;
134-
}
135-
136-
if (aquaMaiInstalled)
137-
{
138-
return ModPaths.AquaMaiDllInstalledPath;
139-
}
140-
141-
throw new AquaMaiNotInstalledException();
25+
this.modConfigService = modConfigService;
14226
}
14327

14428
[HttpGet]
14529
public AquaMaiConfigDto.ConfigDto GetAquaMaiConfig(bool forceDefault = false, bool skipSignatureCheck = false)
14630
{
147-
var dllPath = GetAquaMaiDllPath();
31+
var dllPath = modConfigService.GetAquaMaiDllPath();
14832
Dictionary<string, string[]>? configSort = null;
14933
using (var stream = new FileStream(dllPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
15034
{
@@ -159,7 +43,7 @@ public AquaMaiConfigDto.ConfigDto GetAquaMaiConfig(bool forceDefault = false, bo
15943
}
16044
}
16145
var shouldSkipSignatureCheck = skipSignatureCheck || !string.Equals(dllPath, ModPaths.AquaMaiDllInstalledPath, StringComparison.OrdinalIgnoreCase);
162-
var config = GetCurrentAquaMaiConfig(forceDefault, shouldSkipSignatureCheck);
46+
var config = modConfigService.GetCurrentAquaMaiConfig(forceDefault, shouldSkipSignatureCheck);
16347
return new AquaMaiConfigDto.ConfigDto(
16448
config.ReflectionManager.Sections.Select(section =>
16549
{
@@ -175,12 +59,12 @@ public AquaMaiConfigDto.ConfigDto GetAquaMaiConfig(bool forceDefault = false, bo
17559
[HttpPut]
17660
public async Task SetAquaMaiConfig(AquaMaiConfigDto.ConfigSaveDto config)
17761
{
178-
var dllPath = GetAquaMaiDllPath();
62+
var dllPath = modConfigService.GetAquaMaiDllPath();
17963
var jsonOptions = new JsonSerializerOptions();
18064
jsonOptions.Converters.Add(new JsonStringEnumConverter());
18165

18266
var configInterface = HeadlessConfigLoader.LoadFromPacked(dllPath);
183-
CheckConfigApiVersion(configInterface);
67+
modConfigService.CheckConfigApiVersion(configInterface);
18468
var configEdit = configInterface.CreateConfig();
18569

18670
foreach (var section in configEdit.ReflectionManager.Sections)
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
using AquaMai.Config.HeadlessLoader;
2+
using AquaMai.Config.Interfaces;
3+
using MaiChartManager.Utils;
4+
5+
namespace MaiChartManager.Controllers.Mod;
6+
7+
public class ModConfigService
8+
{
9+
private readonly MuModService _muModService;
10+
11+
public ModConfigService(MuModService muModService)
12+
{
13+
_muModService = muModService;
14+
}
15+
16+
public class UnsupportedConfigApiVersionException() : Exception(Locale.UnsupportedConfigVersion);
17+
18+
public class ConfigCorruptedException() : Exception(Locale.AquaMaiConfigCorrupted);
19+
20+
public class AquaMaiNotInstalledException() : Exception(Locale.AquaMaiNotInstalled);
21+
22+
public class AquaMaiSignatureVerificationFailedException() : Exception("AquaMaiSignatureVerificationFailed");
23+
24+
public void CheckConfigApiVersion(HeadlessConfigInterface configInterface)
25+
{
26+
var currentSupportedApiVersion = new Version(1, 1);
27+
var configApiVersion = new Version(configInterface.ApiVersion);
28+
if (currentSupportedApiVersion.Major != configApiVersion.Major)
29+
{
30+
throw new UnsupportedConfigApiVersionException();
31+
}
32+
33+
if (currentSupportedApiVersion.Minor > configApiVersion.Minor)
34+
{
35+
throw new UnsupportedConfigApiVersionException();
36+
}
37+
}
38+
39+
public string GetAquaMaiDllPath()
40+
{
41+
var muModInstalled = _muModService.IsMuModInstalled();
42+
var aquaMaiInstalled = File.Exists(ModPaths.AquaMaiDllInstalledPath);
43+
44+
if (muModInstalled && !aquaMaiInstalled)
45+
{
46+
var cachePath = _muModService.GetResolvedCachePath();
47+
if (!File.Exists(cachePath))
48+
{
49+
throw new AquaMaiNotInstalledException();
50+
}
51+
52+
return cachePath;
53+
}
54+
55+
if (aquaMaiInstalled)
56+
{
57+
return ModPaths.AquaMaiDllInstalledPath;
58+
}
59+
60+
throw new AquaMaiNotInstalledException();
61+
}
62+
63+
public IConfig GetCurrentAquaMaiConfig(bool forceDefault = false, bool skipSignatureCheck = false)
64+
{
65+
var dllPath = GetAquaMaiDllPath();
66+
67+
var binary = File.ReadAllBytes(dllPath);
68+
if (!skipSignatureCheck)
69+
{
70+
var sigResult = AquaMaiSignatureV2.VerifySignature(binary);
71+
if (sigResult.Status != AquaMaiSignatureV2.VerifyStatus.Valid)
72+
{
73+
throw new AquaMaiSignatureVerificationFailedException();
74+
}
75+
}
76+
var configInterface = HeadlessConfigLoader.LoadFromPacked(binary);
77+
var config = configInterface.CreateConfig();
78+
CheckConfigApiVersion(configInterface);
79+
if (File.Exists(ModPaths.AquaMaiConfigPath) && !forceDefault)
80+
{
81+
try
82+
{
83+
var view = configInterface.CreateConfigView(File.ReadAllText(ModPaths.AquaMaiConfigPath));
84+
var migrationManager = configInterface.GetConfigMigrationManager();
85+
86+
if (migrationManager.GetVersion(view) != migrationManager.LatestVersion)
87+
{
88+
Console.WriteLine("Migrating AquaMai config from {0} to {1}", migrationManager.GetVersion(view), migrationManager.LatestVersion);
89+
view = migrationManager.Migrate(view);
90+
}
91+
92+
var parser = configInterface.GetConfigParser();
93+
parser.Parse(config, view);
94+
StaticSettings.UpdateAssetPathsFromAquaMaiConfig(config);
95+
}
96+
catch (Exception ex)
97+
{
98+
Console.WriteLine("无法加载 AquaMai 配置");
99+
Console.WriteLine(ex);
100+
if (ex.Message.Contains("Could not migrate the config"))
101+
{
102+
// 这个应该是,AquaMai 未安装或需要更新
103+
throw;
104+
}
105+
// 这个的提示是 AquaMai 配置文件损坏
106+
throw new ConfigCorruptedException();
107+
}
108+
}
109+
110+
return config;
111+
}
112+
}

MaiChartManager/Front/vite.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export default defineConfig(({command}) => ({
3434
server: {
3535
proxy: {
3636
'/MaiChartManagerServlet': 'http://localhost:5181'
37-
}
37+
},
38+
port: 5182,
3839
}
3940
}));

MaiChartManager/ServerManager.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public static void StartApp(bool export, Action<string>? onStart = null)
117117
.AddSingleton<StaticSettings>()
118118
.AddSingleton<MaidataImportService>()
119119
.AddSingleton<MuModService>()
120+
.AddSingleton<ModConfigService>()
120121
.AddEndpointsApiExplorer()
121122
.AddSwaggerGen(options => { options.CustomSchemaIds(type => type.Name == "Config" ? type.FullName : type.Name); })
122123
.Configure<FormOptions>(x =>

MaiChartManager/StaticSettings.cs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ public partial class StaticSettings
3131
public static string CurrentLocale { get; set; } = "zh";
3232

3333
private readonly ILogger<StaticSettings> _logger;
34+
private readonly Controllers.Mod.ModConfigService _modConfigService;
3435

35-
public StaticSettings(ILogger<StaticSettings> logger)
36+
public StaticSettings(ILogger<StaticSettings> logger, Controllers.Mod.ModConfigService modConfigService)
3637
{
3738
_logger = logger;
39+
_modConfigService = modConfigService;
3840
if (string.IsNullOrEmpty(GamePath)) return; // OOBE mode: skip scan
3941
try
4042
{
@@ -98,7 +100,15 @@ public void RescanAll()
98100
{
99101
GetGameVersion();
100102
StartupErrorsList.Clear();
101-
UpdateAssetPathsFromAquaMaiConfig();
103+
try
104+
{
105+
var config = _modConfigService.GetCurrentAquaMaiConfig();
106+
UpdateAssetPathsFromAquaMaiConfig(config);
107+
}
108+
catch (Exception)
109+
{
110+
Console.WriteLine("无法获取 AquaMai 配置");
111+
}
102112
ScanMusicList();
103113
ScanGenre();
104114
ScanVersionList();
@@ -302,21 +312,8 @@ public string GetFreeAssetDir()
302312
return $"A{id:000}";
303313
}
304314

305-
public static void UpdateAssetPathsFromAquaMaiConfig(IConfig? config = null)
315+
public static void UpdateAssetPathsFromAquaMaiConfig(IConfig config)
306316
{
307-
if (config == null)
308-
{
309-
try
310-
{
311-
config = Controllers.Mod.ConfigurationController.GetCurrentAquaMaiConfig();
312-
}
313-
catch (Exception e)
314-
{
315-
Console.WriteLine("无法获取 AquaMai 配置");
316-
return;
317-
}
318-
}
319-
320317
var imageAssetsDir = config.GetEntryState("GameSystem.Assets.LoadLocalImages.ImageAssetsDir");
321318
// 旧版兼容
322319
var localAssetsDir = config.GetEntryState("GameSystem.Assets.LoadLocalImages.LocalAssetsDir");

0 commit comments

Comments
 (0)