Skip to content

Commit ae30af6

Browse files
committed
Update only specified files via new arg -update-files [Assembly-CSharp.dll,UnturnedDat.dll,etc]
1 parent aeb5fc2 commit ae30af6

File tree

3 files changed

+112
-17
lines changed

3 files changed

+112
-17
lines changed

src/UnturnedRedistUpdateTool/Program.cs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ private static async Task<int> Main(string[] args)
1616
#if DEBUG
1717
if (args.Length == 0)
1818
{
19-
args = [@"C:\Me\Apps\Steam\steamapps\common\Unturned", Path.Combine(AppContext.BaseDirectory, "TempRedist", "Client"), "304930", "--force", "-publicize", "Assembly-CSharp.dll"];
19+
args = [@"C:\Me\Apps\Steam\steamapps\common\Unturned", Path.Combine(AppContext.BaseDirectory, "TempRedist", "Client"), "304930", "--force", "-publicize", "Assembly-CSharp.dll", "-update-files", "Assembly-CSharp.dll,UnturnedDat.dll,UnityEx.dll,SystemEx.dll,SDG.NetTransport.dll,SDG.NetPak.Runtime.dll,SDG.HostBans.Runtime.dll,SDG.Glazier.Runtime.dll,com.rlabrecque.steamworks.net.dll"];
2020
}
2121
#endif
2222

@@ -30,18 +30,13 @@ private static async Task<int> Main(string[] args)
3030
var appId = args[2];
3131
var force = args.Any(x => x.Equals("--force", StringComparison.OrdinalIgnoreCase));
3232
var preview = args.Any(x => x.Equals("--preview", StringComparison.OrdinalIgnoreCase));
33-
List<string> publicizeAssemblies = [];
34-
var publicizeIndex = Array.FindIndex(args, x => x.Equals("-publicize", StringComparison.OrdinalIgnoreCase));
35-
if (publicizeIndex != -1)
33+
var publicizeAssemblies = ParseArrayArg(args, "-publicize");
34+
var updateFiles = ParseArrayArg(args, "-update-files");
35+
if (updateFiles.Count == 0)
3636
{
37-
for (var i = publicizeIndex + 1; i < args.Length; i++)
38-
{
39-
if (args[i].StartsWith("-") || args[i].StartsWith("--"))
40-
break;
41-
publicizeAssemblies.Add(args[i]);
42-
}
37+
Console.WriteLine("-update-files is not specified");
38+
return 1;
4339
}
44-
4540
if (string.IsNullOrWhiteSpace(appId))
4641
{
4742
Console.WriteLine("AppId is not specified.");
@@ -108,7 +103,7 @@ private static async Task<int> Main(string[] args)
108103

109104
Console.WriteLine($"Current Build Id: {versionInfo?.BuildId}");
110105

111-
var redistUpdater = new RedistUpdater(managedDirectory, redistPath, publicizeAssemblies);
106+
var redistUpdater = new RedistUpdater(managedDirectory, redistPath, publicizeAssemblies, updateFiles);
112107
var (updatedFiles, manifests) = await redistUpdater.UpdateAsync();
113108
if (updatedFiles.Count == 0)
114109
{
@@ -198,4 +193,21 @@ private static string CreateCombinedHash(Dictionary<string, string> manifests)
198193
}
199194
return Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(combinedData.ToString())));
200195
}
196+
197+
private static List<string> ParseArrayArg(string[] args, string argName)
198+
{
199+
var index = Array.FindIndex(args, x => x.Equals(argName, StringComparison.OrdinalIgnoreCase));
200+
if (index != -1 && index + 1 < args.Length)
201+
{
202+
var argValue = args[index + 1];
203+
if (!argValue.StartsWith("-") && !argValue.StartsWith("--"))
204+
{
205+
return argValue.Split(',', StringSplitOptions.RemoveEmptyEntries)
206+
.Select(x => x.Trim())
207+
.Where(x => !string.IsNullOrWhiteSpace(x))
208+
.ToList();
209+
}
210+
}
211+
return [];
212+
}
201213
}

src/UnturnedRedistUpdateTool/RedistUpdater.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ public class RedistUpdater
99
private readonly string _managedDir;
1010
private readonly string _redistPath;
1111
private readonly List<string> _publicizeAssemblies;
12+
private readonly List<string> _updateFiles;
1213

13-
public RedistUpdater(string managedDir, string redistPath, List<string> publicizeAssemblies)
14+
public RedistUpdater(string managedDir, string redistPath, List<string> publicizeAssemblies, List<string> updateFiles)
1415
{
1516
_managedDir = managedDir;
1617
_redistPath = redistPath;
1718
_publicizeAssemblies = publicizeAssemblies;
19+
_updateFiles = updateFiles;
1820
}
1921

2022
public async Task<(Dictionary<string, string> UpdatedFiles, Dictionary<string, string> Manifests)> UpdateAsync()
@@ -24,7 +26,24 @@ public RedistUpdater(string managedDir, string redistPath, List<string> publiciz
2426
var managedFiles = new DirectoryInfo(_managedDir).GetFiles();
2527
if (managedFiles.Length == 0)
2628
throw new InvalidOperationException($"{_managedDir} is empty");
27-
foreach (var file in managedFiles)
29+
30+
// If updateFiles is empty, process all files
31+
// If updateFiles has items, only process those files
32+
var filesToProcess = _updateFiles.Count > 0
33+
? managedFiles.Where(f => _updateFiles.Contains(f.Name)).ToArray()
34+
: managedFiles;
35+
36+
// Only validate if updateFiles has items
37+
if (_updateFiles.Count > 0)
38+
{
39+
var existingFileNames = managedFiles.Select(f => f.Name).ToHashSet();
40+
var missingFiles = _updateFiles.Where(fileName => !existingFileNames.Contains(fileName)).ToList();
41+
if (missingFiles.Count > 0)
42+
{
43+
throw new FileNotFoundException($"The following files specified in -update-files were not found in the managed directory: {string.Join(", ", missingFiles)}");
44+
}
45+
}
46+
foreach (var file in filesToProcess)
2847
{
2948
var managedFilePath = file.FullName;
3049
var redistFilePath = Path.Combine(_redistPath, file.Name);

tests/UnturnedRedistUpdateTool.Tests/RedistUpdaterTests.cs

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,20 @@ public async Task ShouldCopyOnlyChangedFilesAndGenerateManifest()
1616
Directory.CreateDirectory(source);
1717
Directory.CreateDirectory(target);
1818

19-
// Create one changed file and one unchanged file
2019
File.WriteAllText(Path.Combine(source, "Test.dll"), "hello");
2120
File.WriteAllText(Path.Combine(target, "Test.dll"), "stale");
2221

2322
File.WriteAllText(Path.Combine(source, "Unchanged.dll"), "same");
2423
File.WriteAllText(Path.Combine(target, "Unchanged.dll"), "same");
2524

26-
var updater = new RedistUpdater(source, target, []);
25+
var updater = new RedistUpdater(source, target, [], ["Test.dll"]);
2726
var (updated, manifests) = await updater.UpdateAsync();
2827

2928
updated.ShouldContainKey(Path.Combine(source, "Test.dll"));
3029
updated.ShouldNotContainKey(Path.Combine(source, "Unchanged.dll"));
3130

3231
File.ReadAllText(Path.Combine(target, "Test.dll")).ShouldBe("hello");
3332

34-
// Manifest checks
3533
manifests.ShouldContainKey("Test.dll");
3634
manifests.Count.ShouldBe(1);
3735

@@ -41,4 +39,70 @@ public async Task ShouldCopyOnlyChangedFilesAndGenerateManifest()
4139
var manifestContent = File.ReadAllText(manifestPath);
4240
manifestContent.ShouldContain("Test.dll");
4341
}
42+
43+
[Fact]
44+
public async Task ShouldOnlyUpdateSpecifiedFilesWhenUpdateFilesListProvided()
45+
{
46+
using var tempDir = new TempDir();
47+
var sourceDir = tempDir.Path;
48+
var source = Path.Combine(sourceDir, "source");
49+
var target = Path.Combine(sourceDir, "target");
50+
Directory.CreateDirectory(source);
51+
Directory.CreateDirectory(target);
52+
53+
File.WriteAllText(Path.Combine(source, "Test1.dll"), "hello1");
54+
File.WriteAllText(Path.Combine(target, "Test1.dll"), "stale1");
55+
56+
File.WriteAllText(Path.Combine(source, "Test2.dll"), "hello2");
57+
File.WriteAllText(Path.Combine(target, "Test2.dll"), "stale2");
58+
59+
File.WriteAllText(Path.Combine(source, "Test3.dll"), "hello3");
60+
File.WriteAllText(Path.Combine(target, "Test3.dll"), "stale3");
61+
62+
List<string> updateFiles = ["Test1.dll", "Test3.dll"];
63+
var updater = new RedistUpdater(source, target, [], updateFiles);
64+
var (updated, manifests) = await updater.UpdateAsync();
65+
66+
updated.ShouldContainKey(Path.Combine(source, "Test1.dll"));
67+
updated.ShouldContainKey(Path.Combine(source, "Test3.dll"));
68+
updated.ShouldNotContainKey(Path.Combine(source, "Test2.dll"));
69+
70+
File.ReadAllText(Path.Combine(target, "Test1.dll")).ShouldBe("hello1");
71+
File.ReadAllText(Path.Combine(target, "Test3.dll")).ShouldBe("hello3");
72+
File.ReadAllText(Path.Combine(target, "Test2.dll")).ShouldBe("stale2");
73+
74+
manifests.ShouldContainKey("Test1.dll");
75+
manifests.ShouldContainKey("Test3.dll");
76+
manifests.ShouldNotContainKey("Test2.dll");
77+
manifests.Count.ShouldBe(2);
78+
79+
var manifestPath = Path.Combine(target, "manifest.sha256.json");
80+
File.Exists(manifestPath).ShouldBeTrue();
81+
82+
var manifestContent = File.ReadAllText(manifestPath);
83+
manifestContent.ShouldContain("Test1.dll");
84+
manifestContent.ShouldContain("Test3.dll");
85+
manifestContent.ShouldNotContain("Test2.dll");
86+
}
87+
88+
[Fact]
89+
public async Task ShouldThrowExceptionWhenSpecifiedFileDoesNotExist()
90+
{
91+
using var tempDir = new TempDir();
92+
var sourceDir = tempDir.Path;
93+
var source = Path.Combine(sourceDir, "source");
94+
var target = Path.Combine(sourceDir, "target");
95+
Directory.CreateDirectory(source);
96+
Directory.CreateDirectory(target);
97+
98+
File.WriteAllText(Path.Combine(source, "Test1.dll"), "hello1");
99+
File.WriteAllText(Path.Combine(target, "Test1.dll"), "stale1");
100+
101+
List<string> updateFiles = ["Test1.dll", "NonExistent.dll"];
102+
var updater = new RedistUpdater(source, target, [], updateFiles);
103+
104+
var exception = await Assert.ThrowsAsync<FileNotFoundException>(() => updater.UpdateAsync());
105+
exception.Message.ShouldContain("NonExistent.dll");
106+
exception.Message.ShouldContain("-update-files");
107+
}
44108
}

0 commit comments

Comments
 (0)