SavourySnaX.InstanceTrampoline 8.0.2

dotnet add package SavourySnaX.InstanceTrampoline --version 8.0.2
NuGet\Install-Package SavourySnaX.InstanceTrampoline -Version 8.0.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SavourySnaX.InstanceTrampoline" Version="8.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SavourySnaX.InstanceTrampoline --version 8.0.2
#r "nuget: SavourySnaX.InstanceTrampoline, 8.0.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SavourySnaX.InstanceTrampoline as a Cake Addin
#addin nuget:?package=SavourySnaX.InstanceTrampoline&version=8.0.2

// Install SavourySnaX.InstanceTrampoline as a Cake Tool
#tool nuget:?package=SavourySnaX.InstanceTrampoline&version=8.0.2

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • 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.

Version Downloads Last updated
8.0.2 126 4/4/2024
8.0.1 94 2/23/2024
8.0.0 92 2/23/2024