cache nexus info when validating mod links

This commit is contained in:
Timothy Baldridge 2019-08-24 14:23:16 -06:00
parent 4dcbee6fdc
commit 7be81d9a98
3 changed files with 24 additions and 2 deletions

View File

@ -27,6 +27,8 @@ namespace Wabbajack.Common
}
}
public static string NexusCacheDirectory = "nexus_link_cache";
public static string WABBAJACK_INCLUDE = "WABBAJACK_INCLUDE";
public static String AppName = "Wabbajack";

View File

@ -363,7 +363,7 @@ namespace Wabbajack
Status($"Getting Nexus info for {found.Name}");
try
{
var link = NexusAPI.GetNexusDownloadLink((NexusMod)result, NexusKey);
var link = NexusAPI.GetNexusDownloadLink((NexusMod)result, NexusKey, true);
}
catch (Exception ex)
{

View File

@ -65,8 +65,10 @@ namespace Wabbajack
return _baseHttpClient;
}
public static string GetNexusDownloadLink(NexusMod archive, string apikey)
public static string GetNexusDownloadLink(NexusMod archive, string apikey, bool cache=true)
{
if (cache && TryGetCachedLink(archive, apikey, out string result)) return result;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var client = BaseNexusClient(apikey);
string url;
@ -79,6 +81,24 @@ namespace Wabbajack
}
}
private static bool TryGetCachedLink(NexusMod archive, string apikey, out string result)
{
if (!(Directory.Exists(Consts.NexusCacheDirectory)))
Directory.CreateDirectory(Consts.NexusCacheDirectory);
string path = Path.Combine(Consts.NexusCacheDirectory, $"link-{archive.GameName}-{archive.ModID}-{archive.FileID}.txt");
if (!File.Exists(path) || DateTime.Now - new FileInfo(path).LastWriteTime > new TimeSpan(24, 0, 0))
{
File.Delete(path);
result = GetNexusDownloadLink(archive, apikey, false);
File.WriteAllText(path, result);
return true;
}
result = File.ReadAllText(path);
return true;
}
private static string ConvertGameName(string gameName)
{
if (gameName == "SkyrimSE") return "skyrimspecialedition";