-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcommand-lua.xml
127 lines (114 loc) · 3.73 KB
/
command-lua.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?xml version="1.0" encoding="UTF-8"?>
<mod name="command-lua" version="1.0" author="slawkens, elf" contact="otland.net" enabled="yes">
<lib name="command-lua-lib"><![CDATA[
if(not table.append) then
table.append = table.insert
end
if(not table.totext) then
function table.totext(t, i, ingame)
i = i or 1
ingame = ingame or false
local indent = string.rep(ingame and string.rep(" ", 5) or "\t", i)
local maxn, str, l = table.maxn(t), "", 0
for k, v in pairs(t) do
l = l + 1
if type(k) == "string" then k = '"'..k..'"' end
str = str .. indent .. "[" .. k .. "] = "
if type(v) == "table" then
str = str .. "{\n" .. table.totext(v, i+1, ingame) .. "\n" .. indent .. "}"
elseif type(v) == 'boolean' then
str = str .. (v and "true" or "false")
elseif type(v) == "string" or type(v) == "number" then
if type(v) == "string" then v = '"' .. v .. '"' end
str = str .. v
else
str = str .. "_" .. type(v):upper() .. "_"
end
str = str .. (l<maxn and ",\n" or "")
end
return str
end
end
if(not table.serialize) then
function table.serialize(x, recur)
local t = type(x)
recur = recur or {}
if(t == nil) then
return "nil"
elseif(t == "string") then
return string.format("%q", x)
elseif(t == "number") then
return tostring(x)
elseif(t == "boolean") then
return t and "true" or "false"
elseif(getmetatable(x)) then
error("Can not serialize a table that has a metatable associated with it.")
elseif(t == "table") then
if(table.find(recur, x)) then
error("Can not serialize recursive tables.")
end
table.append(recur, x)
local s = "{"
for k, v in pairs(x) do
s = s .. "[" .. table.serialize(k, recur) .. "]"
s = s .. " = " .. table.serialize(v, recur) .. ","
end
s = s .. "}"
return s
else
error("Can not serialize value of type '" .. t .. "'.")
end
end
end
function doSecondsFormat(i)
local str, found = string.gsub(i, "(%d)(%d%d%d)$", "%1.%2", 1), 0
repeat
str, found = string.gsub(str, "(%d)(%d%d%d),", "%1.%2,", 1)
until found == 0
return str
end
]]></lib>
<talkaction words="/lua" access="5" event="script"><![CDATA[
domodlib('command-lua-lib')
function onSay(cid, words, param, channel)
local constants = {
["cid"] = cid,
["pos"] = getCreaturePosition(cid),
["c"] = "getCreatureByName"
}
for k, v in pairs(constants) do
param = k .. " = " .. (type(v) == 'string' and v or table.serialize(v)) .. " " .. param
end
local started = os.mtime and os.mtime() or os.time()
local f = loadstring(param)
local status, ret = pcall(f)
local t = type(ret)
if(table.isStrIn(t, {'number', 'string', 'boolean', 'table', 'function'})) then
if(ret == true or ret == false) then
ret = tostring(ret)
elseif(t == 'table') then
ret = '(table)\n' .. table.totext(ret, 1, true)
elseif(t == 'function') then
ret = 'function'
end
doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Result: " .. ret)
else
doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Code executed, without result.")
end
local done, str = os.time() - started, ""
if(os.mtime) then
done = os.mtime() - started
if(done < 100) then
str = "0.0" .. done
elseif(done < 1000) then
str = "0." .. done
else
str = doSecondsFormat(done)
if(str:len() == 0) then str = "0.0" end
end
end
doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Done in: " .. str .. "s")
return true
end
]]></talkaction>
</mod>