diff --git a/Wabbajack.Common/Consts.cs b/Wabbajack.Common/Consts.cs index 7e5a4e8a..fdf6eac1 100644 --- a/Wabbajack.Common/Consts.cs +++ b/Wabbajack.Common/Consts.cs @@ -61,5 +61,7 @@ namespace Wabbajack.Common return headerString; } } + + public static TimeSpan NexusCacheExpiry = new TimeSpan(1, 0, 0, 0); } } \ No newline at end of file diff --git a/Wabbajack.Test/DownloaderTests.cs b/Wabbajack.Test/DownloaderTests.cs index 0291a550..2bf824c7 100644 --- a/Wabbajack.Test/DownloaderTests.cs +++ b/Wabbajack.Test/DownloaderTests.cs @@ -128,6 +128,8 @@ namespace Wabbajack.Test var converted = state.ViaJSON(); Assert.IsTrue(converted.Verify()); + // Exercise the cache code + Assert.IsTrue(converted.Verify()); var filename = Guid.NewGuid().ToString(); Assert.IsTrue(converted.IsWhitelisted(new ServerWhitelist { AllowedPrefixes = new List () })); diff --git a/Wabbajack/Downloaders/NexusDownloader.cs b/Wabbajack/Downloaders/NexusDownloader.cs index d5ba05f2..c250a56e 100644 --- a/Wabbajack/Downloaders/NexusDownloader.cs +++ b/Wabbajack/Downloaders/NexusDownloader.cs @@ -103,8 +103,8 @@ namespace Wabbajack.Downloaders { try { - new NexusApiClient().GetNexusDownloadLink(this, true); - return true; + var info = new NexusApiClient().GetFileInfo(this); + return info.category_name != null; } catch (Exception ex) { diff --git a/Wabbajack/NexusApi/NexusApi.cs b/Wabbajack/NexusApi/NexusApi.cs index 973d73a7..80cb0301 100644 --- a/Wabbajack/NexusApi/NexusApi.cs +++ b/Wabbajack/NexusApi/NexusApi.cs @@ -9,7 +9,9 @@ using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Reflection; +using System.Runtime.Serialization.Json; using System.Security.Authentication; +using System.Text; using System.Threading.Tasks; using Wabbajack.Common; using Wabbajack.Downloaders; @@ -190,6 +192,20 @@ namespace Wabbajack.NexusApi } } + private T GetCached(string url) + { + var code = Encoding.UTF8.GetBytes(url).ToHEX(); + var cache_file = Path.Combine(Consts.NexusCacheDirectory, code + ".json"); + if (File.Exists(cache_file) && DateTime.Now - File.GetLastWriteTime(cache_file) < Consts.NexusCacheExpiry) + { + return cache_file.FromJSON(); + } + + var result = Get(url); + result.ToJSON(cache_file); + return result; + } + public string GetNexusDownloadLink(NexusDownloader.State archive, bool cache = false) { @@ -223,7 +239,7 @@ namespace Wabbajack.NexusApi public NexusFileInfo GetFileInfo(NexusDownloader.State mod) { var url = $"https://api.nexusmods.com/v1/games/{ConvertGameName(mod.GameName)}/mods/{mod.ModID}/files/{mod.FileID}.json"; - return Get(url); + return GetCached(url); } public ModInfo GetModInfo(string gameName, string modId)