Use assembly version as the current version (#2164)

* Put assembly version in window title
* Treat version.txt as a Version
* Cache current version
* Don't use resources anymore for version
* Re-add version.txt, for backwards compatibility
* Set old version.txt's build action to None
* Use GitHub API for version checking
This commit is contained in:
Evan Dixon
2018-12-10 22:36:18 -06:00
committed by Kurt
parent efb9b7eba2
commit 34f4fb176c
9 changed files with 67 additions and 41 deletions

View File

@@ -7,6 +7,7 @@
using System.IO;
using System.Linq;
using System.Media;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
@@ -20,6 +21,8 @@ namespace PKHeX.WinForms
{
public partial class Main : Form
{
private static readonly Version CurrentProgramVersion = Assembly.GetExecutingAssembly().GetName().Version;
public Main()
{
new Task(() => new SplashScreen().ShowDialog()).Start();
@@ -103,7 +106,6 @@ private set
private static readonly string TemplatePath = Path.Combine(WorkingDirectory, "template");
private static readonly string PluginPath = Path.Combine(WorkingDirectory, "plugins");
private const string ThreadPath = "https://projectpokemon.org/pkhex/";
private const string VersionPath = "https://raw.githubusercontent.com/kwsch/PKHeX/master/PKHeX.WinForms/Resources/text/version.txt";
#endregion
@@ -235,20 +237,25 @@ private void FormLoadInitialFiles(string[] args)
private void FormLoadCheckForUpdates()
{
L_UpdateAvailable.Click += (sender, e) => Process.Start(ThreadPath);
new Task(() =>
Task.Run(() =>
{
string data = NetUtil.GetStringFromURL(VersionPath);
if (data == null)
return;
if (int.TryParse(data, out var upd) && int.TryParse(Resources.ProgramVersion, out var cur) && upd <= cur)
return;
Invoke((MethodInvoker)(() =>
try
{
L_UpdateAvailable.Visible = true;
L_UpdateAvailable.Text = $"{MsgProgramUpdateAvailable} {upd:d}";
}));
}).Start();
var latestVersion = NetUtil.GetLatestPKHeXVersion();
if (latestVersion == null || latestVersion <= CurrentProgramVersion)
return;
Invoke((MethodInvoker)(() =>
{
L_UpdateAvailable.Visible = true;
L_UpdateAvailable.Text = $"{MsgProgramUpdateAvailable} {latestVersion.ToString(3)}";
}));
}
catch (Exception ex)
{
Debug.WriteLine($"Exception while checking for latest version: {ex}");
}
});
}
private void FormLoadConfig(out bool BAKprompt, out bool showChangelog)
@@ -271,16 +278,15 @@ private void FormLoadConfig(out bool BAKprompt, out bool showChangelog)
// Version Check
if (Settings.Version.Length > 0) // already run on system
{
int.TryParse(Settings.Version, out int lastrev);
int.TryParse(Resources.ProgramVersion, out int currrev);
showChangelog = lastrev < currrev;
Version.TryParse(Settings.Version, out Version lastrev);
showChangelog = lastrev < CurrentProgramVersion;
}
// BAK Prompt
if (!Settings.BAKPrompt)
BAKprompt = Settings.BAKPrompt = true;
Settings.Version = Resources.ProgramVersion;
Settings.Version = CurrentProgramVersion.ToString();
}
private void FormLoadPlugins()
@@ -762,13 +768,8 @@ private void ResetSAVPKMEditors(SaveFile sav)
private static string GetProgramTitle()
{
#if DEBUG
var d = File.GetLastWriteTime(System.Reflection.Assembly.GetEntryAssembly().Location);
string date = $"d-{d:yyyyMMdd}";
#else
string date = Resources.ProgramVersion;
#endif
return $"PKH{(HaX ? "a" : "e")}X ({date})";
string version = CurrentProgramVersion.ToString(3);
return $"PKH{(HaX ? "a" : "e")}X ({version})";
}
private static string GetProgramTitle(SaveFile sav)

View File

@@ -878,6 +878,7 @@
</ItemGroup>
<ItemGroup>
<Content Include="icon.ico" />
<None Include="Resources\text\version.txt" />
<None Include="Resources\img\Shiny Pokemon Sprites\25-8ps.png" />
<None Include="Resources\img\Shiny Pokemon Sprites\25-1ps.png" />
<None Include="Resources\img\Shiny Pokemon Sprites\133-1ps.png" />
@@ -1990,7 +1991,6 @@
<EmbeddedResource Include="Resources\byte\fashion_m_sm_illegal" />
<None Include="Resources\text\shortcuts.txt" />
<None Include="Resources\text\changelog.txt" />
<None Include="Resources\text\version.txt" />
<EmbeddedResource Include="Resources\byte\PGLDings-NormalRegular.ttf" />
<None Include="Resources\icon.ico" />
<None Include="Resources\img\warn.png" />

View File

@@ -32,6 +32,6 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("18.12.02.0")]
[assembly: AssemblyFileVersion("18.12.02.0")]
[assembly: NeutralResourcesLanguageAttribute("en")]

View File

@@ -28288,15 +28288,6 @@ internal class Resources {
}
}
/// <summary>
/// Looks up a localized string similar to 20181202.
/// </summary>
internal static string ProgramVersion {
get {
return ResourceManager.GetString("ProgramVersion", resourceCulture);
}
}
/// <summary>
/// Looks up a localized resource of type System.Drawing.Bitmap.
/// </summary>

View File

@@ -5926,9 +5926,6 @@
<data name="pgldings_normalregular" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\byte\PGLDings-NormalRegular.ttf;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="ProgramVersion" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\text\version.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
</data>
<data name="changelog" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\text\changelog.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
</data>

View File

@@ -1 +1 @@
20181202
20181202

View File

@@ -3,16 +3,23 @@
using System.Drawing;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
namespace PKHeX.WinForms
{
public static class NetUtil
{
private static Regex LatestGitTagRegex = new Regex("\\\"tag_name\"\\s*\\:\\s*\\\"([0-9]+\\.[0-9]+\\.[0-9]+)\\\""); // Match `"tag_name": "18.12.02"`. Group 1 is `18.12.02`
public static string GetStringFromURL(string webURL)
{
try
{
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(webURL);
// The GitHub API will fail if no user agent is provided
httpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36";
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
var reader = new StreamReader(httpWebResponse.GetResponseStream());
return reader.ReadToEnd();
@@ -39,5 +46,34 @@ public static Image GetImageFromURL(string webURL)
return null;
}
}
/// <summary>
/// Gets the latest version of PKHeX according to the Github API
/// </summary>
/// <returns>A version representing the latest available version of PKHeX, or null if the latest version could not be determined</returns>
public static Version GetLatestPKHeXVersion()
{
var apiEndpoint = "https://api.github.com/repos/kwsch/pkhex/releases/latest";
var responseJson = GetStringFromURL(apiEndpoint);
if (string.IsNullOrEmpty(responseJson))
{
return null;
}
// Using a regex to get the tag to avoid importing an entire JSON parsing library
var tagMatch = LatestGitTagRegex.Match(responseJson);
if (!tagMatch.Success)
{
return null;
}
var tagString = tagMatch.Groups[1].Value;
if (!Version.TryParse(tagString, out var latestVersion))
{
return null;
}
return latestVersion;
}
}
}

View File

@@ -15,7 +15,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PKHeX.WinForms", "PKHeX.Win
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PKHeX.Core", "PKHeX.Core\PKHeX.Core.csproj", "{279E59F2-50EA-475D-8BA4-FA69F0578C0D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PKHeX.Core.Tests", "Tests\PKHeX.Core.Tests\PKHeX.Core.Tests.csproj", "{C3B5B74F-ACE8-4FB2-A917-0DEDBFD5703B}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PKHeX.Core.Tests", "Tests\PKHeX.Core.Tests\PKHeX.Core.Tests.csproj", "{C3B5B74F-ACE8-4FB2-A917-0DEDBFD5703B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution

1
version.txt Normal file
View File

@@ -0,0 +1 @@
18.11.01