vanilla-wow-addons – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | --------------------------------------------------------------------------------------------------- |
2 | -- Name: QuestieUtils |
||
3 | -- Description: Utility Functions |
||
4 | --------------------------------------------------------------------------------------------------- |
||
5 | --///////////////////////////////////////////////////////////////////////////////////////////////-- |
||
6 | --------------------------------------------------------------------------------------------------- |
||
7 | -- todo give these functions a QuestieUtils: in front |
||
8 | function print_r ( t ) |
||
9 | local print_r_cache={} |
||
10 | local function sub_print_r(t,indentAmount) |
||
11 | if (print_r_cache[tostring(t)]) then |
||
12 | questieDebugPrint(string.rep(" ", indentAmount).."*"..tostring(t)) |
||
13 | else |
||
14 | print_r_cache[tostring(t)]=true |
||
15 | if (type(t)=="table") then |
||
16 | for pos,val in pairs(t) do |
||
17 | if (type(val)=="table") then |
||
18 | questieDebugPrint(string.rep(" ", indentAmount).."["..pos.."] => "..tostring(t).." {") |
||
19 | if next(val) then |
||
20 | sub_print_r(val,indentAmount+1) |
||
21 | end |
||
22 | questieDebugPrint(string.rep(" ", indentAmount).."}") |
||
23 | elseif (type(val)=="string") then |
||
24 | questieDebugPrint(string.rep(" ", indentAmount).."["..pos..'] => "'..val..'"') |
||
25 | else |
||
26 | questieDebugPrint(string.rep(" ", indentAmount).."["..pos.."] => "..tostring(val)) |
||
27 | end |
||
28 | end |
||
29 | else |
||
30 | questieDebugPrint(string.rep(" ", indentAmount)..tostring(t)) |
||
31 | end |
||
32 | end |
||
33 | end |
||
34 | if (type(t)=="table") then |
||
35 | questieDebugPrint(tostring(t).." {") |
||
36 | sub_print_r(t,1) |
||
37 | questieDebugPrint("}") |
||
38 | else |
||
39 | sub_print_r(t,1) |
||
40 | end |
||
41 | questieDebugPrint() |
||
42 | end |
||
43 | --------------------------------------------------------------------------------------------------- |
||
44 | function deepcopy(orig) |
||
45 | local orig_type = type(orig) |
||
46 | local copy |
||
47 | if orig_type == 'table' then |
||
48 | copy = {} |
||
49 | for orig_key, orig_value in next, orig, nil do |
||
50 | copy[deepcopy(orig_key)] = deepcopy(orig_value) |
||
51 | end |
||
52 | setmetatable(copy, deepcopy(getmetatable(orig))) |
||
53 | else -- number, string, boolean, etc |
||
54 | copy = orig |
||
55 | end |
||
56 | return copy |
||
57 | end |
||
58 | --------------------------------------------------------------------------------------------------- |
||
59 | -- Debug print function |
||
60 | --------------------------------------------------------------------------------------------------- |
||
61 | -- todo make existing usages of Questie:debug_Print use this function instead |
||
62 | function questieDebugPrint(...) |
||
63 | local debugWin = 0; |
||
64 | local name, shown; |
||
65 | for i=1, NUM_CHAT_WINDOWS do |
||
66 | name,_,_,_,_,_,shown = GetChatWindowInfo(i); |
||
67 | if (string.lower(name) == "questiedebug") then debugWin = i; break; end |
||
68 | end |
||
69 | if (debugWin == 0) then return end |
||
70 | local out = ""; |
||
71 | for i = 1, arg.n, 1 do |
||
72 | if (i > 1) then out = out .. ", "; end |
||
73 | local t = type(arg[i]); |
||
74 | if (t == "string") then |
||
75 | out = out .. '"'..arg[i]..'"'; |
||
76 | elseif (t == "number") then |
||
77 | out = out .. arg[i]; |
||
78 | else |
||
79 | out = out .. dump(arg[i]); |
||
80 | end |
||
81 | end |
||
82 | getglobal("ChatFrame"..debugWin):AddMessage(out, 1.0, 1.0, 0.3); |
||
83 | end |
||
84 | --------------------------------------------------------------------------------------------------- |
||
85 | -- Get numeric value from string |
||
86 | --------------------------------------------------------------------------------------------------- |
||
87 | function GetNumberFromString(arg) |
||
88 | if type(arg) == "string" then |
||
89 | local n |
||
90 | for x in string.gfind(arg, "%d+") do n = x; end |
||
91 | n = tonumber(n); |
||
92 | return n |
||
93 | end |
||
94 | return nil |
||
95 | end |
||
96 | --------------------------------------------------------------------------------------------------- |
||
97 | -- Simple Rounding Function |
||
98 | --------------------------------------------------------------------------------------------------- |
||
99 | function round(x) |
||
100 | return floor(x+0.5) |
||
101 | end |