ProjectWIND/core/app_admin_windows.go

93 lines
1.9 KiB
Go
Raw Normal View History

2025-01-27 16:45:39 +08:00
//go:build windows
// +build windows
2024-12-07 17:16:44 +08:00
package core
import (
"ProjectWIND/LOG"
"ProjectWIND/wba"
2025-01-27 16:45:39 +08:00
"fmt"
2024-12-07 17:16:44 +08:00
"os"
"path/filepath"
2025-01-27 16:45:39 +08:00
"syscall"
"unsafe"
2024-12-07 17:16:44 +08:00
)
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 {
2024-12-27 15:56:59 +08:00
LOG.ERROR("加载应用所在目录失败:%v", err)
2024-12-07 17:16:44 +08:00
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())
2025-01-27 16:45:39 +08:00
if ext == ".dll" {
2024-12-07 17:16:44 +08:00
pluginPath := filepath.Join(appsDir, file.Name())
2025-01-27 16:45:39 +08:00
lib, err := syscall.LoadLibrary(pluginPath)
2024-12-07 17:16:44 +08:00
if err != nil {
2025-01-27 16:45:39 +08:00
LOG.ERROR("加载应用 %s 失败: %v", pluginPath, err)
2024-12-07 17:16:44 +08:00
return 1, 0
}
2025-01-27 16:45:39 +08:00
defer func(handle syscall.Handle) {
err := syscall.FreeLibrary(handle)
if err != nil {
LOG.ERROR("释放应用 %s 时发生错误: %v", pluginPath, err)
}
}(lib)
2024-12-07 17:16:44 +08:00
2025-01-27 16:45:39 +08:00
// 获取函数地址
sym, err := syscall.GetProcAddress(lib, "AppInit")
2024-12-07 17:16:44 +08:00
if err != nil {
2025-01-27 16:45:39 +08:00
fmt.Println("找不到应用 %s 提供的 AppInit 接口: %v", err)
2024-12-07 17:16:44 +08:00
return 1, 0
}
2025-01-27 16:45:39 +08:00
// 定义函数类型
AppInitPtr := (*func() wba.AppInfo)(unsafe.Pointer(&sym))
AppInit := *AppInitPtr
app := AppInit()
2024-12-07 17:16:44 +08:00
err = app.Init(&AppApi)
if err != nil {
2024-12-27 15:56:59 +08:00
LOG.ERROR("初始化应用 %s 失败: %v", pluginPath, err)
2024-12-07 17:16:44 +08:00
}
2024-12-13 22:04:56 +08:00
CmdMap = mergeMaps(CmdMap, app.Get().CmdMap)
2024-12-27 15:56:59 +08:00
LOG.INFO("应用 %s 加载成功", pluginPath)
2024-12-07 17:16:44 +08:00
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
}