From 8ec6b94cc5b34d784673962f4114b51d6c7b0ee7 Mon Sep 17 00:00:00 2001 From: Chomp Date: Tue, 28 Jan 2025 19:44:19 +0000 Subject: [PATCH] Implemented `ModHashCacheService` --- .../Services/Cache/ModHashCacheService.cs | 39 +++++++++++++++---- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/Libraries/Core/Services/Cache/ModHashCacheService.cs b/Libraries/Core/Services/Cache/ModHashCacheService.cs index 1c18158d..889394a8 100644 --- a/Libraries/Core/Services/Cache/ModHashCacheService.cs +++ b/Libraries/Core/Services/Cache/ModHashCacheService.cs @@ -1,29 +1,52 @@ -namespace Core.Services.Cache; +using Core.Models.Utils; +using Core.Utils; +using SptCommon.Annotations; -public class ModHashCacheService + +namespace Core.Services.Cache; +[Injectable] +public class ModHashCacheService( + ISptLogger _logger, + JsonUtil _jsonUtil, + HashUtil _hashUtil, + FileUtil _fileUtil + ) { - public string GetStoredValue(string key) + protected readonly Dictionary _modHashes = new(); + protected readonly string _modCachePath = "./user/cache/modCache.json"; + + public string? GetStoredValue(string key) { - throw new NotImplementedException(); + _modHashes.TryGetValue(key, out var value); + + return value; } public void StoreValue(string key, string value) { - throw new NotImplementedException(); + _modHashes.TryAdd(key, value); + + _fileUtil.WriteFile(_modCachePath, _jsonUtil.Serialize(_modHashes)); + + _logger.Debug($"Mod {key} hash stored in: {_modCachePath}"); } public bool MatchWithStoredHash(string modName, string hash) { - throw new NotImplementedException(); + return GetStoredValue(modName) == hash; } public bool CalculateAndCompareHash(string modName, string modContent) { - throw new NotImplementedException(); + var generatedHash = _hashUtil.GenerateSha1ForData(modContent); + + return MatchWithStoredHash(modName, generatedHash); } public void CalculateAndStoreHash(string modName, string modContent) { - throw new NotImplementedException(); + var generatedHash = _hashUtil.GenerateSha1ForData(modContent); + + StoreValue(modName, generatedHash); } }