77using System . Text . RegularExpressions ;
88using Microsoft . Build . Framework ;
99using Microsoft . Build . Utilities ;
10- using Newtonsoft . Json ;
1110using StardewModdingAPI . ModBuildConfig . Framework ;
12- using StardewModdingAPI . Toolkit . Framework ;
13- using StardewModdingAPI . Toolkit . Serialization ;
14- using StardewModdingAPI . Toolkit . Serialization . Models ;
1511using StardewModdingAPI . Toolkit . Utilities ;
1612
1713namespace StardewModdingAPI . ModBuildConfig ;
@@ -34,6 +30,10 @@ public class DeployModTask : Task
3430 [ Required ]
3531 public string ModZipPath { get ; set ; }
3632
33+ /// <summary>The version number for the project assembly.</summary>
34+ [ Required ]
35+ public string ProjectVersion { get ; set ; }
36+
3737 /// <summary>The folder containing the project files.</summary>
3838 [ Required ]
3939 public string ProjectDir { get ; set ; }
@@ -87,33 +87,12 @@ public override bool Execute()
8787 if ( ! this . EnableModDeploy && ! this . EnableModZip )
8888 return true ;
8989
90- // validate the manifest file
91- IManifest manifest ;
90+ // read & validate manifest
91+ string manifestPath = Path . Combine ( this . ProjectDir , BundleFile . ManifestFileName ) ;
92+ if ( ! ManifestHelper . TryLoadManifest ( manifestPath , this . ProjectVersion , out IManifest manifest , out string overrideManifestJson , out string error ) )
9293 {
93- try
94- {
95- string manifestPath = Path . Combine ( this . ProjectDir , "manifest.json" ) ;
96- if ( ! new JsonHelper ( ) . ReadJsonFileIfExists ( manifestPath , out Manifest rawManifest ) )
97- {
98- this . Log . LogError ( "[mod build package] The mod's manifest.json file doesn't exist." ) ;
99- return false ;
100- }
101- manifest = rawManifest ;
102- }
103- catch ( JsonReaderException ex )
104- {
105- // log the inner exception, otherwise the message will be generic
106- Exception exToShow = ex . InnerException ?? ex ;
107- this . Log . LogError ( $ "[mod build package] The mod's manifest.json file isn't valid JSON: { exToShow . Message } ") ;
108- return false ;
109- }
110-
111- // validate manifest fields
112- if ( ! ManifestValidator . TryValidateFields ( manifest , out string error ) )
113- {
114- this . Log . LogError ( $ "[mod build package] The mod's manifest.json file is invalid: { error } ") ;
115- return false ;
116- }
94+ this . Log . LogError ( $ "[mod build package] The mod's { BundleFile . ManifestFileName } is invalid: { error } ") ;
95+ return false ;
11796 }
11897
11998 // deploy files
@@ -128,7 +107,7 @@ public override bool Execute()
128107
129108 var modPackages = new Dictionary < string , IModFileManager >
130109 {
131- [ this . ModFolderName ] = new MainModFileManager ( this . ProjectDir , this . TargetDir , ignoreFilePaths , ignoreFilePatterns , bundleAssemblyTypes , this . ModDllName , validateRequiredModFiles : this . EnableModDeploy || this . EnableModZip )
110+ [ this . ModFolderName ] = new MainModFileManager ( this . ProjectDir , this . TargetDir , ignoreFilePaths , ignoreFilePatterns , bundleAssemblyTypes , this . ModDllName , overrideManifestJson , validateRequiredModFiles : this . EnableModDeploy || this . EnableModZip )
132111 } ;
133112
134113 if ( this . ContentPacks != null )
@@ -158,7 +137,7 @@ public override bool Execute()
158137 this . Log . LogMessage ( MessageImportance . High , $ "[mod build package] Bundling content pack: { folderName } v{ version } at { contentPath } .") ;
159138 modPackages . Add (
160139 folderName ,
161- new ContentPackFileManager ( this . ProjectDir , contentPath , version , ignoreFilePaths , ignoreFilePatterns , validateManifest )
140+ new ContentPackFileManager ( this . ProjectDir , contentPath , this . ProjectVersion , version , ignoreFilePaths , ignoreFilePatterns , validateManifest )
162141 ) ;
163142 }
164143 }
@@ -299,21 +278,17 @@ private IEnumerable<string> GetCustomIgnoreFilePaths(string pattern)
299278 /// <param name="outputPath">The folder path to create with the mod files.</param>
300279 private void CreateModFolder ( IDictionary < string , IModFileManager > modPackages , string outputPath )
301280 {
302- foreach ( var mod in modPackages )
281+ foreach ( var pair in modPackages )
303282 {
304- string relativePath = modPackages . Count == 1
305- ? outputPath
306- : Path . Combine ( outputPath , this . EscapeInvalidFilenameCharacters ( mod . Key ) ) ;
307-
308- foreach ( var file in mod . Value . GetFiles ( ) )
309- {
310- string fromPath = file . Value . FullName ;
311- string toPath = Path . Combine ( relativePath , file . Key ) ;
283+ string folderName = pair . Key ;
284+ IModFileManager fileManager = pair . Value ;
312285
313- Directory . CreateDirectory ( Path . GetDirectoryName ( toPath ) ! ) ;
286+ string folderPath = modPackages . Count == 1
287+ ? outputPath
288+ : Path . Combine ( outputPath , this . EscapeInvalidFilenameCharacters ( folderName ) ) ;
314289
315- File . Copy ( fromPath , toPath , overwrite : true ) ;
316- }
290+ foreach ( BundleFile from in fileManager . GetFiles ( ) )
291+ from . CopyToFolder ( folderPath ) ;
317292 }
318293 }
319294
@@ -330,21 +305,19 @@ private void CreateReleaseZip(IDictionary<string, IModFileManager> modPackages,
330305 foreach ( var mod in modPackages )
331306 {
332307 string modFolder = this . EscapeInvalidFilenameCharacters ( mod . Key ) ;
333- foreach ( var file in mod . Value . GetFiles ( ) )
308+ foreach ( BundleFile from in mod . Value . GetFiles ( ) )
334309 {
335- string relativePath = file . Key ;
310+ string relativePath = from . RelativePath ;
311+
336312 if ( relativePath . Contains ( '\\ ' ) )
337313 relativePath = string . Join ( "/" , PathUtilities . GetSegments ( relativePath ) ) ; // zip files use forward slashes regardless of OS
338314
339- FileInfo fileInfo = file . Value ;
340-
341315 string archivePath = modPackages . Count == 1
342316 ? $ "{ modFolder } /{ relativePath } "
343317 : $ "{ this . ModFolderName } /{ modFolder } /{ relativePath } ";
344318
345- using Stream fileStream = new FileStream ( fileInfo . FullName , FileMode . Open , FileAccess . Read ) ;
346319 using Stream fileStreamInZip = archive . CreateEntry ( archivePath ) . Open ( ) ;
347- fileStream . CopyTo ( fileStreamInZip ) ;
320+ from . CopyToStream ( fileStreamInZip ) ;
348321 }
349322 }
350323 }
0 commit comments