87 lines
1.7 KiB
Lua
87 lines
1.7 KiB
Lua
-- @name koptilnya/libs/utils
|
|
function checkVarClass(var, class, message)
|
|
if type(var) ~= 'table' or var['class'] == nil or not var:isInstanceOf(class) then
|
|
throw(message == nil and 'Wrong variable class.' or message, 1, true)
|
|
end
|
|
end
|
|
|
|
function accessorFunc(tbl, varName, name, defaultValue)
|
|
tbl[varName] = defaultValue
|
|
tbl['get' .. name] = function(self)
|
|
return self[varName]
|
|
end
|
|
tbl['set' .. name] = function(self, value)
|
|
self[varName] = value
|
|
end
|
|
end
|
|
|
|
function rotateAround(entity, pivot, angles)
|
|
local pos = entity:getPos()
|
|
local localPivotPos = entity:worldToLocal(pivot)
|
|
|
|
entity:setAngles(angles)
|
|
pos = pos + (pivot - entity:localToWorld(localPivotPos))
|
|
entity:setPos(pos)
|
|
end
|
|
|
|
function tobase(number, base)
|
|
local ret = ''
|
|
|
|
if base < 2 or base > 36 or number == 0 then
|
|
return '0'
|
|
end
|
|
|
|
if base == 10 then
|
|
return tostring(number)
|
|
end
|
|
|
|
local chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
|
local loops = 0
|
|
while number > 0 do
|
|
loops = loops + 1
|
|
number, d = math.floor(number / base), (number % base) + 1
|
|
ret = string.sub(chars, d, d) .. ret
|
|
if loops > 32000 then
|
|
break
|
|
end
|
|
end
|
|
return ret
|
|
end
|
|
|
|
function bytesToHex(bytes)
|
|
local hex = '0x'
|
|
|
|
for _, v in pairs(bytes) do
|
|
hex = hex .. bit.tohex(v, 2)
|
|
end
|
|
|
|
return hex
|
|
end
|
|
|
|
function byteTable(str, start, length)
|
|
local result = {}
|
|
|
|
for i = 1, length do
|
|
result[i] = string.byte(str, i + start - 1)
|
|
end
|
|
|
|
return result
|
|
end
|
|
|
|
function isURL(str)
|
|
local _1, _2, prefix = str:find('^(%w-):')
|
|
|
|
return prefix == 'http' or prefix == 'https' or prefix == 'data'
|
|
end
|
|
|
|
function debounce(func, delay)
|
|
local lastCall = 0
|
|
return function(...)
|
|
local now = timer.systime()
|
|
if now - lastCall >= delay then
|
|
lastCall = now
|
|
return func(...)
|
|
end
|
|
end
|
|
end
|