Advertisement
ReIative

vncd from lyqyd github

Nov 19th, 2015
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 KB | None | 0 0
  1. local args = {...}
  2.  
  3. local connections = {}
  4.  
  5. local nshAPI = {
  6. connList = connections
  7. }
  8.  
  9. nshAPI.getRemoteID = function()
  10. return false
  11. end
  12.  
  13. nshAPI.send = function(msg)
  14. return false
  15. end
  16.  
  17. nshAPI.receive = function(timeout)
  18. return false
  19. end
  20.  
  21. nshAPI.getClientCapabilities = function()
  22. return false
  23. end
  24.  
  25. nshAPI.getRemoteConnections = function()
  26. local remotes = {}
  27. for cNum, cInfo in pairs(nshAPI.connList) do
  28. table.insert(remotes, cNum)
  29. if cInfo.outbound then
  30. table.insert(remotes, cInfo.outbound)
  31. end
  32. end
  33. return remotes
  34. end
  35.  
  36. nshAPI.packFile = function(path)
  37. return false
  38. end
  39.  
  40. nshAPI.unpackAndSaveFile = function(path, data)
  41. return false
  42. end
  43.  
  44. local packetConversion = {
  45. query = "SQ",
  46. response = "SR",
  47. data = "SP",
  48. close = "SC",
  49. textTable = "TT",
  50. event = "EV",
  51. SQ = "query",
  52. SR = "response",
  53. SP = "data",
  54. SC = "close",
  55. TT = "textTable",
  56. EV = "event",
  57. }
  58.  
  59. local function openModem()
  60. local modemFound = false
  61. for _, side in ipairs(rs.getSides()) do
  62. if peripheral.getType(side) == "modem" then
  63. if not rednet.isOpen(side) then rednet.open(side) end
  64. modemFound = true
  65. break
  66. end
  67. end
  68. return modemFound
  69. end
  70.  
  71. local function send(id, pType, message)
  72. if pType and message then
  73. if term.current then
  74. return rednet.send(id, packetConversion[pType]..":;"..message, "tror")
  75. else
  76. return rednet.send(id, packetConversion[pType]..":;"..message)
  77. end
  78. end
  79. end
  80.  
  81. local function resumeThread(co, event)
  82. if not co.filter or event[1] == co.filter then
  83. co.filter = nil
  84. local _old = term.redirect(co.target)
  85. local passback = {coroutine.resume(co.thread, unpack(event))}
  86. if _old then term.redirect(_old) else term.restore() end
  87. if passback[1] and passback[2] then
  88. co.filter = passback[2]
  89. end
  90. if coroutine.status(co.thread) == "dead" then
  91. for cNum, cInfo in pairs(connections) do
  92. send(cNum, "close", "disconnect")
  93. end
  94. connections = {}
  95. end
  96. if connections[conn] and conn ~= "localShell" and framebuffer then
  97. for cNum, cInfo in pairs(connections) do
  98. send(cNum, "textTable", textutils.serialize(co.target.buffer))
  99. end
  100. end
  101. framebuffer.draw(co.target.buffer)
  102. end
  103. end
  104.  
  105. if not openModem() then error("No modem present!") end
  106.  
  107. local bufferDirs = {"/","/LyqydOS/","/usr/apis/","/disk/"}
  108.  
  109. if not framebuffer then
  110. for i = 1, #bufferDirs do
  111. if fs.exists(bufferDirs[i].."framebuffer") and os.loadAPI(bufferDirs[i].."framebuffer") then
  112. break
  113. end
  114. end
  115. end
  116. if not framebuffer then
  117. print("Couldn't find framebuffer API!")
  118. return
  119. end
  120.  
  121. term.clear()
  122. term.setCursorPos(1,1)
  123. local x, y = term.getSize()
  124.  
  125. local redirect = framebuffer.new(x, y, term.isColor())
  126.  
  127. local shellRoutine = coroutine.create(function() shell.run("/rom/programs/shell", unpack(args)) end)
  128. local co = {thread = shellRoutine, target = redirect}
  129.  
  130. resumeThread(co, {})
  131.  
  132. _G.nsh = nshAPI
  133.  
  134. while coroutine.status(co.thread) ~= "dead" do
  135. event = {os.pullEventRaw()}
  136. if event[1] == "rednet_message" then
  137. if packetConversion[string.sub(event[3], 1, 2)] then
  138. --this is a packet meant for us.
  139. conn = event[2]
  140. packetType = packetConversion[string.sub(event[3], 1, 2)]
  141. message = string.match(event[3], ";(.*)")
  142. if connections[conn] and connections[conn].status == "open" then
  143. if packetType == "event" then
  144. local eventTable = textutils.unserialize(message)
  145. resumeThread(co, eventTable)
  146. elseif packetType == "query" then
  147. connections[conn] = {status = "open"}
  148. send(conn, "response", "OK")
  149. send(conn, "textTable", textutils.serialize(co.target.buffer))
  150. elseif packetType == "close" then
  151. connections[conn] = nil
  152. send(conn, "close", "disconnect")
  153. --close connection
  154. end
  155. elseif packetType ~= "query" then
  156. --usually, we would send a disconnect here, but this prevents one from hosting nsh and connecting to other computers. Pass these to all shells as well.
  157. resumeThread(co, event)
  158. else
  159. --open new connection
  160. connections[conn] = {status = "open"}
  161. send(conn, "response", "OK")
  162. send(conn, "textTable", textutils.serialize(co.target.buffer))
  163. end
  164. else
  165. --rednet message, but not in the correct format, so pass to all shells.
  166. resumeThread(co, event)
  167. end
  168. elseif event[1] == "terminate" then
  169. break
  170. else
  171. --dispatch all other events to all shells
  172. resumeThread(co, event)
  173. end
  174. end
  175.  
  176. for cNum, cInfo in pairs(connections) do
  177. send(cNum, "close", "disconnect")
  178. end
  179. _G.nsh = nil
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement