-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.go
191 lines (159 loc) · 4.11 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
package xplugeth
import (
"path"
"path/filepath"
"strings"
"io/ioutil"
"reflect"
"github.com/go-yaml/yaml"
"github.com/ethereum/go-ethereum/log"
)
var configPath string
type pluginLoader struct {
modules []reflect.Type
hookInterfaces []reflect.Type
hooks map[reflect.Type][]any
moduleValues []reflect.Value
names map[string]reflect.Type
patchsets map[reflect.Type][]Patchset
singletons map[reflect.Type]any
}
func (pl *pluginLoader) registerHook(t reflect.Type, p ...Patchset) {
pl.hookInterfaces = append(pl.hookInterfaces, t)
if len(p) > 0 {
pl.patchsets[t] = p
}
}
func (pl *pluginLoader) registerModule(t reflect.Type, name string) {
pl.modules = append(pl.modules, t)
if pl.names == nil {
n := make(map[string]reflect.Type)
pl.names = n
}
pl.names[name] = t
}
func (pl *pluginLoader) initialize(dirpath string) {
pl.hooks = make(map[reflect.Type][]any)
for _, mt := range pl.modules {
mv := reflect.New(mt)
pl.moduleValues = append(pl.moduleValues, mv)
for _, ht := range pl.hookInterfaces {
if reflect.PointerTo(mt).Implements(ht) {
pl.hooks[ht] = append(pl.hooks[ht], mv.Interface())
}
}
}
configPath = dirpath
}
func (pl *pluginLoader) getModules(t reflect.Type) []any {
return pl.hooks[t]
}
func (pl *pluginLoader) getModulesByMethodName(name string) []any {
results := make([]any, len(pl.moduleValues))
for i, mv := range pl.moduleValues {
if v := mv.MethodByName(name); v.IsValid() && !v.IsZero() {
results[i] = mv.Interface()
}
}
return results
}
func (pl *pluginLoader) storeSingleton(t reflect.Type, v any) error {
if _, ok := pl.singletons[t]; ok {
return ErrSingletonAlreadySet
}
pl.singletons[t] = v
return nil
}
func (pl *pluginLoader) getSingleton(t reflect.Type) (any, bool) {
v, ok := pl.singletons[t]
return v, ok
}
func (pl *pluginLoader) hasModule(name string) bool {
_, ok := pl.names[name]
return ok
}
var pl *pluginLoader
func init() {
pl = &pluginLoader{
modules: []reflect.Type{},
hookInterfaces: []reflect.Type{},
hooks: make(map[reflect.Type][]any),
singletons: make(map[reflect.Type]any),
patchsets: make(map[reflect.Type][]Patchset),
}
}
func RegisterModule[t any](name string) {
pl.registerModule(reflect.TypeFor[t](), name)
}
func RegisterHook[t any](p ...Patchset) {
pl.registerHook(reflect.TypeFor[t](), p...)
}
func Initialize(dirpath string) {
pl.initialize(dirpath)
}
func GetModules[t any]() []t {
mods := pl.getModules(reflect.TypeFor[t]())
res := make([]t, len(mods))
for i, m := range mods {
res[i] = m.(t)
}
return res
}
func GetModulesByMethodName(name string) []any {
return pl.getModulesByMethodName(name)
}
func StoreSingleton[t any](value t) error {
return pl.storeSingleton(reflect.TypeFor[t](), value)
}
func GetSingleton[t any]() (t, bool) {
v, ok := pl.getSingleton(reflect.TypeFor[t]())
if !ok {
var x t
return x, ok
}
return v.(t), ok
}
func HasModule(name string) bool {
return pl.hasModule(name)
}
func GetConfig[T any](name string) (*T, bool) {
files, err := ioutil.ReadDir(configPath)
if err != nil {
log.Warn("Could not load plugins config directory, config values set to default.", "path", configPath)
return nil, false
}
var fpath string
for _, file := range files {
ext := filepath.Ext(file.Name())
nameWithoutExt := strings.TrimSuffix(file.Name(), ext)
if nameWithoutExt == name {
if !strings.HasSuffix(file.Name(), ".yaml") && !strings.HasSuffix(file.Name(), ".yml") {
log.Warn("plugin config file is not .yml or .yaml file. Skipping.", "file", file.Name())
continue
} else {
fpath = path.Join(configPath, file.Name())
}
} else {
log.Warn("plugin config file does not exist")
continue
}
}
c := new(T)
data, err := ioutil.ReadFile(fpath)
if err != nil {
log.Error("error reading plugin config", "err", err)
return nil, false
}
if err := yaml.Unmarshal(data, c); err != nil {
log.Error("error unmarshalling plugin config", "err", err)
return nil, false
}
return c, true
}
func GetPatchsets() [][]Patchset {
res := make([][]Patchset, 0, len(pl.patchsets))
for _, patchsets := range pl.patchsets {
res = append(res, patchsets)
}
return res
}