From b96ffd85e278f1a80168be89f7abb6e2b6a3beb4 Mon Sep 17 00:00:00 2001 From: brliron Date: Wed, 7 Apr 2021 19:23:28 +0200 Subject: [PATCH] Download games list from thpatch.net --- App.config | 8 ++++ GamesList.cs | 75 +++++++++++++++++++++++++++++++++--- MainWindow.xaml | 11 +++++- MainWindow.xaml.cs | 10 ++++- StandaloneGeneratorV3.csproj | 3 ++ packages.config | 1 + 6 files changed, 100 insertions(+), 8 deletions(-) diff --git a/App.config b/App.config index 98b176b..44b3dcd 100644 --- a/App.config +++ b/App.config @@ -9,6 +9,14 @@ + + + + + + + + \ No newline at end of file diff --git a/GamesList.cs b/GamesList.cs index 76e5819..2560161 100644 --- a/GamesList.cs +++ b/GamesList.cs @@ -1,9 +1,15 @@ -using System; +using HtmlAgilityPack; +using System; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.IO; using System.Linq; +using System.Net; using System.Text; +using System.Text.Json; +using System.Text.Json.Serialization; using System.Threading.Tasks; +using System.Windows.Media.Imaging; namespace StandaloneGeneratorV3 { @@ -11,6 +17,22 @@ class Game { public string Id { get; set; } public string Name { get; set; } + [JsonIgnore] + public string ImagePath { get => AppContext.BaseDirectory + @".\res\icon\" + Id + ".png"; } + private BitmapImage _image; + [JsonIgnore] + public BitmapImage Image { get { + if (_image == null) + { + _image = new BitmapImage(); + _image.BeginInit(); + _image.UriSource = new Uri(ImagePath); + _image.CacheOption = BitmapCacheOption.OnLoad; + _image.EndInit(); + } + return _image; + } } + [JsonIgnore] public bool IsSelected { get; set; } public Game(string id, string name) { @@ -21,13 +43,56 @@ public Game(string id, string name) } class GamesList { + public static readonly string BaseURL = "https://www.thpatch.net/"; public static List Load() { - return new List() + try { - new Game("th06", "Embodiment of Scarlet Devil"), - new Game("th18", "Unconnected Marketeers") - }; + return JsonSerializer.Deserialize>(File.ReadAllText("res\\games_list.js")); + } + catch + { + return new List(); + } + } + + private static async Task GameDomToObject(WebClient webClient, HtmlNode gameDom) + { + string id; + string name; + string iconUrl; + + HtmlNode titleNode = gameDom.SelectSingleNode("td[1]/a[2]"); + id = titleNode.Attributes["title"].Value.ToLower(); + name = titleNode.InnerText; + var game = new Game(id, name); + + iconUrl = GamesList.BaseURL + gameDom.SelectSingleNode(".//a[@class='image']/img").Attributes["src"].Value; + if (!Directory.Exists("res\\icon")) + Directory.CreateDirectory("res\\icon"); + await webClient.DownloadFileTaskAsync(iconUrl, game.ImagePath); + + return game; + } + + public static async Task> Reload() + { + WebClient webClient = new WebClient(); + string mainPage = await webClient.DownloadStringTaskAsync(GamesList.BaseURL); + HtmlDocument doc = new HtmlDocument(); + doc.LoadHtml(mainPage); + + var domGamesTable = doc.DocumentNode.SelectSingleNode("//table[@class='progtable']"); + var domGamesList = domGamesTable.SelectNodes("tr").Skip(1); + + var gamesList = new List(); + foreach (var domGame in domGamesList) + gamesList.Add(await GameDomToObject(webClient, domGame)); + + string gamesListJs = JsonSerializer.Serialize(gamesList, new JsonSerializerOptions { WriteIndented = true }); + File.WriteAllText("res\\games_list.js", gamesListJs); + + return gamesList; } } } diff --git a/MainWindow.xaml b/MainWindow.xaml index f4d56b4..93b0064 100644 --- a/MainWindow.xaml +++ b/MainWindow.xaml @@ -14,12 +14,19 @@ - + - + + + + + + + + diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index e0854aa..a4e1474 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -56,6 +56,14 @@ private void Window_Loaded(object sender, RoutedEventArgs e) }); } + private async void ReloadGamesList(object sender, RoutedEventArgs e) + { + logger.LogLine("Reloading games list from " + GamesList.BaseURL + " ..."); + gamesList = await GamesList.Reload(); + this.uiGamesList.ItemsSource = gamesList; + logger.LogLine("Games list reloaded and saved to disk!"); + } + private string GeneratePatchNameFromStack() { bool skip = false; @@ -175,7 +183,7 @@ await Task.Run(() => ThcrapUpdateDll.stack_update( Environment.CurrentDirectory = ".."; - CreateExe(game.Id, AppContext.BaseDirectory + @"res\Icon_th18.png"); + CreateExe(game.Id, game.ImagePath); CreateExe(game.Id + "_custom", null); ThcrapDll.stack_free(); diff --git a/StandaloneGeneratorV3.csproj b/StandaloneGeneratorV3.csproj index 922ca02..0084dfe 100644 --- a/StandaloneGeneratorV3.csproj +++ b/StandaloneGeneratorV3.csproj @@ -35,6 +35,9 @@ 4 + + packages\HtmlAgilityPack.1.11.32\lib\Net45\HtmlAgilityPack.dll + packages\Microsoft.Bcl.AsyncInterfaces.5.0.0\lib\net461\Microsoft.Bcl.AsyncInterfaces.dll diff --git a/packages.config b/packages.config index 4c21e88..d2a5410 100644 --- a/packages.config +++ b/packages.config @@ -1,5 +1,6 @@  +