s&box CLI
面向 s&box 游戏引擎 (Facepunch Studios, Source 2) 的代理原生 CLI。79+ 命令,14 个分组。直接操作 .scene、.prefab、.vmat、.sound、Input.config、Collision.config 等 JSON 文件,生成符合惯用法的 C# 组件,并在项目上运行实时资源图。
安装
pip install git+https://github.com/HKUDS/CLI-Anything.git#subdirectory=sbox/agent-harness
系统要求
- Python 3.10+
- 通过 Steam 安装的 s&box – 自动检测,或设置
SBOX_PATH指向自定义安装目录。 - 大多数命令完全基于项目的 JSON 文件运行 – s&box 无需运行。
全局选项
--json JSON 格式输出(代理使用时始终传递此参数)
--project PATH 项目目录(若省略则从当前工作目录的 .sbproj 自动检测)
命令分组
project - 管理 s&box 项目
cli-anything-sbox project new --name MyGame --output-dir ./MyGame
cli-anything-sbox --json project info
cli-anything-sbox project config --max-players 32 --tick-rate 64
cli-anything-sbox --json --project ./MyGame project add-package facepunch.libsdf
cli-anything-sbox --json --project ./MyGame project remove-package facepunch.libsdf
# 检查:断开的资源引用、重复的 GUID、格式错误的 Input.config
cli-anything-sbox --json --project ./MyGame project validate
cli-anything-sbox --project ./MyGame project validate --no-inputs --no-guids
scene - 操作 .scene 文件
cli-anything-sbox scene new --name gameplay -o ./Assets/scenes/gameplay.scene
cli-anything-sbox --json scene info ./scene.scene
cli-anything-sbox --json scene list ./scene.scene
cli-anything-sbox scene add-object ./scene.scene Enemy --position "100,0,0" --components "model,box_collider,rigidbody" --tags "enemy"
cli-anything-sbox scene remove-object ./scene.scene --name Enemy
cli-anything-sbox scene add-component ./scene.scene <object-guid> Sandbox.PointLight
cli-anything-sbox scene remove-component ./scene.scene <object-guid> --component-type Sandbox.Rigidbody
cli-anything-sbox --json scene modify-object ./scene.scene --guid <guid> --name NewName --position "200,0,0"
cli-anything-sbox --json scene clone-object ./scene.scene --name Enemy --new-name EnemyCopy --position "300,0,0"
cli-anything-sbox --json scene get-object ./scene.scene --name Sun
cli-anything-sbox scene set-property ./scene.scene --fixed-update-freq 64 --timescale 0.5
cli-anything-sbox scene set-navmesh ./scene.scene --enabled --agent-height 72 --agent-radius 16
cli-anything-sbox --json scene list-presets
cli-anything-sbox --json scene modify-component ./scene.scene <object-guid> <component-guid> --properties '{"Damage":50}'
# 查找匹配一个或多个过滤器的对象(AND 组合)
cli-anything-sbox --json scene query ./scene.scene --has-component rigidbody --has-tag tower
cli-anything-sbox --json scene query ./scene.scene --name-regex "^Tower\d$" --in-bounds "0,-500,-50,1000,500,50"
# 从场景中提取所有资源引用(.vmdl、.vmat、.vsnd、.vtex、.vpcf、.prefab)
cli-anything-sbox --json scene refs ./scene.scene
# 对所有匹配查询的对象应用相同的修改
cli-anything-sbox --json scene bulk-modify ./scene.scene --has-tag tower --position "0,0,100" --enable
# 两个场景之间的结构差异(添加/移除/修改的 GameObject + SceneProperties)
cli-anything-sbox --json scene diff ./old.scene ./new.scene
# 将一个预设作为 GameObject 插入场景中
cli-anything-sbox --json scene instantiate-prefab ./level.scene ./Assets/prefabs/bullet.prefab --position "10,0,0"
prefab - 操作 .prefab 文件
cli-anything-sbox prefab new --name Bullet -o ./Assets/prefabs/bullet.prefab --components "model,sphere_collider,rigidbody"
cli-anything-sbox --json prefab info ./Assets/prefabs/bullet.prefab
cli-anything-sbox prefab from-scene ./scene.scene <object-guid> -o ./Assets/prefabs/extracted.prefab
cli-anything-sbox --json prefab add-component ./prefab.prefab rigidbody
cli-anything-sbox prefab remove-component ./prefab.prefab --component-type Sandbox.Rigidbody
cli-anything-sbox --json prefab list
# 预设内部的资源引用
cli-anything-sbox --json prefab refs ./prefab.prefab
# 按组件类型或组件 GUID 修改预设上的组件属性
cli-anything-sbox --json prefab modify-component ./prefab.prefab --component-type Sandbox.Rigidbody --properties '{"Gravity":false,"MassOverride":50}'
# 两个预设之间的结构差异(根对象 + 命名子对象)
cli-anything-sbox --json prefab diff ./old.prefab ./new.prefab
codegen - 生成 C# 代码
# 普通组件
cli-anything-sbox codegen component --name PlayerController --methods OnUpdate,OnFixedUpdate -o ./Code/PlayerController.cs
# 带有 [Property] 字段的组件
cli-anything-sbox --json codegen component --name Tower --properties '[{"name":"Damage","type":"float","default":"25f"},{"name":"Range","type":"float","default":"500f"}]'
# 网络化组件(分部类)并包含 RPC 方法存根
cli-anything-sbox codegen component --name NetPlayer --networked --methods OnUpdate,OnFixedUpdate --rpc-methods "Fire:Broadcast,TakeDamage:Host" -o ./Code/NetPlayer.cs
# GameResource
cli-anything-sbox codegen gameresource --name TowerData --display-name "Tower Data" --extension tower -o ./Code/TowerData.cs
# 编辑器菜单(Editor/ 程序集)
cli-anything-sbox codegen editor-menu --name MyTools --menu-path "Editor/My Tools/Open" -o ./Editor/MyTools.cs
# 普通 Razor UI 组件(.razor + .razor.scss)
cli-anything-sbox codegen razor --name HudPanel --properties '[{"name":"Score","type":"int","default":"0"}]' -o ./UI/HudPanel.razor
# 静态类或基类派生类
cli-anything-sbox codegen class --name Math2 --static -o ./Code/Math2.cs
# PanelComponent + 同级 ScreenPanel 脚手架(处理 s&box 中 PanelComponent 输入仅当两者位于同一 GameObject 时才能工作的特性)。
# 生成 .razor + .razor.scss + 一个可直接粘贴的 GameObject 片段,或使用 --scene 直接追加到场景中。
cli-anything-sbox --json codegen panel-component --name HudBar -o ./UI/HudBar.razor
cli-anything-sbox codegen panel-component --name Crosshair -o ./UI/Crosshair.razor --scene ./Assets/scenes/game.scene
input - 管理 Input.config
cli-anything-sbox --json input list
cli-anything-sbox input add --name PlaceTower --group Gameplay --keyboard mouse1 --gamepad RightTrigger
cli-anything-sbox input remove --name PlaceTower
cli-anything-sbox input set --name Attack1 --keyboard mouse1
collision - 管理 Collision.config
cli-anything-sbox --json collision list
cli-anything-sbox collision add-layer --name projectile --default Collide
cli-anything-sbox collision add-rule --layer-a projectile --layer-b solid --result Collide
cli-anything-sbox collision remove-rule --layer-a projectile --layer-b solid
cli-anything-sbox collision remove-layer --name projectile
material - 管理 .vmat 材质
cli-anything-sbox --json material new --name floor --shader complex --color-texture "textures/floor.tga" --metalness 0.3 -o ./Assets/materials/floor.vmat
cli-anything-sbox --json material info ./Assets/materials/floor.vmat
cli-anything-sbox --json material list
cli-anything-sbox --json material set ./Assets/materials/floor.vmat --metalness 0.8
sound - 管理 .sound 事件
cli-anything-sbox --json sound new --name gunshot --sounds "sounds/gun1.vsnd,sounds/gun2.vsnd" --volume 0.8 -o ./Assets/sounds/gunshot.sound
cli-anything-sbox --json sound info ./Assets/sounds/gunshot.sound
cli-anything-sbox --json sound list
cli-anything-sbox --json sound set ./Assets/sounds/gunshot.sound --volume 0.5
localization - 管理翻译
cli-anything-sbox localization new --lang en -o ./Localization/en.json
cli-anything-sbox localization set ./Localization/en.json --key "game.title" --value "My Game"
cli-anything-sbox --json localization list ./Localization/en.json
cli-anything-sbox localization get ./Localization/en.json --key "game.title"
cli-anything-sbox localization remove ./Localization/en.json --key "game.title"
cli-anything-sbox localization bulk-set ./Localization/en.json --keys '{"ui.start":"Start","ui.quit":"Quit"}'
server - 专用服务器
cli-anything-sbox server info
cli-anything-sbox server start --game my_game --map facepunch.flatgrass
asset - 资源管理及反向查找
cli-anything-sbox --json asset list --type scene
cli-anything-sbox --json asset info ./Assets/scenes/minimal.scene
cli-anything-sbox asset compile ./Assets/materials/floor.vmat
# 哪些场景/预设引用了此资源?
cli-anything-sbox --json --project ./MyGame asset find-refs models/myteam/widget.vmdl
# 查找未被引用的资源(按类型,或跨所有可引用类型)
cli-anything-sbox --json --project ./MyGame asset find-unused
cli-anything-sbox --json --project ./MyGame asset find-unused --type model --type material
# 重命名资源并更新所有场景/预设引用(省略扩展名则保持原样)
cli-anything-sbox --json --project ./MyGame asset rename models/team/widget.vmdl gizmo
cli-anything-sbox --project ./MyGame asset rename models/team/widget.vmdl gizmo --dry-run
# 将资源移动到其他目录并更新所有引用
cli-anything-sbox --json --project ./MyGame asset move models/team/widget.vmdl models/shared/widget.vmdl
session - 状态管理
cli-anything-sbox session status
cli-anything-sbox session undo
cli-anything-sbox session redo
test - 自动化地图/场景生成工具
cli-anything-sbox test setup
cli-anything-sbox --json test run
launch - 在 s&box 编辑器中打开项目
cli-anything-sbox launch
组件预设
29 个简短名称,可与 scene add-object、prefab new 等命令的 --components 参数配合使用。每个名称映射到 Sandbox.* 组件类型。
| 预设名称 | 组件类型 |
|---|---|
model | Sandbox.ModelRenderer |
box_collider | Sandbox.BoxCollider |
sphere_collider | Sandbox.SphereCollider |
capsule_collider | Sandbox.CapsuleCollider |
plane_collider | Sandbox.PlaneCollider |
model_collider | Sandbox.ModelCollider |
rigidbody | Sandbox.Rigidbody |
character_controller | Sandbox.CharacterController |
player_controller | Sandbox.PlayerController |
camera | Sandbox.CameraComponent |
light_directional | Sandbox.DirectionalLight |
light_point | Sandbox.PointLight |
spot_light | Sandbox.SpotLight |
ambient_light | Sandbox.AmbientLight |
sprite_renderer | Sandbox.SpriteRenderer |
skinned_model_renderer | Sandbox.SkinnedModelRenderer |
text_renderer | Sandbox.TextRenderer |
line_renderer | Sandbox.LineRenderer |
decal_renderer | Sandbox.DecalRenderer |
trail_renderer | Sandbox.TrailRenderer |
particle_effect | Sandbox.ParticleEffect |
sound_point | Sandbox.SoundPointComponent |
nav_mesh_agent | Sandbox.NavMeshAgent |
screen_panel | Sandbox.ScreenPanel |
world_panel | Sandbox.WorldPanel |
fixed_joint | Sandbox.FixedJoint |
hinge_joint | Sandbox.HingeJoint |
spring_joint | Sandbox.SpringJoint |
ball_socket_joint | Sandbox.BallSocketJoint |
scene list-presets 以 JSON 格式返回实时的预设注册表。
代理使用模式
对于每次代理驱动的调用,请传递 --json。通过 jq 管道在命令之间传递 GUID 和路径:
# 引导项目
sbproj=$(cli-anything-sbox --json project new --name MyGame --output-dir /tmp/MyGame | jq -r '.sbproj')
cd /tmp/MyGame
# 添加一个具有特定组件布局的塔,捕获其 GUID
guid=$(cli-anything-sbox --json scene add-object ./Assets/scenes/main.scene Tower \
--components "model,box_collider,rigidbody" --tags "tower" \
| jq -r '.guid')
# 生成对应的网络化 C# 组件
cli-anything-sbox --json codegen component --name Tower --networked \
--rpc-methods "Fire:Host" --properties '[{"name":"Damage","type":"float","default":"25f"}]' \
-o ./Code/Tower.cs
# 批量调整场景中所有塔
cli-anything-sbox --json scene bulk-modify ./Assets/scenes/main.scene \
--has-tag tower --enable --position-y-add 50
# 提交前验证
cli-anything-sbox --json project validate
REPL 模式
cli-anything-sbox 不带参数时进入交互式 REPL,带有样式化提示符:
$ cli-anything-sbox
cli-anything-sbox> project info
cli-anything-sbox> scene list ./Assets/scenes/main.scene
cli-anything-sbox> codegen component --name Foo -o ./Code/Foo.cs
cli-anything-sbox> quit
环境变量
| 变量 | 描述 | 默认值 |
|---|---|---|
SBOX_PATH | s&box 安装目录 | 从 Steam 库文件夹自动检测 |
CLI_ANYTHING_SKILL_REPO | 覆盖 repl_skin 读取 SKILL.md 的仓库 | HKUDS/CLI-Anything |