2023-03-28 18:29:29 +00:00
|
|
|
using System;
|
2021-02-21 23:01:28 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
public static class UpdateUtil
|
2021-02-21 23:01:28 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
2023-12-04 04:13:20 +00:00
|
|
|
/// Gets the latest version of PKHeX according to the GitHub API
|
2022-06-18 18:04:24 +00:00
|
|
|
/// </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()
|
2021-02-21 23:01:28 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
const string apiEndpoint = "https://api.github.com/repos/kwsch/pkhex/releases/latest";
|
|
|
|
var responseJson = NetUtil.GetStringFromURL(new Uri(apiEndpoint));
|
|
|
|
if (responseJson is null)
|
|
|
|
return null;
|
2021-02-21 23:01:28 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Parse it manually; no need to parse the entire json to object.
|
|
|
|
const string tag = "tag_name";
|
|
|
|
var index = responseJson.IndexOf(tag, StringComparison.Ordinal);
|
|
|
|
if (index == -1)
|
|
|
|
return null;
|
2021-05-15 19:39:37 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
var first = responseJson.IndexOf('"', index + tag.Length + 1) + 1;
|
|
|
|
if (first == 0)
|
|
|
|
return null;
|
|
|
|
var second = responseJson.IndexOf('"', first);
|
|
|
|
if (second == -1)
|
|
|
|
return null;
|
2021-02-21 23:01:28 +00:00
|
|
|
|
2023-03-28 18:29:29 +00:00
|
|
|
var tagString = responseJson.AsSpan()[first..second];
|
2022-06-18 18:04:24 +00:00
|
|
|
return !Version.TryParse(tagString, out var latestVersion) ? null : latestVersion;
|
2021-02-21 23:01:28 +00:00
|
|
|
}
|
|
|
|
}
|