2020-06-01 22:27:57 +00:00
|
|
|
|
using System;
|
2020-04-30 22:35:16 +00:00
|
|
|
|
using System.Collections.Generic;
|
2020-06-20 22:51:47 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
2020-05-20 03:25:41 +00:00
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Text;
|
2020-04-30 22:35:16 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2020-02-27 13:46:34 +00:00
|
|
|
|
using Wabbajack.Common;
|
2020-05-21 22:12:51 +00:00
|
|
|
|
using Wabbajack.Common.Exceptions;
|
2020-05-20 03:25:41 +00:00
|
|
|
|
using Wabbajack.Common.Serialization.Json;
|
|
|
|
|
using Wabbajack.Lib.Downloaders;
|
2020-06-20 22:51:47 +00:00
|
|
|
|
using Wabbajack.VirtualFileSystem;
|
2020-02-27 13:46:34 +00:00
|
|
|
|
|
2020-06-20 22:51:47 +00:00
|
|
|
|
namespace Wabbajack.Lib
|
2020-02-27 13:46:34 +00:00
|
|
|
|
{
|
2020-05-20 03:25:41 +00:00
|
|
|
|
[JsonName("ModUpgradeRequest")]
|
|
|
|
|
public class ModUpgradeRequest
|
|
|
|
|
{
|
|
|
|
|
public Archive OldArchive { get; set; }
|
|
|
|
|
public Archive NewArchive { get; set; }
|
|
|
|
|
|
|
|
|
|
public ModUpgradeRequest(Archive oldArchive, Archive newArchive)
|
|
|
|
|
{
|
|
|
|
|
OldArchive = oldArchive;
|
|
|
|
|
NewArchive = newArchive;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsValid
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2020-05-23 21:03:25 +00:00
|
|
|
|
if (OldArchive.Size > 2_500_000_000 || NewArchive.Size > 2_500_000_000) return false;
|
2020-05-20 03:25:41 +00:00
|
|
|
|
if (OldArchive.Hash == NewArchive.Hash && OldArchive.State.PrimaryKeyString == NewArchive.State.PrimaryKeyString) return false;
|
|
|
|
|
if (OldArchive.State.GetType() != NewArchive.State.GetType())
|
|
|
|
|
return false;
|
|
|
|
|
if (OldArchive.State is IUpgradingState u)
|
|
|
|
|
{
|
|
|
|
|
return u.ValidateUpgrade(NewArchive.State);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-27 13:46:34 +00:00
|
|
|
|
public class ClientAPI
|
|
|
|
|
{
|
2020-05-25 14:31:56 +00:00
|
|
|
|
public static async Task<Common.Http.Client> GetClient()
|
2020-02-27 13:46:34 +00:00
|
|
|
|
{
|
|
|
|
|
var client = new Common.Http.Client();
|
2020-06-01 22:27:57 +00:00
|
|
|
|
client.Headers.Add((Consts.MetricsKeyHeader, await Metrics.GetMetricsKey()));
|
2020-02-27 13:46:34 +00:00
|
|
|
|
return client;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-20 03:25:41 +00:00
|
|
|
|
public static async Task<Uri> GetModUpgrade(Archive oldArchive, Archive newArchive, TimeSpan? maxWait = null, TimeSpan? waitBetweenTries = null)
|
2020-02-27 13:46:34 +00:00
|
|
|
|
{
|
2020-05-20 03:25:41 +00:00
|
|
|
|
maxWait ??= TimeSpan.FromMinutes(10);
|
|
|
|
|
waitBetweenTries ??= TimeSpan.FromSeconds(15);
|
|
|
|
|
|
|
|
|
|
var request = new ModUpgradeRequest( oldArchive, newArchive);
|
|
|
|
|
var start = DateTime.UtcNow;
|
|
|
|
|
|
|
|
|
|
RETRY:
|
|
|
|
|
|
2020-05-25 14:31:56 +00:00
|
|
|
|
var response = await (await GetClient())
|
2020-05-20 03:25:41 +00:00
|
|
|
|
.PostAsync($"{Consts.WabbajackBuildServerUri}mod_upgrade", new StringContent(request.ToJson(), Encoding.UTF8, "application/json"));
|
|
|
|
|
|
2020-04-10 03:54:02 +00:00
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
|
|
|
{
|
2020-05-20 03:25:41 +00:00
|
|
|
|
switch (response.StatusCode)
|
|
|
|
|
{
|
|
|
|
|
case HttpStatusCode.OK:
|
|
|
|
|
return new Uri(await response.Content.ReadAsStringAsync());
|
|
|
|
|
case HttpStatusCode.Accepted:
|
|
|
|
|
Utils.Log($"Waiting for patch processing on the server for {oldArchive.Name}, sleeping for another 15 seconds");
|
|
|
|
|
await Task.Delay(TimeSpan.FromSeconds(15));
|
|
|
|
|
response.Dispose();
|
|
|
|
|
if (DateTime.UtcNow - start > maxWait)
|
|
|
|
|
throw new HttpException(response);
|
|
|
|
|
goto RETRY;
|
|
|
|
|
}
|
2020-04-10 03:54:02 +00:00
|
|
|
|
}
|
2020-05-20 03:25:41 +00:00
|
|
|
|
var ex = new HttpException(response);
|
|
|
|
|
response.Dispose();
|
|
|
|
|
throw ex;
|
2020-02-27 13:46:34 +00:00
|
|
|
|
}
|
2020-03-31 22:05:36 +00:00
|
|
|
|
|
2020-04-02 21:16:46 +00:00
|
|
|
|
public class NexusCacheStats
|
|
|
|
|
{
|
|
|
|
|
public long CachedCount { get; set; }
|
|
|
|
|
public long ForwardCount { get; set; }
|
|
|
|
|
public double CacheRatio { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static async Task<NexusCacheStats> GetNexusCacheStats()
|
|
|
|
|
{
|
2020-05-25 14:31:56 +00:00
|
|
|
|
return await (await GetClient())
|
2020-04-02 21:16:46 +00:00
|
|
|
|
.GetJsonAsync<NexusCacheStats>($"{Consts.WabbajackBuildServerUri}nexus_cache/stats");
|
|
|
|
|
}
|
2020-04-30 22:35:16 +00:00
|
|
|
|
|
2020-06-20 22:51:47 +00:00
|
|
|
|
public static async Task SendModListDefinition(ModList modList)
|
2020-04-30 22:35:16 +00:00
|
|
|
|
{
|
2020-06-20 22:51:47 +00:00
|
|
|
|
var client = await GetClient();
|
|
|
|
|
await client.PostAsync($"{Consts.WabbajackBuildServerUri}list_definitions/ingest", new StringContent(modList.ToJson(), Encoding.UTF8, "application/json"));
|
2020-04-30 22:35:16 +00:00
|
|
|
|
}
|
2020-06-14 13:13:29 +00:00
|
|
|
|
|
2020-06-20 22:51:47 +00:00
|
|
|
|
public static async Task<Archive[]> GetExistingGameFiles(WorkQueue queue, Game game)
|
2020-06-14 13:13:29 +00:00
|
|
|
|
{
|
|
|
|
|
var client = await GetClient();
|
2020-06-20 22:51:47 +00:00
|
|
|
|
var metaData = game.MetaData();
|
|
|
|
|
var results =
|
|
|
|
|
await client.GetJsonAsync<Archive[]>(
|
|
|
|
|
$"{Consts.WabbajackBuildServerUri}game_files/{game}/{metaData.InstalledVersion}");
|
|
|
|
|
|
|
|
|
|
return (await results.PMap(queue, async file => (await file.State.Verify(file), file))).Where(f => f.Item1)
|
|
|
|
|
.Select(f =>
|
|
|
|
|
{
|
|
|
|
|
f.file.Name = ((GameFileSourceDownloader.State)f.file.State).GameFile.Munge().ToString();
|
|
|
|
|
return f.file;
|
|
|
|
|
})
|
|
|
|
|
.ToArray();
|
2020-06-14 13:13:29 +00:00
|
|
|
|
}
|
2020-06-21 22:03:54 +00:00
|
|
|
|
|
|
|
|
|
public static async Task<AbstractDownloadState?> InferDownloadState(Hash hash)
|
|
|
|
|
{
|
|
|
|
|
var client = await GetClient();
|
|
|
|
|
var results = await client.GetJsonAsync<Archive[]>($"{Consts.WabbajackBuildServerUri}mod_files/by_hash/{hash.ToHex()}");
|
|
|
|
|
|
|
|
|
|
foreach (var result in results)
|
|
|
|
|
{
|
|
|
|
|
if (await result.State.Verify(result)) return result.State;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2020-02-27 13:46:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|