From 8cf76efb1186d2fe307d9fb396dce532562270b5 Mon Sep 17 00:00:00 2001 From: status102 Date: Sun, 15 Oct 2023 10:21:28 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E8=BF=81=E7=A7=BB=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E4=BA=86MaaCore=E7=9A=84=E6=96=B9=E6=B3=95=EF=BC=8C?= =?UTF-8?q?=E7=BB=9F=E4=B8=80=E5=90=88=E5=B9=B6=E8=87=B3MaaService?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/MaaWpfGui/Helper/WindowManager.cs | 2 - src/MaaWpfGui/Main/AsstProxy.cs | 83 +++++-------------- src/MaaWpfGui/Services/MaaService.cs | 59 +++++++++++++ src/MaaWpfGui/Services/StageManager.cs | 7 +- .../ViewModels/UI/CopilotViewModel.cs | 1 + .../ViewModels/UI/SettingsViewModel.cs | 13 ++- .../ViewModels/UI/VersionUpdateViewModel.cs | 6 +- 7 files changed, 91 insertions(+), 80 deletions(-) create mode 100644 src/MaaWpfGui/Services/MaaService.cs diff --git a/src/MaaWpfGui/Helper/WindowManager.cs b/src/MaaWpfGui/Helper/WindowManager.cs index b6ae54845d..669e3ad500 100644 --- a/src/MaaWpfGui/Helper/WindowManager.cs +++ b/src/MaaWpfGui/Helper/WindowManager.cs @@ -208,11 +208,9 @@ namespace MaaWpfGui.Helper SetWindowPlacement(window.Handle, ref wp); return true; - } #region Win32 API declarations to set and get window placement - [DllImport("user32.dll")] private static extern bool SetWindowPlacement(IntPtr hWnd, [In] ref WindowPlacement lpwndpl); diff --git a/src/MaaWpfGui/Main/AsstProxy.cs b/src/MaaWpfGui/Main/AsstProxy.cs index ce6209ed6a..14936a68bd 100644 --- a/src/MaaWpfGui/Main/AsstProxy.cs +++ b/src/MaaWpfGui/Main/AsstProxy.cs @@ -27,6 +27,7 @@ using HandyControl.Data; using MaaWpfGui.Constants; using MaaWpfGui.Extensions; using MaaWpfGui.Helper; +using MaaWpfGui.Services; using MaaWpfGui.Services.Notification; using MaaWpfGui.States; using MaaWpfGui.ViewModels.UI; @@ -35,14 +36,12 @@ using Newtonsoft.Json.Linq; using Serilog; using Stylet; using static MaaWpfGui.Helper.Instances.Data; +using AsstHandle = System.IntPtr; +using AsstInstanceOptionKey = System.Int32; +using AsstTaskId = System.Int32; namespace MaaWpfGui.Main { - using AsstHandle = IntPtr; - using AsstInstanceOptionKey = Int32; - - using AsstTaskId = Int32; - /// /// MaaCore 代理类。 /// @@ -51,10 +50,6 @@ namespace MaaWpfGui.Main private readonly RunningState _runningState; private static readonly ILogger _logger = Log.ForContext(); - private delegate void CallbackDelegate(int msg, IntPtr jsonBuffer, IntPtr customArg); - - private delegate void ProcCallbackMsg(AsstMsg msg, JObject details); - private static unsafe byte[] EncodeNullTerminatedUtf8(string s) { var enc = Encoding.UTF8.GetEncoder(); @@ -71,40 +66,25 @@ namespace MaaWpfGui.Main } } - [DllImport("MaaCore.dll")] - private static extern unsafe bool AsstLoadResource(byte* dirname); - private static unsafe bool AsstLoadResource(string dirname) { fixed (byte* ptr = EncodeNullTerminatedUtf8(dirname)) { _logger.Information($"AsstLoadResource dirname: {dirname}"); - var ret = AsstLoadResource(ptr); + var ret = MaaService.AsstLoadResource(ptr); _logger.Information($"AsstLoadResource ret: {ret}"); return ret; } } - [DllImport("MaaCore.dll")] - private static extern AsstHandle AsstCreateEx(CallbackDelegate callback, IntPtr customArg); - - [DllImport("MaaCore.dll")] - private static extern void AsstDestroy(AsstHandle handle); - - [DllImport("MaaCore.dll")] - private static extern unsafe bool AsstSetInstanceOption(AsstHandle handle, AsstInstanceOptionKey key, byte* value); - private static unsafe bool AsstSetInstanceOption(AsstHandle handle, AsstInstanceOptionKey key, string value) { fixed (byte* ptr1 = EncodeNullTerminatedUtf8(value)) { - return AsstSetInstanceOption(handle, key, ptr1); + return MaaService.AsstSetInstanceOption(handle, key, ptr1); } } - [DllImport("MaaCore.dll")] - private static extern unsafe bool AsstConnect(AsstHandle handle, byte* adbPath, byte* address, byte* config); - private static unsafe bool AsstConnect(AsstHandle handle, string adbPath, string address, string config) { _logger.Information($"handle: {(long)handle}, adbPath: {adbPath}, address: {address}, config: {config}"); @@ -113,44 +93,29 @@ namespace MaaWpfGui.Main ptr2 = EncodeNullTerminatedUtf8(address), ptr3 = EncodeNullTerminatedUtf8(config)) { - bool ret = AsstConnect(handle, ptr1, ptr2, ptr3); + bool ret = MaaService.AsstConnect(handle, ptr1, ptr2, ptr3); _logger.Information($"handle: {((long)handle).ToString()}, adbPath: {adbPath}, address: {address}, config: {config}, return: {ret}"); return ret; } } - [DllImport("MaaCore.dll")] - private static extern unsafe AsstTaskId AsstAppendTask(AsstHandle handle, byte* type, byte* taskParams); - private static unsafe AsstTaskId AsstAppendTask(AsstHandle handle, string type, string taskParams) { fixed (byte* ptr1 = EncodeNullTerminatedUtf8(type), ptr2 = EncodeNullTerminatedUtf8(taskParams)) { - return AsstAppendTask(handle, ptr1, ptr2); + return MaaService.AsstAppendTask(handle, ptr1, ptr2); } } - [DllImport("MaaCore.dll")] - private static extern unsafe bool AsstSetTaskParams(AsstHandle handle, AsstTaskId id, byte* taskParams); - private static unsafe bool AsstSetTaskParams(AsstHandle handle, AsstTaskId id, string taskParams) { fixed (byte* ptr1 = EncodeNullTerminatedUtf8(taskParams)) { - return AsstSetTaskParams(handle, id, ptr1); + return MaaService.AsstSetTaskParams(handle, id, ptr1); } } - [DllImport("MaaCore.dll")] - private static extern bool AsstStart(AsstHandle handle); - - [DllImport("MaaCore.dll")] - private static extern bool AsstRunning(AsstHandle handle); - - [DllImport("MaaCore.dll")] - private static extern bool AsstStop(AsstHandle handle); - // 现在拆分了 core 和 UI 的日志,所以这个函数暂时没用到 /* [DllImport("MaaCore.dll")] @@ -170,22 +135,16 @@ namespace MaaWpfGui.Main } */ - [DllImport("MaaCore.dll")] - private static extern unsafe ulong AsstGetImage(AsstHandle handle, byte* buff, ulong buffSize); - - [DllImport("MaaCore.dll")] - private static extern ulong AsstGetNullSize(); - public static unsafe BitmapImage AsstGetImage(AsstHandle handle) { byte[] buff = new byte[1280 * 720 * 3]; ulong readSize; fixed (byte* ptr = buff) { - readSize = AsstGetImage(handle, ptr, (ulong)buff.Length); + readSize = MaaService.AsstGetImage(handle, ptr, (ulong)buff.Length); } - if (readSize == AsstGetNullSize()) + if (readSize == MaaService.AsstGetNullSize()) { return null; } @@ -210,7 +169,7 @@ namespace MaaWpfGui.Main return AsstGetImage(_handle); } - private readonly CallbackDelegate _callback; + private readonly MaaService.CallbackDelegate _callback; /// /// Initializes a new instance of the class. @@ -286,7 +245,7 @@ namespace MaaWpfGui.Main { bool loaded = LoadResource(); - _handle = AsstCreateEx(_callback, AsstHandle.Zero); + _handle = MaaService.AsstCreateEx(_callback, AsstHandle.Zero); if (loaded == false || _handle == AsstHandle.Zero) { @@ -302,6 +261,7 @@ namespace MaaWpfGui.Main AsstSetInstanceOption(InstanceOptionKey.TouchMode, Instances.SettingsViewModel.TouchMode); AsstSetInstanceOption(InstanceOptionKey.DeploymentWithPause, Instances.SettingsViewModel.DeploymentWithPause ? "1" : "0"); AsstSetInstanceOption(InstanceOptionKey.AdbLiteEnabled, Instances.SettingsViewModel.AdbLiteEnabled ? "1" : "0"); + // TODO: 之后把这个 OnUIThread 拆出来 // ReSharper disable once AsyncVoidLambda Execute.OnUIThread(async () => @@ -367,7 +327,7 @@ namespace MaaWpfGui.Main // Console.WriteLine(json_str); JObject json = (JObject)JsonConvert.DeserializeObject(jsonStr); - ProcCallbackMsg dlg = ProcMsg; + MaaService.ProcCallbackMsg dlg = ProcMsg; Execute.OnUIThread(() => { dlg((AsstMsg)msg, json); @@ -974,6 +934,7 @@ namespace MaaWpfGui.Main break; } + case "CombatRecordRecognitionTask": { string what = details["what"]?.ToString(); @@ -1434,10 +1395,10 @@ namespace MaaWpfGui.Main { string bsHvAddress = Instances.SettingsViewModel.TryToSetBlueStacksHyperVAddress(); - if (String.Equals(Instances.SettingsViewModel.ConnectAddress, bsHvAddress)) + if (string.Equals(Instances.SettingsViewModel.ConnectAddress, bsHvAddress)) { // 防止bsHvAddress和connectAddress重合 - bsHvAddress = String.Empty; + bsHvAddress = string.Empty; } // tcp连接测试端口是否有效,超时时间500ms @@ -2088,7 +2049,7 @@ namespace MaaWpfGui.Main /// 是否成功。 public bool AsstStart() { - return AsstStart(_handle); + return MaaService.AsstStart(_handle); } /// @@ -2097,7 +2058,7 @@ namespace MaaWpfGui.Main /// 是否正在运行。 public bool AsstRunning() { - return AsstRunning(_handle); + return MaaService.AsstRunning(_handle); } /// @@ -2107,7 +2068,7 @@ namespace MaaWpfGui.Main /// 是否成功。 public bool AsstStop(bool clearTask = true) { - bool ret = AsstStop(_handle); + bool ret = MaaService.AsstStop(_handle); if (clearTask) { _latestTaskId.Clear(); @@ -2121,7 +2082,7 @@ namespace MaaWpfGui.Main /// public void AsstDestroy() { - AsstDestroy(_handle); + MaaService.AsstDestroy(_handle); } } diff --git a/src/MaaWpfGui/Services/MaaService.cs b/src/MaaWpfGui/Services/MaaService.cs new file mode 100644 index 0000000000..efb5545542 --- /dev/null +++ b/src/MaaWpfGui/Services/MaaService.cs @@ -0,0 +1,59 @@ +using System; +using System.Runtime.InteropServices; +using MaaWpfGui.Main; +using Newtonsoft.Json.Linq; +using AsstHandle = System.IntPtr; +using AsstInstanceOptionKey = System.Int32; +using AsstTaskId = System.Int32; + +namespace MaaWpfGui.Services +{ + public class MaaService + { + public delegate void CallbackDelegate(int msg, IntPtr jsonBuffer, IntPtr customArg); + + public delegate void ProcCallbackMsg(AsstMsg msg, JObject details); + + [DllImport("MaaCore.dll")] + public static extern AsstHandle AsstCreateEx(CallbackDelegate callback, IntPtr customArg); + + [DllImport("MaaCore.dll")] + public static extern void AsstDestroy(AsstHandle handle); + + [DllImport("MaaCore.dll")] + public static extern unsafe bool AsstSetInstanceOption(AsstHandle handle, AsstInstanceOptionKey key, byte* value); + + [DllImport("MaaCore.dll")] + public static extern unsafe bool AsstLoadResource(byte* dirname); + + [DllImport("MaaCore.dll")] + public static extern void AsstClearAvatarCache(); + + [DllImport("MaaCore.dll")] + public static extern unsafe bool AsstConnect(AsstHandle handle, byte* adbPath, byte* address, byte* config); + + [DllImport("MaaCore.dll")] + public static extern unsafe AsstTaskId AsstAppendTask(AsstHandle handle, byte* type, byte* taskParams); + + [DllImport("MaaCore.dll")] + public static extern unsafe bool AsstSetTaskParams(AsstHandle handle, AsstTaskId id, byte* taskParams); + + [DllImport("MaaCore.dll")] + public static extern bool AsstStart(AsstHandle handle); + + [DllImport("MaaCore.dll")] + public static extern bool AsstRunning(AsstHandle handle); + + [DllImport("MaaCore.dll")] + public static extern bool AsstStop(AsstHandle handle); + + [DllImport("MaaCore.dll")] + public static extern unsafe ulong AsstGetImage(AsstHandle handle, byte* buff, ulong buffSize); + + [DllImport("MaaCore.dll")] + public static extern ulong AsstGetNullSize(); + + [DllImport("MaaCore.dll")] + public static extern IntPtr AsstGetVersion(); + } +} diff --git a/src/MaaWpfGui/Services/StageManager.cs b/src/MaaWpfGui/Services/StageManager.cs index 2b04750c7b..7c101fa65b 100644 --- a/src/MaaWpfGui/Services/StageManager.cs +++ b/src/MaaWpfGui/Services/StageManager.cs @@ -36,9 +36,6 @@ namespace MaaWpfGui.Services /// public class StageManager { - [DllImport("MaaCore.dll")] - private static extern IntPtr AsstGetVersion(); - private const string StageApi = "gui/StageActivity.json"; private const string TasksApi = "resource/tasks.json"; @@ -179,8 +176,8 @@ namespace MaaWpfGui.Services var clientType = GetClientType(); - bool isDebugVersion = Marshal.PtrToStringAnsi(AsstGetVersion())!.Contains("DEBUG"); - bool curVerParsed = SemVersion.TryParse(Marshal.PtrToStringAnsi(AsstGetVersion()), SemVersionStyles.AllowLowerV, out var curVersionObj); + bool isDebugVersion = Marshal.PtrToStringAnsi(MaaService.AsstGetVersion())!.Contains("DEBUG"); + bool curVerParsed = SemVersion.TryParse(Marshal.PtrToStringAnsi(MaaService.AsstGetVersion()), SemVersionStyles.AllowLowerV, out var curVersionObj); // bool curResourceVerParsed = SemVersion.TryParse( // tasksJsonClient?["ResourceVersion"]?.ToString() ?? tasksJson?["ResourceVersion"]?.ToString() ?? string.Empty, diff --git a/src/MaaWpfGui/ViewModels/UI/CopilotViewModel.cs b/src/MaaWpfGui/ViewModels/UI/CopilotViewModel.cs index 304c6d1f76..41068c25c2 100644 --- a/src/MaaWpfGui/ViewModels/UI/CopilotViewModel.cs +++ b/src/MaaWpfGui/ViewModels/UI/CopilotViewModel.cs @@ -24,6 +24,7 @@ using System.Windows.Input; using HandyControl.Tools.Extension; using MaaWpfGui.Constants; using MaaWpfGui.Helper; +using MaaWpfGui.Services; using MaaWpfGui.States; using Microsoft.Win32; using Newtonsoft.Json; diff --git a/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs b/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs index eee4326ef5..48237ae35b 100644 --- a/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs +++ b/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs @@ -35,6 +35,7 @@ using MaaWpfGui.Extensions; using MaaWpfGui.Helper; using MaaWpfGui.Main; using MaaWpfGui.Models; +using MaaWpfGui.Services; using MaaWpfGui.Services.HotKeys; using MaaWpfGui.Services.Notification; using MaaWpfGui.Services.RemoteControl; @@ -61,9 +62,6 @@ namespace MaaWpfGui.ViewModels.UI private static readonly ILogger _logger = Log.ForContext(); - [DllImport("MaaCore.dll")] - private static extern IntPtr AsstGetVersion(); - [DllImport("user32.dll")] private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); @@ -75,7 +73,7 @@ namespace MaaWpfGui.ViewModels.UI /// /// Gets the core version. /// - public static string CoreVersion { get; } = Marshal.PtrToStringAnsi(AsstGetVersion()); + public static string CoreVersion { get; } = Marshal.PtrToStringAnsi(MaaService.AsstGetVersion()); private static readonly string _uiVersion = FileVersionInfo.GetVersionInfo(Application.ResourceAssembly.Location).ProductVersion.Split('+')[0]; @@ -258,7 +256,6 @@ namespace MaaWpfGui.ViewModels.UI } } - private string _remoteControlDeviceIdentity = ConfigurationHelper.GetValue(ConfigurationKeys.RemoteControlDeviceIdentity, string.Empty); public string RemoteControlDeviceIdentity @@ -520,7 +517,6 @@ namespace MaaWpfGui.ViewModels.UI // new CombData { Display = "两者兼顾,投资过后退出", Value = "2" } // 弃用 // new CombData { Display = Localization.GetString("3"), Value = "3" }, // 开发中 - new CombinedData { Display = LocalizationHelper.GetString("RoguelikeLastReward"), Value = "4" }, }; @@ -883,6 +879,7 @@ namespace MaaWpfGui.ViewModels.UI { // ReSharper disable once SuspiciousTypeConversion.Global var link = (IShellLink)new ShellLink(); + // ReSharper disable once SuspiciousTypeConversion.Global var file = (IPersistFile)link; file.Load(EmulatorPath, 0); // STGM_READ @@ -1713,7 +1710,7 @@ namespace MaaWpfGui.ViewModels.UI private string _roguelikeMode = ConfigurationHelper.GetValue(ConfigurationKeys.RoguelikeMode, "0"); /// - /// 策略,往后打 / 刷一层就退 / 烧热水 + /// Gets or sets 策略,往后打 / 刷一层就退 / 烧热水 /// public string RoguelikeMode { @@ -1779,7 +1776,7 @@ namespace MaaWpfGui.ViewModels.UI private ObservableCollection _roguelikeCoreCharList = new ObservableCollection(); /// - /// Gets or sets the roguelike core character. + /// Gets the roguelike core character. /// public ObservableCollection RoguelikeCoreCharList { diff --git a/src/MaaWpfGui/ViewModels/UI/VersionUpdateViewModel.cs b/src/MaaWpfGui/ViewModels/UI/VersionUpdateViewModel.cs index 360fe30fed..24da3e49a9 100644 --- a/src/MaaWpfGui/ViewModels/UI/VersionUpdateViewModel.cs +++ b/src/MaaWpfGui/ViewModels/UI/VersionUpdateViewModel.cs @@ -28,6 +28,7 @@ using MaaWpfGui.Constants; using MaaWpfGui.Helper; using MaaWpfGui.Main; using MaaWpfGui.Models; +using MaaWpfGui.Services; using MaaWpfGui.States; using Newtonsoft.Json; using Newtonsoft.Json.Linq; @@ -52,9 +53,6 @@ namespace MaaWpfGui.ViewModels.UI _runningState = RunningState.Instance; } - [DllImport("MaaCore.dll")] - private static extern IntPtr AsstGetVersion(); - private static readonly ILogger _logger = Log.ForContext(); private static string AddContributorLink(string text) @@ -68,7 +66,7 @@ namespace MaaWpfGui.ViewModels.UI return Regex.Replace(text, @"([^\[`]|^)@([^\s]+)", "$1[@$2](https://github.com/$2)"); } - private readonly string _curVersion = Marshal.PtrToStringAnsi(AsstGetVersion()); + private readonly string _curVersion = Marshal.PtrToStringAnsi(MaaService.AsstGetVersion()); private string _latestVersion; private string _updateTag = ConfigurationHelper.GetValue(ConfigurationKeys.VersionName, string.Empty);