From 5228a2eb6c4e5680beeee5682af378b885cf53ad Mon Sep 17 00:00:00 2001 From: Jaehyuk-Lee Date: Thu, 10 Jun 2021 23:31:15 +0900 Subject: [PATCH] add an updater (#9) * add an updator * change update branch * make updater work * change update branch * make work * add new line * add exception log + clean up --- Data/DcLang-Japanese.txt | 3 ++ Data/DcLang-Korean(Actoz).txt | 3 ++ Data/DcLang-Korean(Test).txt | 3 ++ DcContent.cs | 59 ++++++++++++++++++++--------------- DutyContent.csproj | 2 ++ Resources/mesg.txt | 3 ++ Tab/ConfigForm.cs | 10 ++++++ Tab/DutyForm.cs | 9 ++++-- ThirdParty/WebApi.cs | 42 +++++++++++++++++++++++++ Updater.cs | 25 +++++++++++++++ 10 files changed, 131 insertions(+), 28 deletions(-) create mode 100644 ThirdParty/WebApi.cs create mode 100644 Updater.cs diff --git a/Data/DcLang-Japanese.txt b/Data/DcLang-Japanese.txt index 9e81b62..0a48900 100644 --- a/Data/DcLang-Japanese.txt +++ b/Data/DcLang-Japanese.txt @@ -25,6 +25,9 @@ LANG=日本語 26=基本 27=なし +30=Webサービスでエラーが発生しました。 +31=コンテンツデータのアップデート中にエラーが発生しました。 + 99=ドユーティー・コンテンツ・プラグイン {0} 101=WAVEファイルを選んでください diff --git a/Data/DcLang-Korean(Actoz).txt b/Data/DcLang-Korean(Actoz).txt index b6c3762..ace41c3 100644 --- a/Data/DcLang-Korean(Actoz).txt +++ b/Data/DcLang-Korean(Actoz).txt @@ -25,6 +25,9 @@ LANG=한국어(액토즈) 26=기본 27=없음 +30=웹 요청중 에러 발생 +31=임무 데이터 업데이트중 에러 발생 + 99=듀티 콘텐츠 플러그인 {0} 101=소리 파일을 선택하세요 diff --git a/Data/DcLang-Korean(Test).txt b/Data/DcLang-Korean(Test).txt index 48f4baa..e980568 100644 --- a/Data/DcLang-Korean(Test).txt +++ b/Data/DcLang-Korean(Test).txt @@ -25,6 +25,9 @@ LANG=한국어 26=기본 27=없음 +30=웹 요청중 에러 발생 +31=임무 데이터 업데이트중 에러 발생 + 99=듀티 콘텐츠 플러그인 {0} 101=소리 파일을 선택하세요 diff --git a/DcContent.cs b/DcContent.cs index a50cbaa..6c6b186 100644 --- a/DcContent.cs +++ b/DcContent.cs @@ -54,16 +54,18 @@ namespace DutyContent // group public class Group { - public string Version { get; set; } + public decimal Version { get; set; } public string Language { get; set; } + public string DisplayLanguage { get; set; } public Dictionary Roulettes { get; set; } public Dictionary Instances { get; set; } public Dictionary Areas { get; set; } } // - public static string Version { get; private set; } + public static decimal Version { get; private set; } = 0; public static string Language { get; private set; } + public static string DisplayLanguage { get; private set; } public static IReadOnlyDictionary Roulettes { get; private set; } = new Dictionary(); public static IReadOnlyDictionary Instances { get; private set; } = new Dictionary(); public static IReadOnlyDictionary Areas { get; private set; } = new Dictionary(); @@ -92,40 +94,47 @@ namespace DutyContent // parse json - private static bool Fill(string json) + public static bool Fill(string json) { Group data = JsonConvert.DeserializeObject(json); Dictionary fates = new Dictionary(); - foreach (var area in data.Areas) + var version = data.Version; + var language = data.Language; + + if (version > Version || language != Language) { - foreach (var fate in area.Value.Fates) + foreach (var area in data.Areas) { - try + foreach (var fate in area.Value.Fates) { - fate.Value.Area = area.Value; - fates.Add(fate.Key, fate.Value); - } - catch (NullReferenceException /*nex*/) - { - MesgLog.E(7, fate.Key); - return false; - } - catch (Exception ex) - { - MesgLog.Ex(ex, 8); - return false; + try + { + fate.Value.Area = area.Value; + fates.Add(fate.Key, fate.Value); + } + catch (NullReferenceException /*nex*/) + { + MesgLog.E(7, fate.Key); + return false; + } + catch (Exception ex) + { + MesgLog.Ex(ex, 8); + return false; + } } } - } - Version = data.Version; - Language = data.Language; - Roulettes = data.Roulettes; - Instances = data.Instances; - Areas = data.Areas; - Fates = fates; + Version = data.Version; + Language = data.Language; + DisplayLanguage = data.DisplayLanguage; + Roulettes = data.Roulettes; + Instances = data.Instances; + Areas = data.Areas; + Fates = fates; + } return true; } diff --git a/DutyContent.csproj b/DutyContent.csproj index 1bcdf58..f0cefac 100644 --- a/DutyContent.csproj +++ b/DutyContent.csproj @@ -105,6 +105,8 @@ + + diff --git a/Resources/mesg.txt b/Resources/mesg.txt index ae77a71..8e00666 100644 --- a/Resources/mesg.txt +++ b/Resources/mesg.txt @@ -25,6 +25,9 @@ LANG=English 26=Default 27=None +30=Error requesting web service +31=Error while updating duty/FATE Data + 99=DutyContent Plugin {0} 101=Select wave file diff --git a/Tab/ConfigForm.cs b/Tab/ConfigForm.cs index 91a6704..7d33fd6 100644 --- a/Tab/ConfigForm.cs +++ b/Tab/ConfigForm.cs @@ -7,6 +7,7 @@ using System.Drawing; using System.IO; using System.Linq; using System.Text; +using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; @@ -46,6 +47,15 @@ namespace DutyContent.Tab if (cboDispLang.SelectedIndex < 0) cboDispLang.SelectedIndex = 0; + + Task.Factory.StartNew(() => + { + while (true) + { + Updater.CheckNewVersion(); + Thread.Sleep(30 * 60 * 1000); + } + }); } public void RefreshLocale() diff --git a/Tab/DutyForm.cs b/Tab/DutyForm.cs index c3360fe..c419125 100644 --- a/Tab/DutyForm.cs +++ b/Tab/DutyForm.cs @@ -43,7 +43,7 @@ namespace DutyContent.Tab public void PluginInitialize() { // - lblCurrentDataSet.Text = DcContent.Language; + lblCurrentDataSet.Text = DcContent.DisplayLanguage; // var lang = MakeDutyLangList(); @@ -534,11 +534,14 @@ namespace DutyContent.Tab if (!string.IsNullOrWhiteSpace(l) && !l.Equals(DcConfig.Duty.Language) && DcContent.ReadContent(l)) { - UpdateFates(); - lblCurrentDataSet.Text = DcContent.Language; + lblCurrentDataSet.Text = DcContent.DisplayLanguage; SaveConfig(); + + Updater.CheckNewVersion(); + + UpdateFates(); } } diff --git a/ThirdParty/WebApi.cs b/ThirdParty/WebApi.cs new file mode 100644 index 0000000..84bd391 --- /dev/null +++ b/ThirdParty/WebApi.cs @@ -0,0 +1,42 @@ +using System; +using System.IO; +using System.Net; +using System.Net.Cache; +using System.Text; + +namespace DutyContent +{ + internal static class WebApi + { + internal static string Request(string urlfmt, params object[] args) + { + try + { + var url = string.Format(urlfmt, args); + + ServicePointManager.Expect100Continue = true; + ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; + ServicePointManager.DefaultConnectionLimit = 9999; + + var request = (HttpWebRequest)WebRequest.Create(url); + request.UserAgent = "DFA"; + request.Timeout = 10000; + request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore); + using (var response = (HttpWebResponse)request.GetResponse()) + { + var encoding = Encoding.GetEncoding(response.CharacterSet); + + using (var responseStream = response.GetResponseStream()) + using (var reader = new StreamReader(responseStream, encoding)) + return reader.ReadToEnd(); + } + } + catch (Exception ex) + { + MesgLog.Ex(ex, 30); + } + + return null; + } + } +} diff --git a/Updater.cs b/Updater.cs new file mode 100644 index 0000000..5974bb4 --- /dev/null +++ b/Updater.cs @@ -0,0 +1,25 @@ +using System; +using System.Threading.Tasks; +using Newtonsoft.Json; + +namespace DutyContent +{ + internal class Updater + { + internal static void CheckNewVersion() + { + Task.Factory.StartNew(() => + { + try + { + var json = WebApi.Request($"https://raw.githubusercontent.com/kshman/DutyContent/main/Data/DcDuty-{DcContent.Language}.json"); + DcContent.Fill(json); + } + catch (Exception ex) + { + MesgLog.Ex(ex, 31); + } + }); + } + } +}