From ae1e84cb376b94ce29f9adfa94e0ee07fca9be1d Mon Sep 17 00:00:00 2001 From: MistEO Date: Thu, 5 Jan 2023 00:55:05 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=88=9D=E6=AD=A5=E6=90=AD=E5=BB=BA?= =?UTF-8?q?=E4=BB=8Eweb=E8=8E=B7=E5=8F=96UI=E4=BF=A1=E6=81=AF=E7=9A=84?= =?UTF-8?q?=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) {