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}" ); Console.WriteLine( $"User.SteamLevel: {SteamUser.SteamLevel}" );
} }
[TestMethod]
public void Name()
{
Console.WriteLine( $"SteamClient.Name: {SteamClient.Name}" );
}
[TestMethod] [TestMethod]
public async Task GetStoreAuthUrlAsync() public async Task GetStoreAuthUrlAsync()
{ {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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