Files
Reims-Go/internal/plugin/builtin/bootstrap.go
T
千川白浪 107a8d73e0 refactor: 收拢核心管线、强化错误处理、优化热路径与更新体积 (#200)
* refactor: 收拢核心管线、强化错误处理、优化热路径与更新体积

* fix: preserve download recovery and bound PBZX decoding

Keep VM diagnostics available during plugin initialization failures and verify settings through the unified driver lifecycle.
2026-12-05 20:54:41 +08:00

105 lines
3.3 KiB
Go

package builtin
import (
"fmt"
"github.com/Nwflower/reims-go/internal/driver"
"github.com/Nwflower/reims-go/internal/host"
"github.com/Nwflower/reims-go/internal/i18n"
"github.com/Nwflower/reims-go/internal/plugin"
"github.com/Nwflower/reims-go/internal/version"
)
// KernelBootstrapOptions configures the shared GUI/CLI plugin-kernel and
// driver-router composition root.
type KernelBootstrapOptions struct {
DataRoot string
DataRootFn func() string
SafeMode bool
LocaleFn func() i18n.Locale
Logger func(source, line string)
HostHandlers func(pluginID string) map[string]plugin.RPCRequestHandler
OnSettingsChanged func(pluginID, vmName string)
OnSidecarStopped func(pluginID string)
VMInventory driver.VMInventory
Callbacks HostCallbacks
RegisterGuest func(reg *plugin.Registry, cb HostCallbacks, desired func(id string, defaultEnabled bool) bool) error
// StrictRegister causes builtin/guest registration errors to abort bootstrap
// instead of logging warnings (used by CLI commands).
StrictRegister bool
}
// BootstrapKernel creates and wires the plugin manager, driver router,
// built-in extensions, optional guest plugin, and external plugins in one
// canonical order shared by both the desktop app and the CLI.
func BootstrapKernel(opts KernelBootstrapOptions) (*plugin.Manager, *driver.Router, error) {
logf := opts.Logger
if logf == nil {
logf = func(string, string) {}
}
mgr, err := plugin.NewManager(plugin.ManagerOptions{
DataRoot: opts.DataRoot,
HostVersion: version.Version,
SafeMode: opts.SafeMode,
RedactFn: host.RedactText,
LocaleFn: opts.LocaleFn,
Logger: logf,
HostHandlers: opts.HostHandlers,
OnSettingsChanged: opts.OnSettingsChanged,
OnSidecarStopped: opts.OnSidecarStopped,
SidecarDriverFactory: func(contrib plugin.GuestDriverContribution, sup *plugin.SidecarSupervisor) any {
return driver.NewSidecarDriver(contrib, sup)
},
})
if err != nil {
if opts.StrictRegister || mgr == nil {
if mgr != nil {
mgr.Close()
}
return nil, nil, err
}
logf("plugin", "Plugin state warning: "+err.Error())
}
dataRootFn := opts.DataRootFn
if dataRootFn == nil {
root := opts.DataRoot
dataRootFn = func() string { return root }
}
router := driver.NewRouter(mgr.Registry(), dataRootFn, opts.VMInventory)
mgr.Registry().SetInUseChecker(router.PluginInUseChecker())
mgr.Registry().SetBusyChecker(router.PluginBusyChecker())
cb := opts.Callbacks
if cb.Log == nil {
cb.Log = logf
}
if cb.GetSetting == nil {
cb.GetSetting = func(pluginID, vmName, key string) (any, bool) {
v, ok := mgr.GetSettings(pluginID, vmName, false)[key]
return v, ok
}
}
if err := RegisterAll(mgr.Registry(), cb, mgr.DesiredFor); err != nil {
if opts.StrictRegister {
mgr.Close()
return nil, nil, err
}
logf("plugin", "Builtin extension register error: "+err.Error())
}
if opts.RegisterGuest != nil {
if err := opts.RegisterGuest(mgr.Registry(), cb, mgr.DesiredFor); err != nil {
if opts.StrictRegister {
mgr.Close()
return nil, nil, err
}
logf("plugin", "macOS guest plugin register error: "+err.Error())
}
}
if err := mgr.LoadExternal(); err != nil {
logf("plugin", fmt.Sprintf("External plugin load warning: %v", err))
}
return mgr, router, nil
}