queries and restart fixes
This commit is contained in:
@@ -7,6 +7,7 @@ using System.Threading.Tasks;
|
||||
using Torch.API.Managers;
|
||||
using Torch.API.Session;
|
||||
using VRage.Game.ModAPI;
|
||||
using Version = SemanticVersioning.Version;
|
||||
|
||||
namespace Torch.API
|
||||
{
|
||||
@@ -65,7 +66,7 @@ namespace Torch.API
|
||||
/// <summary>
|
||||
/// The binary version of the current instance.
|
||||
/// </summary>
|
||||
InformationalVersion TorchVersion { get; }
|
||||
Version TorchVersion { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Invoke an action on the game thread.
|
||||
|
@@ -1,62 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Torch.API
|
||||
{
|
||||
/// <summary>
|
||||
/// Version in the form v#.#.#.#-branch
|
||||
/// </summary>
|
||||
public class InformationalVersion
|
||||
{
|
||||
public Version Version { get; set; }
|
||||
public string Branch { get; set; }
|
||||
|
||||
public static bool TryParse(string input, out InformationalVersion version)
|
||||
{
|
||||
version = default(InformationalVersion);
|
||||
var trim = input.TrimStart('v');
|
||||
var info = trim.Split(new[]{'-'}, 2);
|
||||
if (!Version.TryParse(info[0], out Version result))
|
||||
return false;
|
||||
|
||||
version = new InformationalVersion { Version = result };
|
||||
|
||||
if (info.Length > 1)
|
||||
version.Branch = info[1];
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString()
|
||||
{
|
||||
if (Branch == null)
|
||||
return $"v{Version}";
|
||||
|
||||
return $"v{Version}-{string.Join("-", Branch)}";
|
||||
}
|
||||
|
||||
public static explicit operator InformationalVersion(Version v)
|
||||
{
|
||||
return new InformationalVersion { Version = v };
|
||||
}
|
||||
|
||||
public static implicit operator Version(InformationalVersion v)
|
||||
{
|
||||
return v.Version;
|
||||
}
|
||||
|
||||
public static bool operator >(InformationalVersion lhs, InformationalVersion rhs)
|
||||
{
|
||||
return lhs.Version > rhs.Version;
|
||||
}
|
||||
|
||||
public static bool operator <(InformationalVersion lhs, InformationalVersion rhs)
|
||||
{
|
||||
return lhs.Version < rhs.Version;
|
||||
}
|
||||
}
|
||||
}
|
@@ -20,6 +20,7 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
|
||||
<PackageReference Include="NLog" Version="4.7.13" />
|
||||
<PackageReference Include="SemanticVersioning" Version="2.0.0" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="HavokWrapper, Version=1.0.6278.22649, Culture=neutral, processorArchitecture=AMD64">
|
||||
|
21
Torch.API/Utils/SemanticVersionConverter.cs
Normal file
21
Torch.API/Utils/SemanticVersionConverter.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using Version = SemanticVersioning.Version;
|
||||
|
||||
namespace Torch.API.Utils;
|
||||
|
||||
public class SemanticVersionConverter : JsonConverter<Version>
|
||||
{
|
||||
public override Version? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
Version.TryParse(reader.GetString(), out var ver);
|
||||
return ver;
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, Version value, JsonSerializerOptions options)
|
||||
{
|
||||
writer.WriteStringValue(value.ToString());
|
||||
}
|
||||
}
|
@@ -4,10 +4,13 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using NLog;
|
||||
using Torch.API.Utils;
|
||||
using Version = SemanticVersioning.Version;
|
||||
|
||||
namespace Torch.API.WebAPI
|
||||
{
|
||||
@@ -20,7 +23,7 @@ namespace Torch.API.WebAPI
|
||||
private static readonly Logger Log = LogManager.GetCurrentClassLogger();
|
||||
|
||||
private static JenkinsQuery _instance;
|
||||
public static JenkinsQuery Instance => _instance ?? (_instance = new JenkinsQuery());
|
||||
public static JenkinsQuery Instance => _instance ??= new JenkinsQuery();
|
||||
private HttpClient _client;
|
||||
|
||||
private JenkinsQuery()
|
||||
@@ -39,92 +42,43 @@ namespace Torch.API.WebAPI
|
||||
return null;
|
||||
}
|
||||
|
||||
string r = await h.Content.ReadAsStringAsync();
|
||||
var branchResponse = await h.Content.ReadFromJsonAsync<BranchResponse>();
|
||||
|
||||
BranchResponse response;
|
||||
try
|
||||
if (branchResponse is null)
|
||||
{
|
||||
response = JsonConvert.DeserializeObject<BranchResponse>(r);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex, "Failed to deserialize branch response!");
|
||||
Log.Error("Error reading branch response");
|
||||
return null;
|
||||
}
|
||||
|
||||
h = await _client.GetAsync($"{branchResponse.LastStableBuild.Url}{API_PATH}");
|
||||
if (h.IsSuccessStatusCode)
|
||||
return await h.Content.ReadFromJsonAsync<Job>();
|
||||
|
||||
Log.Error($"Job query failed with code {h.StatusCode}");
|
||||
return null;
|
||||
|
||||
h = await _client.GetAsync($"{response.LastStableBuild.URL}{API_PATH}");
|
||||
if (!h.IsSuccessStatusCode)
|
||||
{
|
||||
Log.Error($"Job query failed with code {h.StatusCode}");
|
||||
return null;
|
||||
}
|
||||
|
||||
r = await h.Content.ReadAsStringAsync();
|
||||
|
||||
Job job;
|
||||
try
|
||||
{
|
||||
job = JsonConvert.DeserializeObject<Job>(r);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex, "Failed to deserialize job response!");
|
||||
return null;
|
||||
}
|
||||
return job;
|
||||
}
|
||||
|
||||
public async Task<bool> DownloadRelease(Job job, string path)
|
||||
{
|
||||
var h = await _client.GetAsync(job.URL + ARTIFACT_PATH);
|
||||
var h = await _client.GetAsync(job.Url + ARTIFACT_PATH);
|
||||
if (!h.IsSuccessStatusCode)
|
||||
{
|
||||
Log.Error($"Job download failed with code {h.StatusCode}");
|
||||
return false;
|
||||
}
|
||||
var s = await h.Content.ReadAsStreamAsync();
|
||||
using (var fs = new FileStream(path, FileMode.Create))
|
||||
{
|
||||
await s.CopyToAsync(fs);
|
||||
await fs.FlushAsync();
|
||||
}
|
||||
await using var fs = new FileStream(path, FileMode.Create);
|
||||
await s.CopyToAsync(fs);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class BranchResponse
|
||||
{
|
||||
public string Name;
|
||||
public string URL;
|
||||
public Build LastBuild;
|
||||
public Build LastStableBuild;
|
||||
}
|
||||
public record BranchResponse(string Name, string Url, Build LastBuild, Build LastStableBuild);
|
||||
|
||||
public class Build
|
||||
{
|
||||
public int Number;
|
||||
public string URL;
|
||||
}
|
||||
public record Build(int Number, string Url);
|
||||
|
||||
public class Job
|
||||
{
|
||||
public int Number;
|
||||
public bool Building;
|
||||
public string Description;
|
||||
public string Result;
|
||||
public string URL;
|
||||
private InformationalVersion _version;
|
||||
|
||||
public InformationalVersion Version
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_version == null)
|
||||
InformationalVersion.TryParse(Description, out _version);
|
||||
|
||||
return _version;
|
||||
}
|
||||
}
|
||||
}
|
||||
public record Job(int Number, bool Building, string Description, string Result, string Url,
|
||||
[property: JsonConverter(typeof(SemanticVersionConverter))] Version Version);
|
||||
}
|
||||
|
@@ -14,7 +14,7 @@ namespace Torch.API.WebAPI
|
||||
public class PluginQuery
|
||||
{
|
||||
private const string ALL_QUERY = "https://torchapi.com/api/plugins/";
|
||||
private const string PLUGIN_QUERY = "https://torchapi.com/api/plugins/item/{0}/";
|
||||
private const string PLUGIN_QUERY = "https://torchapi.com/api/plugins/?guid={0}";
|
||||
private readonly HttpClient _client;
|
||||
private static readonly Logger Log = LogManager.GetCurrentClassLogger();
|
||||
|
||||
@@ -42,15 +42,15 @@ namespace Torch.API.WebAPI
|
||||
CancellationToken.None);
|
||||
}
|
||||
|
||||
public async Task<bool> DownloadPlugin(Guid guid, string path = null)
|
||||
public Task<bool> DownloadPlugin(Guid guid, string path = null)
|
||||
{
|
||||
return await DownloadPlugin(guid.ToString(), path);
|
||||
return DownloadPlugin(guid.ToString(), path);
|
||||
}
|
||||
|
||||
public async Task<bool> DownloadPlugin(string guid, string path = null)
|
||||
{
|
||||
var item = await QueryOne(guid);
|
||||
if (item == null) return false;
|
||||
if (item is null) return false;
|
||||
return await DownloadPlugin(item, path);
|
||||
}
|
||||
|
||||
@@ -59,14 +59,13 @@ namespace Torch.API.WebAPI
|
||||
try
|
||||
{
|
||||
path ??= Path.Combine(Directory.GetCurrentDirectory(), "Plugins", $"{item.Name}.zip");
|
||||
|
||||
var response = await QueryOne(item.Id);
|
||||
if (response.Versions.Length == 0)
|
||||
|
||||
if (item.Versions.Length == 0)
|
||||
{
|
||||
Log.Error($"Selected plugin {item.Name} does not have any versions to download!");
|
||||
return false;
|
||||
}
|
||||
var version = response.Versions.FirstOrDefault(v => v.Version == response.LatestVersion);
|
||||
var version = item.Versions.FirstOrDefault(v => v.Version == item.LatestVersion);
|
||||
if (version is null)
|
||||
{
|
||||
Log.Error($"Could not find latest version for selected plugin {item.Name}");
|
||||
|
Reference in New Issue
Block a user