Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

项目配置

项目清单既可以只是 schema root,也可以是完整的构建描述。它可以写成 SCON、TOML、YAML、JSON 或 Lua;新项目推荐 SCON。

project { id = "game_config" }
groups { common { default = true } }
views {
  default {
    contract = "game_config/default"
    groups = ["common"]
  }
}
includes = ["schema/items.scon"]

parsers { scripts = ["tools/parsers.lua"] }
type_mappings { scripts = ["tools/type_mappings.lua"] }

build {
  default_source_format = "xlsx"
  data_root = "data"
  view = "default"
  schema_lock = "generated/schema.lock"
  excel_templates = "generated/excel"
  codegen = [
    { target = "rust", out = "rust/src/generated", format = "auto" },
  ]
  exports = [
    { format = "binary", out = "generated/config.sora" },
  ]
}

运行所有配置好的输出:

sora build --project project.scon

data_root 和 excel_templates 的用途不同。data_root 是 export 和 build 读取的输入目录,里面放已经填写过行数据的文件。excel_templates 是生成 workbook 模板的输出目录,schema 变更后可以删除并重新生成。不要把 excel_templates 指向已经编辑过的数据目录,除非你明确想替换那些 workbook。

parsers.scripts 列出 CLI 读取该 project 时使用的自定义 Lua 单元格 parser 脚本。路径相对 project 文件所在目录。脚本 API 见单元格 Parser。

type_mappings.scripts 列出用于自定义生成语言类型的 Lua 脚本。路径相对 project 文件所在目录。类型映射只影响 codegen:schema 仍然使用 struct<Vec3> 这类语言无关的 Sora 类型,映射脚本可以把这个命名类型映射到目标语言自己的类型。

多语言通过 project root 的 localization 声明。keyed sources 独立于普通 table;见多语言。

只运行一个配置好的 codegen target:

sora build --project project.scon --target rust

Target Options

语言相关选项放在 [codegen.<target>] 下:

[codegen.rust]
runtime_format = "sora"

[codegen.typescript]
runtime_format = "json"
enum_repr = "string"

[codegen.lua]
runtime_format = "cbor"
lua_version = "5.4"

这些选项由对应生成器消费。归一化 IR 保持语言无关。

类型映射脚本返回带 type_mappings 的 table。每条映射对应一个目标语言和一个命名 schema 类型:

return {
  type_mappings = {
    {
      target = "csharp",
      schema_type = "Vec3",
      type_name = "Vector3",
      nullable_type_name = "Vector3?",
      decode = "GameMappings.ToVector3({value})",
      value_decode = "GameMappings.ToVector3({value})",
      imports = { "UnityEngine" },
    },
  },
}

nullable_type_name 是可选字段。当 optional<schema_type> 需要不同于后端默认 nullable wrapper 的目标语言类型表达式时使用它。

decode 包裹默认的 binary runtime decode 表达式,value_decode 包裹 JSON/CBOR/protobuf 风格的 value decode 表达式。{value} 会替换成生成器默认生成的表达式。

C target 使用写入目标指针的 decode 函数,所以 C 映射应使用 decode_into,而不是 decode。{target} 会替换成输出指针表达式。C 映射也可以提供 free,其中 {target} 会替换成需要释放的指针:

{
  target = "c",
  schema_type = "Vec3",
  type_name = "game_vector3",
  decode_into = "game_vector3_decode(reader, {target})",
  free = "game_vector3_free({target});",
  imports = { "#include \"vector3.h\"" },
}

imports 是目标语言相关的,只由需要它的语言生成器输出。C#、Java、Kotlin、Scala 期望不带关键字的 namespace/path;Go 期望类似 "example.com/game/vector" 的 import spec;Python、TypeScript、JavaScript、Dart、Godot、C、C++、Rust 期望完整 import/include/use/preload 行。

runtime_format 可以是 sora、json、cbor 或 sora-protobuf,但不是每个 target 都支持所有 runtime format。支持矩阵见运行时格式。

内置 Target Options

TargetOptions
rustruntime_format 默认 sora;map_type = "std" 或 "fx_hash_map",默认 std;string_storage = "owned" 或 "arc",默认 owned;可选 crate 用于生成独立 library crate。
kotlinruntime_format 默认 sora。
csharpruntime_format 默认 sora。
javaruntime_format 默认 sora;nullable_annotation 默认 SoraNullable,也可以设置成 org.jetbrains.annotations.Nullable 这类 annotation class,或设置为 "" 禁用 annotation。
scalaruntime_format 默认 sora;scala_version = "2.12"、"2.13" 或 "3",默认 3。
goruntime_format 默认 sora。
dartruntime_format = "json"、"cbor" 或 "sora-protobuf"。建议显式设置;Dart 不支持 sora。
godotruntime_format = "json"。建议显式设置;这是 Godot 唯一支持的 runtime format。
cruntime_format = "sora";c_standard = "c99"、"c11"、"c17" 或 "c23",默认 c11;prefix 是可选 symbol prefix。
cppruntime_format = "sora";cpp_standard = "c++11"、"c++14"、"c++17"、"c++20" 或 "c++23",默认 c++17;namespace 是可选 C++ namespace。
typescriptruntime_format 默认 sora;enum_repr = "string" 或 "integer",默认 string。
javascriptruntime_format 默认 sora;enum_repr = "string" 或 "integer",默认 string;emit_dts 是 boolean,默认 true。
erlangruntime_format 默认 sora;enum_repr = "atom" 或 "integer",默认 atom。
luaruntime_format 默认 sora;module 是可选 require/import 前缀;lua_version = "5.1"、"5.2"、"5.3"、"5.4" 或 "luajit",默认 5.4;enum_repr = "string" 或 "integer",默认 string。
pythonruntime_format 默认 sora。
proto-schema没有 target options。它生成 .proto schema 文件,不生成 runtime loader。

包含多种语言选项的示例:

[codegen.rust]
runtime_format = "sora"
map_type = "fx_hash_map"
string_storage = "arc"

[codegen.cpp]
runtime_format = "sora"
cpp_standard = "c++20"
namespace = "game::config"

[codegen.javascript]
runtime_format = "json"
enum_repr = "integer"
emit_dts = true

独立 Rust Crate

Rust 默认会把 module tree 直接写到配置的 out 目录。设置 codegen.rust.crate 后, out 表示一个完整 library crate 的根目录:

build {
  codegen = [{ target = "rust", out = "generated/game-config" }]
}

codegen {
  rust {
    runtime_format = "sora"
    crate {
      name = "game-config"
      version = "0.1.0"
      edition = "2024"
      publish = false
    }
  }
}

Sora 会在 generated/game-config 下写出 Cargo.toml、src/lib.rs、生成的 model 和 runtime。name 必填;version、edition 和 publish 分别默认取 0.1.0、2024 和 false。生成的 manifest 会根据 runtime format 和 Rust 选项自动加入所需依赖。 应用可以像普通 path dependency 一样使用它:

[dependencies]
game-config = { path = "../generated/game-config" }

未设置 crate 时,原有 module 布局保持不变,此时 out 应指向消费方 crate 内部的 module 目录。