Game.Modding.Toolchain.Dependencies.NodeJSDependency
Assembly:
Namespace: Game.Modding.Toolchain.Dependencies
Type: class
Base: BaseDependency
Summary:
Manages Node.js as a toolchain dependency for the modding toolchain. Provides logic for detecting installed Node.js version, determining if Node.js needs to be downloaded/installed, downloading the Node.js MSI, installing (via msiexec) into a configurable installation directory, uninstalling, and reporting disk-space requirements. Uses CliWrap to invoke external commands and integrates with the toolchain helper APIs (IToolchainDependency, AsyncUtils, SharedSettings, GameManager).
Fields
-
public static readonly string kNodeJSVersion = "20.11.0"
This is the targeted Node.js version that the toolchain expects to install. -
public static readonly string kMinNodeJSVersion = "18.0"
Minimum acceptable Node.js version for the toolchain functionality. -
public static readonly string kNodeJSInstallerUrl
Constructed URL to the Node.js MSI installer for the configured kNodeJSVersion and the current OS architecture. Used as the download source. -
public static readonly string kDefaultInstallationDirectory
Default base installation directory (Program Files folder). -
public static readonly string kInstallationFolder = "nodejs"
Relative folder name appended to installationDirectory when setting where Node.js should be installed. -
private long? m_DownloadSize
Cached size of the Node.js installer (download size). Populated lazily. -
private string m_InstallationDirectory
Backing field for the installationDirectory property; stores the full path where Node.js will be installed (default is Program Files\nodejs).
Properties
-
public override string name => "Node.js"
Human-readable name of the dependency. -
public override string icon => "Media/Toolchain/NodeJS.svg"
Path to an icon representing the dependency in the UI. -
public override bool confirmUninstallation => true
Indicates the UI should confirm uninstallation with the user. -
public string installerPath
Computed path to where the MSI installer should be downloaded. Uses SharedSettings.instance.modding.downloadDirectory and the file name from kNodeJSInstallerUrl. -
public override string installationDirectory { get; set; }
Gets/sets the installation directory. Setter will combine the provided base path with kInstallationFolder and normalize it to a full path. -
public override bool canChangeInstallationDirectory => true
Indicates users can change the installation directory for this dependency. -
public override LocalizedString installDescr
Localized description displayed when installing, includes placeholders for NODEJS_VERSION and HOST (installer URL host). -
public override LocalizedString uninstallMessage
Localized message shown on uninstall confirming version information. -
public override string version { get; protected set; }
Detected installed Node.js version. On first read, it will synchronously attempt to determine the node version by invoking GetNodeVersion using the GameManager termination token.
Constructors
public NodeJSDependency()
Initializes the dependency and sets installationDirectory to the default (Program Files\nodejs).
Methods
-
public override async Task<bool> IsInstalled(CancellationToken token)
Checks whether Node.js is installed by attempting to obtain the node version. Returns true if a non-empty version string is found. -
public override async Task<bool> IsUpToDate(CancellationToken token)
Compares the detected Node.js version against kMinNodeJSVersion. Returns true if the installed version is >= kMinNodeJSVersion. -
public override async Task<bool> NeedDownload(CancellationToken token)
Determines whether the installer needs to be downloaded. Returns true if the installer file does not exist or its size differs from the expected remote size. If the local file exists but size mismatches, it deletes the local file and returns true. -
private async Task<long> GetDotNetInstallerSize(CancellationToken token)
(Private helper) Returns the cached download size or queries the remote installer size via IToolchainDependency.GetDownloadSizeAsync and caches it in m_DownloadSize. -
public override async Task<List<IToolchainDependency.DiskSpaceRequirements>> GetRequiredDiskSpace(CancellationToken token)
Calculates disk-space requirements for installing Node.js. If not installed, requests ~100 MB for the installation directory and adds the installer file size if the installer must be downloaded. -
public override Task Download(CancellationToken token)
Starts downloading the MSI installer. Delegates to BaseDependency.Download with kNodeJSInstallerUrl, installerPath, and a localized progress key "DownloadingNodeJS". -
public override async Task Install(CancellationToken token)
Installs Node.js from the downloaded MSI using msiexec (/i ... /passive /norestart INSTALLDIR="..."). Uses CliWrap to launch the process, pipes stdout/stderr to IToolchainDependency.log, updates the PATH environment variable via IToolchainDependency.UpdateProcessEnvVarPathValue() after successful install, and always deletes the installer file at the end. Converts common msiexec exit codes for user-cancellation into a ToolchainException with a clearer message; wraps other failures in ToolchainException. -
public override async Task Uninstall(CancellationToken token)
Attempts to find an installer GUID (uninstaller key) using IToolchainDependency.GetUninstaller with DisplayName = "Node.js" and DisplayVersion = version. If found, runs msiexec /x/passive /norestart via CliWrap and handles common exit codes and exceptional conditions similarly to Install. -
private async Task<string> GetNodeVersion(CancellationToken token)
Invokes "node -v" using CliWrap to capture the output and any error strings. Strips a leading 'v' from the returned version string (e.g., "v20.11.0" -> "20.11.0"), sets base.version, and returns it. Handles Win32Exception when node is not found and logs other errors. Aggregates stderr lines and logs them as warnings if present. -
public override LocalizedString GetLocalizedVersion()
Returns the localized version string for UI. If no version is detected, returns a localized string warning about the minimum supported version (MIN_VERSION = kMinNodeJSVersion). Otherwise returns the base GetLocalizedVersion.
Usage Example
// Example usage pattern (simplified). Run from an async context.
var nodeDep = new NodeJSDependency();
// Use the game's termination token where appropriate:
var token = GameManager.instance.terminationToken;
// Check installation state
if (!await nodeDep.IsInstalled(token))
{
// Ensure enough disk space
var requirements = await nodeDep.GetRequiredDiskSpace(token);
// (UI should present requirements and obtain user confirmation)
// Download installer if needed
if (await nodeDep.NeedDownload(token))
{
await nodeDep.Download(token);
}
// Install Node.js (will run msiexec and clean up installer)
await nodeDep.Install(token);
}
// Get detected version
string installedVersion = nodeDep.version;
Notes: - This class relies on other toolchain utilities (IToolchainDependency, BaseDependency, AsyncUtils, SharedSettings, GameManager) and CliWrap for running external processes. - Installer behavior is implemented for MSI-based installers (msiexec) and thus targets Windows platforms where an MSI is available.