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
This commit is contained in:
Jaehyuk-Lee 2021-06-10 23:31:15 +09:00 committed by GitHub
parent 43911d0c81
commit 5228a2eb6c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 131 additions and 28 deletions

View file

@ -25,6 +25,9 @@ LANG=日本語
26=基本 26=基本
27=なし 27=なし
30=Webサービスでエラーが発生しました。
31=コンテンツデータのアップデート中にエラーが発生しました。
99=ドユーティー・コンテンツ・プラグイン {0} 99=ドユーティー・コンテンツ・プラグイン {0}
101=WAVEファイルを選んでください 101=WAVEファイルを選んでください

View file

@ -25,6 +25,9 @@ LANG=한국어(액토즈)
26=기본 26=기본
27=없음 27=없음
30=웹 요청중 에러 발생
31=임무 데이터 업데이트중 에러 발생
99=듀티 콘텐츠 플러그인 {0} 99=듀티 콘텐츠 플러그인 {0}
101=소리 파일을 선택하세요 101=소리 파일을 선택하세요

View file

@ -25,6 +25,9 @@ LANG=한국어
26=기본 26=기본
27=없음 27=없음
30=웹 요청중 에러 발생
31=임무 데이터 업데이트중 에러 발생
99=듀티 콘텐츠 플러그인 {0} 99=듀티 콘텐츠 플러그인 {0}
101=소리 파일을 선택하세요 101=소리 파일을 선택하세요

View file

@ -54,16 +54,18 @@ namespace DutyContent
// group // group
public class Group public class Group
{ {
public string Version { get; set; } public decimal Version { get; set; }
public string Language { get; set; } public string Language { get; set; }
public string DisplayLanguage { get; set; }
public Dictionary<int, Roulette> Roulettes { get; set; } public Dictionary<int, Roulette> Roulettes { get; set; }
public Dictionary<int, Instance> Instances { get; set; } public Dictionary<int, Instance> Instances { get; set; }
public Dictionary<int, Area> Areas { get; set; } public Dictionary<int, Area> 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 Language { get; private set; }
public static string DisplayLanguage { get; private set; }
public static IReadOnlyDictionary<int, Roulette> Roulettes { get; private set; } = new Dictionary<int, Roulette>(); public static IReadOnlyDictionary<int, Roulette> Roulettes { get; private set; } = new Dictionary<int, Roulette>();
public static IReadOnlyDictionary<int, Instance> Instances { get; private set; } = new Dictionary<int, Instance>(); public static IReadOnlyDictionary<int, Instance> Instances { get; private set; } = new Dictionary<int, Instance>();
public static IReadOnlyDictionary<int, Area> Areas { get; private set; } = new Dictionary<int, Area>(); public static IReadOnlyDictionary<int, Area> Areas { get; private set; } = new Dictionary<int, Area>();
@ -92,12 +94,17 @@ namespace DutyContent
// parse json // parse json
private static bool Fill(string json) public static bool Fill(string json)
{ {
Group data = JsonConvert.DeserializeObject<Group>(json); Group data = JsonConvert.DeserializeObject<Group>(json);
Dictionary<int, Fate> fates = new Dictionary<int, Fate>(); Dictionary<int, Fate> fates = new Dictionary<int, Fate>();
var version = data.Version;
var language = data.Language;
if (version > Version || language != Language)
{
foreach (var area in data.Areas) foreach (var area in data.Areas)
{ {
foreach (var fate in area.Value.Fates) foreach (var fate in area.Value.Fates)
@ -122,10 +129,12 @@ namespace DutyContent
Version = data.Version; Version = data.Version;
Language = data.Language; Language = data.Language;
DisplayLanguage = data.DisplayLanguage;
Roulettes = data.Roulettes; Roulettes = data.Roulettes;
Instances = data.Instances; Instances = data.Instances;
Areas = data.Areas; Areas = data.Areas;
Fates = fates; Fates = fates;
}
return true; return true;
} }

View file

@ -105,6 +105,8 @@
<Compile Include="ThirdParty\LineDb.cs" /> <Compile Include="ThirdParty\LineDb.cs" />
<Compile Include="ThirdParty\NativeMethods.cs" /> <Compile Include="ThirdParty\NativeMethods.cs" />
<Compile Include="ThirdParty\ThreadWorker.cs" /> <Compile Include="ThirdParty\ThreadWorker.cs" />
<Compile Include="ThirdParty\WebApi.cs" />
<Compile Include="Updater.cs" />
<Compile Include="WorkerAct.cs" /> <Compile Include="WorkerAct.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View file

@ -25,6 +25,9 @@ LANG=English
26=Default 26=Default
27=None 27=None
30=Error requesting web service
31=Error while updating duty/FATE Data
99=DutyContent Plugin {0} 99=DutyContent Plugin {0}
101=Select wave file 101=Select wave file

View file

@ -7,6 +7,7 @@ using System.Drawing;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
@ -46,6 +47,15 @@ namespace DutyContent.Tab
if (cboDispLang.SelectedIndex < 0) if (cboDispLang.SelectedIndex < 0)
cboDispLang.SelectedIndex = 0; cboDispLang.SelectedIndex = 0;
Task.Factory.StartNew(() =>
{
while (true)
{
Updater.CheckNewVersion();
Thread.Sleep(30 * 60 * 1000);
}
});
} }
public void RefreshLocale() public void RefreshLocale()

View file

@ -43,7 +43,7 @@ namespace DutyContent.Tab
public void PluginInitialize() public void PluginInitialize()
{ {
// //
lblCurrentDataSet.Text = DcContent.Language; lblCurrentDataSet.Text = DcContent.DisplayLanguage;
// //
var lang = MakeDutyLangList(); var lang = MakeDutyLangList();
@ -534,11 +534,14 @@ namespace DutyContent.Tab
if (!string.IsNullOrWhiteSpace(l) && !l.Equals(DcConfig.Duty.Language) && DcContent.ReadContent(l)) if (!string.IsNullOrWhiteSpace(l) && !l.Equals(DcConfig.Duty.Language) && DcContent.ReadContent(l))
{ {
UpdateFates();
lblCurrentDataSet.Text = DcContent.Language; lblCurrentDataSet.Text = DcContent.DisplayLanguage;
SaveConfig(); SaveConfig();
Updater.CheckNewVersion();
UpdateFates();
} }
} }

42
ThirdParty/WebApi.cs vendored Normal file
View file

@ -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;
}
}
}

25
Updater.cs Normal file
View file

@ -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);
}
});
}
}
}