diff --git a/Raylib-cs/Raylib-cs.csproj b/Raylib-cs/Raylib-cs.csproj index 0776b14..285069d 100644 --- a/Raylib-cs/Raylib-cs.csproj +++ b/Raylib-cs/Raylib-cs.csproj @@ -53,6 +53,7 @@ + diff --git a/Raylib-cs/types/Raylib.Utils.cs b/Raylib-cs/types/Raylib.Utils.cs index db4c676..e6c562b 100644 --- a/Raylib-cs/types/Raylib.Utils.cs +++ b/Raylib-cs/types/Raylib.Utils.cs @@ -56,8 +56,8 @@ namespace Raylib_cs /// Load shader from files and bind default locations public static Shader LoadShader(string vsFileName, string fsFileName) { - using var str1 = vsFileName.ToUTF8Buffer(); - using var str2 = fsFileName.ToUTF8Buffer(); + using var str1 = vsFileName.ToAnsiBuffer(); + using var str2 = fsFileName.ToAnsiBuffer(); return LoadShader(str1.AsPointer(), str2.AsPointer()); } @@ -86,43 +86,43 @@ namespace Raylib_cs /// Takes a screenshot of current screen (saved a .png) public static void TakeScreenshot(string fileName) { - using var str1 = fileName.ToUTF8Buffer(); + using var str1 = fileName.ToAnsiBuffer(); TakeScreenshot(str1.AsPointer()); } /// Check file extension public static CBool IsFileExtension(string fileName, string ext) { - using var str1 = fileName.ToUTF8Buffer(); - using var str2 = ext.ToUTF8Buffer(); + using var str1 = fileName.ToAnsiBuffer(); + using var str2 = ext.ToAnsiBuffer(); return IsFileExtension(str1.AsPointer(), str2.AsPointer()); } /// Get file modification time (last write time) public static long GetFileModTime(string fileName) { - using var str1 = fileName.ToUTF8Buffer(); + using var str1 = fileName.ToAnsiBuffer(); return GetFileModTime(str1.AsPointer()); } /// Load image from file into CPU memory (RAM) public static Image LoadImage(string fileName) { - using var str1 = fileName.ToUTF8Buffer(); + using var str1 = fileName.ToAnsiBuffer(); return LoadImage(str1.AsPointer()); } /// Load image from RAW file data 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); } /// Load image sequence from file (frames appended to image.data) public static Image LoadImageAnim(string fileName, out int frames) { - using var str1 = fileName.ToUTF8Buffer(); + using var str1 = fileName.ToAnsiBuffer(); fixed (int* p = &frames) { return LoadImageAnim(str1.AsPointer(), p); @@ -134,7 +134,7 @@ namespace Raylib_cs /// public static Image LoadImageFromMemory(string fileType, byte[] fileData) { - using var fileTypeNative = fileType.ToUTF8Buffer(); + using var fileTypeNative = fileType.ToAnsiBuffer(); fixed (byte* fileDataNative = fileData) { Image image = LoadImageFromMemory(fileTypeNative.AsPointer(), fileDataNative, fileData.Length); @@ -145,14 +145,14 @@ namespace Raylib_cs /// Export image data to file public static CBool ExportImage(Image image, string fileName) { - using var str1 = fileName.ToUTF8Buffer(); + using var str1 = fileName.ToAnsiBuffer(); return ExportImage(image, str1.AsPointer()); } /// Export image as code file defining an array of bytes public static CBool ExportImageAsCode(Image image, string fileName) { - using var str1 = fileName.ToUTF8Buffer(); + using var str1 = fileName.ToAnsiBuffer(); return ExportImageAsCode(image, str1.AsPointer()); } @@ -225,7 +225,7 @@ namespace Raylib_cs /// Load file data as byte array (read) public static byte* LoadFileData(string fileName, ref uint bytesRead) { - using var str1 = fileName.ToUTF8Buffer(); + using var str1 = fileName.ToAnsiBuffer(); fixed (uint* p = &bytesRead) { return LoadFileData(str1.AsPointer(), p); @@ -662,7 +662,7 @@ namespace Raylib_cs /// Load texture from file into GPU memory (VRAM) public static Texture2D LoadTexture(string fileName) { - using var str1 = fileName.ToUTF8Buffer(); + using var str1 = fileName.ToAnsiBuffer(); return LoadTexture(str1.AsPointer()); } @@ -708,14 +708,14 @@ namespace Raylib_cs /// Load font from file into GPU memory (VRAM) public static Font LoadFont(string fileName) { - using var str1 = fileName.ToUTF8Buffer(); + using var str1 = fileName.ToAnsiBuffer(); return LoadFont(str1.AsPointer()); } /// Load font from file with extended parameters 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) { return LoadFontEx(str1.AsPointer(), fontSize, p, glyphCount); @@ -733,7 +733,7 @@ namespace Raylib_cs int glyphCount ) { - using var fileTypeNative = fileType.ToUTF8Buffer(); + using var fileTypeNative = fileType.ToAnsiBuffer(); fixed (byte* fileDataNative = fileData) { fixed (int* fontCharsNative = fontChars) @@ -791,7 +791,7 @@ namespace Raylib_cs /// Load model animations from file public static ReadOnlySpan LoadModelAnimations(string fileName, ref uint animCount) { - using var str1 = fileName.ToUTF8Buffer(); + using var str1 = fileName.ToAnsiBuffer(); fixed (uint* p = &animCount) { ModelAnimation* modelAnimations = LoadModelAnimations(str1.AsPointer(), p); @@ -965,14 +965,14 @@ namespace Raylib_cs /// Draw a model (with texture if set) public static Model LoadModel(string fileName) { - using var str1 = fileName.ToUTF8Buffer(); + using var str1 = fileName.ToAnsiBuffer(); return LoadModel(str1.AsPointer()); } /// Export mesh data to file, returns true on success public static CBool ExportMesh(Mesh mesh, string fileName) { - using var str1 = fileName.ToUTF8Buffer(); + using var str1 = fileName.ToAnsiBuffer(); return ExportMesh(mesh, str1.AsPointer()); } @@ -997,7 +997,7 @@ namespace Raylib_cs /// Load wave data from file public static Wave LoadWave(string fileName) { - using var str1 = fileName.ToUTF8Buffer(); + using var str1 = fileName.ToAnsiBuffer(); return LoadWave(str1.AsPointer()); } @@ -1009,7 +1009,7 @@ namespace Raylib_cs byte[] fileData ) { - using var fileTypeNative = fileType.ToUTF8Buffer(); + using var fileTypeNative = fileType.ToAnsiBuffer(); fixed (byte* fileDataNative = fileData) { @@ -1026,28 +1026,28 @@ namespace Raylib_cs /// Load sound from file public static Sound LoadSound(string fileName) { - using var str1 = fileName.ToUTF8Buffer(); + using var str1 = fileName.ToAnsiBuffer(); return LoadSound(str1.AsPointer()); } /// Export wave data to file public static CBool ExportWave(Wave wave, string fileName) { - using var str1 = fileName.ToUTF8Buffer(); + using var str1 = fileName.ToAnsiBuffer(); return ExportWave(wave, str1.AsPointer()); } /// Export wave sample data to code (.h) public static CBool ExportWaveAsCode(Wave wave, string fileName) { - using var str1 = fileName.ToUTF8Buffer(); + using var str1 = fileName.ToAnsiBuffer(); return ExportWaveAsCode(wave, str1.AsPointer()); } /// Load music stream from file public static Music LoadMusicStream(string fileName) { - using var str1 = fileName.ToUTF8Buffer(); + using var str1 = fileName.ToAnsiBuffer(); return LoadMusicStream(str1.AsPointer()); } @@ -1059,7 +1059,7 @@ namespace Raylib_cs byte[] fileData ) { - using var fileTypeNative = fileType.ToUTF8Buffer(); + using var fileTypeNative = fileType.ToAnsiBuffer(); fixed (byte* fileDataNative = fileData) { diff --git a/Raylib-cs/types/native/AnsiBuffer.cs b/Raylib-cs/types/native/AnsiBuffer.cs new file mode 100644 index 0000000..311496a --- /dev/null +++ b/Raylib-cs/types/native/AnsiBuffer.cs @@ -0,0 +1,36 @@ +using System; +using System.Runtime.InteropServices; + +namespace Raylib_cs +{ + /// + /// Converts text to a Ansi buffer for passing to native code + /// + 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); + } + } +} diff --git a/Raylib-cs/types/native/UTF8Buffer.cs b/Raylib-cs/types/native/UTF8Buffer.cs index 4dd201d..a46f4f3 100644 --- a/Raylib-cs/types/native/UTF8Buffer.cs +++ b/Raylib-cs/types/native/UTF8Buffer.cs @@ -1,19 +1,19 @@ -using System; -using System.Text; +using System; using System.Runtime.InteropServices; +using System.Text; namespace Raylib_cs { /// /// Converts text to a UTF8 buffer for passing to native code /// - public ref struct UTF8Buffer + public readonly ref struct UTF8Buffer { - private IntPtr data; + private readonly IntPtr data; public UTF8Buffer(string text) { - this.data = Marshal.StringToHGlobalAnsi(text); + data = Marshal.StringToCoTaskMemUTF8(text); } public unsafe sbyte* AsPointer() @@ -23,7 +23,7 @@ namespace Raylib_cs public void Dispose() { - Marshal.FreeHGlobal(data); + Marshal.ZeroFreeCoTaskMemUTF8(data); } }