release: v0.1.0 package exports, CLI, integration guide, and AI rules template
This commit is contained in:
1 parent
c0ac6eaad6
commit
1f76bca55c
8 files changed
+500
-33
No files matched your search
@@ -0,0 +1,26 @@
|
||||
# Changelog
|
||||
|
||||
All notable changes to `ts2lua-unreal` will be documented in this file.
|
||||
|
||||
The format is based on [Semantic Versioning](https://semver.org/).
|
||||
|
||||
## [0.1.0] - 2026-11-29
|
||||
|
||||
### Added
|
||||
- **规范与接入文档**:
|
||||
- [docs/ts-unlua-spec.md](docs/ts-unlua-spec.md):面向 UE 5.8 + UnLua 2.3.6(Lua 5.4)的 TypeScript 开发与语言约束规范。
|
||||
- [docs/integration-guide.md](docs/integration-guide.md):UE 项目接入、错误码速查、版本升级与本地 `npm link` 联调手册。
|
||||
- [docs/ai-rules-template.md](docs/ai-rules-template.md):可直接复用的业务项目规则模板。
|
||||
- **UnLua TSTL 编译器插件 (`src/plugin/`)**:
|
||||
- 编译期拦截全部禁用语言特性(`90001`~`90016`)与 `@unlua` 绑定类专项约束(`90101`~`90110`,含继承 `UE.UObject` 漏标 `@unlua` 防呆)。
|
||||
- `@unlua` 类原生 UnLua 代码生成:`local ClassName = UnLua.Class(...)`、`function ClassName.Method(self, ...)`、`return ClassName`,消除 `____exports` / `__TS__Class` / `__TS__Decorate` 垫片。
|
||||
- 精确 `super.Method(...)` 映射(UE 原生基类 -> `self.Overridden.Method(self, ...)`;`@unlua` TS 父类 -> `BaseClass.Method(self, ...)`)。
|
||||
- `@unlua` 默认导入直连优化(`import Base from "./Base"` -> `local Base = require("...")`,不生成 `.default`)。
|
||||
- 面向大项目(3000~5000+ 文件)的 `WeakMap` 类型与文件名缓存优化。
|
||||
- **包导出与 CLI**:
|
||||
- 暴露 `ts2lua-unreal`、`ts2lua-unreal/tsconfig.base.json`、`ts2lua-unreal/types/unlua.d.ts` 及全部文档导出。
|
||||
- 提供 `ts2lua-unreal build` / `watch` / `info` CLI 命令与 `prepare` 自动构建脚本,支持通过 npm Git Tag 依赖无缝接入任意 UE 项目。
|
||||
- **基础类型、示例与测试**:
|
||||
- [types/unlua.d.ts](types/unlua.d.ts):全局 `@unlua` 装饰器及 UnLua/UE 核心基础类型声明。
|
||||
- `examples/`:还原 `UnrealLuaDemo` 的端到端 TS -> UnLua 示例工程。
|
||||
- `test/plugin.test.mjs`:16 组正反用例自动化测试套件。
|
||||
@@ -11,33 +11,50 @@
|
||||
- **编译期强约束(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 模式支持。
|
||||
|
||||
## 目录结构
|
||||
## 在 UE 项目中接入(快速预览)
|
||||
|
||||
```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 # 插件构建配置
|
||||
在 UE 项目的 `TypeScript/package.json` 中通过 Git Tag 依赖接入:
|
||||
|
||||
```json
|
||||
{
|
||||
"scripts": {
|
||||
"build": "ts2lua-unreal build",
|
||||
"watch": "ts2lua-unreal watch",
|
||||
"info": "ts2lua-unreal info"
|
||||
},
|
||||
"devDependencies": {
|
||||
"ts2lua-unreal": "git+ssh://git@106.55.3.105:20022/szy73711/ts2lua-unreal.git#v0.1.0"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 快速开始
|
||||
`TypeScript/tsconfig.json` 直接继承内置基线配置:
|
||||
|
||||
```json
|
||||
{
|
||||
"extends": "ts2lua-unreal/tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"rootDir": "src",
|
||||
"outDir": "../Content/Script"
|
||||
},
|
||||
"include": [
|
||||
"src/**/*.ts",
|
||||
"types/**/*.d.ts",
|
||||
"node_modules/ts2lua-unreal/types/unlua.d.ts"
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
> 完整的接入步骤、错误码速查表、版本升级与本地 `npm link` 联调套路,请查阅 **[UE 项目接入与迭代升级指南](docs/integration-guide.md)**。
|
||||
|
||||
## 文档索引
|
||||
|
||||
- [UE 项目接入与迭代升级指南](docs/integration-guide.md)(首次接入、错误码速查、升级/回滚与 `npm link` 联调)
|
||||
- [TypeScript to UnLua 开发与语言约束规范](docs/ts-unlua-spec.md)(完整语言子集约束与原理说明)
|
||||
- [业务项目开发规则模板](docs/ai-rules-template.md)(可直接复制到 UE 项目的 `AGENTS.md` / 团队规范)
|
||||
- [版本变更日志](CHANGELOG.md)
|
||||
|
||||
## 本仓库开发命令
|
||||
|
||||
```bash
|
||||
# 安装依赖
|
||||
@@ -49,10 +66,6 @@ npm test
|
||||
# 编译示例工程(输出至 examples/out/)
|
||||
npm run build:examples
|
||||
|
||||
# 增量监听模式(适配 UnLua HotReload)
|
||||
# 增量监听模式
|
||||
npm run watch:examples
|
||||
```
|
||||
|
||||
## 文档
|
||||
|
||||
- [TypeScript to UnLua 开发与语言约束规范](docs/ts-unlua-spec.md)
|
||||
Executable
+77
@@ -0,0 +1,77 @@
|
||||
#!/usr/bin/env node
|
||||
"use strict";
|
||||
|
||||
const path = require("node:path");
|
||||
const { spawnSync } = require("node:child_process");
|
||||
|
||||
const pkgRoot = path.resolve(__dirname, "..");
|
||||
const pkg = require(path.join(pkgRoot, "package.json"));
|
||||
|
||||
function printHelp() {
|
||||
console.log(`ts2lua-unreal v${pkg.version} - TypeScript to UnLua compiler toolchain
|
||||
|
||||
Usage:
|
||||
ts2lua-unreal build [tstl args...] Compile TS project to UnLua Lua (default: -p tsconfig.json)
|
||||
ts2lua-unreal watch [tstl args...] Watch and incrementally compile for UnLua HotReload
|
||||
ts2lua-unreal info Show installed version and local documentation paths
|
||||
ts2lua-unreal --help Show this help message
|
||||
`);
|
||||
}
|
||||
|
||||
function printInfo() {
|
||||
console.log(
|
||||
JSON.stringify(
|
||||
{
|
||||
name: pkg.name,
|
||||
version: pkg.version,
|
||||
packageRoot: pkgRoot,
|
||||
tsconfigBase: path.join(pkgRoot, "tsconfig.base.json"),
|
||||
unluaTypes: path.join(pkgRoot, "types", "unlua.d.ts"),
|
||||
docs: {
|
||||
integrationGuide: path.join(pkgRoot, "docs", "integration-guide.md"),
|
||||
spec: path.join(pkgRoot, "docs", "ts-unlua-spec.md"),
|
||||
aiRulesTemplate: path.join(pkgRoot, "docs", "ai-rules-template.md"),
|
||||
changelog: path.join(pkgRoot, "CHANGELOG.md"),
|
||||
},
|
||||
},
|
||||
null,
|
||||
2
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
function runTstl(extraArgs) {
|
||||
const tstlBin = require.resolve("typescript-to-lua/dist/tstl.js");
|
||||
const hasProjectFlag = extraArgs.some((arg) => arg === "-p" || arg === "--project");
|
||||
const args = hasProjectFlag ? extraArgs : ["-p", "tsconfig.json", ...extraArgs];
|
||||
const result = spawnSync(process.execPath, [tstlBin, ...args], {
|
||||
stdio: "inherit",
|
||||
});
|
||||
process.exit(result.status ?? 1);
|
||||
}
|
||||
|
||||
const [, , cmd, ...rest] = process.argv;
|
||||
|
||||
if (!cmd || cmd === "-h" || cmd === "--help" || cmd === "help") {
|
||||
printHelp();
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
if (cmd === "-v" || cmd === "--version") {
|
||||
console.log(pkg.version);
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
if (cmd === "info") {
|
||||
printInfo();
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
if (cmd === "build") {
|
||||
runTstl(rest);
|
||||
} else if (cmd === "watch") {
|
||||
runTstl(["--watch", ...rest]);
|
||||
} else {
|
||||
// 直接透传给 tstl
|
||||
runTstl([cmd, ...rest]);
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
# ts2lua-unreal 业务项目开发规则模板
|
||||
|
||||
> 接入 `ts2lua-unreal` 的 UE 项目可将本文档内容复制或引用至项目的 `AGENTS.md` / 团队开发规范 / IDE AI Rules 中,确保开发者与 AI 助手遵循统一约束。
|
||||
> 完整规范见 `node_modules/ts2lua-unreal/docs/ts-unlua-spec.md`,接入与升级流程见 `node_modules/ts2lua-unreal/docs/integration-guide.md`。
|
||||
|
||||
## 1. 工具链与编译约定
|
||||
- TypeScript 源码位于 `TypeScript/src/`,编译产物输出至 `Content/Script/`(UnLua 直接加载)。
|
||||
- 目标 Lua 版本为 **Lua 5.4**,编译器为 `ts2lua-unreal`(基于 TypeScriptToLua + UnLua 定制插件)。
|
||||
- 常用命令(在 `TypeScript/` 目录下执行):
|
||||
- 全量编译:`npm run build`(即 `ts2lua-unreal build`)
|
||||
- 增量监听(配合 UnLua HotReload):`npm run watch`(即 `ts2lua-unreal watch`)
|
||||
- 查看当前工具链版本与内置文档路径:`npm run info`(即 `ts2lua-unreal info`)
|
||||
- 升级 `ts2lua-unreal` 时,在 `TypeScript/package.json` 中更新 Git Tag(`#vX.Y.Z`)后执行 `npm install && npm run build`,并提交 `package.json` 与 `package-lock.json`。
|
||||
|
||||
## 2. UnLua 绑定类(UObject 派生类)强制规则
|
||||
1. **必须加全局装饰器 `@unlua`**(无需 `import`),并使用 `export default class Xxx extends ...` 导出。
|
||||
2. **单文件单类**:`@unlua` 文件禁止额外 `export` 其他运行时变量/函数/类(允许 `export type` / `export interface`)。
|
||||
3. **禁止 `constructor` 与实例字段初始化器**(含箭头函数类属性 `OnClick = () => {}`):
|
||||
- 实例字段只写类型声明(如 `private hp!: number;`);
|
||||
- 状态初始化统一放在 `Initialize` / `Construct` / `ReceiveBeginPlay` 中。
|
||||
4. **禁止 `new MyUnLuaActor()`**:必须通过 UE API(`SpawnActor` / `UE.NewObject` / `CreateWidget`)创建。
|
||||
5. **`super` 仅允许 `super.Method(...)`**:
|
||||
- 继承 UE 原生基类自动编译为 `self.Overridden.Method(self, ...)`;
|
||||
- 继承另一个 `@unlua` TS 类自动编译为 `BaseClass.Method(self, ...)`;
|
||||
- 访问父类属性/组件直接用 `this.prop`,禁止 `super.prop`。
|
||||
6. **UE Delegate 绑定禁止匿名函数/箭头函数**:必须传入类成员方法引用 `this.OnXxx`,并在 `ReceiveEndPlay` / `Destruct` 中成对 `Remove` 或 `Clear`。
|
||||
|
||||
## 3. 通用 TypeScript 语言子集强制约束
|
||||
以下特性会被 `ts2lua-unreal` 在编译期直接报错阻断:
|
||||
- **禁用 `get` / `set` 访问器** -> 改用 `GetXxx()` / `SetXxx(v)`。
|
||||
- **禁用 `async` / `await` / `Promise`** -> 改用 Lua `coroutine` 封装或回调 + `IsValid()` 检查。
|
||||
- **禁用业务代码 `try` / `catch` / `finally` / `throw`** -> 改用错误码/防御性判空(仅框架顶层边界可加 `/** @unlua-allow-try */`)。
|
||||
- **禁用非白名单装饰器** -> 仅允许 `@unlua`。
|
||||
- **禁用 `RegExp`(含 `/abc/`)、`Map`、`Set`、`WeakMap`、`WeakSet`、`Proxy`、`Reflect`、`Symbol`、`BigInt`、`Object.defineProperty/create/freeze/setPrototypeOf`、`eval`、`with`、`arguments`、`function*`/`yield`、动态 `import()`** -> 字典/集合用 `Record<K, V>` / `LuaTable` / `UE.TMap` / `UE.TSet`,正则用 Lua Pattern(`string.match`/`string.gsub`)。
|
||||
- **禁用普通运行时 `enum`** -> 改用 `const enum`、字符串联合类型或 `declare enum`。
|
||||
- **禁用 `for ... in`** -> 遍历 Lua 表用 `for (const [k, v] of pairs(tbl))`。
|
||||
- **禁用对数组或 `UObject` 使用 `delete`** -> 数组删除用 `table.remove` / `splice`。
|
||||
- **禁用对 `UObject` 使用 `instanceof` 或对象展开 `{ ...obj }`** -> 类型判断用 `obj.IsA(UE.ClassName.StaticClass())`。
|
||||
|
||||
## 4. 纯逻辑模块、容器与性能约定
|
||||
- 纯函数工具模块文件顶部加 `/** @noSelfInFile */`,编译为点号调用(如 `Log.Info(...)`)。
|
||||
- 严格区分 TS 数组 `T[]`(0-based,TSTL 自动转 1-based)与 `UE.TArray`(UnLua `Get(i)`/`Set(i, v)` 为 1-based)。
|
||||
- 跨帧持有的 `UObject` 使用前必须通过 `UE.UKismetSystemLibrary.IsValid(obj)` 判空。
|
||||
- `ReceiveTick` 等每帧热路径禁止 `.map().filter()` 链式分配与高频临时 `FVector`/`FRotator` 分配。
|
||||
- 热重载友好:禁止在模块顶层缓存其他模块的方法引用或 `UObject` 实例引用。
|
||||
@@ -0,0 +1,281 @@
|
||||
# Unreal Engine + UnLua 项目接入与迭代升级指南
|
||||
|
||||
> 本指南面向所有接入 `ts2lua-unreal` 的 Unreal Engine(UnLua)项目,完整说明 **项目初始化接入、目录结构、编码约束、错误码排查、版本升级与本地联调套路**。
|
||||
>
|
||||
> 安装本包后,你也可以随时运行 `npx ts2lua-unreal info` 查看当前安装版本及本地文档路径。
|
||||
|
||||
---
|
||||
|
||||
## 1. 环境前提
|
||||
|
||||
- **Node.js**:推荐 Node 20+(已验证 Node 24)
|
||||
- **Git SSH 权限**:能拉取远程仓库 `ssh://git@106.55.3.105:20022/szy73711/ts2lua-unreal.git`
|
||||
- **Unreal Engine / UnLua 基线**:
|
||||
- UnLua 2.x,内置 **Lua 5.4**(`Config/DefaultUnLuaEditor.ini` 中 `LuaVersion=lua-5.4.3` 或 `5.4.x`)
|
||||
- 推荐开启 `bEnableCallOverriddenFunction=True`、`bWithUE4Namespace=True`、`bLegacyReturnOrder=False`
|
||||
|
||||
---
|
||||
|
||||
## 2. UE 项目首次接入(标准目录与配置)
|
||||
|
||||
### 2.1 推荐目录结构
|
||||
在 UE 项目根目录下新建独立的 `TypeScript/` 目录,不污染 UE 工程根目录,编译产物直接输出到 UnLua 读取的 `Content/Script/`:
|
||||
|
||||
```text
|
||||
YourUEProject/
|
||||
├── Config/
|
||||
├── Content/
|
||||
│ └── Script/ # TSTL 编译输出目录(UnLua 直接加载这里)
|
||||
│ ├── Framework/
|
||||
│ └── Game/
|
||||
├── Plugins/UnLua/
|
||||
├── Source/
|
||||
└── TypeScript/ # 新增:TS 开发工程根目录
|
||||
├── package.json # 声明对 ts2lua-unreal 的 Git Tag 依赖
|
||||
├── package-lock.json # 锁定精确依赖与 commit,务必提交 Git
|
||||
├── tsconfig.json # 继承 ts2lua-unreal/tsconfig.base.json
|
||||
├── types/ # 本项目 UE C++/蓝图类型声明 (.d.ts)
|
||||
│ └── project.d.ts
|
||||
└── src/ # 业务 TS 源码(目录结构镜像 Content/Script)
|
||||
├── Framework/
|
||||
└── Game/
|
||||
```
|
||||
|
||||
### 2.2 创建 `TypeScript/package.json`
|
||||
|
||||
使用 **方案 1(npm Git 依赖 + Tag 锁定)** 接入:
|
||||
|
||||
```json
|
||||
{
|
||||
"name": "your-ue-project-ts",
|
||||
"version": "1.0.0",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"build": "ts2lua-unreal build",
|
||||
"watch": "ts2lua-unreal watch",
|
||||
"info": "ts2lua-unreal info"
|
||||
},
|
||||
"devDependencies": {
|
||||
"ts2lua-unreal": "git+ssh://git@106.55.3.105:20022/szy73711/ts2lua-unreal.git#v0.1.0"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 2.3 创建 `TypeScript/tsconfig.json`
|
||||
|
||||
直接继承 `ts2lua-unreal/tsconfig.base.json`(已内置 `luaTarget: "5.4"`、`skipLibCheck: true`、`luaLibImport: "require-minimal"` 以及 `ts2lua-unreal` 插件,无需手写插件路径):
|
||||
|
||||
```json
|
||||
{
|
||||
"extends": "ts2lua-unreal/tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"rootDir": "src",
|
||||
"outDir": "../Content/Script"
|
||||
},
|
||||
"include": [
|
||||
"src/**/*.ts",
|
||||
"types/**/*.d.ts",
|
||||
"node_modules/ts2lua-unreal/types/unlua.d.ts"
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
### 2.4 安装与首次编译
|
||||
|
||||
```bash
|
||||
cd YourUEProject/TypeScript
|
||||
npm install
|
||||
npm run build
|
||||
```
|
||||
|
||||
开发期配合 UnLua HotReload 使用增量监听模式(修改保存后百毫秒级更新 `Content/Script/*.lua`):
|
||||
|
||||
```bash
|
||||
npm run watch
|
||||
```
|
||||
|
||||
### 2.5 `.gitignore` 策略约定
|
||||
- **务必忽略**:`TypeScript/node_modules/`
|
||||
- **务必提交**:`TypeScript/package.json`、`TypeScript/package-lock.json`、`TypeScript/tsconfig.json`、`TypeScript/src/`、`TypeScript/types/`
|
||||
- **关于 `Content/Script/*.lua` 是否提交 Git**:
|
||||
- **若美术/策划/UE 打包机不安装 Node.js(推荐 UE 团队默认)**:保留生成的 `Content/Script/*.lua` 并随 TS 源码一起提交,确保无 Node 环境的机器可直接打开编辑器或打包;
|
||||
- **若全员及 CI 均已安装 Node.js**:可将生成的 `.lua` 加入 `.gitignore`,并在 UE 构建前执行 `npm --prefix TypeScript run build`。
|
||||
- **注意**:如果项目中存在少量尚未迁移的**手写 `.lua` 文件**,不要与 `TypeScript/src/` 下文件重名,否则 `npm run build` 会覆盖同名 `.lua`。
|
||||
|
||||
---
|
||||
|
||||
## 3. 核心编码约定速查(必读)
|
||||
|
||||
详细设计原理请参阅同目录下的 [ts-unlua-spec.md](./ts-unlua-spec.md)。接入项目开发时必须遵循以下核心规则:
|
||||
|
||||
### 3.1 UnLua 绑定类(`@unlua`)模板
|
||||
- 使用全局免 `import` 装饰器 `@unlua`(编译期完全擦除,无运行时代码)。
|
||||
- **必须且只能** `export default class Xxx ...`,同文件禁止其他运行时 `export`(允许 `export type` / `export interface`)。
|
||||
- **禁止** `constructor`、实例字段初始化器(含箭头函数方法)、`new MyActor()`。
|
||||
- `super.Method(...)` 会按父类自动精确转译:
|
||||
- 继承 UE 原生基类(`UE.AActor` 等)-> `self.Overridden.Method(self, ...)`
|
||||
- 继承另一个 `@unlua` TS 类 -> `BaseClass.Method(self, ...)`
|
||||
|
||||
```ts
|
||||
// TypeScript/src/Game/MyActor.ts
|
||||
import * as Log from "../Framework/Log";
|
||||
|
||||
@unlua
|
||||
export default class MyActor extends UE.AActor {
|
||||
// 仅做类型声明,禁止写 = 初始值
|
||||
private hp!: number;
|
||||
|
||||
ReceiveBeginPlay(): void {
|
||||
super.ReceiveBeginPlay(); // -> self.Overridden.ReceiveBeginPlay(self)
|
||||
this.hp = 100;
|
||||
Log.Info("MyActor ready");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 3.2 继承另一个 `@unlua` 绑定类
|
||||
```ts
|
||||
// TypeScript/src/Game/MySubActor.ts
|
||||
import MyActor from "./MyActor"; // -> local MyActor = require("Game.MyActor")(无 .default)
|
||||
|
||||
@unlua
|
||||
export default class MySubActor extends MyActor {
|
||||
ReceiveBeginPlay(): void {
|
||||
super.ReceiveBeginPlay(); // -> MyActor.ReceiveBeginPlay(self)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 3.3 尚无 `.d.ts` 的纯蓝图绑定类
|
||||
```ts
|
||||
@unlua // 或 @unlua("UI.WBP_Base") 显式指定 Lua 父类模块名
|
||||
export default class WBP_TempPopup {
|
||||
Construct(): void {
|
||||
// 正常输出 local WBP_TempPopup = UnLua.Class() ... return WBP_TempPopup
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 3.4 纯逻辑工具模块(非 `UObject`)
|
||||
文件顶部加 `/** @noSelfInFile */`,编译为点号调用模块(如 `Log.Info(...)`、`EventBus.On(...)`):
|
||||
|
||||
```ts
|
||||
/** @noSelfInFile */
|
||||
|
||||
export function Info(fmt: string, ...args: unknown[]): void {
|
||||
const msg = args.length > 0 ? string.format(fmt, ...args) : fmt;
|
||||
print("[INFO] " + msg);
|
||||
}
|
||||
```
|
||||
|
||||
### 3.5 UE 静态函数 / 结构体 / 容器 / Delegate 约定
|
||||
- **UE 静态函数**:在 `.d.ts` 中标注 `/** @noSelf */` 或 `this: void`,保证点号调用(如 `UE.UKismetSystemLibrary.PrintString(this, ...)`)。
|
||||
- **UE 结构体构造**:使用函数调用形式(`UE.FVector(x, y, z)`、`UE.FRotator(p, y, r)`、`UE.FLinearColor(r, g, b, a)`),热路径(`ReceiveTick`)尽量复用成员变量避免高频分配。
|
||||
- **索引基准**:TS `T[]` 是 `0-based`(TSTL 自动 `+1`),`UE.TArray` 在 UnLua 中 `Get(i)`/`Set(i, v)` 是 `1-based`,切勿混用。
|
||||
- **Delegate 绑定**:必须传类成员方法引用并成对解绑,禁止匿名函数/箭头函数:
|
||||
```ts
|
||||
this.BtnOk.OnClicked.Add(this, this.OnOkClicked);
|
||||
// 销毁时
|
||||
this.BtnOk.OnClicked.Remove(this, this.OnOkClicked);
|
||||
```
|
||||
- **UObject 判空与类型判断**:跨帧持有对象必须用 `UE.UKismetSystemLibrary.IsValid(obj)` 判空;类型判断使用 `obj.IsA(UE.AActor.StaticClass())`(禁用 `instanceof`)。
|
||||
|
||||
---
|
||||
|
||||
## 4. 编译器错误码速查表(`ts2lua-unlua`)
|
||||
|
||||
| 错误码 | 名称 | 触发原因 | 正确替代方案 |
|
||||
| :--- | :--- | :--- | :--- |
|
||||
| `90001` | `ForbiddenAccessor` | 使用了 `get` / `set` 属性访问器 | 改为显式 `GetXxx()` / `SetXxx(v)` 方法 |
|
||||
| `90002` | `ForbiddenAsyncAwait` | 使用了 `async` 函数或 `await` | 改用 Lua `coroutine` 封装或回调 + `IsValid()` |
|
||||
| `90003` | `ForbiddenPromise` | 使用了 `new Promise` 或 `Promise.*` | 同上 |
|
||||
| `90004` | `ForbiddenTryCatchThrow` | 业务代码使用了 `try/catch/finally` 或 `throw` | 改用错误码/防御性返回;框架顶层边界可加 `/** @unlua-allow-try */` |
|
||||
| `90005` | `ForbiddenDecorator` | 使用了非白名单装饰器 | 仅允许编译期白名单装饰器 `@unlua` |
|
||||
| `90006` | `ForbiddenBuiltinType` | 使用了 `Map`/`Set`/`WeakMap`/`WeakSet`/`Proxy`/`Symbol`/`BigInt`/`Reflect`/`Object.defineProperty` 等 | 改用 `Record<K, V>` / `LuaTable` / `UE.TMap` / `UE.TSet` |
|
||||
| `90007` | `ForbiddenRegExp` | 使用了 `/regex/` 或 `RegExp` | 改用 Lua Pattern(`string.match` / `string.gsub` / `string.gmatch`) |
|
||||
| `90008` | `ForbiddenGenerator` | 使用了 `function*` / `yield` | 改用 Lua `coroutine` |
|
||||
| `90009` | `ForbiddenForIn` | 使用了 `for (const k in obj)` | 遍历 Lua 表用 `for (const [k, v] of pairs(tbl))`;数组用 `for` 或 `for..of` |
|
||||
| `90010` | `ForbiddenRuntimeEnum` | 定义了普通运行时 `enum` | 改用 `const enum`、字符串联合类型或 `declare enum` |
|
||||
| `90011` | `ForbiddenDeleteOnArrayOrUObject` | 对数组或 `UObject` 使用了 `delete` | 数组删除用 `table.remove` / `splice` |
|
||||
| `90012` | `ForbiddenInstanceOfUObject` | 对 `UObject` 使用了 `instanceof` | 改用 `obj.IsA(UE.ClassName.StaticClass())` |
|
||||
| `90013` | `ForbiddenDynamicEvalOrWith` | 使用了 `eval()` 或 `with` | 移除动态代码执行 |
|
||||
| `90014` | `ForbiddenArguments` | 使用了 `arguments` | 改用显式 rest 参数 `...args` |
|
||||
| `90015` | `ForbiddenDynamicImport` | 使用了动态 `import(...)` | 改用静态 `import` |
|
||||
| `90016` | `ForbiddenUObjectSpread` | 对 `UObject` 使用了 `{ ...actor }` | 显式读取所需属性 |
|
||||
| `90101` | `UnLuaClassMustBeDefaultExport` | `@unlua` 类未使用 `export default class` | 改为 `export default class Xxx` |
|
||||
| `90102` | `UnLuaFileExtraRuntimeExport` | `@unlua` 文件导出了额外运行时符号 | 移至独立模块,或改为非导出本地定义(允许 `export type`/`interface`) |
|
||||
| `90103` | `UnLuaClassForbiddenConstructor` | `@unlua` 类定义了 `constructor` | 移入 `Initialize` / `Construct` / `ReceiveBeginPlay` |
|
||||
| `90104` | `UnLuaClassForbiddenPropertyInitializer` | `@unlua` 类写了实例字段初始化器或箭头函数方法 | 字段改 `prop!: Type;` 在生命周期内初始化;方法用标准方法声明 |
|
||||
| `90105` | `UnLuaClassForbiddenNew` | 对 `@unlua` 类使用了 `new` | 改用 `SpawnActor` / `UE.NewObject` / `CreateWidget` |
|
||||
| `90106` | `MissingUnLuaDecorator` | 继承 `UE.UObject` 但漏标 `@unlua` | 在类上补充 `@unlua` 与 `export default` |
|
||||
| `90107` | `UnLuaDelegateForbiddenInlineFunction` | Delegate `.Add`/`.Bind` 传入了箭头/匿名函数 | 传入类成员方法引用 `this.OnXxx` |
|
||||
| `90108` | `InvalidUnLuaDecoratorArgs` | `@unlua(...)` 传参非法 | 仅支持 `@unlua` 或 `@unlua("Path.To.Base")` |
|
||||
| `90109` | `UnLuaMultipleClassesInFile` | 单文件包含多个 `@unlua` 类 | 拆分为单文件单类 |
|
||||
| `90110` | `UnLuaForbiddenSuperUsage` | `@unlua` 类中调用 `super()` 或直接访问 `super.prop` | 仅支持 `super.Method(...)`;父类属性/组件直接用 `this.prop` |
|
||||
|
||||
---
|
||||
|
||||
## 5. 持续迭代、升级与本地联调套路
|
||||
|
||||
### 5.1 常规版本升级(生产/团队标准流程)
|
||||
当 `ts2lua-unreal` 发布了新版本 Tag(例如 `v0.2.0`):
|
||||
|
||||
1. 查看 `node_modules/ts2lua-unreal/CHANGELOG.md`(或远端仓库 `CHANGELOG.md`)确认变更与破坏性更新说明。
|
||||
2. 修改业务项目 `TypeScript/package.json` 中的 Tag:
|
||||
```json
|
||||
"devDependencies": {
|
||||
"ts2lua-unreal": "git+ssh://git@106.55.3.105:20022/szy73711/ts2lua-unreal.git#v0.2.0"
|
||||
}
|
||||
```
|
||||
3. 重新安装并全量回归编译:
|
||||
```bash
|
||||
cd YourUEProject/TypeScript
|
||||
npm install
|
||||
npm run build
|
||||
```
|
||||
4. 验证无误后,提交 `TypeScript/package.json` 与 `TypeScript/package-lock.json`(及更新后的 `.lua`,若入库)。
|
||||
|
||||
### 5.2 异常快速回滚
|
||||
若新版本引入意外回归,直接把 `TypeScript/package.json` 中的 `#v0.2.0` 改回 `#v0.1.0`,执行 `npm install && npm run build` 即可秒级回滚。
|
||||
|
||||
### 5.3 本地高频联调(同时改 `ts2lua-unreal` 插件与 UE 业务代码)
|
||||
无需每次改一行插件都打 Tag,使用 `npm link` 本地直连:
|
||||
|
||||
```bash
|
||||
# 1. 在 ts2lua-unreal 仓库中注册全局链接并构建插件
|
||||
cd /path/to/ts2lua-unreal
|
||||
npm run build:plugin
|
||||
npm link
|
||||
|
||||
# 2. 在 UE 项目的 TypeScript 目录中链接到本地插件
|
||||
cd /path/to/YourUEProject/TypeScript
|
||||
npm link ts2lua-unreal
|
||||
|
||||
# 3. 联调循环:修改 ts2lua-unreal/src/plugin -> npm run build:plugin -> 在 UE 项目跑 npm run build 即刻生效
|
||||
|
||||
# 4. 联调完成后,先将 ts2lua-unreal 提交并打新 Tag(如 v0.2.0),再切回锁定版本
|
||||
cd /path/to/YourUEProject/TypeScript
|
||||
npm unlink ts2lua-unreal
|
||||
# 更新 package.json 中的 Tag 为 #v0.2.0 后:
|
||||
npm install
|
||||
```
|
||||
|
||||
### 5.4 `ts2lua-unreal` 仓库发版规范(维护者)
|
||||
每次向外提供新版本时遵循:
|
||||
1. 更新 `src/plugin/`、`types/` 或 `docs/` 并补齐 `test/plugin.test.mjs` 自动化测试;
|
||||
2. 运行 `npm test && npm run build:examples` 确保全部通过;
|
||||
3. 按 SemVer 更新 `package.json` 版本号与 `CHANGELOG.md`(`patch` 修 bug,`minor` 增特性/新校验,`major` 破坏性变更);
|
||||
4. 提交并打 Tag 推送:
|
||||
```bash
|
||||
git add .
|
||||
git commit -m "release: v0.2.0"
|
||||
git tag -a v0.2.0 -m "v0.2.0"
|
||||
git push origin main --tags
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 6. 在业务项目中引入规则(人类 & AI 助手同步遵守)
|
||||
|
||||
本包自带一份精简规则模板:[ai-rules-template.md](./ai-rules-template.md)。
|
||||
接入新 UE 项目时,建议在业务项目的 `AGENTS.md` / 项目规则文件中加入引用,或复制该模板,确保后续开发者与 AI 助手都能严格遵守全部 `ts2lua-unreal` 规范。
|
||||
@@ -299,7 +299,7 @@ StartCoroutine(this, () => {
|
||||
"noHeader": true,
|
||||
"sourceMapTraceback": false,
|
||||
"luaPlugins": [
|
||||
{ "name": "./dist/plugin/index.js" }
|
||||
{ "name": "ts2lua-unreal" }
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -308,7 +308,7 @@ StartCoroutine(this, () => {
|
||||
> **说明**:
|
||||
> - `luaTarget`:已对齐 `UnrealLuaDemo` 配置(`LuaVersion=lua-5.4.3`),锁定为 `"5.4"`。
|
||||
> - `skipLibCheck`:大型 UE 项目必须设为 `true`,跳过对庞大 UE `.d.ts` 的重复语义校验,显著加快冷启动编译速度。
|
||||
> - `luaPlugins`:加载预编译后的 JS 插件(`./dist/plugin/index.js`),避免运行时动态转译插件带来额外启动耗时。
|
||||
> - `luaPlugins`:外部 UE 项目直接通过 `"extends": "ts2lua-unreal/tsconfig.base.json"` 自动继承 `"ts2lua-unreal"` 插件(加载预编译后的 JS 插件,零运行时动态转译开销)。
|
||||
> - `luaLibImport`:设为 `"require-minimal"`,并由自定义插件进一步审计允许引入的 lualib 符号白名单。
|
||||
> - `sourceMapTraceback`:开发期可按需开启便于排查堆栈,发布包(Shipping)务必关闭以减少开销。
|
||||
|
||||
|
||||
+26
-1
@@ -2,10 +2,35 @@
|
||||
"name": "ts2lua-unreal",
|
||||
"version": "0.1.0",
|
||||
"description": "TypeScript to Lua toolchain and compiler plugin tailored for Unreal Engine + UnLua",
|
||||
"private": true,
|
||||
"main": "dist/plugin/index.js",
|
||||
"types": "dist/plugin/index.d.ts",
|
||||
"bin": {
|
||||
"ts2lua-unreal": "./bin/ts2lua-unreal.js"
|
||||
},
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./dist/plugin/index.d.ts",
|
||||
"default": "./dist/plugin/index.js"
|
||||
},
|
||||
"./tsconfig.base.json": "./tsconfig.base.json",
|
||||
"./types/unlua.d.ts": "./types/unlua.d.ts",
|
||||
"./docs/ts-unlua-spec.md": "./docs/ts-unlua-spec.md",
|
||||
"./docs/integration-guide.md": "./docs/integration-guide.md",
|
||||
"./docs/ai-rules-template.md": "./docs/ai-rules-template.md",
|
||||
"./CHANGELOG.md": "./CHANGELOG.md",
|
||||
"./package.json": "./package.json"
|
||||
},
|
||||
"files": [
|
||||
"bin/",
|
||||
"dist/",
|
||||
"types/",
|
||||
"docs/",
|
||||
"tsconfig.base.json",
|
||||
"CHANGELOG.md",
|
||||
"README.md"
|
||||
],
|
||||
"scripts": {
|
||||
"prepare": "npm run build:plugin",
|
||||
"build:plugin": "tsc -p tsconfig.plugin.json",
|
||||
"build:examples": "npm run build:plugin && tstl -p examples/tsconfig.json",
|
||||
"watch:examples": "tstl -p examples/tsconfig.json --watch",
|
||||
|
||||
+1
-1
@@ -21,7 +21,7 @@
|
||||
"sourceMapTraceback": false,
|
||||
"luaPlugins": [
|
||||
{
|
||||
"name": "./dist/plugin/index.js"
|
||||
"name": "ts2lua-unreal"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user