{"modulePath":"github.com/nsgo-dev/game","version":"v0.4.0-rc.3","isLatest":false,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package scriptplugin\n\nPackage scriptplugin is experimental: its API and documented formats may change in a minor release; see [https://nsgo.games/docs/stability/](https://nsgo.games/docs/stability/).\n\nIt runs editor plugins written as sandboxed TSX scripts. A script plugin is a mod whose mod.yaml (version 2) sets target: editor, in a directory of its own under the project's editor/plugins/; a Host loads each one on package modding's sandbox and registers it with the editor as an ordinary editor.Plugin named after its directory, so game.yaml's plugins: list turns it on and off, PluginProblems reports it and its lifecycle hooks run as a Go plugin's do.\n\nA script reaches the editor through the \"editor\" TSX module, whose functions each need a host capability the plugin requests in mod.yaml: CapPanel for registerPanel, CapCommand for registerCommand, CapContextAction for registerContextAction, CapSceneRead for selection, object and component, and CapSceneEdit for setComponentField, moveObject and deleteObject. Every edit goes through the Editor method of the same name, so it is one undoable History entry, and the edits one command or context action makes are grouped into one entry that a single Undo reverts. A script that throws an error it does not catch fails only its own plugin: its edits in that callback are undone, the editor turns the plugin off for the session (Editor.SuspendPlugin) and shows the error, and Reload tries it again.\n\nInspectors, viewport tools, gizmos, importers and build hooks stay Go-only.\n\n## Constants\n\n```go\nconst (\n\t// CapPanel allows registerPanel.\n\tCapPanel = \"editor.panel\"\n\t// CapCommand allows registerCommand.\n\tCapCommand = \"editor.command\"\n\t// CapContextAction allows registerContextAction.\n\tCapContextAction = \"editor.contextAction\"\n\t// CapSceneRead allows selection, object and component.\n\tCapSceneRead = \"scene.read\"\n\t// CapSceneEdit allows setComponentField, moveObject and deleteObject.\n\tCapSceneEdit = \"scene.edit\"\n)\n```\nThe host capabilities a script plugin requests in mod.yaml's capabilities list to call the editor module's functions.\n\n```go\nconst Dir = \"editor/plugins\"\n```\nDir is where a project keeps its script plugins, one directory per plugin, relative to the project root.\n\n```go\nconst Module = \"editor\"\n```\nModule is the TSX import specifier of the editor API script plugins import, as in import { registerCommand } from \"editor\".\n\n## Functions\n\n```go\nfunc Capabilities() []modding.HostCapability\n```\nCapabilities returns the host capabilities the editor module declares, with what each allows, in the order a consent prompt lists them.\n\n```go\nfunc WriteDTS(dir string) error\n```\nWriteDTS writes the TypeScript declarations script plugins compile against into dir: game.d.ts for the game module and editor.d.ts for Module.\n\n## Types\n\n```go\ntype Host struct {\n\t// contains filtered or unexported fields\n}\n```\nHost loads a project's script plugins into one editor. Its methods run on the editor goroutine, as the editor's do.\n\n```go\nfunc New(cx *core.Scope, ed *editor.Editor, opts Options) *Host\n```\nNew returns a Host that loads script plugins into ed, running their sandboxes under cx; disposing cx unloads them.\n\n```go\nfunc (h *Host) Err(name string) error\n```\nErr returns why plugin name cannot run: the failure of its mod.yaml, its script or a callback that faulted, or nil while it is healthy or unknown.\n\n```go\nfunc (h *Host) Load(fsys fs.FS) error\n```\nLoad discovers the script plugins in the directories directly under fsys, loads their scripts and registers each with the editor under its directory's name, which must be a valid plugin name (manifest.ValidateName). A plugin whose mod.yaml, script or registrations fail is still registered, without contributions and with an OnEnable that returns the failure, so the editor shows why it cannot turn on; Load returns the failures joined. A Host loads once; a second call returns an error.\n\n```go\nfunc (h *Host) LoadProject(projectDir string) error\n```\nLoadProject loads the script plugins under projectDir's Dir, as Load does; a project without that directory has none and LoadProject returns nil.\n\n```go\nfunc (h *Host) Manager() *modding.Manager\n```\nManager returns the mod manager running the plugins' scripts, for their logs and state.\n\n```go\nfunc (h *Host) Plugins() []string\n```\nPlugins returns the names of the script plugins Load found, in load order.\n\n```go\nfunc (h *Host) Reload(name string) error\n```\nReload reads plugin name's mod.yaml and script again and replaces its contributions in the editor (Editor.ReplacePlugin): its panels mount the new views, its commands and context actions are registered anew, and the undo history is kept. A mod.yaml that no longer validates is reported and the running version stays. A script that fails to load is reported, and the plugin stays registered without contributions and off for the session until a later Reload succeeds.\n\n```go\ntype Options struct {\n\t// GameVersion, when non-empty, is checked against each plugin's gameVersion constraint, as modding.ManagerOptions.GameVersion is.\n\tGameVersion string\n\t// Limits overrides tsx.DefaultLimits for each plugin's sandbox.\n\tLimits *tsx.Limits\n}\n```\nOptions configures a Host.\n\n","path":"github.com/nsgo-dev/game/pkg/editor/scriptplugin","name":"scriptplugin","synopsis":"Package scriptplugin is experimental: its API and documented formats may change in a minor release; see https://nsgo.games/docs/stability/.","isRedistributable":true}
