Update libbl.lua

This commit is contained in:
2025-10-06 10:01:54 -04:00
parent e309e2427b
commit 66ed695010

View File

@@ -118,7 +118,8 @@ end
bl._forceType = bl._forceType or {} bl._forceType = bl._forceType or {}
local function valFromTs(val, name, name2) -- todo: ensure name and name2 are already lowercase local function valFromTs(val, name, name2) -- todo: ensure name and name2 are already lowercase
if type(val) ~= 'string' then 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 if name then
name = name:lower() name = name:lower()
if bl._forceType[name] then if bl._forceType[name] then
@@ -143,9 +144,11 @@ local function valFromTs(val, name, name2) -- todo: ensure name and name2 are al
'^(%-?[0-9%.e]+) (%-?[0-9%.e]+) (%-?[0-9%.e]+) ' .. '^(%-?[0-9%.e]+) (%-?[0-9%.e]+) (%-?[0-9%.e]+) ' ..
'(%-?[0-9%.e]+) (%-?[0-9%.e]+) (%-?[0-9%.e]+)$') '(%-?[0-9%.e]+) (%-?[0-9%.e]+) (%-?[0-9%.e]+)$')
-- box (2 vectors) -- box (2 vectors)
if x1S then return { if x1S then
return {
vector { tonumber(x1S), tonumber(y1S), tonumber(z1S) }, vector { tonumber(x1S), tonumber(y1S), tonumber(z1S) },
vector{tonumber(x2S),tonumber(y2S),tonumber(z2S)} } end vector { tonumber(x2S), tonumber(y2S), tonumber(z2S) } }
end
-- string -- string
return val return val
end end
@@ -162,15 +165,18 @@ end
local function classFromForceTypeStr(name) local function classFromForceTypeStr(name)
local class, rest = name:match('^([a-zA-Z0-9_]+)(::.+)$') local class, rest = name:match('^([a-zA-Z0-9_]+)(::.+)$')
if not class then 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 return class, rest
end end
local setForceType local setForceType
setForceType = function(ftname, typ) setForceType = function(ftname, typ)
if typ ~= nil and not fromTsForceTypes[typ] then 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 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() ftname = ftname:lower()
@@ -201,8 +207,11 @@ 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 tsIsFunctionNs(ns, name) return tsBool(_bllua_ts.call('isFunction', ns, name)) end
local function tsIsFunctionNsname(nsname) local function tsIsFunctionNsname(nsname)
local ns, name = nsname:match('^([^:]+)::([^:]+)$') local ns, name = nsname:match('^([^:]+)::([^:]+)$')
if ns then return tsIsFunctionNs(ns, name) if ns then
else return tsIsFunction(nsname) end return tsIsFunctionNs(ns, name)
else
return tsIsFunction(nsname)
end
end end
function bl.isObject(obj) function bl.isObject(obj)
@@ -215,12 +224,15 @@ function bl.isObject(obj)
end end
return tsIsObject(obj) return tsIsObject(obj)
end end
function bl.isFunction(a1, a2) function bl.isFunction(a1, a2)
if type(a1) ~= 'string' then if type(a1) ~= 'string' then
error('bl.isFunction: argument #1: expected string', 2) end error('bl.isFunction: argument #1: expected string', 2)
end
if a2 then if a2 then
if type(a2) ~= 'string' then if type(a2) ~= 'string' then
error('bl.isFunction: argument #2: expected string', 2) end error('bl.isFunction: argument #2: expected string', 2)
end
return tsIsFunctionNs(a1, a2) return tsIsFunctionNs(a1, a2)
else else
return tsIsFunction(a1) return tsIsFunction(a1)
@@ -237,9 +249,11 @@ local tsClassMeta = {
bl._objectUserMetas = bl._objectUserMetas or {} bl._objectUserMetas = bl._objectUserMetas or {}
function bl.class(cname, inhname) function bl.class(cname, inhname)
if not (type(cname) == 'string' and isValidFuncName(cname)) then 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 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() cname = cname:lower()
local met = bl._objectUserMetas[cname] or { local met = bl._objectUserMetas[cname] or {
@@ -254,15 +268,18 @@ function bl.class(cname, inhname)
inhname = inhname:lower() inhname = inhname:lower()
local inh = bl._objectUserMetas[inhname] local inh = bl._objectUserMetas[inhname]
if not inh then error('bl.class: argument #2: \''..inhname..'\' is not the '.. if not inh then
'name of an existing class', 2) end error('bl.class: argument #2: \'' .. inhname .. '\' is not the ' ..
'name of an existing class', 2)
end
inh._children[cname] = true inh._children[cname] = true
local inhI = met._inherit local inhI = met._inherit
if inhI and inhI ~= inh then if inhI and inhI ~= inh then
error('bl.class: argument #2: class already exists and ' .. error('bl.class: argument #2: class already exists and ' ..
'inherits a different parent.', 2) end 'inherits a different parent.', 2)
end
met._inherit = inh met._inherit = inh
-- apply inherited method and field types -- apply inherited method and field types
@@ -274,6 +291,7 @@ function bl.class(cname, inhname)
end end
end end
end end
local function objectInheritedMetas(name) local function objectInheritedMetas(name)
local inh = bl._objectUserMetas[name:lower()] local inh = bl._objectUserMetas[name:lower()]
return function() return function()
@@ -288,15 +306,18 @@ local tsObjectMeta = {
-- Return torque member function or value -- Return torque member function or value
__index = function(t, name) __index = function(t, name)
if rawget(t, '_deleted') then 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 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 if getmetatable(t)[name] then
return getmetatable(t)[name] return getmetatable(t)[name]
elseif type(name) == 'number' then elseif type(name) == 'number' then
if not tsIsFunctionNs(rawget(t, '_tsNamespace'), 'getObject') then if not tsIsFunctionNs(rawget(t, '_tsNamespace'), 'getObject') then
error('ts object __index: index is number, but object does not have ' .. 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', return toTsObject(_bllua_ts.callobj(t._tsObjectId, 'getObject',
tostring(name))) tostring(name)))
else else
@@ -328,9 +349,11 @@ local tsObjectMeta = {
-- Use :set() to set Torque data -- Use :set() to set Torque data
__newindex = function(t, name, val) __newindex = function(t, name, val)
if rawget(t, '_deleted') then 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 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) rawset(t, name, val)
-- create strong reference since it's now storing lua data -- create strong reference since it's now storing lua data
bl._objectsStrong[rawget(t, '_tsObjectId')] = t bl._objectsStrong[rawget(t, '_tsObjectId')] = t
@@ -339,11 +362,14 @@ local tsObjectMeta = {
-- Use to set torque data -- Use to set torque data
set = function(t, name, val) set = function(t, name, val)
if t == nil or type(t) ~= 'table' or not t._tsObjectId then 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 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 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)) _bllua_ts.setfield(t._tsObjectId, name, valToTs(val))
end, end,
-- __tostring: Called when printing -- __tostring: Called when printing
@@ -356,9 +382,11 @@ local tsObjectMeta = {
-- If the object has a getCount method, return its count -- If the object has a getCount method, return its count
__len = function(t) __len = function(t)
if t._deleted then 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 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')) return tonumber(_bllua_ts.callobj(t._tsObjectId, 'getCount'))
end, end,
-- object:members() -- object:members()
@@ -366,14 +394,17 @@ local tsObjectMeta = {
-- for index, object in group:members() do ... end -- for index, object in group:members() do ... end
members = function(t) members = function(t)
if t == nil then 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 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 ( if not (
tsIsFunctionNs(t._tsNamespace, 'getCount') and tsIsFunctionNs(t._tsNamespace, 'getCount') and
tsIsFunctionNs(t._tsNamespace, 'getObject')) then tsIsFunctionNs(t._tsNamespace, 'getObject')) then
error('ts object :members() - ' .. 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 count = tonumber(_bllua_ts.callobj(t._tsObjectId, 'getCount'))
local idx = 0 local idx = 0
return function() return function()
@@ -390,23 +421,29 @@ local tsObjectMeta = {
-- Wrap some Torque functions for performance and error checking -- Wrap some Torque functions for performance and error checking
getName = function(t) getName = function(t)
if t == nil then 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 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 return t._tsName
end, end,
getId = function(t) getId = function(t)
if t == nil then 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 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) return tonumber(t._tsObjectId)
end, end,
getType = function(t) getType = function(t)
if t == nil then 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 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')] return tsTypesByNum[_bllua_ts.callobj(t._tsObjectId, 'getType')]
end, end,
---- Schedule method for objects ---- Schedule method for objects
@@ -428,9 +465,11 @@ local tsObjectMeta = {
--end, --end,
exists = function(t) exists = function(t)
if t == nil then 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 if t._deleted then
return false end return false
end
return tsIsObject(t._tsObjectId) return tsIsObject(t._tsObjectId)
end, end,
} }
@@ -460,13 +499,15 @@ end
-- Return a Torque object for the object ID string, or create one if none exists -- Return a Torque object for the object ID string, or create one if none exists
toTsObject = function(idiS) toTsObject = function(idiS)
if type(idiS) ~= 'string' then 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() idiS = idiS:lower()
if bl._objectsWeak[idiS] then return bl._objectsWeak[idiS] end if bl._objectsWeak[idiS] then return bl._objectsWeak[idiS] end
if not tsBool(_bllua_ts.call('isObject', idiS)) then if not tsBool(_bllua_ts.call('isObject', idiS)) then
--error('toTsObject: object \''..idiS..'\' does not exist', 2) end --error('toTsObject: object \''..idiS..'\' does not exist', 2) end
return nil end return nil
end
local className = _bllua_ts.callobj(idiS, 'getClassName') local className = _bllua_ts.callobj(idiS, 'getClassName')
local obj = { local obj = {
@@ -554,12 +595,15 @@ function bl.call(func, ...)
local argsS = arglistToTs(args) local argsS = arglistToTs(args)
return _bllua_ts.call(func, unpack(argsS)) return _bllua_ts.call(func, unpack(argsS))
end end
function bl.eval(code) function bl.eval(code)
return valFromTs(_bllua_ts.eval(code)) return valFromTs(_bllua_ts.eval(code))
end end
function bl.exec(file) function bl.exec(file)
return valFromTs(_bllua_ts.call('exec', file)) return valFromTs(_bllua_ts.call('exec', file))
end end
function bl.boolean(val) function bl.boolean(val)
return val ~= nil and return val ~= nil and
val ~= false and val ~= false and
@@ -567,6 +611,7 @@ function bl.boolean(val)
--val~='0' and --val~='0' and
val ~= 0 val ~= 0
end end
function bl.object(id) function bl.object(id)
if type(id) == 'table' and id._tsObjectId then if type(id) == 'table' and id._tsObjectId then
return id return id
@@ -576,14 +621,17 @@ function bl.object(id)
error('bl.object: id must be a ts object, number, or string', 2) error('bl.object: id must be a ts object, number, or string', 2)
end end
end end
function bl.array(name, ...) function bl.array(name, ...)
local rest = { ... } local rest = { ... }
return name .. table.concat(rest, '_') return name .. table.concat(rest, '_')
end end
function _bllua_call(fnameS, ...) function _bllua_call(fnameS, ...)
local args = arglistFromTs('lua:' .. fnameS:lower(), { ... }) -- todo: allow this though bl.type local args = arglistFromTs('lua:' .. fnameS:lower(), { ... }) -- todo: allow this though bl.type
if not _G[fnameS] then if not _G[fnameS] then
error('luacall: no global lua function named \''..fnameS..'\'') end error('luacall: no global lua function named \'' .. fnameS .. '\'')
end
-- todo: library fields and object methods -- todo: library fields and object methods
local res = _G[fnameS](unpack(args)) local res = _G[fnameS](unpack(args))
return valToTs(res) return valToTs(res)
@@ -615,6 +663,7 @@ function bl.schedule(time, cb, ...)
bl._scheduleTable[id] = sch bl._scheduleTable[id] = sch
return sch return sch
end end
function _bllua_schedule_callback(idS) function _bllua_schedule_callback(idS)
local id = tonumber(idS) or local id = tonumber(idS) or
error('_bllua_schedule_callback: invalid id: ' .. tostring(idS)) error('_bllua_schedule_callback: invalid id: ' .. tostring(idS))
@@ -633,9 +682,11 @@ function _bllua_process_cmd(cmdS, ...)
if not func then error('_bllua_process_cmd: no cmd named \'' .. cmd .. '\'') end if not func then error('_bllua_process_cmd: no cmd named \'' .. cmd .. '\'') end
func(unpack(args)) --pcall(func, unpack(args)) func(unpack(args)) --pcall(func, unpack(args))
end end
local function addCmd(cmd, func) local function addCmd(cmd, func)
if not isValidFuncName(cmd) then 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 bl._cmds[cmd] = func
local arglist = '%a,%b,%c,%d,%e,%f,%g,%h,%i,%j,%k,%l,%m,%n,%o,%p' local arglist = '%a,%b,%c,%d,%e,%f,%g,%h,%i,%j,%k,%l,%m,%n,%o,%p'
_bllua_ts.eval('function ' .. cmd .. '(' .. arglist .. '){' .. _bllua_ts.eval('function ' .. cmd .. '(' .. arglist .. '){' ..
@@ -646,6 +697,7 @@ function bl.addServerCmd(name, func)
addCmd('servercmd' .. name, func) addCmd('servercmd' .. name, func)
bl._forceType['servercmd' .. name .. ':1'] = 'object' bl._forceType['servercmd' .. name .. ':1'] = 'object'
end end
function bl.addClientCmd(name, func) function bl.addClientCmd(name, func)
name = name:lower() name = name:lower()
addCmd('clientcmd' .. name, func) addCmd('clientcmd' .. name, func)
@@ -657,11 +709,13 @@ function bl.commandToServer(cmd, ...)
_bllua_ts.call('addTaggedString', cmd), _bllua_ts.call('addTaggedString', cmd),
unpack(arglistToTs({ ... }))) unpack(arglistToTs({ ... })))
end end
function bl.commandToClient(cmd, ...) function bl.commandToClient(cmd, ...)
_bllua_ts.call('commandToClient', _bllua_ts.call('commandToClient',
_bllua_ts.call('addTaggedString', cmd), _bllua_ts.call('addTaggedString', cmd),
unpack(arglistToTs({ ... }))) unpack(arglistToTs({ ... })))
end end
function bl.commandToAll(cmd, ...) function bl.commandToAll(cmd, ...)
_bllua_ts.call('commandToAll', _bllua_ts.call('commandToAll',
_bllua_ts.call('addTaggedString', cmd), _bllua_ts.call('addTaggedString', cmd),
@@ -689,14 +743,16 @@ local function deactivatePackage(pkg)
end end
local hookNargs = 8 local hookNargs = 8
local hookArglistLocal = '%a,%b,%c,%d,%e,%f,%g,%h' local hookArglistLocal = '%a,%b,%c,%d,%e,%f,%g,%h'
local hookArglistGlobal = '$_bllua_hook_arg1,$_bllua_hook_arg2,$_bllua_hook_arg3,$_bllua_hook_arg4,$_bllua_hook_arg5,$_bllua_hook_arg6,$_bllua_hook_arg7,$_bllua_hook_arg8' local hookArglistGlobal =
'$_bllua_hook_arg1,$_bllua_hook_arg2,$_bllua_hook_arg3,$_bllua_hook_arg4,$_bllua_hook_arg5,$_bllua_hook_arg6,$_bllua_hook_arg7,$_bllua_hook_arg8'
bl._hooks = bl._hooks or {} bl._hooks = bl._hooks or {}
function _bllua_process_hook_before(pkgS, nameS, ...) function _bllua_process_hook_before(pkgS, nameS, ...)
local args = arglistFromTs(nameS, { ... }) local args = arglistFromTs(nameS, { ... })
local func = bl._hooks[pkgS] and bl._hooks[pkgS][nameS] and local func = bl._hooks[pkgS] and bl._hooks[pkgS][nameS] and
bl._hooks[pkgS][nameS].before bl._hooks[pkgS][nameS].before
if not func then 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') _bllua_ts.setvar('_bllua_hook_abort', '0')
func(args) --pcall(func, args) func(args) --pcall(func, args)
if args._return then if args._return then
@@ -707,6 +763,7 @@ function _bllua_process_hook_before(pkgS, nameS, ...)
_bllua_ts.setvar('_bllua_hook_arg' .. i, valToTs(args[i])) _bllua_ts.setvar('_bllua_hook_arg' .. i, valToTs(args[i]))
end end
end end
function _bllua_process_hook_after(pkgS, nameS, resultS, ...) function _bllua_process_hook_after(pkgS, nameS, resultS, ...)
nameS = nameS:lower() nameS = nameS:lower()
local args = arglistFromTs(nameS, { ... }) local args = arglistFromTs(nameS, { ... })
@@ -714,10 +771,12 @@ function _bllua_process_hook_after(pkgS, nameS, resultS, ...)
local func = bl._hooks[pkgS] and bl._hooks[pkgS][nameS] and local func = bl._hooks[pkgS] and bl._hooks[pkgS][nameS] and
bl._hooks[pkgS][nameS].after bl._hooks[pkgS][nameS].after
if not func then 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) func(args) --pcall(func, args)
return valToTs(args._return) return valToTs(args._return)
end end
local function updateHook(pkg, name, hk) local function updateHook(pkg, name, hk)
local beforeCode = hk.before and local beforeCode = hk.before and
('_bllua_luacall("_bllua_process_hook_before", "' .. pkg .. '","' .. name .. ('_bllua_luacall("_bllua_process_hook_before", "' .. pkg .. '","' .. name ..
@@ -743,13 +802,17 @@ local function updateHook(pkg, name, hk)
end end
function bl.hook(pkg, name, time, func) function bl.hook(pkg, name, time, func)
if not isValidFuncName(pkg) then 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 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 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 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] = bl._hooks[pkg] or {}
bl._hooks[pkg][name] = bl._hooks[pkg][name] or {} bl._hooks[pkg][name] = bl._hooks[pkg][name] or {}
@@ -758,16 +821,20 @@ function bl.hook(pkg, name, time, func)
updateHook(pkg, name, bl._hooks[pkg][name]) updateHook(pkg, name, bl._hooks[pkg][name])
activatePackage(pkg) activatePackage(pkg)
end end
local function tableEmpty(t) local function tableEmpty(t)
return next(t) ~= nil return next(t) ~= nil
end end
function bl.unhook(pkg, name, time) function bl.unhook(pkg, name, time)
if not isValidFuncName(pkg) then 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 not isValidFuncNameNs(name) then if 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 ~= 'before' and time ~= 'after' then if 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 not name then
if bl._hooks[pkg] then if bl._hooks[pkg] then
@@ -791,7 +858,8 @@ function bl.unhook(pkg, name, time)
updateHook(pkg, name, {}) updateHook(pkg, name, {})
else else
if time ~= 'before' and time ~= 'after' then 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 bl._hooks[pkg][name][time] = nil
if tableEmpty(bl._hooks[pkg][name]) and tableEmpty(bl._hooks[pkg]) then if tableEmpty(bl._hooks[pkg][name]) and tableEmpty(bl._hooks[pkg]) then
bl._hooks[pkg] = nil bl._hooks[pkg] = nil
@@ -811,14 +879,16 @@ end
-- Container search/raycast -- Container search/raycast
local function vecToTs(v) local function vecToTs(v)
if not isTsVector(v) then 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, ' ') return table.concat(v, ' ')
end end
local function maskToTs(mask) local function maskToTs(mask)
if type(mask) == 'string' then if type(mask) == 'string' then
local val = tsTypesByName[mask:lower()] local val = tsTypesByName[mask:lower()]
if not val then if not val then
error('maskToTs: invalid mask \''..mask..'\'', 3) end error('maskToTs: invalid mask \'' .. mask .. '\'', 3)
end
return tostring(val) return tostring(val)
elseif type(mask) == 'table' then elseif type(mask) == 'table' then
local tval = 0 local tval = 0
@@ -828,7 +898,8 @@ local function maskToTs(mask)
local val = tsTypesByName[v:lower()] local val = tsTypesByName[v:lower()]
if not val then if not val then
error('maskToTs: invalid mask \'' .. v .. error('maskToTs: invalid mask \'' .. v ..
'\' at index '..i..' in mask list', 3) end '\' at index ' .. i .. ' in mask list', 3)
end
tval = tval + val tval = tval + val
seen[v] = true seen[v] = true
end end
@@ -870,6 +941,7 @@ function bl.raycast(start, stop, mask, ignores)
return hit, pos, norm return hit, pos, norm
end end
end end
local function tsContainerSearchIterator() local function tsContainerSearchIterator()
local retS = _bllua_ts.call('containerSearchNext') local retS = _bllua_ts.call('containerSearchNext')
if retS == '0' then if retS == '0' then
@@ -886,10 +958,12 @@ function bl.boxSearch(pos, size, mask)
_bllua_ts.call('initContainerBoxSearch', posS, sizeS, maskS) _bllua_ts.call('initContainerBoxSearch', posS, sizeS, maskS)
return tsContainerSearchIterator return tsContainerSearchIterator
end end
function bl.radiusSearch(pos, radius, mask) function bl.radiusSearch(pos, radius, mask)
local posS = vecToTs(pos) local posS = vecToTs(pos)
if type(radius) ~= 'number' then 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 radiusS = tostring(radius)
local maskS = maskToTs(mask) local maskS = maskToTs(mask)
@@ -927,7 +1001,8 @@ local function createTsObj(keyword, class, name, inherit, props)
if props then if props then
for k, v in pairs(props) do for k, v in pairs(props) do
if not isValidFuncName(k) then 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) .. '";') table.insert(propsT, k .. '="' .. valToTs(v) .. '";')
end end
end end
@@ -938,7 +1013,8 @@ local function createTsObj(keyword, class, name, inherit, props)
table.concat(propsT) .. '};') table.concat(propsT) .. '};')
local obj = toTsObject(objS) local obj = toTsObject(objS)
if not obj then 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 return obj
end end
@@ -963,13 +1039,15 @@ local function parseTsDecl(decl)
(inherit == nil or isValidFuncName(inherit))) then (inherit == nil or isValidFuncName(inherit))) then
error('bl.new/bl.datablock: invalid decl \'' .. decl .. '\'\n' .. error('bl.new/bl.datablock: invalid decl \'' .. decl .. '\'\n' ..
'must be of the format: \'className\', \'className name\', ' .. '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 return class, name, inherit
end end
function bl.new(decl, props) function bl.new(decl, props)
local class, name, inherit = parseTsDecl(decl) local class, name, inherit = parseTsDecl(decl)
return createTsObj('new', class, name, inherit, props) return createTsObj('new', class, name, inherit, props)
end end
function bl.datablock(decl, props) function bl.datablock(decl, props)
local class, name, inherit = parseTsDecl(decl) local class, name, inherit = parseTsDecl(decl)
if not name then error('bl.datablock: must specify a name', 2) end if not name then error('bl.datablock: must specify a name', 2) end