Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ESAPI for ComputerCraft/OpenComputers
- -- electronic_steve сделал это. http://computercraft.ru/topic/1126-oc-esapi
- es={}
- es.log_text=""
- function es.log(msg)
- es.log_text=es.log_text..msg.."\n"
- end
- function es.create_map(minx,miny,maxx,maxy,n)
- local map={}
- for x=minx,maxx do
- map[x]={}
- for y=miny,maxy do
- map[x][y]=n
- end
- end
- return map
- end
- function es.get_table_map(tbl,J)
- J=(J or 1)
- local function B(i)
- local text=""
- for I=1,i/2 do
- text=text.." "
- end
- return text
- end
- local function A(i,v,J)
- local a,b="",""
- if type(i)=="number" then a="["..i.."]"
- else a=tostring(i) end
- if type(v)=="string" then b="\""..v.."\""
- elseif type(v)=="table" then b=es.get_table_map(v,J+1)
- else b=tostring(v) end
- return B(J)..a.."="..b..",\n"
- end
- local text="{\n"
- for i,v in pairs(tbl) do
- text=text..A(i,v,J+1)
- end
- text=text..B(J).."}"
- return text
- end
- function es.rotation(x,y,cos,sin)
- local ix,iy
- if sin==nil then
- ix=x*math.cos(cos)-y*math.sin(cos)
- iy=x*math.sin(cos)+y*math.cos(cos)
- else
- ix=x*cos-y*sin
- iy=x*sin+y*cos
- end
- return ix,iy
- end
- function es.round(num, idp)
- local mult = 10^(idp or 0)
- return math.floor(num * mult + 0.5) / mult
- end
- function es.getminmax(x,y,x2,y2)
- local minx=math.min(x,x2)
- local maxx=math.max(x,x2)
- local miny=math.min(y,y2)
- local maxy=math.max(y,y2)
- return minx,miny,maxx,maxy
- end
- function es.interval(min,n,max)
- local x=math.max(n,min)
- return math.min(x,max)
- end
- function es.interval_test(min,n,max)
- if n>=min and n<=max then return 0
- elseif n<min then return -1
- elseif n>max then return 1 end
- end
- es.quad={{1,-1},{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1}}
- es.mquad={{0,-1},{1,0},{0,1},{-1,0}}
- --------------es.quad---------------------
- -- 7 8 1 {-1,-1} { 0,-1} { 1,-1} --
- -- 6 C 2 {-1, 0} Core { 1, 0} --
- -- 5 4 3 {-1, 1} { 0, 1} { 1, 1} --
- --------------es.mquad--------------------
- -- 1 { 0,-1} --
- -- 4 C 2 {-1, 0} Core { 1, 0} --
- -- 3 { 0, 1} --
- ------------------------------------------
- function es.create_circle(rad)
- local model={}
- for x=-rad,rad do
- for y=-rad,rad do
- if math.sqrt(x^2+y^2)<=rad then
- table.insert(model,{x,y})
- end
- end
- end
- return model
- end
- function es.create_ring(rad)
- local model={}
- for i=1,rad*math.pi do
- table.insert()
- end
- return model
- end
- function es.save_file(name,msg)
- io.open(name,"w"):write(msg)
- end
- --https://github.com/IgorTimofeev/OpenComputers/blob/master/lib/xmlParser.lua
- local xml = {}
- ------------------------------------------------------------------------------------------------------------
- function xml.parseargs(s)
- local arg = {}
- string.gsub(s, "([%-%w]+)=([\"'])(.-)%2", function (w, _, a)
- arg[w] = a
- end)
- return arg
- end
- function xml.collect(s)
- local stack = {}
- local top = {}
- table.insert(stack, top)
- local ni,c,label,xarg, empty
- local i, j = 1, 1
- while true do
- ni,j,c,label,xarg, empty = string.find(s, "<(%/?)([%w:]+)(.-)(%/?)>", i)
- if not ni then break end
- local text = string.sub(s, i, ni-1)
- if not string.find(text, "^%s*$") then
- table.insert(top, text)
- end
- if empty == "/" then -- empty element tag
- table.insert(top, {label=label, xarg=xml.parseargs(xarg), empty=1})
- elseif c == "" then -- start tag
- top = {label=label, xarg=xml.parseargs(xarg)}
- table.insert(stack, top) -- new level
- else -- end tag
- local toclose = table.remove(stack) -- remove top
- top = stack[#stack]
- if #stack < 1 then
- error("nothing to close with "..label)
- end
- if toclose.label ~= label then
- error("trying to close "..toclose.label.." with "..label)
- end
- table.insert(top, toclose)
- end
- i = j+1
- end
- local text = string.sub(s, i)
- if not string.find(text, "^%s*$") then
- table.insert(stack[#stack], text)
- end
- if #stack > 1 then
- error("unclosed "..stack[#stack].label)
- end
- return stack[1]
- end
- es.pb={} -- pastebin
- es.pb.dev_key="c4b98a02a2da7dc54edfa2d7e1f4b5fc"
- local user_key=""
- es.pb.post_url="http://pastebin.com/api/api_post.php"
- es.pb.login_url="http://pastebin.com/api/api_login.php"
- es.pb.bridge_name="bridge"
- local function encode(code)
- if code then
- code = string.gsub(code, "([^%w ])", function (c)
- return string.format("%%%02X", string.byte(c))
- end)
- code = string.gsub(code, " ", "+")
- end
- return code
- end
- function es.pb.require(path,paste_id)
- if require("filesystem").exists(path) then
- return dofile(path)
- else
- require("shell").execute("pastebin get "..paste_id.." "..path)
- os.sleep(1)
- return dofile(path)
- end
- end
- function es.pb.login(user_name,pass)
- 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))
- local out=""
- for q in response do out=out..q end
- user_key=out
- return out
- end
- function es.pb.create(msg,paste_name,private,time)
- local private=private or 1
- local time = time or "N"
- local dev_key,user_key=es.pb.dev_key,user_key
- 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))
- local out=""
- for q in response do out=out..q end
- return string.match(out, "http://pastebin.com/(.+)")
- end
- function es.pb.delete(past_id)
- 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)
- local out=""
- for q in response do out=out..q end
- return out
- end
- function es.pb.list()
- local response=require("internet").request(es.pb.post_url,"api_option=list&api_dev_key="..es.pb.dev_key.."&api_user_key="..user_key)
- local out=""
- for q in response do out=out..q end
- return xml.collect(out)
- end
- function es.pb.get_paste_id(paste_name)
- local x=es.pb.list()
- for i=1,#x do
- if x[i][3][1]==paste_name then
- return x[i][1][1]
- end
- end
- return ""
- end
- function es.pb.get(paste_id)
- local response=require("internet").request("http://pastebin.com/raw.php?i="..paste_id)
- local out=""
- for q in response do out=out..string.gsub(q, "\r\n", "\n") end
- return out
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement