Extract ProcessFileName for re-use

This commit is contained in:
JustArchi
2019-12-08 00:32:59 +01:00
parent 1c809ffb13
commit d11603c74b
3 changed files with 10 additions and 20 deletions

View File

@@ -20,6 +20,7 @@
// limitations under the License.
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
@@ -30,6 +31,9 @@ using JetBrains.Annotations;
namespace ArchiSteamFarm {
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);
[NotNull]

View File

@@ -42,9 +42,6 @@ namespace ArchiSteamFarm {
internal static bool RestartAllowed { get; private set; } = true;
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 bool SystemRequired;
@@ -62,7 +59,7 @@ namespace ArchiSteamFarm {
return;
}
string executableName = Path.GetFileNameWithoutExtension(ProcessFileName);
string executableName = Path.GetFileNameWithoutExtension(OS.ProcessFileName);
if (string.IsNullOrEmpty(executableName)) {
ASF.ArchiLogger.LogNullError(nameof(executableName));
@@ -73,7 +70,7 @@ namespace ArchiSteamFarm {
IEnumerable<string> arguments = Environment.GetCommandLineArgs().Skip(executableName.Equals(SharedInfo.AssemblyName) ? 1 : 0);
try {
Process.Start(ProcessFileName, string.Join(" ", arguments));
Process.Start(OS.ProcessFileName, string.Join(" ", arguments));
} catch (Exception e) {
ASF.ArchiLogger.LogGenericException(e);
}

View File

@@ -69,28 +69,17 @@ namespace ArchiSteamFarm {
}
// 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
string binaryPath;
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 {
// In order to achieve that, we have to guess the case from the binary's name
CachedHomeDirectory = Path.GetFileNameWithoutExtension(OS.ProcessFileName) 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
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
"dotnet" => AppContext.BaseDirectory,
"mono" => AppContext.BaseDirectory,
// Unhandled case
_ => throw new ArgumentOutOfRangeException(nameof(binaryPath))
_ => throw new ArgumentOutOfRangeException(nameof(OS.ProcessFileName))
};
return CachedHomeDirectory;