Advertisement
Redxone

[OC] Compuercraft Defs for OpenComputers

Jun 26th, 2016
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.21 KB | None | 0 0
  1. local filesystem = require"filesystem"
  2. local keyboard = require"keyboard"
  3. local r = {}
  4. local com = require"component"
  5. r.nfsopen = function(file,mode)
  6.     local file = io.open(file,mode)
  7.     file.online = 1
  8.    if(mode == "r")then
  9.         file.readLine = function()
  10.            local ind = 1
  11.            for lines in file:lines() do
  12.               if file.online == ind then
  13.                 file.online = file.online + 1
  14.                 return lines
  15.               else
  16.                 ind = ind + 1
  17.               end
  18.            end
  19.          end
  20.        
  21.        file.readAll = function()
  22.            local cont = ""
  23.            for lines in file:lines() do
  24.               cont = cont .. lines
  25.            end
  26.            return cont
  27.        end
  28.    elseif(mode == "w")then
  29.        file.dowrite = file.write
  30.        file.writestr = ""
  31.        file.write = function(str)
  32.            file.writestr = file.writestr .. str
  33.        end
  34.        file.writeLine = function(line)
  35.            file:dowrite(line .. "\n")
  36.        end
  37.    end
  38.    local oldclose = file.close
  39.    file.close = function()
  40.       if(file.writestr ~= nil)then
  41.           file:dowrite(file.writestr)
  42.       end
  43.       oldclose(file)
  44.    end
  45.  
  46.    return file
  47. end
  48.  
  49. r.shell = require"shell"
  50. r.shell.dir = r.shell.getWorkingDirectory
  51. r.shell.setDir = r.shell.setWorkingDirectory
  52. r.shell.isColor = function() return true end
  53. r.unpack = table.unpack
  54. r.os = os
  55. r.unpack = table.unpack
  56. r.term = require"term"
  57. colors = require"colors"
  58. term = require"term"
  59. r.write = term.write
  60. r.term.getSize = com.gpu.getResolution
  61. r.os.pullEvent = function(ev)
  62.     local tmatrix = {
  63.         ['mouse_click'] = "touch",
  64.         ['mouse_drag'] = "drag",
  65.         ['mouse_scroll'] = "scroll",  
  66.         ['key'] = "key_down",
  67.     }
  68.     local ccmatrix = {
  69.         ['touch'] = "mouse_click",
  70.         ['drag'] = "mouse_drag",
  71.         ['scroll'] = "mouse_scroll",
  72.         ['key_down'] = "key",
  73.     }
  74.     if(tmatrix[ev] ~= nil)then ev = tmatrix[ev] end
  75.     local rev = {require"event".pull(ev)}
  76.     if(ccmatrix[rev[1]] ~= nil)then rev[1] = ccmatrix[rev[1]] end
  77.     if(rev[1] == "key" or rev[1] == "key_up" or rev[1] == "key_down")then
  78.        rev[2] = rev[4]
  79.     end
  80.     if(rev[1]:find("mouse"))then
  81.        if(rev[1] == "mouse_click" or rev[1] == "mouse_drag")then
  82.           rev[5] = rev[5] + 1
  83.        end
  84.        if(rev[1] == "mouse_scroll")then
  85.           rev[5] = rev[5] * -1
  86.        end
  87.        rev[2] = rev[5]
  88.        table.remove(rev,5)  
  89.     end
  90.     return table.unpack(rev)
  91.    
  92. end
  93. r.sleep = os.sleep
  94. r.paintutils = require"paintutils"
  95. r.term.setCursorPos = term.setCursor
  96. r.term.getCursorPos = term.getCursor
  97. r.term.setBackgroundColor = com.gpu.setBackground
  98. r.term.getBackgroundColor = com.gpu.getBackground
  99. r.term.setTextColor = com.gpu.setForeground
  100. r.term.getTextColor = com.gpu.getForeground
  101. r.term.current = function() return {setVisible = function(bool) end} end
  102. r.read = term.read
  103. newcolors = {
  104.   ['white']     = 0xF0F0F0,
  105.   ['orange']    = 0xF2B233,
  106.   ['magenta']   = 0xE57FD8,
  107.   ['lightBlue'] = 0x99B2F2,
  108.   ['yellow']    = 0xDEDE6C,
  109.   ['lime']      = 0x7FCC19,
  110.   ['pink']      = 0xF2B2CC,
  111.   ['gray']      = 0x4C4C4C,
  112.   ['lightGray'] = 0x999999,
  113.   ['cyan']      = 0x4C99B2,
  114.   ['purple']    = 0xB266E5,
  115.   ['blue']      = 0x3366CC,
  116.   ['brown']     = 0x7F664C,
  117.   ['green']     = 0x57A64E,
  118.   ['red']       = 0xCC4C4C,
  119.   ['black']     = 0x000000,
  120. }
  121. r.colors = newcolors
  122. -- Loading apis and stuff --
  123. r.loadfile = function( _sFile, _tEnv )
  124.     local file = fs.open( _sFile, "r" )
  125.     if file then
  126.         local func, err = load( file.readAll(), fs.getName( _sFile ), "t", _tEnv )
  127.         file.close()
  128.         return func, err
  129.     end
  130.     return nil, "File not found"
  131. end
  132.  
  133. r.dofile = function( _sFile )
  134.     local fnFile, e = r.loadfile( _sFile, _G )
  135.     if fnFile then
  136.         return fnFile()
  137.     else
  138.         error( e, 2 )
  139.     end
  140. end
  141.  
  142. function r.os.run( _tEnv, _sPath, ... )
  143.     local tArgs = { ... }
  144.     local tEnv = _tEnv
  145.     setmetatable( tEnv, { __index = _G } )
  146.     local fnFile, err = r.loadfile( _sPath, tEnv )
  147.     if fnFile then
  148.         local ok, err = pcall( function()
  149.             fnFile( table.unpack( tArgs ) )
  150.         end )
  151.         if not ok then
  152.             if err and err ~= "" then
  153.                 error( err )
  154.             end
  155.             return false
  156.         end
  157.         return true
  158.     end
  159.     if err and err ~= "" then
  160.         error( err )
  161.     end
  162.     return false
  163. end
  164.  
  165. _G['tAPIsLoading'] = {}
  166. function r.os.loadAPI( _sPath )
  167.     local sName = filesystem.name( _sPath )
  168.     if _G['tAPIsLoading'][sName] == true then
  169.         error( "API "..sName.." is already being loaded" )
  170.         return false
  171.     end
  172.     _G['tAPIsLoading'][sName] = true
  173.  
  174.     local tEnv = {}
  175.     setmetatable( tEnv, { __index = _G } )
  176.     local fnAPI, err = r.loadfile( _sPath, tEnv )
  177.     if fnAPI then
  178.         local ok, err = pcall( fnAPI )
  179.         if not ok then
  180.             error( err )
  181.             _G['tAPIsLoading'][sName] = nil
  182.             return false
  183.         end
  184.     else
  185.         error( err )
  186.         _G['tAPIsLoading'][sName] = nil
  187.         return false
  188.     end
  189.    
  190.     local tAPI = {}
  191.  
  192.     for k,v in pairs( tEnv ) do
  193.         if k ~= "_ENV" then
  194.             tAPI[k] =  v
  195.         end
  196.     end
  197.  
  198.     _G[sName] = tAPI    
  199.     _G['tAPIsLoading'][sName] = nil
  200.     return true
  201. end
  202.  
  203. function r.os.unloadAPI( _sName )
  204.     if _sName ~= "_G" and type(_G[_sName]) == "table" then
  205.         _G[_sName] = nil
  206.     end
  207. end
  208.  
  209. r.ccdefs_unload = function(self)
  210.    colors = require"colors"
  211.    self.fs = nil
  212.    self.unpack = nil
  213.    self.keys = nil
  214.    paintutils = nil
  215.    write = nil
  216.    keys = nil
  217.    fs = nil
  218.    os = nil
  219.    isCC = nil
  220.    read = nil
  221.    r.nfsopen = nil
  222.    r.shell = require"shell"
  223.    r.shell.dir = nil
  224.    r.shell.setDir = nil
  225.    r.unpack = table.unpack
  226.    r.term = require"term"
  227.    colors = require"colors"
  228.    term = require"term"
  229.    r.write = nil
  230.    term.getSize = nil
  231.    r.os = nil
  232.    os = require"os"
  233.    sleep = nil
  234.    paintutils = nil
  235.    term.setCursorPos = nil
  236.    term.getCursorPos = nil
  237.    term.setBackgroundColor = nil
  238.    term.getBackgroundColor = nil
  239.    term.setTextColor = nil
  240.    term.getTextColor = nil
  241.    term.current = nil
  242.    read = nil
  243. end
  244.  
  245. r.init = function(self)
  246.    colors = newcolors
  247.    paintutils = self.paintutils
  248.    write = self.write
  249.    self.fs = {
  250.       open = self.nfsopen,
  251.       isDir = filesystem.isDirectory,
  252.       exists = filesystem.exists,
  253.       copy = filesystem.copy,
  254.       list = function(_Path)
  255.         local files = {}
  256.         for file in filesystem.list(_Path) do
  257.            files[#files+1] = file
  258.         end
  259.         return files
  260.       end,
  261.       move = filesystem.rename,
  262.       delete = filesystem.remove,
  263.       getName = filesystem.name,
  264.    }
  265.    self.unpack = table.unpack
  266.    self.keys = keyboard.keys
  267.    self.keys.leftCtrl = keyboard.keys.lcontrol
  268.    self.keys.rightCtrl = keyboard.keys.rcontrol
  269.    keys = self.keys
  270.    fs = self.fs
  271.    os = self.os
  272.    sleep = os.sleep
  273.    isCC = function() return true end
  274.    r.read = function(pwdchar)
  275.        return term.read(nil,nil,nil,pwdchar)
  276.    end
  277.    ccdefs_unload = function() r:ccdefs_unload() end
  278.    read = self.read
  279.    table.unpack(self)
  280. end
  281. return r
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement