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:
szy73711 committed 2026-11-30 02:42:51 +08:00
1 parent 4ad4a8f36f
commit 783dccf53d
64 files changed
+2368 -714

No files matched your search

+118 -32
View File
@@ -1,32 +1,32 @@
local ____exports = {}
local ____TestRunner = require("Tests.Harness.TestRunner")
local Reset = ____TestRunner.Reset
local PrintReport = ____TestRunner.PrintReport
local Suite = ____TestRunner.Suite
local Test = ____TestRunner.Test
local AssertEqual = ____TestRunner.AssertEqual
local AssertNear = ____TestRunner.AssertNear
local AssertTrue = ____TestRunner.AssertTrue
local AssertFalse = ____TestRunner.AssertFalse
local AssertNotNil = ____TestRunner.AssertNotNil
local AssertNil = ____TestRunner.AssertNil
local AssertArrayEqual = ____TestRunner.AssertArrayEqual
local ____BasicTypesAndControlFlow = require("Tests.Syntax.BasicTypesAndControlFlow")
local RunBasicTypesAndControlFlowTests = ____BasicTypesAndControlFlow.RunBasicTypesAndControlFlowTests
local ____FunctionsClosuresAndTuples = require("Tests.Syntax.FunctionsClosuresAndTuples")
local RunFunctionsClosuresAndTuplesTests = ____FunctionsClosuresAndTuples.RunFunctionsClosuresAndTuplesTests
local ____PureClassesAndGenerics = require("Tests.Syntax.PureClassesAndGenerics")
local RunPureClassesAndGenericsTests = ____PureClassesAndGenerics.RunPureClassesAndGenericsTests
local ____GameplaySuite = require("Tests.Gameplay.GameplaySuite")
local RunGameplayTests = ____GameplaySuite.RunGameplayTests
local ____Lua54PitfallsTest = require("Tests.Extra.Lua54PitfallsTest")
local RunLua54PitfallsTests = ____Lua54PitfallsTest.RunLua54PitfallsTests
local EventBus = require("Framework.EventBus")
local HotReload = require("Framework.HotReload")
local ____Ex06_NativeContainers = require("Tests.UnLuaExamples.Ex06_NativeContainers")
local VerifyTArrayOperations = ____Ex06_NativeContainers.VerifyTArrayOperations
local VerifyTMapOperations = ____Ex06_NativeContainers.VerifyTMapOperations
local VerifyTSetOperations = ____Ex06_NativeContainers.VerifyTSetOperations
local ____exports = __module.env
local M__Tests_Harness_TestRunner = __MODULE__.Import("Tests.Harness.TestRunner", __module)
local Reset = M__Tests_Harness_TestRunner.Reset
local PrintReport = M__Tests_Harness_TestRunner.PrintReport
local Suite = M__Tests_Harness_TestRunner.Suite
local Test = M__Tests_Harness_TestRunner.Test
local AssertEqual = M__Tests_Harness_TestRunner.AssertEqual
local AssertNear = M__Tests_Harness_TestRunner.AssertNear
local AssertTrue = M__Tests_Harness_TestRunner.AssertTrue
local AssertFalse = M__Tests_Harness_TestRunner.AssertFalse
local AssertNotNil = M__Tests_Harness_TestRunner.AssertNotNil
local AssertNil = M__Tests_Harness_TestRunner.AssertNil
local AssertArrayEqual = M__Tests_Harness_TestRunner.AssertArrayEqual
local M__Tests_Syntax_BasicTypesAndControlFlow = __MODULE__.Import("Tests.Syntax.BasicTypesAndControlFlow", __module)
local RunBasicTypesAndControlFlowTests = M__Tests_Syntax_BasicTypesAndControlFlow.RunBasicTypesAndControlFlowTests
local M__Tests_Syntax_FunctionsClosuresAndTuples = __MODULE__.Import("Tests.Syntax.FunctionsClosuresAndTuples", __module)
local RunFunctionsClosuresAndTuplesTests = M__Tests_Syntax_FunctionsClosuresAndTuples.RunFunctionsClosuresAndTuplesTests
local M__Tests_Syntax_PureClassesAndGenerics = __MODULE__.Import("Tests.Syntax.PureClassesAndGenerics", __module)
local RunPureClassesAndGenericsTests = M__Tests_Syntax_PureClassesAndGenerics.RunPureClassesAndGenericsTests
local M__Tests_Gameplay_GameplaySuite = __MODULE__.Import("Tests.Gameplay.GameplaySuite", __module)
local RunGameplayTests = M__Tests_Gameplay_GameplaySuite.RunGameplayTests
local M__Tests_Extra_Lua54PitfallsTest = __MODULE__.Import("Tests.Extra.Lua54PitfallsTest", __module)
local RunLua54PitfallsTests = M__Tests_Extra_Lua54PitfallsTest.RunLua54PitfallsTests
local EventBus = __MODULE__.Import("Framework.EventBus", __module)
local HotReload = __MODULE__.Import("Framework.HotReload", __module)
local M__Tests_UnLuaExamples_Ex06_NativeContainers = __MODULE__.Import("Tests.UnLuaExamples.Ex06_NativeContainers", __module)
local VerifyTArrayOperations = M__Tests_UnLuaExamples_Ex06_NativeContainers.VerifyTArrayOperations
local VerifyTMapOperations = M__Tests_UnLuaExamples_Ex06_NativeContainers.VerifyTMapOperations
local VerifyTSetOperations = M__Tests_UnLuaExamples_Ex06_NativeContainers.VerifyTSetOperations
local UnLuaBaseActorClass = require("Tests.Syntax.UnLuaBaseActor")
local function TickUEWorld(steps, deltaSeconds)
local tickFn = _G.__UE_TEST_TICK
@@ -325,7 +325,7 @@ function ____exports.RunUERuntimeSuite(world)
end
)
Test(
"Real UE ALuaDemoActor (IUnLuaInterface static binding) + EventBus + Tick + EndPlay",
"Real UE ALuaDemoActor (C++ callback -> pure TS Game.LuaDemoActor) + EventBus + Tick + EndPlay",
function()
local demoActor = world:SpawnActor(
UE.ALuaDemoActor.StaticClass(),
@@ -339,7 +339,7 @@ function ____exports.RunUERuntimeSuite(world)
AssertNotNil(demoActor)
AssertTrue(demoActor:IsA(UE.ALuaDemoActor.StaticClass()))
local initText = demoActor:GetStatusTextString()
AssertTrue((string.find(initText, "UnLua Active", 1, true)) ~= nil)
AssertTrue((string.find(initText, "Pure-TS Actor", 1, true)) ~= nil, "initText: " .. initText)
AssertTrue((string.find(initText, "Pings: 0", 1, true)) ~= nil)
if canTickWorld then
TickUEWorld(5, 0.05)
@@ -443,6 +443,72 @@ function ____exports.RunUERuntimeSuite(world)
AssertTrue(ok)
end
)
Test(
"Real UE single-file __MODULE__.Reimport hot-reloads Game.LuaDemoActor spin speed and color live",
function()
local demoActor = world:SpawnActor(
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)
local beforeText = demoActor:GetStatusTextString()
AssertTrue((string.find(beforeText, "v1", 1, true)) ~= nil)
AssertTrue((string.find(beforeText, "Speed: 60", 1, true)) ~= nil)
AssertTrue((string.find(beforeText, "Color: Cyan", 1, true)) ~= nil)
local actorMod = __MODULE__.Import("Game.LuaDemoActor")
local activeBefore = actorMod:GetActiveControllerCount()
AssertTrue(activeBefore >= 1)
local defaultDir = UE.ULuaHotUpdateSubsystem.GetDefaultScriptDir()
local overrideDir = UE.ULuaHotUpdateSubsystem.GetHotUpdateScriptDir()
local srcPath = defaultDir .. "/Game/LuaDemoActor.lua"
local dstGameDir = overrideDir .. "/Game"
local dstPath = dstGameDir .. "/LuaDemoActor.lua"
local srcFile = (io.open(srcPath, "rb"))
AssertNotNil(srcFile)
local origLua = srcFile:read("a")
srcFile:close()
local 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))
local dstFile = (io.open(dstPath, "wb"))
AssertNotNil(dstFile)
dstFile:write(patchedLua)
dstFile:close()
local okReload, errReload = HotReload.ReloadModule("Game.LuaDemoActor")
os.remove(dstPath)
AssertTrue(
okReload,
tostring(errReload)
)
AssertEqual(actorMod.DEMO_VERSION, "v2-HOTFIX")
AssertEqual(actorMod.ACTOR_SPIN_SPEED, 240)
AssertEqual(
actorMod:GetActiveControllerCount(),
activeBefore
)
local newColor = demoActor:GetCubeColor()
AssertNear(newColor.R, 0.95, 0.01)
AssertNear(newColor.G, 0.12, 0.01)
local afterText = demoActor:GetStatusTextString()
AssertTrue((string.find(afterText, "v2-HOTFIX", 1, true)) ~= nil, "afterText: " .. afterText)
AssertTrue((string.find(afterText, "Speed: 240", 1, true)) ~= nil, "afterText: " .. afterText)
AssertTrue((string.find(afterText, "Color: Crimson", 1, true)) ~= nil, "afterText: " .. afterText)
local okRestore = HotReload.ReloadModule("Game.LuaDemoActor")
AssertTrue(okRestore)
AssertEqual(actorMod.DEMO_VERSION, "v1")
AssertEqual(actorMod.ACTOR_SPIN_SPEED, 60)
demoActor:K2_DestroyActor()
end
)
end
)
end
@@ -461,4 +527,24 @@ function ____exports.RunAllInUE(world)
end
return PrintReport()
end
return ____exports
function __module.UpdateImports()
Reset = M__Tests_Harness_TestRunner.Reset
PrintReport = M__Tests_Harness_TestRunner.PrintReport
Suite = M__Tests_Harness_TestRunner.Suite
Test = M__Tests_Harness_TestRunner.Test
AssertEqual = M__Tests_Harness_TestRunner.AssertEqual
AssertNear = M__Tests_Harness_TestRunner.AssertNear
AssertTrue = M__Tests_Harness_TestRunner.AssertTrue
AssertFalse = M__Tests_Harness_TestRunner.AssertFalse
AssertNotNil = M__Tests_Harness_TestRunner.AssertNotNil
AssertNil = M__Tests_Harness_TestRunner.AssertNil
AssertArrayEqual = M__Tests_Harness_TestRunner.AssertArrayEqual
RunBasicTypesAndControlFlowTests = M__Tests_Syntax_BasicTypesAndControlFlow.RunBasicTypesAndControlFlowTests
RunFunctionsClosuresAndTuplesTests = M__Tests_Syntax_FunctionsClosuresAndTuples.RunFunctionsClosuresAndTuplesTests
RunPureClassesAndGenericsTests = M__Tests_Syntax_PureClassesAndGenerics.RunPureClassesAndGenericsTests
RunGameplayTests = M__Tests_Gameplay_GameplaySuite.RunGameplayTests
RunLua54PitfallsTests = M__Tests_Extra_Lua54PitfallsTest.RunLua54PitfallsTests
VerifyTArrayOperations = M__Tests_UnLuaExamples_Ex06_NativeContainers.VerifyTArrayOperations
VerifyTMapOperations = M__Tests_UnLuaExamples_Ex06_NativeContainers.VerifyTMapOperations
VerifyTSetOperations = M__Tests_UnLuaExamples_Ex06_NativeContainers.VerifyTSetOperations
end