From be61ce992b0933a6c9d6968236629a0f6b1b1291 Mon Sep 17 00:00:00 2001 From: Nickolas McDonald <43690021+n77y@users.noreply.github.com> Date: Fri, 11 Aug 2023 15:43:15 -0400 Subject: [PATCH] Seperate UTF8 from Ansi --- Raylib-cs.sln | 30 +++++++++--- Raylib-cs/Raylib-cs.csproj | 5 +- Raylib-cs/types/Raylib.Utils.cs | 68 ++++++++++++++-------------- Raylib-cs/types/native/AnsiBuffer.cs | 36 +++++++++++++++ Raylib-cs/types/native/UTF8Buffer.cs | 12 ++--- 5 files changed, 102 insertions(+), 49 deletions(-) create mode 100644 Raylib-cs/types/native/AnsiBuffer.cs diff --git a/Raylib-cs.sln b/Raylib-cs.sln index 823188f..3d468c2 100644 --- a/Raylib-cs.sln +++ b/Raylib-cs.sln @@ -1,9 +1,9 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.27703.2035 +# Visual Studio Version 17 +VisualStudioVersion = 17.6.33829.357 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Raylib-cs", "Raylib-cs\Raylib-cs.csproj", "{B02C431E-271A-432E-BA5C-EE3B68DBF585}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Raylib-cs", "Raylib-cs\Raylib-cs.csproj", "{B02C431E-271A-432E-BA5C-EE3B68DBF585}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Raylib-cs.Tests", "Raylib-cs.Tests\Raylib-cs.Tests.csproj", "{523DEE6A-20CD-47AB-94A6-8D3C3CF9ADAD}" EndProject @@ -17,14 +17,30 @@ Global Release|x86 = Release|x86 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {B02C431E-271A-432E-BA5C-EE3B68DBF585}.Release|Any CPU.ActiveCfg = Release|Any CPU - {B02C431E-271A-432E-BA5C-EE3B68DBF585}.Release|Any CPU.Build.0 = Release|Any CPU {B02C431E-271A-432E-BA5C-EE3B68DBF585}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {B02C431E-271A-432E-BA5C-EE3B68DBF585}.Debug|Any CPU.Build.0 = Debug|Any CPU - {523DEE6A-20CD-47AB-94A6-8D3C3CF9ADAD}.Release|Any CPU.ActiveCfg = Release|Any CPU - {523DEE6A-20CD-47AB-94A6-8D3C3CF9ADAD}.Release|Any CPU.Build.0 = Release|Any CPU + {B02C431E-271A-432E-BA5C-EE3B68DBF585}.Debug|x64.ActiveCfg = Debug|Any CPU + {B02C431E-271A-432E-BA5C-EE3B68DBF585}.Debug|x64.Build.0 = Debug|Any CPU + {B02C431E-271A-432E-BA5C-EE3B68DBF585}.Debug|x86.ActiveCfg = Debug|Any CPU + {B02C431E-271A-432E-BA5C-EE3B68DBF585}.Debug|x86.Build.0 = Debug|Any CPU + {B02C431E-271A-432E-BA5C-EE3B68DBF585}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B02C431E-271A-432E-BA5C-EE3B68DBF585}.Release|Any CPU.Build.0 = Release|Any CPU + {B02C431E-271A-432E-BA5C-EE3B68DBF585}.Release|x64.ActiveCfg = Release|Any CPU + {B02C431E-271A-432E-BA5C-EE3B68DBF585}.Release|x64.Build.0 = Release|Any CPU + {B02C431E-271A-432E-BA5C-EE3B68DBF585}.Release|x86.ActiveCfg = Release|Any CPU + {B02C431E-271A-432E-BA5C-EE3B68DBF585}.Release|x86.Build.0 = Release|Any CPU {523DEE6A-20CD-47AB-94A6-8D3C3CF9ADAD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {523DEE6A-20CD-47AB-94A6-8D3C3CF9ADAD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {523DEE6A-20CD-47AB-94A6-8D3C3CF9ADAD}.Debug|x64.ActiveCfg = Debug|Any CPU + {523DEE6A-20CD-47AB-94A6-8D3C3CF9ADAD}.Debug|x64.Build.0 = Debug|Any CPU + {523DEE6A-20CD-47AB-94A6-8D3C3CF9ADAD}.Debug|x86.ActiveCfg = Debug|Any CPU + {523DEE6A-20CD-47AB-94A6-8D3C3CF9ADAD}.Debug|x86.Build.0 = Debug|Any CPU + {523DEE6A-20CD-47AB-94A6-8D3C3CF9ADAD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {523DEE6A-20CD-47AB-94A6-8D3C3CF9ADAD}.Release|Any CPU.Build.0 = Release|Any CPU + {523DEE6A-20CD-47AB-94A6-8D3C3CF9ADAD}.Release|x64.ActiveCfg = Release|Any CPU + {523DEE6A-20CD-47AB-94A6-8D3C3CF9ADAD}.Release|x64.Build.0 = Release|Any CPU + {523DEE6A-20CD-47AB-94A6-8D3C3CF9ADAD}.Release|x86.ActiveCfg = Release|Any CPU + {523DEE6A-20CD-47AB-94A6-8D3C3CF9ADAD}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Raylib-cs/Raylib-cs.csproj b/Raylib-cs/Raylib-cs.csproj index e0db93b..8e51cb7 100644 --- a/Raylib-cs/Raylib-cs.csproj +++ b/Raylib-cs/Raylib-cs.csproj @@ -38,7 +38,7 @@ - + @@ -53,7 +53,8 @@ - + + diff --git a/Raylib-cs/types/Raylib.Utils.cs b/Raylib-cs/types/Raylib.Utils.cs index db4c676..739b342 100644 --- a/Raylib-cs/types/Raylib.Utils.cs +++ b/Raylib-cs/types/Raylib.Utils.cs @@ -9,14 +9,14 @@ namespace Raylib_cs /// Initialize window and OpenGL context public static void InitWindow(int width, int height, string title) { - using var str1 = title.ToUTF8Buffer(); + using var str1 = title.ToAnsiBuffer(); InitWindow(width, height, str1.AsPointer()); } /// Set title for window (only PLATFORM_DESKTOP) public static void SetWindowTitle(string title) { - using var str1 = title.ToUTF8Buffer(); + using var str1 = title.ToAnsiBuffer(); SetWindowTitle(str1.AsPointer()); } @@ -35,7 +35,7 @@ namespace Raylib_cs /// Set clipboard text content public static void SetClipboardText(string text) { - using var str1 = text.ToUTF8Buffer(); + using var str1 = text.ToAnsiBuffer(); SetClipboardText(str1.AsPointer()); } @@ -56,73 +56,73 @@ 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()); } /// Load shader from code string and bind default locations public static Shader LoadShaderFromMemory(string vsCode, string fsCode) { - using var str1 = vsCode.ToUTF8Buffer(); - using var str2 = fsCode.ToUTF8Buffer(); + using var str1 = vsCode.ToAnsiBuffer(); + using var str2 = fsCode.ToAnsiBuffer(); return LoadShaderFromMemory(str1.AsPointer(), str2.AsPointer()); } /// Get shader uniform location public static int GetShaderLocation(Shader shader, string uniformName) { - using var str1 = uniformName.ToUTF8Buffer(); + using var str1 = uniformName.ToAnsiBuffer(); return GetShaderLocation(shader, str1.AsPointer()); } /// Get shader attribute location public static int GetShaderLocationAttrib(Shader shader, string attribName) { - using var str1 = attribName.ToUTF8Buffer(); + using var str1 = attribName.ToAnsiBuffer(); return GetShaderLocationAttrib(shader, str1.AsPointer()); } /// 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); } }