2022-08-12 06:02:06 -04:00
|
|
|
package core
|
2022-07-28 11:00:05 -04:00
|
|
|
|
|
|
|
import (
|
2023-06-07 13:18:56 -04:00
|
|
|
"errors"
|
2023-06-07 15:11:42 -04:00
|
|
|
"strings"
|
|
|
|
|
2023-07-29 07:27:15 -04:00
|
|
|
"github.com/InazumaV/V2bX/conf"
|
2022-07-28 11:00:05 -04:00
|
|
|
)
|
|
|
|
|
2023-06-07 13:18:56 -04:00
|
|
|
var (
|
|
|
|
cores = map[string]func(c *conf.CoreConfig) (Core, error){}
|
|
|
|
)
|
2022-07-28 11:00:05 -04:00
|
|
|
|
2023-06-07 13:18:56 -04:00
|
|
|
func NewCore(c *conf.CoreConfig) (Core, error) {
|
2023-06-09 09:20:41 -04:00
|
|
|
// multi core
|
|
|
|
if types := strings.Split(c.Type, " "); len(types) > 1 {
|
|
|
|
var cs []Core
|
|
|
|
for _, t := range types {
|
2023-07-23 19:30:22 -04:00
|
|
|
f, ok := cores[strings.ToLower(t)]
|
2023-06-09 09:20:41 -04:00
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("unknown core type: " + t)
|
|
|
|
}
|
|
|
|
core1, err := f(c)
|
|
|
|
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-06-07 15:11:42 -04:00
|
|
|
if f, ok := cores[strings.ToLower(c.Type)]; ok {
|
2023-06-07 13:18:56 -04:00
|
|
|
return f(c)
|
|
|
|
} else {
|
|
|
|
return nil, errors.New("unknown core type")
|
2022-10-09 23:31:15 -04:00
|
|
|
}
|
2022-07-28 11:00:05 -04:00
|
|
|
}
|
2022-10-09 23:31:15 -04:00
|
|
|
|
2023-06-07 13:18:56 -04:00
|
|
|
func RegisterCore(t string, f func(c *conf.CoreConfig) (Core, error)) {
|
|
|
|
cores[t] = f
|
2022-10-09 23:31:15 -04:00
|
|
|
}
|
2023-06-07 15:11:42 -04:00
|
|
|
|
|
|
|
func RegisteredCore() []string {
|
|
|
|
cs := make([]string, 0, len(cores))
|
|
|
|
for k := range cores {
|
|
|
|
cs = append(cs, k)
|
|
|
|
}
|
|
|
|
return cs
|
|
|
|
}
|