using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
namespace StgSharp.Graphic.GL
{
///
/// Provides a common foundation for all flat API bindings and implements the extension loading interface.
///
public abstract class BindingsBase
{
///
/// Constructs a new BindingsBase instance.
///
public BindingsBase()
{
}
///
/// Marshals a pointer to a null-terminated byte array to a new System.String.
/// This method supports OpenTK and is not intended to be called by user code.
///
/// A pointer to a null-terminated byte array.
///
/// A System.String with the data from .
///
protected static string MarshalPtrToString(IntPtr ptr)
{
if (ptr == IntPtr.Zero)
{
throw new ArgumentException("ptr");
}
unsafe
{
sbyte* str = (sbyte*)ptr;
int len = 0;
while (*str != 0)
{
++len;
++str;
}
return new string((sbyte*)ptr, 0, len, Encoding.UTF8);
}
}
///
/// Marshal a System.String to unmanaged memory.
/// The resulting string is encoded in UTF8 and must be freed
/// with FreeStringPtr.
///
/// The System.String to marshal.
///
/// An unmanaged pointer containing the marshalled string.
/// This pointer must be freed with FreeStringPtr
///
protected static IntPtr MarshalStringToPtr(string str)
{
if (string.IsNullOrEmpty(str))
{
return IntPtr.Zero;
}
// Allocate a buffer big enough to hold the marshalled string.
// GetMaxByteCount() appears to allocate space for the final NUL
// character, but allocate an extra one just in case (who knows
// what old Mono version would do here.)
int max_count = Encoding.UTF8.GetMaxByteCount(str.Length) + 1;
IntPtr ptr = Marshal.AllocHGlobal(max_count);
if (ptr == IntPtr.Zero)
{
throw new OutOfMemoryException();
}
// Pin the managed string and convert it to UTF8 using
// the pointer overload of System.Encoding.UTF8.GetBytes().
unsafe
{
fixed (char* pstr = str)
{
int actual_count = Encoding.UTF8.GetBytes(pstr, str.Length, (byte*)ptr, max_count);
Marshal.WriteByte(ptr, actual_count, 0); // Append '\0' at the end of the string
return ptr;
}
}
}
///
/// Frees a marshalled string that allocated by MarshalStringToPtr.
///
/// An unmanaged pointer allocated with MarshalStringToPtr
protected static void FreeStringPtr(IntPtr ptr)
{
Marshal.FreeHGlobal(ptr);
}
///
/// Marshals a System.String array to unmanaged memory by calling
/// Marshal.AllocHGlobal for each element.
///
/// An unmanaged pointer to an array of null-terminated strings
/// The string array to marshal.
protected static IntPtr MarshalStringArrayToPtr(string[] str_array)
{
IntPtr ptr = IntPtr.Zero;
if (str_array != null && str_array.Length != 0)
{
ptr = Marshal.AllocHGlobal(str_array.Length * IntPtr.Size);
if (ptr == IntPtr.Zero)
{
throw new OutOfMemoryException();
}
int i = 0;
try
{
for (i = 0; i < str_array.Length; i++)
{
IntPtr str = MarshalStringToPtr(str_array[i]);
Marshal.WriteIntPtr(ptr, i * IntPtr.Size, str);
}
}
catch (OutOfMemoryException)
{
for (i = i - 1; i >= 0; --i)
{
Marshal.FreeHGlobal(Marshal.ReadIntPtr(ptr, i * IntPtr.Size));
}
Marshal.FreeHGlobal(ptr);
throw;
}
}
return ptr;
}
///
/// Frees a marshalled string that allocated by MarshalStringArrayToPtr.
///
/// An unmanaged pointer allocated with MarshalStringArrayToPtr
/// The length of the string array.
protected static void FreeStringArrayPtr(IntPtr ptr, int length)
{
for (int i = 0; i < length; i++)
{
Marshal.FreeHGlobal(Marshal.ReadIntPtr(ptr, i * IntPtr.Size));
}
Marshal.FreeHGlobal(ptr);
}
private protected static void InitializeDummyEntryPoints(IntPtr[] entryPoints)
{
var ptr = Marshal.GetFunctionPointerForDelegate(UninitializedDelegate);
for (var i = 0; i < entryPoints.Length; i++)
{
entryPoints[i] = ptr;
}
}
private static readonly Action UninitializedDelegate = Uninitialized;
private static void Uninitialized()
{
throw new InvalidOperationException("You need to initialize the OpenGL binding first by calling LoadBindings() or creating a compatible OpenGL window.");
}
}
}