121 lines
3.7 KiB
Go
121 lines
3.7 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/thumb"
|
|
"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)
|
|
VMInventories []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.VMInventories...)
|
|
mgr.Registry().SetInUseChecker(router.PluginInUseChecker())
|
|
mgr.Registry().SetBusyChecker(router.PluginBusyChecker())
|
|
|
|
cb := opts.Callbacks
|
|
if cb.ConsoleImages.Load == nil {
|
|
cb.ConsoleImages.Load = thumb.Load
|
|
}
|
|
if cb.ConsoleImages.Scale == nil {
|
|
cb.ConsoleImages.Scale = thumb.Scale
|
|
}
|
|
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 cb.PluginDataDir == nil {
|
|
cb.PluginDataDir = mgr.Layout().DataDir
|
|
}
|
|
if cb.PluginLogPath == nil {
|
|
cb.PluginLogPath = mgr.Layout().LogPath
|
|
}
|
|
if cb.CallHostAPI == nil {
|
|
cb.CallHostAPI = mgr.CallHostAPI
|
|
}
|
|
|
|
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
|
|
}
|