This repository has been archived by the owner on Dec 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.go
212 lines (183 loc) · 5.57 KB
/
loader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package so
import (
"errors"
"os"
"plugin"
"unsafe"
_const "github.com/238Studio/child-nodes-assist/const"
"github.com/238Studio/child-nodes-assist/util"
loader "github.com/238Studio/child-nodes-bin-loader"
jsoniter "github.com/json-iterator/go"
)
// GetName 获取name
// 传入:无
// 传出:全局唯一的包名
func (so *SoPackage) GetName() string {
return so.name
}
// GetID 获取id
// 传入:无
// 传出:id
func (so *SoPackage) GetID() int {
return so.id
}
// GetFunctions 获取函数列表
// 传入:无
// 传出:支持的函数列表
func (so *SoPackage) GetFunctions() []string {
return so.functions
}
// GetFunctionsArgsTypes 获取函数入参类型
// 传入:函数名
// 传出:函数入参类型,错误
func (so *SoPackage) GetFunctionsArgsTypes(methodName string) ([]string, error) {
functionArgs, isEXIST := so.functionsArgTypes[methodName]
if !isEXIST {
return nil, util.NewError(_const.CommonException, _const.Bin, errors.New("function not exist"))
}
return functionArgs, nil
}
// GetFunctionReturnTypes 获取函数返回值类型
// 传入:函数名
// 传出:函数返回值类型,错误
func (so *SoPackage) GetFunctionReturnTypes(methodName string) ([]string, error) {
functionReturn, isEXIST := so.functionsReturnTypes[methodName]
if !isEXIST {
return nil, util.NewError(_const.CommonException, _const.Bin, errors.New("function not exist"))
}
return functionReturn, nil
}
// GetInfo 获取其他信息
// 传入:key
// 传出:value,错误
func (so *SoPackage) GetInfo(key string) (string, error) {
value, isEXIST := so.info[key]
if !isEXIST {
return "", util.NewError(_const.CommonException, _const.Bin, errors.New("info not exist"))
}
return value, nil
}
// Execute 执行函数
// 传入:函数名,参数
// 传出:返回值(通过指针),错误
func (so *SoPackage) Execute(method string, args []uintptr, re uintptr) (err error) {
//捕获panic
defer func() {
if er := recover(); er != nil {
//特例panic,级别非fatal,牵涉到cgo
err = util.NewError(_const.CommonException, _const.Bin, errors.New(er.(string)))
}
}()
//加载函数
ptr, err := so.so.Lookup(method)
if err != nil {
return util.NewError(_const.CommonException, _const.Bin, err)
}
//如果没有参数(args==nil),则调用无参函数
if args == nil {
ptr.(func())()
} else {
//如果有参数,则调用有参函数
ptr.(func(uintptr, uintptr))(uintptr(unsafe.Pointer(&args)), re)
}
return nil
}
// LoadBinPackage 根据路径加在二进制包
// 传入:路径
// 传出:包对象,错误
func (soLoader *SoLoader) LoadBinPackage(path string) (name string, id int, err error) {
//捕获恐慌
defer func() {
if er := recover(); er != nil {
//特例panic,级别非fatal,牵涉到cgo
err = util.NewError(_const.CommonException, _const.Bin, errors.New(er.(string)))
}
}()
soInfoPath := path + ".json" //so包对应的描述文件地址
soPackagePath := path + ".so" //so包地址
//加载so包
so, err := plugin.Open(soPackagePath)
if err != nil {
return "", 0, util.NewError(_const.CommonException, _const.Bin, err)
}
//加载so包对应的描述文件
content, err := os.ReadFile(soInfoPath)
if err != nil {
return "", 0, util.NewError(_const.CommonException, _const.Bin, err)
}
//解析描述文件
var (
payload loader.BinInfo
json = jsoniter.ConfigCompatibleWithStandardLibrary
)
err = json.Unmarshal(content, &payload)
if err != nil {
return "", 0, util.NewError(_const.CommonException, _const.Bin, err)
}
//创建包对象
soPackage := &SoPackage{
name: payload.Name,
id: 0,
functions: payload.Functions,
functionsArgTypes: payload.FunctionsArgsTypes,
functionsReturnTypes: payload.FunctionsReturnTypes,
info: payload.Info,
so: so,
}
//id分配
if num, ok := soLoader.soCounter[soPackage.name]; !ok {
soLoader.soCounter[soPackage.name] = 0 //初始化
} else {
soPackage.id = num
soLoader.soCounter[soPackage.name]++
}
//加入集合
if _, ok := soLoader.Sos[soPackage.name]; !ok {
soLoader.Sos[soPackage.name] = make(map[int]*SoPackage)
}
soLoader.Sos[soPackage.name][soPackage.id] = soPackage
return soPackage.name, soPackage.id, nil
}
// ReleasePackage 释放so包
// 传入:二进制执行包
// 传出:错误
func (soLoader *SoLoader) ReleasePackage(name string, id int) (err error) {
//捕获恐慌
defer func() {
if er := recover(); er != nil {
//特例panic,级别非fatal,牵涉到cgo
err = util.NewError(_const.CommonException, _const.Bin, errors.New(er.(string)))
}
}()
//通过name和id获取so package
soPackage, isEXIST := soLoader.Sos[name][id]
if !isEXIST {
return util.NewError(_const.CommonException, _const.Bin, errors.New("package not exist"))
}
//释放so package
err = soPackage.Execute("Release", nil, 0)
if err != nil {
return util.NewError(_const.CommonException, _const.Bin, err)
}
//从集合中移除
delete(soLoader.Sos[name], id)
return nil
}
// GetBinPackage 获取so包
// 传入:包名,id
// 传出:包对象,错误
func (soLoader *SoLoader) GetBinPackage(name string, id int) (soPackage *SoPackage, err error) {
//捕获恐慌
defer func() {
if er := recover(); er != nil {
//特例panic,级别非fatal,牵涉到cgo
err = util.NewError(_const.CommonException, _const.Bin, errors.New(er.(string)))
}
}()
//通过name和id获取so package
soPackage, isEXIST := soLoader.Sos[name][id]
if !isEXIST {
return nil, util.NewError(_const.CommonException, _const.Bin, errors.New("package not exist"))
}
return soPackage, nil
}