Skip to content

Commit

Permalink
support android
Browse files Browse the repository at this point in the history
  • Loading branch information
lz1998 committed Dec 19, 2021
1 parent c671fc2 commit dfe6e5e
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 12 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ captcha.jpg
cuberbot
tmp
dist/
plugins/
plugins/
gmc_android.aar
gmc_android-sources.jar
3 changes: 3 additions & 0 deletions build_android.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

gomobile bind -target=android ./service/gmc_android
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ require (
github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816
github.com/zserge/lorca v0.1.10
golang.org/x/mobile v0.0.0-20211207041440-4e6c2922fdee
golang.org/x/net v0.0.0-20211123203042-d83791d6bcd9 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/protobuf v1.27.1 // indirect
Expand All @@ -43,9 +44,11 @@ require (
github.com/ugorji/go/codec v1.2.6 // indirect
go.uber.org/atomic v1.9.0 // indirect
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
golang.org/x/mod v0.4.2 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/tools v0.1.8-0.20211022200916-316ba0b74098 // indirect
)

//replace github.com/Mrs4s/MiraiGo => ../../Mrs4s/MiraiGo
22 changes: 22 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/Mrs4s/MiraiGo v0.0.0-20211218093950-72ffc08b72ac h1:YYOJTY+L8ttv9CFUkcmZ+3V7n8rdRGrelfSZWdL+7VA=
github.com/Mrs4s/MiraiGo v0.0.0-20211218093950-72ffc08b72ac/go.mod h1:HI05ardsa5HE1Z2xTw69dytmaqqRWv4ajD5Jpnycqqw=
github.com/RomiChan/protobuf v0.0.0-20211204042931-ff4f35848737 h1:p4o7/eSoP39jwnGZz08N1IpH/mNzg9SdCn7kPM9A9BE=
Expand Down Expand Up @@ -99,20 +100,35 @@ github.com/ugorji/go v1.2.6/go.mod h1:anCg0y61KIhDlPZmnH+so+RQbysYVyDko0IMgJv0Nn
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
github.com/ugorji/go/codec v1.2.6 h1:7kbGefxLoDBuYXOms4yD7223OpNMMPNPZxXk5TvFcyQ=
github.com/ugorji/go/codec v1.2.6/go.mod h1:V6TCNZ4PHqoHGFZuSG1W8nrCzzdgA2DozYxWFFpvxTw=
github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/zserge/lorca v0.1.10 h1:f/xBJ3D3ipcVRCcvN8XqZnpoKcOXV8I4vwqlFyw7ruc=
github.com/zserge/lorca v0.1.10/go.mod h1:bVmnIbIRlOcoV285KIRSe4bUABKi7R7384Ycuum6e4A=
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 h1:/UOmuWzQfxxo9UtlXMwuQU8CMgg1eZXqTRwkSQJWKOI=
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56/go.mod h1:JhuoJpWY28nO4Vef9tZUw9qufEGTyX1+7lmHxV5q5G4=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
golang.org/x/mobile v0.0.0-20211207041440-4e6c2922fdee h1:/tShaw8UTf0XzI8DOZwQHzC7d6Vi3EtrBnftiZ4vAvU=
golang.org/x/mobile v0.0.0-20211207041440-4e6c2922fdee/go.mod h1:pe2sM7Uk+2Su1y7u/6Z8KJ24D7lepUjFZbhFOrmDfuQ=
golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY=
golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200222125558-5a598a2470a0/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211123203042-d83791d6bcd9 h1:0qxwC5n+ttVOINCBeRHO0nq9X7uy8SDsPoi5OaCdIEI=
golang.org/x/net v0.0.0-20211123203042-d83791d6bcd9/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand All @@ -133,7 +149,13 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.8-0.20211022200916-316ba0b74098 h1:YuekqPskqwCCPM79F1X5Dhv4ezTCj+Ki1oNwiafxkA0=
golang.org/x/tools v0.1.8-0.20211022200916-316ba0b74098/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
24 changes: 13 additions & 11 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/ProtobufBot/Go-Mirai-Client/pkg/util"
log "github.com/sirupsen/logrus"
"io/ioutil"
"os"
"path"
"strings"

"github.com/ProtobufBot/Go-Mirai-Client/pkg/util"

log "github.com/sirupsen/logrus"
)

//go:generate go run github.com/a8m/syncmap -o "gen_plugin_map.go" -pkg config -name PluginMap "map[string]*Plugin"
Expand Down Expand Up @@ -58,13 +60,13 @@ type Plugin struct {
// TODO event filter, msg filter, regex filter, prefix filter, suffix filter
}

const pluginPath = "plugins"
var PluginPath = "plugins"

func LoadPlugins() {
if !util.PathExists(pluginPath) {
if !util.PathExists(PluginPath) {
return
}
files, err := ioutil.ReadDir(pluginPath)
files, err := ioutil.ReadDir(PluginPath)
if err != nil {
log.Warnf("failed to read plugin dir: %s", err)
return
Expand All @@ -81,7 +83,7 @@ func LoadPlugins() {
continue
}
pluginName := strings.TrimSuffix(file.Name(), ".json")
filepath := path.Join(pluginPath, file.Name())
filepath := path.Join(PluginPath, file.Name())
b, err := os.ReadFile(filepath)
if err != nil {
log.Warnf("failed to read plugin file: %s %s", filepath, err)
Expand All @@ -98,16 +100,16 @@ func LoadPlugins() {
}

func WritePlugins() {
if !util.PathExists(pluginPath) {
if err := os.MkdirAll(pluginPath, 0777); err != nil {
if !util.PathExists(PluginPath) {
if err := os.MkdirAll(PluginPath, 0777); err != nil {
log.Warnf("failed to mkdir")
return
}
}
DeletePluginFiles()
Plugins.Range(func(key string, plugin *Plugin) bool {
pluginFilename := fmt.Sprintf("%s.json", plugin.Name)
filepath := path.Join(pluginPath, pluginFilename)
filepath := path.Join(PluginPath, pluginFilename)
b, err := json.MarshalIndent(plugin, "", " ")
if err != nil {
log.Warnf("failed to marshal plugin, %s", plugin.Name)
Expand All @@ -122,15 +124,15 @@ func WritePlugins() {
}

func DeletePluginFiles() {
files, err := ioutil.ReadDir(pluginPath)
files, err := ioutil.ReadDir(PluginPath)
if err != nil {
log.Warnf("failed to read plugin dir: %s", err)
}
for _, file := range files {
if !strings.HasSuffix(file.Name(), ".json") {
continue
}
filepath := path.Join(pluginPath, file.Name())
filepath := path.Join(PluginPath, file.Name())
if err := os.Remove(filepath); err != nil {
log.Warnf("failed to remove plugin file: %s", filepath)
continue
Expand Down
33 changes: 33 additions & 0 deletions service/gmc_android/gmc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package gmc_android

import (
"github.com/ProtobufBot/Go-Mirai-Client/pkg/config"
"github.com/ProtobufBot/Go-Mirai-Client/pkg/gmc"

log "github.com/sirupsen/logrus"
_ "golang.org/x/mobile/bind"
)

var logger AndroidLogger

// SetPluginPath 设置插件配置路径
func SetPluginPath(pluginPath string) {
config.PluginPath = pluginPath
}

// SetSms 设置是否短信优先
func SetSms(sms bool) {
config.SMS = sms
}

// Start 启动主程序
func Start() {
gmc.Start()
}

// SetLogger 设置日志输出
func SetLogger(androidLogger AndroidLogger) {
logger = androidLogger
log.SetOutput(&AndroidWriter{})
log.SetFormatter(&AndroidFormatter{})
}
39 changes: 39 additions & 0 deletions service/gmc_android/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package gmc_android

import (
"bytes"
"strings"

log "github.com/sirupsen/logrus"
)

type AndroidLogger interface {
Log(str string)
}

type AndroidWriter struct {
}

func (AndroidWriter) Write(p []byte) (n int, err error) {
if logger != nil {
logger.Log(string(p))
}
return 0, nil
}

type AndroidFormatter struct {
}

func (AndroidFormatter) Format(entry *log.Entry) ([]byte, error) {
buf := bytes.Buffer{}
buf.WriteByte('[')
buf.WriteString(entry.Time.Format("2006-01-02 15:04:05"))
buf.WriteString("] [")
buf.WriteString(strings.ToUpper(entry.Level.String()))
buf.WriteString("]: ")
buf.WriteString(entry.Message)
buf.WriteString(" \n")
buf.Bytes()
return append([]byte(nil), buf.Bytes()...), nil
}

0 comments on commit dfe6e5e

Please sign in to comment.