queries and restart fixes
This commit is contained in:
@@ -7,6 +7,7 @@ using System.Threading.Tasks;
|
|||||||
using Torch.API.Managers;
|
using Torch.API.Managers;
|
||||||
using Torch.API.Session;
|
using Torch.API.Session;
|
||||||
using VRage.Game.ModAPI;
|
using VRage.Game.ModAPI;
|
||||||
|
using Version = SemanticVersioning.Version;
|
||||||
|
|
||||||
namespace Torch.API
|
namespace Torch.API
|
||||||
{
|
{
|
||||||
@@ -65,7 +66,7 @@ namespace Torch.API
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// The binary version of the current instance.
|
/// The binary version of the current instance.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
InformationalVersion TorchVersion { get; }
|
Version TorchVersion { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Invoke an action on the game thread.
|
/// 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>
|
<ItemGroup>
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
|
||||||
<PackageReference Include="NLog" Version="4.7.13" />
|
<PackageReference Include="NLog" Version="4.7.13" />
|
||||||
|
<PackageReference Include="SemanticVersioning" Version="2.0.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Reference Include="HavokWrapper, Version=1.0.6278.22649, Culture=neutral, processorArchitecture=AMD64">
|
<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.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
|
using System.Net.Http.Json;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Newtonsoft.Json;
|
|
||||||
using NLog;
|
using NLog;
|
||||||
|
using Torch.API.Utils;
|
||||||
|
using Version = SemanticVersioning.Version;
|
||||||
|
|
||||||
namespace Torch.API.WebAPI
|
namespace Torch.API.WebAPI
|
||||||
{
|
{
|
||||||
@@ -20,7 +23,7 @@ namespace Torch.API.WebAPI
|
|||||||
private static readonly Logger Log = LogManager.GetCurrentClassLogger();
|
private static readonly Logger Log = LogManager.GetCurrentClassLogger();
|
||||||
|
|
||||||
private static JenkinsQuery _instance;
|
private static JenkinsQuery _instance;
|
||||||
public static JenkinsQuery Instance => _instance ?? (_instance = new JenkinsQuery());
|
public static JenkinsQuery Instance => _instance ??= new JenkinsQuery();
|
||||||
private HttpClient _client;
|
private HttpClient _client;
|
||||||
|
|
||||||
private JenkinsQuery()
|
private JenkinsQuery()
|
||||||
@@ -39,92 +42,43 @@ namespace Torch.API.WebAPI
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
string r = await h.Content.ReadAsStringAsync();
|
var branchResponse = await h.Content.ReadFromJsonAsync<BranchResponse>();
|
||||||
|
|
||||||
BranchResponse response;
|
if (branchResponse is null)
|
||||||
try
|
|
||||||
{
|
{
|
||||||
response = JsonConvert.DeserializeObject<BranchResponse>(r);
|
Log.Error("Error reading branch response");
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Log.Error(ex, "Failed to deserialize branch response!");
|
|
||||||
return null;
|
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)
|
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)
|
if (!h.IsSuccessStatusCode)
|
||||||
{
|
{
|
||||||
Log.Error($"Job download failed with code {h.StatusCode}");
|
Log.Error($"Job download failed with code {h.StatusCode}");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
var s = await h.Content.ReadAsStreamAsync();
|
var s = await h.Content.ReadAsStreamAsync();
|
||||||
using (var fs = new FileStream(path, FileMode.Create))
|
await using var fs = new FileStream(path, FileMode.Create);
|
||||||
{
|
await s.CopyToAsync(fs);
|
||||||
await s.CopyToAsync(fs);
|
|
||||||
await fs.FlushAsync();
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class BranchResponse
|
public record BranchResponse(string Name, string Url, Build LastBuild, Build LastStableBuild);
|
||||||
{
|
|
||||||
public string Name;
|
|
||||||
public string URL;
|
|
||||||
public Build LastBuild;
|
|
||||||
public Build LastStableBuild;
|
|
||||||
}
|
|
||||||
|
|
||||||
public class Build
|
public record Build(int Number, string Url);
|
||||||
{
|
|
||||||
public int Number;
|
|
||||||
public string URL;
|
|
||||||
}
|
|
||||||
|
|
||||||
public class Job
|
public record Job(int Number, bool Building, string Description, string Result, string Url,
|
||||||
{
|
[property: JsonConverter(typeof(SemanticVersionConverter))] Version Version);
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@@ -14,7 +14,7 @@ namespace Torch.API.WebAPI
|
|||||||
public class PluginQuery
|
public class PluginQuery
|
||||||
{
|
{
|
||||||
private const string ALL_QUERY = "https://torchapi.com/api/plugins/";
|
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 readonly HttpClient _client;
|
||||||
private static readonly Logger Log = LogManager.GetCurrentClassLogger();
|
private static readonly Logger Log = LogManager.GetCurrentClassLogger();
|
||||||
|
|
||||||
@@ -42,15 +42,15 @@ namespace Torch.API.WebAPI
|
|||||||
CancellationToken.None);
|
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)
|
public async Task<bool> DownloadPlugin(string guid, string path = null)
|
||||||
{
|
{
|
||||||
var item = await QueryOne(guid);
|
var item = await QueryOne(guid);
|
||||||
if (item == null) return false;
|
if (item is null) return false;
|
||||||
return await DownloadPlugin(item, path);
|
return await DownloadPlugin(item, path);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,14 +59,13 @@ namespace Torch.API.WebAPI
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
path ??= Path.Combine(Directory.GetCurrentDirectory(), "Plugins", $"{item.Name}.zip");
|
path ??= Path.Combine(Directory.GetCurrentDirectory(), "Plugins", $"{item.Name}.zip");
|
||||||
|
|
||||||
var response = await QueryOne(item.Id);
|
if (item.Versions.Length == 0)
|
||||||
if (response.Versions.Length == 0)
|
|
||||||
{
|
{
|
||||||
Log.Error($"Selected plugin {item.Name} does not have any versions to download!");
|
Log.Error($"Selected plugin {item.Name} does not have any versions to download!");
|
||||||
return false;
|
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)
|
if (version is null)
|
||||||
{
|
{
|
||||||
Log.Error($"Could not find latest version for selected plugin {item.Name}");
|
Log.Error($"Could not find latest version for selected plugin {item.Name}");
|
||||||
|
@@ -52,12 +52,8 @@ quit";
|
|||||||
{
|
{
|
||||||
if (_init)
|
if (_init)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
#if !DEBUG
|
|
||||||
AppDomain.CurrentDomain.UnhandledException += HandleException;
|
AppDomain.CurrentDomain.UnhandledException += HandleException;
|
||||||
LogManager.Configuration.AddRule(LogLevel.Info, LogLevel.Fatal, "console");
|
|
||||||
LogManager.ReconfigExistingLoggers();
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
//enables logging debug messages when built in debug mode. Amazing.
|
//enables logging debug messages when built in debug mode. Amazing.
|
||||||
@@ -232,16 +228,10 @@ quit";
|
|||||||
private void HandleException(object sender, UnhandledExceptionEventArgs e)
|
private void HandleException(object sender, UnhandledExceptionEventArgs e)
|
||||||
{
|
{
|
||||||
_server.FatalException = true;
|
_server.FatalException = true;
|
||||||
|
if (Debugger.IsAttached)
|
||||||
|
return;
|
||||||
var ex = (Exception)e.ExceptionObject;
|
var ex = (Exception)e.ExceptionObject;
|
||||||
Log.Fatal(ex.ToStringDemystified());
|
Log.Fatal(ex.ToStringDemystified());
|
||||||
if (MyFakes.ENABLE_MINIDUMP_SENDING)
|
|
||||||
{
|
|
||||||
string path = Path.Combine(MyFileSystem.UserDataPath, "Minidump.dmp");
|
|
||||||
Log.Info($"Generating minidump at {path}");
|
|
||||||
Log.Error("Keen broke the minidump, sorry.");
|
|
||||||
//MyMiniDump.Options options = MyMiniDump.Options.WithProcessThreadData | MyMiniDump.Options.WithThreadInfo;
|
|
||||||
//MyMiniDump.Write(path, options, MyMiniDump.ExceptionInfo.Present);
|
|
||||||
}
|
|
||||||
LogManager.Flush();
|
LogManager.Flush();
|
||||||
if (Config.RestartOnCrash)
|
if (Config.RestartOnCrash)
|
||||||
{
|
{
|
||||||
|
@@ -1,9 +1,9 @@
|
|||||||
{
|
{
|
||||||
"$schema": "http://json.schemastore.org/launchsettings.json",
|
"$schema": "https://json.schemastore.org/launchsettings.json",
|
||||||
"profiles": {
|
"profiles": {
|
||||||
"Torch.Server": {
|
"Torch.Server": {
|
||||||
"commandName": "Project",
|
"commandName": "Project",
|
||||||
"commandLineArgs": "-noupdate",
|
|
||||||
"use64Bit": true,
|
"use64Bit": true,
|
||||||
"hotReloadEnabled": false
|
"hotReloadEnabled": false
|
||||||
}
|
}
|
||||||
|
@@ -207,13 +207,12 @@ namespace Torch.Server
|
|||||||
var config = (TorchConfig)torch.Config;
|
var config = (TorchConfig)torch.Config;
|
||||||
LogManager.Flush();
|
LogManager.Flush();
|
||||||
|
|
||||||
string exe = Assembly.GetExecutingAssembly().Location;
|
string exe = Assembly.GetExecutingAssembly().Location.Replace("dll", "exe");
|
||||||
Debug.Assert(exe != null);
|
config.WaitForPID = Environment.ProcessId.ToString();
|
||||||
config.WaitForPID = Process.GetCurrentProcess().Id.ToString();
|
|
||||||
config.TempAutostart = true;
|
config.TempAutostart = true;
|
||||||
Process.Start(exe, config.ToString());
|
Process.Start(exe, config.ToString());
|
||||||
|
|
||||||
Process.GetCurrentProcess().Kill();
|
Environment.Exit(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -50,7 +50,7 @@ namespace Torch.Managers
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var job = await JenkinsQuery.Instance.GetLatestVersion(Torch.TorchVersion.Branch);
|
var job = await JenkinsQuery.Instance.GetLatestVersion(Torch.TorchVersion.Build);
|
||||||
if (job == null)
|
if (job == null)
|
||||||
{
|
{
|
||||||
_log.Info("Failed to fetch latest version.");
|
_log.Info("Failed to fetch latest version.");
|
||||||
|
@@ -63,7 +63,7 @@ namespace Torch
|
|||||||
public ITorchConfig Config { get; protected set; }
|
public ITorchConfig Config { get; protected set; }
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public InformationalVersion TorchVersion { get; }
|
public SemanticVersioning.Version TorchVersion { get; }
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public Version GameVersion { get; private set; }
|
public Version GameVersion { get; private set; }
|
||||||
@@ -115,16 +115,16 @@ namespace Torch
|
|||||||
#pragma warning restore CS0618
|
#pragma warning restore CS0618
|
||||||
Config = config;
|
Config = config;
|
||||||
|
|
||||||
var versionString = Assembly.GetEntryAssembly()
|
var versionString = GetType().Assembly
|
||||||
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
|
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()!
|
||||||
.InformationalVersion;
|
.InformationalVersion;
|
||||||
|
|
||||||
if (!InformationalVersion.TryParse(versionString, out InformationalVersion version))
|
if (!SemanticVersioning.Version.TryParse(versionString, out var version))
|
||||||
throw new TypeLoadException("Unable to parse the Torch version from the assembly.");
|
throw new TypeLoadException("Unable to parse the Torch version from the assembly.");
|
||||||
|
|
||||||
TorchVersion = version;
|
TorchVersion = version;
|
||||||
|
|
||||||
RunArgs = new string[0];
|
RunArgs = Array.Empty<string>();
|
||||||
|
|
||||||
Managers = new DependencyManager();
|
Managers = new DependencyManager();
|
||||||
|
|
||||||
@@ -140,7 +140,7 @@ namespace Torch
|
|||||||
Managers.AddManager(sessionManager);
|
Managers.AddManager(sessionManager);
|
||||||
Managers.AddManager(new PatchManager(this));
|
Managers.AddManager(new PatchManager(this));
|
||||||
Managers.AddManager(new FilesystemManager(this));
|
Managers.AddManager(new FilesystemManager(this));
|
||||||
Managers.AddManager(new UpdateManager(this));
|
// Managers.AddManager(new UpdateManager(this));
|
||||||
Managers.AddManager(new EventManager(this));
|
Managers.AddManager(new EventManager(this));
|
||||||
#pragma warning disable CS0618
|
#pragma warning disable CS0618
|
||||||
Managers.AddManager(Plugins);
|
Managers.AddManager(Plugins);
|
||||||
|
Reference in New Issue
Block a user