-
Notifications
You must be signed in to change notification settings - Fork 4
/
common.lua
310 lines (272 loc) · 6.33 KB
/
common.lua
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
function triggerName(event)
return string.format('rcore_preset:%s',event)
end
function dprint(str,...)
if Config.Debug then
print('[rcore_preset] '..string.format(str,...))
end
end
Keys = {
["ESC"] = 322,
["F1"] = 288,
["F2"] = 289,
["F3"] = 170,
["F5"] = 166,
["F6"] = 167,
["F7"] = 168,
["F8"] = 169,
["F9"] = 56,
["F10"] = 57,
["~"] = 243,
["1"] = 157,
["2"] = 158,
["3"] = 160,
["4"] = 164,
["5"] = 165,
["6"] = 159,
["7"] = 161,
["8"] = 162,
["9"] = 163,
["-"] = 84,
["="] = 83,
["BACKSPACE"] = 177,
["TAB"] = 37,
["Q"] = 44,
["W"] = 32,
["E"] = 38,
["R"] = 45,
["T"] = 245,
["Y"] = 246,
["U"] = 303,
["P"] = 199,
["["] = 39,
["]"] = 40,
["ENTER"] = 18,
["CAPS"] = 137,
["A"] = 34,
["S"] = 8,
["D"] = 9,
["F"] = 23,
["G"] = 47,
["H"] = 74,
["K"] = 311,
["L"] = 182,
["LEFTSHIFT"] = 21,
["Z"] = 20,
["X"] = 73,
["C"] = 26,
["V"] = 0,
["B"] = 29,
["N"] = 249,
["M"] = 244,
[","] = 82,
["."] = 81,
["LEFTCTRL"] = 36,
["LEFTALT"] = 19,
["SPACE"] = 22,
["RIGHTCTRL"] = 70,
["HOME"] = 213,
["PAGEUP"] = 10,
["PAGEDOWN"] = 11,
["DELETE"] = 178,
["LEFT"] = 174,
["RIGHT"] = 175,
["TOP"] = 27,
["DOWN"] = 173,
["NENTER"] = 201,
["N4"] = 108,
["N5"] = 60,
["N6"] = 107,
["N+"] = 96,
["N-"] = 97,
["N7"] = 117,
["N8"] = 61,
["N9"] = 118
}
-- Stolen from: https://rosettacode.org/wiki/Strip_control_codes_and_extended_characters_from_a_string
function normalizeString(str)
local s = ""
for i = 1, str:len() do
if str:byte(i) >= 32 and str:byte(i) <= 126 then
s = s .. str:sub(i, i)
end
end
return s
end
exports('normalizeString', normalizeString)
-- Stolen from: https://forums.coronalabs.com/topic/43048-remove-special-characters-from-string/
function urlencode(str)
if (str) then
str = string.gsub(str, "\n", "\r\n")
str = string.gsub(str, "([^%w ])",
function()
return string.format("%%%02X", string.byte)
end)
str = string.gsub(str, " ", "+")
end
return str
end
exports('urlencode', urlencode)
function round(num, numDecimalPlaces)
local mult = 10 ^ (numDecimalPlaces or 0)
return math.floor(num * mult + 0.5) / mult
end
exports('round', round)
--- @param object object
--- stolen: https://forums.coronalabs.com/topic/27482-copy-not-direct-reference-of-table/
function deepCopy(object)
local lookup_table = {}
local function _copy(object)
if type(object) ~= "table" then
return object
elseif lookup_table[object] then
return lookup_table[object]
end
local new_table = {}
lookup_table[object] = new_table
for index, value in pairs(object) do
new_table[_copy(index)] = _copy(value)
end
return setmetatable(new_table, getmetatable(object))
end
return _copy(object)
end
exports('deepCopy', deepCopy)
--- @param sourceTable table
--- @param targetTable table
--- @return table
--- stolen: https://stackoverflow.com/questions/1283388/lua-merge-tables
function mergeTables(t1, t2)
local target = deepCopy(t1)
local source = deepCopy(t2)
for k, v in pairs(source) do
if (type(v) == "table") and (type(target[k] or false) == "table") then
mergeTables(target[k], source[k])
else
target[k] = v
end
end
return target
end
exports('mergeTables', mergeTables)
function mergeParams(options, defaults)
local target = deepCopy(defaults)
local source = deepCopy(options)
for k, v in pairs(source) do
target[k] = v
end
return target
end
--- @param table table
--- @return boolean
function emptyTable(table)
if isTable(table) then
if next(table) == nil then
return true
else
return false
end
else
return true
end
end
exports('emptyTable', emptyTable)
--- @param table table
--- @return boolean
function isTable(table)
if table ~= nil then
if type(table) == "table" then
return true
end
return false
else
return false
end
end
exports('isTable', isTable)
--- @param func function
--- @return boolean
function isFunction(func)
if table ~= nil then
if type(func) == "function" then
return true
end
return false
else
return false
end
end
exports('isFunction', isFunction)
function getKeys()
return Keys
end
exports('getKeys', getKeys)
--- @param table table
--- @return number
function tableLength(tb)
local count = 0
if isTable(tb) then
for _ in pairs(tb) do
count = count + 1
end
return count
end
return nil
end
exports('tableLength', tableLength)
--- @param table table
--- @return number
function tableLastIterator(table)
local last = 0
for i, v in pairs(table) do
last = i
end
return last
end
exports('tableLastIterator', tableLastIterator)
function getConfig()
return Config
end
exports('getConfig', getConfig)
--Taken from ESX
function dumpTable(table, nb)
if nb == nil then
nb = 0
end
if type(table) == 'table' then
local s = ''
for i = 1, nb + 1, 1 do
s = s .. " "
end
s = '{\n'
for k, v in pairs(table) do
if type(k) ~= 'number' then
k = '"' .. k .. '"'
end
for i = 1, nb, 1 do
s = s .. " "
end
s = s .. '[' .. k .. '] = ' .. dumpTable(v, nb + 1) .. ',\n'
end
for i = 1, nb, 1 do
s = s .. " "
end
return s .. '}'
else
return tostring(table)
end
end
exports('dumpTable', dumpTable)
function triggerName(event)
return string.format('rcore:%s', event)
end
function escapeHtml(str)
str = string.gsub( str, '&', '&' )
str = string.gsub( str, '<', '<' )
str = string.gsub( str, '>', '>' )
str = string.gsub( str, '"', '"' )
str = string.gsub( str, "'", ''' )
str = string.gsub( str, "/", '/' )
return str
end
exports('escapeHtml', escapeHtml)