File size: 7,150 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
// Copyright Epic Games, Inc.All Rights Reserved.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using AutomationTool;
using EpicGame;
using EpicGames.Core;
using Microsoft.Extensions.Logging;

using static AutomationTool.CommandUtils;

namespace LyraTest
{
	[Help("Updates the Audit_InCook collections.")]
	[RequireP4]
	public class Lyra_UpdateAuditCollections : BuildCommand
	{
		public override void ExecuteBuild()
		{
			Logger.LogInformation("************************* UpdateAuditCollections");

			// Now update what is in the InCook audit collection
			string CollectionName = "Audit_InCook";
			string ManifestFilename = "Manifest_UFSFiles_Win64.txt";

			// Attempt to find the UFS file list. Default to the log folder
			string UFSFilename = CommandUtils.CombinePaths(CommandUtils.CmdEnv.LogFolder, ManifestFilename);
			if (!CommandUtils.FileExists_NoExceptions(UFSFilename))
			{
				UFSFilename = CommandUtils.CombinePaths(CommandUtils.CmdEnv.LogFolder, "..", "..", ManifestFilename);
			}

			Logger.LogInformation("Attempting to use UFS manifest {UFSFilename} (exists={Arg1})", UFSFilename, CommandUtils.FileExists_NoExceptions(UFSFilename));

			UpdateInCookAuditCollection(CollectionName, UFSFilename);
		}


		static string AssetExtension = ".uasset";
		static string MapExtention = ".umap";

		static string EngineFolderName = "Engine/Content";
		static string EnginePluginFolderName = "Engine/Plugins/";

		//@TODO: Should derive these, otherwise it will break as soon as someone clones the projects
		static string GameFolderName = "Lyra/Content";
		static string GamePluginFolderName = "Lyra/Plugins/";
		static string GameProjectDirectory = "Samples/Games/Lyra";

		static public void UpdateInCookAuditCollection(string CollectionName, string UFSFilename)
		{
			if (!CommandUtils.FileExists_NoExceptions(UFSFilename))
			{
				Logger.LogWarning("{Text}", "Could not update audit collection, missing file: " + UFSFilename);
				return;
			}

			int WorkingCL = -1;
			if (CommandUtils.P4Enabled)
			{
				WorkingCL = CommandUtils.P4.CreateChange(CommandUtils.P4Env.Client, String.Format("Updated " + CollectionName + " collection using CL {0}\n#skipci\n#okforgithub public", P4Env.Changelist));
			}

			var CollectionFilenameLocal = CommandUtils.CombinePaths(CommandUtils.CmdEnv.LocalRoot, GameProjectDirectory, "Content", "Collections", CollectionName + ".collection");
			if (!InternalUtils.SafeCreateDirectory(Path.GetDirectoryName(CollectionFilenameLocal)))
			{
				Logger.LogWarning("Could not create directory {Arg0}", Path.GetDirectoryName(CollectionFilenameLocal));
				return;
			}

			bool bNeedsAdd = false;
			if (WorkingCL > 0)
			{
				
				if (!CommandUtils.FileExists_NoExceptions(CollectionFilenameLocal))
				{
					bNeedsAdd = true;
				}
				else
				{
					CommandUtils.P4.Edit(WorkingCL, CollectionFilenameLocal);
				}
			}


			StreamReader ManifestFile = null;
			StreamWriter CollectionFile = null;
			try
			{
				CollectionFile = new StreamWriter(CollectionFilenameLocal);
				CollectionFile.WriteLine("FileVersion:1");
				CollectionFile.WriteLine("Type:Static");
				CollectionFile.WriteLine("");

				string Line = "";
				ManifestFile = new StreamReader(UFSFilename);
				while ((Line = ManifestFile.ReadLine()) != null)
				{
					string[] Tokens = Line.Split('\t');
					if (Tokens.Length > 1)
					{
						string UFSPath = Tokens[0];
						UFSPath = UFSPath.Trim('\"');
						bool bIsAsset = UFSPath.EndsWith(AssetExtension, StringComparison.InvariantCultureIgnoreCase);
						bool bIsMap = !bIsAsset && UFSPath.EndsWith(MapExtention, StringComparison.InvariantCultureIgnoreCase);
						if (bIsAsset || bIsMap)
						{
							bool bIsGame = UFSPath.StartsWith(GameFolderName);
							bool bIsEngine = UFSPath.StartsWith(EngineFolderName);
							bool bIsGamePlugin = UFSPath.StartsWith(GamePluginFolderName);
							bool bIsEnginePlugin = UFSPath.StartsWith(EnginePluginFolderName);
							if (bIsGame || bIsEngine || bIsGamePlugin || bIsEnginePlugin)
							{
								string ObjectPath = UFSPath;

								bool bValidPath = true;
								if (bIsGame)
								{
									ObjectPath = "/Game" + ObjectPath.Substring(GameFolderName.Length);
								}
								else if (bIsEngine)
								{
									ObjectPath = "/Engine" + ObjectPath.Substring(EngineFolderName.Length);
								}
								else if (bIsGamePlugin || bIsEnginePlugin)
								{
									int ContentIdx = ObjectPath.IndexOf("/Content/");
									if (ContentIdx != -1)
									{
										int PluginIdx = ObjectPath.LastIndexOf("/", ContentIdx - 1);
										if (PluginIdx == -1)
										{
											PluginIdx = 0;
										}
										else
										{
											// Skip the leading "/"
											PluginIdx++;
										}

										DirectoryReference PluginRoot = new DirectoryReference(ObjectPath.Substring(0, ContentIdx));
										string PluginName = "";
										foreach (FileReference PluginFile in CommandUtils.FindFiles("*.uplugin", false, PluginRoot))
										{
											PluginName = PluginFile.GetFileNameWithoutAnyExtensions();
											break;
										}
										if (string.IsNullOrEmpty(PluginName))
										{
											// Fallback to the directory name if the .uplugin file doesn't exist
											PluginName = ObjectPath.Substring(PluginIdx, ContentIdx - PluginIdx);
										}
										if (PluginName.Length > 0)
										{
											int PathStartIdx = ContentIdx + "/Content/".Length;
											ObjectPath = "/" + PluginName + "/" + ObjectPath.Substring(PathStartIdx);
										}
										else
										{
											Logger.LogWarning("{Text}", "Could not add asset to collection. No plugin name. Path:" + UFSPath);
											bValidPath = false;
										}
									}
									else
									{
										Logger.LogWarning("{Text}", "Could not add asset to collection. No content folder. Path:" + UFSPath);
										bValidPath = false;
									}
								}

								if (bValidPath)
								{
									string ObjectName = Path.GetFileNameWithoutExtension(ObjectPath);
									ObjectPath = Path.GetDirectoryName(ObjectPath) + "/" + ObjectName + "." + ObjectName;

									ObjectPath = ObjectPath.Replace('\\', '/');

									CollectionFile.WriteLine(ObjectPath);
								}
							}
						}
					}
				}

			}
			catch (Exception Ex)
			{
				Logger.LogInformation("Did not update InCook collection. {Arg0}", Ex.Message);
			}
			finally
			{
				if (ManifestFile != null)
				{
					ManifestFile.Close();
				}

				if (CollectionFile != null)
				{
					CollectionFile.Close();
					if (bNeedsAdd)
					{
						CommandUtils.P4.Add(WorkingCL, CollectionFilenameLocal);
					}
				}
			}

			if (WorkingCL > 0)
			{
				// Check in the collection
				int SubmittedCL;
				CommandUtils.P4.Submit(WorkingCL, out SubmittedCL, true, true);
			}

		}
	}
}