From 99c5d9615ae5b215f6dac6410e27464bb9f342f8 Mon Sep 17 00:00:00 2001 From: MistEO Date: Thu, 5 Jan 2023 00:55:05 +0800 Subject: [PATCH 1/4] =?UTF-8?q?feat:=20=E5=88=9D=E6=AD=A5=E6=90=AD?= =?UTF-8?q?=E5=BB=BA=E4=BB=8Eweb=E8=8E=B7=E5=8F=96UI=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E7=9A=84=E6=A1=86=E6=9E=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/MaaWpfGui/Helper/StageManager.cs | 21 ++++- src/MaaWpfGui/Helper/WebService.cs | 88 ++++++++++++++++++++ src/MaaWpfGui/MaaWpfGui.csproj | 1 + src/MaaWpfGui/Main/VersionUpdateViewModel.cs | 46 +--------- 4 files changed, 110 insertions(+), 46 deletions(-) create mode 100644 src/MaaWpfGui/Helper/WebService.cs diff --git a/src/MaaWpfGui/Helper/StageManager.cs b/src/MaaWpfGui/Helper/StageManager.cs index 1c7a0943fd..604474598a 100644 --- a/src/MaaWpfGui/Helper/StageManager.cs +++ b/src/MaaWpfGui/Helper/StageManager.cs @@ -16,7 +16,9 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; +using MaaWpfGui.Helper; using Newtonsoft.Json; +using Newtonsoft.Json.Linq; namespace MaaWpfGui { @@ -53,12 +55,23 @@ namespace MaaWpfGui // 这里会被 “剩余理智” 复用,第一个必须是 string.Empty 的 // 「当前/上次」关卡导航 { string.Empty, new StageInfo { Display = Localization.GetString("DefaultStage"), Value = string.Empty } }, + }; - // SideStory「将进酒」复刻活动 - { "IW-8", new StageInfo { Display = "IW-8", Value = "IW-8", Drop = "30063", Activity = sideStory } }, - { "IW-7", new StageInfo { Display = "IW-7", Value = "IW-7", Drop = "30013", Activity = sideStory } }, - { "IW-6", new StageInfo { Display = "IW-6", Value = "IW-6", Drop = "30103", Activity = sideStory } }, + var activity = WebService.RequestMaaApiWithCache("StageActivity.json"); + foreach (var stageObj in activity["Official"] as JArray) + { + _stages.Add(stageObj["value"].ToString(), + new StageInfo + { + Display = stageObj["display"].ToString(), + Value = stageObj["value"].ToString(), + Drop = stageObj["drop"].ToString(), + Activity = sideStory, + }); + } + // TODO: 把这些也放到 web json 上 + new Dictionary { // 主线关卡 { "1-7", new StageInfo { Display = "1-7", Value = "1-7" } }, diff --git a/src/MaaWpfGui/Helper/WebService.cs b/src/MaaWpfGui/Helper/WebService.cs new file mode 100644 index 0000000000..569f4224a9 --- /dev/null +++ b/src/MaaWpfGui/Helper/WebService.cs @@ -0,0 +1,88 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net; +using System.Security.Policy; +using System.Text; +using System.Threading.Tasks; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; + +namespace MaaWpfGui.Helper +{ + public static class WebService + { + public const string RequestUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36 Edg/97.0.1072.76"; + + public static string RequestUrl(string url) + { + try + { + HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url); + httpWebRequest.Method = "GET"; + httpWebRequest.UserAgent = RequestUserAgent; + httpWebRequest.Accept = "application/vnd.github.v3+json"; + + var httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse; + if (httpWebResponse.StatusCode != HttpStatusCode.OK) + { + return null; + } + + var streamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.UTF8); + var responseContent = streamReader.ReadToEnd(); + streamReader.Close(); + httpWebResponse.Close(); + return responseContent; + } + catch (Exception info) + { + Console.WriteLine(info.Message); + return null; + } + } + + private const string CacheDir = "cache/gui/"; + private const string MaaApi = "https://ota.maa.plus/MaaAssistantArknights/api/"; + + // 如果请求失败,则读取缓存。否则写入缓存 + public static JObject RequestMaaApiWithCache(string api) + { + if (!Directory.Exists(CacheDir)) + { + Directory.CreateDirectory(CacheDir); + } + + api = api.Replace('/', '_'); + + var url = MaaApi + api; + var cache = CacheDir + api; + + var response = RequestUrl(url); + if (response == null) + { + if (File.Exists(cache)) + { + response = File.ReadAllText(cache); + } + else + { + return null; + } + } + + try + { + var json = (JObject)JsonConvert.DeserializeObject(response); + File.WriteAllText(cache, response); + + return json; + } + catch + { + return null; + } + } + } +} diff --git a/src/MaaWpfGui/MaaWpfGui.csproj b/src/MaaWpfGui/MaaWpfGui.csproj index 5eb73cc54d..558e93a3d4 100644 --- a/src/MaaWpfGui/MaaWpfGui.csproj +++ b/src/MaaWpfGui/MaaWpfGui.csproj @@ -104,6 +104,7 @@ Designer + diff --git a/src/MaaWpfGui/Main/VersionUpdateViewModel.cs b/src/MaaWpfGui/Main/VersionUpdateViewModel.cs index f27a791b7f..c2c8dc6b8c 100644 --- a/src/MaaWpfGui/Main/VersionUpdateViewModel.cs +++ b/src/MaaWpfGui/Main/VersionUpdateViewModel.cs @@ -22,6 +22,7 @@ using System.Runtime.InteropServices; using System.Text; using System.Text.RegularExpressions; using System.Windows; +using MaaWpfGui.Helper; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Stylet; @@ -146,7 +147,6 @@ namespace MaaWpfGui private const string MaaReleaseRequestUrlByTag = "repos/MaaAssistantArknights/MaaRelease/releases/tags/"; private const string InfoRequestUrl = "repos/MaaAssistantArknights/MaaAssistantArknights/releases/tags/"; - private const string RequestUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36 Edg/97.0.1072.76"; private JObject _latestJson; private JObject _assetsObject; @@ -632,44 +632,6 @@ namespace MaaWpfGui } } - /// - /// 访问 API - /// - /// API 地址 - /// 返回 API 的返回值,如出现错误则返回空字符串 - public string RequestApi(string url) - { - try - { - HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url); - httpWebRequest.Method = "GET"; - httpWebRequest.UserAgent = RequestUserAgent; - httpWebRequest.Accept = "application/vnd.github.v3+json"; - var settings = _container.Get(); - if (!string.IsNullOrWhiteSpace(settings.Proxy)) - { - httpWebRequest.Proxy = new WebProxy(settings.Proxy); - } - - var httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse; - if (httpWebResponse.StatusCode != HttpStatusCode.OK) - { - return null; - } - - var streamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.UTF8); - var responseContent = streamReader.ReadToEnd(); - streamReader.Close(); - httpWebResponse.Close(); - return responseContent; - } - catch (Exception info) - { - Console.WriteLine(info.Message); - return null; - } - } - private string RequestGithubApi(string url, int retryTimes) { string response = string.Empty; @@ -678,7 +640,7 @@ namespace MaaWpfGui { for (var i = 0; i < requestSource.Length; i++) { - response = RequestApi(requestSource[i] + url); + response = WebService.RequestUrl(requestSource[i] + url); if (!string.IsNullOrEmpty(response)) { break; @@ -763,7 +725,7 @@ namespace MaaWpfGui private static bool DownloadFileForAria2(string url, string saveTo, string fileName, string proxy = "") { var aria2FilePath = Path.GetFullPath(Directory.GetCurrentDirectory() + "/aria2c.exe"); - var aria2Args = "\"" + url + "\" --continue=true --dir=\"" + saveTo + "\" --out=\"" + fileName + "\" --user-agent=\"" + RequestUserAgent + "\""; + var aria2Args = "\"" + url + "\" --continue=true --dir=\"" + saveTo + "\" --out=\"" + fileName + "\" --user-agent=\"" + WebService.RequestUserAgent + "\""; if (proxy.Length > 0) { @@ -806,7 +768,7 @@ namespace MaaWpfGui // 设定相关属性 httpWebRequest.Method = "GET"; - httpWebRequest.UserAgent = RequestUserAgent; + httpWebRequest.UserAgent = WebService.RequestUserAgent; httpWebRequest.Accept = contentType; if (proxy.Length > 0) { From 3239ad051fe936e19a32f12333b5c24be4ba1196 Mon Sep 17 00:00:00 2001 From: uye <2396806385@qq.com> Date: Fri, 6 Jan 2023 02:56:27 +0800 Subject: [PATCH 2/4] =?UTF-8?q?feat:=20=E4=BB=8Eweb=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E6=B4=BB=E5=8A=A8=E5=85=B3=E5=8D=A1=E4=BF=A1=E6=81=AF=E4=B8=8E?= =?UTF-8?q?=E6=B4=BB=E5=8A=A8=E5=BC=80=E6=94=BE=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/MaaWpfGui/Helper/StageManager.cs | 88 ++++++++++++++++++---------- 1 file changed, 56 insertions(+), 32 deletions(-) diff --git a/src/MaaWpfGui/Helper/StageManager.cs b/src/MaaWpfGui/Helper/StageManager.cs index 604474598a..10178be377 100644 --- a/src/MaaWpfGui/Helper/StageManager.cs +++ b/src/MaaWpfGui/Helper/StageManager.cs @@ -13,6 +13,7 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.IO; using System.Linq; using System.Text; @@ -34,22 +35,6 @@ namespace MaaWpfGui /// public StageManager() { - var sideStory = new StageActivityInfo() - { - Tip = "SideStory「将进酒」复刻活动", - StageName = "IW", - UtcStartTime = new DateTime(2023, 1, 1, 16, 0, 0).AddHours(-8), - UtcExpireTime = new DateTime(2023, 1, 11, 4, 0, 0).AddHours(-8), - }; - - var resourceCollection = new StageActivityInfo() - { - Tip = "「感谢庆典」,“资源收集”限时全天开放", - UtcStartTime = new DateTime(2022, 11, 15, 16, 0, 0).AddHours(-8), - UtcExpireTime = new DateTime(2022, 11, 29, 4, 0, 0).AddHours(-8), - IsResourceCollection = true, - }; - _stages = new Dictionary { // 这里会被 “剩余理智” 复用,第一个必须是 string.Empty 的 @@ -58,20 +43,60 @@ namespace MaaWpfGui }; var activity = WebService.RequestMaaApiWithCache("StageActivity.json"); - foreach (var stageObj in activity["Official"] as JArray) + + // activity = (JObject)JsonConvert.DeserializeObject(File.ReadAllText("StageActivity.json")); + var resourceCollection = new StageActivityInfo() { - _stages.Add(stageObj["value"].ToString(), - new StageInfo - { - Display = stageObj["display"].ToString(), - Value = stageObj["value"].ToString(), - Drop = stageObj["drop"].ToString(), - Activity = sideStory, - }); + IsResourceCollection = true, + }; + try + { + var resource = activity["Official"]["resourceCollection"]; + resourceCollection.Tip = resource?["Tip"]?.ToString(); + resourceCollection.UtcStartTime = DateTime.ParseExact(resource["UtcStartTime"].ToString(), + "yyyy/MM/dd HH:mm:ss", + CultureInfo.InvariantCulture).AddHours(-Convert.ToInt32(resource?["TimeZone"].ToString() ?? "0")); + resourceCollection.UtcExpireTime = DateTime.ParseExact(resource["UtcExpireTime"].ToString(), + "yyyy/MM/dd HH:mm:ss", + CultureInfo.InvariantCulture).AddHours(-Convert.ToInt32(resource?["TimeZone"].ToString() ?? "0")); + } + catch + { + // DoNothing } - // TODO: 把这些也放到 web json 上 - new Dictionary { + foreach (var stageObj in activity["Official"]["sideStoryStage"] as JArray) + { + try + { + _stages.Add( + stageObj["Value"].ToString(), + new StageInfo + { + Display = stageObj["Display"].ToString(), + Value = stageObj["Value"].ToString(), + Drop = stageObj["Drop"].ToString(), + Activity = new StageActivityInfo() + { + Tip = stageObj["Activity"]["Tip"].ToString(), + StageName = stageObj["Activity"]["StageName"].ToString(), + UtcStartTime = DateTime.ParseExact(stageObj["Activity"]["UtcStartTime"].ToString(), + "yyyy/MM/dd HH:mm:ss", + CultureInfo.InvariantCulture).AddHours(-Convert.ToInt32(stageObj["Activity"]?["TimeZone"]?.ToString() ?? "0")), + UtcExpireTime = DateTime.ParseExact(stageObj["Activity"]["UtcExpireTime"].ToString(), + "yyyy/MM/dd HH:mm:ss", + CultureInfo.InvariantCulture).AddHours(-Convert.ToInt32(stageObj["Activity"]?["TimeZone"]?.ToString() ?? "0")), + }, + }); + } + catch + { + // DoNothing + } + } + + foreach (var kvp in new Dictionary + { // 主线关卡 { "1-7", new StageInfo { Display = "1-7", Value = "1-7" } }, @@ -100,11 +125,10 @@ namespace MaaWpfGui // 周一和周日的关卡提示 { "Pormpt1", new StageInfo { Tip = Localization.GetString("Pormpt1"), OpenDays = new[] { DayOfWeek.Monday }, IsHidden = true } }, { "Pormpt2", new StageInfo { Tip = Localization.GetString("Pormpt2"), OpenDays = new[] { DayOfWeek.Sunday }, IsHidden = true } }, - - // 老版本「当前/上次」关卡导航 - // new StageInfo { Display = Localization.GetString("CurrentStage"), Value = string.Empty }, - // new StageInfo { Display = Localization.GetString("LastBattle"), Value = "LastBattle" }, - }; + }) + { + _stages.Add(kvp.Key, kvp.Value); + } } /// From edcca46875d32179354c1f1481cb46049f22c3fe Mon Sep 17 00:00:00 2001 From: uye <2396806385@qq.com> Date: Fri, 6 Jan 2023 03:30:46 +0800 Subject: [PATCH 3/4] =?UTF-8?q?refactor:=20=E6=8F=90=E5=8F=96=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E5=BC=80=E6=94=BE=E6=97=B6=E9=97=B4=E7=9A=84=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/MaaWpfGui/Helper/StageManager.cs | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/MaaWpfGui/Helper/StageManager.cs b/src/MaaWpfGui/Helper/StageManager.cs index 10178be377..b8663ae95e 100644 --- a/src/MaaWpfGui/Helper/StageManager.cs +++ b/src/MaaWpfGui/Helper/StageManager.cs @@ -44,21 +44,22 @@ namespace MaaWpfGui var activity = WebService.RequestMaaApiWithCache("StageActivity.json"); - // activity = (JObject)JsonConvert.DeserializeObject(File.ReadAllText("StageActivity.json")); var resourceCollection = new StageActivityInfo() { IsResourceCollection = true, }; + + static DateTime GetDateTime(JToken keyValuePairs, string key) + => DateTime.ParseExact(keyValuePairs[key].ToString(), + "yyyy/MM/dd HH:mm:ss", + CultureInfo.InvariantCulture).AddHours(-Convert.ToInt32(keyValuePairs?["TimeZone"].ToString() ?? "0")); + try { var resource = activity["Official"]["resourceCollection"]; resourceCollection.Tip = resource?["Tip"]?.ToString(); - resourceCollection.UtcStartTime = DateTime.ParseExact(resource["UtcStartTime"].ToString(), - "yyyy/MM/dd HH:mm:ss", - CultureInfo.InvariantCulture).AddHours(-Convert.ToInt32(resource?["TimeZone"].ToString() ?? "0")); - resourceCollection.UtcExpireTime = DateTime.ParseExact(resource["UtcExpireTime"].ToString(), - "yyyy/MM/dd HH:mm:ss", - CultureInfo.InvariantCulture).AddHours(-Convert.ToInt32(resource?["TimeZone"].ToString() ?? "0")); + resourceCollection.UtcStartTime = GetDateTime(resource, "UtcStartTime"); + resourceCollection.UtcExpireTime = GetDateTime(resource, "UtcExpireTime"); } catch { @@ -80,12 +81,8 @@ namespace MaaWpfGui { Tip = stageObj["Activity"]["Tip"].ToString(), StageName = stageObj["Activity"]["StageName"].ToString(), - UtcStartTime = DateTime.ParseExact(stageObj["Activity"]["UtcStartTime"].ToString(), - "yyyy/MM/dd HH:mm:ss", - CultureInfo.InvariantCulture).AddHours(-Convert.ToInt32(stageObj["Activity"]?["TimeZone"]?.ToString() ?? "0")), - UtcExpireTime = DateTime.ParseExact(stageObj["Activity"]["UtcExpireTime"].ToString(), - "yyyy/MM/dd HH:mm:ss", - CultureInfo.InvariantCulture).AddHours(-Convert.ToInt32(stageObj["Activity"]?["TimeZone"]?.ToString() ?? "0")), + UtcStartTime = GetDateTime(stageObj["Activity"], "UtcStartTime"), + UtcExpireTime = GetDateTime(stageObj["Activity"], "UtcExpireTime"), }, }); } From 01cf1f94189c61a01631c828f932d1e98c73e692 Mon Sep 17 00:00:00 2001 From: MistEO Date: Fri, 6 Jan 2023 17:52:09 +0800 Subject: [PATCH 4/4] fix: proxy for c# webservice --- src/MaaWpfGui/Helper/WebService.cs | 11 ++++++----- src/MaaWpfGui/Main/SettingsViewModel.cs | 2 ++ 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/MaaWpfGui/Helper/WebService.cs b/src/MaaWpfGui/Helper/WebService.cs index 7b5183a0e8..aaa75d086e 100644 --- a/src/MaaWpfGui/Helper/WebService.cs +++ b/src/MaaWpfGui/Helper/WebService.cs @@ -16,6 +16,8 @@ namespace MaaWpfGui.Helper { public const string RequestUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36 Edg/97.0.1072.76"; + public static string Proxy { get; set; } = string.Empty; + public static string RequestUrl(string url) { try @@ -24,6 +26,10 @@ namespace MaaWpfGui.Helper httpWebRequest.Method = "GET"; httpWebRequest.UserAgent = RequestUserAgent; httpWebRequest.Accept = "application/vnd.github.v3+json"; + if (!string.IsNullOrWhiteSpace(Proxy)) + { + httpWebRequest.Proxy = new WebProxy(Proxy); + } var httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse; if (httpWebResponse.StatusCode != HttpStatusCode.OK) @@ -37,11 +43,6 @@ namespace MaaWpfGui.Helper httpWebResponse.Close(); return responseContent; } - catch (WebException e) - { - Logger.Error(e.ToString(), MethodBase.GetCurrentMethod().Name); - return null; - } catch (Exception e) { Logger.Error(e.ToString(), MethodBase.GetCurrentMethod().Name); diff --git a/src/MaaWpfGui/Main/SettingsViewModel.cs b/src/MaaWpfGui/Main/SettingsViewModel.cs index fa0753f481..f4e1b778bc 100644 --- a/src/MaaWpfGui/Main/SettingsViewModel.cs +++ b/src/MaaWpfGui/Main/SettingsViewModel.cs @@ -23,6 +23,7 @@ using System.Threading; using System.Threading.Tasks; using System.Windows; using IWshRuntimeLibrary; +using MaaWpfGui.Helper; using MaaWpfGui.MaaHotKeys; using Stylet; using StyletIoC; @@ -1880,6 +1881,7 @@ namespace MaaWpfGui get => _proxy; set { + WebService.Proxy = value; SetAndNotify(ref _proxy, value); ViewStatusStorage.Set("VersionUpdate.Proxy", value); }