forked from flavius-st/TheAdventure
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSdlContext.cs
More file actions
96 lines (83 loc) · 2.78 KB
/
Copy pathSdlContext.cs
File metadata and controls
96 lines (83 loc) · 2.78 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
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime.InteropServices;
using Silk.NET.Core.Contexts;
namespace TheAdventure;
public class SdlContext : INativeContext
{
private readonly IntPtr _nativeLibrary;
public SdlContext()
{
string runtimesPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "runtimes");
string libraryName;
string platform;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
libraryName = "libSDL2-2.0.so";
platform = "linux";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
libraryName = "SDL2.dll";
platform = "win";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
_nativeLibrary = NativeLibrary.Load(Path.Combine(runtimesPath, "osx", "native", "libSDL2-2.0.dylib"));
return;
}
else
{
throw new PlatformNotSupportedException("Only Linux, macOS, and Windows are supported.");
}
if (RuntimeInformation.OSArchitecture == Architecture.X64)
{
_nativeLibrary = NativeLibrary.Load(Path.Combine(runtimesPath, platform + "-x64", "native", libraryName));
}
else if (RuntimeInformation.OSArchitecture == Architecture.X86)
{
_nativeLibrary = NativeLibrary.Load(Path.Combine(runtimesPath, platform + "-x86", "native", libraryName));
}
else if (RuntimeInformation.OSArchitecture == Architecture.Arm64)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
throw new PlatformNotSupportedException("ARM64 is not supported on Linux.");
}
_nativeLibrary = NativeLibrary.Load(Path.Combine(runtimesPath, platform + "-arm", "native", libraryName));
}
else
{
throw new PlatformNotSupportedException("Only x64, x86, and ARM64 are supported.");
}
}
public IntPtr GetProcAddress(string proc, int? slot = null)
{
return NativeLibrary.GetExport(_nativeLibrary, proc);
}
public bool TryGetProcAddress(string proc, [UnscopedRef] out IntPtr addr, int? slot = null)
{
try
{
addr = NativeLibrary.GetExport(_nativeLibrary, proc);
}
catch (EntryPointNotFoundException)
{
addr = IntPtr.Zero;
}
return addr != IntPtr.Zero;
}
private void ReleaseUnmanagedResources()
{
NativeLibrary.Free(_nativeLibrary);
}
public void Dispose()
{
ReleaseUnmanagedResources();
GC.SuppressFinalize(this);
}
~SdlContext()
{
ReleaseUnmanagedResources();
}
}