Files
UnrealEngineUWP/Engine/Source/Programs/AutomationTool/BuildGraph/Script.cs
Ben Marsh fedc653232 Copying //UE4/Dev-Core to //UE4/Dev-Main (Source: //UE4/Dev-Core @ 3620134)
#lockdown Nick.Penwarden
#rb none

============================
  MAJOR FEATURES & CHANGES
============================

Change 3550452 by Ben.Marsh

	UAT: Improve readability of error message when an editor commandlet fails with an error code.

Change 3551179 by Ben.Marsh

	Add methods for reading text files into an array of strings.

Change 3551260 by Ben.Marsh

	Core: Change FFileHelper routines to use enum classes for flags.

Change 3555697 by Gil.Gribb

	Fixed a rare crash when the asset registry scanner found old cooked files with package level compression.

	#jira UE-47668

Change 3556464 by Ben.Marsh

	UGS: If working in a virtual stream, use the name of the first non-virtual ancestor for writing version files.

Change 3557630 by Ben.Marsh

	Allow the network version to be set via Build.version if it's not overriden from Version.h.

Change 3561357 by Gil.Gribb

	Fixed crashes related to loading old unversioned files in the editor.

	#jira UE-47806

Change 3565711 by Graeme.Thornton

	PR #3839: Make non-encoding specific Base64 functions accessible (Contributed by stfx)


Change 3565864 by Robert.Manuszewski

	Temp fix for a race condition with the async loading thread enabled - caching the linker in case it gets removed (but not deleted) from super class object.

Change 3569022 by Ben.Marsh

	PR #3849: Update gitignore (Contributed by mhutch)


Change 3569113 by Ben.Marsh

	Fix Japanese errors not displaying correctly in the cook output log.

	#jira UE-47746

Change 3569486 by Ben.Marsh

	UGS: Always sync the Enterprise folder if the selected .uproject file has the "Enterprise" flag set.

Change 3570483 by Graeme.Thornton

	Minor C# cleanups. Removing some redundant "using" calls which also cause dotnetcore compile errors

Change 3570513 by Robert.Manuszewski

	Fix for a race condition with async loading thread enabled.

Change 3570664 by Ben.Marsh

	UBT: Use P/Invoke to determine number of physical processors on Windows rather than using WMI. Starting up WMIC adds 2.5 seconds to build times, and is not compatible with .NET core.

Change 3570708 by Robert.Manuszewski

	Added ENABLE_GC_OBJECT_CHECKS macro to be able to quickly toggle UObject pointer checks in shipping builds when the garbage collector is running.

Change 3571592 by Ben.Marsh

	UBT: Allow running with -installed without creating [InstalledPlatforms] entries in BaseEngine.ini. If there is no HasInstalledPlatformInfo=true setting, assume that all platforms are still available.

Change 3572215 by Graeme.Thornton

	UBT
	- Remove some unnecessary using directives
	- Point SN-DBS code at the new Utils.GetPhysicalProcessorCount call, rather than trying to calculate it itself

Change 3572437 by Robert.Manuszewski

	Game-specific fix for lazy object pointer issues in one of the test levels. The previous fix had to be partially reverted due to side-effects.

	#jira UE-44996

Change 3572480 by Robert.Manuszewski

	MaterialInstanceCollections will no longer be added to GC clusters to prevent materials staying around in memory for too long

Change 3573547 by Ben.Marsh

	Add support for displaying log timestamps in local time. Set LogTimes=Local in *Engine.ini, or pass -LocalLogTimes on the command line.

Change 3574562 by Robert.Manuszewski

	PR #3847: Add GC callbacks for script integrations (Contributed by mhutch)


Change 3575017 by Ben.Marsh

	Move some functions related to generating window resolutions out of Core (FParse::Resolution, GenerateConvenientWindowedResolutions). Also remove a few headers from shared PCHs prior to splitting application functionality out of Core.

Change 3575689 by Ben.Marsh

	Add a fixed URL for opening the API documentation, so it works correctly in "internal" and "perforce" builds.

Change 3575934 by Steve.Robb

	Fix for nested preprocessor definitions.

Change 3575961 by Steve.Robb

	Fix for nested zeros.

Change 3576297 by Robert.Manuszewski

	Material resources will now be discarded in PostLoad (Game Thread) instead of in Serialize (potentially Async Loading Thread) so that shader deregistration doesn't assert when done from a different thread than the game thread.

	#jira FORT-38977

Change 3576366 by Ben.Marsh

	Add shim functions to allow redirecting FPlatformMisc::ClipboardCopy()/ClipboardPaste() to FPlatformApplicationMisc::ClipboardCopy()/ClipboardPaste() while they are deprecated.

Change 3578290 by Graeme.Thornton

	Changes to Ionic zip library to allow building on dot net core

Change 3578291 by Graeme.Thornton

	Ionic zip library binaries built for .NET Core

Change 3578354 by Graeme.Thornton

	Added FBase64::GetDecodedDataSize() to determine the size of bytes of a decoded base64 string

Change 3578674 by Robert.Manuszewski

	After loading packages flush linker cache on uncooked platforms to free precache memory

Change 3579068 by Steve.Robb

	Fix for CLASS_Intrinsic getting stomped.
	Fix to EClassFlags so that they are visible in the debugger.
	Re-added mysteriously-removed comments.

Change 3579228 by Steve.Robb

	BOM removed.

Change 3579297 by Ben.Marsh

	Fix exception if a plugin lists the same module twice.

	#jira UE-48232

Change 3579898 by Robert.Manuszewski

	When creating GC clusters and asserting due to objects still being pending load, the object name and cluster name will now be logged with the assert.

Change 3579983 by Robert.Manuszewski

	More fixes for freeing linker cache memory in the editor.

Change 3580012 by Graeme.Thornton

	Remove redundant copy of FileReference.cs

Change 3580408 by Ben.Marsh

	Validate that arguments passed to the checkf macro are valid sprintf types, and fix up a few places which are currently incorrect.

Change 3582104 by Graeme.Thornton

	Added a dynamic compilation path that uses the latest roslyn apis. Currently only used by the .NET Core path.

Change 3582131 by Graeme.Thornton

	#define out some PerformanceCounter calls that don't exist in .NET Core. They're only used by mono-specific calls anyway.

Change 3582645 by Ben.Marsh

	PR #3879: fix bug when creating a new VS2017 C++ project (Contributed by mnannola)

	#jira UE-48192

Change 3583955 by Robert.Manuszewski

	Support for EDL cooked packages in the editor

Change 3584035 by Graeme.Thornton

	Split RunExternalExecutable into RunExternaNativelExecutable and RunExternalDotNETExecutable. When running under .NET Core, externally launched DotNET utilities must be launched via the 'dotnet' proxy to work correctly.

Change 3584177 by Robert.Manuszewski

	Removed unused member variable (FArchiveAsync2::bKeepRestOfFilePrecached)

Change 3584315 by Ben.Marsh

	Move Android JNI accessor functions into separate header, to decouple it from the FAndroidApplication class.

Change 3584370 by Ben.Marsh

	Move hooks which allow platforms to load any modules into the FPlatformApplicationMisc classes.

Change 3584498 by Ben.Marsh

	Move functions for getting and setting the hardware window pointer onto the appropriate platform window classes.

Change 3585003 by Steve.Robb

	Fix for TChunkedArray ranged-for iteration.

	#jira UE-48297

Change 3585235 by Ben.Marsh

	Remove LogEngine extern from Core; use the platform log channels instead.

Change 3585942 by Ben.Marsh

	Move MessageBoxExt() implementation into application layer for platforms that require it.

Change 3587071 by Ben.Marsh

	Move Linux's UngrabAllInput() function into a callback, so DebugBreak still works without SDL.

Change 3587161 by Ben.Marsh

	Remove headers which will be stripped out of the Core module from Core.h and PlatformIncludes.h.

Change 3587579 by Steve.Robb

	Fix for Children list not being rebuilt after hot reload.

Change 3587584 by Graeme.Thornton

	Logging improvements for pak signature check failures
	 - Added "PakCorrupt" console command which corrupts the master signature table
	 - Added some extra log information about which block failed
	 - Re-hash the master signature table and to make sure that it hasn't changed since startup
	 - Moved the ensure around so that some extra logging messages can make it out before the ensure is hit
	 - Added PAK_SIGNATURE_CHECK_FAILS_ARE_FATAL to IPlatformFilePak.h so we have a single place to make signature check failures fatal again

Change 3587586 by Graeme.Thornton

	Changes to make UBT build and run on .NET Core
	 - Added *_DNC csproj files for DotNETUtilities and UnrealBuildTool projects which contain the .NET Core build setups
	 - VCSharpProjectFile can no be asked for the CsProjectInfo for a particular configuration, which is cached for future use
	 - After loading VCSharpProjectFiles, .NET Core based projects will be excluded unless generating VSCode projects

Change 3587953 by Steve.Robb

	Allow arbitrary UENUM initializers for enumerators.
	Editor-only data UENUM support.
	Enumerators named MAX are now treated as the UENUM's maximum, and will not cause a MAX+1 value to be generated.

	#jira UE-46274

Change 3589827 by Graeme.Thornton

	More fixes for VSCode project generation and for UBT running on .NET Core
	 - Use a different file extension for rules assemblies when build on .NET Core, so they never get used by their counterparts
	 - UEConsoleTraceListener supports stdout/stderror constructor parameter and outputs to the appropriate channel
	 - Added documentation for UEConsoleTraceListener
	 - All platforms .NET project compilation tasks/launch configs now use "dotnet" and not the normal batch files
	 - Restored the default UBT log verbosity to "Log" rather than "VeryVeryVerbose"
	 - Renamed assemblies for .NETCore versions of DotNETUtilities and UnrealBuildTool so they don't conflict with the output of the existing .NET Desktop Framework stuff

Change 3589868 by Graeme.Thornton

	Separate .NET Core projects for UBT and DotNETCommon out into their own directories so that their intermediates don't overlap with the standard .NET builds, causing failures.

	UBT registers ONLY .NET Core C# projects when generating VSCode solutions, and ONLY standard C# projects in all other cases

Change 3589919 by Robert.Manuszewski

	Fixing crash when cooking textures that have already been cooked for EDL (support for cooked content in the editor)

Change 3589940 by Graeme.Thornton

	Force UBT to think it's running on mono when actually running on .NET Core. Disables a lot of windows specific code paths.

Change 3590078 by Graeme.Thornton

	Fully disable automatic assembly info generation in .NET Core projects

Change 3590534 by Robert.Manuszewski

	Marking UObject as intrinsic clas to fix a crash on UFE startup.

Change 3591498 by Gil.Gribb

	UE4 - Fixed several edge cases in the low level async loading code, especially around cancellation. Also PakFileTest is a console command which can be used to stress test pak file loading.

Change 3591605 by Gil.Gribb

	UE4 - Follow up to fixing several edge cases in the low level async loading code.

Change 3592577 by Graeme.Thornton

	.NET Core C# projects now reference source files explicitly, to stop it accidentally compiling various intermediates

Change 3592684 by Steve.Robb

	Fix for EObjectFlags being passed as the wrong argument to csgCopyBrush.

Change 3592710 by Steve.Robb

	Fix for invalid casts in ListProps command.
	Some name changes in command output.

Change 3592715 by Ben.Marsh

	Move Windows event log code into cpp file, and expose it to other modules even if it's not enabled by default.

Change 3592767 by Gil.Gribb

	UE4 - Changed the logic so that engine UObjects boot before anything else. The engine classes are known to be cycle-free, so we will get them done before moving onto game modules.

Change 3592770 by Gil.Gribb

	UE4 - Fixed a race condition with async read completion in the prescence of cancels.

Change 3593090 by Steve.Robb

	Better error message when there two clashing type names are found.

Change 3593697 by Steve.Robb

	VisitTupleElements function, which calls a functor for each element in the tuple.

Change 3595206 by Ben.Marsh

	Include additional diagnostics for missing imports when a module load fails.

Change 3596140 by Graeme.Thornton

	Batch file for running MSBuild

Change 3596267 by Steve.Robb

	Thread safety fix to FPaths::GetProjectFilePath().

Change 3596271 by Robert.Manuszewski

	Added code to verify compression flags in package file summary to avoid cases where corrupt packages are crashing the editor

	#jira UE-47535

Change 3596283 by Steve.Robb

	Redundant casts removed from UHT.

Change 3596303 by Ben.Marsh

	EC: Improve parsing of Android Clang errors and warnings, which are formatted as MSVC diagnostics to allow go-to-line clicking in the Output Window.

Change 3596337 by Ben.Marsh

	UBT: Format messages about incorrect headers in a way that makes them clickable from Visual Studio.

Change 3596367 by Steve.Robb

	Iterator checks in ranged-for on TMap, TSet and TSparseArray.

Change 3596410 by Gil.Gribb

	UE4 - Improved some error messages on runtime failures in the EDL.

Change 3596532 by Ben.Marsh

	UnrealVS: Fix setting command line to empty not affecting property sheet. Also remove support for VS2013.

	#jira UE-48119

Change 3596631 by Steve.Robb

	Tool which takes a .map file and a .objmap file (from UBT) and creates a report which shows the size of all the symbols contributed by the source code per-folder.

Change 3596807 by Ben.Marsh

	Improve Intellisense when generated headers are missing or out of date (eg. line numbers changed, etc...). These errors seem to be masked by VAX, but are present when using the default Visual Studio Intellisense.

	* UCLASS macro is defined to empty when __INTELLISENSE__ is defined. Previous macro was preventing any following class declaration being parsed correctly if generated code was out of date, causing squiggles over all class methods/variables.
	* Insert a semicolon after each expanded GENERATED_BODY macro, so that if it parses incorrectly, the compiler can still continue parsing the next declaration.

Change 3596957 by Steve.Robb

	UBT can be used to write out an .objsrcmap file for use with the MapFileParser.
	Renaming of ObjMap to ObjSrcMap in MapFileParser.

Change 3597213 by Ben.Marsh

	Remove AutoReporter. We don't support this any more.

Change 3597558 by Ben.Marsh

	UGS: Allow adding custom actions to the context menu for right clicking on a changelist. Actions are specified in the project's UnrealEngine.ini file, with the following syntax:

	+ContextMenu=(Label="This is the menu item", Execute="foo.exe", Arguments="bar")

	The standard set of variables for custom tools is expanded in each parameter (eg. $(ProjectDir), $(EditorConfig), etc...), plus the $(Change) variable.

Change 3597982 by Ben.Marsh

	Add an option to allow overriding the local DDC path from the editor (under Editor Preferences > Global > Local Derived Data Cache).

	#jira UE-47173

Change 3598045 by Ben.Marsh

	UGS: Add variables for stream and client name, and the ability to escape any variables for URIs using the syntax $(VariableName:URI).

Change 3599214 by Ben.Marsh

	Avoid string duplication when comparing extensions.

Change 3600038 by Steve.Robb

	Fix for maps being modified during iteration in cache compaction.

Change 3600136 by Steve.Robb

	GitHub #3538 : Fixed a bug with the handling of 'TMap' key/value types in the UnrealHeaderTool

Change 3600214 by Steve.Robb

	More accurate error message when unsupported template parameters are provided in a TSet property.

Change 3600232 by Ben.Marsh

	UBT: Force UHT to run again if the .build.cs file for a module has changed.

	#jira UE-46119

Change 3600246 by Steve.Robb

	GitHub #3045 : allow multiple interface definition in a file

Change 3600645 by Ben.Marsh

	Convert QAGame to Include-What-You-Use.

Change 3600897 by Ben.Marsh

	Fix invalid path (multiple slashes) in LibCurl.build.cs. Causes exception when scanning for includes.

Change 3601558 by Graeme.Thornton

	Simple first pass VSCode editor integration plugin

Change 3601658 by Graeme.Thornton

	Enable intellisense generation for VS Code project files and setup include paths properly

Change 3601762 by Ben.Marsh

	UBT: Add support for adaptive non-unity builds when working from a Git repository.

	The ISourceFileWorkingSet interface is now used to query files belonging to the working set, and has separate implementations for Perforce (PerforceSourceFileWorkingSet) and Git (GitSourceFileWorkingSet). The Git implementation is used if a .git directory is found in the directory containing the Engine folder, the directory containing the project file, or the parent directory of the project file, and spawns a "git status" process in the background to determine which files are untracked or staged.

	Several new settings are supported in BuildConfiguration.xml to allow modifying default behavior:

	<SourceFileWorkingSet>
	    <Provider>Default</Provider> <!-- May be None, Default, Git or Perforce -->
	    <RepositoryPath></RepositoryPath> <!-- Specifies the path to the repository, relative to the directory containing the Engine folder. If not set, tries to find a .git directory in the locations listed above. -->
	    <GitPath>git</GitPath> <!-- Specifies the path to the Git executable. Defaults to "git", which assumes that it will be on the PATH -->
	</SourceFileWorkingSet>

Change 3604032 by Graeme.Thornton

	First attempt at automatically detecting the existance and location of visual studio code in the source code accessor module. Only works for windows.

Change 3604038 by Graeme.Thornton

	Added FSourceCodeNavigation::GetSelectedSourceCodeIDE() which returns the name of the selected source code accessor.
	Replaced all usages of FSourceCodeNavigation::GetSuggestedSourceCodeIDE() with GetSelectedSourceCodeIDE(), where the message is referring to the opening or editing of code.

Change 3604106 by Steve.Robb

	GitHub #3561 : UE-44950: Don't see all caps struct constructor as macro

Change 3604192 by Steve.Robb

	GitHub #3911 : Improving ToUpper/ToLower efficiency

Change 3604273 by Graeme.Thornton

	IWYU build fixes when malloc profiler is enabled

Change 3605457 by Ben.Marsh

	Fix race for intiialization of ThreadID variable on FRunnableThreadWin, and restore a previous check that was working around it.

Change 3606720 by James.Hopkin

	Dave Ratti's fix to character base recursion protection code - was missing a GetOwner call, instead attempting to cast a component to a pawn.

Change 3606807 by Graeme.Thornton

	Disabled optimizations around FShooterStyle::Create(), which was crashing in Win64 shipping game builds due to some known compiler issue. Same variety of fix as BenZ did in CL 3567741.

Change 3607026 by James.Hopkin

	Fixed incorrect ABrush cast - was attempting to cast a UModel to ABrush, which can never succeed

Change 3607142 by Graeme.Thornton

	UBT - Minor refactor of BackgroundProcess shutdown in SourceFileWorkingSet. Check whether the process has already exited before trying to kill it during Dispose.

Change 3607146 by Ben.Marsh

	UGS: Fix exception due to formatting string when Perforce throws an error.

Change 3607147 by Steve.Robb

	Efficiency fix for integer properties, which were causing a property mismatch and thus a tag lookup every time.
	Float and double conversion support added to int properties.
	NAME_DoubleProperty added.
	Fix for converting enum class enumerators > 255 to int properties.

Change 3607516 by Ben.Marsh

	PR #3935: Fix DECLARE_DELEGATE_NineParams, DECLARE_MULTICAST_DELEGATE_NineParams. (Contributed by enginevividgames)


Change 3610421 by Ben.Marsh

	UAT: Move help for RebuildLightMapsCommand into attributes, so they display when running with -help.

Change 3610657 by Ben.Marsh

	UAT: Unify initialization of command environment for build machines and local execution. Always derive parameters which aren't manually set via environment variables.

Change 3611000 by Ben.Marsh

	UAT: Remove the -ForceLocal command line option. Settings are now determined automatically, independently of the -Buildmachine argument.

Change 3612471 by Ben.Marsh

	UBT: Move FastJSON into DotNETUtilities.

Change 3613479 by Ben.Marsh

	UBT: Remove the bIsCodeProject flag from UProjectInfo. This was only really being used to determine which projects to generate an IDE project for, so it is now checked in the project file generator.

Change 3613910 by Ben.Marsh

	UBT: Remove unnecessary code to guess a project from the target name; doesn't work due to init order, actual project is determined later.

Change 3614075 by Ben.Marsh

	UBT: Remove hacks for testing project file attributes by name.

Change 3614090 by Ben.Marsh

	UBT: Remove global lookup of project by name. Projects should be explicitly specified by path when necessary.

Change 3614488 by Ben.Marsh

	UBT: Prevent annoying (but handled) exception when constructing SQLiteModuleSupport objects with -precompile enabled.

Change 3614490 by Ben.Marsh

	UBT: Simplify generation of arguments for building intellisense; determine the platform/configuration to build from the project file generation code, rather than inside the target itself.

Change 3614962 by Ben.Marsh

	UBT: Move the VS2017 strict conformance mode (/permissive-) behind a command line option (-Strict), and disable it by default. Building with this mode is not guaranteed to work correctly without updated Windows headers.

Change 3615416 by Ben.Marsh

	EC: Include an icon showing the overall status of a build in the grid view.

Change 3615713 by Ben.Marsh

	UBT: Delete any files in output directories which match output files in other directories. Allows automatically deleting build products which are moved into another folder.

	#jira UE-48987

Change 3616652 by Ben.Marsh

	Plugins: Fix incorrect dialog when binaries for a plugin are missing. Should only prompt to disable if starting a content-only project.

	#jira UE-49007

Change 3616680 by Ben.Marsh

	Add the CodeAPI-HTML.tgz file into the installed engine build.

Change 3616767 by Ben.Marsh

	Plugins: Tweak error message if the FModuleManager::IsUpToDate() function returns false for a plugin module; the module may be missing, not just incompatible.

Change 3616864 by Ben.Marsh

	Cap the length of the temporary package name during save, to prevent excessively long filenames going over the limit once a GUID is appended.

	#jira UE-48711

Change 3619964 by Ben.Marsh

	UnrealVS: Fix single file compile for foreign projects, where the command line contains $(SolutionDir) and $(ProjectName) variables.

Change 3548930 by Ben.Marsh

	UBT: Remove UEBuildModuleCSDLL; there is no codepath that still supports creating them. Remove the remaining UEBuildModule/UEBuildModuleCPP abstraction.

Change 3558056 by Ben.Marsh

	Deprecate FString::Trim() and FString::TrimTrailing(), and replace them with separate versions to mutate (TrimStartInline(), TrimEndInline()) or return by copy (TrimStart(), TrimEnd()). Also add a functions to trim whitespace from both ends of a string (TrimStartAndEnd(), TrimStartAndEndInline()).

Change 3563309 by Graeme.Thornton

	Moved some common C# classes into the DotNETCommon assembly

Change 3570283 by Graeme.Thornton

	Move some code out of RPCUtility and into DotNETCommon, removing the dependency between the two projects
	Added UEConsoleTraceListener to replace ConsoleTraceListener, which doesn't exist in DotNetCore

Change 3572811 by Ben.Marsh

	UBT: Add -enableasan / -enabletsan command line options and bEnableAddressSanitizer / bEnableThreadSanitizer settings in BuildConfiguration.xml (and remove environment variables).

Change 3573397 by Ben.Marsh

	UBT: Create a <ExeName>.version file for every target built by UBT, in the same JSON format as Engine/Build/Build.version. This allows monolithic targets to read a version number at runtime, unlike when it's embedded in a modules file, and allows creating versioned client executables that will work with versioned servers when syncing through UGS.

Change 3575659 by Ben.Marsh

	Remove CHM API documentation.

Change 3582103 by Graeme.Thornton

	Simple ResX writer implemetation that the xbox deloyment code can use instead of the one from the windows forms assembly, which isn't supported on .NET Core

	Removed reference to System.Windows.Form from UBT.

Change 3584113 by Ben.Marsh

	Move key-mapping functionality into the InputCore module.

Change 3584278 by Ben.Marsh

	Move FPlatformMisc::RequestMinimize() into FPlatformApplicationMisc.

Change 3584453 by Ben.Marsh

	Move functionality for querying device display density to FApplicationMisc, due to dependence on application-level functionality on mobile platforms.

Change 3585301 by Ben.Marsh

	Move PlatformPostInit() into an FPlatformApplicationMisc function.

Change 3587050 by Ben.Marsh

	Move IsThisApplicationForeground() into FPlatformApplicationMisc.

Change 3587059 by Ben.Marsh

	Move RequiresVirtualKeyboard() into FPlatformApplicationMisc.

Change 3587119 by Ben.Marsh

	Move GetAbsoluteLogFilename() into FPlatformMisc.

Change 3587800 by Steve.Robb

	Fixes to container visualizers for types whose pointer type isn't simply Type*.

Change 3588393 by Ben.Marsh

	Move platform output devices into their own headers.

Change 3588868 by Ben.Marsh

	Move creation of console, error and warning output devices int PlatformApplicationMisc.

Change 3589879 by Graeme.Thornton

	All automation projects now have a reference to DotNETUtilities
	Fixed a build error in the WEX automation library

Change 3590034 by Ben.Marsh

	Move functionality related to windowing and input out of the Core module and into an ApplicationCore module, so it is possible to build utilities with Core without adding dependencies on XInput (Windows), SDL (Linux), and OpenGL (Mac).

Change 3593754 by Steve.Robb

	Fix for tuple debugger visualization.

Change 3597208 by Ben.Marsh

	Move CrashReporter out of a public folder; it's not in a form that is usable by subscribers and licensees.

Change 3600163 by Ben.Marsh

	UBT: Simplify how targets are cleaned. Delete all intermediate folders for a platform/configuration, and delete any build products matching the UE4 naming convention for that target, rather than relying on the current build configuration or list of previous build products. This will ensure that build products which are no longer being generated will also be cleaned.

	#jira UE-46725

Change 3604279 by Graeme.Thornton

	Move pre/post garbage collection delegates into accessor functions so they can be used by globally constructed objects

Change 3606685 by James.Hopkin

	Removed redundant 'Cast's (casting to either the same type or a base).

	In SClassViewer, replaced cast with TAssetPtr::operator* call to get the wrapped UClass.
	Also removed redundant 'IsA's from AnimationRetargetContent::AddRemappedAsset in EditorAnimUtils.cpp.

Change 3610950 by Ben.Marsh

	UAT: Simplify logic for detecting Perforce settings, using environment variables if they are set, otherwise falling back to detecting them. Removes special cases for build machines, and makes it simpler to set up UAT commands on builders outside Epic.

Change 3610991 by Ben.Marsh

	UAT: Use the correct P4 settings to detect settings if only some parameters are specified on the command line.

Change 3612342 by Ben.Marsh

	UBT: Change JsonObject.Read() to take a FileReference parameter.

Change 3612362 by Ben.Marsh

	UBT: Remove some more cases of paths being passed as strings rather than using FileReference objects.

Change 3619128 by Ben.Marsh

	Include builder warnings and errors in the notification emails for automated tests, otherwise it's difficult to track down non-test failures.

[CL 3620189 by Ben Marsh in Main branch]
2017-08-31 12:08:38 -04:00

1620 lines
54 KiB
C#

// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using UnrealBuildTool;
using AutomationTool;
using System.Reflection;
using System.Diagnostics;
using System.Xml.Schema;
using System.Text.RegularExpressions;
using Tools.DotNETCommon;
namespace AutomationTool
{
/// <summary>
/// Implementation of XmlDocument which preserves line numbers for its elements
/// </summary>
class ScriptDocument : XmlDocument
{
/// <summary>
/// The file being read
/// </summary>
FileReference File;
/// <summary>
/// Interface to the LineInfo on the active XmlReader
/// </summary>
IXmlLineInfo LineInfo;
/// <summary>
/// Set to true if the reader encounters an error
/// </summary>
bool bHasErrors;
/// <summary>
/// Private constructor. Use ScriptDocument.Load to read an XML document.
/// </summary>
ScriptDocument(FileReference InFile)
{
File = InFile;
}
/// <summary>
/// Overrides XmlDocument.CreateElement() to construct ScriptElements rather than XmlElements
/// </summary>
public override XmlElement CreateElement(string Prefix, string LocalName, string NamespaceUri)
{
return new ScriptElement(File, LineInfo.LineNumber, Prefix, LocalName, NamespaceUri, this);
}
/// <summary>
/// Loads a script document from the given file
/// </summary>
/// <param name="File">The file to load</param>
/// <param name="Schema">The schema to validate against</param>
/// <param name="OutDocument">If successful, the document that was read</param>
/// <returns>True if the document could be read, false otherwise</returns>
public static bool TryRead(FileReference File, ScriptSchema Schema, out ScriptDocument OutDocument)
{
ScriptDocument Document = new ScriptDocument(File);
XmlReaderSettings Settings = new XmlReaderSettings();
Settings.Schemas.Add(Schema.CompiledSchema);
Settings.ValidationType = ValidationType.Schema;
Settings.ValidationEventHandler += Document.ValidationEvent;
using (XmlReader Reader = XmlReader.Create(File.FullName, Settings))
{
// Read the document
Document.LineInfo = (IXmlLineInfo)Reader;
try
{
Document.Load(Reader);
}
catch (XmlException Ex)
{
if (!Document.bHasErrors)
{
CommandUtils.LogError("{0}", Ex.Message);
Document.bHasErrors = true;
}
}
// If we hit any errors while parsing
if (Document.bHasErrors)
{
OutDocument = null;
return false;
}
// Check that the root element is valid. If not, we didn't actually validate against the schema.
if (Document.DocumentElement.Name != ScriptSchema.RootElementName)
{
CommandUtils.LogError("Script does not have a root element called '{0}'", ScriptSchema.RootElementName);
OutDocument = null;
return false;
}
if (Document.DocumentElement.NamespaceURI != ScriptSchema.NamespaceURI)
{
CommandUtils.LogError("Script root element is not in the '{0}' namespace (add the xmlns=\"{0}\" attribute)", ScriptSchema.NamespaceURI);
OutDocument = null;
return false;
}
}
OutDocument = Document;
return true;
}
/// <summary>
/// Callback for validation errors in the document
/// </summary>
/// <param name="Sender">Standard argument for ValidationEventHandler</param>
/// <param name="Args">Standard argument for ValidationEventHandler</param>
void ValidationEvent(object Sender, ValidationEventArgs Args)
{
if (Args.Severity == XmlSeverityType.Warning)
{
CommandUtils.LogWarning("{0}({1}): {2}", File.FullName, Args.Exception.LineNumber, Args.Message);
}
else
{
CommandUtils.LogError("{0}({1}): {2}", File.FullName, Args.Exception.LineNumber, Args.Message);
bHasErrors = true;
}
}
}
/// <summary>
/// Implementation of XmlElement which preserves line numbers
/// </summary>
class ScriptElement : XmlElement
{
/// <summary>
/// The file containing this element
/// </summary>
public readonly FileReference File;
/// <summary>
/// The line number containing this element
/// </summary>
public readonly int LineNumber;
/// <summary>
/// Constructor
/// </summary>
public ScriptElement(FileReference InFile, int InLineNumber, string Prefix, string LocalName, string NamespaceUri, ScriptDocument Document)
: base(Prefix, LocalName, NamespaceUri, Document)
{
File = InFile;
LineNumber = InLineNumber;
}
}
/// <summary>
/// Reader for build graph definitions. Instanced to contain temporary state; public interface is through ScriptReader.TryRead().
/// </summary>
class ScriptReader
{
/// <summary>
/// The current graph
/// </summary>
Graph Graph = new Graph();
/// <summary>
/// List of property name to value lookups. Modifications to properties are scoped to nodes and agents. EnterScope() pushes an empty dictionary onto the end of this list, and LeaveScope() removes one.
/// ExpandProperties() searches from last to first lookup when trying to resolve a property name, and takes the first it finds.
/// </summary>
List<Dictionary<string, string>> ScopedProperties = new List<Dictionary<string, string>>();
/// <summary>
/// When declaring a property in a nested scope, we enter its name into a set for each parent scope which prevents redeclaration in an OUTER scope later. Subsequent NESTED scopes can redeclare it.
/// The former is likely a coding error, since it implies that the scope of the variable was meant to be further out, whereas the latter is common for temporary and loop variables.
/// </summary>
List<HashSet<string>> ShadowProperties = new List<HashSet<string>>();
/// <summary>
/// Schema for the script
/// </summary>
ScriptSchema Schema;
/// <summary>
/// The number of errors encountered during processing so far
/// </summary>
int NumErrors;
/// <summary>
/// Private constructor. Use ScriptReader.TryRead() to read a script file.
/// </summary>
/// <param name="DefaultProperties">Default properties available to the script</param>
/// <param name="Schema">Schema for the script</param>
private ScriptReader(IDictionary<string, string> DefaultProperties, ScriptSchema Schema)
{
this.Schema = Schema;
EnterScope();
foreach(KeyValuePair<string, string> Pair in DefaultProperties)
{
ScopedProperties[ScopedProperties.Count - 1].Add(Pair.Key, Pair.Value);
}
}
/// <summary>
/// Try to read a script file from the given file.
/// </summary>
/// <param name="File">File to read from</param>
/// <param name="Arguments">Arguments passed in to the graph on the command line</param>
/// <param name="DefaultProperties">Default properties available to the script</param>
/// <param name="Schema">Schema for the script</param>
/// <param name="Graph">If successful, the graph constructed from the given script</param>
/// <returns>True if the graph was read, false if there were errors</returns>
public static bool TryRead(FileReference File, Dictionary<string, string> Arguments, Dictionary<string, string> DefaultProperties, ScriptSchema Schema, out Graph Graph)
{
// Check the file exists before doing anything.
if (!FileReference.Exists(File))
{
CommandUtils.LogError("Cannot open '{0}'", File.FullName);
Graph = null;
return false;
}
// Read the file and build the graph
ScriptReader Reader = new ScriptReader(DefaultProperties, Schema);
if (!Reader.TryRead(File, Arguments) || Reader.NumErrors > 0)
{
Graph = null;
return false;
}
// Make sure all the arguments were valid
bool bInvalidArgument = false;
foreach(string InvalidArgumentName in Arguments.Keys.Except(Reader.Graph.Options.Select(x => x.Name), StringComparer.InvariantCultureIgnoreCase))
{
CommandUtils.LogWarning("Unknown argument '{0}' for '{1}'", InvalidArgumentName, File.FullName);
bInvalidArgument = true;
}
if(bInvalidArgument)
{
Graph = null;
return false;
}
// Return the constructed graph
Graph = Reader.Graph;
return true;
}
/// <summary>
/// Read the script from the given file
/// </summary>
/// <param name="File">File to read from</param>
/// <param name="Arguments">Arguments passed in to the graph on the command line</param>
bool TryRead(FileReference File, Dictionary<string, string> Arguments)
{
// Read the document and validate it against the schema
ScriptDocument Document;
if (!ScriptDocument.TryRead(File, Schema, out Document))
{
NumErrors++;
return false;
}
// Read the root BuildGraph element
ReadGraphBody(Document.DocumentElement, File.Directory, Arguments);
return true;
}
/// <summary>
/// Reads the contents of a graph
/// </summary>
/// <param name="Element">The parent element to read from</param>
/// <param name="BaseDirectory">Base directory to resolve includes against</param>
/// <param name="Arguments">Arguments passed in to the graph on the command line</param>
void ReadGraphBody(XmlElement Element, DirectoryReference BaseDirectory, Dictionary<string, string> Arguments)
{
foreach (ScriptElement ChildElement in Element.ChildNodes.OfType<ScriptElement>())
{
switch (ChildElement.Name)
{
case "Include":
ReadInclude(ChildElement, BaseDirectory, Arguments);
break;
case "Option":
ReadOption(ChildElement, Arguments);
break;
case "Property":
ReadProperty(ChildElement);
break;
case "EnvVar":
ReadEnvVar(ChildElement);
break;
case "Agent":
ReadAgent(ChildElement, null);
break;
case "Aggregate":
ReadAggregate(ChildElement);
break;
case "Report":
ReadReport(ChildElement);
break;
case "Badge":
ReadBadge(ChildElement);
break;
case "Notify":
ReadNotifier(ChildElement);
break;
case "Trigger":
ReadTrigger(ChildElement);
break;
case "Warning":
ReadDiagnostic(ChildElement, LogEventType.Warning, null, null, null);
break;
case "Error":
ReadDiagnostic(ChildElement, LogEventType.Error, null, null, null);
break;
case "Do":
ReadBlock(ChildElement, x => ReadGraphBody(x, BaseDirectory, Arguments));
break;
case "Switch":
ReadSwitch(ChildElement, x => ReadGraphBody(x, BaseDirectory, Arguments));
break;
case "ForEach":
ReadForEach(ChildElement, x => ReadGraphBody(x, BaseDirectory, Arguments));
break;
default:
LogError(ChildElement, "Invalid element '{0}'", ChildElement.Name);
break;
}
}
}
/// <summary>
/// Handles validation messages from validating the document against its schema
/// </summary>
/// <param name="Sender">The source of the event</param>
/// <param name="Args">Event arguments</param>
void ValidationHandler(object Sender, ValidationEventArgs Args)
{
if (Args.Severity == XmlSeverityType.Warning)
{
CommandUtils.LogWarning("Script: {0}", Args.Message);
}
else
{
CommandUtils.LogError("Script: {0}", Args.Message);
NumErrors++;
}
}
/// <summary>
/// Push a new property scope onto the stack
/// </summary>
void EnterScope()
{
ScopedProperties.Add(new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase));
ShadowProperties.Add(new HashSet<string>(StringComparer.InvariantCultureIgnoreCase));
}
/// <summary>
/// Pop a property scope from the stack
/// </summary>
void LeaveScope()
{
ScopedProperties.RemoveAt(ScopedProperties.Count - 1);
ShadowProperties.RemoveAt(ShadowProperties.Count - 1);
}
/// <summary>
/// Sets a property value in the current scope
/// </summary>
/// <param name="Element">Element containing the property assignment. Used for error messages if the property is shadowed in another scope.</param>
/// <param name="Name">Name of the property</param>
/// <param name="Value">Value for the property</param>
void SetPropertyValue(ScriptElement Element, string Name, string Value)
{
// Find the scope containing this property, defaulting to the current scope
int ScopeIdx = 0;
while(ScopeIdx < ScopedProperties.Count - 1 && !ScopedProperties[ScopeIdx].ContainsKey(Name))
{
ScopeIdx++;
}
// Make sure this property name was not already used in a child scope; it likely indicates an error.
if(ShadowProperties[ScopeIdx].Contains(Name))
{
LogError(Element, "Property '{0}' was already used in a child scope. Move this definition before the previous usage if they are intended to share scope, or use a different name.", Name);
}
else
{
// Make sure it's added to the shadow property list for every parent scope
for(int Idx = 0; Idx < ScopeIdx; Idx++)
{
ShadowProperties[Idx].Add(Name);
}
ScopedProperties[ScopeIdx][Name] = Value;
}
}
/// <summary>
/// Tries to get the value of a property
/// </summary>
/// <param name="Name">Name of the property</param>
/// <param name="Value">On success, contains the value of the property. Set to null otherwise.</param>
/// <returns>True if the property was found, false otherwise</returns>
bool TryGetPropertyValue(string Name, out string Value)
{
// Check each scope for the property
for (int ScopeIdx = ScopedProperties.Count - 1; ScopeIdx >= 0; ScopeIdx--)
{
string ScopeValue;
if (ScopedProperties[ScopeIdx].TryGetValue(Name, out ScopeValue))
{
Value = ScopeValue;
return true;
}
}
// If we didn't find it, return false.
Value = null;
return false;
}
/// <summary>
/// Reads the definition for a trigger.
/// </summary>
/// <param name="Element">Xml element to read the definition from</param>
void ReadTrigger(ScriptElement Element)
{
string[] QualifiedName;
if (EvaluateCondition(Element) && TryReadQualifiedObjectName(Element, out QualifiedName))
{
// Validate all the parent triggers
ManualTrigger ParentTrigger = null;
for (int Idx = 0; Idx < QualifiedName.Length - 1; Idx++)
{
ManualTrigger NextTrigger;
if (!Graph.NameToTrigger.TryGetValue(QualifiedName[Idx], out NextTrigger))
{
LogError(Element, "Unknown trigger '{0}'", QualifiedName[Idx]);
return;
}
if (NextTrigger.Parent != ParentTrigger)
{
LogError(Element, "Qualified name of trigger '{0}' is '{1}'", NextTrigger.Name, NextTrigger.QualifiedName);
return;
}
ParentTrigger = NextTrigger;
}
// Get the name of the new trigger
string Name = QualifiedName[QualifiedName.Length - 1];
// Create the new trigger
ManualTrigger Trigger;
if (!Graph.NameToTrigger.TryGetValue(Name, out Trigger))
{
Trigger = new ManualTrigger(ParentTrigger, Name);
Graph.NameToTrigger.Add(Name, Trigger);
}
else if (Trigger.Parent != ParentTrigger)
{
LogError(Element, "Conflicting parent for '{0}' - previously declared as '{1}', now '{2}'", Name, Trigger.QualifiedName, new ManualTrigger(ParentTrigger, Name).QualifiedName);
return;
}
// Read the child elements
ReadTriggerBody(Element, Trigger);
}
}
/// <summary>
/// Reads the body of a trigger element
/// </summary>
/// <param name="Element">Xml element to read the definition from</param>
/// <param name="Trigger">The enclosing trigger definition</param>
void ReadTriggerBody(XmlElement Element, ManualTrigger Trigger)
{
EnterScope();
foreach (ScriptElement ChildElement in Element.ChildNodes.OfType<ScriptElement>())
{
switch (ChildElement.Name)
{
case "Property":
ReadProperty(ChildElement);
break;
case "Agent":
ReadAgent(ChildElement, Trigger);
break;
case "Aggregate":
ReadAggregate(ChildElement);
break;
case "Notifier":
ReadNotifier(ChildElement);
break;
case "Warning":
ReadDiagnostic(ChildElement, LogEventType.Warning, null, null, Trigger);
break;
case "Error":
ReadDiagnostic(ChildElement, LogEventType.Error, null, null, Trigger);
break;
case "Do":
ReadBlock(ChildElement, x => ReadTriggerBody(x, Trigger));
break;
case "Switch":
ReadSwitch(ChildElement, x => ReadTriggerBody(x, Trigger));
break;
case "ForEach":
ReadForEach(ChildElement, x => ReadTriggerBody(x, Trigger));
break;
default:
LogError(ChildElement, "Invalid element '{0}'", ChildElement.Name);
break;
}
}
LeaveScope();
}
/// <summary>
/// Read an include directive, and the contents of the target file
/// </summary>
/// <param name="Element">Xml element to read the definition from</param>
/// <param name="BaseDir">Base directory to resolve relative include paths from </param>
/// <param name="Arguments">Arguments passed in to the graph on the command line</param>
void ReadInclude(ScriptElement Element, DirectoryReference BaseDir, Dictionary<string, string> Arguments)
{
if (EvaluateCondition(Element))
{
FileReference Script = FileReference.Combine(BaseDir, Element.GetAttribute("Script"));
if (!FileReference.Exists(Script))
{
LogError(Element, "Cannot find included script '{0}'", Script.FullName);
}
else
{
TryRead(Script, Arguments);
}
}
}
/// <summary>
/// Reads the definition of a graph option; a parameter which can be set by the user on the command-line or via an environment variable.
/// </summary>
/// <param name="Element">Xml element to read the definition from</param>
/// <param name="Arguments">Arguments passed in to the graph on the command line</param>
void ReadOption(ScriptElement Element, IDictionary<string, string> Arguments)
{
if (EvaluateCondition(Element))
{
string Name = ReadAttribute(Element, "Name");
if (ValidateName(Element, Name))
{
// Make sure we're at global scope
if(ScopedProperties.Count > 1)
{
throw new AutomationException("Incorrect scope depth for reading option settings");
}
// Check if the property already exists. If it does, we don't need to register it as an option.
string ExistingValue;
if(TryGetPropertyValue(Name, out ExistingValue))
{
// If there's a restriction on this definition, check it matches
string Restrict = ReadAttribute(Element, "Restrict");
if(!String.IsNullOrEmpty(Restrict) && !Regex.IsMatch(ExistingValue, "^" + Restrict + "$", RegexOptions.IgnoreCase))
{
LogError(Element, "'{0} is already set to '{1}', which does not match the given restriction ('{2}')", Name, ExistingValue, Restrict);
}
}
else
{
// Create a new option object to store the settings
string Description = ReadAttribute(Element, "Description");
string DefaultValue = ReadAttribute(Element, "DefaultValue");
GraphOption Option = new GraphOption(Name, Description, DefaultValue);
Graph.Options.Add(Option);
// Get the value of this property
string Value;
if(!Arguments.TryGetValue(Name, out Value))
{
Value = Option.DefaultValue;
}
SetPropertyValue(Element, Name, Value);
// If there's a restriction on it, check it's valid
string Restrict = ReadAttribute(Element, "Restrict");
if(!String.IsNullOrEmpty(Restrict))
{
string Pattern = "^" + Restrict + "$";
if(!Regex.IsMatch(Value, Pattern, RegexOptions.IgnoreCase))
{
LogError(Element, "'{0}' is not a valid value for '{1}' (required: '{2}')", Value, Name, Restrict);
}
if(Option.DefaultValue != Value && !Regex.IsMatch(Option.DefaultValue, Pattern, RegexOptions.IgnoreCase))
{
LogError(Element, "Default value '{0}' is not valid for '{1}' (required: '{2}')", Option.DefaultValue, Name, Restrict);
}
}
}
}
}
}
/// <summary>
/// Reads a property assignment.
/// </summary>
/// <param name="Element">Xml element to read the definition from</param>
void ReadProperty(ScriptElement Element)
{
if (EvaluateCondition(Element))
{
string Name = ReadAttribute(Element, "Name");
if (ValidateName(Element, Name))
{
string Value = ReadAttribute(Element, "Value");
if(Element.HasChildNodes)
{
// Read the element content, and append each line to the value as a semicolon delimited list
StringBuilder Builder = new StringBuilder(Value);
foreach(string Line in Element.InnerText.Split('\n'))
{
string TrimLine = ExpandProperties(Element, Line.Trim());
if(TrimLine.Length > 0)
{
if(Builder.Length > 0)
{
Builder.Append(";");
}
Builder.Append(TrimLine);
}
}
Value = Builder.ToString();
}
SetPropertyValue(Element, Name, Value);
}
}
}
/// <summary>
/// Reads a property assignment from an environment variable.
/// </summary>
/// <param name="Element">Xml element to read the definition from</param>
void ReadEnvVar(ScriptElement Element)
{
if (EvaluateCondition(Element))
{
string Name = ReadAttribute(Element, "Name");
if (ValidateName(Element, Name))
{
string Value = Environment.GetEnvironmentVariable(Name) ?? "";
SetPropertyValue(Element, Name, Value);
}
}
}
/// <summary>
/// Reads the definition for an agent.
/// </summary>
/// <param name="Element">Xml element to read the definition from</param>
/// <param name="Trigger">The controlling trigger for nodes in this agent</param>
void ReadAgent(ScriptElement Element, ManualTrigger Trigger)
{
string Name;
if (EvaluateCondition(Element) && TryReadObjectName(Element, out Name))
{
// Read the valid agent types. This may be omitted if we're continuing an existing agent.
string[] Types = ReadListAttribute(Element, "Type");
// Create the agent object, or continue an existing one
Agent Agent;
if (Graph.NameToAgent.TryGetValue(Name, out Agent))
{
if (Types.Length > 0 && Agent.PossibleTypes.Length > 0)
{
string[] NewTypes = Agent.PossibleTypes.Intersect(Types, StringComparer.InvariantCultureIgnoreCase).ToArray();
if (NewTypes.Length == 0)
{
LogError(Element, "No common agent types with previous agent definition");
}
Agent.PossibleTypes = NewTypes;
}
}
else
{
if (Types.Length == 0)
{
LogError(Element, "Missing type for agent '{0}'", Name);
}
Agent = new Agent(Name, Types);
Graph.NameToAgent.Add(Name, Agent);
Graph.Agents.Add(Agent);
}
// Process all the child elements.
ReadAgentBody(Element, Agent, Trigger);
}
}
/// <summary>
/// Read the contents of an agent definition
/// </summary>
/// <param name="Element">Xml element to read the definition from</param>
/// <param name="ParentAgent">The agent to contain the definition</param>
/// <param name="ControllingTrigger">The enclosing trigger</param>
void ReadAgentBody(ScriptElement Element, Agent ParentAgent, ManualTrigger ControllingTrigger)
{
EnterScope();
foreach (ScriptElement ChildElement in Element.ChildNodes.OfType<ScriptElement>())
{
switch (ChildElement.Name)
{
case "Property":
ReadProperty(ChildElement);
break;
case "Node":
ReadNode(ChildElement, ParentAgent, ControllingTrigger);
break;
case "Aggregate":
ReadAggregate(ChildElement);
break;
case "Warning":
ReadDiagnostic(ChildElement, LogEventType.Warning, null, ParentAgent, ControllingTrigger);
break;
case "Error":
ReadDiagnostic(ChildElement, LogEventType.Error, null, ParentAgent, ControllingTrigger);
break;
case "Do":
ReadBlock(ChildElement, x => ReadAgentBody(x, ParentAgent, ControllingTrigger));
break;
case "Switch":
ReadSwitch(ChildElement, x => ReadAgentBody(x, ParentAgent, ControllingTrigger));
break;
case "ForEach":
ReadForEach(ChildElement, x => ReadAgentBody(x, ParentAgent, ControllingTrigger));
break;
default:
LogError(ChildElement, "Unexpected element type '{0}'", ChildElement.Name);
break;
}
}
LeaveScope();
}
/// <summary>
/// Reads the definition for an aggregate
/// </summary>
/// <param name="Element">Xml element to read the definition from</param>
void ReadAggregate(ScriptElement Element)
{
string Name;
if (EvaluateCondition(Element) && TryReadObjectName(Element, out Name) && CheckNameIsUnique(Element, Name))
{
string[] RequiredNames = ReadListAttribute(Element, "Requires");
Graph.AggregateNameToNodes.Add(Name, ResolveReferences(Element, RequiredNames).ToArray());
}
}
/// <summary>
/// Reads the definition for a report
/// </summary>
/// <param name="Element">Xml element to read the definition from</param>
void ReadReport(ScriptElement Element)
{
string Name;
if (EvaluateCondition(Element) && TryReadObjectName(Element, out Name) && CheckNameIsUnique(Element, Name))
{
string[] RequiredNames = ReadListAttribute(Element, "Requires");
Report NewReport = new Report(Name);
foreach (Node ReferencedNode in ResolveReferences(Element, RequiredNames))
{
NewReport.Nodes.Add(ReferencedNode);
NewReport.Nodes.UnionWith(ReferencedNode.OrderDependencies);
}
Graph.NameToReport.Add(Name, NewReport);
}
}
/// <summary>
/// Reads the definition for a badge
/// </summary>
/// <param name="Element">Xml element to read the definition from</param>
void ReadBadge(ScriptElement Element)
{
string Name;
if (EvaluateCondition(Element) && TryReadObjectName(Element, out Name))
{
string[] RequiredNames = ReadListAttribute(Element, "Requires");
string Project = ReadAttribute(Element, "Project");
int Change = ReadIntegerAttribute(Element, "Change", 0);
Badge NewBadge = new Badge(Name, Project, Change);
foreach (Node ReferencedNode in ResolveReferences(Element, RequiredNames))
{
NewBadge.Nodes.Add(ReferencedNode);
}
Graph.Badges.Add(NewBadge);
}
}
/// <summary>
/// Reads the definition for a node, and adds it to the given agent
/// </summary>
/// <param name="Element">Xml element to read the definition from</param>
/// <param name="ParentAgent">Agent for the node to be added to</param>
/// <param name="ControllingTrigger">The controlling trigger for this node</param>
void ReadNode(ScriptElement Element, Agent ParentAgent, ManualTrigger ControllingTrigger)
{
string Name;
if (EvaluateCondition(Element) && TryReadObjectName(Element, out Name))
{
string[] RequiresNames = ReadListAttribute(Element, "Requires");
string[] ProducesNames = ReadListAttribute(Element, "Produces");
string[] AfterNames = ReadListAttribute(Element, "After");
string[] TokenFileNames = ReadListAttribute(Element, "Token");
bool bNotifyOnWarnings = ReadBooleanAttribute(Element, "NotifyOnWarnings", true);
// Resolve all the inputs we depend on
HashSet<NodeOutput> Inputs = ResolveInputReferences(Element, RequiresNames);
// Gather up all the input dependencies, and check they're all upstream of the current node
HashSet<Node> InputDependencies = new HashSet<Node>();
foreach (Node InputDependency in Inputs.Select(x => x.ProducingNode).Distinct())
{
if (InputDependency.ControllingTrigger != null && InputDependency.ControllingTrigger != ControllingTrigger && !InputDependency.ControllingTrigger.IsUpstreamFrom(ControllingTrigger))
{
LogError(Element, "'{0}' is dependent on '{1}', which is behind a different controlling trigger ({2})", Name, InputDependency.Name, InputDependency.ControllingTrigger.QualifiedName);
}
else
{
InputDependencies.Add(InputDependency);
}
}
// Remove all the lock names from the list of required names
HashSet<FileReference> RequiredTokens = new HashSet<FileReference>(TokenFileNames.Select(x => FileReference.Combine(CommandUtils.RootDirectory, x)));
// Recursively include all their dependencies too
foreach (Node InputDependency in InputDependencies.ToArray())
{
RequiredTokens.UnionWith(InputDependency.RequiredTokens);
InputDependencies.UnionWith(InputDependency.InputDependencies);
}
// Validate all the outputs
List<string> ValidOutputNames = new List<string>();
foreach (string ProducesName in ProducesNames)
{
NodeOutput ExistingOutput;
if(Graph.TagNameToNodeOutput.TryGetValue(ProducesName, out ExistingOutput))
{
LogError(Element, "Output tag '{0}' is already generated by node '{1}'", ProducesName, ExistingOutput.ProducingNode.Name);
}
else if(Graph.LocalTagNames.Contains(ProducesName))
{
LogError(Element, "Output tag '{0}' is used elsewhere as a local tag name", ProducesName);
}
else if(!ProducesName.StartsWith("#"))
{
LogError(Element, "Output tag names must begin with a '#' character ('{0}')", ProducesName);
}
else
{
ValidOutputNames.Add(ProducesName);
}
}
// Gather up all the order dependencies
HashSet<Node> OrderDependencies = new HashSet<Node>(InputDependencies);
OrderDependencies.UnionWith(ResolveReferences(Element, AfterNames));
// Recursively include all their order dependencies too
foreach (Node OrderDependency in OrderDependencies.ToArray())
{
OrderDependencies.UnionWith(OrderDependency.OrderDependencies);
}
// Check that we're not dependent on anything completing that is declared after the initial declaration of this agent.
int AgentIdx = Graph.Agents.IndexOf(ParentAgent);
for (int Idx = AgentIdx + 1; Idx < Graph.Agents.Count; Idx++)
{
foreach (Node Node in Graph.Agents[Idx].Nodes.Where(x => OrderDependencies.Contains(x)))
{
LogError(Element, "Node '{0}' has a dependency on '{1}', which was declared after the initial definition of '{2}'.", Name, Node.Name, ParentAgent.Name);
}
}
// Construct and register the node
if (CheckNameIsUnique(Element, Name))
{
// Add it to the node lookup
Node NewNode = new Node(Name, Inputs.ToArray(), ValidOutputNames.ToArray(), InputDependencies.ToArray(), OrderDependencies.ToArray(), ControllingTrigger, RequiredTokens.ToArray());
NewNode.bNotifyOnWarnings = bNotifyOnWarnings;
Graph.NameToNode.Add(Name, NewNode);
// Register all the output tags in the global name table.
foreach(NodeOutput Output in NewNode.Outputs)
{
Graph.TagNameToNodeOutput.Add(Output.TagName, Output);
}
// Add all the tasks
ReadNodeBody(Element, NewNode, ParentAgent, ControllingTrigger);
// Add it to the current agent
ParentAgent.Nodes.Add(NewNode);
}
}
}
/// <summary>
/// Reads the contents of a node element
/// </summary>
/// <param name="Element">Xml element to read the definition from</param>
/// <param name="NewNode">The new node that has been created</param>
/// <param name="ParentAgent">Agent for the node to be added to</param>
/// <param name="ControllingTrigger">The controlling trigger for this node</param>
void ReadNodeBody(XmlElement Element, Node NewNode, Agent ParentAgent, ManualTrigger ControllingTrigger)
{
EnterScope();
foreach (ScriptElement ChildElement in Element.ChildNodes.OfType<ScriptElement>())
{
switch (ChildElement.Name)
{
case "Property":
ReadProperty(ChildElement);
break;
case "Warning":
ReadDiagnostic(ChildElement, LogEventType.Warning, NewNode, ParentAgent, ControllingTrigger);
break;
case "Error":
ReadDiagnostic(ChildElement, LogEventType.Error, NewNode, ParentAgent, ControllingTrigger);
break;
case "Do":
ReadBlock(ChildElement, x => ReadNodeBody(x, NewNode, ParentAgent, ControllingTrigger));
break;
case "Switch":
ReadSwitch(ChildElement, x => ReadNodeBody(x, NewNode, ParentAgent, ControllingTrigger));
break;
case "ForEach":
ReadForEach(ChildElement, x => ReadNodeBody(x, NewNode, ParentAgent, ControllingTrigger));
break;
default:
ReadTask(ChildElement, NewNode);
break;
}
}
LeaveScope();
}
/// <summary>
/// Reads a block element
/// </summary>
/// <param name="Element">Xml element to read the definition from</param>
/// <param name="ReadContents">Delegate to read the contents of the element, if the condition evaluates to true</param>
void ReadBlock(ScriptElement Element, Action<ScriptElement> ReadContents)
{
if (EvaluateCondition(Element))
{
ReadContents(Element);
}
}
/// <summary>
/// Reads a "Switch" element
/// </summary>
/// <param name="Element">Xml element to read the definition from</param>
/// <param name="ReadContents">Delegate to read the contents of the element, if the condition evaluates to true</param>
void ReadSwitch(ScriptElement Element, Action<ScriptElement> ReadContents)
{
foreach (ScriptElement ChildElement in Element.ChildNodes.OfType<ScriptElement>())
{
if (ChildElement.Name == "Default" || EvaluateCondition(ChildElement))
{
ReadContents(ChildElement);
break;
}
}
}
/// <summary>
/// Reads a "ForEach" element
/// </summary>
/// <param name="Element">Xml element to read the definition from</param>
/// <param name="ReadContents">Delegate to read the contents of the element, if the condition evaluates to true</param>
void ReadForEach(ScriptElement Element, Action<ScriptElement> ReadContents)
{
EnterScope();
if(EvaluateCondition(Element))
{
string Name = ReadAttribute(Element, "Name");
if (ValidateName(Element, Name))
{
if(ScopedProperties.Any(x => x.ContainsKey(Name)))
{
LogError(Element, "Loop variable '{0}' already exists as a local property in an outer scope", Name);
}
else
{
// Loop through all the values
string[] Values = ReadListAttribute(Element, "Values");
foreach(string Value in Values)
{
ScopedProperties[ScopedProperties.Count - 1][Name] = Value;
ReadContents(Element);
}
}
}
}
LeaveScope();
}
/// <summary>
/// Reads a task definition from the given element, and add it to the given list
/// </summary>
/// <param name="Element">Xml element to read the definition from</param>
/// <param name="ParentNode">The node which owns this task</param>
void ReadTask(ScriptElement Element, Node ParentNode)
{
if (EvaluateCondition(Element))
{
// Get the reflection info for this element
ScriptTask Task;
if (!Schema.TryGetTask(Element.Name, out Task))
{
LogError(Element, "Unknown task '{0}'", Element.Name);
return;
}
// Check all the required parameters are present
bool bHasRequiredAttributes = true;
foreach (ScriptTaskParameter Parameter in Task.NameToParameter.Values)
{
if (!Parameter.bOptional && !Element.HasAttribute(Parameter.Name))
{
LogError(Element, "Missing required attribute - {0}", Parameter.Name);
bHasRequiredAttributes = false;
}
}
// Read all the attributes into a parameters object for this task
object ParametersObject = Activator.CreateInstance(Task.ParametersClass);
foreach (XmlAttribute Attribute in Element.Attributes)
{
if (String.Compare(Attribute.Name, "If", StringComparison.InvariantCultureIgnoreCase) != 0)
{
// Get the field that this attribute should be written to in the parameters object
ScriptTaskParameter Parameter;
if (!Task.NameToParameter.TryGetValue(Attribute.Name, out Parameter))
{
LogError(Element, "Unknown attribute '{0}'", Attribute.Name);
continue;
}
// Expand variables in the value
string ExpandedValue = ExpandProperties(Element, Attribute.Value);
// Parse it and assign it to the parameters object
object Value;
if (Parameter.ValueType.IsEnum)
{
Value = Enum.Parse(Parameter.ValueType, ExpandedValue);
}
else if (Parameter.ValueType == typeof(Boolean))
{
Value = Condition.Evaluate(ExpandedValue);
}
else
{
Value = Convert.ChangeType(ExpandedValue, Parameter.ValueType);
}
Parameter.FieldInfo.SetValue(ParametersObject, Value);
}
}
// Construct the task
if (bHasRequiredAttributes)
{
// Add it to the list
CustomTask NewTask = (CustomTask)Activator.CreateInstance(Task.TaskClass, ParametersObject);
ParentNode.Tasks.Add(NewTask);
// Set up the source location for diagnostics
NewTask.SourceLocation = Tuple.Create(Element.File, Element.LineNumber);
// Make sure all the read tags are local or listed as a dependency
foreach(string ReadTagName in NewTask.FindConsumedTagNames())
{
NodeOutput Output;
if(!Graph.TagNameToNodeOutput.TryGetValue(ReadTagName, out Output))
{
Graph.LocalTagNames.Add(ReadTagName);
}
else if(Output != null && Output.ProducingNode != ParentNode && !ParentNode.Inputs.Contains(Output))
{
LogError(Element, "The tag '{0}' is not a dependency of node '{1}'", ReadTagName, ParentNode.Name);
}
}
// Make sure all the written tags are local or listed as an output
foreach(string ModifiedTagName in NewTask.FindProducedTagNames())
{
NodeOutput Output;
if(!Graph.TagNameToNodeOutput.TryGetValue(ModifiedTagName, out Output))
{
Graph.LocalTagNames.Add(ModifiedTagName);
}
else if(Output != null && !ParentNode.Outputs.Contains(Output))
{
LogError(Element, "The tag '{0}' is created by '{1}', and cannot be modified downstream", Output.TagName, Output.ProducingNode.Name);
}
}
}
}
}
/// <summary>
/// Reads the definition for an email notifier
/// </summary>
/// <param name="Element">Xml element to read the definition from</param>
void ReadNotifier(ScriptElement Element)
{
if (EvaluateCondition(Element))
{
string[] TargetNames = ReadListAttribute(Element, "Targets");
string[] ExceptNames = ReadListAttribute(Element, "Except");
string[] IndividualNodeNames = ReadListAttribute(Element, "Nodes");
string[] TriggerNames = ReadListAttribute(Element, "Triggers");
string[] ReportNames = ReadListAttribute(Element, "Reports");
string[] Users = ReadListAttribute(Element, "Users");
string[] Submitters = ReadListAttribute(Element, "Submitters");
bool? bWarnings = Element.HasAttribute("Warnings") ? (bool?)ReadBooleanAttribute(Element, "Warnings", true) : null;
bool bAbsolute = Element.HasAttribute("Absolute") ? ReadBooleanAttribute(Element, "Absolute", true) : false;
// Find the list of targets which are included, and recurse through all their dependencies
HashSet<Node> Nodes = new HashSet<Node>();
if (TargetNames != null)
{
HashSet<Node> TargetNodes = ResolveReferences(Element, TargetNames);
foreach (Node Node in TargetNodes)
{
Nodes.Add(Node);
Nodes.UnionWith(Node.InputDependencies);
}
}
// Add all the individually referenced nodes
if (IndividualNodeNames != null)
{
HashSet<Node> IndividualNodes = ResolveReferences(Element, IndividualNodeNames);
Nodes.UnionWith(IndividualNodes);
}
// Exclude all the exceptions
if (ExceptNames != null)
{
HashSet<Node> ExceptNodes = ResolveReferences(Element, ExceptNames);
Nodes.ExceptWith(ExceptNodes);
}
// Update all the referenced nodes with the settings
foreach (Node Node in Nodes)
{
if (Users != null)
{
if (bAbsolute)
{
Node.NotifyUsers = new HashSet<string>(Users);
}
else
{
Node.NotifyUsers.UnionWith(Users);
}
}
if (Submitters != null)
{
if (bAbsolute)
{
Node.NotifySubmitters = new HashSet<string>(Submitters);
}
else
{
Node.NotifySubmitters.UnionWith(Submitters);
}
}
if (bWarnings.HasValue)
{
Node.bNotifyOnWarnings = bWarnings.Value;
}
}
// Add the users to the list of triggers
if (TriggerNames != null)
{
foreach (string TriggerName in TriggerNames)
{
ManualTrigger Trigger;
if (Graph.NameToTrigger.TryGetValue(TriggerName, out Trigger))
{
Trigger.NotifyUsers.UnionWith(Users);
}
else
{
LogError(Element, "Trigger '{0}' has not been defined", TriggerName);
}
}
}
// Add the users to the list of reports
if (ReportNames != null)
{
foreach (string ReportName in ReportNames)
{
Report Report;
if (Graph.NameToReport.TryGetValue(ReportName, out Report))
{
Report.NotifyUsers.UnionWith(Users);
}
else
{
LogError(Element, "Report '{0}' has not been defined", ReportName);
}
}
}
}
}
/// <summary>
/// Reads a warning from the given element, evaluates the condition on it, and writes it to the log if the condition passes.
/// </summary>
/// <param name="Element">Xml element to read the definition from</param>
/// <param name="EventType">The diagnostic event type</param>
/// <param name="EnclosingNode">The node that this diagnostic is declared in, or null</param>
/// <param name="EnclosingAgent">The agent that this diagnostic is declared in, or null</param>
/// <param name="EnclosingTrigger">The trigger that this diagnostic is declared in, or null</param>
void ReadDiagnostic(ScriptElement Element, LogEventType EventType, Node EnclosingNode, Agent EnclosingAgent, ManualTrigger EnclosingTrigger)
{
if (EvaluateCondition(Element))
{
string Message = ReadAttribute(Element, "Message");
GraphDiagnostic Diagnostic = new GraphDiagnostic();
Diagnostic.EventType = EventType;
Diagnostic.Message = String.Format("{0}({1}): {2}", Element.File.FullName, Element.LineNumber, Message);
Diagnostic.EnclosingNode = EnclosingNode;
Diagnostic.EnclosingAgent = EnclosingAgent;
Diagnostic.EnclosingTrigger = EnclosingTrigger;
Graph.Diagnostics.Add(Diagnostic);
}
}
/// <summary>
/// Checks that the given name does not already used to refer to a node, and print an error if it is.
/// </summary>
/// <param name="Element">Xml element to read from</param>
/// <param name="Name">Name of the alias</param>
/// <returns>True if the name was registered correctly, false otherwise.</returns>
bool CheckNameIsUnique(ScriptElement Element, string Name)
{
// Get the nodes that it maps to
if (Graph.ContainsName(Name))
{
LogError(Element, "'{0}' is already defined; cannot add a second time", Name);
return false;
}
return true;
}
/// <summary>
/// Resolve a list of references to a set of nodes
/// </summary>
/// <param name="Element">Element used to locate any errors</param>
/// <param name="ReferenceNames">Sequence of names to look up</param>
/// <returns>Hashset of all the nodes included by the given names</returns>
HashSet<Node> ResolveReferences(ScriptElement Element, IEnumerable<string> ReferenceNames)
{
HashSet<Node> Nodes = new HashSet<Node>();
foreach (string ReferenceName in ReferenceNames)
{
Node[] OtherNodes;
if (Graph.TryResolveReference(ReferenceName, out OtherNodes))
{
Nodes.UnionWith(OtherNodes);
}
else if (!ReferenceName.StartsWith("#") && Graph.TagNameToNodeOutput.ContainsKey("#" + ReferenceName))
{
LogError(Element, "Reference to '{0}' cannot be resolved; did you mean '#{0}'?", ReferenceName);
}
else
{
LogError(Element, "Reference to '{0}' cannot be resolved; check it has been defined.", ReferenceName);
}
}
return Nodes;
}
/// <summary>
/// Resolve a list of references to a set of nodes
/// </summary>
/// <param name="Element">Element used to locate any errors</param>
/// <param name="ReferenceNames">Sequence of names to look up</param>
/// <returns>Set of all the nodes included by the given names</returns>
HashSet<NodeOutput> ResolveInputReferences(ScriptElement Element, IEnumerable<string> ReferenceNames)
{
HashSet<NodeOutput> Inputs = new HashSet<NodeOutput>();
foreach (string ReferenceName in ReferenceNames)
{
NodeOutput[] ReferenceInputs;
if (Graph.TryResolveInputReference(ReferenceName, out ReferenceInputs))
{
Inputs.UnionWith(ReferenceInputs);
}
else if (!ReferenceName.StartsWith("#") && Graph.TagNameToNodeOutput.ContainsKey("#" + ReferenceName))
{
LogError(Element, "Reference to '{0}' cannot be resolved; did you mean '#{0}'?", ReferenceName);
}
else
{
LogError(Element, "Reference to '{0}' cannot be resolved; check it has been defined.", ReferenceName);
}
}
return Inputs;
}
/// <summary>
/// Reads an object name from its defining element. Outputs an error if the name is missing.
/// </summary>
/// <param name="Element">Element to read the name for</param>
/// <param name="Name">Output variable to receive the name of the object</param>
/// <returns>True if the object had a valid name (assigned to the Name variable), false if the name was invalid or missing.</returns>
bool TryReadObjectName(ScriptElement Element, out string Name)
{
// Check the name attribute is present
if (!Element.HasAttribute("Name"))
{
LogError(Element, "Missing 'Name' attribute");
Name = null;
return false;
}
// Get the value of it, strip any leading or trailing whitespace, and make sure it's not empty
string Value = ReadAttribute(Element, "Name");
if (!ValidateName(Element, Value))
{
Name = null;
return false;
}
// Return it
Name = Value;
return true;
}
/// <summary>
/// Reads an qualified object name from its defining element. Outputs an error if the name is missing.
/// </summary>
/// <param name="Element">Element to read the name for</param>
/// <param name="QualifiedName">Output variable to receive the name of the object</param>
/// <returns>True if the object had a valid name (assigned to the Name variable), false if the name was invalid or missing.</returns>
bool TryReadQualifiedObjectName(ScriptElement Element, out string[] QualifiedName)
{
// Check the name attribute is present
if (!Element.HasAttribute("Name"))
{
LogError(Element, "Missing 'Name' attribute");
QualifiedName = null;
return false;
}
// Get the value of it, strip any leading or trailing whitespace, and make sure it's not empty
string[] Values = ReadAttribute(Element, "Name").Split('.');
foreach (string Value in Values)
{
if (!ValidateName(Element, Value))
{
QualifiedName = null;
return false;
}
}
// Return it
QualifiedName = Values;
return true;
}
/// <summary>
/// Checks that the given name is valid syntax
/// </summary>
/// <param name="Element">The element that contains the name</param>
/// <param name="Name">The name to check</param>
/// <returns>True if the name is valid</returns>
bool ValidateName(ScriptElement Element, string Name)
{
// Check it's not empty
if (Name.Length == 0)
{
LogError(Element, "Name is empty");
return false;
}
// Check there are no invalid characters
for (int Idx = 0; Idx < Name.Length; Idx++)
{
if (Idx > 0 && Name[Idx] == ' ' && Name[Idx - 1] == ' ')
{
LogError(Element, "Consecutive spaces in object name");
return false;
}
if(Char.IsControl(Name[Idx]) || ScriptSchema.IllegalNameCharacters.IndexOf(Name[Idx]) != -1)
{
LogError(Element, "Invalid character in object name - '{0}'", Name[Idx]);
return false;
}
}
return true;
}
/// <summary>
/// Expands any properties and reads an attribute.
/// </summary>
/// <param name="Element">Element to read the attribute from</param>
/// <param name="Name">Name of the attribute</param>
/// <returns>Array of names, with all leading and trailing whitespace removed</returns>
string ReadAttribute(ScriptElement Element, string Name)
{
return ExpandProperties(Element, Element.GetAttribute(Name));
}
/// <summary>
/// Expands any properties and reads a list of strings from an attribute, separated by semi-colon characters
/// </summary>
/// <param name="Element"></param>
/// <param name="Name"></param>
/// <returns>Array of names, with all leading and trailing whitespace removed</returns>
string[] ReadListAttribute(ScriptElement Element, string Name)
{
string Value = ReadAttribute(Element, Name);
return Value.Split(new char[] { ';' }).Select(x => x.Trim()).Where(x => x.Length > 0).ToArray();
}
/// <summary>
/// Reads an attribute from the given XML element, expands any properties in it, and parses it as a boolean.
/// </summary>
/// <param name="Element">Element to read the attribute from</param>
/// <param name="Name">Name of the attribute</param>
/// <param name="bDefaultValue">Default value if the attribute is missing</param>
/// <returns>The value of the attribute field</returns>
bool ReadBooleanAttribute(ScriptElement Element, string Name, bool bDefaultValue)
{
bool bResult = bDefaultValue;
if (Element.HasAttribute(Name))
{
string Value = ReadAttribute(Element, Name).Trim();
if (Value.Equals("true", StringComparison.InvariantCultureIgnoreCase))
{
bResult = true;
}
else if (Value.Equals("false", StringComparison.InvariantCultureIgnoreCase))
{
bResult = false;
}
else
{
LogError(Element, "Invalid boolean value '{0}' - expected 'true' or 'false'", Value);
}
}
return bResult;
}
/// <summary>
/// Reads an attribute from the given XML element, expands any properties in it, and parses it as an integer.
/// </summary>
/// <param name="Element">Element to read the attribute from</param>
/// <param name="Name">Name of the attribute</param>
/// <param name="DefaultValue">Default value for the integer, if the attribute is missing</param>
/// <returns>The value of the attribute field</returns>
int ReadIntegerAttribute(ScriptElement Element, string Name, int DefaultValue)
{
int Result = DefaultValue;
if (Element.HasAttribute(Name))
{
string Value = ReadAttribute(Element, Name).Trim();
int IntValue;
if (Int32.TryParse(Value, out IntValue))
{
Result = IntValue;
}
else
{
LogError(Element, "Invalid integer value '{0}'", Value);
}
}
return Result;
}
/// <summary>
/// Reads an attribute from the given XML element, expands any properties in it, and parses it as an enum of the given type.
/// </summary>
/// <typeparam name="T">The enum type to parse the attribute as</typeparam>
/// <param name="Element">Element to read the attribute from</param>
/// <param name="Name">Name of the attribute</param>
/// <param name="DefaultValue">Default value for the enum, if the attribute is missing</param>
/// <returns>The value of the attribute field</returns>
T ReadEnumAttribute<T>(ScriptElement Element, string Name, T DefaultValue) where T : struct
{
T Result = DefaultValue;
if (Element.HasAttribute(Name))
{
string Value = ReadAttribute(Element, Name).Trim();
T EnumValue;
if (Enum.TryParse(Value, true, out EnumValue))
{
Result = EnumValue;
}
else
{
LogError(Element, "Invalid value '{0}' - expected {1}", Value, String.Join("/", Enum.GetNames(typeof(T))));
}
}
return Result;
}
/// <summary>
/// Outputs an error message to the log and increments the number of errors, referencing the file and line number of the element that caused it.
/// </summary>
/// <param name="Element">The script element causing the error</param>
/// <param name="Format">Standard String.Format()-style format string</param>
/// <param name="Args">Optional arguments</param>
void LogError(ScriptElement Element, string Format, params object[] Args)
{
CommandUtils.LogError("{0}({1}): {2}", Element.File.FullName, Element.LineNumber, String.Format(Format, Args));
NumErrors++;
}
/// <summary>
/// Outputs a warning message to the log and increments the number of errors, referencing the file and line number of the element that caused it.
/// </summary>
/// <param name="Element">The script element causing the error</param>
/// <param name="Format">Standard String.Format()-style format string</param>
/// <param name="Args">Optional arguments</param>
void LogWarning(ScriptElement Element, string Format, params object[] Args)
{
CommandUtils.LogWarning("{0}({1}): {2}", Element.File.FullName, Element.LineNumber, String.Format(Format, Args));
}
/// <summary>
/// Evaluates the (optional) conditional expression on a given XML element via the If="..." attribute, and returns true if the element is enabled.
/// </summary>
/// <param name="Element">The element to check</param>
/// <returns>True if the element's condition evaluates to true (or doesn't have a conditional expression), false otherwise</returns>
bool EvaluateCondition(ScriptElement Element)
{
// Check if the element has a conditional attribute
const string AttributeName = "If";
if (!Element.HasAttribute(AttributeName))
{
return true;
}
// If it does, try to evaluate it.
try
{
string Text = ExpandProperties(Element, Element.GetAttribute("If"));
return Condition.Evaluate(Text);
}
catch (ConditionException Ex)
{
LogError(Element, "Error in condition: {0}", Ex.Message);
return false;
}
}
/// <summary>
/// Expand all the property references (of the form $(PropertyName)) in a string.
/// </summary>
/// <param name="Element">The element containing the string. Used for diagnostic messages.</param>
/// <param name="Text">The input string to expand properties in</param>
/// <returns>The expanded string</returns>
string ExpandProperties(ScriptElement Element, string Text)
{
string Result = Text;
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(!TryGetPropertyValue(Name, out Value))
{
LogWarning(Element, "Property '{0}' is not defined", Name);
Value = "";
}
// Replace the variable, or skip past it
Result = Result.Substring(0, Idx) + Value + Result.Substring(EndIdx + 1);
// Make sure we skip over the expanded variable; we don't want to recurse on it.
Idx += Value.Length;
}
return Result;
}
}
}