aboutsummaryrefslogtreecommitdiff
path: root/Build/Scripts/PackageSimpleUGCPlugin.cs
blob: b14c5db05bfcb5f8268438f0d85f095095123e4e (plain) (blame)
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
// Copyright Epic Games, Inc. All Rights Reserved.

using AutomationTool;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnrealBuildTool;
using Tools.DotNETCommon;

using Microsoft.Win32;
using System.Diagnostics;

namespace SimpleUGC.Automation
{
	public class PackageUGC : BuildCommand
	{
		static public ProjectParams GetParams(BuildCommand Cmd, string ProjectFileName, out FileReference PluginFile)
		{
			string VersionString = Cmd.ParseParamValue("Version", "NOVERSION");

			// Get the plugin filename
			string PluginPath = Cmd.ParseParamValue("PluginPath");
			if (PluginPath == null)
			{
				throw new AutomationException("Missing -PluginPath=... argument");
			}

			// Check it exists
			PluginFile = new FileReference(PluginPath);
			if (!FileReference.Exists(PluginFile))
			{
				throw new AutomationException("Plugin '{0}' not found", PluginFile.FullName);
			}

			string ReleaseVersion = Cmd.ParseParamValue("BasedOnReleaseVersion", "UGCExampleGame_v1");

			FileReference ProjectFile = new FileReference(ProjectFileName);

			ProjectParams Params = new ProjectParams(
				RawProjectPath: ProjectFile,
				Command: Cmd,
				ClientTargetPlatforms: new List<TargetPlatformDescriptor>(){ new TargetPlatformDescriptor(UnrealTargetPlatform.Win64) },
				Build: false,
				Cook: true,
				Stage: true,
				Pak: true,
				Manifests: true,
				DLCIncludeEngineContent: true, // Need this to allow engine content that wasn't cooked in the base game to be included in the PAK file 
				BasedOnReleaseVersion: ReleaseVersion,
				DLCName: PluginFile.GetFileNameWithoutAnyExtensions(),

				RunAssetNativization: true
				);

			Params.ValidateAndLog();
			return Params;
		}

		public override void ExecuteBuild()
		{
			int WorkingCL = -1;
			FileReference PluginFile = null;
			string ProjectFileName = ParseParamValue("Project");
			if(ProjectFileName == null)
			{
				ProjectFileName = CombinePaths(CmdEnv.LocalRoot, "SimpleGame", "SimpleGame.uproject");
			}
			LogInformation(ProjectFileName);

			ProjectParams Params = GetParams(this, ProjectFileName, out PluginFile);

			// Check whether folder already exists so we know if we can delete it later
			string PlatformStageDir = Path.Combine(Params.StageDirectoryParam, "WindowsNoEditor");
			bool bPreExistingStageDir = Directory.Exists(PlatformStageDir);

			PluginDescriptor Plugin = PluginDescriptor.FromFile(PluginFile);

			FileReference ProjectFile = new FileReference(ProjectFileName);

			// Add Plugin to folders excluded for nativization in config file
			FileReference UserEditorIni = new FileReference(Path.Combine(Path.GetDirectoryName(ProjectFileName), "Config", "UserEditor.ini"));
			bool bPreExistingUserEditorIni = FileReference.Exists(UserEditorIni);
			if (!bPreExistingUserEditorIni)
			{
				// Expect this most of the time so we will create and clean up afterwards
				DirectoryReference.CreateDirectory(UserEditorIni.Directory);
				CommandUtils.WriteAllText(UserEditorIni.FullName, "");
			}

			const string ConfigSection = "BlueprintNativizationSettings";
			const string ConfigKey = "ExcludedFolderPaths";
			string ConfigValue = "/" + PluginFile.GetFileNameWithoutAnyExtensions() + "/";

			ConfigFile UserEditorConfig = new ConfigFile(UserEditorIni);
			ConfigFileSection BPNSection = UserEditorConfig.FindOrAddSection(ConfigSection);
			bool bUpdateConfigFile = !BPNSection.Lines.Exists(x => String.Equals(x.Key, ConfigKey, StringComparison.OrdinalIgnoreCase) && String.Equals(x.Value, ConfigValue, StringComparison.OrdinalIgnoreCase));
			if (bUpdateConfigFile)
			{
				BPNSection.Lines.Add(new ConfigLine(ConfigLineAction.Add, ConfigKey, ConfigValue));
				UserEditorConfig.Write(UserEditorIni);
			}

			Project.Cook(Params);
			if (!bPreExistingUserEditorIni)
			{
				FileReference.Delete(UserEditorIni);
			}

			Project.CopyBuildToStagingDirectory(Params);
			Project.Package(Params, WorkingCL);
			Project.Archive(Params);
			Project.Deploy(Params);

			// Get path to where the plugin was staged
			string StagedPluginDir = Path.Combine(PlatformStageDir, Path.GetFileNameWithoutExtension(ProjectFileName), PluginFile.Directory.MakeRelativeTo(ProjectFile.Directory));
			string ZipFile = Path.Combine(Params.StageDirectoryParam, PluginFile.GetFileNameWithoutAnyExtensions());
			CommandUtils.DeleteFile(ZipFile);
			System.IO.Compression.ZipFile.CreateFromDirectory(StagedPluginDir, ZipFile + ".zip");

			if (!bPreExistingStageDir)
			{
				CommandUtils.DeleteDirectory(PlatformStageDir);
			}
		}
	}
}