Skip to content

Commit 993d61a

Browse files
committed
Update Program.cs
1 parent 33ce212 commit 993d61a

File tree

1 file changed

+58
-8
lines changed

1 file changed

+58
-8
lines changed

UnturnedRedistUpdateTool/Program.cs

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Diagnostics;
22
using System.IO.Compression;
33
using System.Runtime.InteropServices;
4+
using System.Security.Cryptography;
45
using System.Text;
56
using System.Text.Json.Nodes;
67
using System.Xml.Linq;
@@ -105,7 +106,18 @@ public static async Task<int> Main(string[] args)
105106

106107
doc.Save(nuspecFilePath);
107108

108-
UpdateRedist(managedDirectory);
109+
var updatedFiles = UpdateRedist(managedDirectory);
110+
if (updatedFiles.Count == 0)
111+
{
112+
Console.WriteLine($"No one file were updated, perhaps something went wrong.");
113+
return 1;
114+
}
115+
116+
Console.WriteLine($"Updated {updatedFiles.Count} File(s)");
117+
foreach (var (fromPath, toPath) in updatedFiles)
118+
{
119+
Console.WriteLine("Updated");
120+
}
109121

110122
var forcedNote = Force ? " [Forced]" : "";
111123

@@ -121,24 +133,43 @@ void AssertPlatformSupported()
121133
throw new PlatformNotSupportedException();
122134
}
123135
}
124-
void UpdateRedist(string unturnedManagedDirectory)
136+
Dictionary<string, string> UpdateRedist(string unturnedManagedDirectory)
125137
{
126138
var managedFiles = new DirectoryInfo(unturnedManagedDirectory).GetFiles();
127139
if (managedFiles.Length == 0)
128140
{
129-
throw new InvalidOperationException($"{nameof(managedFiles)} was empty");
141+
throw new InvalidOperationException($"{unturnedManagedDirectory} directory was empty");
130142
}
131143

144+
var updatedFiles = new Dictionary<string, string>();
132145
foreach (var fileInfo in managedFiles)
133146
{
134-
var redistFilePath = Path.Combine(redistPath, fileInfo.Name);
135-
if (File.Exists(redistFilePath) == false)
147+
try
136148
{
137-
continue;
149+
var managedFilePath = fileInfo.FullName;
150+
var redistFilePath = Path.Combine(redistPath, fileInfo.Name);
151+
if (File.Exists(redistFilePath) == false)
152+
{
153+
continue;
154+
}
155+
var managedFileData = File.ReadAllBytes(managedFilePath);
156+
var redistFileData = File.ReadAllBytes(redistFilePath);
157+
if (HashHelper.IsSameHashes(managedFileData, redistFileData))
158+
{
159+
continue;
160+
}
161+
162+
fileInfo.CopyTo(redistFilePath, true);
163+
updatedFiles.Add(managedFilePath, redistFilePath);
164+
}
165+
catch
166+
{
167+
Console.WriteLine($"An error occured while updating file: \"{fileInfo.FullName}\".");
168+
throw;
138169
}
139-
140-
fileInfo.CopyTo(redistFilePath, true);
141170
}
171+
172+
return updatedFiles;
142173
}
143174
}
144175

@@ -175,4 +206,23 @@ private static string GetUnturnedDataDirectoryName(string unturnedPath)
175206
var buildId1 = obj["buildid"].ToString();
176207
return (version, buildId1);
177208
}
209+
}
210+
211+
internal static class HashHelper
212+
{
213+
public static string GetHashFromArray(byte[] data)
214+
{
215+
using var sha = SHA256.Create();
216+
using var input = new MemoryStream(data);
217+
var output = sha.ComputeHash(input);
218+
const string minusSymbol = "-";
219+
return BitConverter
220+
.ToString(output)
221+
.Replace(minusSymbol, string.Empty)
222+
.ToLowerInvariant();
223+
}
224+
public static bool IsSameHashes(byte[] managedFileData, byte[] redistFileData)
225+
{
226+
return GetHashFromArray(managedFileData) == GetHashFromArray(redistFileData);
227+
}
178228
}

0 commit comments

Comments
 (0)