Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions MaaFramework.Binding.sln
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
.editorconfig = .editorconfig
.gitattributes = .gitattributes
.gitignore = .gitignore
Directory.Build.props = Directory.Build.props
Directory.Packages.props = Directory.Packages.props
src\Directory.Build.props = src\Directory.Build.props
src\Directory.Packages.props = src\Directory.Packages.props
LICENSE.md = LICENSE.md
README.md = README.md
README.zh_cn.md = README.zh_cn.md
Expand Down
13 changes: 13 additions & 0 deletions sample/Agent/.config/dotnet-tools.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": 1,
"isRoot": true,
"tools": {
"dotnet-script": {
"version": "1.6.0",
"commands": [
"dotnet-script"
],
"rollForward": false
}
}
}
58 changes: 58 additions & 0 deletions sample/Agent/AgentChild.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env dotnet-script
#r "nuget: Maa.Framework.Binding.Native, 4.0.0-preview.25163.6"

using MaaFramework.Binding;
using MaaFramework.Binding.Buffers;
using MaaFramework.Binding.Custom;

var commandLineArgs = Environment.GetCommandLineArgs();
if (commandLineArgs.Length < 4)
{
Console.WriteLine("Call AgentMain.cs instead of this file.");
return 1;
}

var socketId = commandLineArgs[^1];
var userPath = commandLineArgs[^2];
var dllPath = commandLineArgs[^3];

NativeBindingInfo.Set(isAgentServer: true, dllPath); // First step
_ = new MaaToolkit(true, userPath);
var agentServer = new MaaAgentServer();
agentServer.Register(new MyRec());
agentServer.Register(new MyAct());
agentServer.StartUp(socketId);
agentServer.Join();
agentServer.ShutDown();

Console.Write("Press any key to exit:");
Console.ReadKey();
return 0;

internal sealed class MyRec : IMaaCustomRecognition
{
public string Name { get; set; } = nameof(MyRec);

public bool Analyze(in IMaaContext context, in AnalyzeArgs args, in AnalyzeResults results)
{
Console.WriteLine("{0} Called", Name);

results.Box.SetValues(0, 0, 100, 100);
results.Detail.SetValue("Hello Client!");
return true;
}
}

internal sealed class MyAct : IMaaCustomAction
{
public string Name { get; set; } = nameof(MyAct);

public bool Run(in IMaaContext context, in RunArgs args)
{
Console.WriteLine("{0} Called", Name);
Console.WriteLine("recognition detail: {0}", args.RecognitionDetail);
Console.WriteLine("custom action param: {0}", args.ActionParam);

return true;
}
}
68 changes: 68 additions & 0 deletions sample/Agent/AgentMain.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env dotnet-script
#r "nuget: Maa.Framework.Native, 4.0.0-preview.25163.6"
#r "nuget: Maa.Framework.Runtime.win-x64, 4.0.0"

using System.Diagnostics;
using MaaFramework.Binding;

var toolkit = new MaaToolkit(true);
var resource = new MaaResource();
var maa = new MaaTasker
{
Controller = toolkit.AdbDevice.Find().First().ToAdbController(),
Resource = resource,
DisposeOptions = DisposeOptions.All,
Toolkit = toolkit,
};

if (!maa.Initialized)
throw new InvalidOperationException("Failed to init tasker.");

var agent = new MaaAgentClient
{
Resource = resource,
DisposeOptions = DisposeOptions.All,
};

var socketId = agent.CreateSocket(string.Empty)
?? throw new InvalidOperationException("Failed to create socket.");

var p = Process.Start(new ProcessStartInfo(
"dotnet",["script",
"AgentChild.cs",
NativeBindingInfo.NativeAssemblyDirectory
?? throw new ArgumentNullException("Native.BindingInfo.NativeAssemblyDirectory"),
Environment.CurrentDirectory,
socketId]) { UseShellExecute = true });

if (!agent.LinkStart())
throw new InvalidOperationException("Failed to connect.");

var ppover = """
{
"Entry": {"next": "Rec"},
"Rec": {
"recognition": "Custom",
"custom_recognition": "MyRec",
"action": "Custom",
"custom_action": "MyAct",
"custom_action_param": {
"param": "Hello Server!"
}
}
}
""";
Console.WriteLine(ppover);

var detail = maa
.AppendTask("Entry", ppover)
.WaitFor(MaaJobStatus.Succeeded)
.QueryTaskDetail()
?? throw new InvalidOperationException("Failed to pipeline.");
Console.WriteLine($"pipeline detail: {detail}");
Console.WriteLine($"MyRec detail: {detail.QueryRecognitionDetail(maa, 1)?.Detail}");

agent.LinkStop();

Console.Write("Press any key to exit:");
Console.ReadKey();
3 changes: 3 additions & 0 deletions sample/Agent/QuickStart.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$ENV:DOTNET_SCRIPT_CACHE_LOCATION="$PSScriptRoot\.cache"
dotnet tool restore
dotnet script AgentMain.cs -s https://api.nuget.org/v3/index.json -s https://maaxyz.github.io/pkg/nuget/index.json
File renamed without changes.
4 changes: 2 additions & 2 deletions Directory.Packages.props → src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Maa.AgentBinary" Version="1.0.0" />
<PackageVersion Include="Maa.Framework.Runtimes" Version="3.0.4" />
<PackageVersion Include="Maa.AgentBinary" Version="1.1.0" />
<PackageVersion Include="Maa.Framework.Runtimes" Version="4.0.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageVersion Include="MSTest.TestAdapter" Version="3.6.1" />
<PackageVersion Include="MSTest.TestFramework" Version="3.6.1" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

#pragma warning disable CS1573 // 参数在 XML 注释中没有匹配的 param 标记
#pragma warning disable CS1591 // 缺少对公共可见类型或成员的 XML 注释

using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;

namespace MaaFramework.Binding.Interop.Native;

public static partial class MaaAgentClient
{
[LibraryImport("MaaAgentClient", StringMarshalling = StringMarshalling.Utf8)]
public static partial MaaAgentClientHandle MaaAgentClientCreate();

[LibraryImport("MaaAgentClient", StringMarshalling = StringMarshalling.Utf8)]
public static partial void MaaAgentClientDestroy(MaaAgentClientHandle client);

[LibraryImport("MaaAgentClient", StringMarshalling = StringMarshalling.Utf8)]
[return: MarshalAs(UnmanagedType.U1)]
public static partial bool MaaAgentClientBindResource(MaaAgentClientHandle client, MaaResourceHandle res);

[LibraryImport("MaaAgentClient", StringMarshalling = StringMarshalling.Utf8)]
[return: MarshalAs(UnmanagedType.U1)]
public static partial bool MaaAgentClientCreateSocket(MaaAgentClientHandle client, MaaStringBufferHandle identifier);

[LibraryImport("MaaAgentClient", StringMarshalling = StringMarshalling.Utf8)]
[return: MarshalAs(UnmanagedType.U1)]
public static partial bool MaaAgentClientConnect(MaaAgentClientHandle client);

[LibraryImport("MaaAgentClient", StringMarshalling = StringMarshalling.Utf8)]
[return: MarshalAs(UnmanagedType.U1)]
public static partial bool MaaAgentClientDisconnect(MaaAgentClientHandle client);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

#pragma warning disable CS1573 // 参数在 XML 注释中没有匹配的 param 标记
#pragma warning disable CS1591 // 缺少对公共可见类型或成员的 XML 注释

global using MaaAgentClientHandle = nint;

namespace MaaFramework.Binding.Interop.Native;

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

#pragma warning disable CS1573 // 参数在 XML 注释中没有匹配的 param 标记
#pragma warning disable CS1591 // 缺少对公共可见类型或成员的 XML 注释

using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;

namespace MaaFramework.Binding.Interop.Native;

public static partial class MaaAgentServer
{
[LibraryImport("MaaAgentServer", StringMarshalling = StringMarshalling.Utf8)]
[return: MarshalAs(UnmanagedType.U1)]
public static partial bool MaaAgentServerRegisterCustomRecognition(string name, MaaCustomRecognitionCallback recognition, nint transArg);

[LibraryImport("MaaAgentServer", StringMarshalling = StringMarshalling.Utf8)]
[return: MarshalAs(UnmanagedType.U1)]
public static partial bool MaaAgentServerRegisterCustomAction(string name, MaaCustomActionCallback action, nint transArg);

[LibraryImport("MaaAgentServer", StringMarshalling = StringMarshalling.Utf8)]
[return: MarshalAs(UnmanagedType.U1)]
public static partial bool MaaAgentServerStartUp(string identifier);

[LibraryImport("MaaAgentServer", StringMarshalling = StringMarshalling.Utf8)]
public static partial void MaaAgentServerShutDown();

[LibraryImport("MaaAgentServer", StringMarshalling = StringMarshalling.Utf8)]
public static partial void MaaAgentServerJoin();

[LibraryImport("MaaAgentServer", StringMarshalling = StringMarshalling.Utf8)]
public static partial void MaaAgentServerDetach();
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ nint transArg
ActionParam: customActionParam,
RecognitionDetail: recognitionDetail,
RecognitionBox: new MaaRectBuffer(boxHandle)
)
),
new RunResults()
);
};
return callback;
Expand Down
Loading
Loading