From 47b7da5194be65b91e6791b21288dc960fb76f0d Mon Sep 17 00:00:00 2001 From: Alex Zeier Date: Tue, 4 Feb 2025 16:20:19 -0800 Subject: [PATCH] feat: add etag of included CardDefs.base.xml --- .../HearthDb.CardDefsDownloader.csproj | 9 ++++++ HearthDb.CardDefsDownloader/Program.cs | 31 +++++++++++++++++++ HearthDb.Tests/CardDefsLoadTest.cs | 6 ++++ HearthDb.sln | 10 ++++++ HearthDb/Cards.cs | 15 +++++++++ HearthDb/HearthDb.csproj | 12 +++---- 6 files changed, 77 insertions(+), 6 deletions(-) create mode 100644 HearthDb.CardDefsDownloader/HearthDb.CardDefsDownloader.csproj create mode 100644 HearthDb.CardDefsDownloader/Program.cs diff --git a/HearthDb.CardDefsDownloader/HearthDb.CardDefsDownloader.csproj b/HearthDb.CardDefsDownloader/HearthDb.CardDefsDownloader.csproj new file mode 100644 index 00000000..918adb65 --- /dev/null +++ b/HearthDb.CardDefsDownloader/HearthDb.CardDefsDownloader.csproj @@ -0,0 +1,9 @@ + + + + netcoreapp3.1 + 8 + Exe + + + diff --git a/HearthDb.CardDefsDownloader/Program.cs b/HearthDb.CardDefsDownloader/Program.cs new file mode 100644 index 00000000..391e55d7 --- /dev/null +++ b/HearthDb.CardDefsDownloader/Program.cs @@ -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()}"); + } + } +} \ No newline at end of file diff --git a/HearthDb.Tests/CardDefsLoadTest.cs b/HearthDb.Tests/CardDefsLoadTest.cs index f6724ee4..0a2c8054 100644 --- a/HearthDb.Tests/CardDefsLoadTest.cs +++ b/HearthDb.Tests/CardDefsLoadTest.cs @@ -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()); + } } } \ No newline at end of file diff --git a/HearthDb.sln b/HearthDb.sln index 5773565f..286e4c35 100644 --- a/HearthDb.sln +++ b/HearthDb.sln @@ -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 @@ -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 diff --git a/HearthDb/Cards.cs b/HearthDb/Cards.cs index 618da68f..880b2534 100644 --- a/HearthDb/Cards.cs +++ b/HearthDb/Cards.cs @@ -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) diff --git a/HearthDb/HearthDb.csproj b/HearthDb/HearthDb.csproj index cd82c23e..aa2c0247 100644 --- a/HearthDb/HearthDb.csproj +++ b/HearthDb/HearthDb.csproj @@ -1,4 +1,4 @@ - + netstandard2.0 @@ -20,6 +20,9 @@ $(RootNamespace).CardDefs.base.xml + + $(RootNamespace).CardDefs.base.etag + @@ -29,15 +32,12 @@ - - https://api.hearthstonejson.com/v1/latest/CardDefs.base.xml - - - + +