mirror of
https://github.com/MaaAssistantArknights/MaaAssistantArknights.git
synced 2026-07-18 10:10:45 +08:00
feat: 初步搭建从web获取UI信息的框架
This commit is contained in:
@@ -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<string, StageInfo> {
|
||||
// 主线关卡
|
||||
{ "1-7", new StageInfo { Display = "1-7", Value = "1-7" } },
|
||||
|
||||
|
||||
88
src/MaaWpfGui/Helper/WebService.cs
Normal file
88
src/MaaWpfGui/Helper/WebService.cs
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -104,6 +104,7 @@
|
||||
<SubType>Designer</SubType>
|
||||
</ApplicationDefinition>
|
||||
<Compile Include="Helper\Utils.cs" />
|
||||
<Compile Include="Helper\WebService.cs" />
|
||||
<Compile Include="Main\Bootstrapper.cs" />
|
||||
<Compile Include="GlobalSuppressions.cs" />
|
||||
<Compile Include="Main\AsstProxy.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
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 访问 API
|
||||
/// </summary>
|
||||
/// <param name="url">API 地址</param>
|
||||
/// <returns>返回 API 的返回值,如出现错误则返回空字符串</returns>
|
||||
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<SettingsViewModel>();
|
||||
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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user