-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutilities.lua
More file actions
executable file
·288 lines (239 loc) · 8.16 KB
/
utilities.lua
File metadata and controls
executable file
·288 lines (239 loc) · 8.16 KB
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
-- utilities.lua
-- catch-all file for miscellaneous global utility functions
-------------------------------------------------------------------------------
-- Module Loading
-------------------------------------------------------------------------------
local ADDON_NAME, ADDON_SYMBOL_TABLE = ...
ADDON_SYMBOL_TABLE.Wormhole() -- Lua voodoo magic that replaces the current Global namespace with the ADDON_SYMBOL_TABLE
-------------------------------------------------------------------------------
-- Utility Functions
-------------------------------------------------------------------------------
sprintf = string.format
function registerSlashCmd(cmdName, callbacks)
_G["SLASH_"..cmdName.."1"] = "/" .. cmdName
if not callbacks.help then
local helpFunc = function()
for name, cmd in pairs(callbacks) do
if cmd.desc then
print("/"..cmdName .. " " .. name.. " - " .. cmd.desc)
end
end
end
callbacks.help = { fnc = helpFunc }
end
SlashCmdList[cmdName] = function(arg)
if isEmpty(arg) then
arg = "help"
end
local cmd = callbacks[arg]
if not cmd then
msgUser(L10N.SLASH_UNKNOWN_COMMAND .. ": \"".. arg .."\"")
else
msgUser(arg .."...")
local func = cmd.fnc
func()
end
end
end
function msgUser(msg)
if not ADDON_SYMBOL_TABLE.myNameInColor then
ADDON_SYMBOL_TABLE.myNameInColor = zebug.info:colorize(ADDON_NAME)
end
print(ADDON_SYMBOL_TABLE.myNameInColor .. ": " .. msg)
end
function isInCombatLockdown(actionDescription)
if InCombatLockdown() then
local msg = actionDescription or "That action"
zebug.info:print(msg .. " is not allowed during combat.")
return true
else
return false
end
end
function getIdForCurrentToon()
local name, realm = UnitFullName("player") -- FU Bliz, realm is arbitrarily nil sometimes but not always
realm = GetRealmName()
return name.."-"..realm
end
-- I had to create this function to replace lua's strjoin() because
-- lua poops the bed in the strsplit(strjoin(array)) roundtrip whenever the "array" is actually a table because an element was set to nil
function fknJoin(array)
array = array or {}
local n = lastIndex(array)
local omfgDumbAssLanguage = {}
for i=1,n,1 do
omfgDumbAssLanguage[i] = array[i] or EMPTY_ELEMENT
end
local result = strjoin(DELIMITER,unpack(omfgDumbAssLanguage,1,n)) or ""
return result
end
-- because lua arrays turn into tables when an element = nil
function lastIndex(table)
local biggest = 0
for k,v in pairs(table) do
if (k > biggest) then
biggest = k
end
end
return biggest
end
-- ensures then special characters introduced by fknJoin()
function fknSplit(str)
local omfgDumbassLanguage = { strsplit(DELIMITER, str or "") }
omfgDumbassLanguage = stripEmptyElements(omfgDumbassLanguage)
return omfgDumbassLanguage
end
function stripEmptyElements(table)
for k,v in ipairs(table) do
if (v == EMPTY_ELEMENT) then
table[k] = nil
end
end
return table
end
function deepcopy(src, target)
local orig_type = type(src)
local copy
if orig_type == 'table' then
copy = target or {}
for orig_key, orig_value in next, src, nil do
copy[deepcopy(orig_key)] = deepcopy(orig_value)
end
--setmetatable(copy, deepcopy(getmetatable(src)))
else -- number, string, boolean, etc
copy = src
end
return copy
end
local next = next
function isTableNotEmpty(table)
return table and ( next(table) ~= nil )
end
function isTableEmpty(table)
return not isTableNotEmpty(table)
end
function isEmpty(s)
return s == nil or s == ''
end
function exists(s)
return not isEmpty(s)
end
function deleteFromArray(array, killTester)
local j = 1
local modified = false
for i=1,#array do
if (killTester(array[i])) then
array[i] = nil
modified = true
else
-- Move i's kept value to j's position, if it's not already there.
if (i ~= j) then
array[j] = array[i]
array[i] = nil
end
j = j + 1 -- Increment position of where we'll place the next kept value.
end
end
return modified
end
function moveElementInArray(array, oldPos, newPos)
if oldPos == newPos then return false end
zebug.info:line(80)
-- iterate through the array in whichever direction will encounter the oldPos first and newPos last
-- ahhh, I feel like I'm writing C code again... flashback to 1994... I'm old :-/
local forward = oldPos < newPos
local start = forward and 1 or #array
local last = forward and #array or 1
local inc = forward and 1 or -1
local nomad = array[oldPos]
zebug.info:print("moving",nomad, "from", oldPos, "to",newPos)
zebug.info:dumpy("ORIGINAL array", array)
local inMoverMode
for i=start,last,inc do
if i == oldPos then
inMoverMode = true
elseif i == newPos then
array[i] = nomad
zebug.info:dumpy("shifted array", array)
return true
end
if inMoverMode then
array[i] = array[i + inc]
end
end
return true
end
-- convert data structures into JSON-like strings
-- useful for injecting tables into secure functions because SFs don't allow tables
function serialize(val, name, skipnewlines, depth)
skipnewlines = skipnewlines or false
depth = depth or 0
local tmp = string.rep(" ", depth)
if name then tmp = tmp .. name .. " = " end
if type(val) == "table" then
tmp = tmp .. "{" .. (not skipnewlines and "\n" or "")
for k, v in pairs(val) do
tmp = tmp .. serialize(v, k, skipnewlines, depth + 1) .. "," .. (not skipnewlines and "\n" or "")
end
tmp = tmp .. string.rep(" ", depth) .. "}"
elseif type(val) == "number" or type(val) == "boolean" then
tmp = tmp .. tostring(val)
elseif type(val) == "string" then
tmp = tmp .. string.format("%q", val)
else
tmp = tmp .. "\"[inserializeable datatype:" .. type(val) .. "]\""
end
return tmp
end
-- convert data structures into lines of Lua variable assignments
-- useful for injecting tables into secure functions because SFs don't allow tables
-- TODO: can I use this instead of the "asLists" solution? Or would it produce gigantic strings?
function serializeAsAssignments(name, val, isRecurse)
assert(val, ADDON_NAME..": val is required")
assert(name, ADDON_NAME..": name is required")
local tmp
if isRecurse then
tmp = ""
else
tmp = "local "
end
tmp = tmp .. name .. " = "
local typ = type(val)
if "table" == typ then
tmp = tmp .. "{}" .. EOL
-- trust that if there is an index #1 then all other indices are also numbers. Otherwise, this will fail.
local iterFunc = val[1] and ipairs or pairs
for k, v in iterFunc(val) do
if type(k) ~= "number" then
k = string.format("%q", k)
end
local nextName = name .. "["..k.."]"
tmp = tmp .. serializeAsAssignments(nextName, v, true)
end
elseif "number" == typ or "boolean" == typ then
tmp = tmp .. tostring(val) .. EOL
elseif "string" == typ then
tmp = tmp .. string.format("%q", val) .. EOL
else
tmp = tmp .. QUOTE .. "INVALID" .. QUOTE .. EOL
end
return tmp
end
function isClass(firstArg, class)
assert(class, ADDON_NAME..": nil is not a Class")
return (firstArg and type(firstArg) == "table" and firstArg.className == class.className)
end
function assertIsFunctionOf(firstArg, class)
assert(not isClass(firstArg, class), ADDON_NAME..": Um... it's var.foo() not var:foo()")
end
function assertIsMethodOf(firstArg, class)
assert(isClass(firstArg, class), ADDON_NAME..": Um... it's var:foo() not var.foo()")
end
function sortedKeys(table)
local keys = {}
for k,v in pairs(table) do
keys[#keys +1] = k
end
table.sort(keys)
return keys
end