Game.Modding.Toolchain.Dependencies.DotNetDependency
Assembly:
Game (inferred — actual assembly name may vary depending on build)
Namespace:
Game.Modding.Toolchain.Dependencies
Type:
public class
Base:
BaseDependency
Summary:
DotNetDependency manages the .NET SDK dependency used by the modding toolchain. It can detect an installed dotnet SDK version, decide whether a download is required, download the official Microsoft installer (via aka.ms), perform a quiet install into a configurable installation directory, and uninstall the SDK via the Windows package cache/uninstaller. The class uses CliWrap to run the dotnet installer/uninstaller and to query the installed dotnet version. It also reports disk-space requirements and caches the remote installer size. The class expects a minimum supported SDK version (the code queries installed version and treats major >= 6 as installed) and defaults to downloading version 8.0 for the host architecture.
Fields
-
private long? m_DownloadSize
This nullable long caches the remote installer size for the selected .NET SDK installer URL. It is filled by GetDotNetInstallerSize the first time the size is requested to avoid repeated HEAD calls. -
private string m_InstallationDirectory
Backstore for the installationDirectory property. The public installationDirectory setter normalizes and combines the provided value with the internal kInstallationFolder ("dotnet").
Properties
public string installerPath { get; }
installerPath builds a full path where the downloaded installer will be saved: Path.Combine(FullPath(SharedSettings.instance.modding.downloadDirectory), Path.GetFileName(sDotNetInstallerUrl)). Use this to find the on-disk installer file that Download/Install expect.
Constructors
public DotNetDependency()
Default constructor. Sets the installationDirectory to the system Program Files folder (kDefaultInstallationDirectory) combined with the internal kInstallationFolder so the SDK will be installed under "%ProgramFiles%\dotnet" by default.
Methods
-
public override Task<bool> IsInstalled(CancellationToken token)
Checks whether a compatible dotnet SDK is present by invoking GetDotnetVersion(token) and returning true when the reported major version is >= 6. Uses CliWrap to rundotnet --version
internally. -
public override Task<bool> NeedDownload(CancellationToken token)
Returns true if the installer file does not exist locally or its size differs from the remote installer size. If sizes differ the local file is deleted to force a fresh download. Uses GetDotNetInstallerSize to obtain the remote size. -
private async Task<long> GetDotNetInstallerSize(CancellationToken token)
Retrieves and caches the installer size (m_DownloadSize) via IToolchainDependency.GetDownloadSizeAsync(sDotNetInstallerUrl, token). Returns the cached value on subsequent calls. -
public override async Task<List<IToolchainDependency.DiskSpaceRequirements>> GetRequiredDiskSpace(CancellationToken token)
Returns a list of disk-space requirements required to install .NET when not installed. Adds an entry for the installationDirectory (1 GiB in code) and, if the installer must be downloaded, an entry for the installerPath with the size returned from GetDotNetInstallerSize. -
public override Task Download(CancellationToken token)
Starts the download of the dotnet installer into installerPath by delegating to BaseDependency.Download(this, token, sDotNetInstallerUrl, installerPath, "DownloadingDotNet"). -
public override async Task Install(CancellationToken token)
Performs the installation using CliWrap to run the downloaded installer with arguments "/install /quiet /norestart INSTALLDIR=\"\"". Updates toolchain state (DependencyState.Installing) and logs output and errors. Handles CommandExecutionException specially — maps exit codes 1602 and 1603 to a ToolchainException indicating user-cancelled installation. Finally attempts to delete the installer file asynchronously even on error. On success calls IToolchainDependency.UpdateProcessEnvVarPathValue() to refresh PATH handling. -
public override async Task Uninstall(CancellationToken token)
Attempts to find the SDK uninstaller via IToolchainDependency.GetUninstaller (providing a dictionary with "DisplayName" = "Microsoft .NET SDK {version} ({arch})"). Then runs the cached package executable from the CommonApplicationData package cache using CliWrap with "/uninstall /quiet". Maps the same exit codes 1602/1603 to user-cancelled ToolchainException. Sets toolchain state to Removing and logs output and errors. -
private async Task<string> GetVersion(CancellationToken token)
Helper that calls GetDotnetVersion(token), sets base.version to the parsed string (or empty if 0.0.0) and returns the stored version string. -
public override LocalizedString GetLocalizedVersion()
Returns a localized string describing the installed version or a minimum-version warning (min version "6.0") when version is empty. -
public static async Task<System.Version> GetDotnetVersion(CancellationToken token)
Runsdotnet --version
via CliWrap and attempts to parse the stdout into a System.Version. Handles lines with prerelease suffixes (e.g., "8.0.100-rc1") by trimming at '-' and parsing. Collects stderr lines in errorText and logs warnings/errors as appropriate. Returns new Version() (0.0.0) if parsing fails or dotnet is not found. Handles Win32Exception cases (common when dotnet not installed).
Usage Example
// Example usage inside async context (e.g., a toolchain manager)
var dep = new DotNetDependency();
// Check installed
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30));
if (!await dep.IsInstalled(cts.Token))
{
// Ensure we have disk space info and possibly prompt the user
var spaceReqs = await dep.GetRequiredDiskSpace(cts.Token);
// Download if needed
if (await dep.NeedDownload(cts.Token))
{
await dep.Download(cts.Token);
}
// Install
try
{
await dep.Install(cts.Token);
}
catch (ToolchainException te)
{
// handle installation errors (user cancel, failed install, etc.)
IToolchainDependency.log.ErrorFormat(te, "Failed to install .NET");
}
}
// Get textual version for UI
var versionString = dep.GetLocalizedVersion();
Notes and considerations - The class targets Windows installers (uses .exe installer URL and Package Cache uninstaller path). Behavior on non-Windows OSes will be limited or fail; the installer URL uses RuntimeInformation.OSArchitecture in its filename. - Many operations accept a CancellationToken and use ConfigureAwait(false) for async continuations. - CliWrap is used to run external commands; stdout and stderr are captured and forwarded to the toolchain logger. - Exceptions are wrapped into ToolchainException with appropriate ToolchainError values to allow the toolchain to display user-friendly error messages.