mirror of
https://github.com/BepInEx/mono.git
synced 2026-11-17 19:10:23 -10:00
* Remove unnecessary asserts * Make several classes static * Use is instead of as+null check * Use T? instead of Nullable<T> * more static classes * Mark locals as const * Merge declaration and initialization of some variables * Remove unnecessary casts * Remove unnecessary "unsafe"s * Simplify several lambda expressions * Remove redundant parentheses * Remove redundant '== true' * Remove redundant empty lines * Simplify boolean comparison with '== false' * Replace if-statement with return statement * Use while for infinite loop * Add static to all partial static class declarations * Use ++/-- operator instead of assignment * Use string.IsNullOrEmpty * Use coalesce expression * Simplify lazy initialization * Use coalese expression * Join string expressions * Use regular string literal instead of verbatim string literal * Optimize StringBuilder.Append calls * Remove redundant assignment * Remove unnecessary unsafe context * Merge processor directives * Use String.Equals instead of String.Compare * Use Debug.Fail instead of Debug.Assert(false * Remove Attribute suffix * Use predefined type * Use compound assignment * Use while statement to create an infinite loop * Remove redundant base ctor call * Avoid using catch (Exception) * Remove empty regions * Span comparison to null * Avoid unnecessary boxing of value type * Expression is always equal to 'true' * Remove unused method * update coalesce assignment * fix unsafe * fix redundant parens * Fix whitespace errors introduced Signed-off-by: dotnet-bot <[email protected]>
187 lines
6.8 KiB
C#
187 lines
6.8 KiB
C#
// Licensed to the .NET Foundation under one or more agreements.
|
|
// The .NET Foundation licenses this file to you under the MIT license.
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
using System.Collections;
|
|
using System.Diagnostics;
|
|
using System.Reflection;
|
|
using System.Threading;
|
|
|
|
namespace System
|
|
{
|
|
public static partial class Environment
|
|
{
|
|
public static string? GetEnvironmentVariable(string variable)
|
|
{
|
|
if (variable == null)
|
|
throw new ArgumentNullException(nameof(variable));
|
|
|
|
return GetEnvironmentVariableCore(variable);
|
|
}
|
|
|
|
public static string? GetEnvironmentVariable(string variable, EnvironmentVariableTarget target)
|
|
{
|
|
if (target == EnvironmentVariableTarget.Process)
|
|
return GetEnvironmentVariable(variable);
|
|
|
|
if (variable == null)
|
|
throw new ArgumentNullException(nameof(variable));
|
|
|
|
bool fromMachine = ValidateAndConvertRegistryTarget(target);
|
|
return GetEnvironmentVariableFromRegistry(variable, fromMachine);
|
|
}
|
|
|
|
public static IDictionary GetEnvironmentVariables(EnvironmentVariableTarget target)
|
|
{
|
|
if (target == EnvironmentVariableTarget.Process)
|
|
return GetEnvironmentVariables();
|
|
|
|
bool fromMachine = ValidateAndConvertRegistryTarget(target);
|
|
return GetEnvironmentVariablesFromRegistry(fromMachine);
|
|
}
|
|
|
|
public static void SetEnvironmentVariable(string variable, string? value)
|
|
{
|
|
ValidateVariableAndValue(variable, ref value);
|
|
SetEnvironmentVariableCore(variable, value);
|
|
}
|
|
|
|
public static void SetEnvironmentVariable(string variable, string? value, EnvironmentVariableTarget target)
|
|
{
|
|
if (target == EnvironmentVariableTarget.Process)
|
|
{
|
|
SetEnvironmentVariable(variable, value);
|
|
return;
|
|
}
|
|
|
|
ValidateVariableAndValue(variable, ref value);
|
|
|
|
bool fromMachine = ValidateAndConvertRegistryTarget(target);
|
|
SetEnvironmentVariableFromRegistry(variable, value, fromMachine: fromMachine);
|
|
}
|
|
|
|
public static string CommandLine => PasteArguments.Paste(GetCommandLineArgs(), pasteFirstArgumentUsingArgV0Rules: true);
|
|
|
|
public static string CurrentDirectory
|
|
{
|
|
get => CurrentDirectoryCore;
|
|
set
|
|
{
|
|
if (value == null)
|
|
throw new ArgumentNullException(nameof(value));
|
|
|
|
if (value.Length == 0)
|
|
throw new ArgumentException(SR.Argument_PathEmpty, nameof(value));
|
|
|
|
CurrentDirectoryCore = value;
|
|
}
|
|
}
|
|
|
|
public static string ExpandEnvironmentVariables(string name)
|
|
{
|
|
if (name == null)
|
|
throw new ArgumentNullException(nameof(name));
|
|
|
|
if (name.Length == 0)
|
|
return name;
|
|
|
|
return ExpandEnvironmentVariablesCore(name);
|
|
}
|
|
|
|
private static string[]? s_commandLineArgs;
|
|
|
|
internal static void SetCommandLineArgs(string[] cmdLineArgs) // invoked from VM
|
|
{
|
|
s_commandLineArgs = cmdLineArgs;
|
|
}
|
|
|
|
public static string GetFolderPath(SpecialFolder folder) => GetFolderPath(folder, SpecialFolderOption.None);
|
|
|
|
public static string GetFolderPath(SpecialFolder folder, SpecialFolderOption option)
|
|
{
|
|
if (!Enum.IsDefined(typeof(SpecialFolder), folder))
|
|
throw new ArgumentOutOfRangeException(nameof(folder), folder, SR.Format(SR.Arg_EnumIllegalVal, folder));
|
|
|
|
if (option != SpecialFolderOption.None && !Enum.IsDefined(typeof(SpecialFolderOption), option))
|
|
throw new ArgumentOutOfRangeException(nameof(option), option, SR.Format(SR.Arg_EnumIllegalVal, option));
|
|
|
|
return GetFolderPathCore(folder, option);
|
|
}
|
|
|
|
public static bool Is64BitProcess => IntPtr.Size == 8;
|
|
|
|
public static bool Is64BitOperatingSystem => Is64BitProcess || Is64BitOperatingSystemWhen32BitProcess;
|
|
|
|
private static OperatingSystem? s_osVersion;
|
|
|
|
public static OperatingSystem OSVersion
|
|
{
|
|
get
|
|
{
|
|
if (s_osVersion == null)
|
|
{
|
|
Interlocked.CompareExchange(ref s_osVersion, GetOSVersion(), null);
|
|
}
|
|
return s_osVersion;
|
|
}
|
|
}
|
|
|
|
public static bool UserInteractive => true;
|
|
|
|
public static Version Version
|
|
{
|
|
get
|
|
{
|
|
// FX_PRODUCT_VERSION is expected to be set by the host
|
|
// Use AssemblyInformationalVersionAttribute as fallback if the exact product version is not specified by the host
|
|
string? versionString = (string?)AppContext.GetData("FX_PRODUCT_VERSION") ??
|
|
typeof(object).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion;
|
|
|
|
ReadOnlySpan<char> versionSpan = versionString.AsSpan();
|
|
|
|
// Strip optional suffixes
|
|
int separatorIndex = versionSpan.IndexOfAny("-+ ");
|
|
if (separatorIndex != -1)
|
|
versionSpan = versionSpan.Slice(0, separatorIndex);
|
|
|
|
// Return zeros rather then failing if the version string fails to parse
|
|
return Version.TryParse(versionSpan, out Version? version) ? version : new Version();
|
|
}
|
|
}
|
|
|
|
private static bool ValidateAndConvertRegistryTarget(EnvironmentVariableTarget target)
|
|
{
|
|
Debug.Assert(target != EnvironmentVariableTarget.Process);
|
|
|
|
if (target == EnvironmentVariableTarget.Machine)
|
|
return true;
|
|
|
|
if (target == EnvironmentVariableTarget.User)
|
|
return false;
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(target), target, SR.Format(SR.Arg_EnumIllegalVal, target));
|
|
}
|
|
|
|
private static void ValidateVariableAndValue(string variable, ref string? value)
|
|
{
|
|
if (variable == null)
|
|
throw new ArgumentNullException(nameof(variable));
|
|
|
|
if (variable.Length == 0)
|
|
throw new ArgumentException(SR.Argument_StringZeroLength, nameof(variable));
|
|
|
|
if (variable[0] == '\0')
|
|
throw new ArgumentException(SR.Argument_StringFirstCharIsZero, nameof(variable));
|
|
|
|
if (variable.Contains('='))
|
|
throw new ArgumentException(SR.Argument_IllegalEnvVarName, nameof(variable));
|
|
|
|
if (string.IsNullOrEmpty(value) || value[0] == '\0')
|
|
{
|
|
// Explicitly null out value if it's empty
|
|
value = null;
|
|
}
|
|
}
|
|
}
|
|
}
|