SavourySnaX.InstanceTrampoline
8.0.4
dotnet add package SavourySnaX.InstanceTrampoline --version 8.0.4
NuGet\Install-Package SavourySnaX.InstanceTrampoline -Version 8.0.4
<PackageReference Include="SavourySnaX.InstanceTrampoline" Version="8.0.4" />
paket add SavourySnaX.InstanceTrampoline --version 8.0.4
#r "nuget: SavourySnaX.InstanceTrampoline, 8.0.4"
// Install SavourySnaX.InstanceTrampoline as a Cake Addin #addin nuget:?package=SavourySnaX.InstanceTrampoline&version=8.0.4 // Install SavourySnaX.InstanceTrampoline as a Cake Tool #tool nuget:?package=SavourySnaX.InstanceTrampoline&version=8.0.4
Instance Trampoline
A simple library which allows wrapping native calls that could otherwise be tricky in C#.
Primarily developed to allow usage of LibRetro endpoints from C#.
usage
Here is how I use it to wrap the raylib audio callback, which in the c# library provides a function pointer, without an instance parameter (which would make it tricky to play multiple streams from C#). The sample is incomplete, and just provides enough information to show how a 2 parameter function pointer, gets modified with a hidden parameter.
/*
RayLib CS audio callbacks have no way to track an instance (for multiple games for instance).
This class wraps the audio system and along with a trampoline dll, allows multiple instances to be tracked.
*/
internal class RayLibAudioHelper
{
struct AudioShared
{
public nint audioBuffer;
}
private unsafe delegate* unmanaged[Cdecl]<void*, void*, uint, void> audioCallback;
private unsafe delegate* unmanaged[Cdecl]<void*, uint, void> audioCallbackTrampoline;
private nint trampoline;
private nint audioSharedData;
public RayLibAudioHelper()
{
audioSharedData = Marshal.AllocHGlobal(Marshal.SizeOf<AudioShared>());
unsafe
{
audioCallback = &RayLibAudioCallback;
trampoline = InstanceTrampoline.InterfaceTrampoline.AllocateTrampoline(audioSharedData, 2, (nint)audioCallback);
audioCallbackTrampoline = (delegate* unmanaged[Cdecl]<void*,uint, void>)trampoline;
}
}
[UnmanagedCallersOnly(CallConvs = new Type[] { typeof(CallConvCdecl) })]
private unsafe static void RayLibAudioCallback(void* instance, void* ptr, uint size)
{
var audioShared = (AudioShared*)instance;
//.....
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.