From c37ea381073677e87127bc5475cb0180c1532e29 Mon Sep 17 00:00:00 2001 From: Garry Newman Date: Tue, 25 Oct 2016 16:37:48 +0100 Subject: [PATCH] Cleanup, native classes disposable --- Facepunch.Steamworks/Interop/Native.cs | 92 +++++++++++- .../SteamNative.Platform.Interface.cs | 2 +- .../SteamNative.Platform.Linux32.cs | 11 ++ .../SteamNative.Platform.Linux64.cs | 11 ++ .../SteamNative/SteamNative.Platform.Mac.cs | 11 ++ .../SteamNative/SteamNative.Platform.Win32.cs | 11 ++ .../SteamNative/SteamNative.Platform.Win64.cs | 11 ++ .../SteamNative/SteamNative.SteamApi.cs | 23 ++- .../SteamNative/SteamNative.SteamAppList.cs | 23 ++- .../SteamNative/SteamNative.SteamApps.cs | 23 ++- .../SteamNative/SteamNative.SteamClient.cs | 23 ++- .../SteamNative.SteamController.cs | 23 ++- .../SteamNative/SteamNative.SteamFriends.cs | 23 ++- .../SteamNative.SteamGameServer.cs | 23 ++- .../SteamNative.SteamGameServerStats.cs | 23 ++- .../SteamNative.SteamHTMLSurface.cs | 23 ++- .../SteamNative/SteamNative.SteamHTTP.cs | 23 ++- .../SteamNative/SteamNative.SteamInventory.cs | 23 ++- .../SteamNative.SteamMatchmaking.cs | 23 ++- .../SteamNative.SteamMatchmakingServers.cs | 23 ++- .../SteamNative/SteamNative.SteamMusic.cs | 23 ++- .../SteamNative.SteamMusicRemote.cs | 23 ++- .../SteamNative.SteamNetworking.cs | 23 ++- .../SteamNative.SteamRemoteStorage.cs | 23 ++- .../SteamNative.SteamScreenshots.cs | 23 ++- .../SteamNative/SteamNative.SteamUGC.cs | 23 ++- .../SteamNative.SteamUnifiedMessages.cs | 23 ++- .../SteamNative/SteamNative.SteamUser.cs | 23 ++- .../SteamNative/SteamNative.SteamUserStats.cs | 23 ++- .../SteamNative/SteamNative.SteamUtils.cs | 23 ++- .../SteamNative/SteamNative.SteamVideo.cs | 23 ++- Generator/CodeWriter.PlatformClass.cs | 78 +++++----- Generator/CodeWriter.cs | 2 +- .../{CodeWriter.Classes.cs => Class.cs} | 140 ++++++++++-------- Generator/CodeWriter/Interface.cs | 8 +- Generator/CodeWriter/Utility.cs | 34 +++++ Generator/Generator.csproj | 3 +- 37 files changed, 830 insertions(+), 136 deletions(-) rename Generator/CodeWriter/{CodeWriter.Classes.cs => Class.cs} (82%) create mode 100644 Generator/CodeWriter/Utility.cs diff --git a/Facepunch.Steamworks/Interop/Native.cs b/Facepunch.Steamworks/Interop/Native.cs index 1e71dff..630eb17 100644 --- a/Facepunch.Steamworks/Interop/Native.cs +++ b/Facepunch.Steamworks/Interop/Native.cs @@ -92,10 +92,100 @@ public void Dispose() { if ( client != null ) { + client.Dispose(); client = null; } - api.SteamAPI_Shutdown(); + if ( user != null ) + { + user.Dispose(); + user = null; + } + + if ( utils != null ) + { + utils.Dispose(); + utils = null; + } + + if ( networking != null ) + { + networking.Dispose(); + networking = null; + } + + if ( gameServerStats != null ) + { + gameServerStats.Dispose(); + gameServerStats = null; + } + + if ( http != null ) + { + http.Dispose(); + http = null; + } + + if ( inventory != null ) + { + inventory.Dispose(); + inventory = null; + } + + if ( ugc != null ) + { + ugc.Dispose(); + ugc = null; + } + + if ( apps != null ) + { + apps.Dispose(); + apps = null; + } + + if ( gameServer != null ) + { + gameServer.Dispose(); + gameServer = null; + } + + if ( friends != null ) + { + friends.Dispose(); + friends = null; + } + + if ( servers != null ) + { + servers.Dispose(); + servers = null; + } + + if ( userstats != null ) + { + userstats.Dispose(); + userstats = null; + } + + if ( screenshots != null ) + { + screenshots.Dispose(); + screenshots = null; + } + + if ( remoteStorage != null ) + { + remoteStorage.Dispose(); + remoteStorage = null; + } + + if ( api != null ) + { + api.SteamAPI_Shutdown(); + api.Dispose(); + api = null; + } } } } diff --git a/Facepunch.Steamworks/SteamNative/SteamNative.Platform.Interface.cs b/Facepunch.Steamworks/SteamNative/SteamNative.Platform.Interface.cs index a81265e..202da83 100644 --- a/Facepunch.Steamworks/SteamNative/SteamNative.Platform.Interface.cs +++ b/Facepunch.Steamworks/SteamNative/SteamNative.Platform.Interface.cs @@ -5,7 +5,7 @@ namespace SteamNative { internal static partial class Platform { - public interface Interface + public interface Interface : IDisposable { // Implementation should return true if _ptr is non null bool IsValid { get; } diff --git a/Facepunch.Steamworks/SteamNative/SteamNative.Platform.Linux32.cs b/Facepunch.Steamworks/SteamNative/SteamNative.Platform.Linux32.cs index 32005e8..cf40adb 100644 --- a/Facepunch.Steamworks/SteamNative/SteamNative.Platform.Linux32.cs +++ b/Facepunch.Steamworks/SteamNative/SteamNative.Platform.Linux32.cs @@ -10,10 +10,21 @@ public class Linux32 : Interface internal IntPtr _ptr; public bool IsValid { get{ return _ptr != null; } } + // + // Constructor sets pointer to native class + // public Linux32( IntPtr pointer ) { _ptr = pointer; } + // + // When shutting down clear all the internals to avoid accidental use + // + public virtual void Dispose() + { + _ptr = IntPtr.Zero; + } + public virtual HSteamPipe /*(HSteamPipe)*/ ISteamClient_CreateSteamPipe() { if ( _ptr == null ) throw new System.Exception( "ISteamClient _ptr is null!" ); diff --git a/Facepunch.Steamworks/SteamNative/SteamNative.Platform.Linux64.cs b/Facepunch.Steamworks/SteamNative/SteamNative.Platform.Linux64.cs index 63b949d..640e0b6 100644 --- a/Facepunch.Steamworks/SteamNative/SteamNative.Platform.Linux64.cs +++ b/Facepunch.Steamworks/SteamNative/SteamNative.Platform.Linux64.cs @@ -10,10 +10,21 @@ public class Linux64 : Interface internal IntPtr _ptr; public bool IsValid { get{ return _ptr != null; } } + // + // Constructor sets pointer to native class + // public Linux64( IntPtr pointer ) { _ptr = pointer; } + // + // When shutting down clear all the internals to avoid accidental use + // + public virtual void Dispose() + { + _ptr = IntPtr.Zero; + } + public virtual HSteamPipe /*(HSteamPipe)*/ ISteamClient_CreateSteamPipe() { if ( _ptr == null ) throw new System.Exception( "ISteamClient _ptr is null!" ); diff --git a/Facepunch.Steamworks/SteamNative/SteamNative.Platform.Mac.cs b/Facepunch.Steamworks/SteamNative/SteamNative.Platform.Mac.cs index adb6dd4..1a6127b 100644 --- a/Facepunch.Steamworks/SteamNative/SteamNative.Platform.Mac.cs +++ b/Facepunch.Steamworks/SteamNative/SteamNative.Platform.Mac.cs @@ -10,10 +10,21 @@ public class Mac : Interface internal IntPtr _ptr; public bool IsValid { get{ return _ptr != null; } } + // + // Constructor sets pointer to native class + // public Mac( IntPtr pointer ) { _ptr = pointer; } + // + // When shutting down clear all the internals to avoid accidental use + // + public virtual void Dispose() + { + _ptr = IntPtr.Zero; + } + public virtual HSteamPipe /*(HSteamPipe)*/ ISteamClient_CreateSteamPipe() { if ( _ptr == null ) throw new System.Exception( "ISteamClient _ptr is null!" ); diff --git a/Facepunch.Steamworks/SteamNative/SteamNative.Platform.Win32.cs b/Facepunch.Steamworks/SteamNative/SteamNative.Platform.Win32.cs index 0d99ff7..ea0c9ab 100644 --- a/Facepunch.Steamworks/SteamNative/SteamNative.Platform.Win32.cs +++ b/Facepunch.Steamworks/SteamNative/SteamNative.Platform.Win32.cs @@ -10,10 +10,21 @@ public class Win32 : Interface internal IntPtr _ptr; public bool IsValid { get{ return _ptr != null; } } + // + // Constructor sets pointer to native class + // public Win32( IntPtr pointer ) { _ptr = pointer; } + // + // When shutting down clear all the internals to avoid accidental use + // + public virtual void Dispose() + { + _ptr = IntPtr.Zero; + } + public virtual HSteamPipe /*(HSteamPipe)*/ ISteamClient_CreateSteamPipe() { if ( _ptr == null ) throw new System.Exception( "ISteamClient _ptr is null!" ); diff --git a/Facepunch.Steamworks/SteamNative/SteamNative.Platform.Win64.cs b/Facepunch.Steamworks/SteamNative/SteamNative.Platform.Win64.cs index b7c717b..a5ece80 100644 --- a/Facepunch.Steamworks/SteamNative/SteamNative.Platform.Win64.cs +++ b/Facepunch.Steamworks/SteamNative/SteamNative.Platform.Win64.cs @@ -10,10 +10,21 @@ public class Win64 : Interface internal IntPtr _ptr; public bool IsValid { get{ return _ptr != null; } } + // + // Constructor sets pointer to native class + // public Win64( IntPtr pointer ) { _ptr = pointer; } + // + // When shutting down clear all the internals to avoid accidental use + // + public virtual void Dispose() + { + _ptr = IntPtr.Zero; + } + public virtual HSteamPipe /*(HSteamPipe)*/ ISteamClient_CreateSteamPipe() { if ( _ptr == null ) throw new System.Exception( "ISteamClient _ptr is null!" ); diff --git a/Facepunch.Steamworks/SteamNative/SteamNative.SteamApi.cs b/Facepunch.Steamworks/SteamNative/SteamNative.SteamApi.cs index 5be57f4..229aa66 100644 --- a/Facepunch.Steamworks/SteamNative/SteamNative.SteamApi.cs +++ b/Facepunch.Steamworks/SteamNative/SteamNative.SteamApi.cs @@ -3,10 +3,16 @@ namespace SteamNative { - public unsafe class SteamApi + public unsafe class SteamApi : IDisposable { + // + // Holds a platform specific implentation + // internal Platform.Interface _pi; + // + // Constructor decides which implementation to use based on current platform + // public SteamApi( IntPtr pointer ) { if ( Platform.IsWindows64 ) _pi = new Platform.Win64( pointer ); @@ -16,8 +22,23 @@ public SteamApi( IntPtr pointer ) else if ( Platform.IsOsx ) _pi = new Platform.Mac( pointer ); } + // + // Class is invalid if we don't have a valid implementation + // public bool IsValid{ get{ return _pi != null && _pi.IsValid; } } + // + // When shutting down clear all the internals to avoid accidental use + // + public virtual void Dispose() + { + if ( _pi != null ) + { + _pi.Dispose(); + _pi = null; + } + } + // HSteamPipe public HSteamPipe SteamAPI_GetHSteamPipe() { diff --git a/Facepunch.Steamworks/SteamNative/SteamNative.SteamAppList.cs b/Facepunch.Steamworks/SteamNative/SteamNative.SteamAppList.cs index d8161ed..1d693c6 100644 --- a/Facepunch.Steamworks/SteamNative/SteamNative.SteamAppList.cs +++ b/Facepunch.Steamworks/SteamNative/SteamNative.SteamAppList.cs @@ -3,10 +3,16 @@ namespace SteamNative { - public unsafe class SteamAppList + public unsafe class SteamAppList : IDisposable { + // + // Holds a platform specific implentation + // internal Platform.Interface _pi; + // + // Constructor decides which implementation to use based on current platform + // public SteamAppList( IntPtr pointer ) { if ( Platform.IsWindows64 ) _pi = new Platform.Win64( pointer ); @@ -16,8 +22,23 @@ public SteamAppList( IntPtr pointer ) else if ( Platform.IsOsx ) _pi = new Platform.Mac( pointer ); } + // + // Class is invalid if we don't have a valid implementation + // public bool IsValid{ get{ return _pi != null && _pi.IsValid; } } + // + // When shutting down clear all the internals to avoid accidental use + // + public virtual void Dispose() + { + if ( _pi != null ) + { + _pi.Dispose(); + _pi = null; + } + } + // int public int GetAppBuildId( AppId_t nAppID /*AppId_t*/ ) { diff --git a/Facepunch.Steamworks/SteamNative/SteamNative.SteamApps.cs b/Facepunch.Steamworks/SteamNative/SteamNative.SteamApps.cs index 9b0f93b..64bf902 100644 --- a/Facepunch.Steamworks/SteamNative/SteamNative.SteamApps.cs +++ b/Facepunch.Steamworks/SteamNative/SteamNative.SteamApps.cs @@ -3,10 +3,16 @@ namespace SteamNative { - public unsafe class SteamApps + public unsafe class SteamApps : IDisposable { + // + // Holds a platform specific implentation + // internal Platform.Interface _pi; + // + // Constructor decides which implementation to use based on current platform + // public SteamApps( IntPtr pointer ) { if ( Platform.IsWindows64 ) _pi = new Platform.Win64( pointer ); @@ -16,8 +22,23 @@ public SteamApps( IntPtr pointer ) else if ( Platform.IsOsx ) _pi = new Platform.Mac( pointer ); } + // + // Class is invalid if we don't have a valid implementation + // public bool IsValid{ get{ return _pi != null && _pi.IsValid; } } + // + // When shutting down clear all the internals to avoid accidental use + // + public virtual void Dispose() + { + if ( _pi != null ) + { + _pi.Dispose(); + _pi = null; + } + } + // bool // with: Detect_StringFetch False public bool BGetDLCDataByIndex( int iDLC /*int*/, ref AppId_t pAppID /*AppId_t **/, out bool pbAvailable /*bool **/, out string pchName /*char **/ ) diff --git a/Facepunch.Steamworks/SteamNative/SteamNative.SteamClient.cs b/Facepunch.Steamworks/SteamNative/SteamNative.SteamClient.cs index 1d35a1f..d8bd3e5 100644 --- a/Facepunch.Steamworks/SteamNative/SteamNative.SteamClient.cs +++ b/Facepunch.Steamworks/SteamNative/SteamNative.SteamClient.cs @@ -3,10 +3,16 @@ namespace SteamNative { - public unsafe class SteamClient + public unsafe class SteamClient : IDisposable { + // + // Holds a platform specific implentation + // internal Platform.Interface _pi; + // + // Constructor decides which implementation to use based on current platform + // public SteamClient( IntPtr pointer ) { if ( Platform.IsWindows64 ) _pi = new Platform.Win64( pointer ); @@ -16,8 +22,23 @@ public SteamClient( IntPtr pointer ) else if ( Platform.IsOsx ) _pi = new Platform.Mac( pointer ); } + // + // Class is invalid if we don't have a valid implementation + // public bool IsValid{ get{ return _pi != null && _pi.IsValid; } } + // + // When shutting down clear all the internals to avoid accidental use + // + public virtual void Dispose() + { + if ( _pi != null ) + { + _pi.Dispose(); + _pi = null; + } + } + // bool public bool BReleaseSteamPipe( HSteamPipe hSteamPipe /*HSteamPipe*/ ) { diff --git a/Facepunch.Steamworks/SteamNative/SteamNative.SteamController.cs b/Facepunch.Steamworks/SteamNative/SteamNative.SteamController.cs index b25694d..a98e787 100644 --- a/Facepunch.Steamworks/SteamNative/SteamNative.SteamController.cs +++ b/Facepunch.Steamworks/SteamNative/SteamNative.SteamController.cs @@ -3,10 +3,16 @@ namespace SteamNative { - public unsafe class SteamController + public unsafe class SteamController : IDisposable { + // + // Holds a platform specific implentation + // internal Platform.Interface _pi; + // + // Constructor decides which implementation to use based on current platform + // public SteamController( IntPtr pointer ) { if ( Platform.IsWindows64 ) _pi = new Platform.Win64( pointer ); @@ -16,8 +22,23 @@ public SteamController( IntPtr pointer ) else if ( Platform.IsOsx ) _pi = new Platform.Mac( pointer ); } + // + // Class is invalid if we don't have a valid implementation + // public bool IsValid{ get{ return _pi != null && _pi.IsValid; } } + // + // When shutting down clear all the internals to avoid accidental use + // + public virtual void Dispose() + { + if ( _pi != null ) + { + _pi.Dispose(); + _pi = null; + } + } + // void public void ActivateActionSet( ControllerHandle_t controllerHandle /*ControllerHandle_t*/, ControllerActionSetHandle_t actionSetHandle /*ControllerActionSetHandle_t*/ ) { diff --git a/Facepunch.Steamworks/SteamNative/SteamNative.SteamFriends.cs b/Facepunch.Steamworks/SteamNative/SteamNative.SteamFriends.cs index d977937..a8deb97 100644 --- a/Facepunch.Steamworks/SteamNative/SteamNative.SteamFriends.cs +++ b/Facepunch.Steamworks/SteamNative/SteamNative.SteamFriends.cs @@ -3,10 +3,16 @@ namespace SteamNative { - public unsafe class SteamFriends + public unsafe class SteamFriends : IDisposable { + // + // Holds a platform specific implentation + // internal Platform.Interface _pi; + // + // Constructor decides which implementation to use based on current platform + // public SteamFriends( IntPtr pointer ) { if ( Platform.IsWindows64 ) _pi = new Platform.Win64( pointer ); @@ -16,8 +22,23 @@ public SteamFriends( IntPtr pointer ) else if ( Platform.IsOsx ) _pi = new Platform.Mac( pointer ); } + // + // Class is invalid if we don't have a valid implementation + // public bool IsValid{ get{ return _pi != null && _pi.IsValid; } } + // + // When shutting down clear all the internals to avoid accidental use + // + public virtual void Dispose() + { + if ( _pi != null ) + { + _pi.Dispose(); + _pi = null; + } + } + // void public void ActivateGameOverlay( string pchDialog /*const char **/ ) { diff --git a/Facepunch.Steamworks/SteamNative/SteamNative.SteamGameServer.cs b/Facepunch.Steamworks/SteamNative/SteamNative.SteamGameServer.cs index 148c7eb..6a08eb1 100644 --- a/Facepunch.Steamworks/SteamNative/SteamNative.SteamGameServer.cs +++ b/Facepunch.Steamworks/SteamNative/SteamNative.SteamGameServer.cs @@ -3,10 +3,16 @@ namespace SteamNative { - public unsafe class SteamGameServer + public unsafe class SteamGameServer : IDisposable { + // + // Holds a platform specific implentation + // internal Platform.Interface _pi; + // + // Constructor decides which implementation to use based on current platform + // public SteamGameServer( IntPtr pointer ) { if ( Platform.IsWindows64 ) _pi = new Platform.Win64( pointer ); @@ -16,8 +22,23 @@ public SteamGameServer( IntPtr pointer ) else if ( Platform.IsOsx ) _pi = new Platform.Mac( pointer ); } + // + // Class is invalid if we don't have a valid implementation + // public bool IsValid{ get{ return _pi != null && _pi.IsValid; } } + // + // When shutting down clear all the internals to avoid accidental use + // + public virtual void Dispose() + { + if ( _pi != null ) + { + _pi.Dispose(); + _pi = null; + } + } + // SteamAPICall_t public SteamAPICall_t AssociateWithClan( CSteamID steamIDClan /*class CSteamID*/ ) { diff --git a/Facepunch.Steamworks/SteamNative/SteamNative.SteamGameServerStats.cs b/Facepunch.Steamworks/SteamNative/SteamNative.SteamGameServerStats.cs index 9088d4d..2bc256c 100644 --- a/Facepunch.Steamworks/SteamNative/SteamNative.SteamGameServerStats.cs +++ b/Facepunch.Steamworks/SteamNative/SteamNative.SteamGameServerStats.cs @@ -3,10 +3,16 @@ namespace SteamNative { - public unsafe class SteamGameServerStats + public unsafe class SteamGameServerStats : IDisposable { + // + // Holds a platform specific implentation + // internal Platform.Interface _pi; + // + // Constructor decides which implementation to use based on current platform + // public SteamGameServerStats( IntPtr pointer ) { if ( Platform.IsWindows64 ) _pi = new Platform.Win64( pointer ); @@ -16,8 +22,23 @@ public SteamGameServerStats( IntPtr pointer ) else if ( Platform.IsOsx ) _pi = new Platform.Mac( pointer ); } + // + // Class is invalid if we don't have a valid implementation + // public bool IsValid{ get{ return _pi != null && _pi.IsValid; } } + // + // When shutting down clear all the internals to avoid accidental use + // + public virtual void Dispose() + { + if ( _pi != null ) + { + _pi.Dispose(); + _pi = null; + } + } + // bool public bool ClearUserAchievement( CSteamID steamIDUser /*class CSteamID*/, string pchName /*const char **/ ) { diff --git a/Facepunch.Steamworks/SteamNative/SteamNative.SteamHTMLSurface.cs b/Facepunch.Steamworks/SteamNative/SteamNative.SteamHTMLSurface.cs index c77ad61..348ac75 100644 --- a/Facepunch.Steamworks/SteamNative/SteamNative.SteamHTMLSurface.cs +++ b/Facepunch.Steamworks/SteamNative/SteamNative.SteamHTMLSurface.cs @@ -3,10 +3,16 @@ namespace SteamNative { - public unsafe class SteamHTMLSurface + public unsafe class SteamHTMLSurface : IDisposable { + // + // Holds a platform specific implentation + // internal Platform.Interface _pi; + // + // Constructor decides which implementation to use based on current platform + // public SteamHTMLSurface( IntPtr pointer ) { if ( Platform.IsWindows64 ) _pi = new Platform.Win64( pointer ); @@ -16,8 +22,23 @@ public SteamHTMLSurface( IntPtr pointer ) else if ( Platform.IsOsx ) _pi = new Platform.Mac( pointer ); } + // + // Class is invalid if we don't have a valid implementation + // public bool IsValid{ get{ return _pi != null && _pi.IsValid; } } + // + // When shutting down clear all the internals to avoid accidental use + // + public virtual void Dispose() + { + if ( _pi != null ) + { + _pi.Dispose(); + _pi = null; + } + } + // void public void AddHeader( HHTMLBrowser unBrowserHandle /*HHTMLBrowser*/, string pchKey /*const char **/, string pchValue /*const char **/ ) { diff --git a/Facepunch.Steamworks/SteamNative/SteamNative.SteamHTTP.cs b/Facepunch.Steamworks/SteamNative/SteamNative.SteamHTTP.cs index 91fe066..e0e511c 100644 --- a/Facepunch.Steamworks/SteamNative/SteamNative.SteamHTTP.cs +++ b/Facepunch.Steamworks/SteamNative/SteamNative.SteamHTTP.cs @@ -3,10 +3,16 @@ namespace SteamNative { - public unsafe class SteamHTTP + public unsafe class SteamHTTP : IDisposable { + // + // Holds a platform specific implentation + // internal Platform.Interface _pi; + // + // Constructor decides which implementation to use based on current platform + // public SteamHTTP( IntPtr pointer ) { if ( Platform.IsWindows64 ) _pi = new Platform.Win64( pointer ); @@ -16,8 +22,23 @@ public SteamHTTP( IntPtr pointer ) else if ( Platform.IsOsx ) _pi = new Platform.Mac( pointer ); } + // + // Class is invalid if we don't have a valid implementation + // public bool IsValid{ get{ return _pi != null && _pi.IsValid; } } + // + // When shutting down clear all the internals to avoid accidental use + // + public virtual void Dispose() + { + if ( _pi != null ) + { + _pi.Dispose(); + _pi = null; + } + } + // HTTPCookieContainerHandle public HTTPCookieContainerHandle CreateCookieContainer( bool bAllowResponsesToModify /*bool*/ ) { diff --git a/Facepunch.Steamworks/SteamNative/SteamNative.SteamInventory.cs b/Facepunch.Steamworks/SteamNative/SteamNative.SteamInventory.cs index 2f155fd..9bf51fe 100644 --- a/Facepunch.Steamworks/SteamNative/SteamNative.SteamInventory.cs +++ b/Facepunch.Steamworks/SteamNative/SteamNative.SteamInventory.cs @@ -3,10 +3,16 @@ namespace SteamNative { - public unsafe class SteamInventory + public unsafe class SteamInventory : IDisposable { + // + // Holds a platform specific implentation + // internal Platform.Interface _pi; + // + // Constructor decides which implementation to use based on current platform + // public SteamInventory( IntPtr pointer ) { if ( Platform.IsWindows64 ) _pi = new Platform.Win64( pointer ); @@ -16,8 +22,23 @@ public SteamInventory( IntPtr pointer ) else if ( Platform.IsOsx ) _pi = new Platform.Mac( pointer ); } + // + // Class is invalid if we don't have a valid implementation + // public bool IsValid{ get{ return _pi != null && _pi.IsValid; } } + // + // When shutting down clear all the internals to avoid accidental use + // + public virtual void Dispose() + { + if ( _pi != null ) + { + _pi.Dispose(); + _pi = null; + } + } + // bool public bool AddPromoItem( ref SteamInventoryResult_t pResultHandle /*SteamInventoryResult_t **/, SteamItemDef_t itemDef /*SteamItemDef_t*/ ) { diff --git a/Facepunch.Steamworks/SteamNative/SteamNative.SteamMatchmaking.cs b/Facepunch.Steamworks/SteamNative/SteamNative.SteamMatchmaking.cs index ab3c352..7239dae 100644 --- a/Facepunch.Steamworks/SteamNative/SteamNative.SteamMatchmaking.cs +++ b/Facepunch.Steamworks/SteamNative/SteamNative.SteamMatchmaking.cs @@ -3,10 +3,16 @@ namespace SteamNative { - public unsafe class SteamMatchmaking + public unsafe class SteamMatchmaking : IDisposable { + // + // Holds a platform specific implentation + // internal Platform.Interface _pi; + // + // Constructor decides which implementation to use based on current platform + // public SteamMatchmaking( IntPtr pointer ) { if ( Platform.IsWindows64 ) _pi = new Platform.Win64( pointer ); @@ -16,8 +22,23 @@ public SteamMatchmaking( IntPtr pointer ) else if ( Platform.IsOsx ) _pi = new Platform.Mac( pointer ); } + // + // Class is invalid if we don't have a valid implementation + // public bool IsValid{ get{ return _pi != null && _pi.IsValid; } } + // + // When shutting down clear all the internals to avoid accidental use + // + public virtual void Dispose() + { + if ( _pi != null ) + { + _pi.Dispose(); + _pi = null; + } + } + // int public int AddFavoriteGame( AppId_t nAppID /*AppId_t*/, uint nIP /*uint32*/, ushort nConnPort /*uint16*/, ushort nQueryPort /*uint16*/, uint unFlags /*uint32*/, uint rTime32LastPlayedOnServer /*uint32*/ ) { diff --git a/Facepunch.Steamworks/SteamNative/SteamNative.SteamMatchmakingServers.cs b/Facepunch.Steamworks/SteamNative/SteamNative.SteamMatchmakingServers.cs index c366788..830589f 100644 --- a/Facepunch.Steamworks/SteamNative/SteamNative.SteamMatchmakingServers.cs +++ b/Facepunch.Steamworks/SteamNative/SteamNative.SteamMatchmakingServers.cs @@ -3,10 +3,16 @@ namespace SteamNative { - public unsafe class SteamMatchmakingServers + public unsafe class SteamMatchmakingServers : IDisposable { + // + // Holds a platform specific implentation + // internal Platform.Interface _pi; + // + // Constructor decides which implementation to use based on current platform + // public SteamMatchmakingServers( IntPtr pointer ) { if ( Platform.IsWindows64 ) _pi = new Platform.Win64( pointer ); @@ -16,8 +22,23 @@ public SteamMatchmakingServers( IntPtr pointer ) else if ( Platform.IsOsx ) _pi = new Platform.Mac( pointer ); } + // + // Class is invalid if we don't have a valid implementation + // public bool IsValid{ get{ return _pi != null && _pi.IsValid; } } + // + // When shutting down clear all the internals to avoid accidental use + // + public virtual void Dispose() + { + if ( _pi != null ) + { + _pi.Dispose(); + _pi = null; + } + } + // void public void CancelQuery( HServerListRequest hRequest /*HServerListRequest*/ ) { diff --git a/Facepunch.Steamworks/SteamNative/SteamNative.SteamMusic.cs b/Facepunch.Steamworks/SteamNative/SteamNative.SteamMusic.cs index 434f7bb..3ae066f 100644 --- a/Facepunch.Steamworks/SteamNative/SteamNative.SteamMusic.cs +++ b/Facepunch.Steamworks/SteamNative/SteamNative.SteamMusic.cs @@ -3,10 +3,16 @@ namespace SteamNative { - public unsafe class SteamMusic + public unsafe class SteamMusic : IDisposable { + // + // Holds a platform specific implentation + // internal Platform.Interface _pi; + // + // Constructor decides which implementation to use based on current platform + // public SteamMusic( IntPtr pointer ) { if ( Platform.IsWindows64 ) _pi = new Platform.Win64( pointer ); @@ -16,8 +22,23 @@ public SteamMusic( IntPtr pointer ) else if ( Platform.IsOsx ) _pi = new Platform.Mac( pointer ); } + // + // Class is invalid if we don't have a valid implementation + // public bool IsValid{ get{ return _pi != null && _pi.IsValid; } } + // + // When shutting down clear all the internals to avoid accidental use + // + public virtual void Dispose() + { + if ( _pi != null ) + { + _pi.Dispose(); + _pi = null; + } + } + // bool public bool BIsEnabled() { diff --git a/Facepunch.Steamworks/SteamNative/SteamNative.SteamMusicRemote.cs b/Facepunch.Steamworks/SteamNative/SteamNative.SteamMusicRemote.cs index c538363..3a80957 100644 --- a/Facepunch.Steamworks/SteamNative/SteamNative.SteamMusicRemote.cs +++ b/Facepunch.Steamworks/SteamNative/SteamNative.SteamMusicRemote.cs @@ -3,10 +3,16 @@ namespace SteamNative { - public unsafe class SteamMusicRemote + public unsafe class SteamMusicRemote : IDisposable { + // + // Holds a platform specific implentation + // internal Platform.Interface _pi; + // + // Constructor decides which implementation to use based on current platform + // public SteamMusicRemote( IntPtr pointer ) { if ( Platform.IsWindows64 ) _pi = new Platform.Win64( pointer ); @@ -16,8 +22,23 @@ public SteamMusicRemote( IntPtr pointer ) else if ( Platform.IsOsx ) _pi = new Platform.Mac( pointer ); } + // + // Class is invalid if we don't have a valid implementation + // public bool IsValid{ get{ return _pi != null && _pi.IsValid; } } + // + // When shutting down clear all the internals to avoid accidental use + // + public virtual void Dispose() + { + if ( _pi != null ) + { + _pi.Dispose(); + _pi = null; + } + } + // bool public bool BActivationSuccess( bool bValue /*bool*/ ) { diff --git a/Facepunch.Steamworks/SteamNative/SteamNative.SteamNetworking.cs b/Facepunch.Steamworks/SteamNative/SteamNative.SteamNetworking.cs index 8f0911e..e1fb375 100644 --- a/Facepunch.Steamworks/SteamNative/SteamNative.SteamNetworking.cs +++ b/Facepunch.Steamworks/SteamNative/SteamNative.SteamNetworking.cs @@ -3,10 +3,16 @@ namespace SteamNative { - public unsafe class SteamNetworking + public unsafe class SteamNetworking : IDisposable { + // + // Holds a platform specific implentation + // internal Platform.Interface _pi; + // + // Constructor decides which implementation to use based on current platform + // public SteamNetworking( IntPtr pointer ) { if ( Platform.IsWindows64 ) _pi = new Platform.Win64( pointer ); @@ -16,8 +22,23 @@ public SteamNetworking( IntPtr pointer ) else if ( Platform.IsOsx ) _pi = new Platform.Mac( pointer ); } + // + // Class is invalid if we don't have a valid implementation + // public bool IsValid{ get{ return _pi != null && _pi.IsValid; } } + // + // When shutting down clear all the internals to avoid accidental use + // + public virtual void Dispose() + { + if ( _pi != null ) + { + _pi.Dispose(); + _pi = null; + } + } + // bool public bool AcceptP2PSessionWithUser( CSteamID steamIDRemote /*class CSteamID*/ ) { diff --git a/Facepunch.Steamworks/SteamNative/SteamNative.SteamRemoteStorage.cs b/Facepunch.Steamworks/SteamNative/SteamNative.SteamRemoteStorage.cs index abdf564..9c67a9b 100644 --- a/Facepunch.Steamworks/SteamNative/SteamNative.SteamRemoteStorage.cs +++ b/Facepunch.Steamworks/SteamNative/SteamNative.SteamRemoteStorage.cs @@ -3,10 +3,16 @@ namespace SteamNative { - public unsafe class SteamRemoteStorage + public unsafe class SteamRemoteStorage : IDisposable { + // + // Holds a platform specific implentation + // internal Platform.Interface _pi; + // + // Constructor decides which implementation to use based on current platform + // public SteamRemoteStorage( IntPtr pointer ) { if ( Platform.IsWindows64 ) _pi = new Platform.Win64( pointer ); @@ -16,8 +22,23 @@ public SteamRemoteStorage( IntPtr pointer ) else if ( Platform.IsOsx ) _pi = new Platform.Mac( pointer ); } + // + // Class is invalid if we don't have a valid implementation + // public bool IsValid{ get{ return _pi != null && _pi.IsValid; } } + // + // When shutting down clear all the internals to avoid accidental use + // + public virtual void Dispose() + { + if ( _pi != null ) + { + _pi.Dispose(); + _pi = null; + } + } + // SteamAPICall_t public SteamAPICall_t CommitPublishedFileUpdate( PublishedFileUpdateHandle_t updateHandle /*PublishedFileUpdateHandle_t*/ ) { diff --git a/Facepunch.Steamworks/SteamNative/SteamNative.SteamScreenshots.cs b/Facepunch.Steamworks/SteamNative/SteamNative.SteamScreenshots.cs index c7a00d5..dc04091 100644 --- a/Facepunch.Steamworks/SteamNative/SteamNative.SteamScreenshots.cs +++ b/Facepunch.Steamworks/SteamNative/SteamNative.SteamScreenshots.cs @@ -3,10 +3,16 @@ namespace SteamNative { - public unsafe class SteamScreenshots + public unsafe class SteamScreenshots : IDisposable { + // + // Holds a platform specific implentation + // internal Platform.Interface _pi; + // + // Constructor decides which implementation to use based on current platform + // public SteamScreenshots( IntPtr pointer ) { if ( Platform.IsWindows64 ) _pi = new Platform.Win64( pointer ); @@ -16,8 +22,23 @@ public SteamScreenshots( IntPtr pointer ) else if ( Platform.IsOsx ) _pi = new Platform.Mac( pointer ); } + // + // Class is invalid if we don't have a valid implementation + // public bool IsValid{ get{ return _pi != null && _pi.IsValid; } } + // + // When shutting down clear all the internals to avoid accidental use + // + public virtual void Dispose() + { + if ( _pi != null ) + { + _pi.Dispose(); + _pi = null; + } + } + // ScreenshotHandle public ScreenshotHandle AddScreenshotToLibrary( string pchFilename /*const char **/, string pchThumbnailFilename /*const char **/, int nWidth /*int*/, int nHeight /*int*/ ) { diff --git a/Facepunch.Steamworks/SteamNative/SteamNative.SteamUGC.cs b/Facepunch.Steamworks/SteamNative/SteamNative.SteamUGC.cs index 79ceae1..0c07bae 100644 --- a/Facepunch.Steamworks/SteamNative/SteamNative.SteamUGC.cs +++ b/Facepunch.Steamworks/SteamNative/SteamNative.SteamUGC.cs @@ -3,10 +3,16 @@ namespace SteamNative { - public unsafe class SteamUGC + public unsafe class SteamUGC : IDisposable { + // + // Holds a platform specific implentation + // internal Platform.Interface _pi; + // + // Constructor decides which implementation to use based on current platform + // public SteamUGC( IntPtr pointer ) { if ( Platform.IsWindows64 ) _pi = new Platform.Win64( pointer ); @@ -16,8 +22,23 @@ public SteamUGC( IntPtr pointer ) else if ( Platform.IsOsx ) _pi = new Platform.Mac( pointer ); } + // + // Class is invalid if we don't have a valid implementation + // public bool IsValid{ get{ return _pi != null && _pi.IsValid; } } + // + // When shutting down clear all the internals to avoid accidental use + // + public virtual void Dispose() + { + if ( _pi != null ) + { + _pi.Dispose(); + _pi = null; + } + } + // bool public bool AddExcludedTag( UGCQueryHandle_t handle /*UGCQueryHandle_t*/, string pTagName /*const char **/ ) { diff --git a/Facepunch.Steamworks/SteamNative/SteamNative.SteamUnifiedMessages.cs b/Facepunch.Steamworks/SteamNative/SteamNative.SteamUnifiedMessages.cs index 90ff2c8..e08f7db 100644 --- a/Facepunch.Steamworks/SteamNative/SteamNative.SteamUnifiedMessages.cs +++ b/Facepunch.Steamworks/SteamNative/SteamNative.SteamUnifiedMessages.cs @@ -3,10 +3,16 @@ namespace SteamNative { - public unsafe class SteamUnifiedMessages + public unsafe class SteamUnifiedMessages : IDisposable { + // + // Holds a platform specific implentation + // internal Platform.Interface _pi; + // + // Constructor decides which implementation to use based on current platform + // public SteamUnifiedMessages( IntPtr pointer ) { if ( Platform.IsWindows64 ) _pi = new Platform.Win64( pointer ); @@ -16,8 +22,23 @@ public SteamUnifiedMessages( IntPtr pointer ) else if ( Platform.IsOsx ) _pi = new Platform.Mac( pointer ); } + // + // Class is invalid if we don't have a valid implementation + // public bool IsValid{ get{ return _pi != null && _pi.IsValid; } } + // + // When shutting down clear all the internals to avoid accidental use + // + public virtual void Dispose() + { + if ( _pi != null ) + { + _pi.Dispose(); + _pi = null; + } + } + // bool public bool GetMethodResponseData( ClientUnifiedMessageHandle hHandle /*ClientUnifiedMessageHandle*/, IntPtr pResponseBuffer /*void **/, uint unResponseBufferSize /*uint32*/, bool bAutoRelease /*bool*/ ) { diff --git a/Facepunch.Steamworks/SteamNative/SteamNative.SteamUser.cs b/Facepunch.Steamworks/SteamNative/SteamNative.SteamUser.cs index 01bb41e..7948e38 100644 --- a/Facepunch.Steamworks/SteamNative/SteamNative.SteamUser.cs +++ b/Facepunch.Steamworks/SteamNative/SteamNative.SteamUser.cs @@ -3,10 +3,16 @@ namespace SteamNative { - public unsafe class SteamUser + public unsafe class SteamUser : IDisposable { + // + // Holds a platform specific implentation + // internal Platform.Interface _pi; + // + // Constructor decides which implementation to use based on current platform + // public SteamUser( IntPtr pointer ) { if ( Platform.IsWindows64 ) _pi = new Platform.Win64( pointer ); @@ -16,8 +22,23 @@ public SteamUser( IntPtr pointer ) else if ( Platform.IsOsx ) _pi = new Platform.Mac( pointer ); } + // + // Class is invalid if we don't have a valid implementation + // public bool IsValid{ get{ return _pi != null && _pi.IsValid; } } + // + // When shutting down clear all the internals to avoid accidental use + // + public virtual void Dispose() + { + if ( _pi != null ) + { + _pi.Dispose(); + _pi = null; + } + } + // void public void AdvertiseGame( CSteamID steamIDGameServer /*class CSteamID*/, uint unIPServer /*uint32*/, ushort usPortServer /*uint16*/ ) { diff --git a/Facepunch.Steamworks/SteamNative/SteamNative.SteamUserStats.cs b/Facepunch.Steamworks/SteamNative/SteamNative.SteamUserStats.cs index 2820e4d..0fca57e 100644 --- a/Facepunch.Steamworks/SteamNative/SteamNative.SteamUserStats.cs +++ b/Facepunch.Steamworks/SteamNative/SteamNative.SteamUserStats.cs @@ -3,10 +3,16 @@ namespace SteamNative { - public unsafe class SteamUserStats + public unsafe class SteamUserStats : IDisposable { + // + // Holds a platform specific implentation + // internal Platform.Interface _pi; + // + // Constructor decides which implementation to use based on current platform + // public SteamUserStats( IntPtr pointer ) { if ( Platform.IsWindows64 ) _pi = new Platform.Win64( pointer ); @@ -16,8 +22,23 @@ public SteamUserStats( IntPtr pointer ) else if ( Platform.IsOsx ) _pi = new Platform.Mac( pointer ); } + // + // Class is invalid if we don't have a valid implementation + // public bool IsValid{ get{ return _pi != null && _pi.IsValid; } } + // + // When shutting down clear all the internals to avoid accidental use + // + public virtual void Dispose() + { + if ( _pi != null ) + { + _pi.Dispose(); + _pi = null; + } + } + // SteamAPICall_t public SteamAPICall_t AttachLeaderboardUGC( SteamLeaderboard_t hSteamLeaderboard /*SteamLeaderboard_t*/, UGCHandle_t hUGC /*UGCHandle_t*/ ) { diff --git a/Facepunch.Steamworks/SteamNative/SteamNative.SteamUtils.cs b/Facepunch.Steamworks/SteamNative/SteamNative.SteamUtils.cs index 20fc278..3ebb6ab 100644 --- a/Facepunch.Steamworks/SteamNative/SteamNative.SteamUtils.cs +++ b/Facepunch.Steamworks/SteamNative/SteamNative.SteamUtils.cs @@ -3,10 +3,16 @@ namespace SteamNative { - public unsafe class SteamUtils + public unsafe class SteamUtils : IDisposable { + // + // Holds a platform specific implentation + // internal Platform.Interface _pi; + // + // Constructor decides which implementation to use based on current platform + // public SteamUtils( IntPtr pointer ) { if ( Platform.IsWindows64 ) _pi = new Platform.Win64( pointer ); @@ -16,8 +22,23 @@ public SteamUtils( IntPtr pointer ) else if ( Platform.IsOsx ) _pi = new Platform.Mac( pointer ); } + // + // Class is invalid if we don't have a valid implementation + // public bool IsValid{ get{ return _pi != null && _pi.IsValid; } } + // + // When shutting down clear all the internals to avoid accidental use + // + public virtual void Dispose() + { + if ( _pi != null ) + { + _pi.Dispose(); + _pi = null; + } + } + // bool public bool BOverlayNeedsPresent() { diff --git a/Facepunch.Steamworks/SteamNative/SteamNative.SteamVideo.cs b/Facepunch.Steamworks/SteamNative/SteamNative.SteamVideo.cs index 08451e5..b5bed9d 100644 --- a/Facepunch.Steamworks/SteamNative/SteamNative.SteamVideo.cs +++ b/Facepunch.Steamworks/SteamNative/SteamNative.SteamVideo.cs @@ -3,10 +3,16 @@ namespace SteamNative { - public unsafe class SteamVideo + public unsafe class SteamVideo : IDisposable { + // + // Holds a platform specific implentation + // internal Platform.Interface _pi; + // + // Constructor decides which implementation to use based on current platform + // public SteamVideo( IntPtr pointer ) { if ( Platform.IsWindows64 ) _pi = new Platform.Win64( pointer ); @@ -16,8 +22,23 @@ public SteamVideo( IntPtr pointer ) else if ( Platform.IsOsx ) _pi = new Platform.Mac( pointer ); } + // + // Class is invalid if we don't have a valid implementation + // public bool IsValid{ get{ return _pi != null && _pi.IsValid; } } + // + // When shutting down clear all the internals to avoid accidental use + // + public virtual void Dispose() + { + if ( _pi != null ) + { + _pi.Dispose(); + _pi = null; + } + } + // void public void GetVideoURL( AppId_t unVideoAppID /*AppId_t*/ ) { diff --git a/Generator/CodeWriter.PlatformClass.cs b/Generator/CodeWriter.PlatformClass.cs index d4555dc..135cca1 100644 --- a/Generator/CodeWriter.PlatformClass.cs +++ b/Generator/CodeWriter.PlatformClass.cs @@ -8,9 +8,6 @@ namespace Generator { public partial class CodeWriter { - - - bool LargePack; private void PlatformClass( string type, string libraryName, bool LargePack ) @@ -18,43 +15,54 @@ private void PlatformClass( string type, string libraryName, bool LargePack ) this.LargePack = LargePack; StartBlock( $"internal static partial class Platform" ); - StartBlock( $"public class {type} : Interface" ); - - WriteLine( "internal IntPtr _ptr;" ); - WriteLine( "public bool IsValid { get{ return _ptr != null; } }" ); - - WriteLine(); - - // - // Constructor - // - StartBlock( $"public {type}( IntPtr pointer )" ); - WriteLine( "_ptr = pointer;" ); - EndBlock(); - - foreach ( var c in def.methods.GroupBy( x => x.ClassName ) ) { - PlatformClass( c.Key, c.ToArray() ); - } + StartBlock( $"public class {type} : Interface" ); + { + WriteLine( "internal IntPtr _ptr;" ); + WriteLine( "public bool IsValid { get{ return _ptr != null; } }" ); + WriteLine(); - StartBlock( $"internal static unsafe class Native" ); - foreach ( var c in def.methods.GroupBy( x => x.ClassName ) ) - { - InteropClass( libraryName, c.Key, c.ToArray() ); - } - EndBlock(); + WriteLine( "//" ); + WriteLine( "// Constructor sets pointer to native class" ); + WriteLine( "//" ); + StartBlock( $"public {type}( IntPtr pointer )" ); + { + WriteLine( "_ptr = pointer;" ); + } + EndBlock(); - EndBlock(); + WriteLine( "//" ); + WriteLine( "// When shutting down clear all the internals to avoid accidental use" ); + WriteLine( "//" ); + StartBlock( $"public virtual void Dispose()" ); + { + WriteLine( "_ptr = IntPtr.Zero;" ); + } + EndBlock(); + WriteLine(); + + foreach ( var c in def.methods.GroupBy( x => x.ClassName ) ) + { + PlatformClass( c.Key, c.ToArray() ); + } + + StartBlock( $"internal static unsafe class Native" ); + { + foreach ( var c in def.methods.GroupBy( x => x.ClassName ) ) + { + InteropClass( libraryName, c.Key, c.ToArray() ); + } + } + EndBlock(); + } + EndBlock(); + } EndBlock(); } private void PlatformClass( string className, SteamApiDefinition.MethodDef[] methodDef ) { - if ( className == "ISteamMatchmakingPingResponse" ) return; - if ( className == "ISteamMatchmakingServerListResponse" ) return; - if ( className == "ISteamMatchmakingPlayersResponse" ) return; - if ( className == "ISteamMatchmakingRulesResponse" ) return; - if ( className == "ISteamMatchmakingPingResponse" ) return; + if ( ShouldIgnoreClass( className ) ) return; LastMethodName = ""; foreach ( var m in methodDef ) @@ -169,11 +177,7 @@ private void InteropClassMethod( string library, string classname, SteamApiDefin private void InteropClass( string libraryName, string className, SteamApiDefinition.MethodDef[] methodDef ) { - if ( className == "ISteamMatchmakingPingResponse" ) return; - if ( className == "ISteamMatchmakingServerListResponse" ) return; - if ( className == "ISteamMatchmakingPlayersResponse" ) return; - if ( className == "ISteamMatchmakingRulesResponse" ) return; - if ( className == "ISteamMatchmakingPingResponse" ) return; + if ( ShouldIgnoreClass( className ) ) return; StartBlock( $"internal static unsafe class {className}" ); diff --git a/Generator/CodeWriter.cs b/Generator/CodeWriter.cs index 55cd4d0..c26e731 100644 --- a/Generator/CodeWriter.cs +++ b/Generator/CodeWriter.cs @@ -260,7 +260,7 @@ public void ToFolder( string folder ) } { - Classes( $"{folder}SteamNative." ); + Class( $"{folder}SteamNative." ); } } diff --git a/Generator/CodeWriter/CodeWriter.Classes.cs b/Generator/CodeWriter/Class.cs similarity index 82% rename from Generator/CodeWriter/CodeWriter.Classes.cs rename to Generator/CodeWriter/Class.cs index adc6e6b..96a7344 100644 --- a/Generator/CodeWriter/CodeWriter.Classes.cs +++ b/Generator/CodeWriter/Class.cs @@ -8,72 +8,7 @@ namespace Generator { public partial class CodeWriter { - void Classes( string targetName ) - { - foreach ( var g in def.methods.GroupBy( x => x.ClassName ) ) - { - if ( g.Key == "ISteamMatchmakingPingResponse" ) continue; - if ( g.Key == "ISteamMatchmakingServerListResponse" ) continue; - if ( g.Key == "ISteamMatchmakingPlayersResponse" ) continue; - if ( g.Key == "ISteamMatchmakingRulesResponse" ) continue; - if ( g.Key == "ISteamMatchmakingPingResponse" ) continue; - if ( g.Key == "ISteamMatchmakingPingResponse" ) continue; - - if ( g.Key == "SteamApi" ) - { - sb = new StringBuilder(); - Header(); - Class( "SteamApi", g.OrderBy( x => x.Name ).ToArray() ); - Footer(); - System.IO.File.WriteAllText( $"{targetName}SteamApi.cs", sb.ToString() ); - return; - } - - sb = new StringBuilder(); - Header(); - Class( g.Key, g.OrderBy( x => x.Name ).ToArray() ); - Footer(); - System.IO.File.WriteAllText( $"{targetName}{g.Key.Substring( 1 )}.cs", sb.ToString() ); - } - } - - private void Class( string classname, SteamApiDefinition.MethodDef[] methodDef ) - { - var GenerateClassName = classname; - if ( classname[0] == 'I' ) GenerateClassName = classname.Substring( 1 ); - - StartBlock( $"public unsafe class {GenerateClassName}" ); - - WriteLine( "internal Platform.Interface _pi;" ); - - WriteLine(); - - // - // Constructor - // - StartBlock( $"public {GenerateClassName}( IntPtr pointer )" ); - - WriteLine( "if ( Platform.IsWindows64 ) _pi = new Platform.Win64( pointer );" ); - WriteLine( "else if ( Platform.IsWindows32 ) _pi = new Platform.Win32( pointer );" ); - WriteLine( "else if ( Platform.IsLinux32 ) _pi = new Platform.Linux32( pointer );" ); - WriteLine( "else if ( Platform.IsLinux64 ) _pi = new Platform.Linux64( pointer );" ); - WriteLine( "else if ( Platform.IsOsx ) _pi = new Platform.Mac( pointer );" ); - - EndBlock(); - WriteLine(); - WriteLine( "public bool IsValid{ get{ return _pi != null && _pi.IsValid; } }" ); - WriteLine(); - - LastMethodName = ""; - - foreach ( var m in methodDef ) - ClassMethod( classname, m ); - - EndBlock(); - } - string LastMethodName; - List BeforeLines; List AfterLines; string ReturnType; @@ -81,6 +16,81 @@ private void Class( string classname, SteamApiDefinition.MethodDef[] methodDef ) SteamApiDefinition.MethodDef MethodDef; string ClassName; + // + // Output a class into a file + // + void Class( string FileName ) + { + foreach ( var g in def.methods.GroupBy( x => x.ClassName ) ) + { + if ( ShouldIgnoreClass( g.Key ) ) continue; + + sb = new StringBuilder(); + Header(); + Class( g.Key, g.OrderBy( x => x.Name ).ToArray() ); + Footer(); + System.IO.File.WriteAllText( $"{FileName}{InterfaceNameToClass(g.Key)}.cs", sb.ToString() ); + } + } + + private void Class( string classname, SteamApiDefinition.MethodDef[] methodDef ) + { + StartBlock( $"public unsafe class {InterfaceNameToClass(classname)} : IDisposable" ); + { + WriteLine( "//" ); + WriteLine( "// Holds a platform specific implentation" ); + WriteLine( "//" ); + WriteLine( "internal Platform.Interface _pi;" ); + WriteLine(); + + WriteLine( "//" ); + WriteLine( "// Constructor decides which implementation to use based on current platform" ); + WriteLine( "//" ); + StartBlock( $"public {InterfaceNameToClass( classname )}( IntPtr pointer )" ); + { + WriteLine( "if ( Platform.IsWindows64 ) _pi = new Platform.Win64( pointer );" ); + WriteLine( "else if ( Platform.IsWindows32 ) _pi = new Platform.Win32( pointer );" ); + WriteLine( "else if ( Platform.IsLinux32 ) _pi = new Platform.Linux32( pointer );" ); + WriteLine( "else if ( Platform.IsLinux64 ) _pi = new Platform.Linux64( pointer );" ); + WriteLine( "else if ( Platform.IsOsx ) _pi = new Platform.Mac( pointer );" ); + } + EndBlock(); + WriteLine(); + + WriteLine( "//" ); + WriteLine( "// Class is invalid if we don't have a valid implementation" ); + WriteLine( "//" ); + WriteLine( "public bool IsValid{ get{ return _pi != null && _pi.IsValid; } }" ); + WriteLine(); + + WriteLine( "//" ); + WriteLine( "// When shutting down clear all the internals to avoid accidental use" ); + WriteLine( "//" ); + StartBlock( $"public virtual void Dispose()" ); + { + StartBlock( " if ( _pi != null )" ); + { + WriteLine( "_pi.Dispose();" ); + WriteLine( "_pi = null;" ); + } + EndBlock(); + } + EndBlock(); + WriteLine(); + + // + // Methods + // + foreach ( var m in methodDef ) + { + ClassMethod( classname, m ); + } + } + EndBlock(); + } + + + private void ClassMethod( string classname, SteamApiDefinition.MethodDef m ) { var argList = BuildArguments( m.Params ); diff --git a/Generator/CodeWriter/Interface.cs b/Generator/CodeWriter/Interface.cs index a5cf4e0..66b27a1 100644 --- a/Generator/CodeWriter/Interface.cs +++ b/Generator/CodeWriter/Interface.cs @@ -15,7 +15,7 @@ void PlatformInterface() { StartBlock( $"internal static partial class Platform" ); { - StartBlock( $"public interface Interface" ); + StartBlock( $"public interface Interface : IDisposable" ); { WriteLine( "// Implementation should return true if _ptr is non null" ); WriteLine( "bool IsValid { get; } " ); @@ -24,11 +24,7 @@ void PlatformInterface() foreach ( var m in def.methods.OrderBy( x => x.ClassName ) ) { - if ( m.ClassName == "ISteamMatchmakingPingResponse" ) continue; - if ( m.ClassName == "ISteamMatchmakingServerListResponse" ) continue; - if ( m.ClassName == "ISteamMatchmakingPlayersResponse" ) continue; - if ( m.ClassName == "ISteamMatchmakingRulesResponse" ) continue; - if ( m.ClassName == "ISteamMatchmakingPingResponse" ) continue; + if ( ShouldIgnoreClass( m.ClassName ) ) continue; PlatformInterfaceMethod( m ); } diff --git a/Generator/CodeWriter/Utility.cs b/Generator/CodeWriter/Utility.cs new file mode 100644 index 0000000..692bfba --- /dev/null +++ b/Generator/CodeWriter/Utility.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Generator +{ + public partial class CodeWriter + { + static string[] IgnoredClasses = new string[] + { + "ISteamMatchmakingPingResponse", + "ISteamMatchmakingServerListResponse", + "ISteamMatchmakingPlayersResponse", + "ISteamMatchmakingRulesResponse", + "ISteamMatchmakingPingResponse", + }; + + public static bool ShouldIgnoreClass( string name ) + { + return IgnoredClasses.Contains( name ); + } + + public string InterfaceNameToClass( string name ) + { + if ( name[0] == 'I' ) + name = name.Substring( 1 ); + + return name; + } + + } +} diff --git a/Generator/Generator.csproj b/Generator/Generator.csproj index d06f1f6..04e46f0 100644 --- a/Generator/Generator.csproj +++ b/Generator/Generator.csproj @@ -48,11 +48,12 @@ - + +