ProjectWIND/core/app_admin_windows.go

93 lines
1.9 KiB
Go

//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
}