Skip to content

Commit

Permalink
feat: add etag of bundled CardDefs.base.xml
Browse files Browse the repository at this point in the history
  • Loading branch information
azeier committed Feb 5, 2025
1 parent c491d74 commit 1d5f88a
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 22 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ jobs:
runs-on: ubuntu-22.04
if: github.event_name == 'push'
permissions:
id-token: write
contents: read
id-token: write
contents: read
steps:
- name: Download artifact
uses: actions/download-artifact@v4
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<LangVersion>8</LangVersion>
<OutputType>Exe</OutputType>
</PropertyGroup>

</Project>
31 changes: 31 additions & 0 deletions HearthDb.CardDefsDownloader/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;

namespace HearthDb.CardDefsDownloader
{
class Program
{
static void Main(string[] args)
{
var outdir = args[0];
Directory.CreateDirectory(outdir);
var httpClient = new HttpClient(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate });

// CardDefs.base.xml contains non localized tags and enUS tag
// Other languages can be found under e.g. CardDefs.deDE.xml
using var request = new HttpRequestMessage(HttpMethod.Get, "https://api.hearthstonejson.com/v1/latest/CardDefs.base.xml");
request.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
request.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("deflate"));

var response = httpClient.SendAsync(request).Result;
var data = response.Content.ReadAsStringAsync().Result;
File.WriteAllText(Path.Combine(outdir, "CardDefs.base.xml"), data);

var etag = response.Headers.ETag.Tag;
var lastModified = response.Content.Headers.LastModified;
File.WriteAllText(Path.Combine(outdir, "CardDefs.base.etag"), $"{etag}\n{lastModified.ToString()}");
}
}
}
11 changes: 10 additions & 1 deletion HearthDb.Tests/CardDefsLoadTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.IO;
using HearthDb.CardDefs;
using HearthDb.Enums;
Expand Down Expand Up @@ -38,5 +39,13 @@ public void LoadCards_CorrectlyAssemblesData()
Assert.AreEqual("Flame Lance", Cards.All["AT_001"].GetLocName(Locale.enUS));
Assert.AreEqual("Flammenlanze", Cards.All["AT_001"].GetLocName(Locale.deDE));
}

[TestMethod]
public void HasValidETagData()
{
var bundled = Cards.GetBundledCardDefsETag();
Assert.IsNotNull(bundled.ETag);
Assert.IsTrue(DateTime.TryParse(bundled.LastModified, out _));
}
}
}
10 changes: 10 additions & 0 deletions HearthDb.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HearthDb.EnumsGenerator", "
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HearthDb.Tests", "HearthDb.Tests\HearthDb.Tests.csproj", "{875316D1-C4C3-48BD-BDAE-3727269B2A67}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HearthDb.CardDefsDownloader", "HearthDb.CardDefsDownloader\HearthDb.CardDefsDownloader.csproj", "{92DBA109-DF71-4874-827A-20B00EABB57D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -51,6 +53,14 @@ Global
{875316D1-C4C3-48BD-BDAE-3727269B2A67}.Release|Any CPU.Build.0 = Release|Any CPU
{875316D1-C4C3-48BD-BDAE-3727269B2A67}.Release|x86.ActiveCfg = Release|Any CPU
{875316D1-C4C3-48BD-BDAE-3727269B2A67}.Release|x86.Build.0 = Release|Any CPU
{92DBA109-DF71-4874-827A-20B00EABB57D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{92DBA109-DF71-4874-827A-20B00EABB57D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{92DBA109-DF71-4874-827A-20B00EABB57D}.Debug|x86.ActiveCfg = Debug|Any CPU
{92DBA109-DF71-4874-827A-20B00EABB57D}.Debug|x86.Build.0 = Debug|Any CPU
{92DBA109-DF71-4874-827A-20B00EABB57D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{92DBA109-DF71-4874-827A-20B00EABB57D}.Release|Any CPU.Build.0 = Release|Any CPU
{92DBA109-DF71-4874-827A-20B00EABB57D}.Release|x86.ActiveCfg = Release|Any CPU
{92DBA109-DF71-4874-827A-20B00EABB57D}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
28 changes: 21 additions & 7 deletions HearthDb/Cards.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,25 @@ public static class Cards

public static string Build { get; private set; }

private static (string ETag, string LastModified)? _bundledCardDefsETag;
public static (string ETag, string LastModified) GetBundledCardDefsETag()
{
if(_bundledCardDefsETag == null)
{
using var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("HearthDb.CardDefs.base.etag")!;
using var reader = new StreamReader(stream);
var text = reader.ReadToEnd().Split('\n');
_bundledCardDefsETag = (text[0], text[1]);
}
return _bundledCardDefsETag.Value;
}

public static CardDefs.CardDefs GetBundledBaseData()
{
var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("HearthDb.CardDefs.base.xml")!;
return ParseCardDefs(stream);
}

static Cards()
{
if(Config.AutoLoadCardDefs)
Expand All @@ -54,14 +73,9 @@ public static CardDefs.CardDefs ParseCardDefs(Stream cardDefsData)

/// <summary>
/// Load base card data (non-localized card metadata, as well as
/// localized strings in enUS and zhCN) included with HearthDb.
/// localized strings in enUS and zhCN) bundled with HearthDb.
/// </summary>
public static void LoadBaseData()
{
var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("HearthDb.CardDefs.base.xml");
if(stream != null)
LoadBaseData(stream);
}
public static void LoadBaseData() => LoadBaseData(GetBundledBaseData());

/// <summary>
/// Load base card data, this should usually be a
Expand Down
19 changes: 10 additions & 9 deletions HearthDb/HearthDb.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk" DefaultsTargets="DownloadBaseData">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
Expand All @@ -20,6 +20,9 @@
<EmbeddedResource Include="hsdata/CardDefs.base.xml">
<LogicalName>$(RootNamespace).CardDefs.base.xml</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="hsdata/CardDefs.base.etag">
<LogicalName>$(RootNamespace).CardDefs.base.etag</LogicalName>
</EmbeddedResource>
</ItemGroup>

<ItemGroup>
Expand All @@ -29,15 +32,13 @@
</ItemGroup>

<PropertyGroup>
<!-- CardDefs.base.xml contains non localized tags and enUS tag -->
<!-- Other languages can be found under e.g. CardDefs.deDE.xml -->
<BaseDataUrl>https://api.hearthstonejson.com/v1/latest/CardDefs.base.xml</BaseDataUrl>
<DownloaderOutDir>$(SolutionDir)HearthDb.CardDefsDownloader/bin/Release/netcoreapp3.1/</DownloaderOutDir>
</PropertyGroup>

<Target Name="DownloadBaseData" BeforeTargets="PreBuildEvent">
<DownloadFile SourceUrl="$(BaseDataUrl)" DestinationFolder="$(MSBuildProjectDirectory)/hsdata" SkipUnchangedFiles="true">
<Output TaskParameter="DownloadedFile" ItemName="Content" />
</DownloadFile>
</Target>
<Target Name="DownloadBaseData" BeforeTargets="PreBuildEvent">
<MSBuild Projects="$(SolutionDir)HearthDb.CardDefsDownloader/HearthDb.CardDefsDownloader.csproj" Properties="Configuration=Release"/>
<Exec Condition="Exists('$(DownloaderOutDir)HearthDb.CardDefsDownloader')" Command='"$(DownloaderOutDir)HearthDb.CardDefsDownloader" "$(SolutionDir)HearthDb/hsdata"'/>
<Exec Condition="Exists('$(DownloaderOutDir)HearthDb.CardDefsDownloader.exe')" Command='"$(DownloaderOutDir)HearthDb.CardDefsDownloader.exe" "$(SolutionDir)HearthDb/hsdata"'/>
</Target>

</Project>
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ By default, HearthDb only loads locale data for enUS and zhCN. Additional langua
data can be downloaded from `api.hearthstonejson.com`, e.g. `https://api.hearthstonejson.com/v1/latest/CardDefs.deDE.xml` and loaded at runtime via
`HearthDb.Cards.LoadLocaleData(...)`.

If desired, all language data can be included by default by replacing the
`<BaseDataUrl>` in `HearthDb.csproj` with `https://github.
com/HearthSim/hsdata/blob/master/CardDefs.xml`.
If desired, all language data can be included by default by replacing the url in `HearthDb.CardDefsDownloader` with `https://github.com/HearthSim/hsdata/blob/master/CardDefs.xml`.

## CardIDs
`HearthDb.CardIds` contains properly named constant for all cardIds existing in Hearthstone.
Expand Down

0 comments on commit 1d5f88a

Please sign in to comment.