mirror of
https://github.com/JustArchiNET/ArchiSteamFarm.git
synced 2026-01-16 08:25:28 +00:00
Extract ProcessFileName for re-use
This commit is contained in:
@@ -20,6 +20,7 @@
|
|||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
@@ -30,6 +31,9 @@ using JetBrains.Annotations;
|
|||||||
|
|
||||||
namespace ArchiSteamFarm {
|
namespace ArchiSteamFarm {
|
||||||
internal static class OS {
|
internal static class OS {
|
||||||
|
// We need to keep this one assigned and not calculated on-demand
|
||||||
|
internal static readonly string ProcessFileName = Process.GetCurrentProcess().MainModule?.FileName ?? throw new ArgumentNullException(nameof(ProcessFileName));
|
||||||
|
|
||||||
internal static bool IsUnix => RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
|
internal static bool IsUnix => RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
|
||||||
|
|
||||||
[NotNull]
|
[NotNull]
|
||||||
|
|||||||
@@ -42,9 +42,6 @@ namespace ArchiSteamFarm {
|
|||||||
internal static bool RestartAllowed { get; private set; } = true;
|
internal static bool RestartAllowed { get; private set; } = true;
|
||||||
internal static bool ShutdownSequenceInitialized { get; private set; }
|
internal static bool ShutdownSequenceInitialized { get; private set; }
|
||||||
|
|
||||||
// We need to keep this one assigned and not calculated on-demand
|
|
||||||
private static readonly string ProcessFileName = Process.GetCurrentProcess().MainModule?.FileName ?? throw new ArgumentNullException(nameof(ProcessFileName));
|
|
||||||
|
|
||||||
private static readonly TaskCompletionSource<byte> ShutdownResetEvent = new TaskCompletionSource<byte>();
|
private static readonly TaskCompletionSource<byte> ShutdownResetEvent = new TaskCompletionSource<byte>();
|
||||||
private static bool SystemRequired;
|
private static bool SystemRequired;
|
||||||
|
|
||||||
@@ -62,7 +59,7 @@ namespace ArchiSteamFarm {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
string executableName = Path.GetFileNameWithoutExtension(ProcessFileName);
|
string executableName = Path.GetFileNameWithoutExtension(OS.ProcessFileName);
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(executableName)) {
|
if (string.IsNullOrEmpty(executableName)) {
|
||||||
ASF.ArchiLogger.LogNullError(nameof(executableName));
|
ASF.ArchiLogger.LogNullError(nameof(executableName));
|
||||||
@@ -73,7 +70,7 @@ namespace ArchiSteamFarm {
|
|||||||
IEnumerable<string> arguments = Environment.GetCommandLineArgs().Skip(executableName.Equals(SharedInfo.AssemblyName) ? 1 : 0);
|
IEnumerable<string> arguments = Environment.GetCommandLineArgs().Skip(executableName.Equals(SharedInfo.AssemblyName) ? 1 : 0);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Process.Start(ProcessFileName, string.Join(" ", arguments));
|
Process.Start(OS.ProcessFileName, string.Join(" ", arguments));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
ASF.ArchiLogger.LogGenericException(e);
|
ASF.ArchiLogger.LogGenericException(e);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -69,28 +69,17 @@ namespace ArchiSteamFarm {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// We're aiming to handle two possible cases here, classic publish and single-file publish
|
// We're aiming to handle two possible cases here, classic publish and single-file publish
|
||||||
// Firstly, we'll get the path to the binary that is running our code
|
// In order to achieve that, we have to guess the case from the binary's name
|
||||||
string binaryPath;
|
CachedHomeDirectory = Path.GetFileNameWithoutExtension(OS.ProcessFileName) switch {
|
||||||
|
|
||||||
using (Process process = Process.GetCurrentProcess()) {
|
|
||||||
binaryPath = process.MainModule?.FileName;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(binaryPath)) {
|
|
||||||
throw new ArgumentNullException(nameof(binaryPath));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Now we need to check what that binary actually is
|
|
||||||
CachedHomeDirectory = Path.GetFileNameWithoutExtension(binaryPath) switch {
|
|
||||||
// This path goes to our own binary, so the user is using OS-specific build, single-file or not, we'll return path to location of that binary then
|
// This path goes to our own binary, so the user is using OS-specific build, single-file or not, we'll return path to location of that binary then
|
||||||
AssemblyName => Path.GetDirectoryName(binaryPath),
|
AssemblyName => Path.GetDirectoryName(OS.ProcessFileName),
|
||||||
|
|
||||||
// This path goes to third-party binary, so the user is using our generic build, we'll return our base directory then
|
// This path goes to third-party binary, so the user is using our generic build, we'll return our base directory then
|
||||||
"dotnet" => AppContext.BaseDirectory,
|
"dotnet" => AppContext.BaseDirectory,
|
||||||
"mono" => AppContext.BaseDirectory,
|
"mono" => AppContext.BaseDirectory,
|
||||||
|
|
||||||
// Unhandled case
|
// Unhandled case
|
||||||
_ => throw new ArgumentOutOfRangeException(nameof(binaryPath))
|
_ => throw new ArgumentOutOfRangeException(nameof(OS.ProcessFileName))
|
||||||
};
|
};
|
||||||
|
|
||||||
return CachedHomeDirectory;
|
return CachedHomeDirectory;
|
||||||
|
|||||||
Reference in New Issue
Block a user