Implemented BundleHashCacheService

This commit is contained in:
Chomp
2025-01-28 19:51:52 +00:00
parent 293b944562
commit 78e8c1b3c1
2 changed files with 37 additions and 12 deletions
@@ -1,29 +1,54 @@
namespace Core.Services.Cache;
using Core.Models.Utils;
using Core.Utils;
using SptCommon.Annotations;
public class BundleHashCacheService
namespace Core.Services.Cache;
[Injectable]
public class BundleHashCacheService(
ISptLogger<BundleHashCacheService> _logger,
HashUtil _hashUtil,
JsonUtil _jsonUtil,
FileUtil _fileUtil
)
{
public int GetStoredValue(string key)
protected Dictionary<string, string> _bundleHashes = new();
protected readonly string _bundleHashCachePath = "./user/cache/bundleHashCache.json";
public string GetStoredValue(string key)
{
throw new NotImplementedException();
_bundleHashes.TryGetValue(key, out var value);
return value;
}
public void StoreValue(string key, int value)
public void StoreValue(string key, string value)
{
throw new NotImplementedException();
_bundleHashes.Add(key, value);
_fileUtil.WriteFile(_bundleHashCachePath, _jsonUtil.Serialize(_bundleHashes));
_logger.Debug($"Bundle {key} hash stored in {_bundleHashCachePath}");
}
public bool MatchWithStoredHash(string bundlePath, int hash)
public bool MatchWithStoredHash(string bundlePath, string hash)
{
throw new NotImplementedException();
return GetStoredValue(bundlePath) == hash;
}
public bool CalculateAndMatchHash(string bundlePath)
{
throw new NotImplementedException();
var fileContents = _fileUtil.ReadFile(bundlePath);
var generatedHash = _hashUtil.GenerateCrc32ForData(fileContents);
return MatchWithStoredHash(bundlePath, generatedHash);
}
public void CalculateAndStoreHash(string bundlePath)
{
throw new NotImplementedException();
var fileContents = _fileUtil.ReadFile(bundlePath);
var generatedHash = _hashUtil.GenerateCrc32ForData(fileContents);
StoreValue(bundlePath, generatedHash);
}
}
+2 -2
View File
@@ -62,11 +62,11 @@ public class FileUtil
return reader.ReadToEnd();
}
public void WriteFile(string filePath, string jsonProfile)
public void WriteFile(string filePath, string json)
{
if (!FileExists(filePath))
CreateFile(filePath);
File.WriteAllText(filePath, jsonProfile);
File.WriteAllText(filePath, json);
}
private void CreateFile(string filePath)