Check GitHub for a newer starter once a day, with a button to check now

The stable channel compares versions; the testing channel offers the staging
build of another commit. The channel switch is the starter's only, not the
simulator's.
This commit is contained in:
maj00r
2026-10-04 13:26:49 +02:00
parent a7a1e34239
commit 2b3c46938c
19 changed files with 777 additions and 1 deletions

View File

@@ -0,0 +1,99 @@
using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;
using StarterNG.Application.Abstractions;
using StarterNG.Domain.Settings;
namespace StarterNG.Infrastructure.Adapters;
public sealed class GitHubReleaseFeed : IReleaseFeed
{
private const string ReleasesUrl = "https://api.github.com/repos/MaSzyna-EU07/StarterNG/releases/";
/// <summary>The tag CI keeps recreating for every build of the staging branch.</summary>
public const string StagingTag = "staging";
private readonly HttpClient _http;
private readonly IDiagnosticsLog _log;
public GitHubReleaseFeed(IDiagnosticsLog log, Version current, HttpMessageHandler? handler = null)
{
_log = log;
_http = handler is null ? new HttpClient() : new HttpClient(handler);
_http.Timeout = TimeSpan.FromSeconds(10);
// The API turns away requests without a User-Agent.
_http.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("StarterNG", current.ToString()));
_http.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.github+json"));
}
public async Task<ReleaseLookup> LatestAsync(UpdateChannel channel, CancellationToken cancellationToken = default)
{
// "latest" is GitHub's own pick: the newest release that is not a pre-release.
string url = ReleasesUrl + (channel == UpdateChannel.Staging ? "tags/" + StagingTag : "latest");
try
{
using var response = await _http.GetAsync(url, cancellationToken);
// Nothing released yet.
if (response.StatusCode == HttpStatusCode.NotFound)
return ReleaseLookup.Found(null);
response.EnsureSuccessStatusCode();
return ReleaseLookup.Found(Parse(await response.Content.ReadAsStringAsync(cancellationToken), channel));
}
// Offline, rate limited or timed out: there is simply no answer this time,
// and a log entry on every start without a network would only be noise.
catch (HttpRequestException)
{
return ReleaseLookup.NoAnswer;
}
catch (TaskCanceledException)
{
return ReleaseLookup.NoAnswer;
}
catch (Exception ex)
{
_log.Log("update check", ex);
return ReleaseLookup.NoAnswer;
}
}
public static ReleaseInfo? Parse(string json, UpdateChannel channel)
{
var release = JsonSerializer.Deserialize(json, GitHubReleaseContext.Default.GitHubReleaseDocument);
if (release is null || release.Draft ||
string.IsNullOrEmpty(release.TagName) || string.IsNullOrEmpty(release.HtmlUrl))
return null;
bool belongs = channel == UpdateChannel.Staging
? release.TagName == StagingTag
: !release.Prerelease;
return belongs ? new ReleaseInfo(release.TagName, release.HtmlUrl, release.TargetCommitish) : null;
}
}
internal sealed class GitHubReleaseDocument
{
[JsonPropertyName("tag_name")] public string? TagName { get; set; }
[JsonPropertyName("html_url")] public string? HtmlUrl { get; set; }
[JsonPropertyName("target_commitish")] public string? TargetCommitish { get; set; }
[JsonPropertyName("draft")] public bool Draft { get; set; }
[JsonPropertyName("prerelease")] public bool Prerelease { get; set; }
}
[JsonSerializable(typeof(GitHubReleaseDocument))]
internal partial class GitHubReleaseContext : JsonSerializerContext
{
}

View File

@@ -50,6 +50,9 @@ public sealed class SettingsPaths
/// </summary>
public string VehicleCacheDirectory() => Path.Combine(UserConfigDirectory(), "vehicles");
/// <summary>The day of the last update check and the release it found.</summary>
public string UpdateCheckPath() => Path.Combine(UserConfigDirectory(), "updatecheck.txt");
public string UserConfigDirectory() =>
Path.GetDirectoryName(UserConfigPath()) ?? _environment.BaseDirectory;
}

View File

@@ -57,6 +57,11 @@ public sealed class SettingsSerializer
s.VirtualShunting = c.GetBool("ai.trainman", true);
s.LogMissingVehicleFiles = c.GetBool("starter.logmissingvehicles", false);
s.DeveloperTools = c.GetBool("starter.developertools", false);
s.CheckForUpdates = c.GetBool("starter.checkupdates", true);
s.UpdateChannel = string.Equals(c.GetString("starter.updatechannel", "stable"), "staging",
StringComparison.OrdinalIgnoreCase)
? UpdateChannel.Staging
: UpdateChannel.Stable;
s.RenderEngine = IndexOf(SimulatorSettings.RenderEngines, c.GetString("gfxrenderer", "full"), 0);
s.Width = c.GetInt("width", 1280);
@@ -197,6 +202,8 @@ public sealed class SettingsSerializer
c.SetBool("ai.trainman", s.VirtualShunting);
c.SetBool("starter.logmissingvehicles", s.LogMissingVehicleFiles);
c.SetBool("starter.developertools", s.DeveloperTools);
c.SetBool("starter.checkupdates", s.CheckForUpdates);
c.Set("starter.updatechannel", s.UpdateChannel == UpdateChannel.Staging ? "staging" : "stable");
c.Set("gfxrenderer", SimulatorSettings.RenderEngines[Clamp(s.RenderEngine, 0, SimulatorSettings.RenderEngines.Length - 1)]);
c.SetInt("width", s.Width);