Skip to content

Commit

Permalink
Download games list from thpatch.net
Browse files Browse the repository at this point in the history
  • Loading branch information
brliron committed Apr 7, 2021
1 parent 2831c2c commit b96ffd8
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 8 deletions.
8 changes: 8 additions & 0 deletions App.config
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Numerics.Vectors" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.4.0" newVersion="4.1.4.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
75 changes: 70 additions & 5 deletions GamesList.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,38 @@
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
{
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)
{
Expand All @@ -21,13 +43,56 @@ public Game(string id, string name)
}
class GamesList
{
public static readonly string BaseURL = "https://www.thpatch.net/";
public static List<Game> Load()
{
return new List<Game>()
try
{
new Game("th06", "Embodiment of Scarlet Devil"),
new Game("th18", "Unconnected Marketeers")
};
return JsonSerializer.Deserialize<List<Game>>(File.ReadAllText("res\\games_list.js"));
}
catch
{
return new List<Game>();
}
}

private static async Task<Game> 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<List<Game>> 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<Game>();
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;
}
}
}
11 changes: 9 additions & 2 deletions MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,19 @@
</Grid.RowDefinitions>
<DockPanel Grid.Row="0">
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
<Button>Reload games list</Button>
<Button Click="ReloadGamesList">Reload games list</Button>
</StackPanel>
<ListBox Name="uiGamesList">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsSelected}" Content="{Binding Name}"/>
<CheckBox IsChecked="{Binding IsSelected}">
<CheckBox.Content>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Image}" Height="16px" />
<TextBlock Text="{Binding Name}" Margin="5,0,0,0" />
</StackPanel>
</CheckBox.Content>
</CheckBox>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Expand Down
10 changes: 9 additions & 1 deletion MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
3 changes: 3 additions & 0 deletions StandaloneGeneratorV3.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="HtmlAgilityPack, Version=1.11.32.0, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a, processorArchitecture=MSIL">
<HintPath>packages\HtmlAgilityPack.1.11.32\lib\Net45\HtmlAgilityPack.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Bcl.AsyncInterfaces, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.Bcl.AsyncInterfaces.5.0.0\lib\net461\Microsoft.Bcl.AsyncInterfaces.dll</HintPath>
</Reference>
Expand Down
1 change: 1 addition & 0 deletions packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="HtmlAgilityPack" version="1.11.32" targetFramework="net472" />
<package id="Microsoft.Bcl.AsyncInterfaces" version="5.0.0" targetFramework="net472" />
<package id="System.Buffers" version="4.5.1" targetFramework="net472" />
<package id="System.Memory" version="4.5.4" targetFramework="net472" />
Expand Down

0 comments on commit b96ffd8

Please sign in to comment.