aboutsummaryrefslogtreecommitdiff
path: root/src/ext.lua
diff options
context:
space:
mode:
authorAlexander Pickering <alex@cogarr.net>2020-02-02 08:11:08 -0500
committerAlexander Pickering <alex@cogarr.net>2020-02-02 08:11:08 -0500
commit57701059b1b65fc08366318e92d32d9dd7094d25 (patch)
treea569db68d27982d83fead3cc9c8192056c49509f /src/ext.lua
downloaddrydock-57701059b1b65fc08366318e92d32d9dd7094d25.tar.gz
drydock-57701059b1b65fc08366318e92d32d9dd7094d25.tar.bz2
drydock-57701059b1b65fc08366318e92d32d9dd7094d25.zip
inital commit
Diffstat (limited to 'src/ext.lua')
-rw-r--r--src/ext.lua72
1 files changed, 72 insertions, 0 deletions
diff --git a/src/ext.lua b/src/ext.lua
new file mode 100644
index 0000000..85d4ef7
--- /dev/null
+++ b/src/ext.lua
@@ -0,0 +1,72 @@
+-- Override tostring to display more info about the table
+local old_tostring = tostring
+local numtabs = 0
+local printed_tables = {}
+--print = log
+--
+ --for obj in *@physobjs
+ --bp.add(t,obj.offset.x,obj.offset.y,obj.size.x,obj.size.y)
+
+local function tostring_helper(el)
+ assert(type(el) == "table", "Tried to call helper with something that was not a table, it was a " .. type(el))
+ local mt = getmetatable(el)
+ if mt and mt.__tostring then
+ return mt.__tostring(el)
+ elseif printed_tables[el] == true then
+ return old_tostring(el)
+ else
+ printed_tables[el] = true
+ numtabs = numtabs + 1
+ local strbuilder = {"{"}
+ for k,v in pairs(el) do
+ local key,value
+ if type(k) == "table" then
+ key = tostring_helper(k)
+ else
+ key = old_tostring(k)
+ end
+ if type(v) == "table" then
+ value = tostring_helper(v)
+ else
+ value = old_tostring(v)
+ end
+ strbuilder[#strbuilder + 1] = string.format("%s%s : %s", string.rep("\t",numtabs), key, value)
+ end
+ strbuilder[#strbuilder + 1] = string.rep("\t",numtabs - 1) .. "}"
+ numtabs = numtabs - 1
+ return table.concat(strbuilder,"\n")
+ end
+
+end
+function tostring(el)
+ printed_tables = {}
+ if type(el) == "table" then
+ return tostring_helper(el)
+ end
+ return old_tostring(el)
+end
+
+function checktype(item,t)
+ if type(item) ~= t then
+ error("Was not call with corret type",3)
+ end
+end
+
+-- Functions to save my hands
+function printf(fmt, ...)
+ print(string.format(fmt,...))
+end
+function errorf(fmt, ...)
+ --Our error isn't actually in this function, it's 1 above us (1) = 2
+ error(string.format(fmt,...),2)
+end
+function assertf(bool, fmt, ...)
+ assert(type(fmt) == "string", "Assertf arg #2 was \"" .. type(fmt) .. "\", expected string")
+ if not bool then
+ args = {fmt}
+ for k,v in ipairs({...}) do
+ table.insert(args,tostring(v))
+ end
+ error(string.format(unpack(args)),2)
+ end
+end