mirror of
https://github.com/sp-tarkov/modules.git
synced 2025-02-13 05:30:43 -05:00
I didn't keep in mind that 'ShouldReaquire' handles both the initial check for acquiring of a file and also the re-acquiring of it, I've renamed the method here to be more accurate in this behavior so that hopefully it prevents this from happening again in the future. Also fixes the issue of an exception being thrown when checking if the bundle exists or not. Thanks to MadByte for finding this issue Reviewed-on: SPT/Modules#160 Co-authored-by: Archangel <jesse@archangel.wtf> Co-committed-by: Archangel <jesse@archangel.wtf>
92 lines
2.9 KiB
C#
92 lines
2.9 KiB
C#
using System.Collections.Concurrent;
|
|
using System.Threading.Tasks;
|
|
using BepInEx.Logging;
|
|
using Newtonsoft.Json;
|
|
using SPT.Common.Http;
|
|
using SPT.Common.Utils;
|
|
using SPT.Custom.Models;
|
|
|
|
namespace SPT.Custom.Utils
|
|
{
|
|
public static class BundleManager
|
|
{
|
|
private const string CachePath = "user/cache/bundles/";
|
|
private static readonly ManualLogSource _logger;
|
|
public static readonly ConcurrentDictionary<string, BundleItem> Bundles;
|
|
|
|
static BundleManager()
|
|
{
|
|
_logger = Logger.CreateLogSource(nameof(BundleManager));
|
|
Bundles = new ConcurrentDictionary<string, BundleItem>();
|
|
}
|
|
|
|
public static string GetBundlePath(BundleItem bundle)
|
|
{
|
|
return RequestHandler.IsLocal
|
|
? $"{bundle.ModPath}/bundles/"
|
|
: CachePath;
|
|
}
|
|
|
|
public static string GetBundleFilePath(BundleItem bundle)
|
|
{
|
|
return GetBundlePath(bundle) + bundle.FileName;
|
|
}
|
|
|
|
public static async Task DownloadManifest()
|
|
{
|
|
// get bundles
|
|
var json = await RequestHandler.GetJsonAsync("/singleplayer/bundles");
|
|
var bundles = JsonConvert.DeserializeObject<BundleItem[]>(json);
|
|
|
|
foreach (var bundle in bundles)
|
|
{
|
|
Bundles.TryAdd(bundle.FileName, bundle);
|
|
}
|
|
}
|
|
|
|
public static async Task DownloadBundle(BundleItem bundle)
|
|
{
|
|
var filepath = GetBundleFilePath(bundle);
|
|
var data = await RequestHandler.GetDataAsync($"/files/bundle/{bundle.FileName}");
|
|
await VFS.WriteFileAsync(filepath, data);
|
|
}
|
|
|
|
// Handles both the check for initially acquiring and also re-acquiring a file.
|
|
public static async Task<bool> ShouldAcquire(BundleItem bundle)
|
|
{
|
|
// read cache
|
|
var filepath = GetBundleFilePath(bundle);
|
|
|
|
if (VFS.Exists(filepath))
|
|
{
|
|
// calculate hash
|
|
var data = await VFS.ReadFileAsync(filepath);
|
|
var crc = Crc32.Compute(data);
|
|
|
|
if (crc == bundle.Crc)
|
|
{
|
|
// file is up-to-date
|
|
var location = RequestHandler.IsLocal
|
|
? "MOD"
|
|
: "CACHE";
|
|
|
|
_logger.LogInfo($"{location}: Loading locally {bundle.FileName}");
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
// crc doesn't match, reaquire the file
|
|
_logger.LogInfo($"CACHE: Bundle is invalid, (re-)acquiring {bundle.FileName}");
|
|
return true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// file doesn't exist in cache
|
|
_logger.LogInfo($"CACHE: Bundle is missing, (re-)acquiring {bundle.FileName}");
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
}
|