@@ -12,91 +12,25 @@ namespace MaiChartManager.Controllers.Mod;
1212
1313[ ApiController ]
1414[ Route ( "MaiChartManagerServlet/[action]Api" ) ]
15- public class ConfigurationController ( StaticSettings settings , ILogger < ConfigurationController > logger ) : ControllerBase
15+ public class ConfigurationController : ControllerBase
1616{
17- public class UnsupportedConfigApiVersionException ( ) : Exception ( Locale . UnsupportedConfigVersion ) ;
17+ private readonly StaticSettings settings ;
18+ private readonly ILogger < ConfigurationController > logger ;
19+ private readonly ModConfigService modConfigService ;
1820
19- public class ConfigCorruptedException( ) : Exception ( Locale . AquaMaiConfigCorrupted ) ;
20-
21- public class AquaMaiNotInstalledException( ) : Exception ( Locale . AquaMaiNotInstalled ) ;
22-
23- public class AquaMaiSignatureVerificationFailedException( ) : Exception ( "AquaMaiSignatureVerificationFailed" ) ;
24-
25- [ NonAction]
26- private static void CheckConfigApiVersion( HeadlessConfigInterface configInterface )
27- {
28- var currentSupportedApiVersion = new Version ( 1 , 1 ) ;
29- var configApiVersion = new Version ( configInterface . ApiVersion ) ;
30- if ( currentSupportedApiVersion . Major ! = configApiVersion . Major )
31- {
32- throw new UnsupportedConfigApiVersionException ( ) ;
33- }
34-
35- if ( currentSupportedApiVersion . Minor > configApiVersion . Minor )
36- {
37- throw new UnsupportedConfigApiVersionException ( ) ;
38- }
39- }
40-
41- [ NonAction ]
42- public static IConfig GetCurrentAquaMaiConfig ( bool forceDefault = false , bool skipSignatureCheck = false )
21+ public ConfigurationController ( StaticSettings settings , ILogger < ConfigurationController > logger , ModConfigService modConfigService )
4322 {
44- if ( ! System . IO . File . Exists ( ModPaths . AquaMaiDllInstalledPath ) )
45- {
46- throw new AquaMaiNotInstalledException ( ) ;
47- }
48-
49- var binary = System . IO . File . ReadAllBytes ( ModPaths . AquaMaiDllInstalledPath ) ;
50- if ( ! skipSignatureCheck )
51- {
52- var sigResult = AquaMaiSignatureV2 . VerifySignature ( binary ) ;
53- if ( sigResult . Status != AquaMaiSignatureV2 . VerifyStatus . Valid )
54- {
55- throw new AquaMaiSignatureVerificationFailedException ( ) ;
56- }
57- }
58- var configInterface = HeadlessConfigLoader . LoadFromPacked ( binary ) ;
59- var config = configInterface . CreateConfig ( ) ;
60- CheckConfigApiVersion ( configInterface ) ;
61- if ( System . IO . File . Exists ( ModPaths . AquaMaiConfigPath ) && ! forceDefault )
62- {
63- try
64- {
65- var view = configInterface . CreateConfigView ( System . IO . File . ReadAllText ( ModPaths . AquaMaiConfigPath ) ) ;
66- var migrationManager = configInterface . GetConfigMigrationManager ( ) ;
67-
68- if ( migrationManager . GetVersion ( view ) != migrationManager . LatestVersion )
69- {
70- Console . WriteLine ( "Migrating AquaMai config from {0} to {1}" , migrationManager . GetVersion ( view ) , migrationManager . LatestVersion ) ;
71- view = migrationManager . Migrate ( view ) ;
72- }
73-
74- var parser = configInterface . GetConfigParser ( ) ;
75- parser . Parse ( config , view ) ;
76- StaticSettings . UpdateAssetPathsFromAquaMaiConfig ( config ) ;
77- }
78- catch ( Exception ex )
79- {
80- Console . WriteLine ( "无法加载 AquaMai 配置" ) ;
81- Console . WriteLine ( ex ) ;
82- if ( ex . Message . Contains ( "Could not migrate the config" ) )
83- {
84- // 这个应该是,AquaMai 未安装或需要更新
85- throw ;
86- }
87- // 这个的提示是 AquaMai 配置文件损坏
88- throw new ConfigCorruptedException ( ) ;
89- }
90- }
91-
92- return config ;
23+ this . settings = settings ;
24+ this . logger = logger ;
25+ this . modConfigService = modConfigService ;
9326 }
9427
9528 [ HttpGet ]
96- public AquaMaiConfigDto . ConfigDto GetAquaMaiConfig ( bool forceDefault = false , bool skipSignatureCheck = false )
29+ public async Task < AquaMaiConfigDto . ConfigDto > GetAquaMaiConfig ( bool forceDefault = false , bool skipSignatureCheck = false )
9730 {
31+ var dllPath = await modConfigService . GetAquaMaiDllPath ( ) ;
9832 Dictionary < string , string [ ] > ? configSort = null ;
99- using ( var stream = new FileStream ( ModPaths . AquaMaiDllInstalledPath , FileMode . Open , FileAccess . Read , FileShare . ReadWrite ) )
33+ using ( var stream = new FileStream ( dllPath , FileMode . Open , FileAccess . Read , FileShare . ReadWrite ) )
10034 {
10135 var asm = ModuleDefinition . ReadModule ( stream ) ;
10236 var configSortRes = asm . Resources . FirstOrDefault ( it => it is EmbeddedResource { Name : "configSort.yaml.compressed" } or EmbeddedResource { Name : "configSort.yaml" } ) ;
@@ -108,7 +42,8 @@ public AquaMaiConfigDto.ConfigDto GetAquaMaiConfig(bool forceDefault = false, bo
10842 configSort = deserializer . Deserialize < Dictionary < string , string [ ] > > ( yaml ) ;
10943 }
11044 }
111- var config = GetCurrentAquaMaiConfig ( forceDefault , skipSignatureCheck ) ;
45+ var shouldSkipSignatureCheck = skipSignatureCheck || ! string . Equals ( dllPath , ModPaths . AquaMaiDllInstalledPath , StringComparison . OrdinalIgnoreCase ) ;
46+ var config = await modConfigService . GetCurrentAquaMaiConfig ( forceDefault , shouldSkipSignatureCheck ) ;
11247 return new AquaMaiConfigDto . ConfigDto (
11348 config . ReflectionManager . Sections . Select ( section =>
11449 {
@@ -124,11 +59,12 @@ public AquaMaiConfigDto.ConfigDto GetAquaMaiConfig(bool forceDefault = false, bo
12459 [ HttpPut ]
12560 public async Task SetAquaMaiConfig ( AquaMaiConfigDto . ConfigSaveDto config )
12661 {
62+ var dllPath = await modConfigService . GetAquaMaiDllPath ( ) ;
12763 var jsonOptions = new JsonSerializerOptions ( ) ;
12864 jsonOptions . Converters . Add ( new JsonStringEnumConverter ( ) ) ;
12965
130- var configInterface = HeadlessConfigLoader . LoadFromPacked ( ModPaths . AquaMaiDllInstalledPath ) ;
131- CheckConfigApiVersion ( configInterface ) ;
66+ var configInterface = HeadlessConfigLoader . LoadFromPacked ( dllPath ) ;
67+ modConfigService . CheckConfigApiVersion ( configInterface ) ;
13268 var configEdit = configInterface . CreateConfig ( ) ;
13369
13470 foreach ( var section in configEdit . ReflectionManager . Sections )
@@ -183,4 +119,4 @@ public async Task SetAquaMaiConfig(AquaMaiConfigDto.ConfigSaveDto config)
183119 // 可能修改了歌曲封面目录
184120 settings . ScanMusicList ( ) ;
185121 }
186- }
122+ }
0 commit comments