Initialize migration. (#88)

Co-authored-by: defnotken <itsdefnotken@gmail.com>
Co-authored-by: cake <admin@cakeandbanana.nl>
Reviewed-on: #88
Reviewed-by: cake <cake@noreply.git.lightless-sync.org>
Co-authored-by: defnotken <defnotken@noreply.git.lightless-sync.org>
Co-committed-by: defnotken <defnotken@noreply.git.lightless-sync.org>
This commit was merged in pull request #88.
This commit is contained in:
2025-11-29 18:02:39 +01:00
committed by cake
parent 9e12725f89
commit 740b58afc4
63 changed files with 1720 additions and 1005 deletions

View File

@@ -1,3 +1,4 @@
using Blake3;
using System;
using System.Collections.Concurrent;
using System.IO;
@@ -16,14 +17,88 @@ public static class Crypto
private static readonly ConcurrentDictionary<string, string> _hashListSHA256 = new(StringComparer.Ordinal);
private static readonly SHA256CryptoServiceProvider _sha256CryptoProvider = new();
public static string GetFileHash(this string filePath)
// BLAKE3 hash caches
private static readonly Dictionary<(string, ushort), string> _hashListPlayersBlake3 = [];
private static readonly Dictionary<string, string> _hashListBlake3 = new(StringComparer.Ordinal);
/// <summary>
/// Supports Blake3 or SHA1 for file transfers, no SHA256 supported on it
/// </summary>
public enum HashAlgo
{
using SHA1 sha1 = SHA1.Create();
using FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
return BitConverter.ToString(sha1.ComputeHash(stream)).Replace("-", "", StringComparison.Ordinal);
Blake3,
Sha1
}
public static async Task<string> GetFileHashAsync(string filePath, CancellationToken cancellationToken = default)
/// <summary>
/// Detects which algo is being used for the file
/// </summary>
/// <param name="hashHex">Hashed string</param>
/// <returns>HashAlgo</returns>
public static HashAlgo DetectAlgo(string hashHex)
{
if (hashHex.Length == 40)
return HashAlgo.Sha1;
return HashAlgo.Blake3;
}
#region File Hashing
/// <summary>
/// Compute file hash with given algorithm, supports BLAKE3 and Sha1 for file hashing
/// </summary>
/// <param name="filePath">Filepath for the hashing</param>
/// <param name="algo">BLAKE3 or Sha1</param>
/// <returns>Hashed file hash</returns>
/// <exception cref="ArgumentOutOfRangeException">Not a valid HashAlgo or Filepath</exception>
public static string ComputeFileHash(string filePath, HashAlgo algo)
{
return algo switch
{
HashAlgo.Blake3 => ComputeFileHashBlake3(filePath),
HashAlgo.Sha1 => ComputeFileHashSha1(filePath),
_ => throw new ArgumentOutOfRangeException(nameof(algo), algo, null)
};
}
/// <summary>
/// Compute file hash asynchronously with given algorithm, supports BLAKE3 and SHA1 for file hashing
/// </summary>
/// <param name="filePath">Filepath for the hashing</param>
/// <param name="algo">BLAKE3 or Sha1</param>
/// <returns>Hashed file hash</returns>
/// <exception cref="ArgumentOutOfRangeException">Not a valid HashAlgo or Filepath</exception>
public static async Task<string> ComputeFileHashAsync(string filePath, HashAlgo algo, CancellationToken cancellationToken = default)
{
return algo switch
{
HashAlgo.Blake3 => await ComputeFileHashBlake3Async(filePath, cancellationToken).ConfigureAwait(false),
HashAlgo.Sha1 => await ComputeFileHashSha1Async(filePath, cancellationToken).ConfigureAwait(false),
_ => throw new ArgumentOutOfRangeException(nameof(algo), algo, message: null)
};
}
/// <summary>
/// Computes an file hash with SHA1
/// </summary>
/// <param name="filePath">Filepath that has to be computed</param>
/// <returns>Hashed file in hex string</returns>
private static string ComputeFileHashSha1(string filePath)
{
using var stream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
using var sha1 = SHA1.Create();
var hash = sha1.ComputeHash(stream);
return Convert.ToHexString(hash);
}
/// <summary>
/// Computes an file hash with SHA1 asynchronously
/// </summary>
/// <param name="filePath">Filepath that has to be computed</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>Hashed file in hex string hashed in SHA1</returns>
private static async Task<string> ComputeFileHashSha1Async(string filePath, CancellationToken cancellationToken)
{
var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete, bufferSize: _bufferSize, options: FileOptions.Asynchronous);
await using (stream.ConfigureAwait(false))
@@ -32,22 +107,121 @@ public static class Crypto
var buffer = new byte[8192];
int bytesRead;
while ((bytesRead = await stream.ReadAsync(buffer, cancellationToken).ConfigureAwait(false)) > 0)
while ((bytesRead = await stream.ReadAsync(buffer.AsMemory(0, buffer.Length), cancellationToken).ConfigureAwait(false)) > 0)
{
sha1.TransformBlock(buffer, 0, bytesRead, outputBuffer: null, 0);
}
sha1.TransformFinalBlock([], 0, 0);
return Convert.ToHexString(sha1.Hash!);
}
}
public static string GetHash256(this (string, ushort) playerToHash)
/// <summary>
/// Computes an file hash with Blake3
/// </summary>
/// <param name="filePath">Filepath that has to be computed</param>
/// <returns>Hashed file in hex string hashed in Blake3</returns>
private static string ComputeFileHashBlake3(string filePath)
{
return _hashListPlayersSHA256.GetOrAdd(playerToHash, key => ComputeHashSHA256(key.Item1 + key.Item2.ToString()));
using var stream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
using var hasher = Hasher.New();
var buffer = new byte[_bufferSize];
int bytesRead;
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
{
hasher.Update(buffer.AsSpan(0, bytesRead));
}
var hash = hasher.Finalize();
return hash.ToString();
}
/// <summary>
/// Computes an file hash with Blake3 asynchronously
/// </summary>
/// <param name="filePath">Filepath that has to be computed</param>
/// <returns>Hashed file in hex string hashed in Blake3</returns>
private static async Task<string> ComputeFileHashBlake3Async(string filePath, CancellationToken cancellationToken)
{
var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete, bufferSize: _bufferSize, options: FileOptions.Asynchronous);
await using (stream.ConfigureAwait(false))
{
using var hasher = Hasher.New();
var buffer = new byte[8192];
int bytesRead;
while ((bytesRead = await stream.ReadAsync(buffer.AsMemory(0, buffer.Length), cancellationToken).ConfigureAwait(false)) > 0)
{
hasher.Update(buffer.AsSpan(0, bytesRead));
}
var hash = hasher.Finalize();
return hash.ToString();
}
}
#endregion
#region String hashing
public static string GetBlake3Hash(this (string, ushort) playerToHash)
{
if (_hashListPlayersBlake3.TryGetValue(playerToHash, out var hash))
return hash;
var toHash = playerToHash.Item1 + playerToHash.Item2.ToString();
hash = ComputeBlake3Hex(toHash);
_hashListPlayersBlake3[playerToHash] = hash;
return hash;
}
/// <summary>
/// Computes or gets an Blake3 hash(ed) string.
/// </summary>
/// <param name="stringToHash">String that needs to be hashsed</param>
/// <returns>Hashed string</returns>
public static string GetBlake3Hash(this string stringToHash)
{
return GetOrComputeBlake3(stringToHash);
}
private static string GetOrComputeBlake3(string stringToCompute)
{
if (_hashListBlake3.TryGetValue(stringToCompute, out var hash))
return hash;
hash = ComputeBlake3Hex(stringToCompute);
_hashListBlake3[stringToCompute] = hash;
return hash;
}
private static string ComputeBlake3Hex(string input)
{
var bytes = Encoding.UTF8.GetBytes(input);
var hash = Hasher.Hash(bytes);
return Convert.ToHexString(hash.AsSpan());
}
public static string GetHash256(this (string, ushort) playerToHash)
{
if (_hashListPlayersSHA256.TryGetValue(playerToHash, out var hash))
return hash;
return _hashListPlayersSHA256[playerToHash] =
Convert.ToHexString(_sha256CryptoProvider.ComputeHash(Encoding.UTF8.GetBytes(playerToHash.Item1 + playerToHash.Item2.ToString())));
}
/// <summary>
/// Computes or gets an SHA256 hash(ed) string.
/// </summary>
/// <param name="stringToHash">String that needs to be hashsed</param>
/// <returns>Hashed string</returns>
public static string GetHash256(this string stringToHash)
{
return _hashListSHA256.GetOrAdd(stringToHash, ComputeHashSHA256);
@@ -55,8 +229,13 @@ public static class Crypto
private static string ComputeHashSHA256(string stringToCompute)
{
using var sha = SHA256.Create();
return BitConverter.ToString(sha.ComputeHash(Encoding.UTF8.GetBytes(stringToCompute))).Replace("-", "", StringComparison.Ordinal);
}
if (_hashListSHA256.TryGetValue(stringToCompute, out var hash))
return hash;
return _hashListSHA256[stringToCompute] =
Convert.ToHexString(_sha256CryptoProvider.ComputeHash(Encoding.UTF8.GetBytes(stringToCompute)));
}
#endregion
#pragma warning restore SYSLIB0021 // Type or member is obsolete
}

View File

@@ -1,6 +1,5 @@
using Microsoft.Extensions.Logging;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace LightlessSync.Utils
@@ -157,7 +156,7 @@ namespace LightlessSync.Utils
return mountOptions;
}
catch (Exception ex)
catch (Exception)
{
return string.Empty;
}
@@ -234,40 +233,6 @@ namespace LightlessSync.Utils
return clusterSize;
}
string realPath = fi.FullName;
if (isWine && realPath.StartsWith("Z:\\", StringComparison.OrdinalIgnoreCase))
{
realPath = "/" + realPath.Substring(3).Replace('\\', '/');
}
var psi = new ProcessStartInfo
{
FileName = "/bin/bash",
Arguments = $"-c \"stat -f -c %s '{realPath.Replace("'", "'\\''")}'\"",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
WorkingDirectory = "/"
};
using var proc = Process.Start(psi);
string stdout = proc?.StandardOutput.ReadToEnd().Trim() ?? "";
string _stderr = proc?.StandardError.ReadToEnd() ?? "";
try { proc?.WaitForExit(); }
catch (Exception ex) { logger?.LogTrace(ex, "stat WaitForExit failed under Wine; ignoring"); }
if (!(!int.TryParse(stdout, out int block) || block <= 0))
{
_blockSizeCache[root] = block;
logger?.LogTrace("Filesystem block size via stat for {root}: {block}", root, block);
return block;
}
logger?.LogTrace("stat did not return valid block size for {file}, output: {out}", fi.FullName, stdout);
_blockSizeCache[root] = _defaultBlockSize;
return _defaultBlockSize;
}
catch (Exception ex)

View File

@@ -497,10 +497,9 @@ public static class SeStringUtils
continue;
var hasColor = fragment.Color.HasValue;
Vector4 color = default;
if (hasColor)
{
color = fragment.Color!.Value;
Vector4 color = fragment.Color!.Value;
builder.PushColorRgba(color);
}
@@ -673,7 +672,7 @@ public static class SeStringUtils
protected abstract byte ChunkType { get; }
}
private class ColorPayload : AbstractColorPayload
private sealed class ColorPayload : AbstractColorPayload
{
protected override byte ChunkType => 0x13;
@@ -687,12 +686,12 @@ public static class SeStringUtils
public ColorPayload(Vector4 color) : this(new Vector3(color.X, color.Y, color.Z)) { }
}
private class ColorEndPayload : AbstractColorEndPayload
private sealed class ColorEndPayload : AbstractColorEndPayload
{
protected override byte ChunkType => 0x13;
}
private class GlowPayload : AbstractColorPayload
private sealed class GlowPayload : AbstractColorPayload
{
protected override byte ChunkType => 0x14;
@@ -706,7 +705,7 @@ public static class SeStringUtils
public GlowPayload(Vector4 color) : this(new Vector3(color.X, color.Y, color.Z)) { }
}
private class GlowEndPayload : AbstractColorEndPayload
private sealed class GlowEndPayload : AbstractColorEndPayload
{
protected override byte ChunkType => 0x14;
}