2016-08-31 03:31:14 +00:00
using System ;
2017-07-02 02:43:51 +00:00
using System.Diagnostics ;
2016-08-31 03:31:14 +00:00
using System.Drawing ;
using System.IO ;
using System.Net ;
2018-12-11 04:36:18 +00:00
using System.Text.RegularExpressions ;
2016-08-31 03:31:14 +00:00
2017-02-01 03:35:18 +00:00
namespace PKHeX.WinForms
2016-08-31 03:31:14 +00:00
{
2017-02-04 16:18:48 +00:00
public static class NetUtil
2016-08-31 03:31:14 +00:00
{
2019-01-26 00:51:58 +00:00
private static readonly 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`
2017-06-18 01:37:19 +00:00
public static string GetStringFromURL ( string webURL )
2016-08-31 03:31:14 +00:00
{
try
{
2019-01-26 00:51:58 +00:00
var stream = GetStreamFromURL ( webURL ) ;
2019-02-04 04:28:03 +00:00
using ( var reader = new StreamReader ( stream ) )
return reader . ReadToEnd ( ) ;
2016-08-31 03:31:14 +00:00
}
catch ( Exception e )
{
2017-07-02 02:43:51 +00:00
Debug . WriteLine ( e . Message ) ;
2016-08-31 03:31:14 +00:00
return null ;
}
}
2018-07-27 02:34:27 +00:00
2019-01-26 00:51:58 +00:00
private static Stream GetStreamFromURL ( string webURL )
{
var 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" ;
var httpWebResponse = httpWebRequest . GetResponse ( ) ;
return httpWebResponse . GetResponseStream ( ) ;
}
2017-06-18 01:37:19 +00:00
public static Image GetImageFromURL ( string webURL )
2016-08-31 03:31:14 +00:00
{
try
{
2019-01-26 00:51:58 +00:00
var stream = GetStreamFromURL ( webURL ) ;
2016-08-31 03:31:14 +00:00
return stream ! = null ? Image . FromStream ( stream ) : null ;
}
catch ( Exception e )
{
2017-07-02 02:43:51 +00:00
Debug . WriteLine ( e . Message ) ;
2016-08-31 03:31:14 +00:00
return null ;
}
}
2019-01-26 00:51:58 +00:00
2018-12-11 04:36:18 +00:00
/// <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 ( )
{
2019-01-26 00:51:58 +00:00
const string apiEndpoint = "https://api.github.com/repos/kwsch/pkhex/releases/latest" ;
2018-12-11 04:36:18 +00:00
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 ;
2019-01-26 00:51:58 +00:00
var tagString = tagMatch . Groups [ 1 ] . Value ;
return ! Version . TryParse ( tagString , out var latestVersion ) ? null : latestVersion ;
2018-12-11 04:36:18 +00:00
}
2016-08-31 03:31:14 +00:00
}
}