tibia-client/modules/core_lib/util.lua

156 lines
3.4 KiB
Lua
Raw Normal View History

2011-08-20 22:30:41 +02:00
function print(...)
local msg = ""
for i,v in ipairs(arg) do
msg = msg .. tostring(v) .. "\t"
end
g_logger.log(LogInfo, msg)
2011-08-21 03:01:46 +02:00
end
2011-12-07 01:31:55 +01:00
function fatal(msg)
g_logger.log(LogFatal, msg)
end
exit = g_app.exit
quit = g_app.exit
2012-01-07 06:35:50 +01:00
2011-12-05 19:27:07 +01:00
function connect(object, signalsAndSlots, pushFront)
for signal,slot in pairs(signalsAndSlots) do
2012-02-08 23:58:27 +01:00
if not object[signal] then
local mt = getmetatable(object)
if mt and type(object) == 'userdata' then
2012-02-08 23:58:27 +01:00
object[signal] = function(...)
return signalcall(mt[signal], ...)
end
end
end
if not object[signal] then
2012-01-24 23:22:56 +01:00
object[signal] = slot
elseif type(object[signal]) == 'function' then
2012-01-24 23:22:56 +01:00
object[signal] = { object[signal] }
end
if type(object[signal]) == 'table' then
if pushFront then
table.insert(object[signal], 1, slot)
else
table.insert(object[signal], #object[signal]+1, slot)
end
end
end
end
2011-11-01 19:32:48 +01:00
2012-02-06 20:19:47 +01:00
function disconnect(object, signalsAndSlots)
for signal,slot in pairs(signalsAndSlots) do
if not object[signal] then
elseif type(object[signal]) == 'function' then
if object[signal] == slot then
object[signal] = nil
end
elseif type(object[signal]) == 'table' then
for k,func in pairs(object[signal]) do
if func == slot then
table.remove(object[signal], k)
if #object[signal] == 1 then
object[signal] = object[signal][1]
end
break
end
end
end
end
end
2012-02-08 00:06:52 +01:00
function newclass()
local class = {}
function class.internalCreate()
local instance = {}
for k,v in pairs(class) do
instance[k] = v
end
return instance
end
class.create = class.internalCreate
return class
end
2012-01-05 02:28:29 +01:00
function extends(base)
local derived = {}
function derived.internalCreate()
local instance = base.create()
for k,v in pairs(derived) do
instance[k] = v
end
return instance
end
derived.create = derived.internalCreate
return derived
end
function newenv()
2011-12-07 01:31:55 +01:00
local env = { }
setmetatable(env, { __index = _G} )
return env
2011-11-01 19:32:48 +01:00
end
2011-11-17 21:40:31 +01:00
2012-01-05 02:28:29 +01:00
function getfsrcpath(depth)
2011-12-30 07:05:32 +01:00
depth = depth or 2
local info = debug.getinfo(1+depth, "Sn")
local path
if info.short_src then
path = info.short_src:match("(.*)/.*")
end
if not path then
path = '/'
elseif path:sub(0, 1) ~= '/' then
path = '/' .. path
end
return path
end
2012-01-05 02:28:29 +01:00
function resolvepath(filePath, depth)
2011-11-17 21:40:31 +01:00
depth = depth or 1
if filePath then
if filePath:sub(0, 1) ~= '/' then
local basepath = getfsrcpath(depth+1)
if basepath:sub(#basepath) ~= '/' then basepath = basepath .. '/' end
return basepath .. filePath
else
return filePath
end
2011-11-17 21:40:31 +01:00
else
local basepath = getfsrcpath(depth+1)
if basepath:sub(#basepath) ~= '/' then basepath = basepath .. '/' end
return basepath
2011-11-17 21:40:31 +01:00
end
end
2012-01-06 04:29:26 +01:00
function toboolean(str)
str = str:trim():lower()
if str == '1' or str == 'true' then
return true
end
return false
end
2012-01-07 01:46:41 +01:00
local oldtonumber = tonumber
function tonumber(v)
2012-01-07 01:52:59 +01:00
if v == nil then return 0 end
return oldtonumber(v)
2012-01-07 01:46:41 +01:00
end
2012-01-11 00:13:38 +01:00
function signalcall(param, ...)
if type(param) == 'function' then
return param(...)
elseif type(param) == 'table' then
for k,v in pairs(param) do
2012-02-08 23:58:27 +01:00
if v(...) then
2012-01-11 00:13:38 +01:00
return true
end
end
elseif func ~= nil then
error('attempt to call a non function value')
end
return false
end