Files
AI-Create-Dht-Chat/docs/files/run.cmd.md
T
MrhongxingandCopilot ec594286f3 文档:逐个源文件的说明 + 架构设计文档
- docs/files/:仓库中每个被跟踪文件一份说明,逐函数/逐常量解释「做什么」与「为什么这么做」
  - dhtchat/ 12 份模块文档(node、chat、gui、app、crypto、protocol、routing、store、identity、util、__main__、__init__)
  - tests/ 3 份测试文档(test_crypto 官方向量、test_dht 真实 UDP 集成、test_chat 聊天语义)
  - run.cmd、start.bat、.gitignore、根 README.md 各一份
- docs/ARCHITECTURE.md:架构设计
  - 六层架构与单向依赖、DHT 设计、键空间与 TTL 策略、报文与分片
  - 聊天室消息双通路与迟到者历史回放的端到端时序
  - 私聊三级投递(直连 → 中继 → 离线信箱)与 AAD 绑定
  - 安全模型(提供什么、明确不提供什么)、线程模型、局域网发现
  - 已知局限清单与扩展指南
- docs/README.md:文档总索引与「想了解 X 就看 Y」导航表
- 根 README.md 的「开发与测试」末尾补一段文档入口
- 26 张 Mermaid 图(分层、时序、流程),267 个相对链接全部校验通过

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-09-18 18:52:08 +08:00

114 lines
4.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# `run.cmd` — Windows 启动器
> 本文件的作用:在 Windows 上**找到可用的 Python 3**、检查 tkinter、然后把参数原样交给 `python -m dhtchat`。
> 它解决的问题是:Windows 上「Python 到底叫什么名字」和「tkinter 有没有装」这两件事在用户机器上都不确定。
- **路径**:`run.cmd`
- **行数**:40
- **类型**:`cmd.exe` 批处理脚本(`@echo off` + `setlocal`)
- **不涉及任何 Python 代码**
---
## 1. 逐段说明
### `@echo off` / `rem ...` 注释块
关掉命令回显;随后一段 `rem` 是给用户看的用法示例(`run.cmd`、`--nick`、`-b`、`--no-gui`)。
### `setlocal`
把环境变量改动**限制在本脚本进程内**:调用方(例如 `start.bat` 或用户的 cmd 窗口)的 `PATH`、`DHTCHAT_PY` 等不会被污染。
### `cd /d "%~dp0"`
切到脚本所在目录(`%~dp0` 是脚本目录,含结尾反斜杠;`/d` 允许跨盘符切换)。
**为什么需要**:双击运行时,当前目录可能是 `C:\Windows\System32` 或用户目录,那样 `python -m dhtchat` 会找不到模块。切到项目根目录后,`-m dhtchat` 才能解析到同级的 `dhtchat\` 包。
### Python 解释器探测(两段式)
```bat
set "DHTCHAT_PY="
where py >nul 2>nul && py -3 -c "import sys" >nul 2>nul && set "DHTCHAT_PY=py -3"
if not defined DHTCHAT_PY (
where python >nul 2>nul && python -c "import sys" >nul 2>nul && set "DHTCHAT_PY=python"
)
```
| 步骤 | 为什么这么做 |
| --- | --- |
| 先清空 `DHTCHAT_PY` | 避免继承用户环境里的同名变量造成误判 |
| 首选 `py -3` | Windows 官方安装器自带的 launcher,**一定指向真解释器**;也是本项目 README 推荐的名字 |
| `py -3 -c "import sys"` 真的执行一次 | `where` 只说明「有这个文件」,**不代表能跑**。很多 Windows 机器上 `python` 是 Microsoft Store 的 0 字节占位程序:`where` 能找到,一执行就退出。所以要实际 `import sys` 验证 |
| 退回 `python` | 有些环境(conda、部分安装包)没有 `py` launcher |
| `>nul 2>nul` | 把所有探测输出丢掉,保持启动界面干净 |
### Python 缺失时的提示
```bat
if not defined DHTCHAT_PY (
echo [错误] 没有找到可用的 Python 3。
...
exit /b 9009
)
```
给出来源链接与「安装时勾选 Add python.exe to PATH」的提示,然后以 **9009**(Windows 的「命令未找到」约定码)退出。`start.bat` 靠这个非零码决定是否 `pause`。
### tkinter 探测与自动降级
```bat
%DHTCHAT_PY% -c "import tkinter" >nul 2>nul
if errorlevel 1 (
echo [提示] 当前 Python 缺少 tkinter,将改用命令行界面。
%DHTCHAT_PY% -m dhtchat --no-gui %*
exit /b %errorlevel%
)
```
**为什么需要**:部分 Linux 风格的最小化安装(或用户手动裁剪)没有 tkinter。此时如果直接启动 GUI,[gui.py](../../dhtchat/gui.py) 会在 `import tkinter` 时抛 `ImportError` 并弹一段用户看不懂的回溯。这里提前探测,**自动降级为控制台模式**,并打印一句人话说明。
注意 `exit /b %errorlevel%` 传的是**聊天客户端的退出码**,不是 `import tkinter` 的。
### 正常路径
```bat
%DHTCHAT_PY% -m dhtchat %*
exit /b %errorlevel%
```
`%*` 把调用方传的全部参数原样透传(`--nick`、`--host`、`--port`、`-b`、`--no-gui` 等,见 [__main__.md](./dhtchat/__main__.md))。`exit /b` 而不是 `exit`,确保只结束批处理而不关掉用户的 cmd 窗口。
---
## 2. 与其他文件的关系
```mermaid
flowchart LR
A["start.bat<br/>双击入口"] -->|call| B["run.cmd<br/>找 Python + 检查 tkinter"]
B -->|有 tkinter| C["py -3 -m dhtchat"]
B -->|无 tkinter| D["py -3 -m dhtchat --no-gui"]
B -->|无 Python| E["echo 错误信息<br/>exit /b 9009"]
C --> F["dhtchat/__main__.py<br/>main()"]
D --> F
A -->|errorlevel != 0| G["pause<br/>让用户看到提示"]
```
- [start.bat](./start.bat.md) 是双击入口,只负责「调用 + 出错时暂停」。
- [.gitignore](./gitignore.md) 不参与运行。
- 参数含义见 [__main__.md](./dhtchat/__main__.md) 的 `build_parser` 表。
---
## 3. 手工验证
```bat
run.cmd --help
run.cmd --no-gui -b 127.0.0.1:47800
echo %errorlevel%
```
- 第一条应打印完整参数帮助(说明 Python 探测与透传正常)。
- 第二条应进入控制台客户端(说明 tkinter 分支与 `-m dhtchat` 都对)。
- 第三条在无 Python 的机器上应为 `9009`。