-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugins.go
More file actions
96 lines (81 loc) · 2.01 KB
/
Copy pathplugins.go
File metadata and controls
96 lines (81 loc) · 2.01 KB
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
package main
import (
"sync"
"github.com/unxed/f4/plugins/archive"
"github.com/unxed/f4/plugins/chroma"
"github.com/unxed/f4/plugins/dummy_internal"
"github.com/unxed/f4/plugins/netfox"
"github.com/unxed/f4/vfs"
"github.com/unxed/vtui"
)
// Plugin represents a loaded module.
type Plugin interface {
Init(api vfs.HostAPI) error
Close() error
GetName() string
}
type PluginMenuItem struct {
Label string
Handler func(app vfs.App)
}
var PluginMenuItems []PluginMenuItem
func RegisterPluginMenuItem(label string, handler func(app vfs.App)) {
PluginMenuItems = append(PluginMenuItems, PluginMenuItem{Label: label, Handler: handler})
}
type PluginManager struct {
mu sync.Mutex
api vfs.HostAPI
plugins []Plugin
}
var GlobalPluginManager *PluginManager
func NewPluginManager() *PluginManager {
return &PluginManager{
api: &coreAPI{},
}
}
func (pm *PluginManager) LoadAll() {
vtui.DebugLog("--- Loading Plugins ---")
// 1. Load Internal Plugins
pm.loadInternal()
// 2. Load External Plugins from Config
for _, path := range AppConfig.RegisteredPlugins {
pm.LoadExternalPlugin(path)
}
}
func (pm *PluginManager) LoadExternalPlugin(path string) {
p := NewRPCPlugin(path)
if err := p.Init(pm.api); err == nil {
pm.mu.Lock()
pm.plugins = append(pm.plugins, p)
pm.mu.Unlock()
vtui.DebugLog("Loaded RPC plugin: %s", p.GetName())
} else {
vtui.DebugLog("Failed RPC plugin %s: %v", path, err)
}
}
func (pm *PluginManager) loadInternal() {
plugins := []Plugin{
&chroma.Plugin{},
&dummy_internal.InternalDummyPlugin{},
&archive.ArchivePlugin{},
&netfox.NetFoxPlugin{},
}
for _, p := range plugins {
if err := p.Init(pm.api); err == nil {
pm.mu.Lock()
pm.plugins = append(pm.plugins, p)
pm.mu.Unlock()
vtui.DebugLog("Loaded internal plugin: %s", p.GetName())
} else {
vtui.DebugLog("Failed to init internal plugin %T: %v", p, err)
}
}
}
func (pm *PluginManager) CloseAll() {
pm.mu.Lock()
defer pm.mu.Unlock()
for _, p := range pm.plugins {
p.Close()
}
pm.plugins = nil
}