Replace LPUtf8StrMarshaler with internal UTF8 API

pull/119/head
Ethan Lee 2017-10-19 12:30:01 -04:00
parent 5785d3d464
commit 795db4433d
8 changed files with 896 additions and 420 deletions

View File

@ -3,7 +3,6 @@
# Source Lists
SRC = \
src/LPUtf8StrMarshaler.cs \
src/SDL2.cs \
src/SDL2_image.cs \
src/SDL2_mixer.cs \

View File

@ -15,7 +15,6 @@
<Compile Include="src\SDL2_image.cs" />
<Compile Include="src\SDL2_mixer.cs" />
<Compile Include="src\SDL2_ttf.cs" />
<Compile Include="src\LPUtf8StrMarshaler.cs" />
</ItemGroup>
<ItemGroup>
<None Include="SDL2-CS.dll.config">

View File

@ -80,7 +80,6 @@
<Compile Include="src\SDL2_image.cs" />
<Compile Include="src\SDL2_mixer.cs" />
<Compile Include="src\SDL2_ttf.cs" />
<Compile Include="src\LPUtf8StrMarshaler.cs" />
</ItemGroup>
<ItemGroup>
<None Include="SDL2-CS.dll.config">

View File

@ -1,106 +0,0 @@
/* SDL2# - C# Wrapper for SDL2
*
* Copyright (c) 2013-2016 Ethan Lee.
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from
* the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software in a
* product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*
* Ethan "flibitijibibo" Lee <flibitijibibo@flibitijibibo.com>
*
*/
using System;
using System.Text;
using System.Runtime.InteropServices;
namespace SDL2
{
internal unsafe class LPUtf8StrMarshaler : ICustomMarshaler
{
public const string LeaveAllocated = "LeaveAllocated";
private static ICustomMarshaler
_leaveAllocatedInstance = new LPUtf8StrMarshaler(true),
_defaultInstance = new LPUtf8StrMarshaler(false);
public static ICustomMarshaler GetInstance(string cookie)
{
switch (cookie)
{
case "LeaveAllocated":
return _leaveAllocatedInstance;
default:
return _defaultInstance;
}
}
private bool _leaveAllocated;
public LPUtf8StrMarshaler(bool leaveAllocated)
{
_leaveAllocated = leaveAllocated;
}
public object MarshalNativeToManaged(IntPtr pNativeData)
{
if (pNativeData == IntPtr.Zero)
return null;
var ptr = (byte*)pNativeData;
while (*ptr != 0)
{
ptr++;
}
var bytes = new byte[ptr - (byte*)pNativeData];
Marshal.Copy(pNativeData, bytes, 0, bytes.Length);
return Encoding.UTF8.GetString(bytes);
}
public IntPtr MarshalManagedToNative(object ManagedObj)
{
if (ManagedObj == null)
return IntPtr.Zero;
var str = ManagedObj as string;
if (str == null)
{
throw new ArgumentException("ManagedObj must be a string.", "ManagedObj");
}
var bytes = Encoding.UTF8.GetBytes(str);
var mem = SDL.SDL_malloc((IntPtr) (bytes.Length + 1));
Marshal.Copy(bytes, 0, mem, bytes.Length);
((byte*)mem)[bytes.Length] = 0;
return mem;
}
public void CleanUpManagedData(object ManagedObj)
{
}
public void CleanUpNativeData(IntPtr pNativeData)
{
if (!_leaveAllocated)
{
SDL.SDL_free(pNativeData);
}
}
public int GetNativeDataSize ()
{
return -1;
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -88,11 +88,14 @@ namespace SDL2
public static extern void IMG_Quit();
/* IntPtr refers to an SDL_Surface* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr IMG_Load(
[In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string file
[DllImport(nativeLibName, EntryPoint = "IMG_Load", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr INTERNAL_IMG_Load(
byte[] file
);
public static IntPtr IMG_Load(string file)
{
return INTERNAL_IMG_Load(SDL.UTF8_ToNative(file));
}
/* src refers to an SDL_RWops*, IntPtr to an SDL_Surface* */
/* THIS IS A PUBLIC RWops FUNCTION! */
@ -104,21 +107,39 @@ namespace SDL2
/* src refers to an SDL_RWops*, IntPtr to an SDL_Surface* */
/* THIS IS A PUBLIC RWops FUNCTION! */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr IMG_LoadTyped_RW(
[DllImport(nativeLibName, EntryPoint = "IMG_LoadTyped_RW", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr INTERNAL_IMG_LoadTyped_RW(
IntPtr src,
int freesrc,
[In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string type
byte[] type
);
public static IntPtr IMG_LoadTyped_RW(
IntPtr src,
int freesrc,
string type
) {
return INTERNAL_IMG_LoadTyped_RW(
src,
freesrc,
SDL.UTF8_ToNative(type)
);
}
/* IntPtr refers to an SDL_Texture*, renderer to an SDL_Renderer* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr IMG_LoadTexture(
[DllImport(nativeLibName, EntryPoint = "IMG_LoadTexture", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr INTERNAL_IMG_LoadTexture(
IntPtr renderer,
[In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string file
byte[] file
);
public static IntPtr IMG_LoadTexture(
IntPtr renderer,
string file
) {
return INTERNAL_IMG_LoadTexture(
renderer,
SDL.UTF8_ToNative(file)
);
}
/* renderer refers to an SDL_Renderer*.
* src refers to an SDL_RWops*.
@ -137,14 +158,26 @@ namespace SDL2
* IntPtr to an SDL_Texture*.
*/
/* THIS IS A PUBLIC RWops FUNCTION! */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr IMG_LoadTextureTyped_RW(
[DllImport(nativeLibName, EntryPoint = "IMG_LoadTextureTyped_RW", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr INTERNAL_IMG_LoadTextureTyped_RW(
IntPtr renderer,
IntPtr src,
int freesrc,
[In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string type
byte[] type
);
public static IntPtr IMG_LoadTextureTyped_RW(
IntPtr renderer,
IntPtr src,
int freesrc,
string type
) {
return INTERNAL_IMG_LoadTextureTyped_RW(
renderer,
src,
freesrc,
SDL.UTF8_ToNative(type)
);
}
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern int IMG_InvertAlpha(int on);
@ -157,12 +190,18 @@ namespace SDL2
);
/* surface refers to an SDL_Surface* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern int IMG_SavePNG(
[DllImport(nativeLibName, EntryPoint = "IMG_SavePNG", CallingConvention = CallingConvention.Cdecl)]
private static extern int INTERNAL_IMG_SavePNG(
IntPtr surface,
[In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string file
byte[] file
);
public static int IMG_SavePNG(IntPtr surface, string file)
{
return INTERNAL_IMG_SavePNG(
surface,
SDL.UTF8_ToNative(file)
);
}
/* surface refers to an SDL_Surface*, dst to an SDL_RWops* */
/* THIS IS A PUBLIC RWops FUNCTION! */

View File

@ -189,11 +189,14 @@ namespace SDL2
}
/* IntPtr refers to a Mix_Music* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr Mix_LoadMUS(
[In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string file
[DllImport(nativeLibName, EntryPoint = "Mix_LoadMUS", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr INTERNAL_Mix_LoadMUS(
byte[] file
);
public static IntPtr Mix_LoadMUS(string file)
{
return INTERNAL_Mix_LoadMUS(SDL.UTF8_ToNative(file));
}
/* IntPtr refers to a Mix_Chunk* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
@ -221,16 +224,26 @@ namespace SDL2
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern int Mix_GetNumChunkDecoders();
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
[return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler), MarshalCookie = LPUtf8StrMarshaler.LeaveAllocated)]
public static extern string Mix_GetChunkDecoder(int index);
[DllImport(nativeLibName, EntryPoint = "Mix_GetChunkDecoder", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr INTERNAL_Mix_GetChunkDecoder(int index);
public static string Mix_GetChunkDecoder(int index)
{
return SDL.UTF8_ToManaged(
INTERNAL_Mix_GetChunkDecoder(index)
);
}
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern int Mix_GetNumMusicDecoders();
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
[return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler), MarshalCookie = LPUtf8StrMarshaler.LeaveAllocated)]
public static extern string Mix_GetMusicDecoder(int index);
[DllImport(nativeLibName, EntryPoint = "Mix_GetMusicDecoder", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr INTERNAL_Mix_GetMusicDecoder(int index);
public static string Mix_GetMusicDecoder(int index)
{
return SDL.UTF8_ToManaged(
INTERNAL_Mix_GetMusicDecoder(index)
);
}
/* music refers to a Mix_Music* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
@ -449,11 +462,16 @@ namespace SDL2
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern int Mix_PlayingMusic();
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern int Mix_SetMusicCMD(
[In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string command
[DllImport(nativeLibName, EntryPoint = "Mix_SetMusicCMD", CallingConvention = CallingConvention.Cdecl)]
private static extern int INTERNAL_Mix_SetMusicCMD(
byte[] command
);
public static int Mix_SetMusicCMD(string command)
{
return INTERNAL_Mix_SetMusicCMD(
SDL.UTF8_ToNative(command)
);
}
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern int Mix_SetSynchroValue(int value);
@ -461,15 +479,23 @@ namespace SDL2
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern int Mix_GetSynchroValue();
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern int Mix_SetSoundFonts(
[In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string paths
[DllImport(nativeLibName, EntryPoint = "Mix_SetSoundFonts", CallingConvention = CallingConvention.Cdecl)]
private static extern int INTERNAL_Mix_SetSoundFonts(
byte[] paths
);
public static int Mix_SetSoundFonts(string paths)
{
return INTERNAL_Mix_SetSoundFonts(
SDL.UTF8_ToNative(paths)
);
}
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
[return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler), MarshalCookie = LPUtf8StrMarshaler.LeaveAllocated)]
public static extern string Mix_GetSoundFonts();
[DllImport(nativeLibName, EntryPoint = "Mix_GetSoundFonts", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr INTERNAL_Mix_GetSoundFonts();
public static string Mix_GetSoundFonts()
{
return SDL.UTF8_ToManaged(INTERNAL_Mix_GetSoundFonts());
}
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern int Mix_EachSoundFont(

View File

@ -93,12 +93,18 @@ namespace SDL2
public static extern int TTF_Init();
/* IntPtr refers to a TTF_Font* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr TTF_OpenFont(
[In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string file,
[DllImport(nativeLibName, EntryPoint = "TTF_OpenFont", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr INTERNAL_TTF_OpenFont(
byte[] file,
int ptsize
);
public static IntPtr TTF_OpenFont(string file, int ptsize)
{
return INTERNAL_TTF_OpenFont(
SDL.UTF8_ToNative(file),
ptsize
);
}
/* src refers to an SDL_RWops*, IntPtr to a TTF_Font* */
/* THIS IS A PUBLIC RWops FUNCTION! */
@ -110,13 +116,23 @@ namespace SDL2
);
/* IntPtr refers to a TTF_Font* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr TTF_OpenFontIndex(
[In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string file,
[DllImport(nativeLibName, EntryPoint = "TTF_OpenFontIndex", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr INTERNAL_TTF_OpenFontIndex(
byte[] file,
int ptsize,
long index
);
public static IntPtr TTF_OpenFontIndex(
string file,
int ptsize,
long index
) {
return INTERNAL_TTF_OpenFontIndex(
SDL.UTF8_ToNative(file),
ptsize,
index
);
}
/* src refers to an SDL_RWops*, IntPtr to a TTF_Font* */
/* THIS IS A PUBLIC RWops FUNCTION! */
@ -185,18 +201,28 @@ namespace SDL2
public static extern int TTF_FontFaceIsFixedWidth(IntPtr font);
/* font refers to a TTF_Font* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
[return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler), MarshalCookie = LPUtf8StrMarshaler.LeaveAllocated)]
public static extern string TTF_FontFaceFamilyName(
[DllImport(nativeLibName, EntryPoint = "TTF_FontFaceFamilyName", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr INTERNAL_TTF_FontFaceFamilyName(
IntPtr font
);
public static string TTF_FontFaceFamilyName(IntPtr font)
{
return SDL.UTF8_ToManaged(
INTERNAL_TTF_FontFaceFamilyName(font)
);
}
/* font refers to a TTF_Font* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
[return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler), MarshalCookie = LPUtf8StrMarshaler.LeaveAllocated)]
public static extern string TTF_FontFaceStyleName(
[DllImport(nativeLibName, EntryPoint = "TTF_FontFaceStyleName", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr INTERNAL_TTF_FontFaceStyleName(
IntPtr font
);
public static string TTF_FontFaceStyleName(IntPtr font)
{
return SDL.UTF8_ToManaged(
INTERNAL_TTF_FontFaceStyleName(font)
);
}
/* font refers to a TTF_Font* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
@ -218,21 +244,33 @@ namespace SDL2
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern int TTF_SizeText(
IntPtr font,
[In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
[In()] [MarshalAs(UnmanagedType.LPStr)]
string text,
out int w,
out int h
);
/* font refers to a TTF_Font* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern int TTF_SizeUTF8(
[DllImport(nativeLibName, EntryPoint = "TTF_SizeUTF8", CallingConvention = CallingConvention.Cdecl)]
public static extern int INTERNAL_TTF_SizeUTF8(
IntPtr font,
[In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string text,
byte[] text,
out int w,
out int h
);
public static int TTF_SizeUTF8(
IntPtr font,
string text,
out int w,
out int h
) {
return INTERNAL_TTF_SizeUTF8(
font,
SDL.UTF8_ToNative(text),
out w,
out h
);
}
/* font refers to a TTF_Font* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
@ -254,13 +292,23 @@ namespace SDL2
);
/* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr TTF_RenderUTF8_Solid(
[DllImport(nativeLibName, EntryPoint = "TTF_RenderUTF8_Solid", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr INTERNAL_TTF_RenderUTF8_Solid(
IntPtr font,
[In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string text,
byte[] text,
SDL.SDL_Color fg
);
public static IntPtr TTF_RenderUTF8_Solid(
IntPtr font,
string text,
SDL.SDL_Color fg
) {
return INTERNAL_TTF_RenderUTF8_Solid(
font,
SDL.UTF8_ToNative(text),
fg
);
}
/* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
@ -290,14 +338,26 @@ namespace SDL2
);
/* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr TTF_RenderUTF8_Shaded(
[DllImport(nativeLibName, EntryPoint = "TTF_RenderUTF8_Shaded", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr INTERNAL_TTF_RenderUTF8_Shaded(
IntPtr font,
[In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string text,
byte[] text,
SDL.SDL_Color fg,
SDL.SDL_Color bg
);
public static IntPtr TTF_RenderUTF8_Shaded(
IntPtr font,
string text,
SDL.SDL_Color fg,
SDL.SDL_Color bg
) {
return INTERNAL_TTF_RenderUTF8_Shaded(
font,
SDL.UTF8_ToNative(text),
fg,
bg
);
}
/* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
@ -328,13 +388,23 @@ namespace SDL2
);
/* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr TTF_RenderUTF8_Blended(
[DllImport(nativeLibName, EntryPoint = "TTF_RenderUTF8_Blended", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr INTERNAL_TTF_RenderUTF8_Blended(
IntPtr font,
[In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string text,
byte[] text,
SDL.SDL_Color fg
);
public static IntPtr TTF_RenderUTF8_Blended(
IntPtr font,
string text,
SDL.SDL_Color fg
) {
return INTERNAL_TTF_RenderUTF8_Blended(
font,
SDL.UTF8_ToNative(text),
fg
);
}
/* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
@ -356,14 +426,26 @@ namespace SDL2
);
/* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr TTF_RenderUTF8_Blended_Wrapped(
[DllImport(nativeLibName, EntryPoint = "TTF_RenderUTF8_Blended_Wrapped", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr INTERNAL_TTF_RenderUTF8_Blended_Wrapped(
IntPtr font,
[In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string text,
byte[] text,
SDL.SDL_Color fg,
uint wrapped
);
public static IntPtr TTF_RenderUTF8_Blended_Wrapped(
IntPtr font,
string text,
SDL.SDL_Color fg,
uint wrapped
) {
return INTERNAL_TTF_RenderUTF8_Blended_Wrapped(
font,
SDL.UTF8_ToNative(text),
fg,
wrapped
);
}
/* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]