Files
UnrealLuaDemo/Script/lua/lib/class.lua
T

53 lines
1.5 KiB
Lua
Raw Normal View History

-- 热更友好的纯 TS 类定义运行时(扁平全局函数,零表查询开销,完全兼容 TSTL lualib_bundle)
local pairs = pairs
local type = type
local allClassTbl = {}
local function __TS__DefineClass(name, mod)
local modname = mod and mod.name or ""
local fullName = modname .. ":" .. name
local cls = allClassTbl[fullName]
if cls then
-- 热更重载时清理原型上已删除的旧方法,保持既有实例与子类继承链的原型表引用不变
local modSys = _G.__MODULE__
if modSys and modSys.IsReimporting and modSys.IsReimporting(mod) then
local proto = cls.prototype
if proto then
for k, v in pairs(proto) do
if k ~= "__index" and k ~= "constructor" and type(v) == "function" then
proto[k] = nil
end
end
end
end
cls.name = name
return cls
end
cls = {
__classdefine__ = true,
__fullname__ = fullName,
name = name,
prototype = {},
}
cls.prototype.__index = cls.prototype
cls.prototype.constructor = cls
allClassTbl[fullName] = cls
return cls
end
_G.__TS__DefineClass = __TS__DefineClass
return {
DefineClass = __TS__DefineClass,
allClassTbl = allClassTbl,
GetClass = function(fullName)
return allClassTbl[fullName]
end,
AllClasses = function()
return allClassTbl
end,
}