refactor: 迁移使用了MaaCore的方法,统一合并至MaaService

This commit is contained in:
status102
2023-10-15 10:21:28 +08:00
parent af8a4f458b
commit 8cf76efb11
7 changed files with 91 additions and 80 deletions

View File

@@ -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);

View File

@@ -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;
/// <summary>
/// MaaCore 代理类。
/// </summary>
@@ -51,10 +50,6 @@ namespace MaaWpfGui.Main
private readonly RunningState _runningState;
private static readonly ILogger _logger = Log.ForContext<AsstProxy>();
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;
/// <summary>
/// Initializes a new instance of the <see cref="AsstProxy"/> 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
/// <returns>是否成功。</returns>
public bool AsstStart()
{
return AsstStart(_handle);
return MaaService.AsstStart(_handle);
}
/// <summary>
@@ -2097,7 +2058,7 @@ namespace MaaWpfGui.Main
/// <returns>是否正在运行。</returns>
public bool AsstRunning()
{
return AsstRunning(_handle);
return MaaService.AsstRunning(_handle);
}
/// <summary>
@@ -2107,7 +2068,7 @@ namespace MaaWpfGui.Main
/// <returns>是否成功。</returns>
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
/// </summary>
public void AsstDestroy()
{
AsstDestroy(_handle);
MaaService.AsstDestroy(_handle);
}
}

View File

@@ -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();
}
}

View File

@@ -36,9 +36,6 @@ namespace MaaWpfGui.Services
/// </summary>
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,

View File

@@ -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;

View File

@@ -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<SettingsViewModel>();
[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
/// <summary>
/// Gets the core version.
/// </summary>
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");
/// <summary>
/// 策略,往后打 / 刷一层就退 / 烧热水
/// Gets or sets 策略,往后打 / 刷一层就退 / 烧热水
/// </summary>
public string RoguelikeMode
{
@@ -1779,7 +1776,7 @@ namespace MaaWpfGui.ViewModels.UI
private ObservableCollection<string> _roguelikeCoreCharList = new ObservableCollection<string>();
/// <summary>
/// Gets or sets the roguelike core character.
/// Gets the roguelike core character.
/// </summary>
public ObservableCollection<string> RoguelikeCoreCharList
{

View File

@@ -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<VersionUpdateViewModel>();
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);