Advertisement
Hack-much

ClosetAPI

May 12th, 2020 (edited)
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.64 KB | None | 0 0
  1. --[[
  2.         ClosetAPI version 1.7.5
  3.  
  4. ~~~~~~~Coders Using Visual Studio Code~~~~~~~~~
  5.     Visual Studio Global fix Simply use:
  6.  
  7.         if false then   local ClosetAPI = _ClosetAPI    end
  8.  
  9.     This will emulate the way Computer Craft adds apis like tables to a global table
  10.  
  11.  
  12.  
  13. Loading APIs with the .lua extension is done as such
  14.  
  15.  
  16. os.loadAPI("FileName.lua")
  17. FileName = _G["FileName.lua"]           Or below to rename it
  18. CustomeAPIname = _G["FileName.lua"]
  19.  
  20. ]]--
  21.  
  22. local p = peripheral
  23. local pw = p.wrap
  24. local args = {...}  --Incase i ever need then when debuging
  25. local arg1 = args[1]
  26. local arg2 = args[2]
  27. local debug = true  --Enable this to disable updating and/or if you are changing code in here
  28. VERSION="1.7.5" -- added minor 1.12 support for plethra peripherals ( more 1.12 suport very soon )
  29.  
  30. getAllPeripherals = function(silent) --Silent makes it not return the full table(Non-Silent by default)
  31.  
  32.     --clear all peripheral tabels
  33.     local monitors = {}
  34.     local reactors = {}
  35.     local powerdevices = {}
  36.     local tanks = {}
  37.     local turbines = {}
  38.     local others = {}
  39.     local cp = p.getNames()
  40.  
  41.     for i = 1,#cp do
  42.         local current = cp[i]
  43.         local curtype = p.getType(current)
  44.  
  45.         if curtype == "monitor" then
  46.             table.insert(monitors,current)
  47.         elseif curtype == "BigReactors-Reactor" then
  48.             table.insert(reactors,current)
  49.         elseif curtype == "tile_blockcapacitorbank_name"    or string.find(curtype,"tile_thermalexpansion_cell") == 1    or curtype == "draconic_rf_storage"    or curtype == "thermalexpansion:storage_cell" then
  50.             table.insert(powerdevices,current)
  51.         elseif curtype == "openblocks_tank"     or curtype == "rcirontankvalvetile" then
  52.             table.insert(tanks,current)
  53.         elseif curtype == "BigReactors-Turbine" then
  54.             table.insert(turbines,current)
  55.         else
  56.             table.insert(others,current)
  57.         end
  58.     end
  59.  
  60.     local gap = {
  61.     mons = monitors,
  62.     reacs = reactors,
  63.     pds = powerdevices,
  64.     tnks = tanks,
  65.     turbs = turbines,
  66.     others = others
  67.     }
  68.     if silent == false or silent == nil then   return gap     end
  69. end
  70.  
  71. ExtraMonitor = {
  72.     Add = {
  73.        
  74.     }
  75.  
  76. }
  77.  
  78. closetUpdate = function()
  79.     if debug == false then
  80.         local p = fs.open("ClosetAPI","r")
  81.         local pr = p.readAll()
  82.         p.close()
  83.        
  84.  
  85.         local l = http.get("https://pastebin.com/raw/pfJSTtvS")
  86.         local lr = l.readAll()
  87.  
  88.         if pr == lr then
  89.             print("Up to date")
  90.         else
  91.             local pw = fs.open("ClosetAPI","w")
  92.             print("updating")
  93.             pw.write(lr)
  94.             pw.close()
  95.         end
  96.     else
  97.         error("You are in debug/coding mode I would highly recommend not updating right now!")
  98.     end
  99. end
  100.  
  101. FileManager = {
  102.     CurrentFileW = "",
  103.     CurrentFileR = "",
  104.     OpenFileW = function(file)
  105.         if fs.getFreeSpace(file) <= 0 then APIdebug.log("Error no space left on computer!",5,"current","current","current") end
  106.         FileManager.CurrentFileW = fs.open(file,"w")
  107.     end,
  108.     OpenFileR = function(file)
  109.         FileManager.CurrentFileR = fs.open(file,"r")
  110.     end,
  111.     WriteLine = function(text,file)
  112.         if FileManager.CurrentFileW == nil then return {"File Manager - [3] - ","File Not Found"} end
  113.         FileManager.CurrentFileW:writeLine(text)
  114.         FileManager.CurrentFileW:flush()
  115.     end,
  116.     CloseCurrentW = function()
  117.         if FileManager.OpenFileW ~= nil then
  118.             if FileManager.CurrentFileW.close ~= nil then
  119.                 FileManager.CurrentFileW:close()
  120.             end
  121.         end
  122.     end,
  123.     CloseCurrentR = function()
  124.         if FileManager.CurrentFileR ~= nil then
  125.             if FileManager.CurrentFileR.close ~= nil then
  126.                 FileManager.CurrentFileR:close()
  127.             end
  128.         end
  129.     end,
  130. }
  131.  
  132.  
  133. APIdebug = {
  134.     currentlog = 1,
  135.     log = function(text,level,debugvar,lmon,logfile)
  136.  
  137.  
  138.         local FileWrite = FileManager.WriteLine
  139.         if logfile ~= nil and logfile ~= false then
  140.             if APIdebug.currentlog > 500 then
  141.                 FileManager.CloseCurrentW()
  142.                 if fs.isReadOnly(logfile) == false then
  143.                     fs.delete(logfile)
  144.                 end
  145.                 FileManager.OpenFileW(logfile)
  146.             end
  147.             if level == 1 then
  148.                 FileWrite("["..APIdebug.currentlog.."] -  Info - "..text)
  149.             elseif level == 2 then
  150.                 FileWrite("["..APIdebug.currentlog.."]-  Warn - "..text)
  151.             elseif level == 3 then
  152.                 FileWrite("["..APIdebug.currentlog.."] -  Error - "..text)
  153.             elseif level == 4 then
  154.                 FileWrite("["..APIdebug.currentlog.."] -  Fatal No Halt - "..text)
  155.             elseif level == 5 then
  156.                 FileWrite("["..APIdebug.currentlog.."] -  Fatal - "..text.." - HALTING")
  157.             end
  158.         end
  159.         if debugvar == true then
  160.             if level == 1 then
  161.                 drawLineText("["..APIdebug.currentlog.."] - Info - "..text,colors.white,lmon,true)
  162.             elseif level == 2 then
  163.                 drawLineText("["..APIdebug.currentlog.."] - Warn - "..text,colors.yellow,lmon,true)
  164.             elseif level == 3 then
  165.                 drawLineText("["..APIdebug.currentlog.."] - Error - "..text,colors.orange,lmon,true)
  166.             elseif level == 4 then
  167.                 drawLineText("["..APIdebug.currentlog.."] - PANIC NO HALT - "..text,colors.red,lmon,true)
  168.                 os.queueEvent("panic")
  169.             elseif level == 5 then
  170.                 drawLineText("["..APIdebug.currentlog.."] - PANIC FORCE HALT  - "..text.." - HALTING",colors.red,lmon,true)
  171.                 os.queueEvent("panic","halt")
  172.                 print("["..APIdebug.currentlog.."] - PANIC - "..text)
  173.                 os.queueEvent("terminate")
  174.             end
  175.         end
  176.         APIdebug.currentlog = APIdebug.currentlog+1
  177.         if level == 5 then os.queueEvent("terminate") os.queueEvent("panic","halt") end
  178.     end,
  179.     Terminal = function(text,debugvar,lmon)
  180.         if lmon == term and lmon ~= nil then
  181.  
  182.         elseif lmon ~= term and lmon ~= nil and type(lmon) ~= "table" then
  183.             lmon = peripheral.wrap(lmon)
  184.         elseif lmon == nil then
  185.             lmon = term
  186.         end
  187.         local maxx,maxy = lmon.getSize()
  188.         local currentx,currenty = lmon.getCursorPos()
  189.         if debugvar == true or debugvar == nil then            
  190.             currenty = currenty+1
  191.             if currenty >= maxy then
  192.                 lmon.setCursorPos(1,1)
  193.                 lmon.write(text..string.rep(" ",maxx))
  194.                 lmon.setCursorPos(1,2)
  195.                 lmon.write(string.rep(" ",maxx))
  196.                 lmon.setCursorPos(1,1)
  197.             else
  198.                 lmon.setCursorPos(1,currenty)
  199.                 lmon.write(text..string.rep(" ",maxx))
  200.                 lmon.setCursorPos(1,currenty+1)
  201.                 lmon.write(string.rep(" ",maxx))
  202.                 lmon.setCursorPos(1,currenty)
  203.             end
  204.  
  205.         end
  206.     end,
  207.     detectedvars = {},
  208. }
  209. --Basic draw line, text features taken from Bars API ver 1.7 and upgraded so it would work better here
  210.  
  211.  
  212. drawLineText = function(text,color_text,lmon,clearfront)
  213.     local maxX,maxY = lmon.getSize()
  214.     local lastcolor = lmon.getTextColor()
  215.     local lastposX, lastposY = lmon.getCursorPos()
  216.     if lastposY >= maxY then
  217.         lmon.setCursorPos(1,1)
  218.  
  219.         lmon.clearLine()
  220.         lmon.setTextColor(color_text)
  221.         lmon.write(text)
  222.         lmon.setTextColor(lastcolor)
  223.     else
  224.         lmon.setCursorPos(1,lastposY+1)
  225.         lmon.clearLine()
  226.         lmon.setTextColor(color_text)
  227.         lmon.write(text)
  228.         lmon.setTextColor(lastcolor)
  229.     end
  230.     if clearfront == true then
  231.         local y,x = lmon.getCursorPos()
  232.         lmon.setCursorPos(1,x+1)
  233.         lmon.clearLine()
  234.         lmon.setCursorPos(y,x)
  235.     end
  236. end
  237.  
  238. drawText = function(x,y,text,color_text,color_background,lmon)
  239.  
  240.     if color_text == nil then
  241.         color_text = colors.white
  242.     end
  243.     if color_background == nil then
  244.         color_background = colors.black
  245.     end
  246.     if lmon == nil then
  247.         lmon = term
  248.     end
  249.  
  250.     local lastcolor = lmon.getTextColor() --saving last color
  251.     local lastbcolor = lmon.getBackgroundColor()
  252.  
  253.     lmon.setCursorPos(x,y)
  254.     lmon.setBackgroundColor(color_background)
  255.     lmon.setTextColor(color_text)
  256.     lmon.write(text)
  257.     lastposX, lastposY = lmon.getCursorPos()
  258.  
  259.     lmon.setBackgroundColor(lastbcolor)  --restoring last color
  260.     lmon.setTextColor(lastcolor)
  261. end
  262. drawLine = function(x,y,tall,length,color_background,lmon)--Starting X, Starting Y, Height going down from starting, Length going out from starting
  263.     if length < 1 then return end
  264.     if tall < 1 then return end
  265.     if color_background == nil then    color_background = colors.white    end
  266.     if lmon == nil then    lmon = term    end
  267.     local lastbcolor = lmon.getBackgroundColor()--saving last color
  268.  
  269.     lmon.setCursorPos(x,y)
  270.     lmon.setBackgroundColor(color_background)
  271.     for yPos = y, y+tall-1 do
  272.         lmon.setCursorPos(x,yPos)
  273.         lmon.write(string.rep(" ", length))
  274.     end
  275.  
  276.     lmon.setBackgroundColor(lastbcolor) --restoring last color
  277. end
  278. drawProg = function(x,y,tall,length,value,maxvalue,color_empty,color_fill,lmon)
  279.     --Starting X, Starting Y, Height going down from starting, Length going out from starting, Current value, Max value this will divide the current to make the percentage full,color,color,mon/term to draw to
  280.     if lmon == nil then    lmon = term    end
  281.     if color_empty == nil then    color_background = colors.white    end
  282.     if color_fill == nil then    color_fill = colors.gray    end
  283.     local percentage = value/maxvalue
  284.     if percentage > 1 then percentage = 1 end
  285.     local perfill = math.floor(percentage*length+.3)
  286.  
  287.     drawLine(x,y,tall,length,color_empty,lmon)
  288.     drawLine(x,y,tall,perfill,color_fill,lmon)
  289. end
  290. function drawNewLine(lmon,text) --Text can be nil
  291.     cx,cy = lmon.getCursorPos()
  292.     lmon.write(text)
  293.     lmon.setCursorPos(1,cy+1)
  294. end
  295.  
  296. function drawGraph(startingX,startingY,LenthOfEach,MaxHight,value,color_empty,color_fill,lmon)  --WIP
  297.     if lmon == nil then    lmon = term    end
  298.     if color_empty == nil then    color_background = colors.white    end
  299.     if color_fill == nil then    color_fill = colors.gray    end
  300.     for i=1,#value do
  301.     end
  302. end
  303.  
  304. numbShorten = function(num, f, table, places, significance)--OLD DO NOT USE numbShorten2  Number,dnu,dnu,dnu,Floor Significance
  305.     numbShorten2(num,significance)
  306. end
  307.  
  308. function numbShorten2(numb,places,returntype) -- Input Number, Floor Significance | ver 4.0
  309.     if numb < 1000 then return numb end
  310.     if places == nil then places = 1 end
  311.     numb = tostring(numb)
  312.     local count = math.floor(#numb/3)
  313.     local letters = {"K","M","B","T","Q","Qi","Sx","Sp","N","D","Ud","Du","Te","Qa","Qid"}
  314.    
  315.     if returntype == nil or returntype == 1 then
  316.         pfnumb = math.floor( numb / 10^(count*3) *10^places )/10^places
  317.         return pfnumb..letters[count]
  318.     elseif returntype == 2 then
  319.         pfnumb = math.floor( numb / 10^(count*3) *10^places )/10^places
  320.         return pfnumb
  321.     elseif returntype == 3 then
  322.         pfnumb = math.floor( numb* (10^places+1) / 10^#numb  )
  323.         return pfnumb.."*10^"..#numb
  324.     elseif returntype == 4 then
  325.         pfnumb = math.floor( numb* (10^places+1) / 10^#numb  )
  326.         return pfnumb.."E"..#numb
  327.     end
  328.  
  329. end
  330.  
  331.  
  332. --Some table saving tactics taken from the wiki.
  333. function tabeSave(table,name)
  334.     if type(name) ~= "string" then APIdebug.log("TabeSave didn't get a string name!",4,"detect","detect","detect") return end
  335.     if table == nil then APIdebug.log("TabeSave didn't get a Table, it got nil!",4,"detect","detect","detect") return end
  336.     local file = fs.open(name,"w")
  337.     file.write(textutils.serialize(table))
  338.     file.close()
  339. end
  340. function tabeLoad(name)
  341.     if fs.exists(name) == false then return false end
  342.     local file = fs.open(name,"r")
  343.     local data = file.readAll()
  344.     file.close()
  345.     return textutils.unserialize(data)
  346. end
  347. function tabeUnnester(table)  -- basic unpacker able to unpack double nested tables
  348.     for i =1,#table do
  349.         for a = 1,#table[i] do
  350.             print("table ",i,"  Index ",a,table[i][a])
  351.         end
  352.     end
  353. end
  354.  
  355. if false then _ClosetAPI = {FileManager = FileManager,APIdebug = APIdebug,drawLine = drawLine,drawProg = drawProg,drawText = drawText,getAllPeripherals = getAllPeripherals,closetUpdate = closetUpdate, tabeSave = tabeSave,tabeLoad = tabeLoad, tabeUnnester = tabeUnnester, numbShorten = numbShorten} end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement