Skip to content

Commit 94517db

Browse files
authored
Auto updater (#210)
* skeleton code * progress * working and polish * remove debug stuff
1 parent 1a86eef commit 94517db

File tree

11 files changed

+329
-394
lines changed

11 files changed

+329
-394
lines changed

Gui/App.axaml

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,16 @@
3636
<Setter Property="BorderBrush" Value="{DynamicResource ButtonBackground}" />
3737
</Style>
3838
<Style Selector="Button.danger">
39-
<Setter Property="Background" Value="{DynamicResource DangerButtonBackgroundBrush}"/>
40-
<Setter Property="Foreground" Value="{DynamicResource DangerButtonForegroundBrush}"/>
39+
<Setter Property="Background" Value="{DynamicResource DangerBackgroundBrush}"/>
40+
<Setter Property="Foreground" Value="{DynamicResource DangerForegroundBrush}"/>
41+
</Style>
42+
<Style Selector="Button.update">
43+
<Setter Property="Background" Value="{DynamicResource UpdateBackgroundBrush}"/>
44+
<Setter Property="Foreground" Value="{DynamicResource UpdateForegroundBrush}"/>
45+
</Style>
46+
<Style Selector="Button.information">
47+
<Setter Property="Background" Value="{DynamicResource InformationBackgroundBrush}"/>
48+
<Setter Property="Foreground" Value="{DynamicResource InformationForegroundBrush}"/>
4149
</Style>
4250
<Style Selector="TextBox">
4351
<Setter Property="BorderBrush" Value="{DynamicResource ButtonBackground}" />
@@ -127,17 +135,25 @@
127135
<ResourceDictionary>
128136
<ResourceDictionary.ThemeDictionaries>
129137
<ResourceDictionary x:Key='Light'>
130-
<SolidColorBrush x:Key='DangerButtonBackgroundBrush'>Crimson</SolidColorBrush>
131-
<SolidColorBrush x:Key='DangerButtonForegroundBrush'>White</SolidColorBrush>
138+
<SolidColorBrush x:Key='DangerBackgroundBrush'>Crimson</SolidColorBrush>
139+
<SolidColorBrush x:Key='DangerForegroundBrush'>White</SolidColorBrush>
140+
<SolidColorBrush x:Key='UpdateBackgroundBrush'>ForestGreen</SolidColorBrush>
141+
<SolidColorBrush x:Key='UpdateForegroundBrush'>White</SolidColorBrush>
142+
<SolidColorBrush x:Key='InformationBackgroundBrush'>RoyalBlue</SolidColorBrush>
143+
<SolidColorBrush x:Key='InformationForegroundBrush'>White</SolidColorBrush>
132144
<SolidColorBrush x:Key='Shadow' Color='#0A0908' />
133145
<SolidColorBrush x:Key='Dark' Color='#22333B' />
134146
<SolidColorBrush x:Key='Neutral' Color='#EAE0D5' />
135147
<SolidColorBrush x:Key='Light' Color='#C6AC8F' />
136148
<SolidColorBrush x:Key='Highlight' Color='#5E503F' />
137149
</ResourceDictionary>
138150
<ResourceDictionary x:Key='Dark'>
139-
<SolidColorBrush x:Key='DangerButtonBackgroundBrush'>Crimson</SolidColorBrush>
140-
<SolidColorBrush x:Key='DangerButtonForegroundBrush'>White</SolidColorBrush>
151+
<SolidColorBrush x:Key='DangerBackgroundBrush'>Crimson</SolidColorBrush>
152+
<SolidColorBrush x:Key='DangerForegroundBrush'>White</SolidColorBrush>
153+
<SolidColorBrush x:Key='UpdateBackgroundBrush'>ForestGreen</SolidColorBrush>
154+
<SolidColorBrush x:Key='UpdateForegroundBrush'>White</SolidColorBrush>
155+
<SolidColorBrush x:Key='InformationBackgroundBrush'>RoyalBlue</SolidColorBrush>
156+
<SolidColorBrush x:Key='InformationForegroundBrush'>White</SolidColorBrush>
141157
<SolidColorBrush x:Key='Shadow' Color='#5E503F' />
142158
<SolidColorBrush x:Key='Dark' Color='#C6AC8F' />
143159
<SolidColorBrush x:Key='Neutral' Color='#EAE0D5' />

Gui/Gui.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
<ItemGroup>
6969
<ProjectReference Include="..\Common\Common.csproj" />
7070
<ProjectReference Include="..\Definitions\Definitions.csproj" />
71+
<ProjectReference Include="..\GuiUpdater\GuiUpdater.csproj" />
7172
<ProjectReference Include="..\Index\Index.csproj" />
7273
</ItemGroup>
7374

Gui/PlatformSpecific.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ public static void FolderOpenInDesktop(string directory, ILogger logger, string?
5555
}
5656
}
5757

58+
public static string EditorPlatformExtension
59+
=> RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "win-x64.zip" :
60+
RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? "osx-x64.tar" :
61+
RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "linux-x64.tar" :
62+
"unknown";
63+
5864
static void FolderOpenInDesktopCore(string directory, string? filename = null)
5965
{
6066
if (!Directory.Exists(directory))

Gui/VersionHelpers.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
using Common.Logging;
12
using NuGet.Versioning;
3+
using System;
24
using System.Diagnostics;
35
using System.IO;
46
using System.Reflection;
@@ -17,13 +19,62 @@ namespace Gui;
1719
public static class VersionHelpers
1820
{
1921
public const string GithubApplicationName = "ObjectEditor";
22+
public const string ObjectEditorUpdaterName = "ObjectEditorUpdater";
2023
public const string GithubIssuePage = "https://github.com/OpenLoco/ObjectEditor/issues";
2124
public const string GithubLatestReleaseDownloadPage = "https://github.com/OpenLoco/ObjectEditor/releases";
2225
public const string GithubLatestReleaseAPI = "https://api.github.com/repos/OpenLoco/ObjectEditor/releases/latest";
2326

27+
// todo: instead of going to downloads, start the auto-updater (ObjectEditorUpdater.exe) with the right args
2428
public static Process? OpenDownloadPage()
2529
=> Process.Start(new ProcessStartInfo(GithubLatestReleaseDownloadPage) { UseShellExecute = true });
2630

31+
public static void StartAutoUpdater(ILogger logger, SemanticVersion latestVersion)
32+
{
33+
try
34+
{
35+
// kill any existing processes of the updater
36+
foreach (var existingProcess in Process.GetProcessesByName(ObjectEditorUpdaterName))
37+
{
38+
try
39+
{
40+
existingProcess.Kill();
41+
existingProcess.WaitForExit();
42+
}
43+
catch (Exception ex)
44+
{
45+
logger.Error(ex, "Failed to kill existing ObjectEditorUpdater process.");
46+
}
47+
}
48+
49+
// win: object-editor-5.3.5-win-x64.zip
50+
// osx: object-editor-5.3.5-osx-x64.tar
51+
// linux: object-editor-5.3.5-linux-x64.tar
52+
var platform = PlatformSpecific.EditorPlatformExtension;
53+
var filename = $"object-editor-{latestVersion}-{platform}";
54+
55+
var startInfo = new ProcessStartInfo($"{ObjectEditorUpdaterName}.exe",
56+
[
57+
"--pid",
58+
$"{Environment.ProcessId}",
59+
"--url",
60+
$"{GithubLatestReleaseDownloadPage}/download/{latestVersion}/{filename}",
61+
"--app-path",
62+
$"{Environment.ProcessPath}",
63+
])
64+
{
65+
UseShellExecute = false,
66+
CreateNoWindow = true,
67+
};
68+
69+
var process = Process.Start(startInfo);
70+
Environment.Exit(0);
71+
}
72+
catch (Exception ex)
73+
{
74+
Debug.WriteLine($"Failed to start auto-updater: {ex}");
75+
}
76+
}
77+
2778
public static SemanticVersion GetCurrentAppVersion()
2879
{
2980
var assembly = Assembly.GetExecutingAssembly();

Gui/ViewModels/MainWindowViewModel.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,13 @@ public class MainWindowViewModel : ViewModelBase
5353
public ReactiveCommand<Unit, Unit> EditSettingsCommand { get; }
5454
public ReactiveCommand<Unit, Unit> ShowLogsCommand { get; }
5555
public ReactiveCommand<Unit, Process?> OpenDownloadLink { get; }
56+
public ReactiveCommand<Unit, Unit> DownloadLatestUpdate { get; }
5657

5758
public string WindowTitle => $"{ObjectEditorModel.ApplicationName} - {ApplicationVersion} ({LatestVersionText})";
5859

5960
[Reactive]
6061
public SemanticVersion ApplicationVersion { get; set; }
62+
SemanticVersion? LatestVersion { get; set; }
6163

6264
[Reactive]
6365
public string LatestVersionText { get; set; } = "Development build";
@@ -125,6 +127,13 @@ public MainWindowViewModel()
125127
});
126128

127129
OpenDownloadLink = ReactiveCommand.Create(VersionHelpers.OpenDownloadPage);
130+
DownloadLatestUpdate = ReactiveCommand.Create(() =>
131+
{
132+
if (LatestVersion != null)
133+
{
134+
var t = Task.Run(() => VersionHelpers.StartAutoUpdater(Model.Logger, LatestVersion));
135+
}
136+
});
128137

129138
#region Version
130139

@@ -138,10 +147,10 @@ public MainWindowViewModel()
138147
#if !DEBUG
139148
try
140149
{
141-
var latestVersion = VersionHelpers.GetLatestAppVersion(ApplicationVersion);
142-
if (latestVersion > ApplicationVersion)
150+
LatestVersion = VersionHelpers.GetLatestAppVersion(ApplicationVersion);
151+
if (LatestVersion > ApplicationVersion)
143152
{
144-
LatestVersionText = $"newer version exists: {latestVersion}";
153+
LatestVersionText = $"newer version exists: {LatestVersion}";
145154
IsUpdateAvailable = true;
146155
}
147156
else

0 commit comments

Comments
 (0)