Skip to content

Finds the path to an installed game from its name, with MSBuild support

License

Notifications You must be signed in to change notification settings

SubnauticaNitrox/Nitrox.Discovery

Repository files navigation

Nitrox.Discovery

NuGet

Discovers the path to an installed game from a given game name. Supports the most popular game stores and on both Windows and Linux.

Use with MSBuild (minimized setup)

<Target Name="FindGameAndIncludeReferences" BeforeTargets="ResolveAssemblyReferences" Condition="'$(_NitroxDiscovery_TaskAssembly)' != ''">
    <DiscoverGame GameName="Subnautica"> <!-- CHANGE THIS -->
        <Output TaskParameter="GamePath" PropertyName="GameDir" />
    </DiscoverGame>
    <Error Condition="'$(GameDir)' == ''" Text="Failed to find the game 'Subnautica' on your machine" />

    <!-- Load any references to game DLLs here -->
    <ItemGroup>
        <Reference Include="MyGameDll">
            <HintPath>$(GameDir)\bin\MyGameDll.dll</HintPath>
        </Reference>
    </ItemGroup>
</Target>

Important

Condition="'$(_NitroxDiscovery_TaskAssembly)' != ''" is needed so Visual Studio can still load Nuget packages without requiring DiscoverGame task to exist!

These options are available for DiscoverGame task

[Required]
public string GameName { get; set; }
/// <summary>
/// Name of the game executable, with or without extension.
/// </summary>
public string ExeName { get; set; } = "";
/// <summary>
/// Relative depth to search within the game root for the game executable. Default is 0 unless on OSX, then 1.
/// </summary>
/// <remarks>
/// As OSX has different file structure (.app/Contents/MacOS or .app/Contents/Resources, the default is 1).
/// </remarks>
public int ExeSearchDepth { get; set; } = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 1 : 0;
/// <summary>
/// Specifies the libraries to include in the search. The search will start in the same order as given.
/// Leave empty to search all libraries. See <see cref="GameLibraries" /> for valid library names.
/// </summary>
/// <remarks>
/// End with "All" to include all libraries in the search, but start the search with the preceding library names.
/// For example: <code>Steam;Epic;All</code>
/// </remarks>
public ITaskItem[] IncludeLibraries { get; set; } = [];
public string IntermediateOutputPath { get; set; }

Recommended setup

<Target Name="FindGameAndIncludeReferences" BeforeTargets="ResolveAssemblyReferences" Condition="'$(_NitroxDiscovery_TaskAssembly)' != ''">
    <PropertyGroup>
        <!-- Change this (optional: can be put in your Directory.Build.props file) -->
        <GameName>MY_GAME</GameName>
    </PropertyGroup>
    <DiscoverGame GameName="$(GameName)">
        <Output TaskParameter="GamePath" PropertyName="GameDir" />
    </DiscoverGame>
    <Error Condition="'$(GameDir)' == ''" Text="Failed to find the game '$(GameName)' on your machine" />
    <PropertyGroup>
        <GameDir>$(GameDir)\</GameDir>
    </PropertyGroup>
    <!-- Optional: do other checks on game (e.g. version check) -->
    <Message Importance="high" Text="Game found at: '$(GameDir)'" />

    <!-- Load any references to game DLLs here -->
    <ItemGroup>
        <Reference Include="MyGameDll">
            <HintPath>$(GameDir)bin\MyGameDll.dll</HintPath>
        </Reference>
    </ItemGroup>
</Target>

You can append other references on a per project basis like this

<Target Name="MoreGameReferences" AfterTargets="FindGameAndIncludeReferences">
    <ItemGroup>
        <Reference Include="SomeOtherGameDll">
            <HintPath>$(GameDir)bin\SomeOtherGameDll.dll</HintPath>
        </Reference>
    </ItemGroup>
</Target>