Skip to content

Commit 4454549

Browse files
committed
feat: add etag of included CardDefs.base.xml
1 parent 81275e5 commit 4454549

File tree

6 files changed

+77
-6
lines changed

6 files changed

+77
-6
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
<LangVersion>8</LangVersion>
6+
<OutputType>Exe</OutputType>
7+
</PropertyGroup>
8+
9+
</Project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.IO;
3+
using System.Net;
4+
using System.Net.Http;
5+
using System.Net.Http.Headers;
6+
7+
namespace HearthDb.CardDefsDownloader
8+
{
9+
class Program
10+
{
11+
static void Main(string[] args)
12+
{
13+
Console.WriteLine(args[0]);
14+
var httpClient = new HttpClient(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate });
15+
16+
// CardDefs.base.xml contains non localized tags and enUS tag
17+
// Other languages can be found under e.g. CardDefs.deDE.xml
18+
using var request = new HttpRequestMessage(HttpMethod.Get, "https://api.hearthstonejson.com/v1/latest/CardDefs.base.xml");
19+
request.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
20+
request.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("deflate"));
21+
22+
var response = httpClient.SendAsync(request).Result;
23+
var data = response.Content.ReadAsStringAsync().Result;
24+
File.WriteAllText(Path.Combine(args[0], "CardDefs.base.xml"), data);
25+
26+
var etag = response.Headers.ETag.Tag;
27+
var lastModified = response.Content.Headers.LastModified;
28+
File.WriteAllText(Path.Combine(args[0], "CardDefs.base.etag"), $"{etag}\n{lastModified.ToString()}");
29+
}
30+
}
31+
}

HearthDb.Tests/CardDefsLoadTest.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,11 @@ public void LoadCards_CorrectlyAssemblesData()
3636
Assert.AreEqual("Flame Lance", Cards.All["AT_001"].GetLocName(Locale.enUS));
3737
Assert.AreEqual("Flammenlanze", Cards.All["AT_001"].GetLocName(Locale.deDE));
3838
}
39+
40+
[TestMethod]
41+
public void HasEtag()
42+
{
43+
Assert.IsNotNull(Cards.GetBaseCardDefsETag());
44+
}
3945
}
4046
}

HearthDb.sln

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HearthDb.EnumsGenerator", "
1111
EndProject
1212
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HearthDb.Tests", "HearthDb.Tests\HearthDb.Tests.csproj", "{875316D1-C4C3-48BD-BDAE-3727269B2A67}"
1313
EndProject
14+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HearthDb.CardDefsDownloader", "HearthDb.CardDefsDownloader\HearthDb.CardDefsDownloader.csproj", "{92DBA109-DF71-4874-827A-20B00EABB57D}"
15+
EndProject
1416
Global
1517
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1618
Debug|Any CPU = Debug|Any CPU
@@ -51,6 +53,14 @@ Global
5153
{875316D1-C4C3-48BD-BDAE-3727269B2A67}.Release|Any CPU.Build.0 = Release|Any CPU
5254
{875316D1-C4C3-48BD-BDAE-3727269B2A67}.Release|x86.ActiveCfg = Release|Any CPU
5355
{875316D1-C4C3-48BD-BDAE-3727269B2A67}.Release|x86.Build.0 = Release|Any CPU
56+
{92DBA109-DF71-4874-827A-20B00EABB57D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
57+
{92DBA109-DF71-4874-827A-20B00EABB57D}.Debug|Any CPU.Build.0 = Debug|Any CPU
58+
{92DBA109-DF71-4874-827A-20B00EABB57D}.Debug|x86.ActiveCfg = Debug|Any CPU
59+
{92DBA109-DF71-4874-827A-20B00EABB57D}.Debug|x86.Build.0 = Debug|Any CPU
60+
{92DBA109-DF71-4874-827A-20B00EABB57D}.Release|Any CPU.ActiveCfg = Release|Any CPU
61+
{92DBA109-DF71-4874-827A-20B00EABB57D}.Release|Any CPU.Build.0 = Release|Any CPU
62+
{92DBA109-DF71-4874-827A-20B00EABB57D}.Release|x86.ActiveCfg = Release|Any CPU
63+
{92DBA109-DF71-4874-827A-20B00EABB57D}.Release|x86.Build.0 = Release|Any CPU
5464
EndGlobalSection
5565
GlobalSection(SolutionProperties) = preSolution
5666
HideSolutionNode = FALSE

HearthDb/Cards.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@ public static class Cards
4141

4242
public static string Build { get; private set; }
4343

44+
private static (string Etag, string LastModified)? _baseCardDefsEtag;
45+
public static (string Etag, string LastModified) GetBaseCardDefsETag()
46+
{
47+
if(_baseCardDefsEtag == null)
48+
{
49+
using var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("HearthDb.CardDefs.base.etag");
50+
if (stream == null)
51+
return (null, null);
52+
using var reader = new StreamReader(stream);
53+
var text = reader.ReadToEnd().Split('\n');
54+
_baseCardDefsEtag = (text[0], text[1]);
55+
}
56+
return _baseCardDefsEtag.Value;
57+
}
58+
4459
static Cards()
4560
{
4661
if(Config.AutoLoadCardDefs)

HearthDb/HearthDb.csproj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk" DefaultsTargets="DownloadBaseData">
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
@@ -20,6 +20,9 @@
2020
<EmbeddedResource Include="hsdata/CardDefs.base.xml">
2121
<LogicalName>$(RootNamespace).CardDefs.base.xml</LogicalName>
2222
</EmbeddedResource>
23+
<EmbeddedResource Include="hsdata/CardDefs.base.etag">
24+
<LogicalName>$(RootNamespace).CardDefs.base.etag</LogicalName>
25+
</EmbeddedResource>
2326
</ItemGroup>
2427

2528
<ItemGroup>
@@ -29,15 +32,12 @@
2932
</ItemGroup>
3033

3134
<PropertyGroup>
32-
<!-- CardDefs.base.xml contains non localized tags and enUS tag -->
33-
<!-- Other languages can be found under e.g. CardDefs.deDE.xml -->
3435
<BaseDataUrl>https://api.hearthstonejson.com/v1/latest/CardDefs.base.xml</BaseDataUrl>
3536
</PropertyGroup>
3637

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

4343
</Project>

0 commit comments

Comments
 (0)