11using System . Runtime . InteropServices ;
2+ using System . Security . Cryptography ;
3+ using System . Text ;
24
35namespace UnturnedRedistUpdateTool ;
46
@@ -27,6 +29,7 @@ private static async Task<int> Main(string[] args)
2729 var redistPath = args [ 1 ] ;
2830 var appId = args [ 2 ] ;
2931 var force = args . Any ( x => x . Equals ( "--force" , StringComparison . OrdinalIgnoreCase ) ) ;
32+ var preview = args . Any ( x => x . Equals ( "--preview" , StringComparison . OrdinalIgnoreCase ) ) ;
3033
3134 if ( string . IsNullOrWhiteSpace ( appId ) )
3235 {
@@ -66,19 +69,19 @@ private static async Task<int> Main(string[] args)
6669 Console . WriteLine ( $ "Unturned Managed Directory not found: \" { managedDirectory } \" ") ;
6770 return 1 ;
6871 }
69- var ( newVersion , buildId ) = await GameInfoParser . ParseAsync ( unturnedPath , appManifestPath ) ;
72+ var ( newVersion , newBuildId ) = await GameInfoParser . ParseAsync ( unturnedPath , appManifestPath ) ;
7073 if ( string . IsNullOrWhiteSpace ( newVersion ) )
7174 {
7275 Console . WriteLine ( "New Game Version is not found" ) ;
7376 return 1 ;
7477 }
75- if ( string . IsNullOrWhiteSpace ( buildId ) )
78+ if ( string . IsNullOrWhiteSpace ( newBuildId ) )
7679 {
7780 Console . WriteLine ( "New Game BuildId is not found" ) ;
7881 return 1 ;
7982 }
8083
81- Console . WriteLine ( $ "Found Unturned v{ newVersion } ({ buildId } )") ;
84+ Console . WriteLine ( $ "Found Unturned v{ newVersion } ({ newBuildId } )") ;
8285
8386 var nuspecHandler = new NuspecHandler ( nuspecFilePath ) ;
8487 var currentNuspecVersion = nuspecHandler . GetVersion ( ) ;
@@ -87,32 +90,55 @@ private static async Task<int> Main(string[] args)
8790 Console . WriteLine ( "Version element not found in nuspec file!" ) ;
8891 return 1 ;
8992 }
90- var newVersionWithBuildId = nuspecHandler . CreateVersion ( newVersion , buildId ) ;
9193 Console . WriteLine ( $ "Current nuspec version: { currentNuspecVersion } ") ;
92- Console . WriteLine ( $ "New Version & Build Id: { newVersionWithBuildId } ") ;
93- if ( newVersionWithBuildId == currentNuspecVersion )
94- {
95- Console . WriteLine ( "Unturned Version is the same as in nuspec, it means new version is not detected, skipping..." ) ;
96- return 1 ;
97- }
98- nuspecHandler . UpdateVersion ( newVersionWithBuildId ) ;
99- nuspecHandler . Save ( ) ;
94+
95+ var versionTracker = new VersionTracker ( redistPath ) ;
96+ var versionInfo = await versionTracker . LoadAsync ( ) ;
10097
10198 var redistUpdater = new RedistUpdater ( managedDirectory , redistPath ) ;
10299 var updatedFiles = await redistUpdater . UpdateAsync ( ) ;
103100 if ( updatedFiles . Count == 0 )
104101 {
105- Console . WriteLine ( "No one file were updated, perhaps something went wrong." ) ;
102+ Console . WriteLine ( "No files were updated - either no changes or something went wrong." ) ;
103+ if ( versionInfo ? . BuildId == newBuildId )
104+ {
105+ Console . WriteLine ( "Build ID is the same, no update needed." ) ;
106+ return 0 ;
107+ }
108+ Console . WriteLine ( "Build ID changed but no files updated - this might be an issue." ) ;
106109 return 1 ;
107110 }
111+ Console . WriteLine ( $ "{ updatedFiles . Count } Unturned's file(s) were updated") ;
112+ var combinedHash = CreateCombinedHash ( updatedFiles ) ;
113+ Console . WriteLine ( $ "Combined hash of updated files: { combinedHash } ") ;
114+ var versionToUse = DetermineVersionToUse ( newVersion , newBuildId , currentNuspecVersion , versionInfo , combinedHash , preview ) ;
115+ Console . WriteLine ( $ "New Version: { newVersion } ") ;
116+ Console . WriteLine ( $ "New Build Id: { newBuildId } ") ;
117+ Console . WriteLine ( $ "Version to use: { versionToUse } ") ;
118+ if ( versionToUse == currentNuspecVersion )
119+ {
120+ Console . WriteLine ( "Files haven't changed since last publish, skipping..." ) ;
121+ return 0 ;
122+ }
123+ await versionTracker . SaveAsync ( new VersionInfo
124+ {
125+ GameVersion = newVersion ,
126+ BuildId = newBuildId ,
127+ NugetVersion = versionToUse ,
128+ FilesHash = combinedHash ,
129+ LastUpdated = DateTime . UtcNow
130+ } ) ;
131+
132+ nuspecHandler . UpdateVersion ( versionToUse ) ;
133+ nuspecHandler . Save ( ) ;
108134
109135 Console . WriteLine ( $ "Updated { updatedFiles . Count } File(s)") ;
110- foreach ( var ( fromPath , toPath ) in updatedFiles )
136+ foreach ( var ( filePath , sha256 ) in updatedFiles )
111137 {
112- Console . WriteLine ( $ "Updated File \" { fromPath } \" -> \" { toPath } \" ") ;
138+ Console . WriteLine ( $ "Updated File \" { filePath } \" (SHA256: { sha256 [ .. 8 ] } ...) ") ;
113139 }
114140
115- await new CommitFileWriter ( ) . WriteAsync ( unturnedPath , newVersion , buildId , force ) ;
141+ await new CommitFileWriter ( ) . WriteAsync ( unturnedPath , versionToUse , newBuildId , force ) ;
116142
117143 return 0 ;
118144
@@ -147,4 +173,34 @@ string GetUnturnedDataDirectoryName()
147173 throw new DirectoryNotFoundException ( $ "Unturned Data directory cannot be found in { unturnedPath } ") ;
148174 }
149175 }
176+
177+ private static string CreateCombinedHash ( Dictionary < string , string > updatedFiles )
178+ {
179+ var sortedFiles = updatedFiles . OrderBy ( kvp => kvp . Key ) . ToList ( ) ;
180+ var combinedData = new StringBuilder ( ) ;
181+ foreach ( var ( filePath , fileHash ) in sortedFiles )
182+ {
183+ combinedData . Append ( $ "{ Path . GetFileName ( filePath ) } :{ fileHash } ") ;
184+ }
185+ return Convert . ToHexString ( SHA256 . HashData ( Encoding . UTF8 . GetBytes ( combinedData . ToString ( ) ) ) ) ;
186+ }
187+
188+ private static string DetermineVersionToUse ( string gameVersion , string buildId , string currentNuspecVersion ,
189+ VersionInfo ? versionInfo , string newFilesHash , bool preview )
190+ {
191+ if ( versionInfo ? . FilesHash == newFilesHash )
192+ {
193+ Console . WriteLine ( "Files haven't changed, keeping current version" ) ;
194+ return currentNuspecVersion ;
195+ }
196+ if ( versionInfo ? . GameVersion != gameVersion )
197+ {
198+ Console . WriteLine ( "Game version changed, using new game version" ) ;
199+ return gameVersion ;
200+ }
201+ Console . WriteLine ( "Same game version but files changed" ) ;
202+ return preview
203+ ? $ "{ gameVersion } -preview{ buildId } "
204+ : gameVersion ;
205+ }
150206}
0 commit comments