Skip to content

Commit

Permalink
Handle steam api not initializing properly on linux
Browse files Browse the repository at this point in the history
  • Loading branch information
bcssov committed Nov 17, 2022
1 parent 2118002 commit 0eaed56
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/IronyModManager.GameHandler/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
45 changes: 43 additions & 2 deletions src/IronyModManager.IO/Platforms/SteamHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -52,6 +54,11 @@ public class SteamHandler : ISteam
/// </summary>
private const int MaxAttempts = 60 * 4;

/// <summary>
/// The steam application identifier file
/// </summary>
private const string SteamAppIdFile = "steam_appid.txt";

/// <summary>
/// The steam launch
/// </summary>
Expand Down Expand Up @@ -118,13 +125,14 @@ public async Task<bool> InitAlternateAsync()
/// <returns>A Task&lt;System.Boolean&gt; representing the asynchronous operation.</returns>
public async Task<bool> InitAsync(long appId)
{
SetAppId(appId);
await SetAppId(appId);

var canProceed = true;
if (!SteamAPI.IsSteamRunning())
{
if (!await OpenAsync(SteamLaunch))
{
CleanupAppId();
return false;
}
var runCheckAttempts = 0;
Expand Down Expand Up @@ -166,9 +174,31 @@ public async Task<bool> InitAsync(long appId)
public Task<bool> ShutdownAPIAsync()
{
SteamAPI.Shutdown();
CleanupAppId();
return Task.FromResult(true);
}

/// <summary>
/// Cleanups the application identifier.
/// </summary>
private void CleanupAppId()
{
try
{
if (File.Exists(SteamAppIdFile))
{
var fileInfo = new System.IO.FileInfo(SteamAppIdFile)
{
Attributes = FileAttributes.Normal
};
fileInfo.Delete();
}
}
catch
{
}
}

/// <summary>
/// Initialize and validate as an asynchronous operation.
/// </summary>
Expand Down Expand Up @@ -215,12 +245,23 @@ private Task<bool> OpenAsync(string command)
/// Sets the application identifier.
/// </summary>
/// <param name="appId">The application identifier.</param>
private void SetAppId(long appId)
/// <returns>System.Threading.Tasks.Task.</returns>
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
Expand Down

0 comments on commit 0eaed56

Please sign in to comment.