feat: upgrade to ts2lua-unreal v0.3.0 and refactor Game classes to pure C++ callback + TS composition hot-reload
This commit is contained in:
1 parent
4ad4a8f36f
commit
783dccf53d
64 files changed
+2368
-714
No files matched your search
Generated
+3
-3
@@ -8,7 +8,7 @@
|
||||
"name": "unreal-lua-demo-ts",
|
||||
"version": "1.0.0",
|
||||
"devDependencies": {
|
||||
"ts2lua-unreal": "git+ssh://git@106.55.3.105:20022/szy73711/ts2lua-unreal.git#v0.2.2"
|
||||
"ts2lua-unreal": "git+ssh://git@106.55.3.105:20022/szy73711/ts2lua-unreal.git#v0.3.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@typescript-to-lua/language-extensions": {
|
||||
@@ -175,8 +175,8 @@
|
||||
}
|
||||
},
|
||||
"node_modules/ts2lua-unreal": {
|
||||
"version": "0.2.2",
|
||||
"resolved": "git+ssh://git@106.55.3.105:20022/szy73711/ts2lua-unreal.git#cb1539d7383e72b32d0fae600199257a05f4d416",
|
||||
"version": "0.3.0",
|
||||
"resolved": "git+ssh://git@106.55.3.105:20022/szy73711/ts2lua-unreal.git#b1a53d8660061dc51dabe5fc5ea0038d8d25a343",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"lua-types": "^2.14.1",
|
||||
|
||||
@@ -11,6 +11,6 @@
|
||||
"info": "ts2lua-unreal info"
|
||||
},
|
||||
"devDependencies": {
|
||||
"ts2lua-unreal": "git+ssh://git@106.55.3.105:20022/szy73711/ts2lua-unreal.git#v0.2.2"
|
||||
"ts2lua-unreal": "git+ssh://git@106.55.3.105:20022/szy73711/ts2lua-unreal.git#v0.3.0"
|
||||
}
|
||||
}
|
||||
@@ -25,9 +25,11 @@ const luaPath = [
|
||||
|
||||
const code = `
|
||||
package.path = ${JSON.stringify(luaPath)} .. ";" .. package.path
|
||||
require("Tests.Harness.MockUnLuaRuntime").InstallIfNeeded()
|
||||
require("Lib.Module")
|
||||
require("Lib.Class")
|
||||
__MODULE__.Import("Tests.Harness.MockUnLuaRuntime").InstallIfNeeded()
|
||||
_G.__TS2LUA_TEST_AUTO_RUN__ = true
|
||||
require("Tests.RunAllTests")
|
||||
__MODULE__.Import("Tests.RunAllTests")
|
||||
`;
|
||||
|
||||
const result = spawnSync(luaBin, ["-e", code], {
|
||||
|
||||
@@ -4,8 +4,12 @@ import * as Log from "./Log";
|
||||
import * as EventBus from "./EventBus";
|
||||
import * as HotReload from "./HotReload";
|
||||
|
||||
export const Version = "0.1.0";
|
||||
export const Version = "0.2.0";
|
||||
|
||||
/** @module_data */
|
||||
export let IsInitialized = false;
|
||||
|
||||
/** @module_data */
|
||||
export let GameInstance: UE.ULuaGameInstance | undefined = undefined;
|
||||
|
||||
export function Init(gameInstance: UE.ULuaGameInstance): void {
|
||||
|
||||
@@ -7,6 +7,7 @@ interface EventEntry {
|
||||
owner?: unknown;
|
||||
}
|
||||
|
||||
/** @module_data */
|
||||
let Listeners: Record<string, EventEntry[] | undefined> = {};
|
||||
|
||||
export function On<TOwner>(
|
||||
|
||||
@@ -6,14 +6,18 @@ import * as EventBus from "./EventBus";
|
||||
export const EVENT_BEFORE_RELOAD = "Framework.BeforeHotReload";
|
||||
export const EVENT_AFTER_RELOAD = "Framework.AfterHotReload";
|
||||
|
||||
function GetLuaPackage(): LuaPackageTable {
|
||||
return (_G as unknown as Record<string, unknown>)["package"] as LuaPackageTable;
|
||||
}
|
||||
|
||||
function CollectReloadableGameModules(): string[] {
|
||||
function CollectLoadedHotReloadModules(): string[] {
|
||||
const modules: string[] = [];
|
||||
for (const [name] of pairs(GetLuaPackage().loaded)) {
|
||||
if (type(name) === "string" && string.match(name as string, "^Game%.")[0] !== undefined) {
|
||||
if (__MODULE__ === undefined) {
|
||||
return modules;
|
||||
}
|
||||
for (const [name] of pairs(__MODULE__.AllLoadedModules())) {
|
||||
if (
|
||||
type(name) === "string" &&
|
||||
name !== "Framework.HotReload" &&
|
||||
(string.match(name as string, "^Game%.")[0] !== undefined ||
|
||||
string.match(name as string, "^Framework%.")[0] !== undefined)
|
||||
) {
|
||||
modules[modules.length] = name as string;
|
||||
}
|
||||
}
|
||||
@@ -22,74 +26,55 @@ function CollectReloadableGameModules(): string[] {
|
||||
}
|
||||
|
||||
/**
|
||||
* Reloads a specific Lua module using UnLua's hot-reload sandbox (falls back to package.loaded reset).
|
||||
* 单文件热更指定模块(完全基于 Lib/Module.lua 的 __MODULE__.ReimportSafely,零入侵 UnLua)。
|
||||
*/
|
||||
export function ReloadModule(moduleName: string): LuaMultiReturn<[boolean, unknown?]> {
|
||||
if (type(moduleName) !== "string" || moduleName === "") {
|
||||
return $multi(false, "Invalid module name");
|
||||
}
|
||||
|
||||
Log.Info("Hot-reloading Lua module: %s", moduleName);
|
||||
if (__MODULE__ === undefined) {
|
||||
return $multi(false, "__MODULE__ runtime is not initialized");
|
||||
}
|
||||
|
||||
if (__MODULE__.GetModuleByName(moduleName) === undefined) {
|
||||
Log.Warn("Module '%s' is not loaded yet, skipping hot-reload.", moduleName);
|
||||
return $multi(false, "Module not loaded: " + moduleName);
|
||||
}
|
||||
|
||||
Log.Info("Hot-reloading module via __MODULE__: %s", moduleName);
|
||||
EventBus.Emit(EVENT_BEFORE_RELOAD, moduleName);
|
||||
|
||||
const [okHr, unluaHr] = pcall(require, "UnLua.HotReload");
|
||||
const hrMod = unluaHr as UnLuaHotReloadModule | undefined;
|
||||
if (okHr && hrMod !== undefined && type(hrMod.reload) === "function") {
|
||||
const [ok, err] = pcall(hrMod.reload, [moduleName]);
|
||||
if (!ok) {
|
||||
Log.Error("UnLua.HotReload.reload('%s') failed: %s", moduleName, tostring(err));
|
||||
return $multi(false, err);
|
||||
}
|
||||
} else {
|
||||
GetLuaPackage().loaded[moduleName] = undefined;
|
||||
const [ok, result] = pcall(require, moduleName);
|
||||
if (!ok) {
|
||||
Log.Error("Failed to hot-reload module '%s': %s", moduleName, tostring(result));
|
||||
return $multi(false, result);
|
||||
}
|
||||
const [ok, err] = __MODULE__.ReimportSafely(moduleName);
|
||||
if (!ok) {
|
||||
Log.Error("__MODULE__.ReimportSafely('%s') failed: %s", moduleName, tostring(err));
|
||||
return $multi(false, err);
|
||||
}
|
||||
|
||||
EventBus.Emit(EVENT_AFTER_RELOAD, moduleName);
|
||||
Log.Info("Successfully hot-reloaded Lua module: %s", moduleName);
|
||||
Log.Info("Successfully hot-reloaded module: %s", moduleName);
|
||||
return $multi(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Triggers UnLua hot-reload across all loaded Game.* modules (supports Saved/HotUpdate/Script overrides).
|
||||
* 批量热更所有已加载的业务/框架模块(Game.* / Framework.*)。
|
||||
*/
|
||||
export function ReloadAll(): boolean {
|
||||
const gameModules = CollectReloadableGameModules();
|
||||
Log.Info("Triggering UnLua HotReload (modules=%d)...", gameModules.length);
|
||||
const managedModules = CollectLoadedHotReloadModules();
|
||||
Log.Info("Triggering HotReload via __MODULE__ (modules=%d)...", managedModules.length);
|
||||
EventBus.Emit(EVENT_BEFORE_RELOAD, "*");
|
||||
|
||||
const [okHr, unluaHr] = pcall(require, "UnLua.HotReload");
|
||||
const hrMod = unluaHr as UnLuaHotReloadModule | undefined;
|
||||
if (okHr && hrMod !== undefined && type(hrMod.reload) === "function") {
|
||||
if (gameModules.length > 0) {
|
||||
const [ok, err] = pcall(hrMod.reload, gameModules);
|
||||
if (!ok) {
|
||||
Log.Error("UnLua.HotReload.reload(gameModules) failed: %s", tostring(err));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// Also run timestamp-based check for any other modified modules
|
||||
const [ok2, err2] = pcall(hrMod.reload);
|
||||
if (!ok2) {
|
||||
Log.Error("UnLua.HotReload.reload() failed: %s", tostring(err2));
|
||||
return false;
|
||||
}
|
||||
} else if (UnLua !== undefined && type(UnLua.HotReload) === "function") {
|
||||
const [ok, err] = pcall(UnLua.HotReload);
|
||||
let allOk = true;
|
||||
for (let i = 0; i < managedModules.length; i++) {
|
||||
const modName = managedModules[i];
|
||||
const [ok, err] = __MODULE__.ReimportSafely(modName);
|
||||
if (!ok) {
|
||||
Log.Error("UnLua.HotReload() failed: %s", tostring(err));
|
||||
return false;
|
||||
Log.Error("__MODULE__.ReimportSafely('%s') failed: %s", modName, tostring(err));
|
||||
allOk = false;
|
||||
}
|
||||
} else {
|
||||
Log.Warn("UnLua.HotReload is unavailable; skipping global hot reload.");
|
||||
return false;
|
||||
}
|
||||
|
||||
EventBus.Emit(EVENT_AFTER_RELOAD, "*");
|
||||
Log.Info("UnLua HotReload completed.");
|
||||
return true;
|
||||
Log.Info("HotReload completed (ok=%s).", tostring(allOk));
|
||||
return allOk;
|
||||
}
|
||||
@@ -1,12 +1,11 @@
|
||||
/** @noSelfInFile */
|
||||
|
||||
import * as Log from "../Framework/Log";
|
||||
|
||||
@unlua
|
||||
export default class LuaCharacter extends UE.ALuaCharacter {
|
||||
ReceiveBeginPlay(): void {
|
||||
Log.Info("LuaCharacter:ReceiveBeginPlay()");
|
||||
}
|
||||
export function OnBeginPlay(_character: UE.ALuaCharacter): void {
|
||||
Log.Info("LuaCharacter.OnBeginPlay()");
|
||||
}
|
||||
|
||||
ReceiveEndPlay(EndPlayReason: number): void {
|
||||
Log.Info("LuaCharacter:ReceiveEndPlay(reason=%s)", tostring(EndPlayReason));
|
||||
}
|
||||
export function OnEndPlay(_character: UE.ALuaCharacter, endPlayReason: number): void {
|
||||
Log.Info("LuaCharacter.OnEndPlay(reason=%s)", tostring(endPlayReason));
|
||||
}
|
||||
@@ -2,7 +2,7 @@ import * as Log from "../Framework/Log";
|
||||
import * as EventBus from "../Framework/EventBus";
|
||||
import * as HotReload from "../Framework/HotReload";
|
||||
|
||||
interface DemoColorEntry {
|
||||
export interface DemoColorEntry {
|
||||
Name: string;
|
||||
R: number;
|
||||
G: number;
|
||||
@@ -10,7 +10,14 @@ interface DemoColorEntry {
|
||||
A: number;
|
||||
}
|
||||
|
||||
const DEMO_COLORS: DemoColorEntry[] = [
|
||||
// ============================================================
|
||||
// PIE 单文件热更测试参数区(修改此处 -> npm run build -> 触发热更,即时生效)
|
||||
// ============================================================
|
||||
export const DEMO_VERSION = "v1";
|
||||
/** 目标旋转速度(度/秒,直接驱动方块自转速度,热更后下一帧立即生效) */
|
||||
export const ACTOR_SPIN_SPEED = 60.0;
|
||||
/** 当前展示配色(热更修改颜色值与名称后,__module.OnReimport 立即刷新方块材质与 3D 文本) */
|
||||
export 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 },
|
||||
@@ -23,41 +30,73 @@ 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 {
|
||||
private ElapsedTime!: number;
|
||||
private TextRefreshTimer!: number;
|
||||
private TickCount!: number;
|
||||
private PingCount!: number;
|
||||
private ReloadCount!: number;
|
||||
private ColorIndex!: number;
|
||||
private HasLoggedTick!: boolean;
|
||||
private _tmpDeltaRot!: UE.FRotator;
|
||||
/** @noSelf */
|
||||
function IsActorAlive(actor: UE.ALuaDemoActor | undefined): boolean {
|
||||
if (actor === undefined) {
|
||||
return false;
|
||||
}
|
||||
if (UE !== undefined && UE.UKismetSystemLibrary !== undefined && UE.UKismetSystemLibrary.IsValid !== undefined) {
|
||||
return UE.UKismetSystemLibrary.IsValid(actor);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
ReceiveBeginPlay(): void {
|
||||
this.ElapsedTime = 0.0;
|
||||
this.TextRefreshTimer = 0.0;
|
||||
this.TickCount = 0;
|
||||
this.PingCount = 0;
|
||||
this.ReloadCount = 0;
|
||||
this.ColorIndex = 0;
|
||||
this.HasLoggedTick = false;
|
||||
/**
|
||||
* 纯 TS 组合控制器(不继承 UE 对象、不使用 @unlua):
|
||||
* 组合持有 `UE.ALuaDemoActor` 引用,由 `Lib/Class.lua` 的 `__TS__DefineClass` 托管原型表,
|
||||
* 支持单文件热更 `Game.LuaDemoActor` 时在运行中实例上原地更新旋转速度、颜色与全部方法逻辑。
|
||||
*/
|
||||
export class DemoActorController {
|
||||
private actor: UE.ALuaDemoActor;
|
||||
private elapsedTime = 0.0;
|
||||
private textRefreshTimer = 0.0;
|
||||
private tickCount = 0;
|
||||
private pingCount = 0;
|
||||
private reloadCount = 0;
|
||||
private colorIndex = 0;
|
||||
private hasLoggedTick = false;
|
||||
private _tmpDeltaRot: UE.FRotator;
|
||||
|
||||
constructor(actor: UE.ALuaDemoActor) {
|
||||
this.actor = actor;
|
||||
this._tmpDeltaRot = UE.FRotator(0.0, 0.0, 0.0);
|
||||
}
|
||||
|
||||
IsValid(): boolean {
|
||||
return IsActorAlive(this.actor);
|
||||
}
|
||||
|
||||
GetSpinSpeed(): number {
|
||||
return ACTOR_SPIN_SPEED;
|
||||
}
|
||||
|
||||
GetCurrentColor(): DemoColorEntry {
|
||||
return DEMO_COLORS[this.colorIndex] ?? DEMO_COLORS[0];
|
||||
}
|
||||
|
||||
OnBeginPlay(): void {
|
||||
EventBus.On("Demo.Ping", this.OnDemoPing, this);
|
||||
EventBus.On(HotReload.EVENT_AFTER_RELOAD, this.OnAfterHotReload, 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, [R] to HotReload",
|
||||
Log.Info(
|
||||
"LuaDemoActor.OnBeginPlay() ready! version=%s speed=%.1f color=%s Lua=%s",
|
||||
DEMO_VERSION,
|
||||
this.GetSpinSpeed(),
|
||||
this.GetCurrentColor().Name,
|
||||
_VERSION
|
||||
);
|
||||
|
||||
const msg = string.format(
|
||||
"[UnLua Demo] Pure-TS Actor active (%s | Speed=%.0f | Color=%s) - [Space] Ping, [R] HotReload",
|
||||
DEMO_VERSION,
|
||||
this.GetSpinSpeed(),
|
||||
this.GetCurrentColor().Name
|
||||
);
|
||||
UE.UKismetSystemLibrary.PrintString(
|
||||
this,
|
||||
this.actor,
|
||||
msg,
|
||||
true,
|
||||
true,
|
||||
@@ -66,65 +105,67 @@ export default class LuaDemoActor extends UE.ALuaDemoActor {
|
||||
);
|
||||
}
|
||||
|
||||
ReceiveTick(DeltaSeconds: number): void {
|
||||
this.ElapsedTime = (this.ElapsedTime ?? 0.0) + DeltaSeconds;
|
||||
this.TickCount = (this.TickCount ?? 0) + 1;
|
||||
OnTick(deltaSeconds: number): void {
|
||||
this.elapsedTime = this.elapsedTime + deltaSeconds;
|
||||
this.tickCount = this.tickCount + 1;
|
||||
|
||||
if (!this.HasLoggedTick) {
|
||||
this.HasLoggedTick = true;
|
||||
Log.Info("LuaDemoActor:ReceiveTick() first tick verified (dt=%.4f)", DeltaSeconds);
|
||||
if (!this.hasLoggedTick) {
|
||||
this.hasLoggedTick = true;
|
||||
Log.Info("LuaDemoActor.OnTick() first tick verified (dt=%.4f, speed=%.1f)", deltaSeconds, this.GetSpinSpeed());
|
||||
}
|
||||
|
||||
// Rotate cube mesh & apply vertical sine-wave bobbing entirely from Lua
|
||||
const yawDelta = (this.SpinSpeed ?? 60.0) * DeltaSeconds;
|
||||
const yawDelta = this.GetSpinSpeed() * deltaSeconds;
|
||||
const pitchDelta = yawDelta * 0.35;
|
||||
if (!this._tmpDeltaRot) {
|
||||
this._tmpDeltaRot = UE.FRotator(pitchDelta, yawDelta, 0.0);
|
||||
} else {
|
||||
this._tmpDeltaRot.Pitch = pitchDelta;
|
||||
this._tmpDeltaRot.Yaw = yawDelta;
|
||||
this._tmpDeltaRot.Roll = 0.0;
|
||||
}
|
||||
this.AddCubeLocalRotation(this._tmpDeltaRot);
|
||||
this._tmpDeltaRot.Pitch = pitchDelta;
|
||||
this._tmpDeltaRot.Yaw = yawDelta;
|
||||
this._tmpDeltaRot.Roll = 0.0;
|
||||
this.actor.AddCubeLocalRotation(this._tmpDeltaRot);
|
||||
|
||||
const zOffset = math.sin(this.ElapsedTime * (this.BobFrequency ?? 2.0)) * (this.BobAmplitude ?? 25.0);
|
||||
this.SetCubeVerticalOffset(zOffset);
|
||||
const zOffset = math.sin(this.elapsedTime * (this.actor.BobFrequency ?? 2.0)) * (this.actor.BobAmplitude ?? 25.0);
|
||||
this.actor.SetCubeVerticalOffset(zOffset);
|
||||
|
||||
this.TextRefreshTimer = (this.TextRefreshTimer ?? 0.0) + DeltaSeconds;
|
||||
if (this.TextRefreshTimer >= 0.2) {
|
||||
this.TextRefreshTimer = 0.0;
|
||||
this.textRefreshTimer = this.textRefreshTimer + deltaSeconds;
|
||||
if (this.textRefreshTimer >= 0.2) {
|
||||
this.textRefreshTimer = 0.0;
|
||||
this.RefreshStatusText();
|
||||
}
|
||||
}
|
||||
|
||||
OnDemoPing(sourceName: unknown): void {
|
||||
this.PingCount = (this.PingCount ?? 0) + 1;
|
||||
this.ColorIndex = ((this.ColorIndex ?? 0) + 1) % DEMO_COLORS.length;
|
||||
this.pingCount = this.pingCount + 1;
|
||||
globalPingCount = globalPingCount + 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,
|
||||
"[UnLua Demo] Ping #%d from %s -> Cube color: %s (Speed=%.0f)",
|
||||
this.pingCount,
|
||||
tostring(sourceName),
|
||||
colorEntry.Name
|
||||
colorEntry.Name,
|
||||
this.GetSpinSpeed()
|
||||
);
|
||||
Log.Info(msg);
|
||||
UE.UKismetSystemLibrary.PrintString(this, msg, true, true, MakeLinearColor(colorEntry), 3.0);
|
||||
UE.UKismetSystemLibrary.PrintString(this.actor, msg, true, true, MakeLinearColor(colorEntry), 3.0);
|
||||
}
|
||||
|
||||
OnAfterHotReload(moduleName: unknown): void {
|
||||
this.ReloadCount = (this.ReloadCount ?? 0) + 1;
|
||||
this.reloadCount = this.reloadCount + 1;
|
||||
globalReloadCount = globalReloadCount + 1;
|
||||
this.ApplyCurrentColor();
|
||||
this.RefreshStatusText();
|
||||
|
||||
const msg = string.format(
|
||||
"[UnLua Demo] HotReload #%d completed (target=%s)",
|
||||
this.ReloadCount,
|
||||
tostring(moduleName)
|
||||
"[UnLua Demo] HotReload #%d applied! target=%s | version=%s | Speed=%.0f | Color=%s",
|
||||
this.reloadCount,
|
||||
tostring(moduleName),
|
||||
DEMO_VERSION,
|
||||
this.GetSpinSpeed(),
|
||||
this.GetCurrentColor().Name
|
||||
);
|
||||
Log.Info(msg);
|
||||
UE.UKismetSystemLibrary.PrintString(
|
||||
this,
|
||||
this.actor,
|
||||
msg,
|
||||
true,
|
||||
true,
|
||||
@@ -133,27 +174,125 @@ export default class LuaDemoActor extends UE.ALuaDemoActor {
|
||||
);
|
||||
}
|
||||
|
||||
ResetToPrimaryColor(): void {
|
||||
this.colorIndex = 0;
|
||||
}
|
||||
|
||||
ApplyCurrentColor(): DemoColorEntry {
|
||||
const colorEntry = DEMO_COLORS[this.ColorIndex ?? 0] ?? DEMO_COLORS[0];
|
||||
this.SetCubeColor(MakeLinearColor(colorEntry));
|
||||
const colorEntry = this.GetCurrentColor();
|
||||
this.actor.SetCubeColor(MakeLinearColor(colorEntry));
|
||||
return colorEntry;
|
||||
}
|
||||
|
||||
RefreshStatusText(): void {
|
||||
const colorEntry = DEMO_COLORS[this.ColorIndex ?? 0] ?? DEMO_COLORS[0];
|
||||
const colorEntry = this.GetCurrentColor();
|
||||
const text = string.format(
|
||||
"UnLua Active (%s)\nTime: %.1fs | Pings: %d | Reloads: %d | Color: %s\n[Space] Ping Lua | [R] HotReload",
|
||||
"Pure-TS Actor (%s | %s)\nSpeed: %.0f deg/s | Color: %s\nTime: %.1fs | Pings: %d | Reloads: %d\n[Space] Ping | [R] HotReload",
|
||||
_VERSION,
|
||||
this.ElapsedTime ?? 0.0,
|
||||
this.PingCount ?? 0,
|
||||
this.ReloadCount ?? 0,
|
||||
colorEntry.Name
|
||||
DEMO_VERSION,
|
||||
this.GetSpinSpeed(),
|
||||
colorEntry.Name,
|
||||
this.elapsedTime,
|
||||
this.pingCount,
|
||||
this.reloadCount
|
||||
);
|
||||
this.SetStatusText(text);
|
||||
this.actor.SetStatusText(text);
|
||||
}
|
||||
|
||||
ReceiveEndPlay(EndPlayReason: number): void {
|
||||
OnEndPlay(_endPlayReason: number): void {
|
||||
EventBus.OffAllForOwner(this);
|
||||
Log.Info("LuaDemoActor:ReceiveEndPlay(ticks=%d, pings=%d)", this.TickCount ?? 0, this.PingCount ?? 0);
|
||||
Log.Info("LuaDemoActor.OnEndPlay(ticks=%d, pings=%d)", this.tickCount, this.pingCount);
|
||||
}
|
||||
}
|
||||
|
||||
/** 跨热更保留的运行中控制器映射表(key = actor:GetName()) */
|
||||
/** @module_data */
|
||||
const activeControllers: Record<string, DemoActorController | undefined> = {};
|
||||
|
||||
/** @module_data */
|
||||
let globalPingCount = 0;
|
||||
|
||||
/** @module_data */
|
||||
let globalReloadCount = 0;
|
||||
|
||||
/** @noSelf */
|
||||
function GetActorKey(actor: UE.ALuaDemoActor): string {
|
||||
if (actor.GetName !== undefined) {
|
||||
return actor.GetName();
|
||||
}
|
||||
return tostring(actor);
|
||||
}
|
||||
|
||||
/** @noSelf */
|
||||
export function OnBeginPlay(actor: UE.ALuaDemoActor): void {
|
||||
const key = GetActorKey(actor);
|
||||
const old = activeControllers[key];
|
||||
if (old !== undefined) {
|
||||
old.OnEndPlay(0);
|
||||
}
|
||||
const ctrl = new DemoActorController(actor);
|
||||
activeControllers[key] = ctrl;
|
||||
ctrl.OnBeginPlay();
|
||||
}
|
||||
|
||||
/** @noSelf */
|
||||
export function OnTick(actor: UE.ALuaDemoActor, deltaSeconds: number): void {
|
||||
const ctrl = activeControllers[GetActorKey(actor)];
|
||||
if (ctrl !== undefined) {
|
||||
ctrl.OnTick(deltaSeconds);
|
||||
}
|
||||
}
|
||||
|
||||
/** @noSelf */
|
||||
export function OnEndPlay(actor: UE.ALuaDemoActor, endPlayReason: number): void {
|
||||
const key = GetActorKey(actor);
|
||||
const ctrl = activeControllers[key];
|
||||
if (ctrl !== undefined) {
|
||||
ctrl.OnEndPlay(endPlayReason);
|
||||
activeControllers[key] = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
/** @noSelf */
|
||||
export function GetGlobalStats(): { pings: number; reloads: number; version: string; spinSpeed: number; primaryColor: string } {
|
||||
return {
|
||||
pings: globalPingCount,
|
||||
reloads: globalReloadCount,
|
||||
version: DEMO_VERSION,
|
||||
spinSpeed: ACTOR_SPIN_SPEED,
|
||||
primaryColor: DEMO_COLORS[0].Name,
|
||||
};
|
||||
}
|
||||
|
||||
/** @noSelf */
|
||||
export function GetActiveControllerCount(): number {
|
||||
let count = 0;
|
||||
for (const [_, ctrl] of pairs(activeControllers)) {
|
||||
if (ctrl !== undefined) {
|
||||
count = count + 1;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
// 模块单文件热更完成后立即将新的颜色、旋转速度与 3D 文本应用到 PIE 中所有活跃 Actor
|
||||
__module.OnReimport = () => {
|
||||
for (const [key, ctrl] of pairs(activeControllers)) {
|
||||
if (ctrl !== undefined) {
|
||||
if (ctrl.IsValid()) {
|
||||
ctrl.ResetToPrimaryColor();
|
||||
ctrl.ApplyCurrentColor();
|
||||
ctrl.RefreshStatusText();
|
||||
} else {
|
||||
activeControllers[key] = undefined;
|
||||
}
|
||||
}
|
||||
}
|
||||
Log.Info(
|
||||
"Game.LuaDemoActor OnReimport applied! version=%s speed=%.1f color=%s activeActors=%d",
|
||||
DEMO_VERSION,
|
||||
ACTOR_SPIN_SPEED,
|
||||
DEMO_COLORS[0].Name,
|
||||
GetActiveControllerCount()
|
||||
);
|
||||
};
|
||||
@@ -1,15 +1,14 @@
|
||||
/** @noSelfInFile */
|
||||
|
||||
import * as Bootstrap from "../Framework/Bootstrap";
|
||||
import * as Log from "../Framework/Log";
|
||||
|
||||
@unlua
|
||||
export default class LuaGameInstance extends UE.ULuaGameInstance {
|
||||
ReceiveInit(): void {
|
||||
Bootstrap.Init(this);
|
||||
Log.Info("LuaGameInstance:ReceiveInit()");
|
||||
}
|
||||
export function OnInit(gameInstance: UE.ULuaGameInstance): void {
|
||||
Bootstrap.Init(gameInstance);
|
||||
Log.Info("LuaGameInstance.OnInit()");
|
||||
}
|
||||
|
||||
ReceiveShutdown(): void {
|
||||
Log.Info("LuaGameInstance:ReceiveShutdown()");
|
||||
Bootstrap.Shutdown();
|
||||
}
|
||||
export function OnShutdown(_gameInstance: UE.ULuaGameInstance): void {
|
||||
Log.Info("LuaGameInstance.OnShutdown()");
|
||||
Bootstrap.Shutdown();
|
||||
}
|
||||
@@ -1,20 +1,19 @@
|
||||
/** @noSelfInFile */
|
||||
|
||||
import * as Log from "../Framework/Log";
|
||||
|
||||
@unlua
|
||||
export default class LuaGameMode extends UE.ALuaGameModeBase {
|
||||
ReceiveBeginPlay(): void {
|
||||
Log.Info("LuaGameMode:ReceiveBeginPlay() - Demo scene started");
|
||||
UE.UKismetSystemLibrary.PrintString(
|
||||
this,
|
||||
"[UnLua Demo] LuaGameMode initialized!",
|
||||
true,
|
||||
true,
|
||||
UE.FLinearColor(0.3, 0.85, 1.0, 1.0),
|
||||
5.0
|
||||
);
|
||||
}
|
||||
export function OnBeginPlay(gameMode: UE.ALuaGameModeBase): void {
|
||||
Log.Info("LuaGameMode.OnBeginPlay() - Demo scene started");
|
||||
UE.UKismetSystemLibrary.PrintString(
|
||||
gameMode,
|
||||
"[UnLua Demo] LuaGameMode initialized (Pure TS Callback Mode)!",
|
||||
true,
|
||||
true,
|
||||
UE.FLinearColor(0.3, 0.85, 1.0, 1.0),
|
||||
5.0
|
||||
);
|
||||
}
|
||||
|
||||
ReceiveEndPlay(EndPlayReason: number): void {
|
||||
Log.Info("LuaGameMode:ReceiveEndPlay(reason=%s)", tostring(EndPlayReason));
|
||||
}
|
||||
export function OnEndPlay(_gameMode: UE.ALuaGameModeBase, endPlayReason: number): void {
|
||||
Log.Info("LuaGameMode.OnEndPlay(reason=%s)", tostring(endPlayReason));
|
||||
}
|
||||
@@ -1,24 +1,23 @@
|
||||
/** @noSelfInFile */
|
||||
|
||||
import * as Log from "../Framework/Log";
|
||||
import * as EventBus from "../Framework/EventBus";
|
||||
import * as HotReload from "../Framework/HotReload";
|
||||
|
||||
@unlua
|
||||
export default class LuaPlayerController extends UE.ALuaPlayerController {
|
||||
ReceiveBeginPlay(): void {
|
||||
Log.Info("LuaPlayerController:ReceiveBeginPlay() - Ready for [Space]=Ping, [R]=HotReload");
|
||||
}
|
||||
export function OnBeginPlay(_pc: UE.ALuaPlayerController): void {
|
||||
Log.Info("LuaPlayerController.OnBeginPlay() - Ready for [Space]=Ping, [R]=HotReload");
|
||||
}
|
||||
|
||||
OnDemoPingPressed(): void {
|
||||
Log.Info("LuaPlayerController:OnDemoPingPressed() -> Emitting Demo.Ping");
|
||||
EventBus.Emit("Demo.Ping", "LuaPlayerController");
|
||||
}
|
||||
export function OnDemoPingPressed(_pc: UE.ALuaPlayerController): void {
|
||||
Log.Info("LuaPlayerController.OnDemoPingPressed() -> Emitting Demo.Ping");
|
||||
EventBus.Emit("Demo.Ping", "LuaPlayerController");
|
||||
}
|
||||
|
||||
OnDemoHotReloadPressed(): void {
|
||||
Log.Info("LuaPlayerController:OnDemoHotReloadPressed() -> Triggering HotReload.ReloadAll()");
|
||||
HotReload.ReloadAll();
|
||||
}
|
||||
export function OnDemoHotReloadPressed(_pc: UE.ALuaPlayerController): void {
|
||||
Log.Info("LuaPlayerController.OnDemoHotReloadPressed() -> Triggering HotReload.ReloadAll()");
|
||||
HotReload.ReloadAll();
|
||||
}
|
||||
|
||||
ReceiveEndPlay(EndPlayReason: number): void {
|
||||
Log.Info("LuaPlayerController:ReceiveEndPlay(reason=%s)", tostring(EndPlayReason));
|
||||
}
|
||||
export function OnEndPlay(_pc: UE.ALuaPlayerController, endPlayReason: number): void {
|
||||
Log.Info("LuaPlayerController.OnEndPlay(reason=%s)", tostring(endPlayReason));
|
||||
}
|
||||
@@ -14,7 +14,7 @@ import { CreateBoundInstance, ResetMockState } from "../Harness/MockUnLuaRuntime
|
||||
import * as Bootstrap from "../../Framework/Bootstrap";
|
||||
import * as EventBus from "../../Framework/EventBus";
|
||||
import * as HotReload from "../../Framework/HotReload";
|
||||
import LuaDemoActor from "../../Game/LuaDemoActor";
|
||||
import * as LuaDemoActor from "../../Game/LuaDemoActor";
|
||||
|
||||
export function RunHotReloadAndFrameworkTests(): void {
|
||||
Suite("Extra.FrameworkAndHotReload", () => {
|
||||
@@ -105,7 +105,7 @@ export function RunHotReloadAndFrameworkTests(): void {
|
||||
EventBus.Clear();
|
||||
});
|
||||
|
||||
Test("LuaDemoActor full integration: BeginPlay, Tick, Ping color cycle, HotReload event & EndPlay cleanup", () => {
|
||||
Test("LuaDemoActor full integration: pure TS callback BeginPlay, Tick, Ping color cycle, HotReload & EndPlay cleanup", () => {
|
||||
ResetMockState();
|
||||
EventBus.Clear();
|
||||
|
||||
@@ -114,8 +114,8 @@ export function RunHotReloadAndFrameworkTests(): void {
|
||||
let rotationAddCount = 0;
|
||||
let lastVerticalOffset = 0;
|
||||
|
||||
const demoActor = CreateBoundInstance<LuaDemoActor>(
|
||||
LuaDemoActor as unknown as Record<string, any>,
|
||||
const fakeActor = CreateBoundInstance<UE.ALuaDemoActor>(
|
||||
{},
|
||||
"ALuaDemoActor",
|
||||
{
|
||||
SpinSpeed: 90,
|
||||
@@ -136,11 +136,12 @@ export function RunHotReloadAndFrameworkTests(): void {
|
||||
}
|
||||
);
|
||||
|
||||
demoActor.ReceiveBeginPlay();
|
||||
LuaDemoActor.OnBeginPlay(fakeActor);
|
||||
AssertEqual(colorApplyCount, 1);
|
||||
AssertTrue(string.find(lastStatusText, "Pings: 0", 1, true)[0] !== undefined);
|
||||
AssertTrue(string.find(lastStatusText, "Speed: 60", 1, true)[0] !== undefined);
|
||||
|
||||
demoActor.ReceiveTick(0.25);
|
||||
LuaDemoActor.OnTick(fakeActor, 0.25);
|
||||
AssertEqual(rotationAddCount, 1);
|
||||
AssertTrue(lastVerticalOffset !== 0);
|
||||
|
||||
@@ -149,12 +150,13 @@ export function RunHotReloadAndFrameworkTests(): void {
|
||||
AssertTrue(string.find(lastStatusText, "Pings: 1", 1, true)[0] !== undefined);
|
||||
|
||||
EventBus.Emit(HotReload.EVENT_AFTER_RELOAD, "Game.LuaDemoActor");
|
||||
AssertEqual(colorApplyCount, 3);
|
||||
AssertTrue(string.find(lastStatusText, "Reloads: 1", 1, true)[0] !== undefined);
|
||||
|
||||
// EndPlay 后不再响应 EventBus
|
||||
demoActor.ReceiveEndPlay(UE.EEndPlayReason.Destroyed);
|
||||
LuaDemoActor.OnEndPlay(fakeActor, UE.EEndPlayReason.Destroyed);
|
||||
EventBus.Emit("Demo.Ping", "AfterEndPlay");
|
||||
AssertEqual(colorApplyCount, 2);
|
||||
AssertEqual(colorApplyCount, 3);
|
||||
EventBus.Clear();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -236,7 +236,7 @@ export function RunUERuntimeSuite(world: UE.UWorld): void {
|
||||
UnLua.Unref(obj);
|
||||
});
|
||||
|
||||
Test("Real UE ALuaDemoActor (IUnLuaInterface static binding) + EventBus + Tick + EndPlay", () => {
|
||||
Test("Real UE ALuaDemoActor (C++ callback -> pure TS Game.LuaDemoActor) + EventBus + Tick + EndPlay", () => {
|
||||
// 不调用 EventBus.Clear(),避免在 PIE 关卡中误清场景里已运行的 LuaDemoActor 监听
|
||||
const demoActor = world.SpawnActor<UE.ALuaDemoActor>(
|
||||
UE.ALuaDemoActor.StaticClass(),
|
||||
@@ -246,9 +246,9 @@ export function RunUERuntimeSuite(world: UE.UWorld): void {
|
||||
AssertNotNil(demoActor);
|
||||
AssertTrue(demoActor!.IsA(UE.ALuaDemoActor.StaticClass()));
|
||||
|
||||
// Spawn 时已自动通过 IUnLuaInterface 绑定 Game.LuaDemoActor 并执行 ReceiveBeginPlay
|
||||
// Spawn 时已通过 C++ FLuaCallbackBridge 回调 Game.LuaDemoActor.OnBeginPlay
|
||||
const initText = demoActor!.GetStatusTextString();
|
||||
AssertTrue(string.find(initText, "UnLua Active", 1, true)[0] !== undefined);
|
||||
AssertTrue(string.find(initText, "Pure-TS Actor", 1, true)[0] !== undefined, "initText: " + initText);
|
||||
AssertTrue(string.find(initText, "Pings: 0", 1, true)[0] !== undefined);
|
||||
|
||||
if (canTickWorld) {
|
||||
@@ -337,6 +337,74 @@ export function RunUERuntimeSuite(world: UE.UWorld): void {
|
||||
const [ok] = HotReload.ReloadModule("Framework.Log");
|
||||
AssertTrue(ok);
|
||||
});
|
||||
|
||||
Test("Real UE single-file __MODULE__.Reimport hot-reloads Game.LuaDemoActor spin speed and color live", () => {
|
||||
const demoActor = world.SpawnActor<UE.ALuaDemoActor>(
|
||||
UE.ALuaDemoActor.StaticClass(),
|
||||
UE.FTransform(UE.FRotator(0, 0, 0).ToQuat(), UE.FVector(0, 0, 200), UE.FVector(1, 1, 1)),
|
||||
UE.ESpawnActorCollisionHandlingMethod.AlwaysSpawn
|
||||
);
|
||||
AssertNotNil(demoActor);
|
||||
|
||||
const beforeText = demoActor!.GetStatusTextString();
|
||||
AssertTrue(string.find(beforeText, "v1", 1, true)[0] !== undefined);
|
||||
AssertTrue(string.find(beforeText, "Speed: 60", 1, true)[0] !== undefined);
|
||||
AssertTrue(string.find(beforeText, "Color: Cyan", 1, true)[0] !== undefined);
|
||||
|
||||
const actorMod = __MODULE__.Import("Game.LuaDemoActor");
|
||||
const activeBefore: number = actorMod.GetActiveControllerCount();
|
||||
AssertTrue(activeBefore >= 1);
|
||||
|
||||
// 模拟修改 Game/LuaDemoActor.lua 的旋转速度与颜色,写入热更覆盖目录并触发单文件热更
|
||||
const defaultDir = UE.ULuaHotUpdateSubsystem.GetDefaultScriptDir();
|
||||
const overrideDir = UE.ULuaHotUpdateSubsystem.GetHotUpdateScriptDir();
|
||||
const srcPath = defaultDir + "/Game/LuaDemoActor.lua";
|
||||
const dstGameDir = overrideDir + "/Game";
|
||||
const dstPath = dstGameDir + "/LuaDemoActor.lua";
|
||||
|
||||
const srcFile = io.open(srcPath, "rb")[0];
|
||||
AssertNotNil(srcFile);
|
||||
const origLua = srcFile!.read("a") as string;
|
||||
srcFile!.close();
|
||||
|
||||
let [patchedLua] = string.gsub(origLua, '"v1"', '"v2-HOTFIX"');
|
||||
[patchedLua] = string.gsub(patchedLua, "ACTOR_SPIN_SPEED = 60", "ACTOR_SPIN_SPEED = 240");
|
||||
[patchedLua] = string.gsub(patchedLua, '"Cyan"', '"Crimson"');
|
||||
[patchedLua] = string.gsub(patchedLua, "R = 0%.08", "R = 0.95");
|
||||
[patchedLua] = string.gsub(patchedLua, "G = 0%.78", "G = 0.12");
|
||||
[patchedLua] = string.gsub(patchedLua, "B = 0%.95", "B = 0.18");
|
||||
os.execute(string.format('mkdir -p "%s"', dstGameDir));
|
||||
const dstFile = io.open(dstPath, "wb")[0];
|
||||
AssertNotNil(dstFile);
|
||||
dstFile!.write(patchedLua);
|
||||
dstFile!.close();
|
||||
|
||||
const [okReload, errReload] = HotReload.ReloadModule("Game.LuaDemoActor");
|
||||
// 立即清理临时覆盖文件,避免断言失败时残留覆盖文件污染后续运行
|
||||
os.remove(dstPath);
|
||||
AssertTrue(okReload, tostring(errReload));
|
||||
|
||||
// 1. 模块导出即时更新且 @module_data 活跃控制器表完整保留
|
||||
AssertEqual(actorMod.DEMO_VERSION, "v2-HOTFIX");
|
||||
AssertEqual(actorMod.ACTOR_SPIN_SPEED, 240);
|
||||
AssertEqual(actorMod.GetActiveControllerCount(), activeBefore);
|
||||
// 2. 运行中 Actor 的颜色与 3D 状态文本已通过 __module.OnReimport 即时刷新
|
||||
const newColor = demoActor!.GetCubeColor();
|
||||
AssertNear(newColor.R, 0.95, 1e-2);
|
||||
AssertNear(newColor.G, 0.12, 1e-2);
|
||||
const afterText = demoActor!.GetStatusTextString();
|
||||
AssertTrue(string.find(afterText, "v2-HOTFIX", 1, true)[0] !== undefined, "afterText: " + afterText);
|
||||
AssertTrue(string.find(afterText, "Speed: 240", 1, true)[0] !== undefined, "afterText: " + afterText);
|
||||
AssertTrue(string.find(afterText, "Color: Crimson", 1, true)[0] !== undefined, "afterText: " + afterText);
|
||||
|
||||
// 热更还原
|
||||
const [okRestore] = HotReload.ReloadModule("Game.LuaDemoActor");
|
||||
AssertTrue(okRestore);
|
||||
AssertEqual(actorMod.DEMO_VERSION, "v1");
|
||||
AssertEqual(actorMod.ACTOR_SPIN_SPEED, 60);
|
||||
|
||||
demoActor!.K2_DestroyActor();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Vendored
+6
@@ -14,3 +14,9 @@ interface LuaPackageTable {
|
||||
declare var GameFramework: Record<string, unknown> | undefined;
|
||||
declare var __UE_TEST_WORLD: UE.UWorld | undefined;
|
||||
declare var __UE_TEST_TICK: ((this: void, deltaSeconds: number) => void) | undefined;
|
||||
|
||||
declare namespace UE {
|
||||
interface ALuaDemoActor {
|
||||
GetCubeColor(): FLinearColor;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user