lots of tweaks, adding compatibility to 0.10.2

This commit is contained in:
Paul Liverman III 2017-07-26 21:15:05 -07:00
parent 7657f31fd1
commit 6ce2e48317
4 changed files with 198 additions and 146 deletions

View File

@ -1,6 +1,7 @@
The MIT License (MIT) The MIT License (MIT)
Copyright (c) 2015 devfirefly Copyright (c) 2015 devfirefly
Some modifications copyright (c) 2017 Guard13007
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
@ -19,4 +20,3 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.

View File

@ -37,7 +37,15 @@ currently reworking it.
Pie:mousepressed(...) Pie:mousepressed(...)
end end
``` ```
5) With sufficient output, press the "E" key to output to file. 5) With sufficient output, press the `E` key to output to file. Example output:
```
-----drawRectangles-----
source:@main.lua:20
current line: 22
time: 548.325
percentage: 98 %
----------------
```
# Keys # Keys
@ -49,7 +57,7 @@ up = decreases depth
down = increases depth down = increases depth
- = decreases step size \- = decreases step size
= = increases step size = = increases step size
@ -95,8 +103,8 @@ piefiller:setKey(table)
# For your own interpretation # For your own interpretation
If you wish to interpret the data on your own use `piefiller:unpack()`. If you wish to interpret the data on your own use `piefiller:unpack()`.
Output is a table as such: Output is a table as such:
```lua ```lua
data = { data = {

View File

@ -60,3 +60,13 @@ end
function love.mousepressed(...) function love.mousepressed(...)
profiler:mousepressed(...) profiler:mousepressed(...)
end end
-- temporarily allowing for old behavior in 0.10.2
function love.wheelmoved(x, y)
local X, Y = love.mouse.getPosition()
if y > 0 then
love.mousepressed(X, Y, "wu")
else
love.mousepressed(X, Y, "wd")
end
end

View File

@ -1,29 +1,34 @@
local path = ... local path = ...
local piefiller = {} local piefiller = {}
local function hsvToRgb(h, s, v) local function hsvToRgb(h, s, v)
local h,s,v = h,1,1 local i, f, p, q, t, r, g, b -- was polluting with globals
local h,s,v = math.fmod(h,360),s,v
if s==0 then return { v,v,v } end
h=h/60
i=math.floor(h)
f=h-i
p=v*(1-s)
q=v*(1-s*f)
t=v*(1-s*(1-f))
if i==0 then r=v g=t b=p -- local h,s,v = h,1,1 -- was ignoring s/v values entirely
elseif i==1 then r=q g=v b=p h = math.fmod(h, 360)
elseif i==2 then r=p g=v b=t
elseif i==3 then r=p g=q b=v
elseif i==4 then r=t g=p b=v
else r=v g=p b=q
end
r=r*255 if s == 0 then return {v, v, v} end
g=g*255 h=h/60
b=b*255 i=math.floor(h)
return { r,g,b } f=h-i
p=v*(1-s)
q=v*(1-s*f)
t=v*(1-s*(1-f))
if i==0 then r=v g=t b=p
elseif i==1 then r=q g=v b=p
elseif i==2 then r=p g=v b=t
elseif i==3 then r=p g=q b=v
elseif i==4 then r=t g=p b=v
else r=v g=p b=q
end
r = r * 255
g = g * 255
b = b * 255
return {r, g, b}
end end
local function copy(t) local function copy(t)
local ret = {} local ret = {}
for i,v in pairs(t) do for i,v in pairs(t) do
@ -34,58 +39,67 @@ local function copy(t)
end end
return ret return ret
end end
function setColor(...)
local function setColor(...) -- was global
local args = {...} local args = {...}
love.graphics.setColor(args[1] or 255,args[2] or 255,args[3] or 255,args[4] or 255) love.graphics.setColor(args[1] or 255, args[2] or 255, args[3] or 255, args[4] or 255)
end end
-- TODO verify these do not break if using multiple profilers
local color_data = {} local color_data = {}
local colors = {} local colors = {} -- the pool of available colors is removed from every time a new time is used
for i=0,300 do
table.insert( colors, hsvToRgb(i, 1, 1) )
end
function piefiller:new() function piefiller:new()
local self = {} local self = {}
setmetatable(self,{__index = piefiller}) setmetatable( self, {__index = piefiller} )
self.data = {} self.data = {}
self.parsed = {} self.parsed = {}
self.last = 0 self.last = 0
self.timer = 0 self.timer = 0
self:reset() self.depth = 2
self.depth = 2 self.small = false
self.small = false self.x = 0
self.x = 0 self.y = 0
self.y = 0 self.scale = 1
self.scale = 1 self.step = 1
self.step = 1 self.keys = {
self.keys = {
reset = "r", reset = "r",
increase_depth = "down", increase_depth = "down",
decrease_depth = "up", decrease_depth = "up",
increase_step_size = ",", -- TODO changing step_size should trigger a reset (by default, make it optional), because else the data is invalid
decrease_step_size = ".", increase_step_size = "=", -- changed default to "+" button
shorten_names = "s", decrease_step_size = "-", -- changed default to "-" button
show_hidden = "c", shorten_names = "z", -- changed default (based on assumption WASD is common control scheme)
save_to_file = "p", show_hidden = "h", -- changed default
save_to_file = "e", -- changed default -> e for export
show_profiler = "p", -- added new control to show/hide the profiler, default p
} }
return self return self
end end
function piefiller:reset() function piefiller:reset()
self.data = {} self.data = {}
-- why should these be reset? only the data matters
self.x = 0 self.x = 0
self.y = 0 self.y = 0
self.scale = 1 self.scale = 1
for i=0,300 do
table.insert(colors,hsvToRgb(i,100,100))
end
end end
function piefiller:setKey(table_or_mode,key)
if type(table_or_mode) == "table" then function piefiller:setKey(table_or_command,key)
self.keys = table_or_mode if type(table_or_command) == "table" then
for i,v in pairs(table_or_mode) do self.keys = table_or_command
for i,v in pairs(table_or_command) do
if self.keys[i] then self.keys[i] = v end if self.keys[i] then self.keys[i] = v end
end end
elseif type(table_or_mode) == "string" then elseif type(table_or_command) == "string" then
if not self.keys[table_or_mode] then error("Invalid mode: "..tostring(table_or_mode)) end if not self.keys[table_or_command] then error("Invalid command: "..tostring(table_or_command)) end
self.keys[table_or_mode] = key self.keys[table_or_command] = key
else else
error("Expected table or string got:"..type(table_or_mode)) error("Expected table or string, got: "..type(table_or_command))
end end
end end
@ -103,22 +117,24 @@ function piefiller:parse(caller,parent)
kids = {}, kids = {},
} }
end end
function piefiller:attach() function piefiller:attach()
self.last = os.clock() self.last = os.clock()
local function hook()
local depth = self.depth local function hook()
local caller = debug.getinfo(depth) local depth = self.depth
local taken = os.clock() - self.last local caller = debug.getinfo(depth)
local taken = os.clock() - self.last
if caller then if caller then
local last_caller local last_caller
local own = string.find(caller.source,path) local own = string.find(caller.source, path)
if caller.func ~= hook and not own then if caller.func ~= hook and not own then
while caller do while caller do
if last_caller and not self.view_children then if last_caller and not self.view_children then
local name = caller.func local name = caller.func
local lc = self.data[last_caller.func] local lc = self.data[last_caller.func]
if not lc.kids[name] then if not lc.kids[name] then
lc.kids[name] = self:parse(caller,last_caller) lc.kids[name] = self:parse(caller, last_caller)
end end
local kid = lc.kids[name] local kid = lc.kids[name]
kid.count = kid.count + 1 kid.count = kid.count + 1
@ -127,7 +143,7 @@ function piefiller:attach()
local name = caller.func local name = caller.func
local raw = self.data[name] local raw = self.data[name]
if not raw then if not raw then
self.data[name] = self:parse(caller,last_caller) self.data[name] = self:parse(caller, last_caller)
end end
raw = self.data[name] raw = self.data[name]
raw.count = raw.count + 1 raw.count = raw.count + 1
@ -139,54 +155,63 @@ function piefiller:attach()
end end
end end
end end
end end
local step = 10^self.step local step = 10^self.step
if self.step < 0 then if self.step < 0 then
step = 1/-self.step step = 1/-self.step
end end
debug.sethook(hook,"",step)
debug.sethook(hook, "", step)
end end
function piefiller:detach(stop)
local totaltime = 0 function piefiller:detach(stop) -- TODO figure out what stop is useful for
local parsed = {} local totaltime = 0
local no = 0 local parsed = {}
for i,v in pairs(self.data) do local no = 0
no = no + 1
totaltime = totaltime + v.time for i,v in pairs(self.data) do
local i = no no = no + 1
parsed[i] = {} totaltime = totaltime + v.time
parsed[i].name = v.info.name local i = no
parsed[i].time = v.time parsed[i] = {}
parsed[i].name = v.info.name
parsed[i].time = v.time
parsed[i].src = v.info.source parsed[i].src = v.info.source
parsed[i].def = v.info.linedefined parsed[i].def = v.info.linedefined
parsed[i].cur = v.info.currentline parsed[i].cur = v.info.currentline
parsed[i].item = v parsed[i].item = v
parsed[i].caller = v.info parsed[i].caller = v.info
if not color_data[v.func] then if not color_data[v.func] then
local i = math.random(#colors) local i = math.random(#colors)
color_data[v.func] = colors[i] color_data[v.func] = colors[i]
table.remove(colors,i) table.remove(colors, i)
end end
parsed[i].color = color_data[v.func] parsed[i].color = color_data[v.func]
end end
local prc = totaltime/100
for i,v in ipairs(parsed) do local prc = totaltime/100
parsed[i].prc = v.time/prc for i,v in ipairs(parsed) do
end parsed[i].prc = v.time/prc
self.parsed = parsed end
self.totaltime = totaltime self.parsed = parsed
self.totaltime = totaltime
if not stop then debug.sethook() end if not stop then debug.sethook() end
end end
local largeFont = love.graphics.newFont(25) local largeFont = love.graphics.newFont(25)
function piefiller:draw(args) function piefiller:draw(args)
local loading local loading
local oldFont = love.graphics.getFont() local oldFont = love.graphics.getFont()
local args = args or {} local args = args or {}
local rad = args["rad"] or 200 local rad = args.rad or 200
local mode = args["mode"] or "simple" -- these weren't even being used
local ret = args["return"] -- local mode = args["mode"] or "simple"
local pi = math.pi -- local ret = args["return"]
local pi = math.pi
local arc = love.graphics.arc local arc = love.graphics.arc
local w,h = love.graphics.getDimensions() local w,h = love.graphics.getDimensions()
@ -196,56 +221,60 @@ function piefiller:draw(args)
love.graphics.scale(self.scale) love.graphics.scale(self.scale)
if self.parsed and self.totaltime > 0 then if self.parsed and self.totaltime > 0 then
local lastangle = 0 local lastangle = 0
local font = love.graphics.getFont() local font = love.graphics.getFont()
for i,v in ipairs(self.parsed) do for i,v in ipairs(self.parsed) do
local color = v.color local color = v.color
local cx,cy = w/2,h/2 local cx,cy = w/2,h/2
local angle = math.rad(3.6*v.prc) local angle = math.rad(3.6*v.prc)
setColor(color) setColor(color)
arc("fill",cx,cy,rad,lastangle,lastangle + angle) arc("fill",cx,cy,rad,lastangle,lastangle + angle)
setColor(colors.black) -- setColor(colors.black) -- not defined, actually needs to be white
if v.prc > 1 then setColor(255, 255, 255, 255)
arc("line",cx,cy,rad,lastangle,lastangle + angle) if v.prc > 1 then
end arc("line",cx,cy,rad,lastangle,lastangle + angle)
lastangle = lastangle + angle end
setColor() lastangle = lastangle + angle
end setColor()
end
lastangle = 0 lastangle = 0
for i,v in ipairs(self.parsed) do for i,v in ipairs(self.parsed) do
local color = v.color
local color = v.color local cx,cy = w/2,h/2
local cx,cy = w/2,h/2 local angle = math.rad(3.6*v.prc)
local angle = math.rad(3.6*v.prc) local x = cx + rad * math.cos(lastangle + angle/2)
local x = cx + rad * math.cos(lastangle + angle/2) local y = cy + rad * math.sin(lastangle + angle/2)
local y = cy + rad * math.sin(lastangle + angle/2) if self.small then
if self.small then txt = tostring(v.src).." @: "..tostring(v.name)
txt = tostring(v.src).." @: "..tostring(v.name) else
else txt = tostring(math.ceil(v.prc)).." % "..tostring(v.name).." : "..tostring(v.src).." @: "..tostring(v.def)
txt = tostring(math.ceil(v.prc)).." % "..tostring(v.name).." : "..tostring(v.src).." @: "..tostring(v.def) end
end local fw = font:getWidth(txt)
local fw = font:getWidth(txt) local sx = 1
local sx = 1 if cx < x then
if cx < x then sx = -1
sx = -1 fw = 0
fw = 0 end
end if cy + rad/2 < y then
if cy + rad/2 < y then y = y + font:getHeight()
y = y + font:getHeight() elseif cy + rad/2 > y then
elseif cy + rad/2 > y then y = y - font:getHeight()
y = y - font:getHeight() end
end local ofx
local ofx love.graphics.print(txt,((x) + (-(fw+20))*sx),y)
love.graphics.print(txt,((x) + (-(fw+20))*sx),y) lastangle = lastangle + angle
lastangle = lastangle + angle
end end
else else
loading = true loading = true
end end
self.timer = self.timer + love.timer.getDelta() self.timer = self.timer + love.timer.getDelta()
if self.timer > 20 then if self.timer > 20 then
self.timer = 0 self.timer = 0
end end
love.graphics.setFont(largeFont) love.graphics.setFont(largeFont)
local t = "Depth: "..self.depth.." with step: "..self.step local t = "Depth: "..self.depth.." with step: "..self.step
local fw = largeFont:getWidth(t) local fw = largeFont:getWidth(t)
@ -261,6 +290,7 @@ function piefiller:draw(args)
love.graphics.setFont(oldFont) love.graphics.setFont(oldFont)
end end
function piefiller:mousepressed(x,y,b) function piefiller:mousepressed(x,y,b)
if b == "wu" then if b == "wu" then
local scale = self.scale - math.floor((0.05*self.scale)*1000)/1000 local scale = self.scale - math.floor((0.05*self.scale)*1000)/1000
@ -293,33 +323,34 @@ function piefiller:mousepressed(x,y,b)
end end
end end
end end
function piefiller:keypressed(key) function piefiller:keypressed(key)
local mode local command
for i,v in pairs(self.keys) do for i,v in pairs(self.keys) do
if key == v then if key == v then
mode = i command = i
break break
end end
end end
if mode then if command then
if mode == "reset" then if command == "reset" then
self:reset() self:reset()
elseif mode == "increase_depth" then elseif command == "increase_depth" then
self:reset() self:reset()
self.depth = self.depth + 1 self.depth = self.depth + 1
elseif mode == "decrease_depth" then elseif command == "decrease_depth" then
self:reset() self:reset()
self.depth = self.depth - 1 self.depth = self.depth - 1
elseif mode == "increase_step_size" then elseif command == "increase_step_size" then
self.step = self.step - 1 self.step = self.step - 1
elseif mode == "decrease_step_size" then elseif command == "decrease_step_size" then
self.step = self.step +1 self.step = self.step +1
elseif mode == "shorten_names" then elseif command == "shorten_names" then
self.small = not self.small self.small = not self.small
elseif mode == "show_hidden" then elseif command == "show_hidden" then
self:reset() self:reset()
self.view_children = not self.view_children self.view_children = not self.view_children
elseif mode == "save_to_file" then elseif command == "save_to_file" then
local parsed = copy(self.parsed) local parsed = copy(self.parsed)
table.sort(parsed,function(a,b) table.sort(parsed,function(a,b)
return a.prc > b.prc return a.prc > b.prc
@ -341,11 +372,12 @@ function piefiller:keypressed(key)
table.insert(d,table.concat(instance)) table.insert(d,table.concat(instance))
end end
local data = table.concat(d) local data = table.concat(d)
love.filesystem.write("Profile",data) love.filesystem.write("Profile.txt",data)
love.system.openURL(love.filesystem.getRealDirectory("Profile")) love.system.openURL(love.filesystem.getRealDirectory("Profile.txt"))
end end
end end
end end
function piefiller:unpack(fn) function piefiller:unpack(fn)
local data = { local data = {
items = {}, items = {},
@ -355,6 +387,7 @@ function piefiller:unpack(fn)
totalTime = self.totaltime, totalTime = self.totaltime,
}, },
} }
for i,v in ipairs(self.parsed) do for i,v in ipairs(self.parsed) do
local a = { local a = {
name = v.name, name = v.name,
@ -366,11 +399,12 @@ function piefiller:unpack(fn)
caller = v.caller, caller = v.caller,
} }
if fn then if fn then
assert(type(fn) == "function","Expected function got:"..type(fn)) assert(type(fn) == "function", "Expected function, got: "..type(fn))
fn(a) fn(a)
end end
table.insert(data.items,a) table.insert(data.items, a)
end end
return data return data
end end
return piefiller return piefiller