Skip to content

Commit

Permalink
feat: add etag of included CardDefs.base.xml
Browse files Browse the repository at this point in the history
  • Loading branch information
azeier committed Feb 5, 2025
1 parent 81275e5 commit 47b7da5
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 6 deletions.
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;
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)
{
Console.WriteLine(args[0]);
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(args[0], "CardDefs.base.xml"), data);

var etag = response.Headers.ETag.Tag;
var lastModified = response.Content.Headers.LastModified;
File.WriteAllText(Path.Combine(args[0], "CardDefs.base.etag"), $"{etag}\n{lastModified.ToString()}");
}
}
}
6 changes: 6 additions & 0 deletions HearthDb.Tests/CardDefsLoadTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,11 @@ 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 HasEtag()
{
Assert.IsNotNull(Cards.GetBaseCardDefsETag());
}
}
}
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
15 changes: 15 additions & 0 deletions HearthDb/Cards.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@ public static class Cards

public static string Build { get; private set; }

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

static Cards()
{
if(Config.AutoLoadCardDefs)
Expand Down
12 changes: 6 additions & 6 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,12 @@
</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>
</PropertyGroup>

<Target Name="DownloadBaseData" BeforeTargets="PreBuildEvent">
<DownloadFile SourceUrl="$(BaseDataUrl)" DestinationFolder="$(MSBuildProjectDirectory)/hsdata" SkipUnchangedFiles="true">
<Output TaskParameter="DownloadedFile" ItemName="Content" />
</DownloadFile>
<Exec Command="msbuild $(SolutionDir)HearthDb.CardDefsDownloader\HearthDb.CardDefsDownloader.csproj"/>
<Exec Command="$(SolutionDir)HearthDb.CardDefsDownloader\bin\Debug\netcoreapp3.1\HearthDb.CardDefsDownloader.exe $(SolutionDir)\HearthDb\hsdata"/>
</Target>

</Project>

0 comments on commit 47b7da5

Please sign in to comment.