Explicitly turn off BOM when marshaling strings to/from Steamworks, just to be safe

This commit is contained in:
Rohan Singh 2025-02-03 22:13:32 -05:00
parent b4d3a65f86
commit 0595c9c796

View File

@ -1,8 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Text;
@ -10,6 +6,8 @@ namespace Steamworks
{ {
internal unsafe class Utf8StringToNative : ICustomMarshaler internal unsafe class Utf8StringToNative : ICustomMarshaler
{ {
internal static readonly Encoding Utf8NoBom = new UTF8Encoding( false, false );
public IntPtr MarshalManagedToNative(object managedObj) public IntPtr MarshalManagedToNative(object managedObj)
{ {
if ( managedObj == null ) if ( managedObj == null )
@ -19,10 +17,10 @@ namespace Steamworks
{ {
fixed ( char* strPtr = str ) fixed ( char* strPtr = str )
{ {
int len = Encoding.UTF8.GetByteCount( str ); int len = Utf8NoBom.GetByteCount( str );
var mem = Marshal.AllocHGlobal( len + 1 ); var mem = Marshal.AllocHGlobal( len + 1 );
var wlen = System.Text.Encoding.UTF8.GetBytes( strPtr, str.Length, (byte*)mem, len + 1 ); var wlen = Utf8NoBom.GetBytes( strPtr, str.Length, (byte*)mem, len + 1 );
( (byte*)mem )[wlen] = 0; ( (byte*)mem )[wlen] = 0;
@ -64,7 +62,7 @@ namespace Steamworks
dataLen++; dataLen++;
} }
return Encoding.UTF8.GetString( bytes, dataLen ); return Utf8StringToNative.Utf8NoBom.GetString( bytes, dataLen );
} }
} }
} }