From 38b1ed5f8806b8f3d1508e5ecd46a5869317d2f3 Mon Sep 17 00:00:00 2001 From: Garry Newman Date: Tue, 2 Jul 2019 13:52:04 +0100 Subject: [PATCH] Il2Cpp didn't like CustomMarshaler on returns, so string returns are Utf8StringPointer instead, which implicit converts to string --- Facepunch.Steamworks.Test/UserTest.cs | 6 ++++ .../Generated/Interfaces/ISteamApps.cs | 9 ++--- .../Generated/Interfaces/ISteamFriends.cs | 27 +++++--------- .../Generated/Interfaces/ISteamInput.cs | 12 +++---- .../Generated/Interfaces/ISteamMatchmaking.cs | 6 ++-- .../Interfaces/ISteamRemoteStorage.cs | 3 +- .../Generated/Interfaces/ISteamUserStats.cs | 9 ++--- .../Generated/Interfaces/ISteamUtils.cs | 6 ++-- Facepunch.Steamworks/Utility/Utf8String.cs | 35 +++++++------------ Generator/CodeWriter/Types/BaseType.cs | 5 +-- 10 files changed, 44 insertions(+), 74 deletions(-) diff --git a/Facepunch.Steamworks.Test/UserTest.cs b/Facepunch.Steamworks.Test/UserTest.cs index 25e28fd..48ef38e 100644 --- a/Facepunch.Steamworks.Test/UserTest.cs +++ b/Facepunch.Steamworks.Test/UserTest.cs @@ -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() { diff --git a/Facepunch.Steamworks/Generated/Interfaces/ISteamApps.cs b/Facepunch.Steamworks/Generated/Interfaces/ISteamApps.cs index 6d32113..665e4ff 100644 --- a/Facepunch.Steamworks/Generated/Interfaces/ISteamApps.cs +++ b/Facepunch.Steamworks/Generated/Interfaces/ISteamApps.cs @@ -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 diff --git a/Facepunch.Steamworks/Generated/Interfaces/ISteamFriends.cs b/Facepunch.Steamworks/Generated/Interfaces/ISteamFriends.cs index a3c13f3..e091157 100644 --- a/Facepunch.Steamworks/Generated/Interfaces/ISteamFriends.cs +++ b/Facepunch.Steamworks/Generated/Interfaces/ISteamFriends.cs @@ -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 diff --git a/Facepunch.Steamworks/Generated/Interfaces/ISteamInput.cs b/Facepunch.Steamworks/Generated/Interfaces/ISteamInput.cs index c70ab2b..fc8a426 100644 --- a/Facepunch.Steamworks/Generated/Interfaces/ISteamInput.cs +++ b/Facepunch.Steamworks/Generated/Interfaces/ISteamInput.cs @@ -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 diff --git a/Facepunch.Steamworks/Generated/Interfaces/ISteamMatchmaking.cs b/Facepunch.Steamworks/Generated/Interfaces/ISteamMatchmaking.cs index 98a623e..fd72e4d 100644 --- a/Facepunch.Steamworks/Generated/Interfaces/ISteamMatchmaking.cs +++ b/Facepunch.Steamworks/Generated/Interfaces/ISteamMatchmaking.cs @@ -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 diff --git a/Facepunch.Steamworks/Generated/Interfaces/ISteamRemoteStorage.cs b/Facepunch.Steamworks/Generated/Interfaces/ISteamRemoteStorage.cs index 7288a22..46cad5c 100644 --- a/Facepunch.Steamworks/Generated/Interfaces/ISteamRemoteStorage.cs +++ b/Facepunch.Steamworks/Generated/Interfaces/ISteamRemoteStorage.cs @@ -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 diff --git a/Facepunch.Steamworks/Generated/Interfaces/ISteamUserStats.cs b/Facepunch.Steamworks/Generated/Interfaces/ISteamUserStats.cs index c012f17..b94a6d5 100644 --- a/Facepunch.Steamworks/Generated/Interfaces/ISteamUserStats.cs +++ b/Facepunch.Steamworks/Generated/Interfaces/ISteamUserStats.cs @@ -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 diff --git a/Facepunch.Steamworks/Generated/Interfaces/ISteamUtils.cs b/Facepunch.Steamworks/Generated/Interfaces/ISteamUtils.cs index be67745..19f83ae 100644 --- a/Facepunch.Steamworks/Generated/Interfaces/ISteamUtils.cs +++ b/Facepunch.Steamworks/Generated/Interfaces/ISteamUtils.cs @@ -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 diff --git a/Facepunch.Steamworks/Utility/Utf8String.cs b/Facepunch.Steamworks/Utility/Utf8String.cs index 9e31bc6..062847d 100644 --- a/Facepunch.Steamworks/Utility/Utf8String.cs +++ b/Facepunch.Steamworks/Utility/Utf8String.cs @@ -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(); } } diff --git a/Generator/CodeWriter/Types/BaseType.cs b/Generator/CodeWriter/Types/BaseType.cs index a97c19a..e4822f6 100644 --- a/Generator/CodeWriter/Types/BaseType.cs +++ b/Generator/CodeWriter/Types/BaseType.cs @@ -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 => ""; }