Skip to content

Game.Modding.Toolchain.Dependencies.BaseDependency

Assembly: Game (Toolchain / Modding assembly)
Namespace: Game.Modding.Toolchain.Dependencies

Type: abstract class

Base: IToolchainDependency

Summary: BaseDependency is an abstract base class that implements common functionality for toolchain dependencies used by the modding toolchain. It provides default implementations and helpers for checking installation state, downloading, installing, uninstalling, refreshing, and reporting progress. Subclasses are expected to override relevant virtual members to provide concrete behavior for specific dependencies (for example, a particular SDK, tool or runtime). The class also exposes a protected static Download helper that wraps UnityWebRequest file downloads and updates the dependency state/progress accordingly.


Fields

  • private IToolchainDependency.State m_State This private field stores the current state of the dependency (wrapped in IToolchainDependency.State). The public state property reads/writes this field and will invoke onNotifyProgress when set.

Properties

  • public virtual LocalizedString localizedName { get; } Returns a LocalizedString identifying the dependency name. Default implementation builds a localization id from the concrete type name: "Options.OPTION[ModdingSettings." + GetType().Name + "]".

  • public virtual string name { get; } Returns the simple type name by default (GetType().Name). Can be overridden.

  • public virtual string version { get; protected set; } String representing the dependency version. Setter is protected so subclasses can update it. The IToolchainDependency.version explicit implementation forwards to this property.

  • string IToolchainDependency.version { get; set; } Explicit interface implementation that delegates to the public version property.

  • public virtual string icon { get; } Optional icon path or identifier for UI; default is null.

  • public IToolchainDependency.State state { get; set; } Gets or sets the dependency state. Setting this property updates the private m_State field and invokes the onNotifyProgress event with the new state to notify listeners of progress/state changes.

  • public bool needDownload { get; protected set; } Indicates whether the dependency requires a download. Default: false. There is also an explicit interface implementation below.

  • bool IToolchainDependency.needDownload { get; set; } Explicit interface implementation that delegates to the public needDownload field/property.

  • public List<IToolchainDependency.DiskSpaceRequirements> spaceRequirements { get; protected set; } A list of disk-space requirements for this dependency. Defaults to an empty list. An explicit interface implementation is also provided.

  • List<IToolchainDependency.DiskSpaceRequirements> IToolchainDependency.spaceRequirements { get; set; } Explicit interface implementation delegating to the public spaceRequirements property.

  • public virtual bool confirmUninstallation { get; } Whether the UI should prompt to confirm uninstallation. Default: false.

  • public virtual bool canBeInstalled { get; } Whether this dependency can be installed. Default: true.

  • public virtual bool canBeUninstalled { get; } Whether this dependency can be uninstalled. Default: true.

  • public virtual bool canChangeInstallationDirectory { get; } Whether the user can change the installation directory. Default: false.

  • public virtual string installationDirectory { get; set; } Installation directory string. Default: string.Empty. Subclasses or UI can get/set this.

  • public virtual LocalizedString description { get; } Localized description. Default uses "Options.OPTION_DESCRIPTION[ModdingSettings." + GetType().Name + "]".

  • public virtual LocalizedString installDescr { get; } Localized install short description. Default returns localizedName.

  • public virtual LocalizedString uninstallDescr { get; } Localized uninstall short description. Default returns localizedName.

  • public virtual LocalizedString uninstallMessage { get; } Optional localized message presented when uninstalling. Default: default(LocalizedString).

  • public virtual Type[] dependsOnInstallation { get; } Array of Types representing other dependencies that must be installed first. Default: empty array.

  • public virtual Type[] dependsOnUninstallation { get; } Array of Types representing dependencies that must be uninstalled first. Default: empty array.

  • public virtual IEnumerable<string> envVariables { get; } Enumerates environment variables this dependency wants to expose. Default: empty (yield break).

  • public event IToolchainDependency.ProgressDelegate onNotifyProgress Event raised when state/progress changes. The state property's setter triggers this event.

Constructors

  • public BaseDependency() The class does not declare an explicit constructor; a public parameterless default constructor is available. Subclasses should call base() implicitly. Initialization of defaults (spaceRequirements, installationDirectory, etc.) is done inline on the fields/properties.

Methods

  • public virtual Task<bool> IsInstalled(CancellationToken token) Default implementation returns Task.FromResult(false). Override to check if dependency is already installed.

  • public virtual Task<bool> IsUpToDate(CancellationToken token) Default implementation returns Task.FromResult(true). Override to determine whether installed version is current.

  • public virtual Task<bool> NeedDownload(CancellationToken token) Default implementation returns Task.FromResult(false). Override to determine whether a download is required.

  • public virtual Task Download(CancellationToken token) Default: Task.CompletedTask. Override to implement custom download behavior. Note: a protected static Download helper is provided below.

  • public virtual Task Install(CancellationToken token) Default: Task.CompletedTask. Override to implement install logic.

  • public virtual Task Uninstall(CancellationToken token) Default: Task.CompletedTask. Override to implement uninstall logic.

  • public virtual Task Refresh(CancellationToken token) Default implementation calls the static IToolchainDependency.Refresh(this, token) helper. Override only if custom refresh logic is needed.

  • public virtual Task<List<IToolchainDependency.DiskSpaceRequirements>> GetRequiredDiskSpace(CancellationToken token) Default returns an empty list. Override to report required disk space before installation.

  • public virtual LocalizedString GetLocalizedState(bool includeProgress) Returns a localized string for the current state. Default uses IToolchainDependency.GetLocalizedState(state, includeProgress).

  • public virtual LocalizedString GetLocalizedVersion() Returns LocalizedString.Value(version) by default. Can be overridden to provide different formatting.

  • public override int GetHashCode() Combines m_State and ((IToolchainDependency)this).availableActions into a hash code. Useful for collections; can be left as-is or overridden.

  • public override string ToString() Returns the dependency's name.

  • protected static async Task Download(BaseDependency dependency, CancellationToken token, string url, string pathOnDisk, string detail) A protected helper that performs a file download using UnityWebRequest and a DownloadHandlerFile. Behavior:

  • Sets dependency.state to Downloading and provides detail text.
  • Creates a UnityWebRequest.Get(url) and attaches a DownloadHandlerFile to save to pathOnDisk (removeFileOnAbort = true).
  • Awaits webRequest.SendWebRequest() using a ConfigureAwait that provides progress updates via the local UpdateProgress callback.
  • If the request fails, throws ToolchainException with appropriate ToolchainError (Download) and message based on UnityWebRequest.Result and webRequest.error.
  • Updates dependency.state progress while the request is in-flight by invoking dependency.state = new State(DependencyState.Downloading, detail, progressPercent).
  • Wraps exceptions into ToolchainException where relevant and rethrows OperationCanceledException when cancellation occurs.

This helper centralizes error handling and progress reporting for downloads; subclasses can call it with a URL and target path to perform robust downloads that update UI state.

Usage Example

// Example subclass that overrides basic checks and uses the protected Download helper.
public class ExampleSdkDependency : BaseDependency
{
    public ExampleSdkDependency()
    {
        version = "1.2.3";
        needDownload = true;
        installationDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "ExampleSdk");
    }

    public override async Task<bool> IsInstalled(CancellationToken token)
    {
        // Simple existence check
        return Directory.Exists(installationDirectory) && File.Exists(Path.Combine(installationDirectory, "sdk.marker"));
    }

    public override async Task<bool> NeedDownload(CancellationToken token)
    {
        // Check remote manifest or versioning to determine if download is necessary.
        return true;
    }

    public override async Task Download(CancellationToken token)
    {
        // Use the protected static helper to download to a temp file, handling progress/state automatically.
        string tempPath = Path.Combine(Path.GetTempPath(), "example-sdk.zip");
        string url = "https://example.com/downloads/example-sdk-1.2.3.zip";
        await Download(this, token, url, tempPath, "Downloading Example SDK");
        // After download completes, you could extract and prepare files in Install().
    }

    public override async Task Install(CancellationToken token)
    {
        // Perform extraction / move files into installationDirectory, set state accordingly.
        this.state = new IToolchainDependency.State(DependencyState.Installing, "Installing Example SDK");
        // ... install logic ...
        this.state = new IToolchainDependency.State(DependencyState.Installed);
    }
}