From bddac0ccc9c2113b03504331e9e22dcda7e9cd51 Mon Sep 17 00:00:00 2001 From: Garry Newman Date: Thu, 20 Jun 2019 11:39:13 +0100 Subject: [PATCH] Cleanup, crash fixes --- Facepunch.Steamworks.Test/InputTest.cs | 3 +- .../Generated/Interfaces/ISteamInput.cs | 36 +++++++++++++++++++ Facepunch.Steamworks/SteamInput.cs | 14 ++++---- Facepunch.Steamworks/Structs/ActionSet.cs | 15 -------- Facepunch.Steamworks/Structs/Controller.cs | 13 +++---- Generator/CodeWriter/Types/BaseType.cs | 6 +++- 6 files changed, 58 insertions(+), 29 deletions(-) delete mode 100644 Facepunch.Steamworks/Structs/ActionSet.cs diff --git a/Facepunch.Steamworks.Test/InputTest.cs b/Facepunch.Steamworks.Test/InputTest.cs index 2e1ed20..0c425db 100644 --- a/Facepunch.Steamworks.Test/InputTest.cs +++ b/Facepunch.Steamworks.Test/InputTest.cs @@ -21,7 +21,8 @@ namespace Steamworks { Console.Write( $"Controller: {controller}" ); - var state = controller.GetDigitalState( "fire" ); + var dstate = controller.GetDigitalState( "fire" ); + var astate = controller.GetAnalogState( "Move" ); } } } diff --git a/Facepunch.Steamworks/Generated/Interfaces/ISteamInput.cs b/Facepunch.Steamworks/Generated/Interfaces/ISteamInput.cs index 2bc2129..34d48ab 100644 --- a/Facepunch.Steamworks/Generated/Interfaces/ISteamInput.cs +++ b/Facepunch.Steamworks/Generated/Interfaces/ISteamInput.cs @@ -26,14 +26,17 @@ namespace Steamworks _GetActiveActionSetLayers = Marshal.GetDelegateForFunctionPointer( Marshal.ReadIntPtr( VTable, 80) ); _GetDigitalActionHandle = Marshal.GetDelegateForFunctionPointer( Marshal.ReadIntPtr( VTable, 88) ); _GetDigitalActionData = Marshal.GetDelegateForFunctionPointer( Marshal.ReadIntPtr( VTable, 96) ); + _GetDigitalActionData_Windows = Marshal.GetDelegateForFunctionPointer( Marshal.ReadIntPtr( VTable, 96) ); _GetDigitalActionOrigins = Marshal.GetDelegateForFunctionPointer( Marshal.ReadIntPtr( VTable, 104) ); _GetAnalogActionHandle = Marshal.GetDelegateForFunctionPointer( Marshal.ReadIntPtr( VTable, 112) ); _GetAnalogActionData = Marshal.GetDelegateForFunctionPointer( Marshal.ReadIntPtr( VTable, 120) ); + _GetAnalogActionData_Windows = Marshal.GetDelegateForFunctionPointer( Marshal.ReadIntPtr( VTable, 120) ); _GetAnalogActionOrigins = Marshal.GetDelegateForFunctionPointer( Marshal.ReadIntPtr( VTable, 128) ); _GetGlyphForActionOrigin = Marshal.GetDelegateForFunctionPointer( Marshal.ReadIntPtr( VTable, 136) ); _GetStringForActionOrigin = Marshal.GetDelegateForFunctionPointer( Marshal.ReadIntPtr( VTable, 144) ); _StopAnalogActionMomentum = Marshal.GetDelegateForFunctionPointer( Marshal.ReadIntPtr( VTable, 152) ); _GetMotionData = Marshal.GetDelegateForFunctionPointer( Marshal.ReadIntPtr( VTable, 160) ); + _GetMotionData_Windows = Marshal.GetDelegateForFunctionPointer( Marshal.ReadIntPtr( VTable, 160) ); _TriggerVibration = Marshal.GetDelegateForFunctionPointer( Marshal.ReadIntPtr( VTable, 168) ); _SetLEDColor = Marshal.GetDelegateForFunctionPointer( Marshal.ReadIntPtr( VTable, 176) ); _TriggerHapticPulse = Marshal.GetDelegateForFunctionPointer( Marshal.ReadIntPtr( VTable, 184) ); @@ -64,14 +67,17 @@ namespace Steamworks _GetActiveActionSetLayers = null; _GetDigitalActionHandle = null; _GetDigitalActionData = null; + _GetDigitalActionData_Windows = null; _GetDigitalActionOrigins = null; _GetAnalogActionHandle = null; _GetAnalogActionData = null; + _GetAnalogActionData_Windows = null; _GetAnalogActionOrigins = null; _GetGlyphForActionOrigin = null; _GetStringForActionOrigin = null; _StopAnalogActionMomentum = null; _GetMotionData = null; + _GetMotionData_Windows = null; _TriggerVibration = null; _SetLEDColor = null; _TriggerHapticPulse = null; @@ -224,10 +230,20 @@ namespace Steamworks [UnmanagedFunctionPointer( CallingConvention.ThisCall )] private delegate DigitalState FGetDigitalActionData( IntPtr self, InputHandle_t inputHandle, InputDigitalActionHandle_t digitalActionHandle ); private FGetDigitalActionData _GetDigitalActionData; + [UnmanagedFunctionPointer( CallingConvention.ThisCall )] + private delegate void FGetDigitalActionData_Windows( IntPtr self, ref DigitalState retVal, InputHandle_t inputHandle, InputDigitalActionHandle_t digitalActionHandle ); + private FGetDigitalActionData_Windows _GetDigitalActionData_Windows; #endregion internal DigitalState GetDigitalActionData( InputHandle_t inputHandle, InputDigitalActionHandle_t digitalActionHandle ) { + if ( Config.Os == OsType.Windows ) + { + var retVal = default( DigitalState ); + _GetDigitalActionData_Windows( Self, ref retVal, inputHandle, digitalActionHandle ); + return retVal; + } + return _GetDigitalActionData( Self, inputHandle, digitalActionHandle ); } @@ -257,10 +273,20 @@ namespace Steamworks [UnmanagedFunctionPointer( CallingConvention.ThisCall )] private delegate AnalogState FGetAnalogActionData( IntPtr self, InputHandle_t inputHandle, InputAnalogActionHandle_t analogActionHandle ); private FGetAnalogActionData _GetAnalogActionData; + [UnmanagedFunctionPointer( CallingConvention.ThisCall )] + private delegate void FGetAnalogActionData_Windows( IntPtr self, ref AnalogState retVal, InputHandle_t inputHandle, InputAnalogActionHandle_t analogActionHandle ); + private FGetAnalogActionData_Windows _GetAnalogActionData_Windows; #endregion internal AnalogState GetAnalogActionData( InputHandle_t inputHandle, InputAnalogActionHandle_t analogActionHandle ) { + if ( Config.Os == OsType.Windows ) + { + var retVal = default( AnalogState ); + _GetAnalogActionData_Windows( Self, ref retVal, inputHandle, analogActionHandle ); + return retVal; + } + return _GetAnalogActionData( Self, inputHandle, analogActionHandle ); } @@ -312,10 +338,20 @@ namespace Steamworks [UnmanagedFunctionPointer( CallingConvention.ThisCall )] private delegate MotionState FGetMotionData( IntPtr self, InputHandle_t inputHandle ); private FGetMotionData _GetMotionData; + [UnmanagedFunctionPointer( CallingConvention.ThisCall )] + private delegate void FGetMotionData_Windows( IntPtr self, ref MotionState retVal, InputHandle_t inputHandle ); + private FGetMotionData_Windows _GetMotionData_Windows; #endregion internal MotionState GetMotionData( InputHandle_t inputHandle ) { + if ( Config.Os == OsType.Windows ) + { + var retVal = default( MotionState ); + _GetMotionData_Windows( Self, ref retVal, inputHandle ); + return retVal; + } + return _GetMotionData( Self, inputHandle ); } diff --git a/Facepunch.Steamworks/SteamInput.cs b/Facepunch.Steamworks/SteamInput.cs index c3c088e..fa5ae92 100644 --- a/Facepunch.Steamworks/SteamInput.cs +++ b/Facepunch.Steamworks/SteamInput.cs @@ -86,13 +86,15 @@ namespace Steamworks return val; } - /// - /// Lookup the handle for an Action Set. Best to do this once on startup, and store the handles for all future API calls. - /// - public static ActionSet GetActionSet( string name ) + internal static Dictionary ActionSets = new Dictionary(); + internal static InputActionSetHandle_t GetActionSetHandle( string name ) { - return new ActionSet( Internal.GetActionSetHandle( name ) ); - } + if ( ActionSets.TryGetValue( name, out var val ) ) + return val; + val = Internal.GetActionSetHandle( name ); + ActionSets.Add( name, val ); + return val; + } } } \ No newline at end of file diff --git a/Facepunch.Steamworks/Structs/ActionSet.cs b/Facepunch.Steamworks/Structs/ActionSet.cs deleted file mode 100644 index 2aa5400..0000000 --- a/Facepunch.Steamworks/Structs/ActionSet.cs +++ /dev/null @@ -1,15 +0,0 @@ -using Steamworks.Data; -using System.Collections.Generic; - -namespace Steamworks -{ - public struct ActionSet - { - internal InputActionSetHandle_t Handle; - - internal ActionSet( InputActionSetHandle_t handle ) - { - this.Handle = handle; - } - } -} \ No newline at end of file diff --git a/Facepunch.Steamworks/Structs/Controller.cs b/Facepunch.Steamworks/Structs/Controller.cs index 6886b74..f694ecd 100644 --- a/Facepunch.Steamworks/Structs/Controller.cs +++ b/Facepunch.Steamworks/Structs/Controller.cs @@ -21,15 +21,14 @@ namespace Steamworks /// This is cheap, and can be safely called repeatedly. It's often easier to repeatedly call it in /// our state loops, instead of trying to place it in all of your state transitions. /// - public ActionSet ActionSet + public string ActionSet { - get => new ActionSet( SteamInput.Internal.GetCurrentActionSet( Handle ) ); - set => SteamInput.Internal.ActivateActionSet( Handle, value.Handle ); + set => SteamInput.Internal.ActivateActionSet( Handle, SteamInput.Internal.GetActionSetHandle( value ) ); } - public void DeactivateLayer( ActionSet layer ) => SteamInput.Internal.DeactivateActionSetLayer( Handle, layer.Handle ); - public void ActivateLayer( ActionSet layer ) => SteamInput.Internal.ActivateActionSetLayer( Handle, layer.Handle ); - public void ClearLayers( ActionSet layer ) => SteamInput.Internal.DeactivateAllActionSetLayers( Handle ); + public void DeactivateLayer( string layer ) => SteamInput.Internal.DeactivateActionSetLayer( Handle, SteamInput.Internal.GetActionSetHandle( layer ) ); + public void ActivateLayer( string layer ) => SteamInput.Internal.ActivateActionSetLayer( Handle, SteamInput.Internal.GetActionSetHandle( layer ) ); + public void ClearLayers() => SteamInput.Internal.DeactivateAllActionSetLayers( Handle ); /// @@ -87,7 +86,9 @@ namespace Steamworks [StructLayout( LayoutKind.Sequential, Pack = 1 )] public struct DigitalState { + [MarshalAs( UnmanagedType.I1 )] internal byte BState; // bState byte + [MarshalAs( UnmanagedType.I1 )] internal byte BActive; // bActive byte public bool Pressed => BState != 0; diff --git a/Generator/CodeWriter/Types/BaseType.cs b/Generator/CodeWriter/Types/BaseType.cs index 7e22c7d..365abbd 100644 --- a/Generator/CodeWriter/Types/BaseType.cs +++ b/Generator/CodeWriter/Types/BaseType.cs @@ -57,6 +57,9 @@ internal class BaseType if ( basicType == "NetIdentity" ) return new StructType { NativeType = type, VarName = varname, StructName = basicType }; if ( basicType == "NetAddress" ) return new StructType { NativeType = type, VarName = varname, StructName = basicType }; if ( basicType == "ConnectionInfo" ) return new StructType { NativeType = type, VarName = varname, StructName = basicType }; + if ( basicType == "DigitalState" ) return new StructType { NativeType = type, VarName = varname, StructName = basicType }; + if ( basicType == "AnalogState" ) return new StructType { NativeType = type, VarName = varname, StructName = basicType }; + if ( basicType == "MotionState" ) return new StructType { NativeType = type, VarName = varname, StructName = basicType }; if ( basicType.StartsWith( "E" ) && char.IsUpper( basicType[1] ) ) return new EnumType { NativeType = type.Substring( 1 ), VarName = varname }; return new BaseType { NativeType = type, VarName = varname }; @@ -164,9 +167,10 @@ internal class StructType : BaseType { "DigitalState", "AnalogState", + "MotionState", }; - public override bool IsReturnedWeird => SpecialTypes.Contains( TypeName ); + public override bool IsReturnedWeird => SpecialTypes.Contains( StructName ); } internal class SteamApiCallType : BaseType