Skip to content

Commit e4884da

Browse files
committed
add 'new version' checking to notify users of new versions
1 parent 6c39ccf commit e4884da

File tree

1 file changed

+62
-3
lines changed

1 file changed

+62
-3
lines changed

Gui/MainForm.cs

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
using Zenith.Core;
1313
using System.Text;
1414
using SixLabors.ImageSharp.PixelFormats;
15-
using SixLabors.ImageSharp.Formats;
16-
using System.IO;
15+
using System.Net.Http.Headers;
16+
using System.Text.Json;
17+
using System.Diagnostics;
1718

1819
namespace OpenLoco.ObjectEditor.Gui
1920
{
@@ -79,9 +80,16 @@ int CurrentUIImagePageNumber
7980
const int ImagesPerPage = 50;
8081

8182
const string ApplicationName = "OpenLoco Object Editor";
83+
84+
const string GithubApplicationName = "ObjectEditor";
85+
const string GithubLatestReleaseDownloadPage = @"https://github.com/OpenLoco/ObjectEditor/releases";
86+
const string GithubLatestReleaseAPI = @"https://api.github.com/repos/OpenLoco/ObjectEditor/releases/latest";
87+
8288
string SettingsPath => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), ApplicationName);
8389
string SettingsFile => Path.Combine(SettingsPath, "settings.json");
8490

91+
Version ApplicationVersion;
92+
8593
public MainForm()
8694
{
8795
InitializeComponent();
@@ -104,14 +112,60 @@ public MainForm()
104112
model = new MainFormModel(logger, SettingsFile, palette);
105113
}
106114

115+
// grab current appl version from assembly
107116
var versionFilename = "Gui.version.txt";
108117
using (var stream = assembly.GetManifestResourceStream(versionFilename))
109118
{
110119
var buf = new byte[5];
111120
var arr = stream!.Read(buf);
112-
Text = $"{ApplicationName} - {Encoding.ASCII.GetString(buf)}";
121+
ApplicationVersion = Version.Parse(Encoding.ASCII.GetString(buf));
122+
}
123+
124+
var latestVersionText = "up-to-date";
125+
126+
// check for new version
127+
var latestVersion = GetLatestVersion();
128+
if (latestVersion > ApplicationVersion)
129+
{
130+
_ = MessageBox.Show($"Current Version: {ApplicationVersion}{System.Environment.NewLine}Latest version: {latestVersion}{System.Environment.NewLine}Taking you to the downloads page now ");
131+
_ = Process.Start(new ProcessStartInfo { FileName = GithubLatestReleaseDownloadPage, UseShellExecute = true });
132+
latestVersionText = $"newer version exists: {latestVersion}";
113133
}
134+
135+
Text = $"{ApplicationName} - {ApplicationVersion} ({latestVersionText})";
136+
}
137+
138+
// thanks for this one @IntelOrca, https://github.com/IntelOrca/PeggleEdit/blob/master/src/peggleedit/Forms/MainMDIForm.cs#L848-L861
139+
Version GetLatestVersion()
140+
{
141+
var client = new HttpClient();
142+
client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(GithubApplicationName, ApplicationVersion.ToString()));
143+
var response = client.GetAsync(GithubLatestReleaseAPI).Result;
144+
if (response.IsSuccessStatusCode)
145+
{
146+
var jsonResponse = response.Content.ReadAsStringAsync().Result;
147+
var body = JsonSerializer.Deserialize<VersionCheckBody>(jsonResponse);
148+
var tagName = body?.tag_name;
149+
var version = Version.Parse(tagName);
150+
return version;
151+
}
152+
throw new Exception("Unable to get latest version");
114153
}
154+
//async Task<Version> GetLatestVersionAsync()
155+
//{
156+
// var client = new HttpClient();
157+
// client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("ObjectEditor", ApplicationVersion.ToString()));
158+
// var response = await client.GetAsync("https://api.github.com/repos/OpenLoco/ObjectEditor/releases/latest");
159+
// if (response.IsSuccessStatusCode)
160+
// {
161+
// var jsonResponse = await response.Content.ReadAsStringAsync();
162+
// var body = JsonSerializer.Deserialize<VersionCheckBody>(jsonResponse);
163+
// var tagName = body?.tag_name;
164+
// var version = Version.Parse(tagName);
165+
// return version;
166+
// }
167+
// throw new Exception("Unable to get latest version");
168+
//}
115169

116170
void MainForm_Load(object sender, EventArgs e)
117171
{
@@ -1438,4 +1492,9 @@ void tstbImageScaling_TextChanged(object sender, EventArgs e)
14381492
}
14391493
}
14401494
}
1495+
1496+
public class VersionCheckBody
1497+
{
1498+
public string tag_name { get; set; }
1499+
}
14411500
}

0 commit comments

Comments
 (0)