From 0eaed5617357250c696d5a4e396127256b41e4c5 Mon Sep 17 00:00:00 2001 From: bcssov Date: Thu, 17 Nov 2022 07:49:26 +0100 Subject: [PATCH] Handle steam api not initializing properly on linux --- src/IronyModManager.GameHandler/Program.cs | 1 + .../Platforms/SteamHandler.cs | 45 ++++++++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/IronyModManager.GameHandler/Program.cs b/src/IronyModManager.GameHandler/Program.cs index f4804d25..8e8e95d9 100644 --- a/src/IronyModManager.GameHandler/Program.cs +++ b/src/IronyModManager.GameHandler/Program.cs @@ -72,6 +72,7 @@ private static async Task MainAsync() { Console.WriteLine("Checking whether steam is running using direct steam integration method."); await handler.InitAsync(commandLineArgs.SteamAppId.GetValueOrDefault()); + await handler.ShutdownAPIAsync(); } Console.WriteLine("Done exiting."); Environment.Exit(0); diff --git a/src/IronyModManager.IO/Platforms/SteamHandler.cs b/src/IronyModManager.IO/Platforms/SteamHandler.cs index c5f8a905..bdebc569 100644 --- a/src/IronyModManager.IO/Platforms/SteamHandler.cs +++ b/src/IronyModManager.IO/Platforms/SteamHandler.cs @@ -14,7 +14,9 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.IO; using System.Linq; +using System.Runtime.InteropServices; using System.Threading.Tasks; using IronyModManager.IO.Common.Platforms; using IronyModManager.Shared; @@ -52,6 +54,11 @@ public class SteamHandler : ISteam /// private const int MaxAttempts = 60 * 4; + /// + /// The steam application identifier file + /// + private const string SteamAppIdFile = "steam_appid.txt"; + /// /// The steam launch /// @@ -118,13 +125,14 @@ public async Task InitAlternateAsync() /// A Task<System.Boolean> representing the asynchronous operation. public async Task InitAsync(long appId) { - SetAppId(appId); + await SetAppId(appId); var canProceed = true; if (!SteamAPI.IsSteamRunning()) { if (!await OpenAsync(SteamLaunch)) { + CleanupAppId(); return false; } var runCheckAttempts = 0; @@ -166,9 +174,31 @@ public async Task InitAsync(long appId) public Task ShutdownAPIAsync() { SteamAPI.Shutdown(); + CleanupAppId(); return Task.FromResult(true); } + /// + /// Cleanups the application identifier. + /// + private void CleanupAppId() + { + try + { + if (File.Exists(SteamAppIdFile)) + { + var fileInfo = new System.IO.FileInfo(SteamAppIdFile) + { + Attributes = FileAttributes.Normal + }; + fileInfo.Delete(); + } + } + catch + { + } + } + /// /// Initialize and validate as an asynchronous operation. /// @@ -215,12 +245,23 @@ private Task OpenAsync(string command) /// Sets the application identifier. /// /// The application identifier. - private void SetAppId(long appId) + /// System.Threading.Tasks.Task. + private async Task SetAppId(long appId) { var appIdValue = appId.ToString(); Environment.SetEnvironmentVariable("SteamAppId", appIdValue); Environment.SetEnvironmentVariable("SteamOverlayGameId", appIdValue); Environment.SetEnvironmentVariable("SteamGameId", appIdValue); + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + try + { + await File.WriteAllTextAsync(SteamAppIdFile, appIdValue); + } + catch + { + } + } } #endregion Methods