121 lines
4.2 KiB
C#
121 lines
4.2 KiB
C#
/*
|
|
* Copyright (c) 2022 Winsider Seminars & Solutions, Inc. All rights reserved.
|
|
*
|
|
* This file is part of System Informer.
|
|
*
|
|
* Authors:
|
|
*
|
|
* jxy-s
|
|
*
|
|
*/
|
|
|
|
namespace CustomBuildTool
|
|
{
|
|
/// <summary>
|
|
/// Provides methods for managing and verifying the build tools identity hash.
|
|
/// </summary>
|
|
public static class BuildToolsId
|
|
{
|
|
/// <summary>
|
|
/// Checks if the build tools are out of date by comparing the current tools hash
|
|
/// with the previously stored hash in the <c>ToolsId.txt</c> file.
|
|
/// Prints a warning message if the hashes do not match.
|
|
/// </summary>
|
|
public static void CheckForOutOfDateTools()
|
|
{
|
|
#if RELEASE
|
|
string directory = Path.GetDirectoryName(Environment.ProcessPath);
|
|
string fileameId = Path.Join([directory, "\\ToolsId.txt"]);
|
|
string currentId = GetToolsId();
|
|
string previousId = string.Empty;
|
|
|
|
if (File.Exists(fileameId))
|
|
{
|
|
previousId = Utils.ReadAllText(fileameId);
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(previousId) || !previousId.Equals(currentId, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
Program.PrintColorMessage($"[WARNING] Build tools are out of date!", ConsoleColor.Yellow);
|
|
}
|
|
#endif
|
|
}
|
|
|
|
/// <summary>
|
|
/// Writes the current build tools hash to the <c>ToolsId.txt</c> file
|
|
/// and prints the hash to the console.
|
|
/// </summary>
|
|
public static void WriteToolsId()
|
|
{
|
|
string directory = Path.GetDirectoryName(Environment.ProcessPath);
|
|
string filenameId = Path.Join([directory, "\\ToolsId.txt"]);
|
|
string currentHash = GetToolsId();
|
|
|
|
Utils.WriteAllText(filenameId, currentHash);
|
|
|
|
Program.PrintColorMessage("Tools Hash: ", ConsoleColor.Gray, false);
|
|
Program.PrintColorMessage(currentHash, ConsoleColor.Green);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Computes an SHA-256 hash of all <c>.cs</c> files in the specified build tool directories.
|
|
/// The hash represents the current state of the build tools source files.
|
|
/// </summary>
|
|
/// <returns>
|
|
/// A hexadecimal string representing the SHA-256 hash of the build tools source files,
|
|
/// or an empty string if the hash could not be computed.
|
|
/// </returns>
|
|
private static string GetToolsId()
|
|
{
|
|
string[] directories =
|
|
[
|
|
"tools\\CustomBuildTool",
|
|
"tools\\CustomBuildTool\\AzureSignTool",
|
|
];
|
|
|
|
return GetToolsId(directories);
|
|
}
|
|
|
|
private static IEnumerable<string> GetAllCsFiles(string[] Directories)
|
|
{
|
|
foreach (string directory in Directories)
|
|
{
|
|
foreach (string source in Directory.EnumerateFiles(directory, "*.cs", SearchOption.TopDirectoryOnly))
|
|
{
|
|
yield return source;
|
|
}
|
|
}
|
|
}
|
|
|
|
private static string GetToolsId(string[] Directories)
|
|
{
|
|
const int bufferSize = 0x4000;
|
|
using var sha256 = SHA256.Create();
|
|
byte[] buffer = ArrayPool<byte>.Shared.Rent(bufferSize);
|
|
|
|
try
|
|
{
|
|
foreach (string source in GetAllCsFiles(Directories))
|
|
{
|
|
using (var filestream = new FileStream(source, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize, FileOptions.SequentialScan))
|
|
{
|
|
int bytesRead;
|
|
|
|
while ((bytesRead = filestream.Read(buffer, 0, buffer.Length)) > 0)
|
|
{
|
|
sha256.TransformBlock(buffer, 0, bytesRead, null, 0);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
ArrayPool<byte>.Shared.Return(buffer, clearArray: true);
|
|
}
|
|
|
|
sha256.TransformFinalBlock(Array.Empty<byte>(), 0, 0);
|
|
return sha256.Hash == null ? string.Empty : Convert.ToHexString(sha256.Hash);
|
|
}
|
|
}
|
|
}
|