Files
ts2lua-unreal/README.md
T

59 lines
3.0 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.
# ts2lua-unreal
面向 **Unreal Engine 5.8 + UnLua 2.3.6(Lua 5.4)** 的 TypeScript 开发工具链与编译器插件,基于官方 [TypeScriptToLua (TSTL)](https://github.com/TypeScriptToLua/TypeScriptToLua) 插件机制实现(无需 Fork TSTL 源码)。
## 特性概览
- **原生 UnLua 代码生成**:通过全局免 `import` 编译期装饰器 `@unlua` 标记绑定类,直接输出 `local M = UnLua.Class(...)`、`function M.Method(self, ...)` 与 `return M`,零 `__TS__Class` / `__TS__Decorate` / `____exports` 垫片污染。
- **精确的 `super` 映射**:
- 继承 UE C++/蓝图原生基类 -> `self.Overridden.Method(self, ...)`
- 继承另一个 `@unlua` TS 绑定类 -> `BaseClass.Method(self, ...)`(规避多层继承 `self.Super` 递归坑)
- **编译期强约束(Fail Fast)**:在 TSTL AST 阶段直接阻断 `get/set` 访问器、`async/await`/`Promise`、`try/catch/throw`、`RegExp`/`Map`/`Set`/`Proxy`/`Symbol`、普通运行时 `enum`、`@unlua` 类 `constructor`/字段初始化器/`new` 实例化、UE Delegate 匿名函数回调、`UObject` 的 `instanceof`/`delete`/对象展开以及继承 `UE.UObject` 漏标 `@unlua` 等问题。
- **面向大型项目(3000~5000+ 文件)优化**:插件预编译为 JS、复用 TSTL 单次 AST 遍历、`WeakMap` 缓存类型查询、`skipLibCheck: true` 与增量 Watch 模式支持。
## 目录结构
```text
ts2lua-unreal/
├── docs/
│ └── ts-unlua-spec.md # TypeScript -> UnLua 开发与语言约束规范
├── types/
│ └── unlua.d.ts # 全局 @unlua 装饰器与 UnLua/UE 基础类型声明
├── src/plugin/ # TSTL UnLua 编译器插件源码
│ ├── index.ts # 插件入口
│ ├── diagnostics.ts # 自定义编译错误码(90001-90110)
│ ├── type-utils.ts # WeakMap 缓存的 TypeChecker 工具
│ ├── validators.ts # 语言子集与 UnLua 约束校验
│ └── unlua-transform.ts # UnLua.Class / return M / super / default import 转换
├── examples/ # 对齐 UnrealLuaDemo 的端到端示例
│ ├── tsconfig.json
│ ├── types/demo.d.ts
│ └── src/
│ ├── Framework/ # 纯工具模块(Log / EventBus)
│ └── Game/ # UnLua 绑定类(LuaDemoActor / LuaSpecialDemoActor)
├── test/
│ └── plugin.test.mjs # Node 原生自动化测试套件
├── tsconfig.base.json # 业务项目可继承的 TSTL 基础配置(Lua 5.4)
└── tsconfig.plugin.json # 插件构建配置
```
## 快速开始
```bash
# 安装依赖
npm install
# 构建插件并运行全部自动化测试
npm test
# 编译示例工程(输出至 examples/out/)
npm run build:examples
# 增量监听模式(适配 UnLua HotReload)
npm run watch:examples
```
## 文档
- [TypeScript to UnLua 开发与语言约束规范](docs/ts-unlua-spec.md)