File size: 8,061 Bytes
de0d85f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright Epic Games, Inc.All Rights Reserved.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using AutomationTool;
using FileLockInfo;
using Microsoft.Extensions.Logging;

using static AutomationTool.CommandUtils;

#pragma warning disable SYSLIB0014

namespace LyraDeployment
{
	// Documentation
	// https://dev.epicgames.com/docs/services/en-US/EpicGamesStore/TechFeaturesConfig/BPTInstructionsSPT/index.html

	public class DeployToEpicGameStore : BuildCommand
	{
		static string DownloadBuildPackageTool()
		{
			// Download build package tool 1.5
			string BuildPackageToolUrl = "https://launcher-public-service-prod.ol.epicgames.com/launcher/api/installer/download/BuildPatchTool.zip";

			var BuildPathToolDownloadPath = CommandUtils.CombinePaths(CommandUtils.CmdEnv.LocalRoot, "BuildPatchTool.zip");
			var BuildPathToolInstallPath = CommandUtils.CombinePaths(CommandUtils.CmdEnv.LocalRoot, "BPT");
			var BuildPathToolPath = CommandUtils.CombinePaths(BuildPathToolInstallPath, "Engine/Binaries/Win64/BuildPatchTool.exe");

			if (Directory.Exists(BuildPathToolInstallPath))
			{
				Directory.Delete(BuildPathToolInstallPath, true);
			}

			using (var client = new WebClient())
			{
				client.DownloadFile(BuildPackageToolUrl, BuildPathToolDownloadPath);
			}

			// Decompress the zip file to the target directory
			Logger.LogInformation("Decompressing to {BuildPathToolInstallPath}...", BuildPathToolInstallPath);
			CommandUtils.LegacyUnzipFiles(BuildPathToolDownloadPath, BuildPathToolInstallPath);
			CommandUtils.DeleteFile(BuildPathToolDownloadPath);

			// Make sure the file we need exists.
			CommandUtils.FileExists(false, BuildPathToolPath);

			return BuildPathToolPath;
		}

		public DeployToEpicGameStore()
		{
		}

		public override void ExecuteBuild()
		{
			Logger.LogInformation("*************************");

			var LogInstanceId = Guid.NewGuid().ToString("N");
			var LogFileDirectory = CommandUtils.CombinePaths(CommandUtils.CmdEnv.LocalRoot, "Saved", "Logs", "BuildPatchToolLog");

			var BuildPathToolPath = DownloadBuildPackageTool();

			string ArtifactId = ParseParamValue("ArtifactId", null);
			string BuildRoot = ParseParamValue("BuildRoot", null);
			string CloudDir = ParseParamValue("CloudDir", null);
			string BuildVersion = ParseParamValue("BuildVersion", null);
			string AppLaunch = ParseParamValue("AppLaunch", null);
			string AppArgs = ParseParamValue("AppArgs", null);
			string FileAttributeList = ParseParamValue("FileAttributeList", null);
			string FileIgnoreList = ParseParamValue("FileIgnoreList", null);
			string Platform = ParseParamValue("Platform", null);
			string Label = ParseParamValue("Label", null);
			string CommandLineFile = ParseParamValue("CommandLineFile", null);

			if (Directory.Exists(CloudDir))
			{
				Directory.Delete(CloudDir, true);
			}

			// Verify nothing is locked before moving on.
			{
				Logger.LogInformation("Are Build Files Are Unlocked: Starting");

				string[] FilesInBuildRoot = Directory.GetFiles(BuildRoot);

				// We need to make sure nothing is locking the build files.
				Stopwatch stopwatch = new Stopwatch();
				stopwatch.Start();
				while (stopwatch.Elapsed.TotalSeconds < 30)
				{
					List<Process> LockingProcesses = Win32Processes.GetProcessesLockingFiles(FilesInBuildRoot);
					if (LockingProcesses.Count > 0)
					{
						string LockingProcessesString = string.Join(", ", LockingProcesses.Select(c => c.ProcessName));
						Logger.LogInformation("The following processes are locking files, {LockingProcessesString}", LockingProcessesString);
						Thread.Sleep(2000);
					}
				}
				stopwatch.Stop();

				Logger.LogInformation("Are Build Files Are Unlocked: Done");
			}

			Logger.LogInformation("Running BuildPatchTool Patch Generation on {ArtifactId}", ArtifactId);
			{
				string Args = "";
				Args += Arg("mode", "PatchGeneration");
				Args += Arg("ArtifactId", ArtifactId);
				Args += Arg("BuildRoot", Path.GetFullPath(BuildRoot));
				Args += Arg("CloudDir", Path.GetFullPath(CloudDir));
				Args += Arg("BuildVersion", BuildVersion);
				Args += Arg("AppLaunch", AppLaunch);
				Args += Arg("AppArgs", AppArgs);
				Args += Arg("FileAttributeList", FileAttributeList);
				Args += Arg("FileIgnoreList", FileIgnoreList);

				if (!String.IsNullOrEmpty(CommandLineFile))
				{
					Args += " " + File.ReadAllText(Path.GetFullPath(CommandLineFile));
				}

				// Add -stdout so that Horde gets logging.
				Args += Arg("stdout");

				//string LogFileName = String.Concat("BuildPatchTool-PatchGeneration-", LogInstanceId);
				//string LogFilePath = Path.Combine(LogFileDirectory, String.Concat(LogFileName, ".stdout.log"));

				try
				{
					CommandUtils.RunAndLog(BuildPathToolPath, Args);
				}
				catch (AutomationException Ex)
				{
					Logger.LogError("{Text}", Ex.ToString());

					//if (File.Exists(LogFilePath))
					//{
					//	CommandUtils.LogInformation(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
					//	CommandUtils.LogInformation(File.ReadAllText(LogFilePath));
					//	CommandUtils.LogInformation(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
					//}

					throw;
				}
			}
			Logger.LogInformation("BuildPatchTool Patch Generation Done!");

			Logger.LogInformation("Running BuildPatchTool LabelBuild on {ArtifactId}", ArtifactId);
			{
				string Args = "";
				Args += Arg("mode", "LabelBuild");
				Args += Arg("ArtifactId", ArtifactId);
				Args += Arg("BuildRoot", Path.GetFullPath(BuildRoot));
				Args += Arg("CloudDir", Path.GetFullPath(CloudDir));
				Args += Arg("BuildVersion", BuildVersion);
				Args += Arg("Platform", Platform);
				Args += Arg("Label", Label);

				if (!String.IsNullOrEmpty(CommandLineFile))
				{
					Args += " " + File.ReadAllText(Path.GetFullPath(CommandLineFile));
				}

				// Add -stdout so that Horde gets logging.
				Args += Arg("stdout");

				//string LogFileName = String.Concat("BuildPatchTool-LabelBuild-", LogInstanceId);
				//string LogFilePath = Path.Combine(LogFileDirectory, String.Concat(LogFileName, ".stdout.log"));

				try
				{
					CommandUtils.RunAndLog(BuildPathToolPath, Args/*, LogFilePath*/);
				}
				catch (AutomationException Ex)
				{
					Logger.LogError("{Text}", Ex.ToString());

					//if (File.Exists(LogFilePath))
					//{
					//	CommandUtils.LogInformation(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
					//	CommandUtils.LogInformation(File.ReadAllText(LogFilePath));
					//	CommandUtils.LogInformation(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
					//}

					throw;
				}
			}
			Logger.LogInformation("BuildPatchTool Patch Generation Done!");


			Logger.LogInformation("*************************");
		}

		private static string Arg(string Key, string Value)
		{
			return Value == null ? "" : " -" + Key + "=\"" + Value + "\"";
		}
		private static string Arg(string Key, int Value)
		{
			return " -" + Key + "=" + Value;
		}
		private static string Arg(string Key, ulong Value)
		{
			return " -" + Key + "=" + Value;
		}
		private static string Arg(string Key)
		{
			return " -" + Key;
		}

		private static string Arg(string Key, IEnumerable<string> Value)
		{
			return " -" + Key + "=\"" + string.Join(",", Value) + "\"";
		}
		private static string CustomArgs<T>(string ArgName, List<KeyValuePair<string, T>> CustomArgs)
		{
			var Result = new StringBuilder();
			if (CustomArgs != null)
			{
				foreach (var CustomArg in CustomArgs)
				{
					Result.Append(string.Format(" -{0}=\"{1}={2}\"", ArgName, CustomArg.Key, CustomArg.Value.ToString()));
				}
			}
			return Result.ToString();
		}
	}
}