15.3 下一步:读源码与自研
读完一卷书不等于能改一个编辑器。源码研读清单按难度升序给出入门顺序,并合并所有仓库的关注点做成关键词表——按关键词反查仓库,比按名字猜该读哪个可靠得多。
本章与站内既有内容的分工
| 主题 | 站内既有内容 | 本节做什么 |
|---|---|---|
| 路线图 | 本卷 15.2 | 15.2 排里程碑;本节排学习顺序 |
| 引擎实现 | 《游戏引擎实现原理》 | 引擎侧读运行时;本节读编辑器与工具链 |
| 全卷脉络 | 本卷目录 | 本节收口全系列 |
从零实现:下一步:读源码与自研
研读路径先按难度上限过滤,再按难度升序排序,返回副本以隔离内部数组。关注点合并保持首次出现顺序,让同一输入得到稳定结果。关键字搜索不区分大小写,只匹配关注点,不匹配仓库名——名字往往不能说明它解决什么问题。
关注点索引按输入的首次出现顺序去重,因此输入顺序变化时结果也可能变化。检索后先固定源码版本,选一条用户操作,从入口沿调用链追踪到数据变更、撤销与刷新,再用小实验复现。
flowchart TD
A["选择操作与源码版本"] --> B["按关注点检索候选项目"]
B --> C["追踪入口、数据变更与通知"]
C --> D["用独立小例验证理解"]
对标 Unity / Unreal / Godot / Cocos:源码研读清单
| 引擎 | 机制 | 取舍 |
|---|---|---|
| Unity | C# 参考与编辑器源码 | 部分编辑器源码开放 |
| Unreal | 完整引擎源码 | 编辑器与运行时同仓 |
| Godot | 完整开源 | 编辑器即引擎,适合通读 |
| Cocos Creator | 编辑器部分开源 | 扩展与运行时接口可读 |
| helio(本节) | studyPath + searchByFocus | 难度排序 + 关注点索引 |
可运行产物:6 条用例
下面是 src/source-study.ts,层级 L1,完整可编译。运行命令 node --test test/15-3-next-steps-source-study.test.ts:
// file: src/source-study.ts
// tier: L1
// verify: node --test test/15-3-next-steps-source-study.test.ts
//
// 卷四(第十五章):下一步——读源码与自研。
// 读完一卷书不等于能改一个编辑器。源码研读清单按难度升序给出入门顺序,
// 并合并所有仓库的关注点做成关键词表——按关键词反查仓库,比按名字猜
// 「该读哪个」可靠得多。
export interface RepoRef {
name: string;
focus: string[];
difficulty: 1 | 2 | 3 | 4 | 5;
}
export function studyPath(repos: RepoRef[], maxDifficulty = 5): RepoRef[] {
return repos
.filter((repo) => repo.difficulty <= maxDifficulty)
.sort((a, b) => a.difficulty - b.difficulty)
.map((repo) => ({ ...repo, focus: [...repo.focus] }));
}
export function collectFocus(repos: RepoRef[]): string[] {
const seen = new Set<string>();
const out: string[] = [];
for (const repo of repos) {
for (const focus of repo.focus) {
if (!seen.has(focus)) {
seen.add(focus);
out.push(focus);
}
}
}
return out;
}
export function searchByFocus(repos: RepoRef[], keyword: string): RepoRef[] {
const wanted = keyword.toLowerCase();
return repos.filter((repo) =>
repo.focus.some((focus) => focus.toLowerCase().includes(wanted)),
);
}
按难度排序而不是按名气排序:从 Dear ImGui 这类小而完整的项目入手,比直接啃大型商业引擎更容易建立「编辑器长什么样」的直觉。关注点索引的价值在于反查——想找「依赖图」怎么实现,就搜 depsgraph,而不是猜哪个仓库有。
下面是 test/15-3-next-steps-source-study.test.ts,层级 L1:
// file: test/15-3-next-steps-source-study.test.ts
// tier: L1
// verify: node --test test/15-3-next-steps-source-study.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { studyPath, collectFocus, searchByFocus } from '../src/source-study.ts';
const repos = [
{ name: 'godot', focus: ['scene', 'editor'], difficulty: 3 as const },
{ name: 'imgui', focus: ['ui', 'dear-imgui'], difficulty: 2 as const },
{ name: 'blender', focus: ['depsgraph', 'editor'], difficulty: 5 as const },
];
test('按难度升序给出研读顺序', () => {
assert.deepEqual(studyPath(repos).map((r) => r.name), ['imgui', 'godot', 'blender']);
});
test('难度上限过滤', () => {
assert.deepEqual(studyPath(repos, 3).map((r) => r.name), ['imgui', 'godot']);
});
test('关注点合并去重且保持首次出现顺序', () => {
assert.deepEqual(collectFocus(repos), ['scene', 'editor', 'ui', 'dear-imgui', 'depsgraph']);
});
test('空清单返回空结果', () => {
assert.deepEqual(studyPath([]), []);
assert.deepEqual(collectFocus([]), []);
assert.deepEqual(searchByFocus([], 'ui'), []);
});
test('按关注点关键字反查仓库', () => {
assert.deepEqual(searchByFocus(repos, 'editor').map((r) => r.name), ['godot', 'blender']);
assert.deepEqual(searchByFocus(repos, 'UI').map((r) => r.name), ['imgui']);
});
test('结果不共享内部数组', () => {
const path = studyPath(repos);
path[0].focus.push('mutated');
assert.deepEqual(repos[1].focus, ['ui', 'dear-imgui']);
});
本机实测(Node v26.5.0,node --test test/15-3-next-steps-source-study.test.ts):tests 6 · pass 6 · fail 0,耗时约 40 ms。
前两条钉难度排序与过滤;第三条钉关注点去重;第四条钉空清单;后两条钉关键字反查与副本隔离。
小结
- 研读顺序按难度而不是名气排列。
- 关注点索引支持按问题反查仓库,而不是按名字猜。
- 过滤与排序返回副本,不修改输入。
- 从小而完整的项目入手,先建立编辑器直觉。
阅读导航:上一节:15.2 从 helio 到工业级编辑器的路线图 · 下一节:《扩展、调试与规模化》卷目录 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。