Skip to content

Commit ddc540e

Browse files
committed
Add option --preview, to make a version as a pre-release
Now also include a version-info.json in the redist with detailed info about version
1 parent fb41411 commit ddc540e

File tree

4 files changed

+126
-29
lines changed

4 files changed

+126
-29
lines changed

src/UnturnedRedistUpdateTool/NuspecHandler.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,5 @@ public void UpdateVersion(string newVersion)
2424
versionElement.Value = newVersion;
2525
}
2626

27-
public string CreateVersion(string version, string buildId)
28-
{
29-
return $"{version}-build{buildId}";
30-
}
31-
3227
public void Save() => _doc.Save(_nuspecFilePath);
3328
}

src/UnturnedRedistUpdateTool/Program.cs

Lines changed: 72 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System.Runtime.InteropServices;
2+
using System.Security.Cryptography;
3+
using System.Text;
24

35
namespace 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
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System.Text.Json;
2+
3+
namespace UnturnedRedistUpdateTool;
4+
5+
public class VersionInfo
6+
{
7+
public string GameVersion { get; set; } = "";
8+
public string BuildId { get; set; } = "";
9+
public string NugetVersion { get; set; } = "";
10+
public string FilesHash { get; set; } = "";
11+
public DateTime LastUpdated { get; set; }
12+
}
13+
14+
public class VersionTracker
15+
{
16+
private static readonly JsonSerializerOptions JsonSerializerOptions = new() { WriteIndented = true };
17+
private readonly string _versionFilePath;
18+
19+
public VersionTracker(string redistPath)
20+
{
21+
_versionFilePath = Path.Combine(redistPath, "version-info.json");
22+
}
23+
24+
public async Task<VersionInfo?> LoadAsync()
25+
{
26+
if (!File.Exists(_versionFilePath))
27+
return null;
28+
29+
try
30+
{
31+
var json = await File.ReadAllTextAsync(_versionFilePath);
32+
return JsonSerializer.Deserialize<VersionInfo>(json);
33+
}
34+
catch (Exception ex)
35+
{
36+
Console.WriteLine($"Could not load version info: {ex}");
37+
return null;
38+
}
39+
}
40+
41+
public async Task SaveAsync(VersionInfo versionInfo)
42+
{
43+
try
44+
{
45+
var json = JsonSerializer.Serialize(versionInfo, JsonSerializerOptions);
46+
await File.WriteAllTextAsync(_versionFilePath, json);
47+
Console.WriteLine($"Version info saved to: {_versionFilePath}");
48+
}
49+
catch (Exception ex)
50+
{
51+
Console.WriteLine($"Could not save version info: {ex}");
52+
}
53+
}
54+
}

tests/UnturnedRedistUpdateTool.Tests/NuspecHandlerTests.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,4 @@ public void UpdateVersion_ShouldModifyVersionElement()
4141
var reloadedHandler = new NuspecHandler(tempNuspecPath);
4242
reloadedHandler.GetVersion().ShouldBe(newVersion);
4343
}
44-
45-
[Fact]
46-
public void CreateVersion_ShouldReturnCorrectFormat()
47-
{
48-
var handler = new NuspecHandler(_realNuspecPath);
49-
var result = handler.CreateVersion("3.25.7.2", "20398");
50-
result.ShouldBe("3.25.7.2-build20398");
51-
}
5244
}

0 commit comments

Comments
 (0)