From a7323ef484a901f005d168fee2b05a0d70455909 Mon Sep 17 00:00:00 2001 From: Kurt Date: Fri, 25 Jan 2019 16:51:58 -0800 Subject: [PATCH] Misc updates Reuse some code in netutil for fetching stream (provide useragent for all uses) make sav4ranch use dynamicly read offsets to handle both variants (quicker) #2248 --- PKHeX.Core/Saves/Storage/SAV4Ranch.cs | 14 ++------- PKHeX.WinForms/Util/NetUtil.cs | 45 ++++++++++++--------------- 2 files changed, 23 insertions(+), 36 deletions(-) diff --git a/PKHeX.Core/Saves/Storage/SAV4Ranch.cs b/PKHeX.Core/Saves/Storage/SAV4Ranch.cs index d48f7fab8..f295704ad 100644 --- a/PKHeX.Core/Saves/Storage/SAV4Ranch.cs +++ b/PKHeX.Core/Saves/Storage/SAV4Ranch.cs @@ -6,9 +6,6 @@ namespace PKHeX.Core { public sealed class SAV4Ranch : BulkStorage { - private const int SIZE_MII = 0x28; - private const int SIZE_MIILINK = 0x2C; - public override int SIZE_STORED => 0x88 + 0x1C; protected override int SIZE_PARTY => SIZE_STORED; @@ -28,7 +25,7 @@ public sealed class SAV4Ranch : BulkStorage public SAV4Ranch(byte[] data) : base(data, typeof(PK4), 0) { Personal = PersonalTable.Pt; - Version = GameVersion.DPPt; + Version = Data.Length == SaveUtil.SIZE_G4RANCH_PLAT ? GameVersion.Pt : GameVersion.DP; HeldItems = Legal.HeldItems_Pt; OT = GetString(0x770, 0x12); @@ -57,18 +54,13 @@ public SAV4Ranch(byte[] data) : base(data, typeof(PK4), 0) * uint32_t name4; */ - var miiCountOffset = Data.Length == SaveUtil.SIZE_G4RANCH_PLAT ? 0x268C : 0x22AC; - var miiCount = BigEndian.ToInt32(Data, miiCountOffset); - var miiLinkCountOffset = miiCountOffset + 4 + (SIZE_MII * miiCount) + 4; - var miiLinkCount = BigEndian.ToInt32(Data, miiLinkCountOffset); - var pkCountOffset = miiLinkCountOffset + 4 + (SIZE_MIILINK * miiLinkCount) + 4; - + var pkCountOffset = BigEndian.ToInt32(Data, 0x34) + 4; SlotCount = BigEndian.ToInt32(Data, pkCountOffset); BoxCount = (int)Math.Ceiling((decimal)SlotCount / SlotsPerBox); Box = pkCountOffset + 4; - FinalCountOffset = pkCountOffset + 4 + (SIZE_STORED * SlotCount); + FinalCountOffset = BigEndian.ToInt32(Data, 0x3C); FinalCount = BigEndian.ToInt32(Data, FinalCountOffset); } diff --git a/PKHeX.WinForms/Util/NetUtil.cs b/PKHeX.WinForms/Util/NetUtil.cs index 35dc66066..85651ac14 100644 --- a/PKHeX.WinForms/Util/NetUtil.cs +++ b/PKHeX.WinForms/Util/NetUtil.cs @@ -9,19 +9,14 @@ 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` - + 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` + 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()); + var stream = GetStreamFromURL(webURL); + var reader = new StreamReader(stream); return reader.ReadToEnd(); } catch (Exception e) @@ -31,13 +26,22 @@ public static string GetStringFromURL(string webURL) } } + 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(); + } + public static Image GetImageFromURL(string webURL) { try { - HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(webURL); - HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); - Stream stream = httpWebResponse.GetResponseStream(); + var stream = GetStreamFromURL(webURL); return stream != null ? Image.FromStream(stream) : null; } catch (Exception e) @@ -46,34 +50,25 @@ public static Image GetImageFromURL(string webURL) return null; } } - + /// /// Gets the latest version of PKHeX according to the Github API /// /// A version representing the latest available version of PKHeX, or null if the latest version could not be determined public static Version GetLatestPKHeXVersion() { - var apiEndpoint = "https://api.github.com/repos/kwsch/pkhex/releases/latest"; + const string 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; + var tagString = tagMatch.Groups[1].Value; + return !Version.TryParse(tagString, out var latestVersion) ? null : latestVersion; } } }