Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local key_col = Color(0,255,255)
- local str_col = Color(255,255,0)
- local vec_ang_col = Color(255,0,255)
- local ent_col = Color(0,255,0)
- local func_col = Color(0,0,255)
- local white = Color(255,255,255)
- local red = Color(255,0,0)
- local green = Color(0,255,0)
- local function print_var(no_recurse, longest_tbl, longest, indentation_level, indent, k, v, no_recurse)
- local pretty_spacing = (" "):rep(math.max(longest, longest_tbl) - utf8.len(tostring(k)))
- if (type(v) == "table") then
- if (IsColor(v)) then
- if (v.a < 255) then
- MsgC(indent, key_col, k, white, pretty_spacing, " = ", v, "Color(r: " .. v.r .. ", g: " .. v.b .. ", b: " .. v.g .. ", a: " .. v.a .. ")\n")
- else
- MsgC(indent, key_col, k, white, pretty_spacing, " = ", v, "Color(r: " .. v.r .. ", g: " .. v.b .. ", b: " .. v.g .. ")\n")
- end
- else
- if (no_recurse ~= nil and no_recurse[v]) then
- MsgC(indent, key_col, k, white, pretty_spacing, " = ", red, "[recursive table]\n")
- else
- local new_no_recurse = table.Copy(no_recurse or {})
- new_no_recurse[v] = true
- if (next(v) == nil) then
- MsgC(indent, key_col, k, white, pretty_spacing, " = ", str_col, "[0] ", key_col, "{}\n")
- else
- MsgC(indent, key_col, k, white, " = ", str_col, "[" .. table.Count(v) .. "] ", key_col, "{\n")
- var_dump(k, v, indentation_level + 1, new_no_recurse)
- MsgC(indent, key_col, "}\n")
- end
- end
- end
- elseif (type(v) == "string") then
- MsgC(indent, key_col, k, white, pretty_spacing, " = ", str_col, "\"" .. v .. "\"\n")
- elseif (isvector(v)) then
- MsgC(indent, key_col, k, white, pretty_spacing, " = ", vec_ang_col, "Vector(x: " .. v.x .. ", y: " .. v.y .. ", z: " .. v.z .. ")\n")
- elseif (isangle(v)) then
- MsgC(indent, key_col, k, white, pretty_spacing, " = ", vec_ang_col, "Angle(y: " .. v.y .. ", p: " .. v.p .. ", r: " .. v.r .. ")\n")
- elseif (isentity(v)) then
- if (not IsValid(v)) then
- MsgC(indent, key_col, k, white, pretty_spacing, " = ", red, "[INVALID]", ent_col, tostring(v), "\n")
- else
- if (v:IsWorld()) then
- MsgC(indent, key_col, k, white, pretty_spacing, " = ", ent_col, "[0] worldspawn\n")
- elseif (v:IsPlayer()) then
- if (v:IsBot()) then
- MsgC(indent, key_col, k, white, pretty_spacing, " = ", team.GetColor(v:Team()), "[BOT #" .. v:SteamID64() - 90071996842377216 + 1 .. "] " .. v:Nick() .. "\n")
- else
- MsgC(indent, key_col, k, white, pretty_spacing, " = ", team.GetColor(v:Team()), "[" .. v:SteamID() .. " #" .. v:UserID() .. "] " .. v:Nick() .. "\n")
- end
- elseif (v.GetPrintName ~= nil) then
- MsgC(indent, key_col, k, white, pretty_spacing, " = ", ent_col, "[" .. v:EntIndex() .. "] [" .. v:GetClass() .. "] " .. v:GetPrintName() .. "\n")
- else
- MsgC(indent, key_col, k, white, pretty_spacing, " = ", ent_col, "[" .. v:EntIndex() .. "] " .. v:GetClass() .. "\n")
- end
- end
- elseif (isfunction(v)) then
- local params = ""
- for i=1,debug.getinfo(v).nparams do
- params = params .. i .. ", "
- end
- MsgC(indent, key_col, k, white, pretty_spacing, " = ", func_col, "[" .. (tostring(v):gsub("^function: ", "")) .. "] function(" .. (params:gsub(", $", "")) .. ")" .. "\n")
- elseif (type(v) == "number") then
- MsgC(indent, key_col, k, white, pretty_spacing, " = ", str_col, tostring(v) .. "\n")
- elseif (type(v) == "boolean") then
- if (v == true) then
- MsgC(indent, key_col, k, white, pretty_spacing, " = ", green, tostring(v) .. "\n")
- else
- MsgC(indent, key_col, k, white, pretty_spacing, " = ", red, tostring(v) .. "\n")
- end
- else
- MsgC(indent, key_col, k, white, pretty_spacing, " = ", str_col, tostring(v) .. "\n")
- end
- end
- function var_dump(str, tbl, indentation_level, no_recurse)
- local indentation_level = indentation_level or 0
- local indent = (" "):rep(indentation_level * 4)
- if (tbl == nil) then return MsgC(indent, red, "Input was nil\n") end
- if (type(tbl) == "table") then
- local no_recurse = no_recurse or {}
- local keys = table.GetKeys(tbl)
- table.sort(keys, function(a, b)
- if (isnumber(a) and isnumber(b)) then return a < b end
- return tostring(a) < tostring(b)
- end)
- local longest = 0
- local longest_tbl = 0
- for _,k in ipairs(keys) do
- local len = utf8.len(tostring(k))
- if (len > longest) then
- longest = len
- end
- if (len > longest_tbl and type(tbl[k]) == "table") then
- longest_tbl = len
- end
- end
- for _,k in ipairs(keys) do
- local v = tbl[k]
- print_var(no_recurse, longest_tbl, longest, indentation_level, indent, k, v, no_recurse)
- end
- else
- print_var(nil, 0, 0, indentation_level, indent, str, tbl, no_recurse)
- end
- end
- concommand.Add("var_dump", function(ply, __, ___, args)
- if (SERVER and IsValid(ply)) then return end
- local worked = xpcall(function() RunString("var_dump(\"" .. (args:gsub("\"", "\\\"")) .. "\"," .. args .. ")") end, function() end)
- if (not worked) then
- MsgC(red, "Failed\n")
- end
- end)
Add Comment
Please, Sign In to add comment