Advertisement
5bitesofcookies

askapro-client

Jul 7th, 2014
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.12 KB | None | 0 0
  1. --set to WIRED modem side:
  2. local wiredModemSide="back"
  3.  
  4. --currently only re-does fs functions that are actually usefull. some of them make me want to facepalm
  5. modem = peripheral.wrap(wiredModemSide)
  6. modem.open(1)
  7. --set old fs vars
  8. oldFS = {}
  9. for i,g in pairs(fs) do
  10.     oldFS[i]=g
  11. end
  12. function listenForModemR(timeout)
  13.   local timer = os.startTimer(timeout)
  14.   while true do
  15.      local evt,type,_,id,msg = coroutine.yield()
  16.      if evt == "timer" and type == timer then
  17.         return false
  18.      elseif evt == "modem_message" then
  19.           if id ==99 then --server id
  20.           return msg,id
  21.         end
  22.       end
  23.     end
  24. end
  25. function fs.open(file, mode)
  26.     if not file or not mode then
  27.         return false
  28.     end
  29.     --check if it's attempting to access ROM
  30.     if string.sub(file,1,3) == "rom" then
  31.         return oldFS.open(file,mode)
  32.     else--we're good to run our code
  33.         modem.transmit(1,1,"e "..file)
  34.         doesExist, id = listenForModemR(1)
  35.         if doesExist and id then
  36.             if id==99 then
  37.                 --okay, it exists, now let's return our own special table
  38.                 local returnTable = {}
  39.                 if mode == "r" then
  40.                     --we need to do this in a string first so we can have variable code
  41.                     local env = {}
  42.                     env["modem"] = modem
  43.                     env["listenForModemR"] = listenForModemR
  44.                     env["textutils"] = textutils
  45.                     setmetatable(env,{__index=getfenv()})
  46.                     local writeDisString = 'modem.transmit(1,1,"g '..file..'") local msg=listenForModemR(10) if msg then return msg else return "Some sort of error" end'
  47.           returnTable.read = loadstring(writeDisString)
  48.           setfenv(returnTable.read, env)
  49.                     returnTable.readLine = returnTable.read
  50.                     function returnTable.close() end --nothing really needs to be done.
  51.                     --it's already been closed on the server. And if the user did not localize
  52.                     --the variable then it's their fault.
  53.                     return returnTable
  54.                 end
  55.             end
  56.         end
  57.         if mode == "w" or mode =="a" then
  58.             local returnTable={}
  59.              writeDisString = ' args ={...}\nmodem.transmit(1,1,"w {\\""..args[1].."\\", \\"'..file..'\\"}")'
  60.       theThing = loadstring(writeDisString)
  61.       local env = {}
  62.       env["textutils"]=textutils
  63.       env["args"]=args
  64.             setmetatable(env,{__index=_G})
  65.             setfenv(theThing, env)
  66.             function returnTable.write(...)
  67.         args = {...}
  68.         print"ran"
  69.         theThing(unpack(args))
  70.       end
  71.       returnTable.writeLine=returnTable.write
  72.       function returnTable.close()
  73.         thisIsAnotherReallyLongVar = nil
  74.       end
  75.       function returnTable.flush() end--no need, but we don't want errors.
  76.       return returnTable
  77.     end
  78.   end
  79. end
  80. function fs.exists(file)
  81.     if not file then
  82.         return false
  83.     end
  84.     modem.transmit(1,1,"e "..file)
  85.     return listenForModemR(10)
  86. end
  87. function fs.copy(file1,file2)
  88.     --first read the thingy
  89.     local readDis = fs.open(file1,"r")
  90.     local readDis=readDis.readAll()
  91.     --we no has 2 close, remember?
  92.   --write it
  93.   local writeDis = fs.open(file2,"w")
  94.   writeDis.write(readDis)
  95.   writeDis.close() --good to close when writing
  96. end
  97. function fs.isReadOnly(path)
  98.     path = string.sub(path,1,3)
  99.     if path=="rom" then
  100.         return true
  101.     else
  102.         return false
  103.     end--I can gurantdamntee you nothing is read only on the server
  104. end
  105. function fs.makeDir(dir)
  106.     modem.transmit(1,1,"m "..dir)
  107. end
  108. function fs.getFreeSpace(dir)
  109.   --check where
  110.   local dirSl = string.find(dir, "/")
  111.   local dirS = string.sub(dir,1,dirSl-1)
  112.   if dirS =="rom" then
  113.     return oldFS.getFreeSpace(dir)
  114.   elseif string.find(dir, "disk") then
  115.     return oldFs.getFreeSpace(dir)
  116.   else
  117.     modem.transmit(1,1,"f")
  118.     return listenForModemR(10)
  119.   end
  120. end
  121. function fs.delete(dir)
  122.     modem.transmit(1,1,"d "..dir)
  123. end
  124. function fs.move(dir,dir2)
  125.   fs.copy(dir,dir2)
  126.   fs.delete(dir)
  127.   --this kind of crap is what makes me want to facepalm. fs is an incredably bloated mostly useless API
  128.   --and only few functions do things that require more than a few lines!
  129. end
  130. function fs.isDir()
  131.     return false
  132. end
  133. function fs.combine(thisFunction,isALoadOfCrap)
  134.     if fs.exists(thisFunction) and fs.exists(isALoadOfCrap) then
  135.         return thisFunction.."/"..isALoadOfCrap
  136.     else
  137.         return false
  138.     end
  139. end
  140. --i will do io in a future version
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement