Add update check

This commit is contained in:
Cuyler36 2018-08-29 20:00:33 -04:00
parent 6faf1f0bea
commit 03d446db55
5 changed files with 125 additions and 1 deletions

3
.gitignore vendored
View file

@ -8,4 +8,5 @@ bin/
.vs/
obj/
*.DotSettings
*.user
*.user
packages/

View file

@ -54,6 +54,9 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
@ -82,6 +85,7 @@
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>ItemColorSettings.Designer.cs</LastGenOutput>
</None>
<None Include="packages.config" />
<None Include="Resources\Animal_Crossing_NL_Badges_28x28.png" />
<None Include="Resources\Animal_Crossing_NL_NoBadge_28x28.png" />
<None Include="Resources\BoxGlow.png" />
@ -4894,6 +4898,7 @@
<Compile Include="Classes\Saves\SaveBase.cs" />
<Compile Include="Classes\Saves\WiiSave.cs" />
<Compile Include="Classes\TrainStation.cs" />
<Compile Include="Classes\Updater.cs" />
<Compile Include="Classes\Utilities\ImageUtility.cs" />
<Compile Include="Classes\Villagers\SimpleVillager.cs" />
<Compile Include="Classes\Villagers\Villager.cs" />

104
Classes/Updater.cs Normal file
View file

@ -0,0 +1,104 @@
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Reflection;
using Newtonsoft.Json.Linq;
namespace ACSE
{
internal sealed class Updater
{
private int _versionMajor;
private int _versionMinor;
private int _versionRevision;
public string UpdateUrl { get; private set; }
private void SetCurrentVersionInfo()
{
var versionInfo = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location);
_versionMajor = versionInfo.FileMajorPart;
_versionMinor = versionInfo.FileMinorPart;
_versionRevision = versionInfo.ProductBuildPart;
}
public Updater()
{
SetCurrentVersionInfo();
}
public string GetVersion() => $"{_versionMajor}.{_versionMinor}.{_versionRevision}";
private (bool, string) CheckForUpdate(string url)
{
// Check the "latest" release first.
var request = (HttpWebRequest) WebRequest.Create(url);
request.Credentials = CredentialCache.DefaultCredentials;
request.ContentType = "application/json";
request.Method = "GET";
request.Accept = "application/json";
request.UserAgent = "ACSE";
// Read response.
string content;
try
{
var response = (HttpWebResponse) request.GetResponse();
using (var contentStream = response.GetResponseStream())
{
if (contentStream == null) return (false, null);
using (var reader = new StreamReader(contentStream))
{
content = reader.ReadToEnd();
}
}
}
catch
{
return (false, null);
}
// Parse JSON.
JObject jObj;
switch (JToken.Parse(content))
{
case JArray jArray:
jObj = (JObject) jArray[0];
break;
case JObject jObject:
jObj = jObject;
break;
default:
return (false, null);
}
var version = (string) jObj["tag_name"];
var updateUrl = (string) jObj["html_url"];
try
{
var versionInfo = version.Split('.');
var updateMajor = int.Parse(versionInfo[0]);
var updateMinor = int.Parse(versionInfo[1]);
var updateRevision = int.Parse(versionInfo[2]);
return (updateMajor > _versionMajor || updateMinor > _versionMinor || updateRevision > _versionRevision, updateUrl);
}
catch
{
return (false, null);
}
}
public bool HasUpdate()
{
var (latestResult, latestUrl) = CheckForUpdate(@"https://api.github.com/repos/cuyler36/ACSE/releases/latest");
UpdateUrl = latestUrl;
if (latestResult) return true;
var (releasesResult, releaseUrl) = CheckForUpdate(@"https://api.github.com/repos/cuyler36/ACSE/releases");
UpdateUrl = releaseUrl;
return releasesResult;
}
}
}

View file

@ -260,6 +260,16 @@ public MainForm()
// Palette Change Buttons
paletteNextButton.MouseClick += (sender, e) => ChangePatternPalette(1);
palettePreviousButton.MouseClick += (sender, e) => ChangePatternPalette(-1);
// Check for updates
var updater = new Updater();
if (updater.HasUpdate() &&
MessageBox.Show(
"An updated version of ACSE is available! Would you like to be taken to the download page?",
"ACSE Update", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
System.Diagnostics.Process.Start(updater.UpdateUrl);
}
}
#region Settings Changing Functions

4
packages.config Normal file
View file

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="11.0.2" targetFramework="net47" />
</packages>