-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNativeDllLoader.cs
More file actions
101 lines (94 loc) · 3.43 KB
/
NativeDllLoader.cs
File metadata and controls
101 lines (94 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System;
using System.IO;
using System.Reflection;
using RGB.NET.Devices.Corsair;
using RGB.NET.Devices.Logitech;
using RGB.NET.Devices.Msi;
using RGB.NET.Devices.Wooting;
public static class NativeDllLoader
{
public static void LoadNativeSDKs()
{
try
{
Console.WriteLine("[Loader] All Embedded Resources:");
var assembly = Assembly.GetExecutingAssembly();
foreach (var name in assembly.GetManifestResourceNames())
{
Console.WriteLine($"[Loader] -> Found resource: {name}");
}
string tempPath = Path.Combine(Path.GetTempPath(), "MineLights", "NativeLibs");
Console.WriteLine($"[Loader] Using temporary path for native libs: {tempPath}");
Directory.CreateDirectory(tempPath);
LoadSdk(
"Corsair",
"MineLights.resources.x64.iCUESDK.x64_2019.dll",
"iCUESDK.x64_2019.dll",
tempPath,
CorsairDeviceProvider.PossibleX64NativePaths
);
LoadSdk(
"MSI",
"MineLights.resources.x64.MysticLight_SDK_x64.dll",
"MysticLight_SDK_x64.dll",
tempPath,
MsiDeviceProvider.PossibleX64NativePaths
);
LoadSdk(
"Logitech",
"MineLights.resources.x64.LogitechLedEnginesWrapper.dll",
"LogitechLedEnginesWrapper.dll",
tempPath,
LogitechDeviceProvider.PossibleX64NativePaths
);
LoadSdk(
"Wooting",
"MineLights.resources.x64.wooting-rgb-sdk.dll",
"wooting-rgb-sdk.dll",
tempPath,
WootingDeviceProvider.PossibleX64NativePathsWindows
);
}
catch (Exception ex)
{
Console.WriteLine($"[Loader] FATAL ERROR: {ex.Message}");
MessageBox.Show(
$"A critical error occurred while loading native libraries:\n\n{ex.Message}\n\nThe application will now exit.",
"MineLights Server Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error
);
Environment.Exit(1);
}
}
private static void LoadSdk(
string name,
string resourceName,
string outputFileName,
string tempPath,
ICollection<string> possiblePaths
)
{
Console.WriteLine($"[Loader] Preparing {name} SDK...");
string dllPath = Path.Combine(tempPath, outputFileName);
ExtractResource(resourceName, dllPath);
possiblePaths.Add(dllPath);
Console.WriteLine($"[Loader] {name} SDK path configured: {dllPath}");
}
private static void ExtractResource(string resourceName, string outputPath)
{
if (File.Exists(outputPath))
return;
Assembly assembly = Assembly.GetExecutingAssembly();
using (Stream? stream = assembly.GetManifestResourceStream(resourceName))
{
if (stream == null)
throw new FileNotFoundException($"Cannot find embedded resource: {resourceName}.");
using (FileStream fileStream = new FileStream(outputPath, FileMode.Create))
{
stream.CopyTo(fileStream);
Console.WriteLine($"[Loader] -> Extracted '{Path.GetFileName(outputPath)}'.");
}
}
}
}