Skip to content

Commit

Permalink
Updater semver support
Browse files Browse the repository at this point in the history
  • Loading branch information
LucHeart committed Apr 26, 2024
1 parent a3154d5 commit d2bfc31
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 51 deletions.
5 changes: 3 additions & 2 deletions ShockOsc/Config/ConfigManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.Extensions.Logging;
using OpenShock.SDK.CSharp.Hub.Utils;
using OpenShock.ShockOsc.Utils;

namespace OpenShock.ShockOsc.Config;
Expand Down Expand Up @@ -31,7 +32,7 @@ public ConfigManager(ILogger<ConfigManager> logger)
{
config = JsonSerializer.Deserialize<ShockOscConfig>(json, Options);
}
catch (JsonException e)
catch (Exception e)
{
_logger.LogCritical(e, "Error during deserialization/loading of config");
_logger.LogWarning("Attempting to move old config and generate a new one");
Expand All @@ -55,7 +56,7 @@ public ConfigManager(ILogger<ConfigManager> logger)
private static readonly JsonSerializerOptions Options = new()
{
WriteIndented = true,
Converters = { new JsonStringEnumConverter() }
Converters = { new JsonStringEnumConverter(), new SemVersionJsonConverter() }
};

private readonly SemaphoreSlim _saveLock = new(1, 1);
Expand Down
6 changes: 4 additions & 2 deletions ShockOsc/Config/ShockOscConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace OpenShock.ShockOsc.Config;
using Semver;

namespace OpenShock.ShockOsc.Config;

public sealed class ShockOscConfig
{
Expand All @@ -7,7 +9,7 @@ public sealed class ShockOscConfig
public OpenShockConf OpenShock { get; set; } = new();
public ChatboxConf Chatbox { get; set; } = new();
public IDictionary<Guid, Group> Groups { get; set; } = new Dictionary<Guid, Group>();
public Version? LastIgnoredVersion { get; set; } = null;
public SemVersion? LastIgnoredVersion { get; set; } = null;

public AppConfig App { get; set; } = new();
}
4 changes: 4 additions & 0 deletions ShockOsc/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using OpenShock.ShockOsc.Logging;
using OpenShock.ShockOsc.OscQueryLibrary;
using OpenShock.ShockOsc.Services;
using OpenShock.ShockOsc.Utils;
using Serilog;
using MauiApp = OpenShock.ShockOsc.Ui.MauiApp;

Expand Down Expand Up @@ -147,6 +148,9 @@ public static Microsoft.Maui.Hosting.MauiApp CreateMauiApp()
app.Services.GetRequiredService<Services.ShockOsc>();
app.Services.GetRequiredService<OscQueryServer>().Start();

var updater = app.Services.GetRequiredService<Updater>();
OsTask.Run(updater.CheckUpdate);

return app;
}
}
42 changes: 22 additions & 20 deletions ShockOsc/Services/Updater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,34 @@
using System.Reflection;
using System.Text.Json;
using Microsoft.Extensions.Logging;
using OpenShock.SDK.CSharp.Updatables;
using OpenShock.ShockOsc.Config;
using OpenShock.ShockOsc.Models;
using OpenShock.ShockOsc.Ui.Utils;
using Semver;

namespace OpenShock.ShockOsc.Services;

public sealed class Updater
{
private const string GithubLatest = "https://api.github.com/repos/OpenShock/ShockOsc/releases/latest";
private const string GithubLatest = "https://api.github.com/repos/OpenShock/ShockOsc/releases/152715042";
private const string SetupFileName = "ShockOSC_Setup.exe"; // OpenShock.ShockOsc.exe

private static readonly HttpClient HttpClient = new();

private readonly string _setupFilePath = Path.Combine(Environment.CurrentDirectory, SetupFileName);

private readonly Version _currentVersion = Assembly.GetEntryAssembly()?.GetName().Version ??
throw new Exception("Could not determine ShockOsc version");
private readonly SemVersion _currentVersion = SemVersion.Parse(typeof(Updater).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()!.InformationalVersion, SemVersionStyles.Strict);

private Uri? LatestDownloadUrl { get; set; }

private readonly ILogger<Updater> _logger;
private readonly ConfigManager _configManager;


public UpdateableVariable<bool> UpdateAvailable { get; } = new(false);
public Version? LatestVersion { get; private set; }
public UpdatableVariable<bool> UpdateAvailable { get; } = new(false);
public bool IsPostponed { get; private set; }
public SemVersion? LatestVersion { get; private set; }


public Updater(ILogger<Updater> logger, ConfigManager configManager)
Expand All @@ -51,7 +53,7 @@ private static bool TryDeleteFile(string fileName)
}
}

private async Task<(Version, GithubReleaseResponse.Asset)?> GetLatestRelease()
private async Task<(SemVersion, GithubReleaseResponse.Asset)?> GetLatestRelease()
{
_logger.LogInformation("Checking GitHub for updates...");

Expand All @@ -74,23 +76,21 @@ private static bool TryDeleteFile(string fileName)
}

var tagName = json.TagName;
if (!string.IsNullOrEmpty(tagName) && tagName[0] == 'v')
tagName = tagName[1..];

if (!Version.TryParse(tagName, out var version))
if (!SemVersion.TryParse(tagName, SemVersionStyles.AllowV, out var version))
{
_logger.LogWarning("Failed to parse version. Value: {Version}", json.TagName);
return null;
}

var asset = json.Assets.FirstOrDefault(x => x.Name == SetupFileName);
var asset = json.Assets.FirstOrDefault(x => x.Name.Equals(SetupFileName, StringComparison.InvariantCultureIgnoreCase));
if (asset == null)
{
_logger.LogWarning("Could not find asset with {@SetupName}. Assets found: {@Assets}", SetupFileName,
json.Assets);
return null;
}

return (version, asset);
}
catch (Exception e)
Expand All @@ -102,37 +102,39 @@ private static bool TryDeleteFile(string fileName)

public async Task CheckUpdate()
{
IsPostponed = false;
UpdateAvailable.Value = false;

var latestVersion = await GetLatestRelease();
if (latestVersion == null)
{
UpdateAvailable.Value = false;
return;
}
if (latestVersion.Value.Item1 <= _currentVersion)

var comparison = _currentVersion.ComparePrecedenceTo(latestVersion.Value.Item1);
if (comparison >= 0)
{
_logger.LogInformation("ShockOsc is up to date ([{Version}] >= [{LatestVersion}])", _currentVersion,
latestVersion.Value.Item1);
_logger.LogInformation("ShockOsc is up to date ([{Version}] >= [{LatestVersion}]) ({Comp})", _currentVersion,
latestVersion.Value.Item1, comparison);
UpdateAvailable.Value = false;
return;
}

UpdateAvailable.Value = true;
LatestVersion = latestVersion.Value.Item1;
LatestDownloadUrl = latestVersion.Value.Item2.BrowserDownloadUrl;
if (_configManager.Config.LastIgnoredVersion != null &&
_configManager.Config.LastIgnoredVersion >= latestVersion.Value.Item1)
if (_configManager.Config.LastIgnoredVersion != null && _configManager.Config.LastIgnoredVersion.ComparePrecedenceTo(latestVersion.Value.Item1) >= 0)
{
_logger.LogInformation(
"ShockOsc is not up to date. Skipping update due to previous postpone. You can reenable the updater by setting 'LastIgnoredVersion' to null");
UpdateAvailable.Value = false;
"ShockOsc is not up to date. Skipping update due to previous postpone");
IsPostponed = true;
return;
}

_logger.LogWarning(
"ShockOsc is not up to date. Newest version is [{NewVersion}] but you are on [{CurrentVersion}]!",
latestVersion.Value.Item1, _currentVersion);

UpdateAvailable.Value = true;
}

public async Task DoUpdate()
Expand Down
7 changes: 4 additions & 3 deletions ShockOsc/ShockOsc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@
<EnableDefaultCssItems>false</EnableDefaultCssItems>
<Nullable>enable</Nullable>
<ApplicationTitle>ShockOsc</ApplicationTitle>
<ApplicationId>com.companyname.shockosc</ApplicationId>
<ApplicationId>org.openshock.shockosc</ApplicationId>
<ApplicationIdGuid>2C147618-324E-4C37-B4B6-C50C8A9BD5ED</ApplicationIdGuid>
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
<ApplicationVersion>1</ApplicationVersion>
<AssemblyName>OpenShock.ShockOsc</AssemblyName>
<RootNamespace>OpenShock.ShockOsc</RootNamespace>
<Company>OpenShock</Company>
<AssemblyVersion>2.0.0</AssemblyVersion>
<Version>2.0.0-rc.2</Version>
<ApplicationIcon>Resources\openshock-icon.ico</ApplicationIcon>
<SelfContained>true</SelfContained>
<Product>ShockOsc</Product>
Expand All @@ -34,6 +33,7 @@

<ResourceLanguages>en</ResourceLanguages>
<SatelliteResourceLanguages>en-US;en</SatelliteResourceLanguages>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
Expand All @@ -60,6 +60,7 @@
<PackageReference Include="OpenShock.SDK.CSharp" Version="0.0.19" />
<PackageReference Include="OpenShock.SDK.CSharp.Hub" Version="0.0.19" />
<PackageReference Include="OpenShock.SDK.CSharp.Live" Version="0.0.20" />
<PackageReference Include="Semver" Version="2.3.0" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="8.0.4" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="8.0.0" />
Expand Down
6 changes: 6 additions & 0 deletions ShockOsc/Ui/Components/UpdateDialog.razor
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
await ConfigManager.SaveAsync();
MudDialog.Close(DialogResult.Ok(true));
}

private void Dismiss()
{
MudDialog.Close(DialogResult.Ok(true));
}

private void DownloadUpdate()
{
Expand Down Expand Up @@ -44,6 +49,7 @@
<DialogActions>
@if (!_isDownloading)
{
<MudButton Color="Color.Primary" OnClick="Dismiss">Dismiss</MudButton>
<MudButton Color="Color.Primary" OnClick="Skip">Skip</MudButton>
<MudButton Color="Color.Primary" OnClick="DownloadUpdate">Update</MudButton>
}
Expand Down
5 changes: 3 additions & 2 deletions ShockOsc/Ui/Components/UpdateLogout.razor
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
Updater.UpdateAvailable.OnValueChanged += v =>
{
InvokeAsync(StateHasChanged);
OpenUpdateDialog();
if (v && !Updater.IsPostponed) OpenUpdateDialog();
};

await Updater.CheckUpdate();
if (Updater.UpdateAvailable.Value && !Updater.IsPostponed) OpenUpdateDialog();

}

private async Task Logout()
Expand Down
22 changes: 0 additions & 22 deletions ShockOsc/Ui/Utils/UpdateableVariable.cs

This file was deleted.

0 comments on commit d2bfc31

Please sign in to comment.