mirror of
https://github.com/MaaAssistantArknights/MaaAssistantArknights.git
synced 2026-07-16 09:50:40 +08:00
perf: 优化代码,消除警告
This commit is contained in:
@@ -131,7 +131,7 @@ namespace MaaWpfGui.Configuration
|
||||
};
|
||||
}
|
||||
|
||||
public static Root Root => _rootConfig.Value;
|
||||
private static Root Root => _rootConfig.Value;
|
||||
|
||||
public static readonly SpecificConfig CurrentConfig = Root.CurrentConfig;
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ namespace MaaWpfGui.Configuration
|
||||
{
|
||||
public class SpecificConfig
|
||||
{
|
||||
// ReSharper disable once UnusedAutoPropertyAccessor.Global
|
||||
// ReSharper disable once AutoPropertyCanBeMadeGetOnly.Global
|
||||
public GUI GUI { get; set; } = new GUI();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,6 +75,8 @@ namespace MaaWpfGui.Helper
|
||||
|
||||
public static IMaaHotKeyActionHandler MaaHotKeyActionHandler { get; private set; }
|
||||
|
||||
// 别的地方有用到这个吗?
|
||||
// ReSharper disable once UnusedAutoPropertyAccessor.Global
|
||||
public static RemoteControlService RemoteControlService { get; private set; }
|
||||
|
||||
public static IMainWindowManager MainWindowManager { get; private set; }
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using System.Windows;
|
||||
@@ -53,13 +54,12 @@ namespace MaaWpfGui.Helper
|
||||
return local;
|
||||
}
|
||||
|
||||
foreach (var lang in SupportedLanguages)
|
||||
foreach (var lang in from lang in SupportedLanguages
|
||||
let key = lang.Key.Contains("-") ? lang.Key.Split('-')[0] : lang.Key
|
||||
where local.StartsWith(key) || key.StartsWith(local)
|
||||
select lang)
|
||||
{
|
||||
var key = lang.Key.Contains("-") ? lang.Key.Split('-')[0] : lang.Key;
|
||||
if (local.StartsWith(key) || key.StartsWith(local))
|
||||
{
|
||||
return lang.Key;
|
||||
}
|
||||
return lang.Key;
|
||||
}
|
||||
|
||||
return "en-us";
|
||||
|
||||
@@ -43,6 +43,8 @@ namespace MaaWpfGui.Models
|
||||
NotModified,
|
||||
}
|
||||
|
||||
// 只有 Release 版本才会检查更新
|
||||
// ReSharper disable once UnusedMember.Global
|
||||
public static async void UpdateAndToast()
|
||||
{
|
||||
var ret = await Update();
|
||||
@@ -86,13 +88,18 @@ namespace MaaWpfGui.Models
|
||||
{
|
||||
var sRet = await UpdateFileWithETag(MaaUrls.MaaResourceApi, file, file);
|
||||
|
||||
if (sRet == UpdateResult.Failed)
|
||||
switch (sRet)
|
||||
{
|
||||
ret = UpdateResult.Failed;
|
||||
}
|
||||
else if (sRet == UpdateResult.Success)
|
||||
{
|
||||
ETagCache.Save();
|
||||
case UpdateResult.Failed:
|
||||
ret = UpdateResult.Failed;
|
||||
break;
|
||||
case UpdateResult.Success:
|
||||
ETagCache.Save();
|
||||
break;
|
||||
case UpdateResult.NotModified:
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
if (ret == UpdateResult.NotModified && sRet == UpdateResult.Success)
|
||||
@@ -109,13 +116,17 @@ namespace MaaWpfGui.Models
|
||||
private static async Task<UpdateResult> UpdateFilesWithIndex()
|
||||
{
|
||||
var indexSRet = await UpdateFileWithETag(MaaUrls.MaaResourceApi, MaaDynamicFilesIndex, MaaDynamicFilesIndex);
|
||||
if (indexSRet == UpdateResult.Failed)
|
||||
switch (indexSRet)
|
||||
{
|
||||
return UpdateResult.Failed;
|
||||
}
|
||||
else if (indexSRet == UpdateResult.Success)
|
||||
{
|
||||
ETagCache.Save();
|
||||
case UpdateResult.Failed:
|
||||
return UpdateResult.Failed;
|
||||
case UpdateResult.Success:
|
||||
ETagCache.Save();
|
||||
break;
|
||||
case UpdateResult.NotModified:
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
var indexPath = Path.Combine(Environment.CurrentDirectory, MaaDynamicFilesIndex);
|
||||
|
||||
@@ -9,20 +9,20 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Media.Imaging;
|
||||
using System.Windows.Threading;
|
||||
using MaaWpfGui.Constants;
|
||||
using MaaWpfGui.Helper;
|
||||
using MaaWpfGui.Services.Web;
|
||||
using MaaWpfGui.States;
|
||||
using MaaWpfGui.ViewModels.UI;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Serilog;
|
||||
using Windows.Media.Protection.PlayReady;
|
||||
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
|
||||
|
||||
namespace MaaWpfGui.Services.RemoteControl
|
||||
{
|
||||
/// <summary>
|
||||
/// The view model of remote control.
|
||||
/// </summary>
|
||||
// 通过 container.Get<RemoteControlService>(); 实例化或获取实例
|
||||
// ReSharper disable once ClassNeverInstantiated.Global
|
||||
public class RemoteControlService
|
||||
{
|
||||
private readonly Task _pollJobTask = Task.CompletedTask;
|
||||
@@ -33,7 +33,7 @@ namespace MaaWpfGui.Services.RemoteControl
|
||||
|
||||
public RemoteControlService()
|
||||
{
|
||||
_pollJobTask = _pollJobTask.ContinueWith(async (_) =>
|
||||
_pollJobTask = _pollJobTask.ContinueWith(async _ =>
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
@@ -51,7 +51,7 @@ namespace MaaWpfGui.Services.RemoteControl
|
||||
// ReSharper disable once FunctionNeverReturns
|
||||
});
|
||||
|
||||
_executeJobTask = _executeJobTask.ContinueWith(async (_) =>
|
||||
_executeJobTask = _executeJobTask.ContinueWith(async _ =>
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
@@ -204,7 +204,7 @@ namespace MaaWpfGui.Services.RemoteControl
|
||||
|
||||
var uid = Instances.SettingsViewModel.RemoteControlUserIdentity;
|
||||
var did = Instances.SettingsViewModel.RemoteControlDeviceIdentity;
|
||||
var response = await Instances.HttpService.PostAsJsonAsync(new Uri(endpoint), new { user=uid, device = did});
|
||||
var response = await Instances.HttpService.PostAsJsonAsync(new Uri(endpoint), new { user = uid, device = did });
|
||||
if (response == null)
|
||||
{
|
||||
Log.Logger.Error("RemoteControlService endpoint failed.");
|
||||
@@ -219,17 +219,20 @@ namespace MaaWpfGui.Services.RemoteControl
|
||||
foreach (var task in tasks.OfType<JObject>())
|
||||
{
|
||||
var type = task.GetValue("type")?.Value<string>();
|
||||
if (!string.IsNullOrWhiteSpace(type))
|
||||
if (string.IsNullOrWhiteSpace(type))
|
||||
{
|
||||
// It is a valid task
|
||||
var id = task.GetValue("id")?.Value<string>();
|
||||
if (!_executedTaskIds.Contains(id))
|
||||
{
|
||||
_executedTaskIds.Add(id);
|
||||
|
||||
_taskQueue.Enqueue(task);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// It is a valid task
|
||||
var id = task.GetValue("id")?.Value<string>();
|
||||
if (_executedTaskIds.Contains(id))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
_executedTaskIds.Add(id);
|
||||
_taskQueue.Enqueue(task);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -247,25 +250,25 @@ namespace MaaWpfGui.Services.RemoteControl
|
||||
switch (type)
|
||||
{
|
||||
case "LinkStart":
|
||||
{
|
||||
// 一键长草特殊任务
|
||||
await _runningState.UntilIdleAsync();
|
||||
var startLogStr = string.Format(LocalizationHelper.GetString("RemoteControlReceivedTask"), type, id);
|
||||
|
||||
Application.Current.Dispatcher.Invoke(() =>
|
||||
{
|
||||
Instances.TaskQueueViewModel.AddLog(startLogStr);
|
||||
Instances.TaskQueueViewModel.LinkStart();
|
||||
});
|
||||
await _runningState.UntilIdleAsync();
|
||||
// 一键长草特殊任务
|
||||
await _runningState.UntilIdleAsync();
|
||||
var startLogStr = string.Format(LocalizationHelper.GetString("RemoteControlReceivedTask"), type, id);
|
||||
|
||||
var stopLogStr = string.Format(LocalizationHelper.GetString("RemoteControlCompletedTask"), type, id);
|
||||
Application.Current.Dispatcher.Invoke(() =>
|
||||
{
|
||||
Instances.TaskQueueViewModel.AddLog(stopLogStr);
|
||||
});
|
||||
break;
|
||||
}
|
||||
Application.Current.Dispatcher.Invoke(() =>
|
||||
{
|
||||
Instances.TaskQueueViewModel.AddLog(startLogStr);
|
||||
Instances.TaskQueueViewModel.LinkStart();
|
||||
});
|
||||
await _runningState.UntilIdleAsync();
|
||||
|
||||
var stopLogStr = string.Format(LocalizationHelper.GetString("RemoteControlCompletedTask"), type, id);
|
||||
Application.Current.Dispatcher.Invoke(() =>
|
||||
{
|
||||
Instances.TaskQueueViewModel.AddLog(stopLogStr);
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
||||
case "LinkStart-Base":
|
||||
case "LinkStart-WakeUp":
|
||||
@@ -275,66 +278,64 @@ namespace MaaWpfGui.Services.RemoteControl
|
||||
case "LinkStart-Mission":
|
||||
case "LinkStart-AutoRoguelike":
|
||||
case "LinkStart-ReclamationAlgorithm":
|
||||
{
|
||||
await LinkStart(new[] { type.Split('-')[1] });
|
||||
break;
|
||||
}
|
||||
{
|
||||
await LinkStart(new[] { type.Split('-')[1] });
|
||||
break;
|
||||
}
|
||||
|
||||
case "Toolbox-GachaOnce":
|
||||
{
|
||||
await _runningState.UntilIdleAsync();
|
||||
Instances.RecognizerViewModel.GachaOnce();
|
||||
while (!Instances.RecognizerViewModel.GachaDone)
|
||||
{
|
||||
await Task.Delay(100); // 暂停100毫秒以避免密集循环
|
||||
}
|
||||
await _runningState.UntilIdleAsync();
|
||||
Instances.RecognizerViewModel.GachaOnce();
|
||||
while (!Instances.RecognizerViewModel.GachaDone)
|
||||
{
|
||||
await Task.Delay(100); // 暂停100毫秒以避免密集循环
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case "Toolbox-GachaTenTimes":
|
||||
{
|
||||
await _runningState.UntilIdleAsync();
|
||||
Instances.RecognizerViewModel.GachaTenTimes();
|
||||
while (!Instances.RecognizerViewModel.GachaDone)
|
||||
{
|
||||
await Task.Delay(100); // 暂停100毫秒以避免密集循环
|
||||
await _runningState.UntilIdleAsync();
|
||||
Instances.RecognizerViewModel.GachaTenTimes();
|
||||
while (!Instances.RecognizerViewModel.GachaDone)
|
||||
{
|
||||
await Task.Delay(100); // 暂停100毫秒以避免密集循环
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case "CaptureImage":
|
||||
{
|
||||
string errMsg = string.Empty;
|
||||
bool connected = await Task.Run(() => Instances.AsstProxy.AsstConnect(ref errMsg));
|
||||
if (connected)
|
||||
{
|
||||
var image = Instances.AsstProxy.AsstGetImage();
|
||||
if (image == null)
|
||||
string errMsg = string.Empty;
|
||||
bool connected = await Task.Run(() => Instances.AsstProxy.AsstConnect(ref errMsg));
|
||||
if (connected)
|
||||
{
|
||||
status = "FAILED";
|
||||
var image = Instances.AsstProxy.AsstGetImage();
|
||||
if (image == null)
|
||||
{
|
||||
status = "FAILED";
|
||||
break;
|
||||
}
|
||||
|
||||
byte[] bytes;
|
||||
using (MemoryStream stream = new MemoryStream())
|
||||
{
|
||||
PngBitmapEncoder encoder = new PngBitmapEncoder();
|
||||
encoder.Frames.Add(BitmapFrame.Create(image));
|
||||
encoder.Save(stream);
|
||||
bytes = stream.ToArray();
|
||||
}
|
||||
|
||||
payload = Convert.ToBase64String(bytes);
|
||||
break;
|
||||
}
|
||||
|
||||
byte[] bytes;
|
||||
using (MemoryStream stream = new MemoryStream())
|
||||
{
|
||||
PngBitmapEncoder encoder = new PngBitmapEncoder();
|
||||
encoder.Frames.Add(BitmapFrame.Create(image));
|
||||
encoder.Save(stream);
|
||||
bytes = stream.ToArray();
|
||||
}
|
||||
|
||||
payload = Convert.ToBase64String(bytes);
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
status = "FAILED";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
case "Settings-ConnectAddress":
|
||||
// ConfigurationHelper.SetValue(type.Split('-')[1], data);
|
||||
@@ -345,6 +346,7 @@ namespace MaaWpfGui.Services.RemoteControl
|
||||
|
||||
break;
|
||||
|
||||
// ReSharper disable once RedundantEmptySwitchSection
|
||||
default:
|
||||
// 未知的Type统一直接发给MAACore
|
||||
// No! 未知的任务一概不处理
|
||||
@@ -360,9 +362,9 @@ namespace MaaWpfGui.Services.RemoteControl
|
||||
{
|
||||
user = uid,
|
||||
device = did,
|
||||
status = status,
|
||||
status,
|
||||
task = id,
|
||||
payload = payload,
|
||||
payload,
|
||||
});
|
||||
if (response == null)
|
||||
{
|
||||
@@ -385,7 +387,8 @@ namespace MaaWpfGui.Services.RemoteControl
|
||||
/// </remarks>
|
||||
/// <param name="originalNames">指定的任务列表。</param>
|
||||
/// <returns>异步任务,无返回结果。</returns>
|
||||
public async Task LinkStart(IEnumerable<string> originalNames)
|
||||
// 这个是不是可以直接做到 TaskQueueViewModel 里面去?
|
||||
private async Task LinkStart(IEnumerable<string> originalNames)
|
||||
{
|
||||
await _runningState.UntilIdleAsync();
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ namespace MaaWpfGui.ViewModels.UI
|
||||
/// <summary>
|
||||
/// The view model of version update.
|
||||
/// </summary>
|
||||
// 通过 container.Get<TaskQueueViewModel>(); 实例化或获取实例
|
||||
// 通过 container.Get<AnnouncementViewModel>(); 实例化或获取实例
|
||||
// ReSharper disable once ClassNeverInstantiated.Global
|
||||
public class AnnouncementViewModel : Screen
|
||||
{
|
||||
|
||||
@@ -38,7 +38,7 @@ namespace MaaWpfGui.ViewModels.UI
|
||||
/// <summary>
|
||||
/// The view model of copilot.
|
||||
/// </summary>
|
||||
// 通过 container.Get<CopilotViewModel>(); 实例化或获取实例,需要添加 qodana ignore rule
|
||||
// 通过 container.Get<CopilotViewModel>(); 实例化或获取实例
|
||||
// ReSharper disable once ClassNeverInstantiated.Global
|
||||
public class CopilotViewModel : Screen
|
||||
{
|
||||
|
||||
@@ -604,29 +604,14 @@ namespace MaaWpfGui.ViewModels.UI
|
||||
List<Tuple<string, int>> operHave = new List<Tuple<string, int>>();
|
||||
List<Tuple<string, int>> operNotHave = new List<Tuple<string, int>>();
|
||||
|
||||
string localizedName;
|
||||
switch (ConfigurationHelper.GetValue(ConfigurationKeys.Localization, string.Empty))
|
||||
string localizedName = ConfigurationHelper.GetValue(ConfigurationKeys.Localization, string.Empty) switch
|
||||
{
|
||||
case "zh-cn":
|
||||
localizedName = "name";
|
||||
break;
|
||||
|
||||
case "ja-jp":
|
||||
localizedName = "name_jp";
|
||||
break;
|
||||
|
||||
case "ko-kr":
|
||||
localizedName = "name_kr";
|
||||
break;
|
||||
|
||||
case "zh-tw":
|
||||
localizedName = "name_tw";
|
||||
break;
|
||||
|
||||
default:
|
||||
localizedName = "name_en";
|
||||
break;
|
||||
}
|
||||
"zh-cn" => "name",
|
||||
"ja-jp" => "name_jp",
|
||||
"ko-kr" => "name_kr",
|
||||
"zh-tw" => "name_tw",
|
||||
_ => "name_en"
|
||||
};
|
||||
|
||||
foreach (JObject operBox in operBoxes.Cast<JObject>())
|
||||
{
|
||||
|
||||
@@ -127,12 +127,14 @@ namespace MaaWpfGui.ViewModels.UI
|
||||
|
||||
public void Sober()
|
||||
{
|
||||
if (Cheers && Language == PallasLangKey)
|
||||
if (!Cheers || Language != PallasLangKey)
|
||||
{
|
||||
ConfigurationHelper.SetValue(ConfigurationKeys.Localization, SoberLanguage);
|
||||
Hangover = true;
|
||||
Cheers = false;
|
||||
return;
|
||||
}
|
||||
|
||||
ConfigurationHelper.SetValue(ConfigurationKeys.Localization, SoberLanguage);
|
||||
Hangover = true;
|
||||
Cheers = false;
|
||||
}
|
||||
|
||||
protected override void OnInitialActivate()
|
||||
@@ -148,11 +150,15 @@ namespace MaaWpfGui.ViewModels.UI
|
||||
|
||||
#region Remote Control
|
||||
|
||||
// UI 绑定的方法
|
||||
// ReSharper disable once UnusedMember.Global
|
||||
public async void RemoteControlConnectionTest()
|
||||
{
|
||||
await RemoteControlService.ConnectionTest();
|
||||
}
|
||||
|
||||
// UI 绑定的方法
|
||||
// ReSharper disable once UnusedMember.Global
|
||||
public void RemoteControlRegenerateDeviceIdentity()
|
||||
{
|
||||
RemoteControlService.RegenerateDeviceIdentity();
|
||||
@@ -3394,6 +3400,8 @@ namespace MaaWpfGui.ViewModels.UI
|
||||
case DarkModeType.SyncWithOs:
|
||||
ThemeHelper.SwitchToSyncWithOsMode();
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -406,6 +406,9 @@ namespace MaaWpfGui.ViewModels.UI
|
||||
{
|
||||
AskToRestart();
|
||||
}
|
||||
#else
|
||||
// 跑个空任务避免 async warning
|
||||
await Task.Run(() => {});
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user