12
12
using Zenith . Core ;
13
13
using System . Text ;
14
14
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 ;
17
18
18
19
namespace OpenLoco . ObjectEditor . Gui
19
20
{
@@ -79,9 +80,16 @@ int CurrentUIImagePageNumber
79
80
const int ImagesPerPage = 50 ;
80
81
81
82
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
+
82
88
string SettingsPath => Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . ApplicationData ) , ApplicationName ) ;
83
89
string SettingsFile => Path . Combine ( SettingsPath , "settings.json" ) ;
84
90
91
+ Version ApplicationVersion ;
92
+
85
93
public MainForm ( )
86
94
{
87
95
InitializeComponent ( ) ;
@@ -104,14 +112,60 @@ public MainForm()
104
112
model = new MainFormModel ( logger , SettingsFile , palette ) ;
105
113
}
106
114
115
+ // grab current appl version from assembly
107
116
var versionFilename = "Gui.version.txt" ;
108
117
using ( var stream = assembly . GetManifestResourceStream ( versionFilename ) )
109
118
{
110
119
var buf = new byte [ 5 ] ;
111
120
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 } ";
113
133
}
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" ) ;
114
153
}
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
+ //}
115
169
116
170
void MainForm_Load ( object sender , EventArgs e )
117
171
{
@@ -1438,4 +1492,9 @@ void tstbImageScaling_TextChanged(object sender, EventArgs e)
1438
1492
}
1439
1493
}
1440
1494
}
1495
+
1496
+ public class VersionCheckBody
1497
+ {
1498
+ public string tag_name { get ; set ; }
1499
+ }
1441
1500
}
0 commit comments