添加了windows的插件入口

This commit is contained in:
Sheyiyuan 2025-01-27 16:45:39 +08:00
parent 4312c7f9f6
commit 5cafc7e5f5
4 changed files with 105 additions and 7 deletions

View File

@ -1,3 +1,6 @@
//go:build linux || darwin
// +build linux darwin
package core package core
import ( import (
@ -35,21 +38,19 @@ func reloadAPP(file os.DirEntry, appsDir string) (totalDelta int, successDelta i
} }
ext := filepath.Ext(file.Name()) ext := filepath.Ext(file.Name())
if ext == ".so" || (ext == ".dll" && os.PathSeparator == '\\') { if ext == ".so" {
pluginPath := filepath.Join(appsDir, file.Name()) pluginPath := filepath.Join(appsDir, file.Name())
p, err := plugin.Open(pluginPath) p, err := plugin.Open(pluginPath)
if err != nil { if err != nil {
LOG.ERROR("打开应用 %s 时发生错误: %v", pluginPath, err) LOG.ERROR("打开应用 %s 时发生错误: %v", pluginPath, err)
return 1, 0 return 1, 0
} }
AppInit, err := p.Lookup("AppInit")
Application, err := p.Lookup("AppInit")
if err != nil { if err != nil {
LOG.ERROR("找不到应用 %s 提供的 Application 接口: %v", pluginPath, err) LOG.ERROR("找不到应用 %s 提供的 AppInit 接口: %v", pluginPath, err)
return 1, 0 return 1, 0
} }
app := AppInit.(func() wba.AppInfo)()
app := Application.(func() wba.AppInfo)()
err = app.Init(&AppApi) err = app.Init(&AppApi)
if err != nil { if err != nil {

92
core/app_admin_windows.go Normal file
View File

@ -0,0 +1,92 @@
//go:build windows
// +build windows
package core
import (
"ProjectWIND/LOG"
"ProjectWIND/wba"
"fmt"
"os"
"path/filepath"
"syscall"
"unsafe"
)
var CmdMap = make(map[string]wba.Cmd)
func ReloadApps() (total int, success int) {
appsDir := "./data/app/"
appFiles, err := os.ReadDir(appsDir)
total = 0
success = 0
if err != nil {
LOG.ERROR("加载应用所在目录失败:%v", err)
return
}
for _, file := range appFiles {
totalDelta, successDelta := reloadAPP(file, appsDir)
total += totalDelta
success += successDelta
}
CmdMap = mergeMaps(CmdMap, AppCore.CmdMap)
return total, success
}
func reloadAPP(file os.DirEntry, appsDir string) (totalDelta int, successDelta int) {
if file.IsDir() {
return 0, 0
}
ext := filepath.Ext(file.Name())
if ext == ".dll" {
pluginPath := filepath.Join(appsDir, file.Name())
lib, err := syscall.LoadLibrary(pluginPath)
if err != nil {
LOG.ERROR("加载应用 %s 失败: %v", pluginPath, err)
return 1, 0
}
defer func(handle syscall.Handle) {
err := syscall.FreeLibrary(handle)
if err != nil {
LOG.ERROR("释放应用 %s 时发生错误: %v", pluginPath, err)
}
}(lib)
// 获取函数地址
sym, err := syscall.GetProcAddress(lib, "AppInit")
if err != nil {
fmt.Println("找不到应用 %s 提供的 AppInit 接口: %v", err)
return 1, 0
}
// 定义函数类型
AppInitPtr := (*func() wba.AppInfo)(unsafe.Pointer(&sym))
AppInit := *AppInitPtr
app := AppInit()
err = app.Init(&AppApi)
if err != nil {
LOG.ERROR("初始化应用 %s 失败: %v", pluginPath, err)
}
CmdMap = mergeMaps(CmdMap, app.Get().CmdMap)
LOG.INFO("应用 %s 加载成功", pluginPath)
return 1, 1
}
return 0, 0
}
func mergeMaps(map1, map2 map[string]wba.Cmd) map[string]wba.Cmd {
// 合并map1和map2到map3中
map3 := make(map[string]wba.Cmd)
for key, value := range map1 {
map3[key] = value
}
for key, value := range map2 {
map3[key] = value
}
return map3
}

5
go.mod
View File

@ -4,4 +4,7 @@ go 1.23.2
require github.com/gorilla/websocket v1.5.3 require github.com/gorilla/websocket v1.5.3
require github.com/robfig/cron/v3 v3.0.1 // indirect require (
github.com/robfig/cron/v3 v3.0.1 // indirect
golang.org/x/sys v0.29.0 // indirect
)

2
go.sum
View File

@ -2,3 +2,5 @@ github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aN
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=