Skip to content

Instantly share code, notes, and snippets.

@CheezusChrust
Created October 27, 2022 01:14
Show Gist options
  • Save CheezusChrust/e6fdb57d390a215d14e09d85b6820e2a to your computer and use it in GitHub Desktop.
Save CheezusChrust/e6fdb57d390a215d14e09d85b6820e2a to your computer and use it in GitHub Desktop.
Simple CPU monitor for Starfalls and E2s, spawn on a Starfall screen component to use
--@name tools/CPU Tracker
--@author Cheezus
--@shared
-- To use this chip, spawn it on a Starfall screen component
if SERVER then
if chip():isWeldedTo() then
chip():isWeldedTo():linkComponent(chip())
end
local function getPlayerChips()
local chips = {}
local ents = table.add(find.byClass("gmod_wire_expression2"), find.byClass("starfall_processor"))
for _, v in ipairs(ents) do
local owner = v:getOwner()
if isValid(owner) and v ~= chip() then
local ownerName = owner:getName()
chips[ownerName] = chips[ownerName] or {}
local cpu = 0
-- Catch SF error when trying to get usage of an E2 that's still uploading
-- starfall/libs_sh/entities.lua:899: attempt to index field 'context' (a nil value)
try(function()
cpu = v:getQuotaAverage() * 1000000
end)
table.insert(chips[ownerName], {
name = v:getChipName() or "",
cpu = cpu
})
end
end
return chips
end
timer.create("interval", 0.5, 0, function()
local chips = getPlayerChips()
net.start("sendChipData")
net.writeTable(chips)
net.send()
end)
else
render.createRenderTarget("bg")
render.createRenderTarget("list")
local headerFont = render.createFont("Roboto Mono", 36, nil, true)
local listFont = render.createFont("Roboto Mono", 28, nil, true)
local function drawToRT(rt, func)
hook.add("renderoffscreen", "drawToRT" .. rt, function()
render.selectRenderTarget(rt)
func()
render.selectRenderTarget()
hook.remove("renderoffscreen", "drawToRT" .. rt)
end)
end
drawToRT("bg", function()
render.clear(Color(50, 50, 50))
render.setColor(Color(200, 200, 200))
render.drawRect(0, 48, 1022, 3)
render.drawRect(512 - 80, 0, 3, 1024)
render.drawRect(512 + 80, 0, 3, 1024)
render.setFont(headerFont)
render.drawText(20, 12, "Player")
render.drawText(512, 12, "CPU (us)", 1)
render.drawText(600, 12, "Chip Name")
end)
drawToRT("list", function()
render.clear(Color(0, 0, 0, 0))
end)
net.receive("sendChipData", function()
local chips = net.readTable()
drawToRT("list", function()
render.clear(Color(0, 0, 0, 0))
local lineCount = 0
for _, d in pairs(chips) do
lineCount = lineCount + #d + 1
end
local row = 0
render.setFont(listFont)
for ply, plyChips in pairs(chips) do
local playerCpu = 0
for _, data in ipairs(plyChips) do
playerCpu = playerCpu + data.cpu
end
local playerIntensity = math.min(playerCpu / 1000, 1)
local playerColor = Color(60 - math.max(playerIntensity - 0.5, 0) * 120, playerIntensity * 2, 1):hsvToRGB()
if ply == "SANDERS" then
ply = "sanders sucks at coding"
end
render.setColor(playerColor)
render.drawText(6, row * 26 + 40, ply)
render.drawText(435, row * 26 + 40, "Total: " .. math.round(playerCpu))
local chipCount = #plyChips
if lineCount < 38 then
for _, data in ipairs(plyChips) do
row = row + 1
local chipIntensity = math.min(data.cpu / 1000, 1)
local chipColor = Color(60 - math.max(chipIntensity - 0.5, 0) * 120, chipIntensity * 2, 1):hsvToRGB()
local name = string.replace(data.name, "\n", " ")
if #name > 28 then
name = string.sub(name, 1, 28) .. "..."
end
render.setColor(chipColor)
render.drawText(589, row * 26 + 40, tostring(math.round(data.cpu)), 2)
render.drawText(605, row * 26 + 40, name)
end
else
local s = string.format("[%s chip%s spawned]", chipCount, chipCount > 1 and "s" or "")
render.drawText(605, row * 26 + 40, s)
end
render.setColor(Color(100, 100, 100))
render.drawRect(0, row * 26 + 67, 1024, 2)
row = row + 1
end
end)
end)
hook.add("render", "drawChipList", function()
render.setRenderTargetTexture("bg")
render.drawTexturedRectFast(0, 0, 512, 512)
render.setRenderTargetTexture("list")
render.drawTexturedRect(0, 0, 512, 512)
render.setRenderTargetTexture()
end)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment