Skip to main content

dse-core · 字典场景引擎(Python 版)

Dictionary Scene Engine —— 场景即字典,能力即插件。 本仓库是引擎的 Python 原生实现(cordis-py),Node/Cordis 版见姊妹仓库 dse-core。立项计划书见 PLAN.md。

30 秒上手

pip install -e .          # 开发安装(或 pip install dse-core)
dse play                  # 玩自带示例内容包(战斗线最小可玩版)
dse play --input "1,1,a,a,a"   # 脚本模式:无交互跑完整条线路(CI 友好)
dse scenes                # 列出场景
dse packs                 # 列出内容包(manifest / 依赖声明)
dse check                 # 校验内容(悬空跳转等),有问题退出码 1
dse doctor                # 体检 + 内容模式 vs 已装插件交叉核对
dse saves                 # 离线列出存档位
dse new my-pack --template hub   # 脚手架生成 YAML 内容包并挂进 dse.yml

自带示例工程随包发布(src/dse_core/example/):在含 dse.yml 的目录里 运行 dse play 玩你自己的工程,否则玩包内示例。

内容包 = 数据,不是代码

与 Node 版最大的差异:内容包是 YAML 数据文件,不是可执行模块。 条件表达式经 AST 白名单求值(仅比较/布尔/算术/下标,禁调用、禁下划线属性)—— 不需要 import 任何用户代码,也没有 new Function。

# content/starter.yml(节选)
pack:
  id: starter
  name: 启程
  entry: start
scenes:
  start:
    title: 村口 · 启程
    text: '天没亮透……三条路摆在你面前。'
    choices:
      - { text: '往北进山', do: { 'flags.starter.mountain': true }, goto: forest }
  forest:
    text: '……一只独眼狼从阴影里站起来。'
    choices:
      - { text: '拔剑', goto: wolf-fight }
  wolf-fight:
    mode: battle                      # 非 normal 模式由能力插件接管
    battle: { enemy: wolf, onWin: forest-win, onLose: forest-lose, onFlee: start }

架构

dse_core/
├── modes.py       # 模式注册表(scene.mode 唯一真源)
├── graph.py       # 场景图静态分析(可达性/悬空/结局/最短路径,纯函数)
├── manifest.py    # 内容包元数据(规范化/校验)
├── loader.py      # 工程加载(dse.yml + YAML 内容包合并)
├── schema.py      # JSON Schema 校验(schema/scene.schema.json)
├── safety.py      # 条件表达式 AST 白名单求值
├── runtime.py     # 插件运行时(Service/Context/依赖拓扑/事件)—— Cordis 的 Python 原生替代
├── sdk.py         # 编程式门面(create_engine / play / inspect_project)
├── cli.py         # 命令行 dse:play / scenes / info / check / packs / saves / doctor / new
└── plugins/       # 内核层 scenes/state/console_io/engine
                   # 能力层 combat
                   # 系统层 save/save-migrate/achievements/i18n(P2)

插件用声明式依赖组装,引擎零改动即可扩展:

from dse_core.runtime import Service

class MyPlugin(Service):
    name = "my-plugin"
    inject = ("state", "io")          # 等价于 Cordis 的 static inject
    def setup(self):
        self.ctx.on("scene/enter", lambda scene_id, scene: ...)

系统层插件(P2,dse.yml 里按 id 启用):

插件 能力
save save/load/slots 命令;slot 白名单 + 原子落盘 + 读档结构校验 + 场景改名兜底
save-migrate 存档版本链迁移(config.migrations 挂模块,旧档不失效)
achievements 事件驱动成就解锁(scene/enter + choice/made),随档持久化
i18n @key@ 查表替换(未命中原样保留)、lang 命令切换语言

与 Node 版的关系

  • Node 版持续维护;本仓库是移植版,内容格式不互通(YAML ↔ ESM)。
  • 移植路线图:P1 可发布最小引擎 ✅ → P2 系统层与完整 CLI ✅ → P3 能力层(trade/dialogue/diary)→ P4 表现层(sound/hot-reload)+ 内容迁移工具。详见 PLAN.md。
  • 已知边界:内容用了某模式但没装对应插件时,引擎响亮报错(而非 Node 版的静默回退)——dse doctor 可静态交叉核对。

安全模型

  • 内容包是数据;表达式求值禁调用与下划线属性访问。
  • dse.yml 与内容包路径均限制在工程目录内(路径白名单)。

License

MIT · Copyright (c) 2026 全哥 (siwpan)

Metadata

Release files for dse-core 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dse-core 0.2.0
File Size Uploaded Provenance Actions
dse_core-0.2.0.tar.gz 55.3 kB Without provenance Details for dse_core-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for dse-core 0.2.0
File Interpreter ABI Platform Actions
dse_core-0.2.0-py3-none-any.whl Python 3 none any Details for dse_core-0.2.0-py3-none-any.whl

Total release size: 109.8 kB

Release files / dse_core-0.2.0.tar.gz

Download URL dse_core-0.2.0.tar.gz
Size 55.3 kB
Tags Source
SHA-256 checksum
How to use checksums
045182e277d941226fd7ddfa4e11dafeac7d8138421de32a6e8a63bdab5f2eea
BLAKE2b-256 checksum
How to use checksums
8b85a9fc9e0720aa050dcdc2ba1279d1d6d5beae9e258c7ef09db85dc93105c0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / dse_core-0.2.0-py3-none-any.whl

Download URL dse_core-0.2.0-py3-none-any.whl
Size 54.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cf5699885f236da365b4774fef3510a25d58e46df5645d41535935c3399cb5f8
BLAKE2b-256 checksum
How to use checksums
9de3f15eb86c6ab5c1bc57efdc687c8044ad7b26d351c836f9ffb0f3d2312442
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Security for dse-core 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page