// 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; namespace UnrealBuildTool { /// /// Stores custom build steps to be executed by a project or plugin /// public class CustomBuildSteps { SortedDictionary HostPlatformToCommands = new SortedDictionary(); /// /// Construct a custom build steps object from a Json object. /// public CustomBuildSteps(JsonObject RawObject) { foreach(string HostPlatformName in RawObject.KeyNames) { UnrealTargetPlatform HostPlatform; if(Enum.TryParse(HostPlatformName, true, out HostPlatform)) { HostPlatformToCommands.Add(HostPlatform, RawObject.GetStringArrayField(HostPlatformName)); } } } /// /// Reads a list of build steps from a Json project or plugin descriptor /// /// The json descriptor object /// Name of the field to read /// Output variable to store the sorted dictionary that was read /// True if the field was read (and OutBuildSteps is set), false otherwise. public static bool TryRead(JsonObject RawObject, string FieldName, out CustomBuildSteps OutBuildSteps) { JsonObject BuildStepsObject; if(RawObject.TryGetObjectField(FieldName, out BuildStepsObject)) { OutBuildSteps = new CustomBuildSteps(BuildStepsObject); return true; } else { OutBuildSteps = null; return false; } } /// /// Reads a list of build steps from a Json project or plugin descriptor /// /// Writer to receive json output /// Name of the field to read /// True if the field was read (and OutBuildSteps is set), false otherwise. public void Write(JsonWriter Writer, string FieldName) { Writer.WriteObjectStart(FieldName); foreach(KeyValuePair Pair in HostPlatformToCommands) { Writer.WriteArrayStart(Pair.Key.ToString()); foreach(string Line in Pair.Value) { Writer.WriteValue(Line); } Writer.WriteArrayEnd(); } Writer.WriteObjectEnd(); } /// /// Determines whether there are custom build steps for the given host platform /// /// The host platform to check for /// True if the host platform exists public bool HasHostPlatform(UnrealTargetPlatform HostPlatform) { string[] Commands; return HostPlatformToCommands.TryGetValue(HostPlatform, out Commands) && Commands.Length > 0; } /// /// Tries to get the commands for a given host platform /// /// The host platform to look for /// Lookup of additional environment variables to expand /// Array of commands /// True if a list of commands was generated public bool TryGetCommands(UnrealTargetPlatform HostPlatform, Dictionary Variables, out string[] OutCommands) { string[] Commands; if(HostPlatformToCommands.TryGetValue(HostPlatform, out Commands) && Commands.Length > 0) { OutCommands = Commands.Select(x => Utils.ExpandVariables(x, Variables)).ToArray(); return true; } else { OutCommands = null; return false; } } } }