Il2Cpp didn't like CustomMarshaler on returns, so string returns are Utf8StringPointer instead, which implicit converts to string

This commit is contained in:
Garry Newman 2019-07-02 13:52:04 +01:00
parent 2fb1426479
commit 38b1ed5f88
10 changed files with 44 additions and 74 deletions

View File

@ -92,6 +92,12 @@ namespace Steamworks
Console.WriteLine( $"User.SteamLevel: {SteamUser.SteamLevel}" );
}
[TestMethod]
public void Name()
{
Console.WriteLine( $"SteamClient.Name: {SteamClient.Name}" );
}
[TestMethod]
public async Task GetStoreAuthUrlAsync()
{

View File

@ -126,8 +126,7 @@ namespace Steamworks
#region FunctionMeta
[UnmanagedFunctionPointer( Platform.MemberConvention )]
[return: MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof( Utf8StringFromNative ) )]
private delegate string FGetCurrentGameLanguage( IntPtr self );
private delegate Utf8StringPointer FGetCurrentGameLanguage( IntPtr self );
private FGetCurrentGameLanguage _GetCurrentGameLanguage;
#endregion
@ -138,8 +137,7 @@ namespace Steamworks
#region FunctionMeta
[UnmanagedFunctionPointer( Platform.MemberConvention )]
[return: MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof( Utf8StringFromNative ) )]
private delegate string FGetAvailableGameLanguages( IntPtr self );
private delegate Utf8StringPointer FGetAvailableGameLanguages( IntPtr self );
private FGetAvailableGameLanguages _GetAvailableGameLanguages;
#endregion
@ -332,8 +330,7 @@ namespace Steamworks
#region FunctionMeta
[UnmanagedFunctionPointer( Platform.MemberConvention )]
[return: MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof( Utf8StringFromNative ) )]
private delegate string FGetLaunchQueryParam( IntPtr self, [MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof( Utf8StringToNative ) )] string pchKey );
private delegate Utf8StringPointer FGetLaunchQueryParam( IntPtr self, [MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof( Utf8StringToNative ) )] string pchKey );
private FGetLaunchQueryParam _GetLaunchQueryParam;
#endregion

View File

@ -168,8 +168,7 @@ namespace Steamworks
#region FunctionMeta
[UnmanagedFunctionPointer( Platform.MemberConvention )]
[return: MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof( Utf8StringFromNative ) )]
private delegate string FGetPersonaName( IntPtr self );
private delegate Utf8StringPointer FGetPersonaName( IntPtr self );
private FGetPersonaName _GetPersonaName;
#endregion
@ -256,8 +255,7 @@ namespace Steamworks
#region FunctionMeta
[UnmanagedFunctionPointer( Platform.MemberConvention )]
[return: MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof( Utf8StringFromNative ) )]
private delegate string FGetFriendPersonaName( IntPtr self, SteamId steamIDFriend );
private delegate Utf8StringPointer FGetFriendPersonaName( IntPtr self, SteamId steamIDFriend );
private FGetFriendPersonaName _GetFriendPersonaName;
#endregion
@ -280,8 +278,7 @@ namespace Steamworks
#region FunctionMeta
[UnmanagedFunctionPointer( Platform.MemberConvention )]
[return: MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof( Utf8StringFromNative ) )]
private delegate string FGetFriendPersonaNameHistory( IntPtr self, SteamId steamIDFriend, int iPersonaName );
private delegate Utf8StringPointer FGetFriendPersonaNameHistory( IntPtr self, SteamId steamIDFriend, int iPersonaName );
private FGetFriendPersonaNameHistory _GetFriendPersonaNameHistory;
#endregion
@ -303,8 +300,7 @@ namespace Steamworks
#region FunctionMeta
[UnmanagedFunctionPointer( Platform.MemberConvention )]
[return: MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof( Utf8StringFromNative ) )]
private delegate string FGetPlayerNickname( IntPtr self, SteamId steamIDPlayer );
private delegate Utf8StringPointer FGetPlayerNickname( IntPtr self, SteamId steamIDPlayer );
private FGetPlayerNickname _GetPlayerNickname;
#endregion
@ -337,8 +333,7 @@ namespace Steamworks
#region FunctionMeta
[UnmanagedFunctionPointer( Platform.MemberConvention )]
[return: MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof( Utf8StringFromNative ) )]
private delegate string FGetFriendsGroupName( IntPtr self, FriendsGroupID_t friendsGroupID );
private delegate Utf8StringPointer FGetFriendsGroupName( IntPtr self, FriendsGroupID_t friendsGroupID );
private FGetFriendsGroupName _GetFriendsGroupName;
#endregion
@ -415,8 +410,7 @@ namespace Steamworks
#region FunctionMeta
[UnmanagedFunctionPointer( Platform.MemberConvention )]
[return: MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof( Utf8StringFromNative ) )]
private delegate string FGetClanName( IntPtr self, SteamId steamIDClan );
private delegate Utf8StringPointer FGetClanName( IntPtr self, SteamId steamIDClan );
private FGetClanName _GetClanName;
#endregion
@ -427,8 +421,7 @@ namespace Steamworks
#region FunctionMeta
[UnmanagedFunctionPointer( Platform.MemberConvention )]
[return: MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof( Utf8StringFromNative ) )]
private delegate string FGetClanTag( IntPtr self, SteamId steamIDClan );
private delegate Utf8StringPointer FGetClanTag( IntPtr self, SteamId steamIDClan );
private FGetClanTag _GetClanTag;
#endregion
@ -726,8 +719,7 @@ namespace Steamworks
#region FunctionMeta
[UnmanagedFunctionPointer( Platform.MemberConvention )]
[return: MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof( Utf8StringFromNative ) )]
private delegate string FGetFriendRichPresence( IntPtr self, SteamId steamIDFriend, [MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof( Utf8StringToNative ) )] string pchKey );
private delegate Utf8StringPointer FGetFriendRichPresence( IntPtr self, SteamId steamIDFriend, [MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof( Utf8StringToNative ) )] string pchKey );
private FGetFriendRichPresence _GetFriendRichPresence;
#endregion
@ -749,8 +741,7 @@ namespace Steamworks
#region FunctionMeta
[UnmanagedFunctionPointer( Platform.MemberConvention )]
[return: MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof( Utf8StringFromNative ) )]
private delegate string FGetFriendRichPresenceKeyByIndex( IntPtr self, SteamId steamIDFriend, int iKey );
private delegate Utf8StringPointer FGetFriendRichPresenceKeyByIndex( IntPtr self, SteamId steamIDFriend, int iKey );
private FGetFriendRichPresenceKeyByIndex _GetFriendRichPresenceKeyByIndex;
#endregion

View File

@ -297,8 +297,7 @@ namespace Steamworks
#region FunctionMeta
[UnmanagedFunctionPointer( Platform.MemberConvention )]
[return: MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof( Utf8StringFromNative ) )]
private delegate string FGetGlyphForActionOrigin( IntPtr self, InputActionOrigin eOrigin );
private delegate Utf8StringPointer FGetGlyphForActionOrigin( IntPtr self, InputActionOrigin eOrigin );
private FGetGlyphForActionOrigin _GetGlyphForActionOrigin;
#endregion
@ -309,8 +308,7 @@ namespace Steamworks
#region FunctionMeta
[UnmanagedFunctionPointer( Platform.MemberConvention )]
[return: MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof( Utf8StringFromNative ) )]
private delegate string FGetStringForActionOrigin( IntPtr self, InputActionOrigin eOrigin );
private delegate Utf8StringPointer FGetStringForActionOrigin( IntPtr self, InputActionOrigin eOrigin );
private FGetStringForActionOrigin _GetStringForActionOrigin;
#endregion
@ -442,8 +440,7 @@ namespace Steamworks
#region FunctionMeta
[UnmanagedFunctionPointer( Platform.MemberConvention )]
[return: MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof( Utf8StringFromNative ) )]
private delegate string FGetStringForXboxOrigin( IntPtr self, XboxOrigin eOrigin );
private delegate Utf8StringPointer FGetStringForXboxOrigin( IntPtr self, XboxOrigin eOrigin );
private FGetStringForXboxOrigin _GetStringForXboxOrigin;
#endregion
@ -454,8 +451,7 @@ namespace Steamworks
#region FunctionMeta
[UnmanagedFunctionPointer( Platform.MemberConvention )]
[return: MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof( Utf8StringFromNative ) )]
private delegate string FGetGlyphForXboxOrigin( IntPtr self, XboxOrigin eOrigin );
private delegate Utf8StringPointer FGetGlyphForXboxOrigin( IntPtr self, XboxOrigin eOrigin );
private FGetGlyphForXboxOrigin _GetGlyphForXboxOrigin;
#endregion

View File

@ -330,8 +330,7 @@ namespace Steamworks
#region FunctionMeta
[UnmanagedFunctionPointer( Platform.MemberConvention )]
[return: MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof( Utf8StringFromNative ) )]
private delegate string FGetLobbyData( IntPtr self, SteamId steamIDLobby, [MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof( Utf8StringToNative ) )] string pchKey );
private delegate Utf8StringPointer FGetLobbyData( IntPtr self, SteamId steamIDLobby, [MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof( Utf8StringToNative ) )] string pchKey );
private FGetLobbyData _GetLobbyData;
#endregion
@ -389,8 +388,7 @@ namespace Steamworks
#region FunctionMeta
[UnmanagedFunctionPointer( Platform.MemberConvention )]
[return: MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof( Utf8StringFromNative ) )]
private delegate string FGetLobbyMemberData( IntPtr self, SteamId steamIDLobby, SteamId steamIDUser, [MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof( Utf8StringToNative ) )] string pchKey );
private delegate Utf8StringPointer FGetLobbyMemberData( IntPtr self, SteamId steamIDLobby, SteamId steamIDUser, [MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof( Utf8StringToNative ) )] string pchKey );
private FGetLobbyMemberData _GetLobbyMemberData;
#endregion

View File

@ -325,8 +325,7 @@ namespace Steamworks
#region FunctionMeta
[UnmanagedFunctionPointer( Platform.MemberConvention )]
[return: MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof( Utf8StringFromNative ) )]
private delegate string FGetFileNameAndSize( IntPtr self, int iFile, ref int pnFileSizeInBytes );
private delegate Utf8StringPointer FGetFileNameAndSize( IntPtr self, int iFile, ref int pnFileSizeInBytes );
private FGetFileNameAndSize _GetFileNameAndSize;
#endregion

View File

@ -265,8 +265,7 @@ namespace Steamworks
#region FunctionMeta
[UnmanagedFunctionPointer( Platform.MemberConvention )]
[return: MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof( Utf8StringFromNative ) )]
private delegate string FGetAchievementDisplayAttribute( IntPtr self, [MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof( Utf8StringToNative ) )] string pchName, [MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof( Utf8StringToNative ) )] string pchKey );
private delegate Utf8StringPointer FGetAchievementDisplayAttribute( IntPtr self, [MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof( Utf8StringToNative ) )] string pchName, [MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof( Utf8StringToNative ) )] string pchKey );
private FGetAchievementDisplayAttribute _GetAchievementDisplayAttribute;
#endregion
@ -300,8 +299,7 @@ namespace Steamworks
#region FunctionMeta
[UnmanagedFunctionPointer( Platform.MemberConvention )]
[return: MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof( Utf8StringFromNative ) )]
private delegate string FGetAchievementName( IntPtr self, uint iAchievement );
private delegate Utf8StringPointer FGetAchievementName( IntPtr self, uint iAchievement );
private FGetAchievementName _GetAchievementName;
#endregion
@ -405,8 +403,7 @@ namespace Steamworks
#region FunctionMeta
[UnmanagedFunctionPointer( Platform.MemberConvention )]
[return: MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof( Utf8StringFromNative ) )]
private delegate string FGetLeaderboardName( IntPtr self, SteamLeaderboard_t hSteamLeaderboard );
private delegate Utf8StringPointer FGetLeaderboardName( IntPtr self, SteamLeaderboard_t hSteamLeaderboard );
private FGetLeaderboardName _GetLeaderboardName;
#endregion

View File

@ -126,8 +126,7 @@ namespace Steamworks
#region FunctionMeta
[UnmanagedFunctionPointer( Platform.MemberConvention )]
[return: MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof( Utf8StringFromNative ) )]
private delegate string FGetIPCountry( IntPtr self );
private delegate Utf8StringPointer FGetIPCountry( IntPtr self );
private FGetIPCountry _GetIPCountry;
#endregion
@ -345,8 +344,7 @@ namespace Steamworks
#region FunctionMeta
[UnmanagedFunctionPointer( Platform.MemberConvention )]
[return: MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof( Utf8StringFromNative ) )]
private delegate string FGetSteamUILanguage( IntPtr self );
private delegate Utf8StringPointer FGetSteamUILanguage( IntPtr self );
private FGetSteamUILanguage _GetSteamUILanguage;
#endregion

View File

@ -10,7 +10,7 @@ namespace Steamworks
{
internal unsafe class Utf8StringToNative : ICustomMarshaler
{
public IntPtr MarshalManagedToNative( object managedObj )
public IntPtr MarshalManagedToNative(object managedObj)
{
if ( managedObj == null )
return IntPtr.Zero;
@ -24,7 +24,7 @@ namespace Steamworks
var wlen = System.Text.Encoding.UTF8.GetBytes( strPtr, str.Length, (byte*)mem, len + 1 );
((byte*)mem)[wlen] = 0;
( (byte*)mem )[wlen] = 0;
return mem;
}
@ -33,27 +33,27 @@ namespace Steamworks
return IntPtr.Zero;
}
public object MarshalNativeToManaged( IntPtr pNativeData ) => throw new System.NotImplementedException();
public void CleanUpNativeData( IntPtr pNativeData ) => Marshal.FreeHGlobal( pNativeData );
public void CleanUpManagedData( object managedObj ) => throw new System.NotImplementedException();
public object MarshalNativeToManaged(IntPtr pNativeData) => throw new System.NotImplementedException();
public void CleanUpNativeData(IntPtr pNativeData) => Marshal.FreeHGlobal( pNativeData );
public void CleanUpManagedData(object managedObj) => throw new System.NotImplementedException();
public int GetNativeDataSize() => -1;
public static ICustomMarshaler GetInstance( string cookie ) => new Utf8StringToNative();
public static ICustomMarshaler GetInstance(string cookie) => new Utf8StringToNative();
}
internal unsafe class Utf8StringFromNative : ICustomMarshaler
internal struct Utf8StringPointer
{
public IntPtr MarshalManagedToNative( object managedObj ) => throw new System.NotImplementedException();
internal IntPtr ptr;
public object MarshalNativeToManaged( IntPtr pNativeData )
public unsafe static implicit operator string( Utf8StringPointer p )
{
if ( pNativeData == IntPtr.Zero )
if ( p.ptr == IntPtr.Zero )
return null;
var bytes = (byte*)pNativeData;
var bytes = (byte*)p.ptr;
var dataLen = 0;
while ( dataLen < 1024 * 1024 * 8 )
while ( dataLen < 1024 * 1024 * 64 )
{
if ( bytes[dataLen] == 0 )
break;
@ -61,16 +61,7 @@ namespace Steamworks
dataLen++;
}
var str = Encoding.UTF8.GetString( bytes, dataLen );
return str;
return Encoding.UTF8.GetString( bytes, dataLen );
}
public void CleanUpNativeData( IntPtr pNativeData ) { }
public void CleanUpManagedData( object managedObj ) { }
public int GetNativeDataSize() => -1;
public static ICustomMarshaler GetInstance( string cookie ) => new Utf8StringFromNative();
}
}

View File

@ -216,11 +216,8 @@ internal class LongType : BaseType
internal class ConstCharType : BaseType
{
public override string TypeName => $"string";
public override string TypeNameFrom => $"string";
public override string TypeNameFrom => $"Utf8StringPointer";
public override string AsArgument() => $"[MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof( Utf8StringToNative ) )] {Ref}{TypeName} {VarName}";
public override string ReturnAttribute => "[return: MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof( Utf8StringFromNative ) )]";
public override string Ref => "";
}