Advertisement
aodminecraft

Arc OS - Worldedit - programs/GeneralAPI

Aug 1st, 2015
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.03 KB | None | 0 0
  1. function argError(...) --Returns error text if any of the arguments are nil. Pass it your command's arguments.
  2.   for k,v in pairs({...}) do
  3.     if v==nil then
  4.       return "Argument "..k.." is nil."
  5.     end
  6.   end
  7. end
  8.  
  9. function typeError(args,types) --Returns error text if the arg(s) in the table are not of the type(s) in the types table.
  10. --A few quirks:
  11. --nil is a type with only one value: nil. So, argError is basically the same as typeError({arg(s)},"nil").
  12. --It can check multiple types for each argument as so: "typeError({arg1,arg2,arg3},{{"string","number"},"thread",{"number","nil"}})"
  13. --If you check, for example, arguments 1,2, and 4, in order for the text to be correct, use "typeError({arg1,arg2,[4]=arg4},type(s))"
  14. --It supports strings or tables, so {"number"} or {{"thread","function"},"string"} would work.
  15. --typeError(arg,type(s)) (type(s) can be a string or a table) will work as long as arg isn't a table.
  16.   if type(args)~="table" then
  17.     if type(types)=="string" and type(args)~=types then
  18.       return "Argument 1 should be of type \""..types.."\", was type \""..type(args).."\""
  19.     elseif type(types)=="table" then
  20.       for i=1,#types do
  21.         if type(args)==types[i] then
  22.           break
  23.         elseif i==#types[k] then
  24.           return "Argument "..k.." should be of type \""..table.concat(types,"\" or \"").."\", was type \""..type(args).."\""
  25.         end
  26.       end
  27.     elseif type(args)~=types and types~=nil then
  28.       return "Argument "..k.." should be of type \""..types.."\", was type \""..type(args).."\""
  29.     else
  30.       return "Argument 2 to typeError() needs to be type(s) as a string! (typeError(arg,\"nil\"), not typeError(arg,nil))"
  31.     end
  32.   elseif type(args)=="table" then
  33.     for k,v in pairs(args) do
  34.       if type(types)=="table" then
  35.         if type(types[k])=="table" then
  36.           for i=1,#types[k] do
  37.             if type(v)==types[k][i] then
  38.               break
  39.             elseif i==#types[k] then
  40.               return "Argument "..k.." should be of type \""..table.concat(types[k],"\" or \"").."\", was type \""..type(v).."\""
  41.             end
  42.           end
  43.         elseif types[k]:sub(1,1)=="~" then
  44.           if type(v)==types[k] then
  45.             return "Argument "..k.." should not be of type \""..types[k]:sub(2).."\", was type \""..type(v).."\""
  46.           end
  47.         elseif type(v)~=types[k] and types[k]~=nil then
  48.           return "Argument "..k.." should be of type \""..types[k].."\", was type \""..type(v).."\""
  49.         end
  50.       elseif type(types)=="string" then
  51.         if types:sub(1,1)=="~" then
  52.           if types:sub(2)==type(v) then
  53.             return "Argument "..k.." should not be of type \""..types:sub(2).."\", was type \""..type(v).."\""
  54.           end
  55.         elseif type(v)~=types then
  56.           return "Argument "..k.." should be of type \""..types.."\", was type \""..type(v).."\""
  57.         end
  58.       else
  59.         return "Argument 2 to typeError() needs to be type(s) as a string! (typeError(arg,\"nil\"), not typeError(arg,nil))"
  60.       end
  61.     end
  62.   end
  63. end
  64.  
  65. local oldmath={}
  66. oldmath.floor=math.floor
  67. function math.floor(...)
  68.   local num={...}
  69.   local errText=typeError(num,"number")
  70.   if errText then error(errText) end
  71.   for k,v in pairs(num) do
  72.     num[k]=oldmath.floor(tonumber(v))
  73.   end
  74.   return unpack(num)
  75. end
  76.  
  77. oldmath.ceil=math.ceil
  78. function math.ceil(...)
  79.   local num={...}
  80.   local errText=typeError(num,"number")
  81.   if errText then error(errText) end
  82.   for k,v in pairs(num) do
  83.     num[k]=oldmath.ceil(tonumber(v))
  84.   end
  85.   return unpack(num)
  86. end
  87.  
  88. function math.round(...)
  89.   local num={...}
  90.   local errText=typeError(num,"number")
  91.   if errText then error(errText) end
  92.   local lowNum=0
  93.   for k,v in pairs(num) do
  94.     lowNum=oldmath.floor(tonumber(v))
  95.     num[k]=lowNum+((v-lowNum<.5 and 0) or 1)
  96.   end
  97.   return unpack(num)
  98. end
  99.  
  100. stringx={}
  101. function stringx.indexOf(str,char,index)
  102.   local reservedChars={"(",")",".","%","+","-","*","?","[","]","^","$"}
  103.   for k,v in pairs(reservedChars) do --Put a % in front of lua pattern chars.
  104.     if char==v then
  105.       char="%"..char
  106.       break
  107.     end
  108.   end
  109.   return ({str:find(char,index)})[1]
  110. end
  111.  
  112. function stringx.lastIndexOf(str,char,index)
  113.   local errText=typeError({str,char,index},{"string","string",{"number","nil"}})
  114.   if errText then error(errText) end
  115.   index=index or 1
  116.   local charLen=#char
  117.   for i=#str-charLen+index,charLen,-1 do
  118.     if str:sub(i,i+charLen-1)==char then
  119.       return i
  120.     end
  121.   end
  122. end
  123.  
  124. function stringx.split(str,char)
  125.   local errText=typeError({str,char},"string")
  126.   if errText then error(errText) end
  127.   tbl={}
  128.   findChar=stringx.indexOf(str,char)
  129.   if findChar then
  130.     table.insert(tbl,str:sub(1,findChar-1))
  131.     repeat
  132.       findChar2=stringx.indexOf(str,char,findChar+1)
  133.       if findChar2~=nil then
  134.         table.insert(tbl,str:sub(findChar+1,findChar2-1))
  135.         findChar=findChar2
  136.       end
  137.     until findChar2==nil
  138.     table.insert(tbl,str:sub(findChar+1))
  139.   else
  140.     return {str}
  141.   end
  142.   return tbl
  143. end
  144.  
  145. --Returns the index of element in tbl, or nil if no such index exists.
  146. --Returns the indices as a table as so: {ind1,ind2,...indN} for tbl[ind1][ind2]...[indN].
  147. function table.indexOf(tbl,element)
  148.   local errText=argError(tbl,element) or typeError({tbl},"table")
  149.   if errText then error(errText) end
  150.   local indices={}
  151.   local function searchTbl(tbl,element)
  152.     for k,v in pairs(tbl) do
  153.       if type(v)=="table" then
  154.         local index=searchTbl(v,element) --Recursive search
  155.         if index~=nil then
  156.           table.insert(indices,k)
  157.           return indices
  158.         else
  159.           return nil
  160.         end
  161.       elseif v==element then
  162.         table.insert(indices,k)
  163.         return indices
  164.       end
  165.     end
  166.     return nil
  167.   end
  168.   for k,v in pairs(tbl) do
  169.     if type(v)=="table" then
  170.       local indices={k}
  171.       local results=searchTbl(v,element)
  172.       for i=#results,1,-1 do --searchTbl returns the indices in reverse, so if you insert them backwards, it's correct.
  173.         indices[#indices+1]=results[i]
  174.       end
  175.       return indices
  176.     elseif v==element then
  177.       return k
  178.     end
  179.   end
  180. end
  181.  
  182. function getTblVal(...) --This is what you would give the results from table.indexOf() to.
  183. --The first argument should be the table being accessed, the following arguments should be the indices in order.
  184. --Example use: To access tbl.bacon[a][5].c, use "getTblVal(tbl,unpack({"bacon",a,5,"c"}))" or "getTblVal(tbl,"bacon",a,5,"c")"
  185.   local args={...}
  186.   return #args>=3 and getTblVal(args[1][args[2]],select(3,...)) or args[1][args[2]]
  187. end
  188.  
  189. --Copied from penlight. https://github.com/stevedonovan/Penlight/blob/master/lua/pl/tablex.lua
  190. function table.equals(tbl1,tbl2,ignoreMetatable,threshold) --Returns if two tables are the same, including sub-tables.
  191.   local type1=type(tbl1)
  192.   local type2=type(tbl2)
  193.   if type1~=type2 then return false end
  194.   --Non-table types can be directly compared
  195.   if type1~="table" then
  196.     if type1=="number" and threshold then
  197.       return math.abs(tbl1-tbl2)<threshold
  198.     end
  199.     return tbl1==tbl2
  200.   end
  201.   --As well as tables which have the metamethod __eq
  202.   local metatable=getmetatable(tbl1)
  203.   if not ignoreMetatable and metatable and metatable.__eq then
  204.     return tbl1==tbl2
  205.   end
  206.   for k1 in pairs(tbl1) do
  207.     if tbl2[k1]==nil then
  208.       return false
  209.     end
  210.   end
  211.   for k2 in pairs(tbl2) do
  212.     if tbl1[k2]==nil then
  213.       return false
  214.     end
  215.   end
  216.   for k1,v1 in pairs(tbl1) do
  217.     local v2=tbl2[k1]
  218.     if not table.equals(v1,v2,ignoreMetatable,threshold) then
  219.       return false
  220.     end
  221.   end
  222.   return true
  223. end
  224.  
  225. --Copies the value of tbl1 to tbl2 instead of making a pointer.
  226. --Copied from penlight. https://github.com/stevedonovan/Penlight/blob/master/lua/pl/tablex.lua
  227. function table.copy(tbl)
  228.   if type(tbl)~="table" then return tbl end
  229.   local metatable=getmetatable(tbl)
  230.   local copiedTbl={}
  231.   for k,v in pairs(tbl) do
  232.     if type(v)=="table" then
  233.       v=table.copy(v)
  234.     end
  235.     copiedTbl[k]=v
  236.   end
  237.   setmetatable(copiedTbl,metatable)
  238.   return copiedTbl
  239. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement