Skip to content

Commit

Permalink
Never thought that setenvironmentvariable will not update a value if …
Browse files Browse the repository at this point in the history
…the process is running, not really consistent
  • Loading branch information
bcssov committed Nov 3, 2023
1 parent 13c150b commit c55dc48
Showing 1 changed file with 42 additions and 6 deletions.
48 changes: 42 additions & 6 deletions src/IronyModManager.IO/Platforms/SteamHandler.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// ***********************************************************************

// ***********************************************************************
// Assembly : IronyModManager.IO
// Author : Mario
// Created : 07-11-2022
//
// Last Modified By : Mario
// Last Modified On : 11-18-2022
// Last Modified On : 11-03-2023
// ***********************************************************************
// <copyright file="SteamHandler.cs" company="Mario">
// Mario
Expand All @@ -24,6 +25,7 @@

namespace IronyModManager.IO.Platforms
{

/// <summary>
/// Class SteamHandler.
/// Implements the <see cref="ISteam" />
Expand Down Expand Up @@ -152,6 +154,7 @@ public async Task<bool> InitAsync(long appId)
}
var result = await InitializeAndValidateAsync();
var initCheckAttempts = 0;

// Will keep trying until steam starts
while (!result)
{
Expand All @@ -177,6 +180,21 @@ public Task<bool> ShutdownAPIAsync()
return Task.FromResult(true);
}

#pragma warning disable SYSLIB1054 // Use 'LibraryImportAttribute' instead of 'DllImportAttribute' to generate P/Invoke marshalling code at compile time
/// <summary>
/// Setenvs the specified name.
/// </summary>
/// <param name="name">The name.</param>
/// <param name="value">The value.</param>
/// <param name="overwrite">if set to <c>true</c> [overwrite].</param>
/// <returns>System.Int32.</returns>
[DllImport("libc", CharSet = CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true)]

// No thanks, don't want to flag allow unsafe just yet
private static extern int setenv(string name, string value, bool overwrite);

#pragma warning restore SYSLIB1054 // Use 'LibraryImportAttribute' instead of 'DllImportAttribute' to generate P/Invoke marshalling code at compile time

/// <summary>
/// Cleanups the application identifier.
/// </summary>
Expand Down Expand Up @@ -248,10 +266,10 @@ private Task<bool> OpenAsync(string command)
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))
SetEnvironmentVariable("SteamAppId", appIdValue);
SetEnvironmentVariable("SteamOverlayGameId", appIdValue);
SetEnvironmentVariable("SteamGameId", appIdValue);
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
try
{
Expand All @@ -263,6 +281,24 @@ private async Task SetAppId(long appId)
}
}

/// <summary>
/// Sets the environment variable.
/// </summary>
/// <param name="name">The name.</param>
/// <param name="value">The value.</param>
private void SetEnvironmentVariable(string name, string value)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
// Way to go Microsoft
_ = setenv(name, value, true);
}
else
{
Environment.SetEnvironmentVariable(name, value);
}
}

#endregion Methods
}
}

0 comments on commit c55dc48

Please sign in to comment.