Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove cgo #3

Merged
merged 36 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
fb1b201
refactor: remove cgo of tasker
dongwlin Nov 14, 2024
f742d3d
refactor: remove cgo of version
dongwlin Nov 14, 2024
b111d2e
chore: format
dongwlin Nov 14, 2024
0a8ec21
refactor: remove redundant symbol
dongwlin Nov 14, 2024
729e83a
refactor: remove cgo of resource
dongwlin Nov 14, 2024
ae5f567
refactor: remove cgo of project interface
dongwlin Nov 14, 2024
b533a50
refactor: remove cgo of controller
dongwlin Nov 14, 2024
18e06cb
refactor: remove unused code
dongwlin Nov 14, 2024
95fc8f3
refactor: remove cgo of context
dongwlin Nov 14, 2024
20b7b5d
refactor: remove cgo of custom controller
dongwlin Nov 14, 2024
b55aa0c
refactor: remove cgo of buffer
dongwlin Nov 15, 2024
217969a
refactor: remove cgo of option
dongwlin Nov 15, 2024
a3a0fbb
chore: tidy mod
dongwlin Nov 15, 2024
a7ebfae
fix: set log dir option error
dongwlin Nov 15, 2024
b0ba617
chore: remove unused file
dongwlin Nov 15, 2024
13c9025
refactor: remove cgo of Adb Device
dongwlin Nov 15, 2024
90a7516
refactor: remove cgo of Tooklkit
dongwlin Nov 15, 2024
ee92e2c
refactor: remove cgo of NotificationCallbackAgent
dongwlin Nov 15, 2024
e4e3f1a
chore: remove unused file
dongwlin Nov 15, 2024
2717bca
ci: disabled cgo
dongwlin Nov 15, 2024
60e10ac
ci: add aarch64 for linux and windows
dongwlin Nov 15, 2024
8f6800c
fix: try fix bad pointer
dongwlin Nov 15, 2024
713a59f
ci: remove go tag
dongwlin Nov 15, 2024
1aa1a54
ci: set GOHOSTARCH for arm64
dongwlin Nov 15, 2024
65ca1e9
ci: set GOARCH for arm64
dongwlin Nov 15, 2024
9817e1b
ci: check system architecture
dongwlin Nov 15, 2024
065d38a
ci: check system architecture
dongwlin Nov 15, 2024
d04d41b
ci: remove arm64 of windows and linux
dongwlin Nov 15, 2024
407bae9
ci: remove arm64 of windows and linux
dongwlin Nov 15, 2024
9f8f76c
refactor: remove unnecessary conversions
dongwlin Nov 15, 2024
4aa9ce2
fix: typo
dongwlin Nov 15, 2024
856a6a3
refactor: remove redundant code
dongwlin Nov 16, 2024
73ca532
refactor: transArg type to uintptr
dongwlin Nov 16, 2024
86a81b1
fix: missing uploads
dongwlin Nov 16, 2024
4719e7c
docs: remove cgo related content
dongwlin Nov 16, 2024
a9da4fe
docs: add some new notes
dongwlin Nov 17, 2024
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
24 changes: 14 additions & 10 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ jobs:
fail-fast: false

steps:
- name: Output system architecture
run: |
echo $env:PROCESSOR_ARCHITECTURE

- name: Checkout
uses: actions/checkout@v4
with:
Expand All @@ -46,14 +50,12 @@ jobs:

- name: Test
env:
CGO_ENABLED: 1
CGO_CFLAGS: -I${{ github.workspace }}/deps/include
CGO_LDFLAGS: -L${{ github.workspace }}/deps/bin -lMaaFramework -lMaaToolkit
CGO_ENABLED: 0
run: |
$env:Path += ";${{ github.workspace }}/deps/bin"
mkdir -p test/data_set/PipelineSmoking/resource/model/ocr
cp -r test/data_set/MaaCommonAssets/OCR/ppocr_v4/zh_cn/* test/data_set/PipelineSmoking/resource/model/ocr
go test -v -tags customenv ./...
go test -v ./...

- uses: actions/upload-artifact@v4
if: always()
Expand All @@ -76,6 +78,10 @@ jobs:
fail-fast: false

steps:
- name: Output system architecture
run: |
uname -m

- name: Update system
run: |
pacman -Syu --noconfirm
Expand Down Expand Up @@ -106,13 +112,12 @@ jobs:

- name: Test
env:
CGO_CFLAGS: -I/__w/maa-framework-go/maa-framework-go/deps/include
CGO_LDFLAGS: -L/__w/maa-framework-go/maa-framework-go/deps/bin -lMaaFramework -lMaaToolkit
CGO_ENABLED: 0
LD_LIBRARY_PATH: /__w/maa-framework-go/maa-framework-go/deps/bin:$LD_LIBRARY_PATH
run: |
mkdir -p test/data_set/PipelineSmoking/resource/model/ocr
cp -r test/data_set/MaaCommonAssets/OCR/ppocr_v4/zh_cn/* test/data_set/PipelineSmoking/resource/model/ocr
go test -v -tags customenv ./...
go test -v ./...

- uses: actions/upload-artifact@v4
if: always()
Expand Down Expand Up @@ -162,13 +167,12 @@ jobs:

- name: Test
env:
CGO_CFLAGS: -I${{ github.workspace }}/deps/include
CGO_LDFLAGS: -L${{ github.workspace }}/deps/bin -lMaaFramework -lMaaToolkit
CGO_ENABLED: 0
DYLD_LIBRARY_PATH: ${{ github.workspace }}/deps/bin:$DYLD_LIBRARY_PATH
run: |
mkdir -p test/data_set/PipelineSmoking/resource/model/ocr
cp -r test/data_set/MaaCommonAssets/OCR/ppocr_v4/zh_cn/* test/data_set/PipelineSmoking/resource/model/ocr
go test -v -tags customenv ./...
go test -v ./...

- uses: actions/upload-artifact@v4
if: always()
Expand Down
58 changes: 12 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ English | [简体中文](README_zh.md)

This is the Go binding for [MaaFramework](https://github.com/MaaXYZ/MaaFramework), providing Go developers with a simple and effective way to use MaaFramework's features within their Go applications.

> No Cgo required!

## Installation

To install the MaaFramework Go binding, run the following command in your terminal:
Expand All @@ -30,6 +32,8 @@ To install the MaaFramework Go binding, run the following command in your termin
go get github.com/MaaXYZ/maa-framework-go
```

In addition, please download the [Release package](https://github.com/MaaXYZ/MaaFramework/releases) for MaaFramework to get the necessary dynamic library files.

## Usage

To use MaaFramework in your Go project, import the package as you would with any other Go package:
Expand All @@ -38,7 +42,14 @@ To use MaaFramework in your Go project, import the package as you would with any
import "github.com/MaaXYZ/maa-framework-go"
```

Then, you can use the functionalities provided by MaaFramework. For detailed usage, refer to the examples and documentation provided in the repository.
Then, you can use the functionalities provided by MaaFramework. For detailed usage, refer to the [documentation](#documentation) and [examples](#examples) provided in the repository.

> Note: Programs built with maa-framework-go rely on the dynamic libraries of MaaFramework. Please ensure one of the following conditions is met:
>
> 1. The program's working directory contains the MaaFramework dynamic libraries.
> 2. Environment variables (such as LD_LIBRARY_PATH or PATH) are set to include the path to the dynamic libraries.
>
> Otherwise, the program may not run correctly.

## Documentation

Expand All @@ -49,51 +60,6 @@ Here are some documents from the maa framework that might help you:
- [QuickStarted](https://github.com/MaaXYZ/MaaFramework/blob/main/docs/en_us/1.1-QuickStarted.md)
- [PipelineProtocol](https://github.com/MaaXYZ/MaaFramework/blob/main/docs/en_us/3.1-PipelineProtocol.md)

## Platform-Specific Notes

### Windows

On Windows, the default location for MaaFramework is `C:\maa`. Ensure that MaaFramework is installed in this directory for the binding to work out of the box.

If you need to specify a custom installation path, refer to the [Custom Environment](#custom-environment) section.

### Linux and macOS

On Linux and macOS, you will need to create a `pkg-config` file named `maa.pc`. This file should correctly point to the locations of the MaaFramework headers and libraries. Place this file in a directory where `pkg-config` can find it (e.g., `/usr/lib/pkgconfig`).

A sample `maa.pc` file might look like this:

```
prefix=/path/to/maafw
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include

Name: MaaFramework
Description: MaaFramework library
Version: 1.0
Libs: -L${libdir} -lMaaFramework -lMaaToolkit
Cflags: -I${includedir}
```

If you need to specify a custom environment, refer to the [Custom Environment](#custom-environment) section.

## Custom Environment

If you need to specify a custom installation path for MaaFramework, you can disable the default location using the `-tags customenv` build tag. Then, set the necessary environment variables `CGO_CFLAGS` and `CGO_LDFLAGS`.

```shell
go build -tags customenv
```

Set the environment variables as follows:

```shell
export CGO_CFLAGS="-I[path to maafw include directory]"
export CGO_LDFLAGS="-L[path to maafw lib directory] -lMaaFramework -lMaaToolkit"
```
Replace `[path to maafw include directory]` with the actual path to the MaaFramework include directory and `[path to maafw lib directory]` with the actual path to the MaaFramework library directory.

## Examples

- [Quirk Start](#quirk-start)
Expand Down
59 changes: 12 additions & 47 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

这是 [MaaFramework](https://github.com/MaaXYZ/MaaFramework) 的Go语言绑定,为Go开发者提供了一种简单而有效的方式,在他们的Go应用程序中使用MaaFramework的功能。

> 无需 Cgo!

## 安装

要安装MaaFramework Go绑定,请在终端中运行以下命令:
Expand All @@ -28,6 +30,8 @@
go get github.com/MaaXYZ/maa-framework-go
```

此外,请下载MaaFramework的[Release 包](https://github.com/MaaXYZ/MaaFramework/releases),以获取必要的动态库文件。

## 使用

要在您的Go项目中使用MaaFramework,请像导入其他Go包一样导入此包:
Expand All @@ -36,7 +40,14 @@ go get github.com/MaaXYZ/maa-framework-go
import "github.com/MaaXYZ/maa-framework-go"
```

然后,您可以使用MaaFramework提供的功能。有关详细用法,请参阅仓库中提供的示例和文档。
然后,您可以使用MaaFramework提供的功能。有关详细用法,请参阅仓库中提供的 [文档](#文档) 和 [示例](#示例)。

> 注意: 使用 maa-framework-go 构建的程序依赖于 MaaFramework 的动态库运行。请确保以下条件之一满足:
>
> 1. 程序的工作目录包含 MaaFramework 的动态库。
> 2. 设置了指向动态库的环境变量(如 LD_LIBRARY_PATH 或 PATH)。
>
> 否则,程序可能无法正确运行。

## 文档

Expand All @@ -47,52 +58,6 @@ import "github.com/MaaXYZ/maa-framework-go"
- [快速开始](https://github.com/MaaXYZ/MaaFramework/blob/main/docs/zh_cn/1.1-%E5%BF%AB%E9%80%9F%E5%BC%80%E5%A7%8B.md)
- [任务流水线协议](https://github.com/MaaXYZ/MaaFramework/blob/main/docs/zh_cn/3.1-%E4%BB%BB%E5%8A%A1%E6%B5%81%E6%B0%B4%E7%BA%BF%E5%8D%8F%E8%AE%AE.md)

## 平台特定说明

### Windows

在Windows上,MaaFramework的默认位置是 `C:\maa`。请确保MaaFramework安装在此目录,以便绑定能够开箱即用。

如果您需要指定自定义安装路径,请参阅 [自定义环境](#自定义环境) 部分。

### Linux 和 macOS

在Linux和macOS上,您需要创建一个名为 `maa.pc` 的 `pkg-config` 文件。此文件应正确指向MaaFramework头文件和库的位置。将此文件放在 `pkg-config` 可以找到的目录中(例如,`/usr/lib/pkgconfig`)。

一个示例 `maa.pc` 文件可能如下所示:

```
prefix=/path/to/maafw
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include

Name: MaaFramework
Description: MaaFramework library
Version: 1.0
Libs: -L${libdir} -lMaaFramework -lMaaToolkit
Cflags: -I${includedir}
```

如果您需要指定自定义环境,请参阅 [自定义环境](#自定义环境) 部分。

## 自定义环境

如果您需要为MaaFramework指定自定义安装路径,可以使用 `-tags customenv` 构建标记禁用默认位置。然后,设置必要的环境变量 `CGO_CFLAGS` 和 `CGO_LDFLAGS`。

```shell
go build -tags customenv
```

设置环境变量如下:

```shell
export CGO_CFLAGS="-I[path to maafw include directory]"
export CGO_LDFLAGS="-L[path to maafw lib directory] -lMaaFramework -lMaaToolkit"
```

将 `[path to maafw include directory]` 替换为MaaFramework包含目录的实际路径,将 `[path to maafw lib directory]` 替换为MaaFramework库目录的实际路径。

## 示例

- [快速开始](#快速开始)
Expand Down
10 changes: 0 additions & 10 deletions cgo.go

This file was deleted.

50 changes: 12 additions & 38 deletions context.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
package maa

/*
#include <stdlib.h>
#include <MaaFramework/MaaAPI.h>
*/
import "C"
import (
"github.com/MaaXYZ/maa-framework-go/internal/buffer"
"image"
"unsafe"

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

type Context struct {
handle *C.MaaContext
handle uintptr
}

func (ctx *Context) handleOverride(override ...any) string {
Expand All @@ -30,12 +26,7 @@ func (ctx *Context) handleOverride(override ...any) string {
}

func (ctx *Context) runPipeline(entry, override string) *TaskDetail {
cEntry := C.CString(entry)
defer C.free(unsafe.Pointer(cEntry))
cOverride := C.CString(override)
defer C.free(unsafe.Pointer(cOverride))

taskId := int64(C.MaaContextRunPipeline(ctx.handle, cEntry, cOverride))
taskId := maa.MaaContextRunPipeline(ctx.handle, entry, override)
tasker := ctx.GetTasker()
return tasker.getTaskDetail(taskId)
}
Expand All @@ -62,15 +53,11 @@ func (ctx *Context) RunPipeline(entry string, override ...any) *TaskDetail {
}

func (ctx *Context) runRecognition(entry, override string, img image.Image) *RecognitionDetail {
cEntry := C.CString(entry)
defer C.free(unsafe.Pointer(cEntry))
cOverride := C.CString(override)
defer C.free(unsafe.Pointer(cOverride))
imgBuf := buffer.NewImageBuffer()
imgBuf.Set(img)
defer imgBuf.Destroy()

recId := int64(C.MaaContextRunRecognition(ctx.handle, cEntry, cOverride, (*C.MaaImageBuffer)(imgBuf.Handle())))
recId := maa.MaaContextRunRecognition(ctx.handle, entry, override, uintptr(imgBuf.Handle()))
tasker := ctx.GetTasker()
return tasker.getRecognitionDetail(recId)
}
Expand All @@ -97,17 +84,11 @@ func (ctx *Context) RunRecognition(entry string, img image.Image, override ...an
}

func (ctx *Context) runAction(entry, override string, box Rect, recognitionDetail string) *NodeDetail {
cEntry := C.CString(entry)
defer C.free(unsafe.Pointer(cEntry))
cOverride := C.CString(override)
defer C.free(unsafe.Pointer(cOverride))
rectBuf := buffer.NewRectBuffer()
rectBuf.Set(box)
defer rectBuf.Destroy()
cRecognitionDetail := C.CString(recognitionDetail)
defer C.free(unsafe.Pointer(cRecognitionDetail))

nodeId := int64(C.MaaContextRunAction(ctx.handle, cEntry, cOverride, (*C.MaaRect)(rectBuf.Handle()), cRecognitionDetail))
nodeId := maa.MaaContextRunAction(ctx.handle, entry, override, uintptr(rectBuf.Handle()), recognitionDetail)
tasker := ctx.GetTasker()
return tasker.getNodeDetail(nodeId)
}
Expand All @@ -134,11 +115,7 @@ func (ctx *Context) RunAction(entry string, box Rect, recognitionDetail string,
}

func (ctx *Context) overridePipeline(override string) bool {
cPipelineOverride := C.CString(override)
defer C.free(unsafe.Pointer(cPipelineOverride))

got := C.MaaContextOverridePipeline(ctx.handle, cPipelineOverride)
return got != 0
return maa.MaaContextOverridePipeline(ctx.handle, override)
}

// OverridePipeline overrides pipeline.
Expand All @@ -156,8 +133,6 @@ func (ctx *Context) OverridePipeline(override any) bool {

// OverrideNext overrides the next list of task by name.
func (ctx *Context) OverrideNext(name string, nextList []string) bool {
cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))
list := buffer.NewStringListBuffer()
defer list.Destroy()
size := len(nextList)
Expand All @@ -172,25 +147,24 @@ func (ctx *Context) OverrideNext(name string, nextList []string) bool {
item.Destroy()
}
}()
got := C.MaaContextOverrideNext(ctx.handle, cName, (*C.MaaStringListBuffer)(list.Handle()))
return got != 0
return maa.MaaContextOverrideNext(ctx.handle, name, uintptr(list.Handle()))
}

// GetTaskJob returns current task job.
func (ctx *Context) GetTaskJob() *TaskJob {
tasker := ctx.GetTasker()
taskId := int64(C.MaaContextGetTaskId(ctx.handle))
taskId := maa.MaaContextGetTaskId(ctx.handle)
return NewTaskJob(taskId, tasker.status, tasker.wait, tasker.getTaskDetail)
}

// GetTasker return current Tasker.
func (ctx *Context) GetTasker() *Tasker {
handle := C.MaaContextGetTasker(ctx.handle)
handle := maa.MaaContextGetTasker(ctx.handle)
return &Tasker{handle: handle}
}

// Clone clones current Context.
func (ctx *Context) Clone() *Context {
handle := C.MaaContextClone(ctx.handle)
handle := maa.MaaContextClone(ctx.handle)
return &Context{handle: handle}
}
Loading
Loading