// Copyright Epic Games, Inc. All Rights Reserved. using AutomationTool; using EpicGames.BuildGraph; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Xml; using EpicGames.Core; using UnrealBuildTool; using UnrealBuildBase; using Microsoft.Extensions.Logging; using System.Security.Cryptography; namespace AutomationTool.Tasks { /// /// Parameters for . /// public class RandomDataTaskParameters { /// /// The size of each file. /// [TaskParameter(Optional = true)] public int Size { get; set; } = 1024; /// /// Number of files to write. /// [TaskParameter(Optional = true)] public int Count { get; set; } = 50; /// /// Whether to generate different data for each output file. /// [TaskParameter(Optional = true)] public bool Different { get; set; } = true; /// /// Output directory /// [TaskParameter(Optional = true)] public string OutputDir { get; set; } /// /// Optional filter to be applied to the list of input files. /// [TaskParameter(Optional = true)] public string Tag { get; set; } } /// /// Creates files containing random data in the specified output directory. Used for generating test data for the temp storage system. /// [TaskElement("RandomData", typeof(RandomDataTaskParameters))] public class RandomDataTask : BgTaskImpl { /// /// Parameters for this task /// RandomDataTaskParameters Parameters; /// /// Constructor /// /// Parameters for this task public RandomDataTask(RandomDataTaskParameters InParameters) { Parameters = InParameters; } /// /// Execute the task. /// /// Information about the current job /// Set of build products produced by this node. /// Mapping from tag names to the set of files they include public override async Task ExecuteAsync(JobContext Job, HashSet BuildProducts, Dictionary> TagNameToFileSet) { DirectoryReference OutputDir = ResolveDirectory(Parameters.OutputDir); byte[] buffer = Array.Empty(); for (int idx = 0; idx < Parameters.Count; idx++) { if (idx == 0 || Parameters.Different) { buffer = RandomNumberGenerator.GetBytes(Parameters.Size); } FileReference file = FileReference.Combine(OutputDir, $"test-{Parameters.Size}-{idx}.dat"); await FileReference.WriteAllBytesAsync(file, buffer); BuildProducts.Add(file); } Logger.LogInformation("Created {NumFiles:n0} files of {Size:n0} bytes in {OutputDir} (Different={Different})", Parameters.Count, Parameters.Size, OutputDir, Parameters.Different); // Apply the optional output tag to them foreach (string TagName in FindTagNamesFromList(Parameters.Tag)) { FindOrAddTagSet(TagNameToFileSet, TagName).UnionWith(BuildProducts); } } /// /// Output this task out to an XML writer. /// public override void Write(XmlWriter Writer) { Write(Writer, Parameters); } /// /// Find all the tags which are used as inputs to this task /// /// The tag names which are read by this task public override IEnumerable FindConsumedTagNames() { yield break; } /// /// Find all the tags which are modified by this task /// /// The tag names which are modified by this task public override IEnumerable FindProducedTagNames() { return FindTagNamesFromList(Parameters.Tag); } } }