Skip to content

Commit

Permalink
refactor: rename APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
dongwlin committed Jan 13, 2025
1 parent e413b08 commit 6b6af7e
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 77 deletions.
14 changes: 7 additions & 7 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,34 @@ func (ctx *Context) handleOverride(override ...any) string {
return str
}

func (ctx *Context) runPipeline(entry, override string) *TaskDetail {
taskId := maa.MaaContextRunPipeline(ctx.handle, entry, override)
func (ctx *Context) runTask(entry, override string) *TaskDetail {
taskId := maa.MaaContextRunTask(ctx.handle, entry, override)
if taskId == 0 {
return nil
}
tasker := ctx.GetTasker()
return tasker.getTaskDetail(taskId)
}

// RunPipeline runs a pipeline and returns its detail.
// RunTask runs a task and returns its detail.
// It accepts an entry string and an optional override parameter which can be
// a JSON string or any data type that can be marshaled to JSON.
// If multiple overrides are provided, only the first one will be used.
//
// Example 1:
//
// ctx.RunPipeline("Task", `{"Task":{"action":"Click","target":[100, 200, 100, 100]}}`)
// ctx.RunTask("Task", `{"Task":{"action":"Click","target":[100, 200, 100, 100]}}`)
//
// Example 2:
//
// ctx.RunPipeline("Task", map[string]interface{}{
// ctx.RunTask("Task", map[string]interface{}{
// "Task": map[string]interface{}{
// "action": "Click",
// "target": []int{100, 200, 100, 100},
// }
// })
func (ctx *Context) RunPipeline(entry string, override ...any) *TaskDetail {
return ctx.runPipeline(entry, ctx.handleOverride(override...))
func (ctx *Context) RunTask(entry string, override ...any) *TaskDetail {
return ctx.runTask(entry, ctx.handleOverride(override...))
}

func (ctx *Context) runRecognition(entry, override string, img image.Image) *RecognitionDetail {
Expand Down
32 changes: 16 additions & 16 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"github.com/stretchr/testify/require"
)

type testContextRunPipelineAct struct {
type testContextRunTaskAct struct {
t *testing.T
}

func (t *testContextRunPipelineAct) Run(ctx *Context, _ *CustomActionArg) bool {
detail := ctx.RunPipeline("Test", J{
func (t *testContextRunTaskAct) Run(ctx *Context, _ *CustomActionArg) bool {
detail := ctx.RunTask("Test", J{
"Test": J{
"action": "Click",
"target": []int{100, 100, 10, 10},
Expand All @@ -21,7 +21,7 @@ func (t *testContextRunPipelineAct) Run(ctx *Context, _ *CustomActionArg) bool {
return true
}

func TestContext_RunPipeline(t *testing.T) {
func TestContext_RunTask(t *testing.T) {
ctrl := createDbgController(t, nil)
defer ctrl.Destroy()
isConnected := ctrl.PostConnect().Wait().Success()
Expand All @@ -34,10 +34,10 @@ func TestContext_RunPipeline(t *testing.T) {
defer tasker.Destroy()
taskerBind(t, tasker, ctrl, res)

ok := res.RegisterCustomAction("TestContext_RunPipelineAct", &testContextRunPipelineAct{t})
ok := res.RegisterCustomAction("TestContext_RunPipelineAct", &testContextRunTaskAct{t})
require.True(t, ok)

got := tasker.PostPipeline("TestContext_RunPipeline", J{
got := tasker.PostTask("TestContext_RunPipeline", J{
"TestContext_RunPipeline": J{
"action": "Custom",
"custom_action": "TestContext_RunPipelineAct",
Expand Down Expand Up @@ -78,7 +78,7 @@ func TestContext_RunRecognition(t *testing.T) {
ok := res.RegisterCustomAction("TestContext_RunRecognitionAct", &testContextRunRecognitionAct{t})
require.True(t, ok)

got := tasker.PostPipeline("TestContext_RunRecognition", J{
got := tasker.PostTask("TestContext_RunRecognition", J{
"TestContext_RunRecognition": J{
"next": []string{
"RunRecognition",
Expand Down Expand Up @@ -125,7 +125,7 @@ func TestContext_RunAction(t *testing.T) {
ok := res.RegisterCustomAction("TestContext_RunActionAct", &testContextRunActionAct{t})
require.True(t, ok)

got := tasker.PostPipeline("TestContext_RunAction", J{
got := tasker.PostTask("TestContext_RunAction", J{
"TestContext_RunAction": J{
"action": "Custom",
"custom_action": "TestContext_RunActionAct",
Expand All @@ -139,7 +139,7 @@ type testContextOverriderPipelineAct struct {
}

func (t *testContextOverriderPipelineAct) Run(ctx *Context, _ *CustomActionArg) bool {
detail1 := ctx.RunPipeline("Test", J{
detail1 := ctx.RunTask("Test", J{
"Test": J{
"action": "Click",
"target": []int{100, 100, 10, 10},
Expand All @@ -154,7 +154,7 @@ func (t *testContextOverriderPipelineAct) Run(ctx *Context, _ *CustomActionArg)
})
require.True(t.t, ok)

detail2 := ctx.RunPipeline("Test")
detail2 := ctx.RunTask("Test")
require.NotNil(t.t, detail2)
return true
}
Expand All @@ -175,7 +175,7 @@ func TestContext_OverridePipeline(t *testing.T) {
ok := res.RegisterCustomAction("TestContext_OverridePipelineAct", &testContextOverriderPipelineAct{t})
require.True(t, ok)

got := tasker.PostPipeline("TestContext_OverridePipeline", J{
got := tasker.PostTask("TestContext_OverridePipeline", J{
"TestContext_OverridePipeline": J{
"action": "Custom",
"custom_action": "TestContext_OverridePipelineAct",
Expand All @@ -201,7 +201,7 @@ func (t *testContextOverrideNextAct) Run(ctx *Context, _ *CustomActionArg) bool
ok2 := ctx.OverrideNext("Test", []string{"TaskB"})
require.True(t.t, ok2)

detail := ctx.RunPipeline("Test")
detail := ctx.RunTask("Test")
require.NotNil(t.t, detail)
return true
}
Expand All @@ -222,7 +222,7 @@ func TestContext_OverrideNext(t *testing.T) {
ok := res.RegisterCustomAction("TestContext_OverrideNextAct", &testContextOverrideNextAct{t})
require.True(t, ok)

got := tasker.PostPipeline("TestContext_OverrideNext", J{
got := tasker.PostTask("TestContext_OverrideNext", J{
"TestContext_OverrideNext": J{
"action": "Custom",
"custom_action": "TestContext_OverrideNextAct",
Expand Down Expand Up @@ -257,7 +257,7 @@ func TestContext_GetTaskJob(t *testing.T) {
ok := res.RegisterCustomAction("TestContext_GetTaskJobAct", &testContextGetTaskJobAct{t})
require.True(t, ok)

got := tasker.PostPipeline("TestContext_GetTaskJob", J{
got := tasker.PostTask("TestContext_GetTaskJob", J{
"TestContext_GetTaskJob": J{
"action": "Custom",
"custom_action": "TestContext_GetTaskJobAct",
Expand Down Expand Up @@ -292,7 +292,7 @@ func TestContext_GetTasker(t *testing.T) {
ok := res.RegisterCustomAction("TestContext_GetTaskerAct", &testContextGetTaskerAct{t})
require.True(t, ok)

got := tasker.PostPipeline("TestContext_GetTasker", J{
got := tasker.PostTask("TestContext_GetTasker", J{
"TestContext_GetTasker": J{
"action": "Custom",
"custom_action": "TestContext_GetTaskerAct",
Expand Down Expand Up @@ -327,7 +327,7 @@ func TestContext_Clone(t *testing.T) {
ok := res.RegisterCustomAction("TestContext_GetTaskerAct", &testContextCloneAct{t})
require.True(t, ok)

got := tasker.PostPipeline("TestContext_GetTasker", J{
got := tasker.PostTask("TestContext_GetTasker", J{
"TestContext_GetTasker": J{
"action": "Custom",
"custom_action": "TestContext_GetTaskerAct",
Expand Down
7 changes: 4 additions & 3 deletions examples/custom-action/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package main

import (
"fmt"
"github.com/MaaXYZ/maa-framework-go"
"os"

"github.com/MaaXYZ/maa-framework-go"
)

func main() {
Expand All @@ -28,7 +29,7 @@ func main() {

res := maa.NewResource(nil)
defer res.Destroy()
res.PostPath("./resource").Wait()
res.PostBundle("./resource").Wait()
tasker.BindResource(res)
if tasker.Initialized() {
fmt.Println("Failed to init MAA.")
Expand All @@ -37,7 +38,7 @@ func main() {

res.RegisterCustomAction("MyAct", &MyAct{})

detail := tasker.PostPipeline("Startup").Wait().GetDetail()
detail := tasker.PostTask("Startup").Wait().GetDetail()
fmt.Println(detail)
}

Expand Down
6 changes: 3 additions & 3 deletions examples/custom-recognition/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func main() {

res := maa.NewResource(nil)
defer res.Destroy()
res.PostPath("./resource").Wait()
res.PostBundle("./resource").Wait()
tasker.BindResource(res)
if tasker.Initialized() {
fmt.Println("Failed to init MAA.")
Expand All @@ -38,7 +38,7 @@ func main() {

res.RegisterCustomRecognition("MyRec", &MyRec{})

detail := tasker.PostPipeline("Startup").Wait().GetDetail()
detail := tasker.PostTask("Startup").Wait().GetDetail()
fmt.Println(detail)
}

Expand All @@ -63,7 +63,7 @@ func (r *MyRec) Run(ctx *maa.Context, arg *maa.CustomRecognitionArg) (*maa.Custo
"roi": []int{100, 200, 300, 400},
},
})
newContext.RunPipeline("MyCustomOCR", arg.Img)
newContext.RunTask("MyCustomOCR", arg.Img)

clickJob := ctx.GetTasker().GetController().PostClick(10, 20)
clickJob.Wait()
Expand Down
7 changes: 4 additions & 3 deletions examples/quick-start/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package main

import (
"fmt"
"github.com/MaaXYZ/maa-framework-go"
"os"

"github.com/MaaXYZ/maa-framework-go"
)

func main() {
Expand All @@ -28,13 +29,13 @@ func main() {

res := maa.NewResource(nil)
defer res.Destroy()
res.PostPath("./resource").Wait()
res.PostBundle("./resource").Wait()
tasker.BindResource(res)
if tasker.Initialized() {
fmt.Println("Failed to init MAA.")
os.Exit(1)
}

detail := tasker.PostPipeline("Startup").Wait().GetDetail()
detail := tasker.PostTask("Startup").Wait().GetDetail()
fmt.Println(detail)
}
18 changes: 9 additions & 9 deletions internal/maa/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ var (
MaaTaskerBindResource func(tasker uintptr, res uintptr) bool
MaaTaskerBindController func(tasker uintptr, ctrl uintptr) bool
MaaTaskerInited func(tasker uintptr) bool
MaaTaskerPostPipeline func(tasker uintptr, entry, pipelineOverride string) int64
MaaTaskerPostTask func(tasker uintptr, entry, pipelineOverride string) int64
MaaTaskerStatus func(tasker uintptr, id int64) int32
MaaTaskerWait func(tasker uintptr, id int64) int32
MaaTaskerRunning func(tasker uintptr) bool
MaaTaskerPostStop func(tasker uintptr) int64
MaaTaskerGetResource func(tasker uintptr) uintptr
MaaTaskerGetController func(tasker uintptr) uintptr
MaaTaskerClearCache func(tasker uintptr) bool
MaaTaskerGetRecognitionDetail func(tasker uintptr, recoId int64, name uintptr, algorithm uintptr, hit *bool, box uintptr, detailJson uintptr, raw uintptr, draws uintptr) bool
MaaTaskerGetNodeDetail func(tasker uintptr, nodeId int64, name uintptr, recoId *int64, completed *bool) bool
MaaTaskerGetRecognitionDetail func(tasker uintptr, recoId int64, nodeName uintptr, algorithm uintptr, hit *bool, box uintptr, detailJson uintptr, raw uintptr, draws uintptr) bool
MaaTaskerGetNodeDetail func(tasker uintptr, nodeId int64, nodeName uintptr, recoId *int64, completed *bool) bool
MaaTaskerGetTaskDetail func(tasker uintptr, taskId int64, entry uintptr, nodeIdList uintptr, nodeIdListSize *uint64, status *int32) bool
MaaTaskerGetLatestNode func(tasker uintptr, taskName string, latestId *int64) bool
)
Expand Down Expand Up @@ -104,7 +104,7 @@ var (
MaaResourceRegisterCustomAction func(res uintptr, name string, action MaaCustomActionCallback, transArg uintptr) bool
MaaResourceUnregisterCustomAction func(res uintptr, name string) bool
MaaResourceClearCustomAction func(res uintptr) bool
MaaResourcePostPath func(res uintptr, path string) int64
MaaResourcePostBundle func(res uintptr, path string) int64
MaaResourceClear func(res uintptr) bool
MaaResourceStatus func(res uintptr, id int64) int32
MaaResourceWait func(res uintptr, id int64) int32
Expand Down Expand Up @@ -299,11 +299,11 @@ func MaaCustomControllerCallbacksCreate(
}

var (
MaaContextRunPipeline func(context uintptr, entry, pipelineOverride string) int64
MaaContextRunTask func(context uintptr, entry, pipelineOverride string) int64
MaaContextRunRecognition func(context uintptr, entry, pipelineOverride string, image uintptr) int64
MaaContextRunAction func(context uintptr, entry, pipelineOverride string, box uintptr, recoDetail string) int64
MaaContextOverridePipeline func(context uintptr, pipelineOverride string) bool
MaaContextOverrideNext func(context uintptr, name string, nextList uintptr) bool
MaaContextOverrideNext func(context uintptr, nodeName string, nextList uintptr) bool
MaaContextGetTaskId func(context uintptr) int64
MaaContextGetTasker func(context uintptr) uintptr
MaaContextClone func(context uintptr) uintptr
Expand Down Expand Up @@ -411,7 +411,7 @@ func init() {
purego.RegisterLibFunc(&MaaTaskerBindResource, maaFramework, "MaaTaskerBindResource")
purego.RegisterLibFunc(&MaaTaskerBindController, maaFramework, "MaaTaskerBindController")
purego.RegisterLibFunc(&MaaTaskerInited, maaFramework, "MaaTaskerInited")
purego.RegisterLibFunc(&MaaTaskerPostPipeline, maaFramework, "MaaTaskerPostPipeline")
purego.RegisterLibFunc(&MaaTaskerPostTask, maaFramework, "MaaTaskerPostPipeline")
purego.RegisterLibFunc(&MaaTaskerStatus, maaFramework, "MaaTaskerStatus")
purego.RegisterLibFunc(&MaaTaskerWait, maaFramework, "MaaTaskerWait")
purego.RegisterLibFunc(&MaaTaskerRunning, maaFramework, "MaaTaskerRunning")
Expand All @@ -432,7 +432,7 @@ func init() {
purego.RegisterLibFunc(&MaaResourceRegisterCustomAction, maaFramework, "MaaResourceRegisterCustomAction")
purego.RegisterLibFunc(&MaaResourceUnregisterCustomAction, maaFramework, "MaaResourceUnregisterCustomAction")
purego.RegisterLibFunc(&MaaResourceClearCustomAction, maaFramework, "MaaResourceClearCustomAction")
purego.RegisterLibFunc(&MaaResourcePostPath, maaFramework, "MaaResourcePostPath")
purego.RegisterLibFunc(&MaaResourcePostBundle, maaFramework, "MaaResourcePostPath")
purego.RegisterLibFunc(&MaaResourceClear, maaFramework, "MaaResourceClear")
purego.RegisterLibFunc(&MaaResourceStatus, maaFramework, "MaaResourceStatus")
purego.RegisterLibFunc(&MaaResourceWait, maaFramework, "MaaResourceWait")
Expand Down Expand Up @@ -464,7 +464,7 @@ func init() {
purego.RegisterLibFunc(&MaaControllerCachedImage, maaFramework, "MaaControllerCachedImage")
purego.RegisterLibFunc(&MaaControllerGetUuid, maaFramework, "MaaControllerGetUuid")
// Context
purego.RegisterLibFunc(&MaaContextRunPipeline, maaFramework, "MaaContextRunPipeline")
purego.RegisterLibFunc(&MaaContextRunTask, maaFramework, "MaaContextRunPipeline")
purego.RegisterLibFunc(&MaaContextRunRecognition, maaFramework, "MaaContextRunRecognition")
purego.RegisterLibFunc(&MaaContextRunAction, maaFramework, "MaaContextRunAction")
purego.RegisterLibFunc(&MaaContextOverridePipeline, maaFramework, "MaaContextOverridePipeline")
Expand Down
2 changes: 1 addition & 1 deletion notification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestNotificationHandler_OnRawNotification(t *testing.T) {
defer tasker.Destroy()
taskerBind(t, tasker, ctrl, res)

got := tasker.PostPipeline("TestNotificationHandler_OnRawNotification", J{
got := tasker.PostTask("TestNotificationHandler_OnRawNotification", J{
"TestNotificationHandler_OnRawNotification": J{
"action": "Click",
"target": []int{100, 200, 100, 100},
Expand Down
6 changes: 3 additions & 3 deletions resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,10 @@ func (r *Resource) ClearCustomAction() bool {
return maa.MaaResourceClearCustomAction(r.handle)
}

// PostPath adds a path to the resource loading paths.
// PostBundle adds a path to the resource loading paths.
// Return id of the resource.
func (r *Resource) PostPath(path string) *Job {
id := maa.MaaResourcePostPath(r.handle, path)
func (r *Resource) PostBundle(path string) *Job {
id := maa.MaaResourcePostBundle(r.handle, path)
return NewJob(id, r.status, r.wait)
}

Expand Down
Loading

0 comments on commit 6b6af7e

Please sign in to comment.