66using EpicGames . Core ;
77using UnrealBuildTool ;
88using AutomationScripts ;
9+ using Microsoft . Extensions . Logging ;
10+ using UnrealBuildBase ;
911
1012namespace Alpakit . Automation ;
1113
1214public class PackagePlugin : BuildCookRun
1315{
1416 public override void ExecuteBuild ( )
1517 {
18+ var mergeArchive = ParseParam ( "merge" ) ;
19+ Logger . LogWarning ( $ "Merge archive: { mergeArchive } ") ;
20+
1621 var projectParams = SetupParams ( ) ;
1722 projectParams . PreModifyDeploymentContextCallback = ( ProjectParams , SC ) =>
1823 {
@@ -36,6 +41,11 @@ public override void ExecuteBuild()
3641 ArchiveStagedPlugin ( projectParams , SC ) ;
3742 }
3843
44+ if ( mergeArchive )
45+ {
46+ ArchiveMergedStagedPlugin ( projectParams , deploymentContexts ) ;
47+ }
48+
3949 foreach ( var SC in deploymentContexts )
4050 {
4151 var copyToGameDirectory = ParseOptionalDirectoryReferenceParam ( $ "CopyToGameDirectory_{ SC . FinalCookPlatform } ") ;
@@ -107,26 +117,65 @@ private static void DeployStagedPlugin(ProjectParams ProjectParams, DeploymentCo
107117
108118 private static void ArchiveStagedPlugin ( ProjectParams ProjectParams , DeploymentContext SC )
109119 {
110- // Plugins will be archived under <Project>/Saved/ArchivedPlugins/<DLCName>
111- var baseArchiveDirectory = CombinePaths ( SC . ProjectRoot . FullName , "Saved" , "ArchivedPlugins" ) ;
112- var archiveDirectory = CombinePaths ( baseArchiveDirectory , ProjectParams . DLCFile . GetFileNameWithoutAnyExtensions ( ) ) ;
113-
114- CreateDirectory ( archiveDirectory ) ;
120+ var archiveDirectory = GetArchivedPluginDirectory ( ProjectParams ) ;
115121
116122 var dlcName = ProjectParams . DLCFile . GetFileNameWithoutAnyExtensions ( ) ;
117123
118124 var zipFileName = $ "{ dlcName } -{ SC . FinalCookPlatform } .zip";
119-
120- var zipFilePath = CombinePaths ( archiveDirectory , zipFileName ) ;
121-
122- if ( FileExists ( zipFilePath ) )
123- DeleteFile ( zipFilePath ) ;
124125
125126 // We only want to archive the staged files of the plugin, not the entire stage directory
126127 var stagedPluginDirectory = Project . ApplyDirectoryRemap ( SC , SC . GetStagedFileLocation ( ProjectParams . DLCFile ) ) ;
127128 var fullStagedPluginDirectory = DirectoryReference . Combine ( SC . StageDirectory , stagedPluginDirectory . Directory . Name ) ;
129+
130+ CreateZipFromDirectory ( fullStagedPluginDirectory , FileReference . Combine ( archiveDirectory , zipFileName ) ) ;
131+ }
132+
133+ private static void ArchiveMergedStagedPlugin ( ProjectParams ProjectParams , List < DeploymentContext > DeploymentContexts )
134+ {
135+ var archiveDirectory = GetArchivedPluginDirectory ( ProjectParams ) ;
136+
137+ var dlcName = ProjectParams . DLCFile . GetFileNameWithoutAnyExtensions ( ) ;
128138
129- ZipFile . CreateFromDirectory ( fullStagedPluginDirectory . FullName , zipFilePath ) ;
139+ var zipFileName = $ "{ dlcName } .zip";
140+
141+ // We only want to archive the staged files of the plugin, not the entire stage directory
142+ var mergedTempDir = DirectoryReference . Combine ( ProjectParams . DLCFile . Directory , "Saved" , "ArchiveMerging" ) ;
143+ try
144+ {
145+ if ( DirectoryReference . Exists ( mergedTempDir ) )
146+ DirectoryReference . Delete ( mergedTempDir , true ) ;
147+ DirectoryReference . CreateDirectory ( mergedTempDir ) ;
148+ foreach ( var SC in DeploymentContexts )
149+ {
150+ var stagedPluginDirectory =
151+ Project . ApplyDirectoryRemap ( SC , SC . GetStagedFileLocation ( ProjectParams . DLCFile ) ) ;
152+ var fullStagedPluginDirectory =
153+ DirectoryReference . Combine ( SC . StageDirectory , stagedPluginDirectory . Directory . Name ) ;
154+ CopyDirectory_NoExceptions ( fullStagedPluginDirectory , DirectoryReference . Combine ( mergedTempDir , SC . FinalCookPlatform ) ) ;
155+ }
156+
157+ CreateZipFromDirectory ( mergedTempDir , FileReference . Combine ( archiveDirectory , zipFileName ) ) ;
158+ }
159+ finally
160+ {
161+ DirectoryReference . Delete ( mergedTempDir , true ) ;
162+ }
163+ }
164+
165+ private static DirectoryReference GetArchivedPluginDirectory ( ProjectParams ProjectParams )
166+ {
167+ // Plugins will be archived under <Project>/Saved/ArchivedPlugins/<DLCName>
168+ return DirectoryReference . Combine ( ProjectParams . RawProjectPath . Directory , "Saved" , "ArchivedPlugins" , ProjectParams . DLCFile . GetFileNameWithoutAnyExtensions ( ) ) ;
169+ }
170+
171+ private static void CreateZipFromDirectory ( DirectoryReference SourceDirectory , FileReference DestinationFile )
172+ {
173+ CreateDirectory ( DestinationFile . Directory ) ;
174+
175+ if ( FileReference . Exists ( DestinationFile ) )
176+ FileReference . Delete ( DestinationFile ) ;
177+
178+ ZipFile . CreateFromDirectory ( SourceDirectory . FullName , DestinationFile . FullName ) ;
130179 }
131180
132181 private static string GetPluginPathRelativeToStageRoot ( ProjectParams ProjectParams )
0 commit comments