diff --git a/src/MaaWpfGui/Helper/StageManager.cs b/src/MaaWpfGui/Helper/StageManager.cs index 1c7a0943fd..b8663ae95e 100644 --- a/src/MaaWpfGui/Helper/StageManager.cs +++ b/src/MaaWpfGui/Helper/StageManager.cs @@ -13,10 +13,13 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.IO; using System.Linq; using System.Text; +using MaaWpfGui.Helper; using Newtonsoft.Json; +using Newtonsoft.Json.Linq; namespace MaaWpfGui { @@ -32,33 +35,65 @@ 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 的 // 「当前/上次」关卡导航 { 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"); + 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 = GetDateTime(resource, "UtcStartTime"); + resourceCollection.UtcExpireTime = GetDateTime(resource, "UtcExpireTime"); + } + catch + { + // DoNothing + } + + 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 = GetDateTime(stageObj["Activity"], "UtcStartTime"), + UtcExpireTime = GetDateTime(stageObj["Activity"], "UtcExpireTime"), + }, + }); + } + catch + { + // DoNothing + } + } + + foreach (var kvp in new Dictionary + { // 主线关卡 { "1-7", new StageInfo { Display = "1-7", Value = "1-7" } }, @@ -87,11 +122,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); + } } /// diff --git a/src/MaaWpfGui/Helper/WebService.cs b/src/MaaWpfGui/Helper/WebService.cs new file mode 100644 index 0000000000..aaa75d086e --- /dev/null +++ b/src/MaaWpfGui/Helper/WebService.cs @@ -0,0 +1,95 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net; +using System.Reflection; +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 Proxy { get; set; } = string.Empty; + + 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"; + if (!string.IsNullOrWhiteSpace(Proxy)) + { + httpWebRequest.Proxy = new WebProxy(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 e) + { + Logger.Error(e.ToString(), MethodBase.GetCurrentMethod().Name); + 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/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); } diff --git a/src/MaaWpfGui/Main/VersionUpdateViewModel.cs b/src/MaaWpfGui/Main/VersionUpdateViewModel.cs index bb5a4698fa..a73ba09982 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; @@ -636,48 +636,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 (WebException) - { - return null; - } - catch (Exception e) - { - Logger.Error(e.ToString(), MethodBase.GetCurrentMethod().Name); - return null; - } - } - private string RequestGithubApi(string url, int retryTimes) { string response = string.Empty; @@ -686,7 +644,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; @@ -778,7 +736,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) { @@ -831,7 +789,7 @@ namespace MaaWpfGui // 设定相关属性 httpWebRequest.Method = "GET"; - httpWebRequest.UserAgent = RequestUserAgent; + httpWebRequest.UserAgent = WebService.RequestUserAgent; httpWebRequest.Accept = contentType; if (proxy.Length > 0) { @@ -1013,4 +971,4 @@ namespace MaaWpfGui Process.Start(e.Parameter.ToString()); } } -} +} \ No newline at end of file