@@ -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 )
0 commit comments