Skip to content

Commit 6c046f7

Browse files
committed
Merge branch 'main' of github.com:CollapseLauncher/Collapse
2 parents 72b4ec9 + 3917e0f commit 6c046f7

27 files changed

Lines changed: 712 additions & 316 deletions

CollapseLauncher/Classes/GameManagement/ImageBackground/ImageBackgroundManager.FFmpeg.cs

Lines changed: 83 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using CollapseLauncher.Helper;
2+
using CollapseLauncher.Helper.FFmpegPInvoke;
23
using CollapseLauncher.Helper.StreamUtility;
34
using FFmpegInteropX;
45
using Hi3Helper;
@@ -7,6 +8,7 @@
78
using System.Collections;
89
using System.Diagnostics.CodeAnalysis;
910
using System.IO;
11+
using System.Linq;
1012
using System.Runtime.CompilerServices;
1113
// ReSharper disable StringLiteralTypo
1214
// ReSharper disable IdentifierTypo
@@ -16,30 +18,52 @@
1618

1719
namespace 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-
3321
public 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) &&

CollapseLauncher/Classes/Helper/FFmpegCodecInstaller.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ await extractDelegate(() => File.Open(packageFile,
113113
extractFolder,
114114
token);
115115

116-
string innerFfmpegDir = ImageBackgroundManager.FindFFmpegInstallFolder(extractFolder) ??
116+
string innerFfmpegDir = ImageBackgroundManager.FindFFmpegInstallFolder(extractFolder, ImageBackgroundManager.Shared.GlobalFFmpegLibraryNames) ??
117117
throw new FileNotFoundException("Library files are not found!");
118118

119119
foreach (string dllFile in Directory.EnumerateFiles(innerFfmpegDir, "*.dll", SearchOption.TopDirectoryOnly))

CollapseLauncher/Classes/Helper/Image/Waifu2X.cs

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using Hi3Helper;
22
using Hi3Helper.SentryHelper;
3-
using Hi3Helper.Shared.Region;
43
using System;
54
using System.IO;
6-
using System.Reflection;
75
using System.Runtime.CompilerServices;
86
using System.Runtime.InteropServices;
97
using static CollapseLauncher.Helper.Image.Waifu2X;
@@ -21,41 +19,6 @@ internal static partial class Waifu2XPInvoke
2119
{
2220
private const string DllName = "Lib\\waifu2x-ncnn-vulkan.dll";
2321

24-
#nullable enable
25-
private static string? appDirPath;
26-
private static string? waifu2xLibPath;
27-
#nullable restore
28-
29-
static Waifu2XPInvoke()
30-
{
31-
// Use custom Dll import resolver
32-
NativeLibrary.SetDllImportResolver(Assembly.GetExecutingAssembly(), DllImportResolver);
33-
}
34-
35-
private static IntPtr DllImportResolver(string libraryName, Assembly assembly, DllImportSearchPath? searchPath)
36-
{
37-
appDirPath ??= LauncherConfig.AppExecutableDir;
38-
39-
if (DllImportSearchPath.AssemblyDirectory != searchPath
40-
&& DllImportSearchPath.ApplicationDirectory != searchPath)
41-
{
42-
return LoadInternal(libraryName, assembly, searchPath);
43-
}
44-
45-
waifu2xLibPath ??= Path.Combine(appDirPath, DllName);
46-
return LoadInternal(waifu2xLibPath, assembly, null);
47-
48-
}
49-
50-
private static IntPtr LoadInternal(string path, Assembly assembly, DllImportSearchPath? searchPath)
51-
{
52-
bool isLoadSuccessful = NativeLibrary.TryLoad(path, assembly, null, out IntPtr pResult);
53-
if (!isLoadSuccessful || pResult == IntPtr.Zero)
54-
throw new FileLoadException($"Failed while loading library from this path: {path} with Search Path: {searchPath}\r\nMake sure that the library/.dll is a valid Win32 library and not corrupted!");
55-
56-
return pResult;
57-
}
58-
5922
#region ncnn PInvokes
6023
[LibraryImport(DllName)]
6124
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)]

0 commit comments

Comments
 (0)