外掛
Go 外掛是用於擴充套件遊戲執行時、場景編輯器或 game 命令列工具的 Go 模組。由於 iOS 和 Web 目標要求所有原生程式碼在編譯期靜態連結,Go 外掛是以普通 Go 包的形式通過 go.mod 接入遊戲專案的,而不是在執行時動態載入共享庫。
import (
"github.com/nsgo-dev/game/pkg/gameplugin"
"github.com/nsgo-dev/game/pkg/gameplugin/manifest"
)
執行時外掛(pkg/gameplugin)
外掛的執行時 Go 包匯出一個返回 gameplugin.Plugin 的建構函式(約定為 func Plugin() gameplugin.Plugin),遊戲通過 gameplugin.Install 將所有啟用的外掛安裝到 gameplugin.Host 上。pkg/gameplugin 不匯入 pkg/editor、pkg/mcp 或 pkg/tooling/playtest,確保釋出的二進位制只連結實際用到的執行時包。
func Plugin() gameplugin.Plugin {
return gameplugin.Plugin{
Name: "health",
Module: "example.com/healthkit",
API: gameplugin.APIVersion,
Components: []sceneformat.ComponentType{
Type(), // 包含 Fields、Events、Handlers 及可選的 Spawn
},
Systems: []gameplugin.System{
{Name: "health.regen", Stage: gameplugin.StageUpdate, Fn: regenSystem},
},
Codecs: func(w *entities.World) error {
return entities.RegisterCodec[Stats](w, "health")
},
}
}
安裝到 Host
gameplugin.Host 聚合遊戲希望外掛填充的登錄檔;任意為 nil 的欄位都會跳過對應類別的貢獻:
Host.Components(*sceneformat.ComponentRegistry):按外掛引數順序與切片順序合併每個外掛的Components(保留ComponentType.Spawn)。Host.Scheduler(*entities.Scheduler):為每個排程器一次性新增四組標準階段錨點,隨後按外掛與切片順序註冊各外掛的Systems。Host.DataTables(*datatable.Registry):按檔案路徑模式排序依次註冊每個外掛的RowTypes。Host.Console(*gamedebug.Console):按切片順序註冊每個外掛的Console命令,拒絕與內建命令或已有命令重名。Host.OnWorld(w)按安裝順序在w上執行每個已安裝外掛的Codecs回撥。每次遊戲建立新的entities.World(包括從存檔恢復的新世界)時都應呼叫OnWorld。Host.Plugins()返回已安裝外掛的後設資料(Name、Module以及通過runtime/debug.ReadBuildInfo讀取的Version;本地構建或無版本號的replace指令返回"(devel)")。Host.Has(name)報告外掛是否已安裝。
host := &gameplugin.Host{
Components: sceneformat.NewComponentRegistry(),
Scheduler: entities.NewScheduler(),
DataTables: datatable.NewRegistry(),
Console: gamedebug.NewConsole(),
}
if err := gameplugin.Install(host, health.Plugin()); err != nil {
return err
}
world := entities.NewWorld()
if err := host.OnWorld(world); err != nil {
return err
}
校驗、確定性與程序全域性登錄檔
- 註冊前校驗:
Install會先檢查Plugin.API == gameplugin.APIVersion、校驗外掛名與模組路徑、拒絕重複外掛名,並確認所有元件、系統、畫素特效、資料表模式與控制台命令名均未被佔用,之後才會寫入任何登錄檔。出現任何衝突或校驗錯誤時,不會產生半成品註冊。 - 確定性順序:按引數順序處理外掛、按宣告順序處理切片欄位(
Components、Systems、Console)、按排序後的鍵順序處理對映(PixelFX、RowTypes)。Plugin.Install逃生艙(func(*Host) error)在所有宣告式貢獻提交之後最後執行。 - 程序全域性冪等(
PixelFX):pixelfx特效儲存在程序全域性登錄檔中。Install會記錄每個外掛註冊特效的歸屬(Name, Module),因此在第二個測試或編輯器過載後重復安裝同一外掛時會自動跳過而不會 panic;若其他外掛或內建特效已佔用該 ID,則在寫入任何狀態前明確報錯。 - 不設
Version欄位:與plugin.yaml一致,gameplugin.Plugin不包含Version欄位,版本號統一通過gameplugin.ModuleVersion(modulePath)從go.mod構建資訊讀取。gameplugin.APIVersion僅在外掛契約發生破壞性變更時遞增。
標準排程階段
第三方系統通常希望在不瞭解宿主遊戲內部系統名稱的前提下插入到確定階段。gameplugin.Install(或 gameplugin.EnsureStages(sched))會為每個 *entities.Scheduler 一次性新增四組固定順序的階段錨點:
gameplugin.StageInput("input")——讀取輸入並轉換玩家意圖。gameplugin.StageUpdate("update")——常規玩法邏輯系統。gameplugin.StagePhysics("physics")——物理、移動與碰撞解算。gameplugin.StageLate("late")——相機跟隨、變換同步與收尾清理。
設定 System.Stage 會自動為系統新增 entities.After(stage.Start()) 與 entities.Before(stage.End()) 約束。同一階段內的系統預設按註冊順序執行,也可通過 Before 和 After 進一步約束。遊戲自身的 Scheduler.AddSystem 呼叫同樣可以引用 stage.Start() 與 stage.End()。
開發期貢獻(pkg/gameplugin/devplugin)
MCP 橋接預製體(pkg/mcp)與自動試玩機器人(pkg/tooling/playtest)依賴較重的檢查與回放包,釋出版遊戲二進位制不應連結這些程式碼(見包體積預算)。這些貢獻拆分在獨立的 github.com/nsgo-dev/game/pkg/gameplugin/devplugin 包中:
import "github.com/nsgo-dev/game/pkg/gameplugin/devplugin"
devHost := &devplugin.Host{Bridge: bridge}
if err := devplugin.Install(devHost, devplugin.Plugin{
Name: "healthkit",
Module: "example.com/healthkit",
API: gameplugin.APIVersion,
Prefabs: []devplugin.PrefabSpec{
{Prefab: patientPrefab},
},
Bots: map[string]playtest.BotFactory{
"healthkit-medic": newMedicBot,
},
}); err != nil {
return err
}
devplugin.Install 遵循同樣的 API 版本校驗、原子化註冊前衝突檢查、確定性順序,以及 playtest 機器人登錄檔的程序全域性歸屬規則(阻止覆蓋內建或其他外掛的機器人,同時允許同一外掛冪等重灌),且不匯入 pkg/editor。
plugin.yaml 清單
每個 Go 外掛都在其 Go 模組根目錄放置一份 plugin.yaml。命令列工具、資源與外掛庫索引以及編輯器會在編譯任何程式碼之前讀取 plugin.yaml,以獲知外掛提供的 Go 入口包、相容的引擎版本與目標平臺,以及所依賴或衝突的其他外掛模組。
manifestVersion: 1
name: healthkit
module: example.com/healthkit
description: Health component, ward panel and admit tool
license: MIT
authors: [Jane Doe]
repository: https://github.com/example/healthkit
engine: '>=0.4.0 <0.5.0'
api: 1
platforms: [all]
entries:
runtime: example.com/healthkit/health
editor: example.com/healthkit/healtheditor
cli:
- name: healthkit
package: example.com/healthkit/cmd/game-healthkit
requires:
example.com/navkit: ^0.3.0
conflicts: [example.com/oldhealth]
contributes:
components: [health]
editor:
panels: [ward]
tools: [admit]
commands: [heal-all]
agentTools: [plugin.healthkit.heal_all]
為什麼沒有 version 欄位
外掛的版本就是其 Go 模組版本,由遊戲專案的 go.mod 解析並由 go.sum 記錄。如果在 plugin.yaml 中再寫一份版本號,必然會與 Git 標籤和 go.mod 產生漂移,因此 plugin.yaml 刻意不設 version 欄位,並將其作為未知欄位拒絕。
出於同樣原因,requires 和 conflicts 以 Go 模組路徑(例如 example.com/navkit)而非短名稱作為鍵,使依賴解析與衝突檢查能夠直接對應 go.mod 中解析出的模組。
欄位說明
| 欄位 | 必填 | 說明 |
|---|---|---|
manifestVersion |
是 | plugin.yaml 的模式版本。必須寫為純十進位制整數 1(拒絕 1.0 和 0x1)。 |
name |
是 | 外掛短名稱:1 到 64 個小寫 ASCII 字母、數字或 -,且以字母或數字開頭(與資源包名稱規則一致)。 |
module |
是 | 外掛的 Go 模組路徑,必須與外掛 go.mod 中的 module 行一致。 |
description |
否 | 單行簡介,不含控制字元。 |
license |
否 | 單行許可證標識,例如 MIT 或 Apache-2.0。 |
authors |
否 | 非空單行作者名稱列表,不允許重複。 |
repository |
否 | 外掛原始碼倉庫的 http:// 或 https:// URL。 |
engine |
是 | 對 github.com/nsgo-dev/game 的語義化版本約束,例如 >=0.4.0 <0.5.0 或 ^0.4.0。 |
api |
是 | 外掛所針對的正整數 gameplugin.APIVersion(例如 1)。 |
platforms |
否 | 單獨的 [all],或 macos、windows、linux、ios、android、web 的子集。省略時表示支援全部平臺。 |
entries |
是 | 位於 module 內部的 Go 包匯入路徑。entries.runtime、entries.editor 與 entries.cli 至少須設定一項。 |
entries.runtime |
否 | 提供執行時入口(func Plugin() gameplugin.Plugin)的 Go 包,必須等於 module 或位於 module/ 之下。 |
entries.editor |
否 | 提供編輯器入口(func Plugin() editor.Plugin)的 Go 包,必須等於 module 或位於 module/ 之下。 |
entries.cli |
否 | 通過 go.mod tool 指令擴充套件 game 命令列工具的 {name, package} 條目列表。每個 name 遵循外掛名稱規則且不可重複;每個 package 必須等於 module 或位於 module/ 之下。 |
requires |
否 | 所依賴外掛的 Go 模組路徑到語義化版本約束(如 ^0.3.0)的對映。鍵必須是合法 Go 模組路徑,不能填寫自身 module,且與 conflicts 互不相交。 |
conflicts |
否 | 不能與本外掛同時安裝的外掛 Go 模組路徑列表。條目必須是合法且不重複的 Go 模組路徑,不能填寫自身 module,且與 requires 互不相交。 |
contributes |
否 | 外掛註冊符號的索引(components、editor.panels、editor.tools、editor.commands 和 agentTools),用於目錄索引並與實際程式碼比對。 |
嚴格解析與規範編碼
pkg/gameplugin/manifest 不依賴 pkg/editor,可供命令列工具、執行時程式碼和索引器直接匯入:
manifest.Parse(data)嚴格解碼單個 YAML 文件,拒絕未知欄位、重複鍵和尾隨文件,要求manifestVersion與api為純十進位制整數,並校驗所有欄位。返回的錯誤可通過errors.Is匹配manifest.ErrInvalid,同時為*loaderr.Error,包含檔案、行號、欄位路徑與修復建議。manifest.ParseFS(fsys, dir)從fsys的dir目錄讀取並解析plugin.yaml,並將檔案路徑附加到任何*loaderr.Error上。manifest.Encode(m)校驗m(若ManifestVersion為0則填入1)並寫出確定性的 2 空格縮排 YAML:所有列表及requires對映鍵均按位元組序排序,且不會修改呼叫方傳入的切片。(Manifest).Validate()校驗程式碼中構造的清單。(Manifest).SupportsPlatform(platform)報告清單是否支援指定平臺(macos、windows、linux、ios、android、web)。
m, err := manifest.ParseFS(os.DirFS(pluginDir), ".")
if err != nil {
return err
}
if !m.SupportsPlatform("web") {
return fmt.Errorf("plugin %s does not support web", m.Name)
}
JSON Schema
plugin.yaml 的已釋出 JSON Schema 位於 https://game.nsgo.dev/schemas/plugin.v1.json(manifest.SchemaURL)。可在 plugin.yaml 首行新增 Schema 註釋(manifest.Parse 會忽略註釋),或在編輯器的 YAML 設定中將 plugin.yaml 對映到該 Schema,以獲得補全與即時校驗:
# yaml-language-server: $schema=https://game.nsgo.dev/schemas/plugin.v1.json