Files
SPT-Server-Build/Libraries/SPTarkov.Server.Core/Services/BundleHashCacheService.cs
T
Cj d1af6bf6e3 Service sessionIDs to mongoIDs (#454)
* Start updating service sessionIDs to mongoIDs

* Finish service conversion + fix other small issues
2025-07-06 13:08:07 +01:00

67 lines
1.7 KiB
C#

using SPTarkov.DI.Annotations;
using SPTarkov.Server.Core.Models.Utils;
using SPTarkov.Server.Core.Utils;
namespace SPTarkov.Server.Core.Services;
[Injectable(InjectionType.Singleton)]
public class BundleHashCacheService(
ISptLogger<BundleHashCacheService> logger,
JsonUtil jsonUtil,
HashUtil hashUtil,
FileUtil fileUtil
)
{
protected const string _bundleHashCachePath = "./user/cache/";
protected const string _cacheName = "bundleHashCache.json";
protected readonly Dictionary<string, uint> _bundleHashes = new();
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;
}
}