Files
UnrealEngineUWP/Engine/Source/Programs/UnrealBuildTool/System/Utils.cs
Ben Marsh 7c77078af1 Copying //UE4/Dev-Core to //UE4/Dev-Main (Source: //UE4/Dev-Core @ 3345728)
#lockdown Nick.Penwarden

==========================
MAJOR FEATURES + CHANGES
==========================

Change 3315219 on 2017/02/21 by Steve.Robb

	Fix for FObjectAndNameAsStringProxyArchive when serializing a TWeakObjectPtr.

Change 3315285 on 2017/02/21 by Steve.Robb

	Explicitly pass string builder into code generation functions.

Change 3315341 on 2017/02/21 by Ben.Marsh

	UAT: Clean up some formatting in StreamCopyDescription output - remove #fyi lines, exclude merge commits, and remove some blank lines.

Change 3315350 on 2017/02/21 by Ben.Marsh

	Fix shared resource files not being rebuilt if the version header changes.

Change 3315823 on 2017/02/21 by Ben.Marsh

	UAT: Use a class derived from AutomationException to return information specific to commandlets failing, rather than putting it in the base class.

Change 3315826 on 2017/02/21 by Ben.Marsh

	UAT: Move Distiller class from general use in UAT; FileFilter provides a much safer and fully featured implementation of the same concepts.

Change 3315857 on 2017/02/21 by Ben.Marsh

	UBT: Remove the StripBaseDirectory() and MakeRerootedFilePath() utility functions from UBT. These operations can now be done more safely with FileReference objects.

Change 3315942 on 2017/02/21 by Ben.Marsh

	UBT: Convert FileFilter to use FileReference and DirectoryReference arguments everywhere.

Change 3316236 on 2017/02/22 by Maciej.Mroz

	#jira UE-42045

	Nativization
	Fixed Warning: TEnumAsByte is not intended for use with enum

Change 3316253 on 2017/02/22 by Robert.Manuszewski

	Fixes for the async log file writer hangs and crashes.

	- potential fix for the logging system hang when running out of disk space while flushing log
	- fix for unexpected concurrency assert when flushing the log buffer to disk

Change 3316293 on 2017/02/22 by Steve.Robb

	GetTypeHash and lexicographical comparison operators (operator<() etc.) for TTuple.

Change 3316342 on 2017/02/22 by Maciej.Mroz

	Nativization: Wrappers (stubs) required only by other wrappers are properly generated.

	#codereview: Mike.Beach

Change 3316344 on 2017/02/22 by Maciej.Mroz

	Fixed crash in nativized Odin
	Async loading properly handles nativized structs.

Change 3316359 on 2017/02/22 by Steve.Robb

	GitHub #3287 : Ignore #pragma in USTRUCTs

	#jira UE-42248

Change 3316389 on 2017/02/22 by Matthew.Griffin

	Switched Installed Engine Filters to multiline properties to make them more readable
	Added Oodle to list of excluded plugins
	#jira UE-42030

Change 3316392 on 2017/02/22 by Ben.Marsh

	UBT: Split out FileReference/DirectoryReference classes into their own file.

Change 3316394 on 2017/02/22 by Ben.Marsh

	UBT: Move FileReference/DirectoryReference extension methods into the appropriate file.

Change 3316411 on 2017/02/22 by Ben.Marsh

	UAT: Remove file functions that take multiple arguments. There's not really a compelling use case for these to exist over looping from the calling code.

Change 3316446 on 2017/02/22 by Ben.Marsh

	UAT: Try disabling function name prefix to log output from UAT, to see if it improves readability. Function names are still included in the log file for debugging.

Change 3316575 on 2017/02/22 by Ben.Marsh

	UAT: Remove unused functionality for dealing with labels, and output a more human readable list of P4 settings at startup.

Change 3318481 on 2017/02/22 by Steve.Robb

	Use of FMath::IsPowerOfTwo in check.
	Static assert to ensure that an inline set allocator will have a hash size of a power of two.

Change 3318496 on 2017/02/22 by Steve.Robb

	Fix for TSet visualizers.

Change 3318919 on 2017/02/23 by Steve.Robb

	Fix for hot reloading UScriptStruct-derived objects in a module, where the CDOs of these objects haven't had PrepareCppStructOps() called on them.

	#jira UE-42178

Change 3318942 on 2017/02/23 by Steve.Robb

	Removal of a redundant insertion which can cause problems on reallocation of the map.

Change 3319010 on 2017/02/23 by Ben.Marsh

	UBT: Fix exception when a file that was previously part of the working set is deleted.

Change 3319134 on 2017/02/23 by Robert.Manuszewski

	Better fix for a deadlock when flushing log while it's already being flushed due to flush timer on the async log writer thread.

Change 3319249 on 2017/02/23 by Matthew.Griffin

	Added a function to check if running with debug game libs instead of checking command line in multiple places
	Added -RunConfig parameter, which has equivalent result to -debug if value of parameter starts with 'debug'
	Added -RunConfig=$(Configuration) as a default commandline argument for Mac so that editor can use debug game libs
	Removed -Shipping argument from VCProject generation as it's not used anymore

Change 3319253 on 2017/02/23 by Maciej.Mroz

	#jira UE-41846

	New mechanism to gather modules necessary for Nativized Assets. The modules are listed based on included headers. Previously the dependencies was gathered only in FBlueprintNativeCodeGenManifest::GatherModuleDependencies.

Change 3319591 on 2017/02/23 by Ben.Marsh

	Don't strip prefixes beginning with WARNING: or ERROR: using the Postp filter.

Change 3320357 on 2017/02/23 by Steven.Hutton

	Slight changes to Add Crash method - Returning select fields instead of entity objects in queries for perf reasons.

Change 3320361 on 2017/02/23 by Steven.Hutton

	Performance improvements subsequent to the recent database changes.

Change 3320446 on 2017/02/23 by Steven.Hutton

	adding my temporary performance tracker class - reports to a private slack channel with add crash performance data.

Change 3320479 on 2017/02/23 by Ben.Marsh

	Fix CIS errors.

Change 3320576 on 2017/02/23 by Jin.Zhang

	Update CrashReporter to use AWS

Change 3320742 on 2017/02/23 by Jin.Zhang

	Merging crash caching

Change 3321119 on 2017/02/24 by Robert.Manuszewski

	DLL injection protection support for non-monolithic builds

Change 3323308 on 2017/02/27 by Matthew.Griffin

	Moved compilation of SwarmInterface after its dependencies so that we will see a build failure immediately if they change version in future

Change 3323423 on 2017/02/27 by Chad.Garyet

	Adding a script to check and warn about csproj targeted .net versions being mismatched

	#JIRA UE-39624

Change 3323442 on 2017/02/27 by Ben.Marsh

	UBT: Output an error if an engine module references a game module.

Change 3323743 on 2017/02/27 by Ben.Marsh

	PR #3303: Resolved PVS scan issues (Contributed by projectgheist)

Change 3323748 on 2017/02/27 by Ben.Marsh

	Convert whitespace to tabs.

Change 3324851 on 2017/02/28 by Chris.Wood

	Add Odin symbol locations to engine config for MDD on CR server.

	NotForLicensees

Change 3324979 on 2017/02/28 by Gil.Gribb

	Fixed bad merge of priority change in the EDL.

Change 3326889 on 2017/03/01 by Steven.Hutton

	Update to buggs controller to generate faster queries.

Change 3326910 on 2017/03/01 by Robert.Manuszewski

	Removing legacy #if from PackageFileSummary.

Change 3327118 on 2017/03/01 by Gil.Gribb

	UE4 - Fixed race that resulted in a memory leak when reading compressed paks.

Change 3327633 on 2017/03/01 by Gil.Gribb

	UE4 - Added a cvar to control the pak precacher thottle.

Change 3327674 on 2017/03/01 by Steve.Robb

	Unified boilerplate between all generated code files.

Change 3328544 on 2017/03/01 by Chris.Wood

	CrashReportProcess.config update (CRP v1.2.17)

	Tweaks to a few values.
	Update website URL to explicitly point to old, non-cloud site on devweb-02.

Change 3328714 on 2017/03/01 by Chris.Wood

	Correct CRP config regression. Point website at new cloud site. Still v1.2.17

Change 3329192 on 2017/03/02 by Matthew.Griffin

	Added Shared Build Id file to the list of Precompiled Build Dependencies in a target receipt so that it's brought into an installed build

Change 3329285 on 2017/03/02 by Ben.Marsh

	UGS: Allow a project to specify a filters for the streams that should be displayed for fast-switching to. The QuickSelectStreamList seting in the [Options] section of the project settings references a depot path containing a list of strings used to filter the stream list. An option is shown to switch back to showing all available streams, if desired.

Change 3330636 on 2017/03/02 by Ben.Marsh

	UBT: Bump version number of C++ include cache to force it to be rebuilt with additional include information for the default RC files.

Change 3331262 on 2017/03/03 by Robert.Manuszewski

	Merging Dev-LoadTimes to Dev-Core (Garbage Collection performance improvements)

	- Improved GC multithreading
	- Improved BeginDestroy performance
	- Introduced ULevelActorCluster for StaticMeshActor and ReflectionCapture actor clustering (can be toggled through project settings or console command gc.ActorClusterEnabled)
	- A few improvements to AddReferencedObjects functions
	- Misc improvements to GC code
	- Garbage Collector now properly handles clusters which had their objects marked as pending kill
	- Blueprints can now create clusters too (can be toggled through project settings or console command gc.BlueprintClusteringEnabled, defaults to disabled)

Change 3331285 on 2017/03/03 by Robert.Manuszewski

	A few fixes for the previous check-in.

Change 3332001 on 2017/03/03 by Ben.Marsh

	UBT: Add support for generating a UDN file containing the valid settings for BuildConfiguration.xml. Pass -configdoc=<filename> on the command line to generate such a file.

Change 3332022 on 2017/03/03 by Ben.Marsh

	Update documentation for where to find the BuildConfiguration settings.

Change 3332031 on 2017/03/03 by Ben.Marsh

	Remove documentation for Windows XP support; it has been removed in the 4.16 release.

Change 3332256 on 2017/03/03 by Ben.Marsh

	UBT: Add support for generating a UDN page containing module and target settings.

Change 3332458 on 2017/03/03 by Ben.Marsh

	UBT: Improvements to generated documentation.

Change 3332459 on 2017/03/03 by Ben.Marsh

	Add generated documentation for .target.cs files, .build.cs files, and BuildConfiguration.xml files.

Change 3332460 on 2017/03/03 by Ben.Marsh

	UBT: Make LinkTypePrivate actually private, so it doesn't show up in the docs.

Change 3332899 on 2017/03/06 by Robert.Manuszewski

	Making sure actor clustering is not used in the editor (fix for actors being deleted when GC runs in the editor)

	#jira UE-42548

Change 3332955 on 2017/03/06 by Maciej.Mroz

	Nativization distinguishes client and server platform:
	- Separated lists on additional assets, additional modules, excluded assets, excluded modules, excluded paths (in config)
	- Context (compilation options, nativization options and platform) is deliveren to BPCOmpilerCppBackend in FCompilerNativizationOptions struct.
	- Wrappers (for unconverted BPs) are created only when they are directly called.

	- Fortnite dedicated server can be nativized

Change 3332990 on 2017/03/06 by Ben.Marsh

	UBT: Add more comprehensive wrapper methods for System.IO.File and System.IO.Directory to FileReference and DirectoryReference.

Change 3333032 on 2017/03/06 by Ben.Marsh

	Documentation for build tools

Change 3333037 on 2017/03/06 by Ben.Marsh

	Add a build step to extract UAT and UBT documentation from XML comments.

Change 3333089 on 2017/03/06 by Ben.Marsh

	UAT: Re-enable logging the calling function to the console in UAT. Needs a pass for readability first.

Change 3333651 on 2017/03/06 by Gil.Gribb

	UE4 - Fix a werid recursive situation where StaticLoadObject could return an object that has not finished loading. Also produces a fatal error if this sometimes happens. EDL only.

Change 3335236 on 2017/03/07 by Ben.Marsh

	UGS: Set the sync changelist separately to the compatibility changelist.

Change 3335261 on 2017/03/07 by Gil.Gribb

	UE4 - Fixed batched render fences when BeginDestroy calls FlushRenderingCommands.

Change 3335740 on 2017/03/07 by Gil.Gribb

	maybe fix static analysis warning

Change 3335945 on 2017/03/07 by Steve.Robb

	Move FFindInstancedReferenceSubobjectHelper code out of header.
	Add map/set property support to allow instanced members of these container types to be handled during CPFUO.

	https://udn.unrealengine.com/questions/349232/tmap-with-instanced-object-as-value-gets-cleared-o.html

Change 3336693 on 2017/03/07 by Ben.Marsh

	UBT: Use shared PCHs for game plugins by default, to reduce time spent generating individual PCHs.

Change 3336694 on 2017/03/07 by Steve.Robb

	Static assert added to TMap to prevent the use of keys which don't implement a GetTypeHash.
	Fixes to types which relied on implicit conversions when calling GetTypeHash.
	Workaround in SAssetView.h and PropertyEditorModule.h for an apparent VC bug where the compiler wrongly instantiates TPointerIsConvertibleFromTo for certain forward-declared types, causing future TSharedPtr conversions to fail.

	#jira UE-42441

Change 3336698 on 2017/03/07 by Steve.Robb

	Hardcoded endpoint handling replaced with a generic string.
	Obsolete .proto and .java code generation removed.

Change 3336811 on 2017/03/07 by Wes.Hunt

	Add a game blacklist to the crash report processor. Fixed a syntax error in Config.cs, added a XML comment to shut up a warning.

Change 3336973 on 2017/03/08 by Steve.Robb

	Fix for missing GetTypeHash in a plugin.

Change 3336996 on 2017/03/08 by Steve.Robb

	Significant refactor of code generation, to try and make data flow more apparent.

Change 3337571 on 2017/03/08 by Steve.Robb

	CIS fixes for missing GetTypeHash functions.
	Non-unity fix.

Change 3337588 on 2017/03/08 by Gil.Gribb

	UE4 - Fixed obscure check with flushing rhi resources.

Change 3337620 on 2017/03/08 by Steve.Robb

	WITH_HOT_RELOAD_CTORS macros removed.
	UseVTableConstructors config option removed.

Change 3339369 on 2017/03/09 by Steve.Robb

	GetTypeHash overload for nn::account::Uid.

Change 3339464 on 2017/03/09 by Daniel.Lamb

	Fixed assert in 4.15 to do with trying to gather dependency info from invalid packages.
	#jira UE-42583
	#test Editor + Cook + Run shootergame

Change 3339465 on 2017/03/09 by Maciej.Mroz

	Fixed serialization issue, after UserDefinedEnum was used in EnumProperty.

Change 3339469 on 2017/03/09 by Maciej.Mroz

	Fixed Nativization problem, when default value is passed as non-const reference.

Change 3340178 on 2017/03/09 by Daniel.Lamb

	Added support for in memory only packages.  The Cooker ignores these and added core functions to recognize these packages.
	Other systems will need to add support where nessisary.

Change 3341002 on 2017/03/10 by Maciej.Mroz

	Nativization: Fixed FFindHeadersToInclude. Headers necessary for owners of subobjects are properly included.

Change 3341076 on 2017/03/10 by Steve.Robb

	Fix for FBakedTextureSourceInfo move semantics.

	#jira UE-42658

Change 3341160 on 2017/03/10 by Gil.Gribb

	UE4 - Fix hazard with SetMaterialUsage from a thread.

Change 3341409 on 2017/03/10 by Steve.Robb

	Reduction of the generated code size for StaticRegisterNatives functions.

Change 3341523 on 2017/03/10 by Steve.Robb

	Code generation simplified.

Change 3341800 on 2017/03/10 by Ben.Marsh

	UnrealVS: Fix UnrealVS compatibility with RTM version of Visual Studio 2017. 2017 toolchain for extensions is no longer able to build <= 2015 extensions due to validation of the VSIX manifest, so create a separate solution for it.

Change 3342034 on 2017/03/10 by Ben.Marsh

	Fix compiler setting not being loaded correctly into the Windows target settings dialog.

	#jira UE-42746

Change 3342041 on 2017/03/10 by Ben.Marsh

	Fix -ErrorOnEngineContentUse not being set in the cooker options correctly.

Change 3342094 on 2017/03/10 by Steve.Robb

	Fix to deteministic name order during code generation.

Change 3342251 on 2017/03/10 by Daniel.Lamb

	Integrate fix for resave lightmaps commandlet when upgrading from no mapbuilddatapackages to mapbuilddatapackages.
	#thanks Tim.Hagberg

	#test None

Change 3342961 on 2017/03/13 by Robert.Manuszewski

	Fixing memory leak when playing while running -nullrhi on the commandline in cooked games caused by shader resources not being destroyed.

	#jira FORT-38977

Change 3343022 on 2017/03/13 by Steve.Robb

	GetTypeHash fixes for FUniqueNetIdLive.

	#jira UE-42788

Change 3343448 on 2017/03/13 by Steve.Robb

	Compiled-in defer object order fixed.
	Debuggability of the deferred registration map improved.

	#jira UE-42828

[CL 3345747 by Ben Marsh in Main branch]
2017-03-14 15:48:33 -04:00

782 lines
28 KiB
C#

// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.
using System;
using System.Collections.Generic;
using System.IO;
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Serialization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Linq;
using System.Management;
using Tools.DotNETCommon.CaselessDictionary;
using System.Web.Script.Serialization;
namespace UnrealBuildTool
{
/// <summary>
/// Utility functions
/// </summary>
public static class Utils
{
/// <summary>
/// Whether we are currently running on Mono platform. We cache this statically because it is a bit slow to check.
/// </summary>
public static readonly bool IsRunningOnMono = Type.GetType("Mono.Runtime") != null;
/// <summary>
/// Searches for a flag in a set of command-line arguments.
/// </summary>
public static bool ParseCommandLineFlag(string[] Arguments, string FlagName, out int ArgumentIndex)
{
// Find an argument with the given name.
for (ArgumentIndex = 0; ArgumentIndex < Arguments.Length; ArgumentIndex++)
{
string Argument = Arguments[ArgumentIndex].ToUpperInvariant();
if (Argument == FlagName.ToUpperInvariant())
{
return true;
}
}
return false;
}
/// <summary>
/// Regular expression to match $(ENV) and/ or %ENV% environment variables.
/// </summary>
static Regex EnvironmentVariableRegex = new Regex(@"\$\((.*?)\)|\%(.*?)\%", RegexOptions.None);
/// <summary>
/// Resolves $(ENV) and/ or %ENV% to the value of the environment variable in the passed in string.
/// </summary>
/// <param name="InString">String to resolve environment variable in.</param>
/// <returns>String with environment variable expanded/ resolved.</returns>
public static string ResolveEnvironmentVariable(string InString)
{
string Result = InString;
// Try to find $(ENV) substring.
Match M = EnvironmentVariableRegex.Match(InString);
// Iterate over all matches, resolving the match to an environment variable.
while (M.Success)
{
// Convoluted way of stripping first and last character and '(' in the case of $(ENV) to get to ENV
string EnvironmentVariable = M.ToString();
if (EnvironmentVariable.StartsWith("$") && EnvironmentVariable.EndsWith(")"))
{
EnvironmentVariable = EnvironmentVariable.Substring(1, EnvironmentVariable.Length - 2).Replace("(", "");
}
if (EnvironmentVariable.StartsWith("%") && EnvironmentVariable.EndsWith("%"))
{
EnvironmentVariable = EnvironmentVariable.Substring(1, EnvironmentVariable.Length - 2);
}
// Resolve environment variable.
Result = Result.Replace(M.ToString(), Environment.GetEnvironmentVariable(EnvironmentVariable));
// Move on to next match. Multiple environment variables are handled correctly by regexp.
M = M.NextMatch();
}
return Result;
}
/// <summary>
/// Expands variables in $(VarName) format in the given string. Variables are retrieved from the given dictionary, or through the environment of the current process.
/// Any unknown variables are ignored.
/// </summary>
/// <param name="InputString">String to search for variable names</param>
/// <param name="AdditionalVariables">Lookup of variable names to values</param>
/// <returns>String with all variables replaced</returns>
public static string ExpandVariables(string InputString, Dictionary<string, string> AdditionalVariables = null)
{
string Result = InputString;
for (int Idx = Result.IndexOf("$("); Idx != -1; Idx = Result.IndexOf("$(", Idx))
{
// Find the end of the variable name
int EndIdx = Result.IndexOf(')', Idx + 2);
if (EndIdx == -1)
{
break;
}
// Extract the variable name from the string
string Name = Result.Substring(Idx + 2, EndIdx - (Idx + 2));
// Find the value for it, either from the dictionary or the environment block
string Value;
if (AdditionalVariables == null || !AdditionalVariables.TryGetValue(Name, out Value))
{
Value = Environment.GetEnvironmentVariable(Name);
if (Value == null)
{
Idx = EndIdx + 1;
continue;
}
}
// Replace the variable, or skip past it
Result = Result.Substring(0, Idx) + Value + Result.Substring(EndIdx + 1);
}
return Result;
}
/// <summary>
/// This is a faster replacement of File.ReadAllText. Code snippet based on code
/// and analysis by Sam Allen
/// http://dotnetperls.com/Content/File-Handling.aspx
/// </summary>
/// <param name="SourceFile"> Source file to fully read and convert to string</param>
/// <returns>Textual representation of file.</returns>
public static string ReadAllText(string SourceFile)
{
using (StreamReader Reader = new StreamReader(SourceFile, System.Text.Encoding.UTF8))
{
return Reader.ReadToEnd();
}
}
/// <summary>
/// Reads the specified environment variable
/// </summary>
/// <param name="VarName"> the environment variable to read</param>
/// <param name="bDefault">the default value to use if missing</param>
/// <returns>the value of the environment variable if found and the default value if missing</returns>
public static bool GetEnvironmentVariable(string VarName, bool bDefault)
{
string Value = Environment.GetEnvironmentVariable(VarName);
if (Value != null)
{
// Convert the string to its boolean value
return Convert.ToBoolean(Value);
}
return bDefault;
}
/// <summary>
/// Reads the specified environment variable
/// </summary>
/// <param name="VarName"> the environment variable to read</param>
/// <param name="Default">the default value to use if missing</param>
/// <returns>the value of the environment variable if found and the default value if missing</returns>
public static string GetStringEnvironmentVariable(string VarName, string Default)
{
string Value = Environment.GetEnvironmentVariable(VarName);
if (Value != null)
{
return Value;
}
return Default;
}
/// <summary>
/// Reads the specified environment variable
/// </summary>
/// <param name="VarName"> the environment variable to read</param>
/// <param name="Default">the default value to use if missing</param>
/// <returns>the value of the environment variable if found and the default value if missing</returns>
public static double GetEnvironmentVariable(string VarName, double Default)
{
string Value = Environment.GetEnvironmentVariable(VarName);
if (Value != null)
{
return Convert.ToDouble(Value);
}
return Default;
}
/// <summary>
/// Reads the specified environment variable
/// </summary>
/// <param name="VarName"> the environment variable to read</param>
/// <param name="Default">the default value to use if missing</param>
/// <returns>the value of the environment variable if found and the default value if missing</returns>
public static string GetEnvironmentVariable(string VarName, string Default)
{
string Value = Environment.GetEnvironmentVariable(VarName);
if (Value != null)
{
return Value;
}
return Default;
}
/// <summary>
/// Try to launch a local process, and produce a friendly error message if it fails.
/// </summary>
public static int RunLocalProcess(Process LocalProcess)
{
int ExitCode = -1;
// release all process resources
using (LocalProcess)
{
LocalProcess.StartInfo.UseShellExecute = false;
LocalProcess.StartInfo.RedirectStandardOutput = true;
LocalProcess.StartInfo.RedirectStandardError = true;
try
{
// Start the process up and then wait for it to finish
LocalProcess.Start();
LocalProcess.BeginOutputReadLine();
LocalProcess.BeginErrorReadLine();
LocalProcess.WaitForExit();
ExitCode = LocalProcess.ExitCode;
}
catch (Exception ex)
{
throw new BuildException(ex, "Failed to start local process for action (\"{0}\"): {1} {2}", ex.Message, LocalProcess.StartInfo.FileName, LocalProcess.StartInfo.Arguments);
}
}
return ExitCode;
}
/// <summary>
/// Runs a local process and pipes the output to the log
/// </summary>
public static int RunLocalProcessAndLogOutput(ProcessStartInfo StartInfo)
{
Process LocalProcess = new Process();
LocalProcess.StartInfo = StartInfo;
LocalProcess.OutputDataReceived += (Sender, Line) => { if (Line != null && Line.Data != null) Log.TraceInformation(Line.Data); };
LocalProcess.ErrorDataReceived += (Sender, Line) => { if (Line != null && Line.Data != null) Log.TraceError(Line.Data); };
return RunLocalProcess(LocalProcess);
}
/// <summary>
/// Runs a command line process, and returns simple StdOut output. This doesn't handle errors or return codes
/// </summary>
/// <returns>The entire StdOut generated from the process as a single trimmed string</returns>
/// <param name="Command">Command to run</param>
/// <param name="Args">Arguments to Command</param>
public static string RunLocalProcessAndReturnStdOut(string Command, string Args)
{
ProcessStartInfo StartInfo = new ProcessStartInfo(Command, Args);
StartInfo.UseShellExecute = false;
StartInfo.RedirectStandardOutput = true;
StartInfo.CreateNoWindow = true;
string FullOutput = "";
using (Process LocalProcess = Process.Start(StartInfo))
{
StreamReader OutputReader = LocalProcess.StandardOutput;
// trim off any extraneous new lines, helpful for those one-line outputs
FullOutput = OutputReader.ReadToEnd().Trim();
}
return FullOutput;
}
/// <summary>
/// Find all the platforms in a given class
/// </summary>
/// <param name="Class">Class of platforms to return</param>
/// <returns>Array of platforms in the given class</returns>
public static UnrealTargetPlatform[] GetPlatformsInClass(UnrealPlatformClass Class)
{
switch (Class)
{
case UnrealPlatformClass.All:
return ((UnrealTargetPlatform[])Enum.GetValues(typeof(UnrealTargetPlatform))).Where(x => x != UnrealTargetPlatform.Unknown).ToArray();
case UnrealPlatformClass.Desktop:
return new UnrealTargetPlatform[] { UnrealTargetPlatform.Win32, UnrealTargetPlatform.Win64, UnrealTargetPlatform.Linux, UnrealTargetPlatform.Mac };
case UnrealPlatformClass.Editor:
return new UnrealTargetPlatform[] { UnrealTargetPlatform.Win64, UnrealTargetPlatform.Linux, UnrealTargetPlatform.Mac };
case UnrealPlatformClass.Server:
return new UnrealTargetPlatform[] { UnrealTargetPlatform.Win32, UnrealTargetPlatform.Win64, UnrealTargetPlatform.Linux, UnrealTargetPlatform.Mac };
}
throw new ArgumentException(String.Format("'{0}' is not a valid value for UnrealPlatformClass", (int)Class));
}
/// <summary>
/// Given a list of supported platforms, returns a list of names of platforms that should not be supported
/// </summary>
/// <param name="SupportedPlatforms">List of supported platforms</param>
/// <returns>List of unsupported platforms in string format</returns>
public static List<string> MakeListOfUnsupportedPlatforms(List<UnrealTargetPlatform> SupportedPlatforms)
{
// Make a list of all platform name strings that we're *not* currently compiling, to speed
// up file path comparisons later on
List<string> OtherPlatformNameStrings = new List<string>();
{
// look at each group to see if any supported platforms are in it
List<UnrealPlatformGroup> SupportedGroups = new List<UnrealPlatformGroup>();
foreach (UnrealPlatformGroup Group in Enum.GetValues(typeof(UnrealPlatformGroup)))
{
// get the list of platforms registered to this group, if any
List<UnrealTargetPlatform> Platforms = UEBuildPlatform.GetPlatformsInGroup(Group);
if (Platforms != null)
{
// loop over each one
foreach (UnrealTargetPlatform Platform in Platforms)
{
// if it's a compiled platform, then add this group to be supported
if (SupportedPlatforms.Contains(Platform))
{
SupportedGroups.Add(Group);
}
}
}
}
// loop over groups one more time, anything NOT in SupportedGroups is now unsuppored, and should be added to the output list
foreach (UnrealPlatformGroup Group in Enum.GetValues(typeof(UnrealPlatformGroup)))
{
if (SupportedGroups.Contains(Group) == false)
{
OtherPlatformNameStrings.Add(Group.ToString());
}
}
foreach (UnrealTargetPlatform CurPlatform in Enum.GetValues(typeof(UnrealTargetPlatform)))
{
if (CurPlatform != UnrealTargetPlatform.Unknown)
{
bool ShouldConsider = true;
// If we have a platform and a group with the same name, don't add the platform
// to the other list if the same-named group is supported. This is a lot of
// lines because we need to do the comparisons as strings.
string CurPlatformString = CurPlatform.ToString();
foreach (UnrealPlatformGroup Group in Enum.GetValues(typeof(UnrealPlatformGroup)))
{
if (Group.ToString().Equals(CurPlatformString))
{
ShouldConsider = false;
break;
}
}
// Don't add our current platform to the list of platform sub-directory names that
// we'll skip source files for
if (ShouldConsider && !SupportedPlatforms.Contains(CurPlatform))
{
OtherPlatformNameStrings.Add(CurPlatform.ToString());
}
}
}
return OtherPlatformNameStrings;
}
}
/// <summary>
/// Takes a path string and makes all of the path separator characters consistent. Also removes unnecessary multiple separators.
/// </summary>
/// <param name="FilePath">File path with potentially inconsistent slashes</param>
/// <param name="UseDirectorySeparatorChar">The directory separator to use</param>
/// <returns>File path with consistent separators</returns>
public static string CleanDirectorySeparators(string FilePath, char UseDirectorySeparatorChar = '\0')
{
StringBuilder CleanPath = null;
if (UseDirectorySeparatorChar == '\0')
{
UseDirectorySeparatorChar = Environment.OSVersion.Platform == PlatformID.Unix ? '/' : '\\';
}
char PrevC = '\0';
// Don't check for double separators until we run across a valid dir name. Paths that start with '//' or '\\' can still be valid.
bool bCanCheckDoubleSeparators = false;
for (int Index = 0; Index < FilePath.Length; ++Index)
{
char C = FilePath[Index];
if (C == '/' || C == '\\')
{
if (C != UseDirectorySeparatorChar)
{
C = UseDirectorySeparatorChar;
if (CleanPath == null)
{
CleanPath = new StringBuilder(FilePath.Substring(0, Index), FilePath.Length);
}
}
if (bCanCheckDoubleSeparators && C == PrevC)
{
if (CleanPath == null)
{
CleanPath = new StringBuilder(FilePath.Substring(0, Index), FilePath.Length);
}
continue;
}
}
else
{
// First non-separator character, safe to check double separators
bCanCheckDoubleSeparators = true;
}
if (CleanPath != null)
{
CleanPath.Append(C);
}
PrevC = C;
}
return CleanPath != null ? CleanPath.ToString() : FilePath;
}
/// <summary>
/// Given a file path and a directory, returns a file path that is relative to the specified directory
/// </summary>
/// <param name="SourcePath">File path to convert</param>
/// <param name="RelativeToDirectory">The directory that the source file path should be converted to be relative to. If this path is not rooted, it will be assumed to be relative to the current working directory.</param>
/// <param name="AlwaysTreatSourceAsDirectory">True if we should treat the source path like a directory even if it doesn't end with a path separator</param>
/// <returns>Converted relative path</returns>
public static string MakePathRelativeTo(string SourcePath, string RelativeToDirectory, bool AlwaysTreatSourceAsDirectory = false)
{
if (String.IsNullOrEmpty(RelativeToDirectory))
{
// Assume CWD
RelativeToDirectory = ".";
}
string AbsolutePath = SourcePath;
if (!Path.IsPathRooted(AbsolutePath))
{
AbsolutePath = Path.GetFullPath(SourcePath);
}
bool SourcePathEndsWithDirectorySeparator = AbsolutePath.EndsWith(Path.DirectorySeparatorChar.ToString()) || AbsolutePath.EndsWith(Path.AltDirectorySeparatorChar.ToString());
if (AlwaysTreatSourceAsDirectory && !SourcePathEndsWithDirectorySeparator)
{
AbsolutePath += Path.DirectorySeparatorChar;
}
Uri AbsolutePathUri = new Uri(AbsolutePath);
string AbsoluteRelativeDirectory = RelativeToDirectory;
if (!Path.IsPathRooted(AbsoluteRelativeDirectory))
{
AbsoluteRelativeDirectory = Path.GetFullPath(AbsoluteRelativeDirectory);
}
// Make sure the directory has a trailing directory separator so that the relative directory that
// MakeRelativeUri creates doesn't include our directory -- only the directories beneath it!
if (!AbsoluteRelativeDirectory.EndsWith(Path.DirectorySeparatorChar.ToString()) && !AbsoluteRelativeDirectory.EndsWith(Path.AltDirectorySeparatorChar.ToString()))
{
AbsoluteRelativeDirectory += Path.DirectorySeparatorChar;
}
// Convert to URI form which is where we can make the relative conversion happen
Uri AbsoluteRelativeDirectoryUri = new Uri(AbsoluteRelativeDirectory);
// Ask the URI system to convert to a nicely formed relative path, then convert it back to a regular path string
Uri UriRelativePath = AbsoluteRelativeDirectoryUri.MakeRelativeUri(AbsolutePathUri);
string RelativePath = Uri.UnescapeDataString(UriRelativePath.ToString()).Replace('/', Path.DirectorySeparatorChar);
// If we added a directory separator character earlier on, remove it now
if (!SourcePathEndsWithDirectorySeparator && AlwaysTreatSourceAsDirectory && RelativePath.EndsWith(Path.DirectorySeparatorChar.ToString()))
{
RelativePath = RelativePath.Substring(0, RelativePath.Length - 1);
}
// Uri.MakeRelativeUri is broken in Mono 2.x and sometimes returns broken path
if (IsRunningOnMono)
{
// Check if result is correct
string TestPath = Path.GetFullPath(Path.Combine(AbsoluteRelativeDirectory, RelativePath));
if (TestPath != AbsolutePath)
{
TestPath += "/";
if (TestPath != AbsolutePath)
{
// Fix the path. @todo Mac: replace this hack with something better
RelativePath = "../" + RelativePath;
}
}
}
return RelativePath;
}
/// <summary>
/// Backspaces the specified number of characters, then displays a progress percentage value to the console
/// </summary>
/// <param name="Numerator">Progress numerator</param>
/// <param name="Denominator">Progress denominator</param>
/// <param name="NumCharsToBackspaceOver">Number of characters to backspace before writing the text. This value will be updated with the length of the new progress string. The first time progress is displayed, you should pass 0 for this value.</param>
public static void DisplayProgress(int Numerator, int Denominator, ref int NumCharsToBackspaceOver)
{
// Backspace over previous progress value
while (NumCharsToBackspaceOver-- > 0)
{
Console.Write("\b");
}
// Display updated progress string and keep track of how long it was
float ProgressValue = Denominator > 0 ? ((float)Numerator / (float)Denominator) : 1.0f;
string ProgressString = String.Format("{0}%", Math.Round(ProgressValue * 100.0f));
NumCharsToBackspaceOver = ProgressString.Length;
Console.Write(ProgressString);
}
/*
* Read and write classes with xml specifiers
*/
static private void UnknownAttributeDelegate(object sender, XmlAttributeEventArgs e)
{
}
static private void UnknownNodeDelegate(object sender, XmlNodeEventArgs e)
{
}
/// <summary>
/// Reads a class using XML serialization
/// </summary>
/// <typeparam name="T">The type to read</typeparam>
/// <param name="FileName">The XML file to read from</param>
/// <returns>New deserialized instance of type T</returns>
static public T ReadClass<T>(string FileName) where T : new()
{
T Instance = new T();
StreamReader XmlStream = null;
try
{
// Get the XML data stream to read from
XmlStream = new StreamReader(FileName);
// Creates an instance of the XmlSerializer class so we can read the settings object
XmlSerializer Serialiser = new XmlSerializer(typeof(T));
// Add our callbacks for unknown nodes and attributes
Serialiser.UnknownNode += new XmlNodeEventHandler(UnknownNodeDelegate);
Serialiser.UnknownAttribute += new XmlAttributeEventHandler(UnknownAttributeDelegate);
// Create an object graph from the XML data
Instance = (T)Serialiser.Deserialize(XmlStream);
}
catch (Exception E)
{
Log.TraceInformation(E.Message);
}
finally
{
if (XmlStream != null)
{
// Done with the file so close it
XmlStream.Close();
}
}
return Instance;
}
/// <summary>
/// Serialize an object to an XML file
/// </summary>
/// <typeparam name="T">Type of the object to serialize</typeparam>
/// <param name="Data">Object to write</param>
/// <param name="FileName">File to write to</param>
/// <param name="DefaultNameSpace">Default namespace for the output elements</param>
/// <returns>True if the file was written successfully</returns>
static public bool WriteClass<T>(T Data, string FileName, string DefaultNameSpace)
{
bool bSuccess = true;
StreamWriter XmlStream = null;
try
{
FileInfo Info = new FileInfo(FileName);
if (Info.Exists)
{
Info.IsReadOnly = false;
}
// Make sure the output directory exists
Directory.CreateDirectory(Path.GetDirectoryName(FileName));
XmlSerializerNamespaces EmptyNameSpace = new XmlSerializerNamespaces();
EmptyNameSpace.Add("", DefaultNameSpace);
XmlStream = new StreamWriter(FileName, false, Encoding.Unicode);
XmlSerializer Serialiser = new XmlSerializer(typeof(T));
// Add our callbacks for unknown nodes and attributes
Serialiser.UnknownNode += new XmlNodeEventHandler(UnknownNodeDelegate);
Serialiser.UnknownAttribute += new XmlAttributeEventHandler(UnknownAttributeDelegate);
Serialiser.Serialize(XmlStream, Data, EmptyNameSpace);
}
catch (Exception E)
{
Log.TraceInformation(E.Message);
bSuccess = false;
}
finally
{
if (XmlStream != null)
{
// Done with the file so close it
XmlStream.Close();
}
}
return (bSuccess);
}
/// <summary>
/// Returns true if the specified Process has been created, started and remains valid (i.e. running).
/// </summary>
/// <param name="p">Process object to test</param>
/// <returns>True if valid, false otherwise.</returns>
public static bool IsValidProcess(Process p)
{
// null objects are always invalid
if (p == null)
return false;
// due to multithreading on Windows, lock the object
lock (p)
{
// Mono has a specific requirement if testing for an alive process
if (IsRunningOnMono)
return p.Handle != IntPtr.Zero; // native handle to the process
// on Windows, simply test the process ID to be non-zero.
// note that this can fail and have a race condition in threads, but the framework throws an exception when this occurs.
try
{
return p.Id != 0;
}
catch { } // all exceptions can be safely caught and ignored, meaning the process is not started or has stopped.
}
return false;
}
/// <summary>
/// Removes multi-dot extensions from a filename (i.e. *.automation.csproj)
/// </summary>
/// <param name="Filename">Filename to remove the extensions from</param>
/// <returns>Clean filename.</returns>
public static string GetFilenameWithoutAnyExtensions(string Filename)
{
Filename = Path.GetFileName(Filename);
int DotIndex = Filename.IndexOf('.');
if (DotIndex == -1)
{
return Filename; // No need to copy string
}
else
{
return Filename.Substring(0, DotIndex);
}
}
/// <summary>
/// Returns Filename with path but without extension.
/// </summary>
/// <param name="Filename">Filename</param>
/// <returns>Path to the file with its extension removed.</returns>
public static string GetPathWithoutExtension(string Filename)
{
if (!String.IsNullOrEmpty(Path.GetExtension(Filename)))
{
return Path.Combine(Path.GetDirectoryName(Filename), Path.GetFileNameWithoutExtension(Filename));
}
else
{
return Filename;
}
}
/// <summary>
/// Returns true if the specified file's path is located under the specified directory, or any of that directory's sub-folders. Does not care whether the file or directory exist or not. This is a simple string-based check.
/// </summary>
/// <param name="FilePath">The path to the file</param>
/// <param name="Directory">The directory to check to see if the file is located under (or any of this directory's subfolders)</param>
/// <returns></returns>
public static bool IsFileUnderDirectory(string FilePath, string Directory)
{
string DirectoryPathPlusSeparator = Path.GetFullPath(Directory);
if (!DirectoryPathPlusSeparator.EndsWith(Path.DirectorySeparatorChar.ToString()))
{
DirectoryPathPlusSeparator += Path.DirectorySeparatorChar;
}
return Path.GetFullPath(FilePath).StartsWith(DirectoryPathPlusSeparator, StringComparison.InvariantCultureIgnoreCase);
}
/// <summary>
/// Checks if given type implements given interface.
/// </summary>
/// <typeparam name="InterfaceType">Interface to check.</typeparam>
/// <param name="TestType">Type to check.</param>
/// <returns>True if TestType implements InterfaceType. False otherwise.</returns>
public static bool ImplementsInterface<InterfaceType>(Type TestType)
{
return Array.IndexOf(TestType.GetInterfaces(), typeof(InterfaceType)) != -1;
}
/// <summary>
/// Returns the User Settings Directory path. This matches FPlatformProcess::UserSettingsDir().
/// NOTE: This function may return null. Some accounts (eg. the SYSTEM account on Windows) do not have a personal folder, and Jenkins
/// runs using this account by default.
/// </summary>
public static DirectoryReference GetUserSettingDirectory()
{
if (BuildHostPlatform.Current.Platform == UnrealTargetPlatform.Mac)
{
return new DirectoryReference(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Library", "Application Support", "Epic"));
}
else if (Environment.OSVersion.Platform == PlatformID.Unix)
{
return new DirectoryReference(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Epic"));
}
else
{
// Not all user accounts have a local application data directory (eg. SYSTEM, used by Jenkins for builds).
string DirectoryName = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
if(String.IsNullOrEmpty(DirectoryName))
{
return null;
}
else
{
return new DirectoryReference(DirectoryName);
}
}
}
/// <summary>
/// Gets the number of physical cores, excluding hyper threading.
/// </summary>
/// <returns>The number of physical cores, or -1 if it could not be obtained</returns>
public static int GetPhysicalProcessorCount()
{
// Can't use WMI queries on Mono; just fail.
if (Utils.IsRunningOnMono)
{
return -1;
}
// On some systems this requires a hot fix to work so catch any exceptions
try
{
int NumCores = 0;
using (ManagementObjectSearcher Mos = new System.Management.ManagementObjectSearcher("Select * from Win32_Processor"))
{
ManagementObjectCollection MosCollection = Mos.Get();
foreach (ManagementBaseObject Item in MosCollection)
{
NumCores += int.Parse(Item["NumberOfCores"].ToString());
}
}
return NumCores;
}
catch (Exception Ex)
{
Log.TraceWarning("Unable to get the number of Cores: {0}", Ex.ToString());
Log.TraceWarning("Falling back to processor count.");
return -1;
}
}
}
}