2
0
mirror of https://github.com/raylib-cs/raylib-cs synced 2025-04-05 11:19:39 -04:00

[Fix #184] Seperate UTF8 from Ansi (#185)

This commit is contained in:
Nickolas McDonald 2023-08-12 17:59:46 -04:00 committed by GitHub
parent f4c70a2872
commit d067c6c0aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 70 additions and 33 deletions

View File

@ -53,6 +53,7 @@
<Compile Include="interop\*.cs" /> <Compile Include="interop\*.cs" />
<Compile Include="types\*.cs" /> <Compile Include="types\*.cs" />
<Compile Include="types\native\CBool.cs" /> <Compile Include="types\native\CBool.cs" />
<Compile Include="types\native\AnsiBuffer.cs" />
<Compile Include="types\native\UTF8Buffer.cs" /> <Compile Include="types\native\UTF8Buffer.cs" />
<Compile Include="types\native\FilePathList.cs" /> <Compile Include="types\native\FilePathList.cs" />
</ItemGroup> </ItemGroup>

View File

@ -56,8 +56,8 @@ namespace Raylib_cs
/// <summary>Load shader from files and bind default locations</summary> /// <summary>Load shader from files and bind default locations</summary>
public static Shader LoadShader(string vsFileName, string fsFileName) public static Shader LoadShader(string vsFileName, string fsFileName)
{ {
using var str1 = vsFileName.ToUTF8Buffer(); using var str1 = vsFileName.ToAnsiBuffer();
using var str2 = fsFileName.ToUTF8Buffer(); using var str2 = fsFileName.ToAnsiBuffer();
return LoadShader(str1.AsPointer(), str2.AsPointer()); return LoadShader(str1.AsPointer(), str2.AsPointer());
} }
@ -86,43 +86,43 @@ namespace Raylib_cs
/// <summary>Takes a screenshot of current screen (saved a .png)</summary> /// <summary>Takes a screenshot of current screen (saved a .png)</summary>
public static void TakeScreenshot(string fileName) public static void TakeScreenshot(string fileName)
{ {
using var str1 = fileName.ToUTF8Buffer(); using var str1 = fileName.ToAnsiBuffer();
TakeScreenshot(str1.AsPointer()); TakeScreenshot(str1.AsPointer());
} }
/// <summary>Check file extension</summary> /// <summary>Check file extension</summary>
public static CBool IsFileExtension(string fileName, string ext) public static CBool IsFileExtension(string fileName, string ext)
{ {
using var str1 = fileName.ToUTF8Buffer(); using var str1 = fileName.ToAnsiBuffer();
using var str2 = ext.ToUTF8Buffer(); using var str2 = ext.ToAnsiBuffer();
return IsFileExtension(str1.AsPointer(), str2.AsPointer()); return IsFileExtension(str1.AsPointer(), str2.AsPointer());
} }
/// <summary>Get file modification time (last write time)</summary> /// <summary>Get file modification time (last write time)</summary>
public static long GetFileModTime(string fileName) public static long GetFileModTime(string fileName)
{ {
using var str1 = fileName.ToUTF8Buffer(); using var str1 = fileName.ToAnsiBuffer();
return GetFileModTime(str1.AsPointer()); return GetFileModTime(str1.AsPointer());
} }
/// <summary>Load image from file into CPU memory (RAM)</summary> /// <summary>Load image from file into CPU memory (RAM)</summary>
public static Image LoadImage(string fileName) public static Image LoadImage(string fileName)
{ {
using var str1 = fileName.ToUTF8Buffer(); using var str1 = fileName.ToAnsiBuffer();
return LoadImage(str1.AsPointer()); return LoadImage(str1.AsPointer());
} }
/// <summary>Load image from RAW file data</summary> /// <summary>Load image from RAW file data</summary>
public static Image LoadImageRaw(string fileName, int width, int height, PixelFormat format, int headerSize) public static Image LoadImageRaw(string fileName, int width, int height, PixelFormat format, int headerSize)
{ {
using var str1 = fileName.ToUTF8Buffer(); using var str1 = fileName.ToAnsiBuffer();
return LoadImageRaw(str1.AsPointer(), width, height, format, headerSize); return LoadImageRaw(str1.AsPointer(), width, height, format, headerSize);
} }
/// <summary>Load image sequence from file (frames appended to image.data)</summary> /// <summary>Load image sequence from file (frames appended to image.data)</summary>
public static Image LoadImageAnim(string fileName, out int frames) public static Image LoadImageAnim(string fileName, out int frames)
{ {
using var str1 = fileName.ToUTF8Buffer(); using var str1 = fileName.ToAnsiBuffer();
fixed (int* p = &frames) fixed (int* p = &frames)
{ {
return LoadImageAnim(str1.AsPointer(), p); return LoadImageAnim(str1.AsPointer(), p);
@ -134,7 +134,7 @@ namespace Raylib_cs
/// </summary> /// </summary>
public static Image LoadImageFromMemory(string fileType, byte[] fileData) public static Image LoadImageFromMemory(string fileType, byte[] fileData)
{ {
using var fileTypeNative = fileType.ToUTF8Buffer(); using var fileTypeNative = fileType.ToAnsiBuffer();
fixed (byte* fileDataNative = fileData) fixed (byte* fileDataNative = fileData)
{ {
Image image = LoadImageFromMemory(fileTypeNative.AsPointer(), fileDataNative, fileData.Length); Image image = LoadImageFromMemory(fileTypeNative.AsPointer(), fileDataNative, fileData.Length);
@ -145,14 +145,14 @@ namespace Raylib_cs
/// <summary>Export image data to file</summary> /// <summary>Export image data to file</summary>
public static CBool ExportImage(Image image, string fileName) public static CBool ExportImage(Image image, string fileName)
{ {
using var str1 = fileName.ToUTF8Buffer(); using var str1 = fileName.ToAnsiBuffer();
return ExportImage(image, str1.AsPointer()); return ExportImage(image, str1.AsPointer());
} }
/// <summary>Export image as code file defining an array of bytes</summary> /// <summary>Export image as code file defining an array of bytes</summary>
public static CBool ExportImageAsCode(Image image, string fileName) public static CBool ExportImageAsCode(Image image, string fileName)
{ {
using var str1 = fileName.ToUTF8Buffer(); using var str1 = fileName.ToAnsiBuffer();
return ExportImageAsCode(image, str1.AsPointer()); return ExportImageAsCode(image, str1.AsPointer());
} }
@ -225,7 +225,7 @@ namespace Raylib_cs
/// <summary>Load file data as byte array (read)</summary> /// <summary>Load file data as byte array (read)</summary>
public static byte* LoadFileData(string fileName, ref uint bytesRead) public static byte* LoadFileData(string fileName, ref uint bytesRead)
{ {
using var str1 = fileName.ToUTF8Buffer(); using var str1 = fileName.ToAnsiBuffer();
fixed (uint* p = &bytesRead) fixed (uint* p = &bytesRead)
{ {
return LoadFileData(str1.AsPointer(), p); return LoadFileData(str1.AsPointer(), p);
@ -662,7 +662,7 @@ namespace Raylib_cs
/// <summary>Load texture from file into GPU memory (VRAM)</summary> /// <summary>Load texture from file into GPU memory (VRAM)</summary>
public static Texture2D LoadTexture(string fileName) public static Texture2D LoadTexture(string fileName)
{ {
using var str1 = fileName.ToUTF8Buffer(); using var str1 = fileName.ToAnsiBuffer();
return LoadTexture(str1.AsPointer()); return LoadTexture(str1.AsPointer());
} }
@ -708,14 +708,14 @@ namespace Raylib_cs
/// <summary>Load font from file into GPU memory (VRAM)</summary> /// <summary>Load font from file into GPU memory (VRAM)</summary>
public static Font LoadFont(string fileName) public static Font LoadFont(string fileName)
{ {
using var str1 = fileName.ToUTF8Buffer(); using var str1 = fileName.ToAnsiBuffer();
return LoadFont(str1.AsPointer()); return LoadFont(str1.AsPointer());
} }
/// <summary>Load font from file with extended parameters</summary> /// <summary>Load font from file with extended parameters</summary>
public static Font LoadFontEx(string fileName, int fontSize, int[] fontChars, int glyphCount) public static Font LoadFontEx(string fileName, int fontSize, int[] fontChars, int glyphCount)
{ {
using var str1 = fileName.ToUTF8Buffer(); using var str1 = fileName.ToAnsiBuffer();
fixed (int* p = fontChars) fixed (int* p = fontChars)
{ {
return LoadFontEx(str1.AsPointer(), fontSize, p, glyphCount); return LoadFontEx(str1.AsPointer(), fontSize, p, glyphCount);
@ -733,7 +733,7 @@ namespace Raylib_cs
int glyphCount int glyphCount
) )
{ {
using var fileTypeNative = fileType.ToUTF8Buffer(); using var fileTypeNative = fileType.ToAnsiBuffer();
fixed (byte* fileDataNative = fileData) fixed (byte* fileDataNative = fileData)
{ {
fixed (int* fontCharsNative = fontChars) fixed (int* fontCharsNative = fontChars)
@ -791,7 +791,7 @@ namespace Raylib_cs
/// <summary>Load model animations from file</summary> /// <summary>Load model animations from file</summary>
public static ReadOnlySpan<ModelAnimation> LoadModelAnimations(string fileName, ref uint animCount) public static ReadOnlySpan<ModelAnimation> LoadModelAnimations(string fileName, ref uint animCount)
{ {
using var str1 = fileName.ToUTF8Buffer(); using var str1 = fileName.ToAnsiBuffer();
fixed (uint* p = &animCount) fixed (uint* p = &animCount)
{ {
ModelAnimation* modelAnimations = LoadModelAnimations(str1.AsPointer(), p); ModelAnimation* modelAnimations = LoadModelAnimations(str1.AsPointer(), p);
@ -965,14 +965,14 @@ namespace Raylib_cs
/// <summary>Draw a model (with texture if set)</summary> /// <summary>Draw a model (with texture if set)</summary>
public static Model LoadModel(string fileName) public static Model LoadModel(string fileName)
{ {
using var str1 = fileName.ToUTF8Buffer(); using var str1 = fileName.ToAnsiBuffer();
return LoadModel(str1.AsPointer()); return LoadModel(str1.AsPointer());
} }
/// <summary>Export mesh data to file, returns true on success</summary> /// <summary>Export mesh data to file, returns true on success</summary>
public static CBool ExportMesh(Mesh mesh, string fileName) public static CBool ExportMesh(Mesh mesh, string fileName)
{ {
using var str1 = fileName.ToUTF8Buffer(); using var str1 = fileName.ToAnsiBuffer();
return ExportMesh(mesh, str1.AsPointer()); return ExportMesh(mesh, str1.AsPointer());
} }
@ -997,7 +997,7 @@ namespace Raylib_cs
/// <summary>Load wave data from file</summary> /// <summary>Load wave data from file</summary>
public static Wave LoadWave(string fileName) public static Wave LoadWave(string fileName)
{ {
using var str1 = fileName.ToUTF8Buffer(); using var str1 = fileName.ToAnsiBuffer();
return LoadWave(str1.AsPointer()); return LoadWave(str1.AsPointer());
} }
@ -1009,7 +1009,7 @@ namespace Raylib_cs
byte[] fileData byte[] fileData
) )
{ {
using var fileTypeNative = fileType.ToUTF8Buffer(); using var fileTypeNative = fileType.ToAnsiBuffer();
fixed (byte* fileDataNative = fileData) fixed (byte* fileDataNative = fileData)
{ {
@ -1026,28 +1026,28 @@ namespace Raylib_cs
/// <summary>Load sound from file</summary> /// <summary>Load sound from file</summary>
public static Sound LoadSound(string fileName) public static Sound LoadSound(string fileName)
{ {
using var str1 = fileName.ToUTF8Buffer(); using var str1 = fileName.ToAnsiBuffer();
return LoadSound(str1.AsPointer()); return LoadSound(str1.AsPointer());
} }
/// <summary>Export wave data to file</summary> /// <summary>Export wave data to file</summary>
public static CBool ExportWave(Wave wave, string fileName) public static CBool ExportWave(Wave wave, string fileName)
{ {
using var str1 = fileName.ToUTF8Buffer(); using var str1 = fileName.ToAnsiBuffer();
return ExportWave(wave, str1.AsPointer()); return ExportWave(wave, str1.AsPointer());
} }
/// <summary>Export wave sample data to code (.h)</summary> /// <summary>Export wave sample data to code (.h)</summary>
public static CBool ExportWaveAsCode(Wave wave, string fileName) public static CBool ExportWaveAsCode(Wave wave, string fileName)
{ {
using var str1 = fileName.ToUTF8Buffer(); using var str1 = fileName.ToAnsiBuffer();
return ExportWaveAsCode(wave, str1.AsPointer()); return ExportWaveAsCode(wave, str1.AsPointer());
} }
/// <summary>Load music stream from file</summary> /// <summary>Load music stream from file</summary>
public static Music LoadMusicStream(string fileName) public static Music LoadMusicStream(string fileName)
{ {
using var str1 = fileName.ToUTF8Buffer(); using var str1 = fileName.ToAnsiBuffer();
return LoadMusicStream(str1.AsPointer()); return LoadMusicStream(str1.AsPointer());
} }
@ -1059,7 +1059,7 @@ namespace Raylib_cs
byte[] fileData byte[] fileData
) )
{ {
using var fileTypeNative = fileType.ToUTF8Buffer(); using var fileTypeNative = fileType.ToAnsiBuffer();
fixed (byte* fileDataNative = fileData) fixed (byte* fileDataNative = fileData)
{ {

View File

@ -0,0 +1,36 @@
using System;
using System.Runtime.InteropServices;
namespace Raylib_cs
{
/// <summary>
/// Converts text to a Ansi buffer for passing to native code
/// </summary>
public readonly ref struct AnsiBuffer
{
private readonly IntPtr data;
public AnsiBuffer(string text)
{
data = Marshal.StringToHGlobalAnsi(text);
}
public unsafe sbyte* AsPointer()
{
return (sbyte*)data.ToPointer();
}
public void Dispose()
{
Marshal.FreeHGlobal(data);
}
}
public static class AnsiStringUtils
{
public static AnsiBuffer ToAnsiBuffer(this string text)
{
return new AnsiBuffer(text);
}
}
}

View File

@ -1,19 +1,19 @@
using System; using System;
using System.Text;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text;
namespace Raylib_cs namespace Raylib_cs
{ {
/// <summary> /// <summary>
/// Converts text to a UTF8 buffer for passing to native code /// Converts text to a UTF8 buffer for passing to native code
/// </summary> /// </summary>
public ref struct UTF8Buffer public readonly ref struct UTF8Buffer
{ {
private IntPtr data; private readonly IntPtr data;
public UTF8Buffer(string text) public UTF8Buffer(string text)
{ {
this.data = Marshal.StringToHGlobalAnsi(text); data = Marshal.StringToCoTaskMemUTF8(text);
} }
public unsafe sbyte* AsPointer() public unsafe sbyte* AsPointer()
@ -23,7 +23,7 @@ namespace Raylib_cs
public void Dispose() public void Dispose()
{ {
Marshal.FreeHGlobal(data); Marshal.ZeroFreeCoTaskMemUTF8(data);
} }
} }