mirror of
https://github.com/Facepunch/sbox-public.git
synced 2026-01-01 10:58:29 -05:00
This commit imports the C# engine code and game files, excluding C++ source code. [Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
48 lines
1.2 KiB
C#
48 lines
1.2 KiB
C#
using System.Runtime.InteropServices;
|
|
|
|
#nullable enable
|
|
|
|
namespace Sandbox;
|
|
|
|
/// <summary>
|
|
/// Helper to wrap <see cref="Marshal.GetFunctionPointerForDelegate"/> while keeping
|
|
/// a reference to the original delegate, so it won't be garbage collected. <see cref="Dispose"/>
|
|
/// must be called to remove the reference.
|
|
/// </summary>
|
|
internal struct DelegateFunctionPointer : IDisposable
|
|
{
|
|
public static DelegateFunctionPointer Null => default;
|
|
|
|
private nint _ptr;
|
|
private Delegate _handle;
|
|
|
|
/// <summary>
|
|
/// Gets the raw function pointer.
|
|
/// </summary>
|
|
public static implicit operator nint( DelegateFunctionPointer fp )
|
|
{
|
|
return fp._ptr;
|
|
}
|
|
|
|
/// <inheritdoc cref="DelegateFunctionPointer"/>
|
|
public static DelegateFunctionPointer Get<T>( T deleg ) where T : Delegate
|
|
{
|
|
return new DelegateFunctionPointer( Marshal.GetFunctionPointerForDelegate( deleg ), deleg );
|
|
}
|
|
|
|
private DelegateFunctionPointer( nint ptr, Delegate handle )
|
|
{
|
|
_ptr = ptr;
|
|
_handle = handle;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Removes the reference to the original delegate, and sets the function pointer to null.
|
|
/// </summary>
|
|
public void Dispose()
|
|
{
|
|
_ptr = nint.Zero;
|
|
_handle = () => { };
|
|
}
|
|
}
|