From c8e914d8a1c72438200fb79058b4d6a652593fda Mon Sep 17 00:00:00 2001 From: gibbed Date: Fri, 24 May 2019 14:40:21 -0500 Subject: [PATCH] Throw exception for client initialization failure. --- SAM.API/Client.cs | 46 ++++++++++++------ SAM.API/ClientInitializeException.cs | 50 ++++++++++++++++++++ SAM.API/ClientInitializeFailure.cs | 34 ++++++++++++++ SAM.Game/Program.cs | 70 ++++++++++++++++++---------- SAM.Picker/Program.cs | 69 ++++++++++++++++++--------- 5 files changed, 208 insertions(+), 61 deletions(-) create mode 100644 SAM.API/ClientInitializeException.cs create mode 100644 SAM.API/ClientInitializeFailure.cs diff --git a/SAM.API/Client.cs b/SAM.API/Client.cs index 82a9bf2..ea8b889 100644 --- a/SAM.API/Client.cs +++ b/SAM.API/Client.cs @@ -27,7 +27,7 @@ using System.Linq; namespace SAM.API { - public class Client + public class Client : IDisposable { public Wrappers.SteamClient018 SteamClient; public Wrappers.SteamUser012 SteamUser; @@ -36,61 +36,71 @@ namespace SAM.API public Wrappers.SteamApps001 SteamApps001; public Wrappers.SteamApps003 SteamApps003; + private bool _IsDisposed = false; private int _Pipe; private int _User; private readonly List _Callbacks = new List(); - public bool Initialize(long appId) + public void Initialize(long appId) { + if (string.IsNullOrEmpty(Steam.GetInstallPath()) == true) + { + throw new ClientInitializeException(ClientInitializeFailure.GetInstallPath, "failed to get Steam install path"); + } + if (appId != 0) { Environment.SetEnvironmentVariable("SteamAppId", appId.ToString(CultureInfo.InvariantCulture)); } - if (Steam.GetInstallPath() == null) - { - return false; - } - if (Steam.Load() == false) { - return false; + throw new ClientInitializeException(ClientInitializeFailure.Load, "failed to load SteamClient"); } this.SteamClient = Steam.CreateInterface("SteamClient018"); if (this.SteamClient == null) { - return false; + throw new ClientInitializeException(ClientInitializeFailure.CreateSteamClient, "failed to create ISteamClient018"); } this._Pipe = this.SteamClient.CreateSteamPipe(); if (this._Pipe == 0) { - return false; + throw new ClientInitializeException(ClientInitializeFailure.CreateSteamPipe, "failed to create pipe"); } this._User = this.SteamClient.ConnectToGlobalUser(this._Pipe); if (this._User == 0) { - return false; + throw new ClientInitializeException(ClientInitializeFailure.ConnectToGlobalUser, "failed to connect to global user"); } this.SteamUtils = this.SteamClient.GetSteamUtils004(this._Pipe); if (appId > 0 && this.SteamUtils.GetAppId() != (uint)appId) { - return false; + throw new ClientInitializeException(ClientInitializeFailure.AppIdMismatch, "appID mismatch"); } this.SteamUser = this.SteamClient.GetSteamUser012(this._User, this._Pipe); this.SteamUserStats = this.SteamClient.GetSteamUserStats006(this._User, this._Pipe); this.SteamApps001 = this.SteamClient.GetSteamApps001(this._User, this._Pipe); this.SteamApps003 = this.SteamClient.GetSteamApps003(this._User, this._Pipe); - return true; } ~Client() { + this.Dispose(false); + } + + protected virtual void Dispose(bool disposing) + { + if (this._IsDisposed == true) + { + return; + } + if (this.SteamClient != null && this._Pipe > 0) { if (this._User > 0) @@ -98,10 +108,18 @@ namespace SAM.API this.SteamClient.ReleaseUser(this._Pipe, this._User); this._User = 0; } - + this.SteamClient.ReleaseSteamPipe(this._Pipe); this._Pipe = 0; } + + this._IsDisposed = true; + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); } public TCallback CreateAndRegisterCallback() diff --git a/SAM.API/ClientInitializeException.cs b/SAM.API/ClientInitializeException.cs new file mode 100644 index 0000000..7481441 --- /dev/null +++ b/SAM.API/ClientInitializeException.cs @@ -0,0 +1,50 @@ +/* Copyright (c) 2019 Rick (rick 'at' gibbed 'dot' us) + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would + * be appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not + * be misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. + */ +using System; + +namespace SAM.API +{ + public class ClientInitializeException : Exception + { + public readonly ClientInitializeFailure Failure; + + public ClientInitializeException(ClientInitializeFailure failure) + { + this.Failure = failure; + } + + public ClientInitializeException(ClientInitializeFailure failure, string message) + : base(message) + { + this.Failure = failure; + } + + public ClientInitializeException( + ClientInitializeFailure failure, + string message, + Exception innerException) + : base(message, innerException) + { + this.Failure = failure; + } + } +} diff --git a/SAM.API/ClientInitializeFailure.cs b/SAM.API/ClientInitializeFailure.cs new file mode 100644 index 0000000..0ffb23d --- /dev/null +++ b/SAM.API/ClientInitializeFailure.cs @@ -0,0 +1,34 @@ +/* Copyright (c) 2019 Rick (rick 'at' gibbed 'dot' us) + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would + * be appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not + * be misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. + */ +namespace SAM.API +{ + public enum ClientInitializeFailure : byte + { + Unknown = 0, + GetInstallPath, + Load, + CreateSteamClient, + CreateSteamPipe, + ConnectToGlobalUser, + AppIdMismatch, + } +} diff --git a/SAM.Game/Program.cs b/SAM.Game/Program.cs index 88e6f88..a8bbad6 100644 --- a/SAM.Game/Program.cs +++ b/SAM.Game/Program.cs @@ -24,6 +24,7 @@ using System; using System.Diagnostics; using System.Net; using System.Windows.Forms; +using SAM.API; namespace SAM.Game { @@ -60,40 +61,61 @@ namespace SAM.Game return; } - API.Client client; - try + using (var client = new API.Client()) { - client = new API.Client(); - - if (client.Initialize(appId) == false) + try + { + client.Initialize(appId); + } + catch (ClientInitializeException e) + { + if (e.Failure == ClientInitializeFailure.ConnectToGlobalUser) + { + // TODO(gibbed): show error about family sharing? + MessageBox.Show( + "Steam is not running. Please start Steam then run this tool again.\n\n(" + e.Message + ")", + "Error", + MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + else if (string.IsNullOrEmpty(e.Message) == false) + { + MessageBox.Show( + "Steam is not running. Please start Steam then run this tool again.\n\n(" + e.Message + ")", + "Error", + MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + else + { + MessageBox.Show( + "Steam is not running. Please start Steam then run this tool again.", + "Error", + MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + return; + } + catch (DllNotFoundException) { MessageBox.Show( - "Steam is not running. Please start Steam then run this tool again.", + "You've caused an exceptional error!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } - } - catch (DllNotFoundException) - { - MessageBox.Show( - "You've caused an exceptional error!", - "Error", - MessageBoxButtons.OK, - MessageBoxIcon.Error); - return; - } - /* Disable server certificate validation. - * This is for media downloads (achievement icons). - * https://media.steamcommunity.com/ has certs issued to (various).e.akamai.net. - */ - ServicePointManager.ServerCertificateValidationCallback = (s, ce, ch, e) => true; + /* Disable server certificate validation. + * This is for media downloads (achievement icons). + * https://media.steamcommunity.com/ has certs issued to (various).e.akamai.net. + */ + ServicePointManager.ServerCertificateValidationCallback = (s, ce, ch, e) => true; - Application.EnableVisualStyles(); - Application.SetCompatibleTextRenderingDefault(false); - Application.Run(new Manager(appId, client)); + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.Run(new Manager(appId, client)); + } } } } diff --git a/SAM.Picker/Program.cs b/SAM.Picker/Program.cs index 82f1a73..ef69e63 100644 --- a/SAM.Picker/Program.cs +++ b/SAM.Picker/Program.cs @@ -23,6 +23,7 @@ using System; using System.Net; using System.Windows.Forms; +using SAM.API; namespace SAM.Picker { @@ -41,39 +42,61 @@ namespace SAM.Picker return; } - API.Client client; - try + using (var client = new API.Client()) { - client = new API.Client(); - if (client.Initialize(0) == false) + try + { + client.Initialize(0); + } + catch (ClientInitializeException e) + { + if (e.Failure == ClientInitializeFailure.ConnectToGlobalUser) + { + // TODO(gibbed): show error about family sharing? + MessageBox.Show( + "Steam is not running. Please start Steam then run this tool again.\n\n(" + e.Message + ")", + "Error", + MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + else if (string.IsNullOrEmpty(e.Message) == false) + { + MessageBox.Show( + "Steam is not running. Please start Steam then run this tool again.\n\n(" + e.Message + ")", + "Error", + MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + else + { + MessageBox.Show( + "Steam is not running. Please start Steam then run this tool again.", + "Error", + MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + return; + } + catch (DllNotFoundException) { MessageBox.Show( - "Steam is not running. Please start Steam then run this tool again.", + "You've caused an exceptional error!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } - } - catch (DllNotFoundException) - { - MessageBox.Show( - "You've caused an exceptional error!", - "Error", - MessageBoxButtons.OK, - MessageBoxIcon.Error); - return; - } - /* Disable server certificate validation. - * This is for media downloads (application logos). - * https://media.steamcommunity.com/ has certs issued to (various).e.akamai.net. - */ - ServicePointManager.ServerCertificateValidationCallback = (s, ce, ch, e) => true; + /* Disable server certificate validation. + * This is for media downloads (application logos). + * https://media.steamcommunity.com/ has certs issued to (various).e.akamai.net. + */ + ServicePointManager.ServerCertificateValidationCallback = (s, ce, ch, e) => true; - Application.EnableVisualStyles(); - Application.SetCompatibleTextRenderingDefault(false); - Application.Run(new GamePicker(client)); + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.Run(new GamePicker(client)); + } } } }