11using CollapseLauncher . Helper ;
2+ using CollapseLauncher . Helper . FFmpegPInvoke ;
23using CollapseLauncher . Helper . StreamUtility ;
34using FFmpegInteropX ;
45using Hi3Helper ;
78using System . Collections ;
89using System . Diagnostics . CodeAnalysis ;
910using System . IO ;
11+ using System . Linq ;
1012using System . Runtime . CompilerServices ;
1113// ReSharper disable StringLiteralTypo
1214// ReSharper disable IdentifierTypo
1618
1719namespace CollapseLauncher . GameManagement . ImageBackground ;
1820
19- #region File Exclusive Fields
20- file static class Fields
21- {
22- public const string DllNameAvcodec = "avcodec-61.dll" ;
23- public const string DllNameAvdevice = "avdevice-61.dll" ;
24- public const string DllNameAvfilter = "avfilter-10.dll" ;
25- public const string DllNameAvformat = "avformat-61.dll" ;
26- public const string DllNameAvutil = "avutil-59.dll" ;
27- public const string DllNamePostproc = "postproc-58.dll" ;
28- public const string DllNameSwresample = "swresample-5.dll" ;
29- public const string DllNameSwscale = "swscale-8.dll" ;
30- }
31- #endregion
32-
3321public partial class ImageBackgroundManager
3422{
3523 #region Shared/Static Properties and Fields
3624
3725 private const string GlobalIsUseFFmpegConfigKey = "GlobalIsUseFFmpeg" ;
26+ private const string GlobalFFmpegVersionToUseConfigKey = "GlobalFFmpegVersionToUse" ;
3827 private const string GlobalFFmpegCustomPathConfigKey = "GlobalFFmpegCustomPath" ;
3928 private const string GlobalFFmpegDecodingModeConfigKey = "GlobalFFmpegDecodingMode" ;
4029
4130 public VideoDecoderMode [ ] AvailableFFmpegDecodingModes => field ??= Enum . GetValues < VideoDecoderMode > ( ) ;
4231
32+ public int GlobalFFmpegVersionToUse
33+ {
34+ get
35+ {
36+ int version = LauncherConfig . GetAppConfigValue ( GlobalFFmpegVersionToUseConfigKey ) ;
37+ return FFmpegPInvoke . FFmpegVersionLibNames . ContainsKey ( version ) ?
38+ version :
39+ FFmpegPInvoke . FFmpegVersionLibNames . Keys . FirstOrDefault ( ) ;
40+ }
41+ set
42+ {
43+ if ( ! FFmpegPInvoke . FFmpegVersionLibNames . ContainsKey ( value ) )
44+ {
45+ return ;
46+ }
47+
48+ LauncherConfig . SetAndSaveConfigValue ( GlobalFFmpegVersionToUseConfigKey , value ) ;
49+ OnPropertyChanged ( ) ;
50+ OnPropertyChanged ( nameof ( GlobalFFmpegLibraryNames ) ) ; // Notify FFmpeg library names update too.
51+ }
52+ }
53+
54+ public FFmpegPInvoke . FFmpegLibraryNames GlobalFFmpegLibraryNames
55+ {
56+ get
57+ {
58+ if ( FFmpegPInvoke . FFmpegVersionLibNames . TryGetValue ( GlobalFFmpegVersionToUse , out var names ) )
59+ {
60+ return names ;
61+ }
62+
63+ return FFmpegPInvoke . FFmpegVersionLibNames . Values . FirstOrDefault ( ) ;
64+ }
65+ }
66+
4367 public bool GlobalIsUseFFmpeg
4468 {
4569 get => LauncherConfig . GetAppConfigValue ( GlobalIsUseFFmpegConfigKey ) ;
@@ -60,7 +84,7 @@ public string? GlobalCustomFFmpegPath
6084 }
6185 }
6286
63- public bool GlobalIsFFmpegAvailable => IsFFmpegAvailable ( Directory . GetCurrentDirectory ( ) , out _ ) ;
87+ public bool GlobalIsFFmpegAvailable => IsFFmpegAvailable ( Directory . GetCurrentDirectory ( ) , GlobalFFmpegLibraryNames , out _ ) ;
6488
6589 public bool GlobalIsFFmpegCurrentlyUsed
6690 {
@@ -86,7 +110,7 @@ public VideoDecoderMode GlobalFFmpegDecodingMode
86110 }
87111 set
88112 {
89- if ( ! Enum . IsDefined < VideoDecoderMode > ( value ) )
113+ if ( ! Enum . IsDefined ( value ) )
90114 {
91115 value = default ;
92116 }
@@ -118,8 +142,10 @@ public bool TryRelinkFFmpegPath()
118142 return false ;
119143 }
120144
145+ var names = GlobalFFmpegLibraryNames ;
146+
121147 string curDir = Directory . GetCurrentDirectory ( ) ;
122- bool isFFmpegAvailable = IsFFmpegAvailable ( curDir , out exception ) ;
148+ bool isFFmpegAvailable = IsFFmpegAvailable ( curDir , names , out exception ) ;
123149 string ? customFFmpegDirPath = GlobalCustomFFmpegPath ;
124150
125151 if ( isFFmpegAvailable )
@@ -129,16 +155,16 @@ public bool TryRelinkFFmpegPath()
129155
130156 // -- 1. Check from custom path first. If it exists, then pass.
131157 if ( ! string . IsNullOrEmpty ( customFFmpegDirPath ) &&
132- IsFFmpegAvailable ( customFFmpegDirPath , out exception ) &&
133- TryLinkFFmpegLibrary ( customFFmpegDirPath , curDir , out exception ) )
158+ IsFFmpegAvailable ( customFFmpegDirPath , names , out exception ) &&
159+ TryLinkFFmpegLibrary ( customFFmpegDirPath , curDir , names , out exception ) )
134160 {
135161 return result = true ;
136162 }
137163
138164 // -- 2. Find one from environment variables. If it exists, then pass.
139165 // Otherwise, return false.
140- return result = TryFindFFmpegInstallFromEnvVar ( out string ? envVarPath , out exception ) &&
141- TryLinkFFmpegLibrary ( envVarPath , curDir , out exception ) ;
166+ return result = TryFindFFmpegInstallFromEnvVar ( names , out string ? envVarPath , out exception ) &&
167+ TryLinkFFmpegLibrary ( envVarPath , curDir , names , out exception ) ;
142168 }
143169 finally
144170 {
@@ -151,7 +177,7 @@ public bool TryRelinkFFmpegPath()
151177 }
152178 }
153179
154- internal bool TryFindFFmpegInstallFromEnvVar ( [ NotNullWhen ( true ) ] out string ? path , out Exception ? exception )
180+ internal bool TryFindFFmpegInstallFromEnvVar ( FFmpegPInvoke . FFmpegLibraryNames libraries , [ NotNullWhen ( true ) ] out string ? path , out Exception ? exception )
155181 {
156182 return FindIn ( EnvironmentVariableTarget . User , out path , out exception ) ||
157183 FindIn ( EnvironmentVariableTarget . Machine , out path , out exception ) ;
@@ -181,7 +207,7 @@ bool FindIn(EnvironmentVariableTarget target, [NotNullWhen(true)] out string? in
181207 string thisPath = envVarPath . ToString ( ) ;
182208
183209 if ( ! Path . IsPathFullyQualified ( thisPath ) ||
184- ! IsFFmpegAvailable ( thisPath , out exception ) ) continue ;
210+ ! IsFFmpegAvailable ( thisPath , libraries , out exception ) ) continue ;
185211
186212 innerPath = thisPath ;
187213 GlobalCustomFFmpegPath = thisPath ; // Set as custom path
@@ -194,6 +220,7 @@ bool FindIn(EnvironmentVariableTarget target, [NotNullWhen(true)] out string? in
194220 }
195221
196222 internal static bool IsFFmpegAvailable ( string ? checkOnDirectory ,
223+ FFmpegPInvoke . FFmpegLibraryNames libraries ,
197224 [ NotNullWhen ( false ) ]
198225 out Exception ? exception )
199226 {
@@ -205,13 +232,13 @@ internal static bool IsFFmpegAvailable(string? checkOnDirectory,
205232
206233 checkOnDirectory = FileUtility . GetFullyQualifiedPath ( checkOnDirectory ) ;
207234
208- string dllPathAvcodec = Path . Combine ( checkOnDirectory , Fields . DllNameAvcodec ) ;
209- string dllPathAvdevice = Path . Combine ( checkOnDirectory , Fields . DllNameAvdevice ) ;
210- string dllPathAvfilter = Path . Combine ( checkOnDirectory , Fields . DllNameAvfilter ) ;
211- string dllPathAvformat = Path . Combine ( checkOnDirectory , Fields . DllNameAvformat ) ;
212- string dllPathAvutil = Path . Combine ( checkOnDirectory , Fields . DllNameAvutil ) ;
213- string dllPathSwresample = Path . Combine ( checkOnDirectory , Fields . DllNameSwresample ) ;
214- string dllPathSwscale = Path . Combine ( checkOnDirectory , Fields . DllNameSwscale ) ;
235+ string dllPathAvcodec = Path . Combine ( checkOnDirectory , libraries . Codec ) ;
236+ string dllPathAvdevice = Path . Combine ( checkOnDirectory , libraries . Device ) ;
237+ string dllPathAvfilter = Path . Combine ( checkOnDirectory , libraries . Filter ) ;
238+ string dllPathAvformat = Path . Combine ( checkOnDirectory , libraries . Format ) ;
239+ string dllPathAvutil = Path . Combine ( checkOnDirectory , libraries . Util ) ;
240+ string dllPathSwresample = Path . Combine ( checkOnDirectory , libraries . Resample ) ;
241+ string dllPathSwscale = Path . Combine ( checkOnDirectory , libraries . Scale ) ;
215242
216243 return FileUtility . IsFileExistOrSymbolicLinkResolved ( dllPathAvcodec , out _ , out exception ) &&
217244 FileUtility . IsFileExistOrSymbolicLinkResolved ( dllPathAvdevice , out _ , out exception ) &&
@@ -222,29 +249,32 @@ internal static bool IsFFmpegAvailable(string? checkOnDirectory,
222249 FileUtility . IsFileExistOrSymbolicLinkResolved ( dllPathSwscale , out _ , out exception ) ;
223250 }
224251
225- internal static string [ ] GetFFmpegRequiredDllFilenames ( ) =>
226- [
227- Fields . DllNameAvcodec ,
228- Fields . DllNameAvdevice ,
229- Fields . DllNameAvfilter ,
230- Fields . DllNameAvformat ,
231- Fields . DllNameAvutil ,
232- Fields . DllNameSwresample ,
233- Fields . DllNameSwscale
234- ] ;
235-
236- internal static string ? FindFFmpegInstallFolder ( string checkOnDirectory )
252+ internal static string [ ] GetFFmpegRequiredDllFilenames ( )
253+ {
254+ var names = Shared . GlobalFFmpegLibraryNames ;
255+ return [
256+ names . Codec ,
257+ names . Device ,
258+ names . Filter ,
259+ names . Format ,
260+ names . Util ,
261+ names . Resample ,
262+ names . Scale
263+ ] ;
264+ }
265+
266+ internal static string ? FindFFmpegInstallFolder ( string checkOnDirectory , FFmpegPInvoke . FFmpegLibraryNames libraries )
237267 {
238268 try
239269 {
240- if ( IsFFmpegAvailable ( checkOnDirectory , out _ ) )
270+ if ( IsFFmpegAvailable ( checkOnDirectory , libraries , out _ ) )
241271 {
242272 return checkOnDirectory ;
243273 }
244274
245275 foreach ( string dirPath in FileUtility . EnumerateDirectoryRecursive ( checkOnDirectory ) )
246276 {
247- if ( IsFFmpegAvailable ( dirPath , out _ ) )
277+ if ( IsFFmpegAvailable ( dirPath , libraries , out _ ) )
248278 {
249279 return dirPath ;
250280 }
@@ -261,6 +291,7 @@ internal static string[] GetFFmpegRequiredDllFilenames() =>
261291 public static bool TryLinkFFmpegLibrary (
262292 string ? sourceDir ,
263293 string ? targetDir ,
294+ FFmpegPInvoke . FFmpegLibraryNames libraries ,
264295 [ NotNullWhen ( false ) ]
265296 out Exception ? exception )
266297 {
@@ -280,14 +311,14 @@ public static bool TryLinkFFmpegLibrary(
280311 return false ;
281312 }
282313
283- string dllPathAvcodec = Path . Combine ( sourceDir , Fields . DllNameAvcodec ) ;
284- string dllPathAvdevice = Path . Combine ( sourceDir , Fields . DllNameAvdevice ) ;
285- string dllPathAvfilter = Path . Combine ( sourceDir , Fields . DllNameAvfilter ) ;
286- string dllPathAvformat = Path . Combine ( sourceDir , Fields . DllNameAvformat ) ;
287- string dllPathAvutil = Path . Combine ( sourceDir , Fields . DllNameAvutil ) ;
288- string dllPathPostproc = Path . Combine ( sourceDir , Fields . DllNamePostproc ) ;
289- string dllPathSwresample = Path . Combine ( sourceDir , Fields . DllNameSwresample ) ;
290- string dllPathSwscale = Path . Combine ( sourceDir , Fields . DllNameSwscale ) ;
314+ string dllPathAvcodec = Path . Combine ( sourceDir , libraries . Codec ) ;
315+ string dllPathAvdevice = Path . Combine ( sourceDir , libraries . Device ) ;
316+ string dllPathAvfilter = Path . Combine ( sourceDir , libraries . Filter ) ;
317+ string dllPathAvformat = Path . Combine ( sourceDir , libraries . Format ) ;
318+ string dllPathAvutil = Path . Combine ( sourceDir , libraries . Util ) ;
319+ string dllPathPostproc = Path . Combine ( sourceDir , libraries . PostProc ) ;
320+ string dllPathSwresample = Path . Combine ( sourceDir , libraries . Resample ) ;
321+ string dllPathSwscale = Path . Combine ( sourceDir , libraries . Scale ) ;
291322
292323 bool result =
293324 CreateSymbolLink ( dllPathAvcodec , targetDir , out exception ) &&
0 commit comments