using LightlessSync.API.Data; using System; using System.Collections.Generic; using System.Linq; namespace LightlessSync.PlayerData.Data; public class FileReplacementDataComparer : IEqualityComparer { private static readonly FileReplacementDataComparer _instance = new(); private FileReplacementDataComparer() { } public static FileReplacementDataComparer Instance => _instance; public bool Equals(FileReplacementData? x, FileReplacementData? y) { if (ReferenceEquals(x, y)) return true; if (x is null || y is null) return false; return string.Equals(x.Hash, y.Hash, StringComparison.OrdinalIgnoreCase) && ComparePathSets(x.GamePaths, y.GamePaths) && string.Equals(x.FileSwapPath ?? string.Empty, y.FileSwapPath ?? string.Empty, StringComparison.Ordinal); } public int GetHashCode(FileReplacementData obj) { if (obj is null) return 0; var hash = StringComparer.OrdinalIgnoreCase.GetHashCode(obj.Hash ?? string.Empty); hash = HashCode.Combine(hash, GetSetHashCode(obj.GamePaths)); hash = HashCode.Combine(hash, StringComparer.OrdinalIgnoreCase.GetHashCode(obj.FileSwapPath ?? string.Empty)); return hash; } private static bool ComparePathSets(IEnumerable first, IEnumerable second) { var left = new HashSet(first ?? Enumerable.Empty(), StringComparer.OrdinalIgnoreCase); var right = new HashSet(second ?? Enumerable.Empty(), StringComparer.OrdinalIgnoreCase); return left.SetEquals(right); } private static int GetSetHashCode(IEnumerable paths) { int hash = 0; foreach (var element in paths ?? Enumerable.Empty()) { hash = unchecked(hash + StringComparer.OrdinalIgnoreCase.GetHashCode(element)); } return hash; } }