V2bX/core/core.go

55 lines
950 B
Go
Raw Normal View History

package core
import (
"errors"
"strings"
2023-07-29 07:27:15 -04:00
"github.com/InazumaV/V2bX/conf"
)
var (
cores = map[string]func(c *conf.CoreConfig) (Core, error){}
)
2023-08-20 03:13:52 -04:00
func NewCore(c []conf.CoreConfig) (Core, error) {
if len(c) < 0 {
return nil, errors.New("no have vail core")
}
2023-06-09 09:20:41 -04:00
// multi core
2023-08-20 03:13:52 -04:00
if len(c) > 1 {
2023-06-09 09:20:41 -04:00
var cs []Core
2023-08-20 03:13:52 -04:00
for _, t := range c {
f, ok := cores[strings.ToLower(t.Type)]
2023-06-09 09:20:41 -04:00
if !ok {
2023-08-20 03:13:52 -04:00
return nil, errors.New("unknown core type: " + t.Type)
2023-06-09 09:20:41 -04:00
}
2023-08-20 03:13:52 -04:00
core1, err := f(&t)
2023-06-09 09:20:41 -04:00
if err != nil {
return nil, err
}
cs = append(cs, core1)
}
2023-06-18 21:56:18 -04:00
return &Selector{
2023-06-09 09:20:41 -04:00
cores: cs,
}, nil
}
// one core
2023-08-20 03:13:52 -04:00
if f, ok := cores[c[0].Type]; ok {
return f(&c[0])
} else {
return nil, errors.New("unknown core type")
2022-10-09 23:31:15 -04:00
}
}
2022-10-09 23:31:15 -04:00
func RegisterCore(t string, f func(c *conf.CoreConfig) (Core, error)) {
cores[t] = f
2022-10-09 23:31:15 -04:00
}
func RegisteredCore() []string {
cs := make([]string, 0, len(cores))
for k := range cores {
cs = append(cs, k)
}
return cs
}