turtle5204

mcbios

Jan 1st, 2016
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. --Put YOUR code into the function main()
  2. --Tip: I think EEPROMs have a max capacity of 4K.
  3.  
  4.  
  5. local function create( first, ... )
  6. if first ~= nil then
  7. if type( first ) ~= "function" then
  8. error( "Expected function, got "..type( first ), 3 )
  9. end
  10. return coroutine.create(first), create( ... )
  11. end
  12. return nil
  13. end
  14.  
  15. local function runUntilLimit( _routines, _limit )
  16. local count = #_routines
  17. local living = count
  18.  
  19. local tFilters = {}
  20. local eventData = {}
  21. while true do
  22. for n=1,count do
  23. local r = _routines[n]
  24. if r then
  25. if tFilters[r] == nil or tFilters[r] == eventData[1] or eventData[1] == "terminate" then
  26. local ok, param = coroutine.resume( r, table.unpack(eventData) )
  27. if not ok then
  28. error( param, 0 )
  29. else
  30. tFilters[r] = param
  31. end
  32. if coroutine.status( r ) == "dead" then
  33. _routines[n] = nil
  34. living = living - 1
  35. if living <= _limit then
  36. return n
  37. end
  38. end
  39. end
  40. end
  41. end
  42. for n=1,count do
  43. local r = _routines[n]
  44. if r and coroutine.status( r ) == "dead" then
  45. _routines[n] = nil
  46. living = living - 1
  47. if living <= _limit then
  48. return n
  49. end
  50. end
  51. end
  52. eventData = { os.pullEventRaw() }
  53. end
  54. end
  55.  
  56. local function waitForAny( ... )
  57. local routines = { create( ... ) }
  58. return runUntilLimit( routines, #routines - 1 )
  59. end
  60.  
  61. function waitForAll( ... )
  62. local routines = { create( ... ) }
  63. runUntilLimit( routines, 0 )
  64. end
  65.  
  66.  
  67. local bios = component.proxy( component.list("eeprom")() )
  68. local m = component.proxy( component.list("modem")() )
  69.  
  70. local port = 8192
  71. local sendport = 8193
  72.  
  73. local whitelist = false
  74. local allowed = {} --Add a list of allowed network cards, must be strings, whitelist must be true.
  75.  
  76. local allowExec = true --Allow the reqType "load"
  77.  
  78. local function isAllowed(addr)
  79. if not whitelist then return true end
  80. for k,v in pairs(allowed) do
  81. if v == addr then
  82. return true
  83. end
  84. end
  85. return false
  86. end
  87.  
  88. m.open(port)
  89.  
  90. local function flashy()
  91. while true do
  92. local ev, _, from, portt, _, mes = computer.pullSignal()
  93. if ev == "modem_message" then
  94. if portt == port and isAllowed(from) and type(mes) == "string" then
  95. if mes:sub(1,6) == "flash;" and mes:sub(7) ~= nil then
  96. bios.set(mes:sub(7) )
  97. computer.beep(700, 0.5)
  98. elseif mes:sub(1,4) == "get;" then
  99. m.send(from, sendport, bios.getData() )
  100. elseif mes:sub(1,5) == "load;" and mes:sub(6) ~= nil and allowExec == true then
  101. load( mes:sub(6) )()
  102. elseif mes:sub(1,4) == "beep" then
  103. computer.beep(700, 0.5)
  104. end
  105. end
  106. end
  107. end
  108. end
  109.  
  110. local function main()
  111. --Put your stuff here
  112. end
  113.  
  114. waitForAll(main,flashy)
Advertisement
Add Comment
Please, Sign In to add comment