Advertisement
electronic_steve

ESAPI Lite

Aug 21st, 2015
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.54 KB | None | 0 0
  1. -- ESAPI for ComputerCraft/OpenComputers
  2. -- electronic_steve сделал это. http://computercraft.ru/topic/1126-oc-esapi
  3. es={}
  4. es.log_text=""
  5.  
  6. function es.log(msg)
  7.     es.log_text=es.log_text..msg.."\n"
  8. end
  9. function es.create_map(minx,miny,maxx,maxy,n)
  10.     local map={}
  11.     for x=minx,maxx do
  12.         map[x]={}
  13.         for y=miny,maxy do
  14.             map[x][y]=n
  15.         end
  16.     end
  17.     return map
  18. end
  19.  
  20. function es.get_table_map(tbl,J)
  21.     J=(J or 1)
  22.     local function B(i)
  23.         local text=""
  24.         for I=1,i/2 do
  25.             text=text.."    "
  26.         end
  27.         return text
  28.     end
  29.     local function A(i,v,J)
  30.         local a,b="",""
  31.         if     type(i)=="number" then a="["..i.."]"
  32.         else a=tostring(i) end
  33.         if     type(v)=="string" then b="\""..v.."\""
  34.         elseif type(v)=="table"  then b=es.get_table_map(v,J+1)
  35.         else b=tostring(v) end
  36.         return B(J)..a.."="..b..",\n"
  37.     end
  38.     local text="{\n"
  39.     for i,v in pairs(tbl) do
  40.         text=text..A(i,v,J+1)
  41.     end
  42.     text=text..B(J).."}"
  43.     return text
  44. end
  45.  
  46. function es.rotation(x,y,cos,sin)
  47.     local ix,iy
  48.     if sin==nil then
  49.          ix=x*math.cos(cos)-y*math.sin(cos)
  50.          iy=x*math.sin(cos)+y*math.cos(cos)
  51.     else
  52.          ix=x*cos-y*sin
  53.          iy=x*sin+y*cos
  54.     end
  55.     return ix,iy
  56. end
  57.  
  58. function es.round(num, idp)
  59.     local mult = 10^(idp or 0)
  60.     return math.floor(num * mult + 0.5) / mult
  61. end
  62.  
  63. function es.getminmax(x,y,x2,y2)
  64.     local  minx=math.min(x,x2)
  65.     local  maxx=math.max(x,x2)
  66.     local  miny=math.min(y,y2)
  67.     local  maxy=math.max(y,y2)
  68.     return minx,miny,maxx,maxy
  69. end
  70.  
  71. function es.interval(min,n,max)
  72.     local x=math.max(n,min)
  73.     return math.min(x,max)
  74. end
  75.  
  76. function es.interval_test(min,n,max)
  77.     if n>=min and n<=max then return 0
  78.     elseif n<min then return -1
  79.     elseif n>max then return 1 end
  80. end
  81.  
  82. es.quad={{1,-1},{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1}}
  83. es.mquad={{0,-1},{1,0},{0,1},{-1,0}}
  84. --------------es.quad---------------------
  85. --  7 8 1      {-1,-1} { 0,-1} { 1,-1}  --
  86. --  6 C 2      {-1, 0} Core    { 1, 0}  --
  87. --  5 4 3      {-1, 1} { 0, 1} { 1, 1}  --
  88. --------------es.mquad--------------------
  89. --    1                { 0,-1}          --
  90. --  4 C 2      {-1, 0} Core    { 1, 0}  --
  91. --    3                { 0, 1}          --
  92. ------------------------------------------
  93.  
  94.  
  95.  
  96.  
  97. function es.create_circle(rad)
  98.     local model={}
  99.     for x=-rad,rad do
  100.         for y=-rad,rad do
  101.             if math.sqrt(x^2+y^2)<=rad then
  102.                 table.insert(model,{x,y})
  103.             end
  104.         end
  105.     end
  106.     return model
  107. end
  108.  
  109. function es.create_ring(rad)
  110.     local model={}
  111.     for i=1,rad*math.pi do
  112.         table.insert()
  113.     end
  114.     return model
  115. end
  116.  
  117. function es.save_file(name,msg)
  118.     io.open(name,"w"):write(msg)
  119. end
  120.  
  121. --https://github.com/IgorTimofeev/OpenComputers/blob/master/lib/xmlParser.lua
  122. local xml = {}
  123.  
  124. ------------------------------------------------------------------------------------------------------------
  125.  
  126. function xml.parseargs(s)
  127.   local arg = {}
  128.   string.gsub(s, "([%-%w]+)=([\"'])(.-)%2", function (w, _, a)
  129.     arg[w] = a
  130.   end)
  131.   return arg
  132. end
  133.    
  134. function xml.collect(s)
  135.   local stack = {}
  136.   local top = {}
  137.   table.insert(stack, top)
  138.   local ni,c,label,xarg, empty
  139.   local i, j = 1, 1
  140.   while true do
  141.     ni,j,c,label,xarg, empty = string.find(s, "<(%/?)([%w:]+)(.-)(%/?)>", i)
  142.     if not ni then break end
  143.     local text = string.sub(s, i, ni-1)
  144.     if not string.find(text, "^%s*$") then
  145.       table.insert(top, text)
  146.     end
  147.     if empty == "/" then  -- empty element tag
  148.       table.insert(top, {label=label, xarg=xml.parseargs(xarg), empty=1})
  149.     elseif c == "" then   -- start tag
  150.       top = {label=label, xarg=xml.parseargs(xarg)}
  151.       table.insert(stack, top)   -- new level
  152.     else  -- end tag
  153.       local toclose = table.remove(stack)  -- remove top
  154.       top = stack[#stack]
  155.       if #stack < 1 then
  156.         error("nothing to close with "..label)
  157.       end
  158.       if toclose.label ~= label then
  159.         error("trying to close "..toclose.label.." with "..label)
  160.       end
  161.       table.insert(top, toclose)
  162.     end
  163.     i = j+1
  164.   end
  165.   local text = string.sub(s, i)
  166.   if not string.find(text, "^%s*$") then
  167.     table.insert(stack[#stack], text)
  168.   end
  169.   if #stack > 1 then
  170.     error("unclosed "..stack[#stack].label)
  171.   end
  172.   return stack[1]
  173. end
  174.  
  175. es.pb={} -- pastebin
  176. es.pb.dev_key="c4b98a02a2da7dc54edfa2d7e1f4b5fc"
  177. local user_key=""
  178. es.pb.post_url="http://pastebin.com/api/api_post.php"
  179. es.pb.login_url="http://pastebin.com/api/api_login.php"
  180. es.pb.bridge_name="bridge"
  181.  
  182. local function encode(code)
  183.   if code then
  184.     code = string.gsub(code, "([^%w ])", function (c)
  185.       return string.format("%%%02X", string.byte(c))
  186.     end)
  187.     code = string.gsub(code, " ", "+")
  188.   end
  189.   return code
  190. end
  191.  
  192.  
  193. function es.pb.require(path,paste_id)
  194.     if require("filesystem").exists(path) then
  195.         return dofile(path)
  196.     else
  197.         require("shell").execute("pastebin get "..paste_id.." "..path)
  198.         os.sleep(1)
  199.         return dofile(path)
  200.     end
  201. end
  202.  
  203. function es.pb.login(user_name,pass)
  204.     local response = require("internet").request(es.pb.login_url, "api_dev_key="..es.pb.dev_key.."&api_user_name="..encode(user_name).."&api_user_password="..encode(pass))
  205.     local out=""
  206.     for q in response do out=out..q end
  207.     user_key=out
  208.     return out
  209. end
  210.  
  211. function es.pb.create(msg,paste_name,private,time)
  212.     local private=private or 1
  213.     local time = time or "N"
  214.     local dev_key,user_key=es.pb.dev_key,user_key
  215.     local response = require("internet").request(es.pb.post_url, "api_option=paste&".."api_dev_key="..dev_key.."&".."api_user_key="..user_key.."&".."api_paste_private="..private.."&api_paste_expire_date"..time.."&api_paste_format=lua&".."api_paste_name="..encode(paste_name).."&api_paste_code="..encode(msg))
  216.     local out=""
  217.     for q in response do out=out..q end
  218.     return string.match(out, "http://pastebin.com/(.+)")
  219. end
  220.  
  221. function es.pb.delete(past_id)
  222.     local response = require("internet").request(es.pb.post_url,"api_option=delete&api_dev_key="..es.pb.dev_key.."&".."api_user_key="..user_key.."&".."api_paste_key="..past_id)
  223.     local out=""
  224.     for q in response do out=out..q end
  225.     return out
  226. end
  227.  
  228. function es.pb.list()
  229.     local response=require("internet").request(es.pb.post_url,"api_option=list&api_dev_key="..es.pb.dev_key.."&api_user_key="..user_key)
  230.     local out=""
  231.     for q in response do out=out..q end
  232.     return xml.collect(out)
  233. end
  234.  
  235. function es.pb.get_paste_id(paste_name)
  236.     local x=es.pb.list()
  237.     for i=1,#x do
  238.         if x[i][3][1]==paste_name then
  239.             return x[i][1][1]
  240.         end
  241.     end
  242.     return ""
  243. end
  244.  
  245. function es.pb.get(paste_id)
  246.     local response=require("internet").request("http://pastebin.com/raw.php?i="..paste_id)
  247.     local out=""
  248.     for q in response do out=out..string.gsub(q, "\r\n", "\n") end
  249.     return out
  250. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement