-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathutil.lua
More file actions
77 lines (70 loc) · 2.04 KB
/
util.lua
File metadata and controls
77 lines (70 loc) · 2.04 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
local UTIL={}
local function timeUnit(t)
return t<10 and
("%7.2f ms"):format(t*1000) or
("%7.2f s"):format(t)
end
local timeList,lastTime={},love.timer.getTime()
---- `UTIL.time()` to start a timer (and clear the log)
---- `UTIL.time(msg)` to print a message plus time since previous call
---- `UTIL.time(msg,true)` doesn't print, print later with `UTIL.showTimeLog()`
---@param msg? string
---@param log? boolean
function UTIL.time(msg,log)
if not msg then
timeList={}
elseif log then
timeList[#timeList+1]={msg=msg,time=love.timer.getTime()-lastTime}
else
LOG('debug',("%s : %s"):format(msg,timeUnit(love.timer.getTime()-lastTime)))
end
lastTime=love.timer.getTime()
end
---Print info generated by `UTIL.time(msg,true)`
function UTIL.showTimeLog()
local maxLen=0
for i=1,#timeList do maxLen=math.max(maxLen,#timeList[i].msg) end
for i=1,#timeList do
local m=timeList[i]
LOG('debug',("%s : %s%s"):format(
m.msg,
(" "):rep(maxLen-#m.msg),
timeUnit(m.time)
))
end
end
---Set metatable for _G, print messages when a new variable is created
function UTIL.runVarMonitor()
setmetatable(_G,{__newindex=function(self,k,v)
print(">>"..k)
print(debug.traceback():match("\n.-\n\t(.-): "))
rawset(self,k,v)
end})
end
---Set Visible collectgarbage call
function UTIL.setCollectGarbageVisible()
local _gc=collectgarbage
collectgarbage=function()
_gc()
print(debug.traceback())
end
end
---Shortcut for `print(debug.traceback())`
---@param msg? string A message to print before the traceback
function UTIL.trace(msg)
print(debug.traceback(msg or "DEBUG",2))
end
---Try to open the console
---@return boolean success
function UTIL.openConsole()
if love['_openConsole'] then
love['_openConsole']()
return true
end
return false
end
---Try to open the save directory
function UTIL.openSaveDirectory()
love.system.openURL(love.filesystem.getSaveDirectory())
end
return UTIL