Unity Win32 callback fixes

This commit is contained in:
Garry Newman 2017-02-01 12:44:00 +00:00
parent d7808bf424
commit b7727a10e1
9 changed files with 12742 additions and 5551 deletions

View File

@ -16,16 +16,29 @@ namespace Facepunch.Steamworks
//
// Windows Config
//
if ( platform == "WindowsEditor" || platform == "Windows" )
if ( platform == "WindowsEditor" || platform == "WindowsPlayer" )
{
//
// 32bit windows unity uses a stdcall
//
if ( IntPtr.Size == 4 ) UseThisCall = false;
ForcePlatform( OperatingSystem.Windows, IntPtr.Size == 4 ? Architecture.x86 : Architecture.x64 );
ForcePlatform( OperatingSystem.Windows, IntPtr.Size == 4 ? Architecture.x86 : Architecture.x64 );
}
if ( platform == "OSXEditor" || platform == "OSXPlayer" || platform == "OSXDashboardPlayer" )
{
ForcePlatform( OperatingSystem.Osx, IntPtr.Size == 4 ? Architecture.x86 : Architecture.x64 );
}
if ( platform == "LinuxPlayer" || platform == "LinuxEditor" )
{
ForcePlatform( OperatingSystem.Linux, IntPtr.Size == 4 ? Architecture.x86 : Architecture.x64 );
}
Console.WriteLine( "Facepunch.Steamworks Unity: " + platform );
Console.WriteLine( "Facepunch.Steamworks Os: " + SteamNative.Platform.Os );
Console.WriteLine( "Facepunch.Steamworks Arch: " + SteamNative.Platform.Arch );
}
/// <summary>

View File

@ -30,9 +30,16 @@ namespace SteamNative
//
internal class ThisCall
{
[UnmanagedFunctionPointer( CallingConvention.StdCall )] public delegate void Result( IntPtr thisptr, IntPtr pvParam );
[UnmanagedFunctionPointer( CallingConvention.StdCall )] public delegate void ResultWithInfo( IntPtr thisptr, IntPtr pvParam, bool bIOFailure, SteamNative.SteamAPICall_t hSteamAPICall );
[UnmanagedFunctionPointer( CallingConvention.StdCall )] public delegate int GetSize( IntPtr thisptr );
[UnmanagedFunctionPointer( CallingConvention.ThisCall )] public delegate void Result( IntPtr thisptr, IntPtr pvParam );
[UnmanagedFunctionPointer( CallingConvention.ThisCall )] public delegate void ResultWithInfo( IntPtr thisptr, IntPtr pvParam, bool bIOFailure, SteamNative.SteamAPICall_t hSteamAPICall );
[UnmanagedFunctionPointer( CallingConvention.ThisCall )] public delegate int GetSize( IntPtr thisptr );
}
internal class StdCall
{
[UnmanagedFunctionPointer( CallingConvention.StdCall )] public delegate void Result( IntPtr pvParam );
[UnmanagedFunctionPointer( CallingConvention.StdCall )] public delegate void ResultWithInfo( IntPtr pvParam, bool bIOFailure, SteamNative.SteamAPICall_t hSteamAPICall );
[UnmanagedFunctionPointer( CallingConvention.StdCall )] public delegate int GetSize();
}

View File

@ -8,7 +8,7 @@ namespace SteamNative
internal class Linux32 : Interface
{
internal IntPtr _ptr;
public bool IsValid { get{ return _ptr != null; } }
public bool IsValid { get{ return _ptr != IntPtr.Zero; } }
//
// Constructor sets pointer to native class

View File

@ -8,7 +8,7 @@ namespace SteamNative
internal class Linux64 : Interface
{
internal IntPtr _ptr;
public bool IsValid { get{ return _ptr != null; } }
public bool IsValid { get{ return _ptr != IntPtr.Zero; } }
//
// Constructor sets pointer to native class

View File

@ -8,7 +8,7 @@ namespace SteamNative
internal class Mac : Interface
{
internal IntPtr _ptr;
public bool IsValid { get{ return _ptr != null; } }
public bool IsValid { get{ return _ptr != IntPtr.Zero; } }
//
// Constructor sets pointer to native class

View File

@ -8,7 +8,7 @@ namespace SteamNative
internal class Win32 : Interface
{
internal IntPtr _ptr;
public bool IsValid { get{ return _ptr != null; } }
public bool IsValid { get{ return _ptr != IntPtr.Zero; } }
//
// Constructor sets pointer to native class

View File

@ -8,7 +8,7 @@ namespace SteamNative
internal class Win64 : Interface
{
internal IntPtr _ptr;
public bool IsValid { get{ return _ptr != null; } }
public bool IsValid { get{ return _ptr != IntPtr.Zero; } }
//
// Constructor sets pointer to native class

File diff suppressed because it is too large Load Diff

View File

@ -246,10 +246,40 @@ namespace Generator
WriteLine( "// Create the functions we need for the vtable" );
WriteLine( "//" );
StartBlock( "if ( Facepunch.Steamworks.Config.UseThisCall )" );
{
CallresultFunctions( c, Result, "ThisCall", "_" );
}
Else();
{
CallresultFunctions( c, Result, "StdCall", "" );
}
EndBlock();
WriteLine( "" );
WriteLine( "//" );
WriteLine( "// Create the callback object" );
WriteLine( "//" );
WriteLine( $"var cb = new Callback();" );
WriteLine( $"cb.vTablePtr = handle.vTablePtr;" );
WriteLine( $"cb.CallbackFlags = steamworks.IsGameServer ? (byte) SteamNative.Callback.Flags.GameServer : (byte) 0;" );
WriteLine( $"cb.CallbackId = CallbackId;" );
WriteLine( "" );
WriteLine( "//" );
WriteLine( "// Pin the callback, so it doesn't get garbage collected and we can pass the pointer to native" );
WriteLine( "//" );
WriteLine( $"handle.PinnedCallback = GCHandle.Alloc( cb, GCHandleType.Pinned );" );
}
private void CallresultFunctions( SteamApiDefinition.StructDef c, bool Result, string ThisCall, string ThisArg )
{
var ThisArgC = ThisArg.Length > 0 ? $"{ThisArg}, " : "";
if ( Result )
{
WriteLine( $"Callback.ThisCall.Result funcA = ( _, p ) => {{ handle.Dispose(); CallbackFunction( FromPointer( p ), false ); }};" );
StartBlock( $"Callback.ThisCall.ResultWithInfo funcB = ( _, p, bIOFailure, hSteamAPICall ) => " );
WriteLine( $"Callback.{ThisCall}.Result funcA = ( {ThisArgC}p ) => {{ handle.Dispose(); CallbackFunction( FromPointer( p ), false ); }};" );
StartBlock( $"Callback.{ThisCall}.ResultWithInfo funcB = ( {ThisArgC}p, bIOFailure, hSteamAPICall ) => " );
{
WriteLine( "if ( hSteamAPICall != call ) return;" );
WriteLine();
@ -262,18 +292,18 @@ namespace Generator
}
else
{
WriteLine( $"Callback.ThisCall.Result funcA = ( _, p ) => {{ CallbackFunction( FromPointer( p ), false ); }};" );
WriteLine( $"Callback.ThisCall.ResultWithInfo funcB = ( _, p, bIOFailure, hSteamAPICall ) => {{ CallbackFunction( FromPointer( p ), bIOFailure ); }};" );
WriteLine( $"Callback.{ThisCall}.Result funcA = ( {ThisArgC}p ) => {{ CallbackFunction( FromPointer( p ), false ); }};" );
WriteLine( $"Callback.{ThisCall}.ResultWithInfo funcB = ( {ThisArgC}p, bIOFailure, hSteamAPICall ) => {{ CallbackFunction( FromPointer( p ), bIOFailure ); }};" );
}
WriteLine( $"Callback.ThisCall.GetSize funcC = ( _ ) => Marshal.SizeOf( typeof( {c.Name} ) );" );
WriteLine( $"Callback.{ThisCall}.GetSize funcC = ( {ThisArg} ) => Marshal.SizeOf( typeof( {c.Name} ) );" );
WriteLine();
WriteLine( "//" );
WriteLine( "// If this platform is PackSmall, use PackSmall versions of everything instead" );
WriteLine( "//" );
StartBlock( "if ( Platform.PackSmall )" );
{
WriteLine( $"funcC = ( _ ) => {{ return Marshal.SizeOf( typeof( PackSmall ) ); }};" );
WriteLine( $"funcC = ( {ThisArg} ) => Marshal.SizeOf( typeof( PackSmall ) );" );
}
EndBlock();
@ -309,21 +339,6 @@ namespace Generator
EndBlock();
WriteLine( "Marshal.StructureToPtr( vTable, handle.vTablePtr, false );" );
WriteLine( "" );
WriteLine( "//" );
WriteLine( "// Create the callback object" );
WriteLine( "//" );
WriteLine( $"var cb = new Callback();" );
WriteLine( $"cb.vTablePtr = handle.vTablePtr;" );
WriteLine( $"cb.CallbackFlags = steamworks.IsGameServer ? (byte) SteamNative.Callback.Flags.GameServer : (byte) 0;" );
WriteLine( $"cb.CallbackId = CallbackId;" );
WriteLine( "" );
WriteLine( "//" );
WriteLine( "// Pin the callback, so it doesn't get garbage collected and we can pass the pointer to native" );
WriteLine( "//" );
WriteLine( $"handle.PinnedCallback = GCHandle.Alloc( cb, GCHandleType.Pinned );" );
}
}
}