2021-06-09 12:54:42 -04:00
// Copyright Epic Games, Inc. All Rights Reserved.
using EpicGames.Core ;
using System ;
using System.Collections.Generic ;
using System.IO ;
using System.Reflection ;
using System.Text ;
namespace UnrealBuildBase
{
2021-06-11 18:20:44 -04:00
public static class Unreal
2021-06-09 12:54:42 -04:00
{
private static DirectoryReference FindRootDirectory ( )
{
2021-06-14 10:54:00 -04:00
if ( LocationOverride . RootDirectory ! = null )
{
return LocationOverride . RootDirectory ;
}
2021-06-09 12:54:42 -04:00
// This base library may be used - and so be launched - from more than one location (at time of writing, UnrealBuildTool and AutomationTool)
// Programs that use this assembly must be located under "Engine/Binaries/DotNET" and so we look for that sequence of directories in that path of the executing assembly
// Use the EntryAssembly (the application path), rather than the ExecutingAssembly (the library path)
string AssemblyLocation = Assembly . GetEntryAssembly ( ) . GetOriginalLocation ( ) ;
2021-06-17 10:42:37 -04:00
DirectoryReference ? FoundRootDirectory = DirectoryReference . FindCorrectCase ( DirectoryReference . FromString ( AssemblyLocation ) ! ) ;
2021-06-09 12:54:42 -04:00
// Search up through the directory tree for the deepest instance of the sub-path "Engine/Binaries/DotNET"
while ( FoundRootDirectory ! = null )
{
if ( String . Equals ( "DotNET" , FoundRootDirectory . GetDirectoryName ( ) ) )
{
FoundRootDirectory = FoundRootDirectory . ParentDirectory ;
if ( FoundRootDirectory ! = null & & String . Equals ( "Binaries" , FoundRootDirectory . GetDirectoryName ( ) ) )
{
FoundRootDirectory = FoundRootDirectory . ParentDirectory ;
if ( FoundRootDirectory ! = null & & String . Equals ( "Engine" , FoundRootDirectory . GetDirectoryName ( ) ) )
{
FoundRootDirectory = FoundRootDirectory . ParentDirectory ;
break ;
}
continue ;
}
continue ;
}
FoundRootDirectory = FoundRootDirectory . ParentDirectory ;
}
if ( FoundRootDirectory = = null )
{
throw new Exception ( $"The BuildUtilities assembly requires that applications are launched from a path containing \" Engine / Binaries / DotNET \ ". This application was launched from {Path.GetDirectoryName(AssemblyLocation)}" ) ;
}
// Confirm that we've found a valid root directory, by testing for the existence of a well-known file
FileReference ExpectedExistingFile = FileReference . Combine ( FoundRootDirectory , "Engine" , "Build" , "Build.version" ) ;
if ( ! FileReference . Exists ( ExpectedExistingFile ) )
{
throw new Exception ( $"Expected file \" Engine / Build / Build . version \ " was not found at {ExpectedExistingFile.FullName}" ) ;
}
return FoundRootDirectory ;
}
private static FileReference FindUnrealBuildTool ( )
{
// todo: use UnrealBuildTool.dll (same on all platforms). Will require changes wherever UnrealBuildTool is invoked.
2021-06-17 10:42:37 -04:00
string UBTName = "UnrealBuildTool" + RuntimePlatform . ExeExtension ;
2021-06-09 12:54:42 -04:00
// the UnrealBuildTool executable is assumed to be located under {RootDirectory}/Engine/Binaries/DotNET/UnrealBuildTool/
FileReference UnrealBuildToolPath = FileReference . Combine ( EngineDirectory , "Binaries" , "DotNET" , "UnrealBuildTool" , UBTName ) ;
2021-06-09 16:34:49 -04:00
UnrealBuildToolPath = FileReference . FindCorrectCase ( UnrealBuildToolPath ) ;
2021-06-09 12:54:42 -04:00
2021-06-09 16:34:49 -04:00
if ( ! FileReference . Exists ( UnrealBuildToolPath ) )
2021-06-09 12:54:42 -04:00
{
2021-06-09 16:34:49 -04:00
throw new Exception ( $"Unable to find {UBTName} in the expected location at {UnrealBuildToolPath.FullName}" ) ;
2021-06-09 12:54:42 -04:00
}
2021-06-09 16:34:49 -04:00
return UnrealBuildToolPath ;
2021-06-09 12:54:42 -04:00
}
2021-06-17 10:26:52 -04:00
static private DirectoryReference FindDotnetDirectory ( )
{
string HostDotNetDirectoryName ;
switch ( RuntimePlatform . Current )
{
case RuntimePlatform . Type . Windows : HostDotNetDirectoryName = "Windows" ; break ;
case RuntimePlatform . Type . Mac : HostDotNetDirectoryName = "Mac" ; break ;
case RuntimePlatform . Type . Linux : HostDotNetDirectoryName = "Linux" ; break ;
default : throw new Exception ( "Unknown host platform" ) ;
}
return DirectoryReference . Combine ( EngineDirectory , "Binaries" , "ThirdParty" , "DotNet" , HostDotNetDirectoryName ) ;
}
2021-06-09 12:54:42 -04:00
/// <summary>
/// The full name of the root UE directory
/// </summary>
public static readonly DirectoryReference RootDirectory = FindRootDirectory ( ) ;
/// <summary>
/// The full name of the Engine directory
/// </summary>
public static readonly DirectoryReference EngineDirectory = DirectoryReference . Combine ( RootDirectory , "Engine" ) ;
/// <summary>
/// The path to UBT
/// </summary>
public static readonly FileReference UnrealBuildToolPath = FindUnrealBuildTool ( ) ;
2021-06-11 11:37:23 -04:00
2021-06-17 10:26:52 -04:00
/// <summary>
/// The directory containing the bundled .NET installation
/// </summary>
static public readonly DirectoryReference DotnetDirectory = FindDotnetDirectory ( ) ;
2021-06-17 10:42:37 -04:00
/// <summary>
/// The path of the bundled dotnet executable
/// </summary>
static public readonly FileReference DotnetPath = FileReference . Combine ( DotnetDirectory , "dotnet" + RuntimePlatform . ExeExtension ) ;
2021-06-11 11:37:23 -04:00
/// <summary>
/// Whether we're running with engine installed
/// </summary>
static private bool? bIsEngineInstalled ;
/// <summary>
/// Returns true if UnrealBuildTool is running using installed Engine components
/// </summary>
/// <returns>True if running using installed Engine components</returns>
static public bool IsEngineInstalled ( )
{
if ( ! bIsEngineInstalled . HasValue )
{
2021-06-11 18:20:44 -04:00
bIsEngineInstalled = FileReference . Exists ( FileReference . Combine ( EngineDirectory , "Build" , "InstalledBuild.txt" ) ) ;
2021-06-11 11:37:23 -04:00
}
return bIsEngineInstalled . Value ;
}
2021-06-14 10:54:00 -04:00
public static class LocationOverride
{
/// <summary>
/// If set, this value will be used to populate Unreal.RootDirectory
/// </summary>
public static DirectoryReference ? RootDirectory = null ;
}
2021-06-09 12:54:42 -04:00
}
}