feat: add UnLua TS spec, TSTL compiler plugin, types, examples, and tests

This commit is contained in:
szy73711 committed 2026-11-28 22:26:22 +08:00
1 parent 68852d049c
commit c0ac6eaad6
19 files changed
+3273 -1

No files matched your search

+44
View File
@@ -0,0 +1,44 @@
/** @noSelfInFile */
interface ListenerEntry {
callback: (this: any, ...args: any[]) => void;
owner: unknown;
}
const listeners: Record<string, ListenerEntry[] | undefined> = {};
export function On<TOwner>(
eventName: string,
callback: (this: TOwner, ...args: any[]) => void,
owner: TOwner
): void {
let list = listeners[eventName];
if (!list) {
list = [];
listeners[eventName] = list;
}
list.push({ callback, owner });
}
export function Emit(eventName: string, ...args: unknown[]): void {
const list = listeners[eventName];
if (!list) return;
for (let i = 0; i < list.length; i++) {
const entry = list[i];
entry.callback.call(entry.owner, ...args);
}
}
export function OffAllForOwner(owner: unknown): void {
for (const [eventName, list] of pairs(listeners)) {
if (!list) continue;
for (let i = list.length - 1; i >= 0; i--) {
if (list[i].owner === owner) {
table.remove(list, i + 1);
}
}
if (list.length === 0) {
listeners[eventName] = undefined;
}
}
}
+16
View File
@@ -0,0 +1,16 @@
/** @noSelfInFile */
export function Info(fmt: string, ...args: unknown[]): void {
const msg = args.length > 0 ? string.format(fmt, ...args) : fmt;
print("[INFO] " + msg);
}
export function Warn(fmt: string, ...args: unknown[]): void {
const msg = args.length > 0 ? string.format(fmt, ...args) : fmt;
print("[WARN] " + msg);
}
export function Error(fmt: string, ...args: unknown[]): void {
const msg = args.length > 0 ? string.format(fmt, ...args) : fmt;
print("[ERROR] " + msg);
}
+140
View File
@@ -0,0 +1,140 @@
import * as Log from "../Framework/Log";
import * as EventBus from "../Framework/EventBus";
interface DemoColorEntry {
Name: string;
R: number;
G: number;
B: number;
A: number;
}
const DEMO_COLORS: DemoColorEntry[] = [
{ Name: "Cyan", R: 0.08, G: 0.78, B: 0.95, A: 1.0 },
{ Name: "Gold", R: 0.98, G: 0.72, B: 0.12, A: 1.0 },
{ Name: "Magenta", R: 0.92, G: 0.22, B: 0.65, A: 1.0 },
{ Name: "Lime", R: 0.25, G: 0.9, B: 0.35, A: 1.0 },
{ Name: "Orange", R: 0.98, G: 0.42, B: 0.14, A: 1.0 },
];
/** @noSelf */
function MakeLinearColor(c: DemoColorEntry): UE.FLinearColor {
return UE.FLinearColor(c.R, c.G, c.B, c.A);
}
@unlua
export default class LuaDemoActor extends UE.ALuaDemoActor {
protected ElapsedTime!: number;
protected TextRefreshTimer!: number;
protected TickCount!: number;
protected PingCount!: number;
protected ColorIndex!: number;
protected HasLoggedTick!: boolean;
ReceiveBeginPlay(): void {
super.ReceiveBeginPlay();
this.ElapsedTime = 0.0;
this.TextRefreshTimer = 0.0;
this.TickCount = 0;
this.PingCount = 0;
this.ColorIndex = 0;
this.HasLoggedTick = false;
EventBus.On("Demo.Ping", this.OnDemoPing, this);
this.ApplyCurrentColor();
this.RefreshStatusText();
Log.Info("LuaDemoActor:ReceiveBeginPlay() bound successfully! Lua=%s", _VERSION);
const msg = string.format(
"[UnLua Demo] LuaDemoActor active (%s) - Press [Space] to Ping",
_VERSION
);
UE.UKismetSystemLibrary.PrintString(
this,
msg,
true,
true,
UE.FLinearColor(0.2, 1.0, 0.4, 1.0),
8.0
);
}
ReceiveTick(DeltaSeconds: number): void {
this.ElapsedTime = (this.ElapsedTime || 0.0) + DeltaSeconds;
this.TickCount = (this.TickCount || 0) + 1;
if (!this.HasLoggedTick) {
this.HasLoggedTick = true;
Log.Info("LuaDemoActor:ReceiveTick() first tick verified (dt=%.4f)", DeltaSeconds);
}
const yawDelta = (this.SpinSpeed || 60.0) * DeltaSeconds;
const pitchDelta = yawDelta * 0.35;
this.AddCubeLocalRotation(UE.FRotator(pitchDelta, yawDelta, 0.0));
const zOffset =
math.sin(this.ElapsedTime * (this.BobFrequency || 2.0)) * (this.BobAmplitude || 25.0);
this.SetCubeVerticalOffset(zOffset);
this.TextRefreshTimer = (this.TextRefreshTimer || 0.0) + DeltaSeconds;
if (this.TextRefreshTimer >= 0.2) {
this.TextRefreshTimer = 0.0;
this.RefreshStatusText();
}
}
OnDemoPing(sourceName: string): void {
this.PingCount = (this.PingCount || 0) + 1;
this.ColorIndex = (this.ColorIndex + 1) % DEMO_COLORS.length;
const colorEntry = this.ApplyCurrentColor();
this.RefreshStatusText();
const msg = string.format(
"[UnLua Demo] Ping #%d from %s -> Cube color: %s",
this.PingCount,
tostring(sourceName),
colorEntry.Name
);
Log.Info(msg);
UE.UKismetSystemLibrary.PrintString(
this,
msg,
true,
true,
MakeLinearColor(colorEntry),
3.0
);
}
ApplyCurrentColor(): DemoColorEntry {
const colorEntry = DEMO_COLORS[this.ColorIndex] || DEMO_COLORS[0];
this.SetCubeColor(MakeLinearColor(colorEntry));
return colorEntry;
}
RefreshStatusText(): void {
const colorEntry = DEMO_COLORS[this.ColorIndex] || DEMO_COLORS[0];
const text = string.format(
"UnLua Active (%s)\nTime: %.1fs | Pings: %d | Color: %s",
_VERSION,
this.ElapsedTime || 0.0,
this.PingCount || 0,
colorEntry.Name
);
this.SetStatusText(text);
}
ReceiveEndPlay(EndPlayReason: number): void {
EventBus.OffAllForOwner(this);
Log.Info(
"LuaDemoActor:ReceiveEndPlay(ticks=%d, pings=%d, reason=%s)",
this.TickCount || 0,
this.PingCount || 0,
tostring(EndPlayReason)
);
super.ReceiveEndPlay(EndPlayReason);
}
}
+17
View File
@@ -0,0 +1,17 @@
import * as Log from "../Framework/Log";
import LuaDemoActor from "./LuaDemoActor";
@unlua
export default class LuaSpecialDemoActor extends LuaDemoActor {
private BoostMultiplier!: number;
ReceiveBeginPlay(): void {
super.ReceiveBeginPlay();
this.BoostMultiplier = 2.0;
Log.Info("LuaSpecialDemoActor initialized with boost=%.1f", this.BoostMultiplier);
}
ReceiveTick(DeltaSeconds: number): void {
super.ReceiveTick(DeltaSeconds * this.BoostMultiplier);
}
}
+24
View File
@@ -0,0 +1,24 @@
{
"extends": "../tsconfig.base.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "out"
},
"include": [
"src/**/*.ts",
"types/**/*.d.ts",
"../types/**/*.d.ts"
],
"tstl": {
"luaTarget": "5.4",
"luaLibImport": "require-minimal",
"noImplicitSelf": false,
"noHeader": true,
"sourceMapTraceback": false,
"luaPlugins": [
{
"name": "../dist/plugin/index.js"
}
]
}
}
+16
View File
@@ -0,0 +1,16 @@
/**
* UnrealLuaDemo 项目自定义 C++ Actor 类型声明示例
*/
declare namespace UE {
class ALuaDemoActor extends AActor {
SpinSpeed: number;
BobFrequency: number;
BobAmplitude: number;
AddCubeLocalRotation(DeltaRotation: FRotator): void;
SetCubeVerticalOffset(ZOffset: number): void;
SetCubeColor(Color: FLinearColor): void;
SetStatusText(Text: string): void;
static StaticClass(this: void): UClass;
}
}