1
0
forked from redo/BlockLua

Update libbl.lua

This commit is contained in:
2025-12-08 03:09:47 -05:00
parent d9a416f5d5
commit 5885dcbed3

View File

@@ -24,8 +24,11 @@ local function ipairsNilable(t)
local i = 0
return function()
i = i + 1
if i>maxk then return nil
else return i, t[i] end
if i > maxk then
return nil
else
return i, t[i]
end
end
end
@@ -133,17 +136,23 @@ local function forceValFromTs(val, typ)
end
local function vectorFromTs(val)
local xS, yS, zS = val:match('^(%-?[0-9%.e]+) (%-?[0-9%.e]+) (%-?[0-9%.e]+)$')
if xS then return vector{tonumber(xS),tonumber(yS),tonumber(zS)}
else return nil end
if xS then
return vector { tonumber(xS), tonumber(yS), tonumber(zS) }
else
return nil
end
end
local function boxFromTs(val)
local x1S, y1S, z1S, x2S, y2S, z2S = val:match(
'^(%-?[0-9%.e]+) (%-?[0-9%.e]+) (%-?[0-9%.e]+) ' ..
'(%-?[0-9%.e]+) (%-?[0-9%.e]+) (%-?[0-9%.e]+)$')
if x1S then return {
if x1S then
return {
vector { tonumber(x1S), tonumber(y1S), tonumber(z1S) },
vector { tonumber(x2S), tonumber(y2S), tonumber(z2S) } }
else return nil end
else
return nil
end
end
local function multinumericFromTs(val)
local tsNumPat = '%-?[0-9]+%.?[0-9]*e?[0-9]*'
@@ -173,7 +182,8 @@ bl._forceType = bl._forceType or {}
-- todo: ensure name and name2 are already lowercase
local function valFromTs(val, name, name2)
if type(val) ~= 'string' then
error('valFromTs: expected string, got '..type(val), 3) end
error('valFromTs: expected string, got ' .. type(val), 3)
end
if name then
name = name:lower()
if bl._forceType[name] then
@@ -218,15 +228,18 @@ end
local function classFromForceTypeStr(name)
local class, rest = name:match('^([a-zA-Z0-9_]+)(::.+)$')
if not class then
class, rest = name:match('^([a-zA-Z0-9_]+)(%..+)$') end
class, rest = name:match('^([a-zA-Z0-9_]+)(%..+)$')
end
return class, rest
end
local setForceType
setForceType = function(ftname, typ)
if typ ~= nil and not fromTsForceTypes[typ] then
error('bl.type: invalid type \''..typ..'\'', 2) end
error('bl.type: invalid type \'' .. typ .. '\'', 2)
end
if not isValidFuncNameNsArgn(ftname) then
error('bl.type: invalid function or variable name \''..ftname..'\'', 2) end
error('bl.type: invalid function or variable name \'' .. ftname .. '\'', 2)
end
ftname = ftname:lower()
@@ -277,15 +290,19 @@ local function tsIsFunction(name) return tsBool(_bllua_ts.call('isFunction', nam
local function tsIsFunctionNs(ns, name) return tsBool(_bllua_ts.call('isFunction', ns, name)) end
local function tsIsFunctionNsname(nsname)
local ns, name = nsname:match('^([^:]+)::([^:]+)$')
if ns then return tsIsFunctionNs(ns, name)
else return tsIsFunction(nsname) end
if ns then
return tsIsFunctionNs(ns, name)
else
return tsIsFunction(nsname)
end
end
-- sanity check to make sure objects that don't isObject are always marked as ._deleted
-- can be removed later
local function assertTsObjectExists(obj)
local is = tsIsObject(obj._tsObjectId)
if not is then
print('Warning: TS object :exists or isobject from lua but no longer exists') end
print('Warning: TS object :exists or isobject from lua but no longer exists')
end
return is
end
function bl.isObject(obj)
@@ -303,6 +320,7 @@ function bl.isObject(obj)
error('bl.isObject: argument #1: expected torque object, number, or string', 2)
end
end
function bl.isFunction(name)
return tsIsFunctionNsname(name)
end
@@ -317,9 +335,11 @@ local tsClassMeta = {
bl._objectUserMetas = bl._objectUserMetas or {}
function bl.class(cname, inhname)
if not (type(cname) == 'string' and isValidFuncName(cname)) then
error('bl.class: argument #1: invalid class name', 2) end
error('bl.class: argument #1: invalid class name', 2)
end
if not (inhname == nil or (type(inhname) == 'string' and isValidFuncName(inhname))) then
error('bl.class: argument #2: inherit name must be a string or nil', 2) end
error('bl.class: argument #2: inherit name must be a string or nil', 2)
end
cname = cname:lower()
local met = bl._objectUserMetas[cname] or {
@@ -334,15 +354,18 @@ function bl.class(cname, inhname)
inhname = inhname:lower()
local inh = bl._objectUserMetas[inhname]
if not inh then error('bl.class: argument #2: \''..inhname..'\' is not the '..
'name of an existing class', 2) end
if not inh then
error('bl.class: argument #2: \'' .. inhname .. '\' is not the ' ..
'name of an existing class', 2)
end
inh._children[cname] = true
local inhI = met._inherit
if inhI and inhI ~= inh then
error('bl.class: argument #2: class already exists and ' ..
'inherits a different parent.', 2) end
'inherits a different parent.', 2)
end
met._inherit = inh
-- apply inherited method and field types
@@ -354,6 +377,7 @@ function bl.class(cname, inhname)
end
end
end
local function objectInheritedMetas(name)
local inh = bl._objectUserMetas[name:lower()]
return function()
@@ -368,15 +392,18 @@ local tsObjectMeta = {
-- Return torque member function or value
__index = function(t, name)
if rawget(t, '_deleted') then
error('ts object index: object no longer exists', 2) end
error('ts object index: object no longer exists', 2)
end
if type(name) ~= 'string' and type(name) ~= 'number' then
error('ts object index: index must be a string or number', 2) end
error('ts object index: index must be a string or number', 2)
end
if getmetatable(t)[name] then
return getmetatable(t)[name]
elseif type(name) == 'number' then
if not tsIsFunctionNs(rawget(t, '_tsNamespace'), 'getObject') then
error('ts object __index: index is number, but object does not have ' ..
'getObject method', 2) end
'getObject method', 2)
end
return toTsObject(_bllua_ts.callobj(t._tsObjectId, 'getObject',
tostring(name)))
else
@@ -389,7 +416,8 @@ local tsObjectMeta = {
then
return function(t2, ...)
if t2 == nil or type(t2) ~= 'table' or not t2._tsObjectId then
error('ts object method: be sure to use :func() not .func()', 2) end
error('ts object method: be sure to use :func() not .func()', 2)
end
local argsS = arglistToTs({ ... })
local res =
_bllua_ts.callobj(t2._tsObjectId, name, unpack(argsS))
@@ -410,9 +438,11 @@ local tsObjectMeta = {
-- Use :set() to set Torque data
__newindex = function(t, name, val)
if rawget(t, '_deleted') then
error('ts object newindex: object no longer exists', 2) end
error('ts object newindex: object no longer exists', 2)
end
if type(name) ~= 'string' then
error('ts object newindex: index must be a string', 2) end
error('ts object newindex: index must be a string', 2)
end
rawset(t, name, val)
-- create strong reference since it's now storing lua data
bl._objectsStrong[rawget(t, '_tsObjectId')] = t
@@ -421,11 +451,14 @@ local tsObjectMeta = {
-- Use to set torque data
set = function(t, name, val)
if t == nil or type(t) ~= 'table' or not t._tsObjectId then
error('ts object method: be sure to use :func() not .func()', 2) end
error('ts object method: be sure to use :func() not .func()', 2)
end
if t._deleted then
error('ts object method: object no longer exists', 2) end
error('ts object method: object no longer exists', 2)
end
if type(name) ~= 'string' then
error('ts object :set(): index must be a string', 2) end
error('ts object :set(): index must be a string', 2)
end
_bllua_ts.setfield(t._tsObjectId, name, valToTs(val))
end,
-- __tostring: Called when printing
@@ -439,9 +472,11 @@ local tsObjectMeta = {
-- If the object has a getCount method, return its count
__len = function(t)
if t._deleted then
error('ts object __len: object no longer exists', 2) end
error('ts object __len: object no longer exists', 2)
end
if not tsIsFunctionNs(t._tsNamespace, 'getCount') then
error('ts object __len: object has no getCount method', 2) end
error('ts object __len: object has no getCount method', 2)
end
return tonumber(_bllua_ts.callobj(t._tsObjectId, 'getCount'))
end,
-- object:members()
@@ -449,14 +484,17 @@ local tsObjectMeta = {
-- for index, object in group:members() do ... end
members = function(t)
if t == nil then
error('ts object method: be sure to use :func() not .func()', 2) end
error('ts object method: be sure to use :func() not .func()', 2)
end
if t._deleted then
error('ts object method: object no longer exists', 2) end
error('ts object method: object no longer exists', 2)
end
if not (
tsIsFunctionNs(t._tsNamespace, 'getCount') and
tsIsFunctionNs(t._tsNamespace, 'getObject')) then
error('ts object :members() - ' ..
'Object does not have getCount and getObject methods', 2) end
'Object does not have getCount and getObject methods', 2)
end
local count = tonumber(_bllua_ts.callobj(t._tsObjectId, 'getCount'))
local idx = 0
return function()
@@ -474,23 +512,29 @@ local tsObjectMeta = {
-- Wrap some Torque functions for performance and error checking
getName = function(t)
if t == nil then
error('ts object method: be sure to use :func() not .func()', 2) end
error('ts object method: be sure to use :func() not .func()', 2)
end
if t._deleted then
error('ts object method: object no longer exists', 2) end
error('ts object method: object no longer exists', 2)
end
return t._tsName
end,
getId = function(t)
if t == nil then
error('ts object method: be sure to use :func() not .func()', 2) end
error('ts object method: be sure to use :func() not .func()', 2)
end
if t._deleted then
error('ts object method: object no longer exists', 2) end
error('ts object method: object no longer exists', 2)
end
return tonumber(t._tsObjectId)
end,
getType = function(t)
if t == nil then
error('ts object method: be sure to use :func() not .func()', 2) end
error('ts object method: be sure to use :func() not .func()', 2)
end
if t._deleted then
error('ts object method: object no longer exists', 2) end
error('ts object method: object no longer exists', 2)
end
return tsTypesByNum[_bllua_ts.callobj(t._tsObjectId, 'getType')]
end,
---- Schedule method for objects
@@ -512,7 +556,8 @@ local tsObjectMeta = {
--end,
exists = function(t)
if t == nil then
error('ts object method: be sure to use :func() not .func()', 2) end
error('ts object method: be sure to use :func() not .func()', 2)
end
if t._deleted then
return false
else
@@ -546,13 +591,15 @@ end
-- Return a Torque object for the object ID string, or create one if none exists
toTsObject = function(idiS)
if type(idiS) ~= 'string' then
error('toTsObject: input must be a string', 2) end
error('toTsObject: input must be a string', 2)
end
idiS = idiS:lower()
if bl._objectsWeak[idiS] then return bl._objectsWeak[idiS] end
if not tsIsObject(idiS) then
--error('toTsObject: object \''..idiS..'\' does not exist', 2) end
return nil end
return nil
end
local className = _bllua_ts.callobj(idiS, 'getClassName')
local obj = {
@@ -641,18 +688,22 @@ function bl.call(func, ...)
local argsS = arglistToTs(args)
return _bllua_ts.call(func, unpack(argsS))
end
function bl.eval(code)
local res = _bllua_ts.eval(code)
return valFromTs(res)
end
function bl.exec(file)
local res = _bllua_ts.call('exec', file)
return valFromTs(res)
end
function bl.array(name, ...)
local rest = { ... }
return name .. table.concat(rest, '_')
end
function bl.boolean(val)
return val ~= nil and
val ~= false and
@@ -660,6 +711,7 @@ function bl.boolean(val)
--val~='0' and
val ~= 0
end
function bl.object(id)
if type(id) == 'table' and id._tsObjectId then
return id
@@ -669,6 +721,7 @@ function bl.object(id)
error('bl.object: id must be a ts object, number, or string', 2)
end
end
function bl.string(val)
return valFromTsTostring(val)
end
@@ -679,24 +732,33 @@ luaLookup = function(tbl, name, set, val)
if name:find('%.') then
local first, rest = name:match('^([^%.:]+)%.(.+)$')
if not isValidFuncName(first) then
error('luaLookup: invalid name \''..tostring(first)..'\'', 3) end
error('luaLookup: invalid name \'' .. tostring(first) .. '\'', 3)
end
if tbl[first] == nil then
if set then tbl[first] = {}
else return nil end
if set then
tbl[first] = {}
else
return nil
end
end
return luaLookup(tbl[first], rest, set, val)
elseif name:find(':') then
local first, rest = name:match('^([^%.:]+):(.*)$')
if rest:find('[%.:]') then
error('luacall: cannot have : or . after :', 3) end
error('luacall: cannot have : or . after :', 3)
end
if not isValidFuncName(first) then
error('luacall: invalid name \''..tostring(first)..'\'', 3) end
error('luacall: invalid name \'' .. tostring(first) .. '\'', 3)
end
if not isValidFuncName(rest) then
error('luacall: invalid method name \''..tostring(first)..'\'', 3) end
error('luacall: invalid method name \'' .. tostring(first) .. '\'', 3)
end
if not tbl[first] then
error('luacall: no object named \''..rest..'\'', 3) end
error('luacall: no object named \'' .. rest .. '\'', 3)
end
if tbl[first][rest] == nil then
error('luacall: no method named \''..rest..'\'', 3) end
error('luacall: no method named \'' .. rest .. '\'', 3)
end
return function(...)
tbl[first][rest](tbl[first], ...)
end
@@ -713,19 +775,23 @@ function _bllua_call(fname, ...)
local args = arglistFromTs(fname:lower(), { ... }) -- todo: separate lua from ts func names?
local func = luaLookup(_G, fname)
if not func then
error('luacall: no global in lua named \''..fname..'\'', 2) end
error('luacall: no global in lua named \'' .. fname .. '\'', 2)
end
local res = func(unpack(args))
return valToTs(res)
end
function _bllua_getvar(vname)
return valToTs(luaLookup(_G, vname))
end
function _bllua_setvar(vname, valS)
return valToTs(luaLookup(_G, vname, true, valFromTs(valS, vname))) -- todo: separate lua from ts var names?
end
function _bllua_eval(code) return loadstring(code)() end
function _bllua_exec(fn) return dofile(fn, 2) end
function _bllua_eval(code) return loadstring(code)() end
function _bllua_exec(fn) return dofile(fn, 2) end
-- bl.schedule: Use TS's schedule function to schedule lua calls
-- bl.schedule(time, function, args...)
@@ -753,6 +819,7 @@ function bl.schedule(time, cb, ...)
bl._scheduleTable[id] = sch
return sch
end
function _bllua_schedule_callback(idS)
local id = tonumber(idS) or
error('_bllua_schedule_callback: invalid id: ' .. tostring(idS))
@@ -771,9 +838,11 @@ function _bllua_process_cmd(cmdS, ...)
if not func then error('_bllua_process_cmd: no cmd named \'' .. cmd .. '\'') end
func(unpack(args)) --pcall(func, unpack(args))
end
local function addCmd(cmd, func)
if not isValidFuncName(cmd) then
error('addCmd: invalid function name \''..tostring(cmd)..'\'') end
error('addCmd: invalid function name \'' .. tostring(cmd) .. '\'')
end
bl._cmds[cmd] = func
_bllua_ts.eval('function ' .. cmd .. '(' .. tsArgsLocal .. '){' ..
'_bllua_luacall(_bllua_process_cmd,"' .. cmd .. '",' .. tsArgsLocal .. ');}')
@@ -783,6 +852,7 @@ function bl.addServerCmd(name, func)
addCmd('servercmd' .. name, func)
bl._forceType['servercmd' .. name .. ':1'] = 'object'
end
function bl.addClientCmd(name, func)
name = name:lower()
addCmd('clientcmd' .. name, func)
@@ -794,12 +864,14 @@ function bl.commandToServer(cmd, ...)
_bllua_ts.call('addTaggedString', cmd),
unpack(arglistToTs({ ... })))
end
function bl.commandToClient(client, cmd, ...)
_bllua_ts.call('commandToClient',
valToTs(client),
_bllua_ts.call('addTaggedString', cmd),
unpack(arglistToTs({ ... })))
end
function bl.commandToAll(cmd, ...)
_bllua_ts.call('commandToAll',
_bllua_ts.call('addTaggedString', cmd),
@@ -831,7 +903,8 @@ function _bllua_process_hook_before(pkgS, nameS, ...)
local func = bl._hooks[pkgS] and bl._hooks[pkgS][nameS] and
bl._hooks[pkgS][nameS].before
if not func then
error('_bllua_process_hook_before: no hook for '..pkgs..':'..nameS) end
error('_bllua_process_hook_before: no hook for ' .. pkgs .. ':' .. nameS)
end
_bllua_ts.setvar('_bllua_hook_abort', '0')
func(args) --pcall(func, args)
if args._return then
@@ -842,6 +915,7 @@ function _bllua_process_hook_before(pkgS, nameS, ...)
_bllua_ts.setvar('_bllua_hook_arg' .. i, valToTs(args[i]))
end
end
function _bllua_process_hook_after(pkgS, nameS, resultS, ...)
nameS = nameS:lower()
local args = arglistFromTs(nameS, { ... })
@@ -849,10 +923,12 @@ function _bllua_process_hook_after(pkgS, nameS, resultS, ...)
local func = bl._hooks[pkgS] and bl._hooks[pkgS][nameS] and
bl._hooks[pkgS][nameS].after
if not func then
error('_bllua_process_hook_after: no hook for '..pkgs..':'..nameS) end
error('_bllua_process_hook_after: no hook for ' .. pkgs .. ':' .. nameS)
end
func(args) --pcall(func, args)
return valToTs(args._return)
end
local function updateHook(pkg, name, hk)
local beforeCode = hk.before and
('_bllua_luacall("_bllua_process_hook_before","' .. pkg .. '","' .. name ..
@@ -879,13 +955,17 @@ local function updateHook(pkg, name, hk)
end
function bl.hook(pkg, name, time, func)
if not isValidFuncName(pkg) then
error('bl.hook: argument #1: invalid package name \''..tostring(pkg)..'\'', 2) end
error('bl.hook: argument #1: invalid package name \'' .. tostring(pkg) .. '\'', 2)
end
if not isValidFuncNameNs(name) then
error('bl.hook: argument #2: invalid function name \''..tostring(name)..'\'', 2) end
error('bl.hook: argument #2: invalid function name \'' .. tostring(name) .. '\'', 2)
end
if time ~= 'before' and time ~= 'after' then
error('bl.hook: argument #3: time must be \'before\' or \'after\'', 2) end
error('bl.hook: argument #3: time must be \'before\' or \'after\'', 2)
end
if type(func) ~= 'function' then
error('bl.hook: argument #4: expected a function', 2) end
error('bl.hook: argument #4: expected a function', 2)
end
bl._hooks[pkg] = bl._hooks[pkg] or {}
bl._hooks[pkg][name] = bl._hooks[pkg][name] or {}
@@ -894,16 +974,20 @@ function bl.hook(pkg, name, time, func)
updateHook(pkg, name, bl._hooks[pkg][name])
activatePackage(pkg)
end
local function tableEmpty(t)
return next(t) ~= nil
end
function bl.unhook(pkg, name, time)
if not isValidFuncName(pkg) then
error('bl.unhook: argument #1: invalid package name \''..tostring(pkg)..'\'', 2) end
error('bl.unhook: argument #1: invalid package name \'' .. tostring(pkg) .. '\'', 2)
end
if name and not isValidFuncNameNs(name) then
error('bl.unhook: argument #2: invalid function name \''..tostring(name)..'\'', 2) end
error('bl.unhook: argument #2: invalid function name \'' .. tostring(name) .. '\'', 2)
end
if time and time ~= 'before' and time ~= 'after' then
error('bl.unhook: argument #3: time must be \'before\' or \'after\'', 2) end
error('bl.unhook: argument #3: time must be \'before\' or \'after\'', 2)
end
if not name then
if bl._hooks[pkg] then
@@ -927,7 +1011,8 @@ function bl.unhook(pkg, name, time)
updateHook(pkg, name, {})
else
if time ~= 'before' and time ~= 'after' then
error('bl.unhook: argument #3: time must be nil, \'before\', or \'after\'', 2) end
error('bl.unhook: argument #3: time must be nil, \'before\', or \'after\'', 2)
end
bl._hooks[pkg][name][time] = nil
if tableEmpty(bl._hooks[pkg][name]) then
bl._hooks[pkg][name] = nil
@@ -950,14 +1035,16 @@ end
-- Container search/raycast
local function vecToTs(v)
if not isTsVector(v) then
error('vecToTs: argument is not a vector', 3) end
error('vecToTs: argument is not a vector', 3)
end
return table.concat(v, ' ')
end
local function maskToTs(mask)
if type(mask) == 'string' then
local val = tsTypesByName[mask:lower()]
if not val then
error('maskToTs: invalid mask \''..mask..'\'', 3) end
error('maskToTs: invalid mask \'' .. mask .. '\'', 3)
end
return tostring(val)
elseif type(mask) == 'table' then
local tval = 0
@@ -967,7 +1054,8 @@ local function maskToTs(mask)
local val = tsTypesByName[v:lower()]
if not val then
error('maskToTs: invalid mask \'' .. v ..
'\' at index '..i..' in mask list', 3) end
'\' at index ' .. i .. ' in mask list', 3)
end
tval = tval + val
seen[v] = true
end
@@ -1009,6 +1097,7 @@ function bl.raycast(start, stop, mask, ignores)
return hit, pos, norm
end
end
local function tsContainerSearchIterator()
local retS = _bllua_ts.call('containerSearchNext')
if retS == '0' then
@@ -1025,10 +1114,12 @@ function bl.boxSearch(pos, size, mask)
_bllua_ts.call('initContainerBoxSearch', posS, sizeS, maskS)
return tsContainerSearchIterator
end
function bl.radiusSearch(pos, radius, mask)
local posS = vecToTs(pos)
if type(radius) ~= 'number' then
error('bl.radiusSearch: argument #2: radius must be a number', 2) end
error('bl.radiusSearch: argument #2: radius must be a number', 2)
end
local radiusS = tostring(radius)
local maskS = maskToTs(mask)
@@ -1066,7 +1157,8 @@ local function createTsObj(keyword, class, name, inherit, props)
if props then
for k, v in pairs(props) do
if not isValidFuncName(k) then
error('bl.new/bl.datablock: invalid property name \''..k..'\'') end
error('bl.new/bl.datablock: invalid property name \'' .. k .. '\'')
end
table.insert(propsT, k .. '="' .. valToTs(v) .. '";')
end
end
@@ -1077,7 +1169,8 @@ local function createTsObj(keyword, class, name, inherit, props)
table.concat(propsT) .. '};')
local obj = toTsObject(objS)
if not obj then
error('bl.new/bl.datablock: failed to create object', 3) end
error('bl.new/bl.datablock: failed to create object', 3)
end
return obj
end
@@ -1102,13 +1195,15 @@ local function parseTsDecl(decl)
(inherit == nil or isValidFuncName(inherit))) then
error('bl.new/bl.datablock: invalid decl \'' .. decl .. '\'\n' ..
'must be of the format: \'className\', \'className name\', ' ..
'\'className :inherit\', or \'className name:inherit\'', 3) end
'\'className :inherit\', or \'className name:inherit\'', 3)
end
return class, name, inherit
end
function bl.new(decl, props)
local class, name, inherit = parseTsDecl(decl)
return createTsObj('new', class, name, inherit, props)
end
function bl.datablock(decl, props)
local class, name, inherit = parseTsDecl(decl)
if not name then error('bl.datablock: must specify a name', 2) end