using Core.Models.Utils; using Core.Utils; using SptCommon.Annotations; namespace Core.Services { [Injectable(InjectionType.Singleton)] public class BundleHashCacheService { private readonly ISptLogger _logger; private readonly JsonUtil _jsonUtil; private readonly HashUtil _hashUtil; private readonly FileUtil _fileUtil; protected readonly Dictionary _bundleHashes = new Dictionary(); protected const string _bundleHashCachePath = "./user/cache/"; protected const string _cacheName = "bundleHashCache.json"; public BundleHashCacheService( ISptLogger logger, JsonUtil jsonUtil, HashUtil hashUtil, FileUtil fileUtil) { _logger = logger; _jsonUtil = jsonUtil; _hashUtil = hashUtil; _fileUtil = fileUtil; } public uint GetStoredValue(string key) { if (!_bundleHashes.TryGetValue(key, out var value)) { return 0; } return value; } public void StoreValue(string bundlePath, uint hash) { _bundleHashes.Add(bundlePath, hash); if (!Directory.Exists(_bundleHashCachePath)) { Directory.CreateDirectory(_bundleHashCachePath); } _fileUtil.WriteFile(Path.Join(_bundleHashCachePath, _cacheName), _jsonUtil.Serialize(_bundleHashes)); _logger.Debug($"Bundle: {bundlePath} hash stored in: ${_bundleHashCachePath}"); } public bool CalculateAndMatchHash(string BundlePath) { return MatchWithStoredHash(BundlePath, CalculateHash(BundlePath)); } public void CalculateAndStoreHash(string BundlePath) { StoreValue(BundlePath, CalculateHash(BundlePath)); } public uint CalculateHash(string BundlePath) { var fileData = _fileUtil.ReadFile(BundlePath); return _hashUtil.GenerateCrc32ForData(fileData); } public bool MatchWithStoredHash(string BundlePath, uint hash) { return GetStoredValue(BundlePath) == hash; } } }