Advertisement
wow0

Untitled

Oct 27th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.97 KB | None | 0 0
  1. local function parseArgs(params)
  2. local args = {}
  3. local options = {}
  4. for key,data in ipairs(params) do
  5. if type(data) == "string" then
  6. if data:find("--") == 1 then
  7. data = data:sub(3,-1)
  8. local head,body = data:match("([^=]+)=([^=]+)")
  9. if body ~= nil then
  10. options[head] = body
  11. else
  12. options[data] = true
  13. end
  14. elseif data:find("-") == 1 then
  15. data = data:sub(2,-1)
  16. for i=1,#data do
  17. options[data:sub(i,i)] = true
  18. end
  19. else
  20. table.insert(args,data)
  21. end
  22. end
  23. end
  24. return args,options
  25. end
  26.  
  27. local function tablecopy(tInput)
  28. local tCopy = {}
  29. for k,v in pairs(tInput) do
  30. tCopy[k] = v
  31. end
  32. return tCopy
  33. end
  34.  
  35. local argst,ops = parseArgs({...})
  36.  
  37. if ops.biosPath == nil then
  38. printError("Missed Argument --biosPath=Path")
  39. return
  40. end
  41. if ops.romPath == nil then
  42. printError("Missed Argument --romPath=Path")
  43. return
  44. end
  45. if ops.rootPath == nil then
  46. printError("Missed Argument --rootPath=Path")
  47. return
  48. end
  49.  
  50. local sBiosPath = fs.combine(ops.biosPath,"")
  51. local sRomPath = fs.combine(ops.romPath,"")
  52. local sRootPath = fs.combine(ops.rootPath,"")
  53. local bShutdown = false
  54. local bReeboot = false
  55. local sVirtualID = tostring(math.random(1,1000))
  56. local sShareName = ops.shareName or "share"
  57. local sSharePath
  58. if ops.sharePath ~= nil then
  59. sSharePath = fs.combine(ops.sharePath,"")
  60. end
  61. local tDisk = {}
  62.  
  63. local function addDisk()
  64. tDisk = {}
  65. for n,sName in pairs( peripheral.getNames() ) do
  66. if disk.isPresent(sName) and disk.hasData(sName) then
  67. table.insert( tDisk, disk.getMountPath(sName) )
  68. end
  69. end
  70. end
  71.  
  72. if ops.diskmount then
  73. addDisk()
  74. end
  75.  
  76. local tFs = {}
  77. for k,v in pairs(fs) do
  78. tFs[k] = v
  79. end
  80. local function getRealPath(sPath)
  81. sPath = fs.combine(sPath,"")
  82. if sPath:find("rom") == 1 then
  83. sPath = sPath:sub(4)
  84. sPath = fs.combine(sRomPath,sPath)
  85. return sPath
  86. elseif sPath:find(sShareName) == 1 and ops.sharePath then
  87. sPath = sPath:sub(#sShareName+1)
  88. return fs.combine(sSharePath,sPath)
  89. end
  90. for k,v in ipairs(tDisk) do
  91. if sPath:find(v) == 1 then
  92. sPath = sPath:sub(#v+1)
  93. return fs.combine(v,sPath)
  94. end
  95. end
  96. local sNewPath = fs.combine(sRootPath,sPath)
  97. return sNewPath
  98. end
  99.  
  100. function tFs.list(sPath)
  101. local sPath = getRealPath(sPath)
  102. local tList = fs.list(sPath)
  103. if sPath == sRootPath then
  104. table.insert(tList,"rom")
  105. if ops.sharePath then
  106. table.insert(tList,sShareName)
  107. end
  108. for k,v in ipairs(tDisk) do
  109. table.insert(tList,v)
  110. end
  111. end
  112. table.sort(tList)
  113. return tList
  114. end
  115.  
  116. function tFs.open(sPath,sMode)
  117. local sPath = getRealPath(sPath)
  118. return fs.open(sPath,sMode)
  119. end
  120.  
  121. function tFs.exists(sPath)
  122. local sPath = getRealPath(sPath)
  123. return fs.exists(sPath)
  124. end
  125.  
  126. function tFs.isDir(sPath)
  127. local sPath = getRealPath(sPath)
  128. return fs.isDir(sPath)
  129. end
  130.  
  131. function tFs.delete(sPath)
  132. local sPath = getRealPath(sPath)
  133. return fs.delete(sPath)
  134. end
  135.  
  136. function tFs.move(sPath,sTo)
  137. local sPath = getRealPath(sPath)
  138. local sTo = getRealPath(sTo)
  139. return fs.move(sPath,sTo)
  140. end
  141.  
  142. function tFs.copy(sPath,sTo)
  143. local sPath = getRealPath(sPath)
  144. local sTo = getRealPath(sTo)
  145. return fs.copy(sPath,sTo)
  146. end
  147.  
  148. function tFs.find(sPath)
  149. local sPath = getRealPath(sPath)
  150. local tResult = fs.find(sPath)
  151. for k,v in ipairs(tResult) do
  152. tResult[k] = v:sub(#sRootPath+2)
  153. end
  154. return tResult
  155. end
  156.  
  157. function tFs.getSize(sPath)
  158. local sPath = getRealPath(sPath)
  159. return fs.getSize(sPath)
  160. end
  161.  
  162. --For Pre 1.8 User
  163. local tPalette = {
  164. [1] = {240, 240, 240},
  165. [2] = {242, 178, 51},
  166. [4] = {229, 127, 216},
  167. [8] = {153, 178, 242},
  168. [16] = {222, 222, 108},
  169. [32] = {127, 204, 25},
  170. [64] = {242, 178, 204},
  171. [128] = {76, 76, 76},
  172. [256] = {153, 153, 153},
  173. [512] = {76, 153, 178},
  174. [1024] = {178, 102, 229},
  175. [2048] = {51, 102, 204},
  176. [4096] = {127, 102, 76},
  177. [8192] = {87, 166, 78},
  178. [16384] = {204, 76, 76},
  179. [32768] = {25, 25, 25},
  180. }
  181. local tTerm = {}
  182. for k,v in pairs(term.current()) do
  183. tTerm[k] = v
  184. end
  185. tTerm.setPaletteColor = tTerm.setPaletteColor or function(col,a,b,c) tPalette[col][1] = a*255 tPalette[col][2] = b*255 tPalette[col][3] = c*255 end
  186. tTerm.setPaletteColour = tTerm.setPaletteColour or function(col,a,b,c) tPalette[col][1] = a*255 tPalette[col][2] = b*255 tPalette[col][3] = c*255 end
  187. tTerm.getPaletteColor = tTerm.getPaletteColor or function(color) return tPalette[color][1]/255,tPalette[color][3]/255,tPalette[color][3]/255 end
  188. tTerm.getPaletteColour = tTerm.getPaletteColour or function(color) return tPalette[color][1]/255,tPalette[color][3]/255,tPalette[color][3]/255 end
  189. if ops.oldnative then
  190. tTerm.native = tTerm
  191. else
  192. tTerm.native = function() return tTerm end
  193. end
  194.  
  195. local nStarttime = os.clock()
  196. local tOs = {}
  197. local sLabel = ops.label
  198. local tTimer = {}
  199. local tTimerID = {}
  200. local nTimerCount = 0
  201. local tAlarm = {}
  202. local tAlarmID = {}
  203. local nAlarmCount = 0
  204. function tOs.startTimer(nTime)
  205. local nID = os.startTimer(nTime)
  206. tTimer[nID] = true
  207. tTimerID[nTimerCount] = nID
  208. nTimerCount = nTimerCount + 1
  209. return nTimerCount - 1
  210. end
  211. function tOs.setAlarm(nTime)
  212. local nID = os.setAlarm(nTime)
  213. tAlarm[nID] = true
  214. tAlarmID[nAlarmCount] = nID
  215. nAlarmCount = nAlarmCount + 1
  216. return nAlarmCount - 1
  217. end
  218. tOs.shutdown = function() bShutdown = true end
  219. tOs.reboot = function() bReeboot = true bShutdown = true end
  220. tOs.clock = function() return os.clock()-nStarttime end
  221. tOs.getComputerID = function() return math.floor(tonumber(ops.id)) or 0 end
  222. tOs.getComputerLabel = function() return sLabel end
  223. tOs.setComputerLabel = function(label) sLabel = label end
  224. tOs.queueEvent = function(...) os.queueEvent("VirtualOS_"..sVirtualID,...) end
  225. function tOs.cancelTimer(nID)
  226. for i=0, #tTimerID do
  227. if tTimerID[i] == nID then
  228. nID = i
  229. end
  230. end
  231. return os.cancelTimer(nID)
  232. end
  233. function tOs.cancelAlarm(nID)
  234. for i=0, #tAlarmID do
  235. if tAlarmID[i] == nID then
  236. nID = i
  237. end
  238. end
  239. return os.cancelAlarm(nID)
  240. end
  241. if ops.notime then
  242. tOs.time = function() return 7 end
  243. else
  244. tOs.time = os.time
  245. end
  246. if ops.noday then
  247. tOs.day = function() return 1 end
  248. else
  249. tOs.day = os.day
  250. end
  251. if ops.noepoch then
  252. tOs.epoch = function() return 111600000 end
  253. else
  254. tOs.epoch = os.epoch or function() return tOs.day() * 86400000 + (tOs.time() * 3600000) end
  255. end
  256.  
  257. local tHttp = {}
  258. tUrl = {}
  259. function tHttp.request(...)
  260. os.queueEvent("Hallo")
  261. local tArgu = {...}
  262. tUrl[tArgu[1]] = true
  263. return http.request(...)
  264. end
  265. function tHttp.checkURL(sUrl)
  266. tUrl[sUrl] = true
  267. local ok,err = http.checkURL(sUrl)
  268. os.queueEvent("http_check",sUrl,ok,err)
  269. return http.checkURL(sUrl)
  270. end
  271.  
  272. local tPer = {}
  273. if ops.noper then
  274. function tPer.isPresent() return false end
  275. function tPer.call() error("No peripheral attached",2) end
  276. function tPer.getType() return nil end
  277. function tPer.getMethods() return nil end
  278. else
  279. tPer.isPresent = peripheral.isPresent
  280. tPer.call = peripheral.call
  281. tPer.getType = peripheral.getType
  282. tPer.getMethods = peripheral.getMethods
  283. end
  284.  
  285. local tEnv = {}
  286. tEnv.ipairs = ipairs
  287. tEnv.type = type
  288. tEnv.rawget = rawget
  289. tEnv.rawset = rawset
  290. tEnv.rawequal = rawequal
  291. tEnv.setmetatable = setmetatable
  292. tEnv.getmetatable = getmetatable
  293. tEnv.loadstring = loadstring
  294. tEnv.setfenv = setfenv
  295. tEnv.pairs = pairs
  296. tEnv.rawset = rawset
  297. tEnv.getfenv = getfenv
  298. tEnv.pcall = pcall
  299. tEnv.xpcall = xpcall
  300. tEnv.tostring = tostring
  301. tEnv.tonumber = tonumber
  302. tEnv.unpack = unpack
  303. tEnv.load = load
  304. tEnv.select = select
  305. tEnv.next = next
  306. tEnv.error = error
  307. tEnv.assert = assert
  308. tEnv.dofile = dofile
  309. tEnv.loadstring = loadstring
  310. tEnv.loadfile = loadfile
  311. tEnv.peripheral = tPer
  312. tEnv.table = tablecopy(table)
  313. tEnv.math = tablecopy(math)
  314. tEnv.bit = tablecopy(bit)
  315. tEnv.rs = tablecopy(rs)
  316. tEnv.redstone = tablecopy(redstone)
  317. tEnv.fs = tFs
  318. tEnv.term = tTerm
  319. tEnv.os = tOs
  320. tEnv.coroutine = tablecopy(coroutine)
  321. tEnv.string = tablecopy(string)
  322. if turtle and not(ops.noturtle) then
  323. tEnv.turtle = tablecopy(turtle)
  324. end
  325. if pocket and not(ops.nopocket) then
  326. tEnv.pocket = tablecopy(pocket)
  327. end
  328. if commands and not(ops.nocomand) then
  329. tEnv.commands = tablecopy(commands)
  330. end
  331. if not ops.nohttp then
  332. tEnv.http = tHttp
  333. end
  334. if ops.diskapi then
  335. tEnv.disk = tablecopy(disk)
  336. end
  337. tEnv._HOST = ops.host or "VirtualOS 4.1"
  338. tEnv._CC_VERSION = ops.ccversion
  339. tEnv._MC_VERSION = ops.mcversion
  340. tEnv._VERSION = _VERSION
  341. tEnv._CC_DEFAULT_SETTINGS = ops.settings or ""
  342. tEnv._G = tEnv
  343.  
  344. local file = fs.open(sBiosPath,"r")
  345. if not file then
  346. error("bios not found",0)
  347. end
  348. local fn,err = load(file.readAll(),"@bios.lua")
  349. file.close()
  350. setfenv(fn,tEnv)
  351.  
  352. if multishell and ops.title ~= true then
  353. multishell.setTitle(multishell.getCurrent(),ops.title or "CraftOS")
  354. end
  355.  
  356. term.setTextColor(colors.white)
  357. term.setBackgroundColor(colors.black)
  358. term.clear()
  359. term.setCursorPos(1,1)
  360. term.setCursorBlink(false)
  361.  
  362. local tWhitelist = {key=true,key_up=true,char=true,mouse_click=true,mouse_scroll=true,mouse_drag=true,mouse_up=true,paste=true,monitor_touch=true,
  363. terminate=true,term_resize=true,modem_message=true,turtle_inventory=true,redstone=true,task_complete=true}
  364. local tHttpEvent = {http_success=true,http_failure=true,http_check=true}
  365. if not ops.noper then
  366. tWhitelist.peripheral = true
  367. tWhitelist.peripheral_detach = true
  368. end
  369.  
  370. local function start()
  371. local cor = coroutine.create(fn)
  372. local ok,sFilter = coroutine.resume(cor)
  373. while true do
  374. local tEventData = table.pack( os.pullEventRaw() )
  375. if tWhitelist[tEventData[1]] == true and (tEventData[1] == sFilter or sFilter == nil) then
  376. ok,sFilter = coroutine.resume(cor,table.unpack(tEventData))
  377. elseif tHttpEvent[tEventData[1]] == true and (tEventData[1] == sFilter or sFilter == nil) and tUrl[tEventData[2]] == true then
  378. tUrl[tEventData[2]] = nil
  379. ok,sFilter = coroutine.resume(cor,table.unpack(tEventData))
  380. elseif tEventData[1] == "timer" and (tEventData[1] == sFilter or sFilter == nil) and tTimer[tEventData[2]] == true then
  381. tTimer[tEventData[2]] = nil
  382. for i=0, #tTimerID do
  383. if tTimerID[i] == tEventData[2] then
  384. tEventData[2] = i
  385. end
  386. end
  387. ok,sFilter = coroutine.resume(cor,table.unpack(tEventData))
  388. elseif tEventData[1] == "alarm" and (tEventData[1] == sFilter or sFilter == nil) and tAlarm[tEventData[2]] == true then
  389. tAlarm[tEventData[2]] = nil
  390. for i=0, #tAlarmID do
  391. if tAlarmID[i] == tEventData[2] then
  392. tEventData[2] = i
  393. end
  394. end
  395. ok,sFilter = coroutine.resume(cor,table.unpack(tEventData))
  396. elseif (tEventData[1] == "disk" or tEventData[1] == "disk_eject") and (tEventData[2] == sFilter or sFilter == nil) and ops.diskmount then
  397. addDisk()
  398. ok,sFilter = coroutine.resume(cor,table.unpack(tEventData))
  399. elseif (tEventData[1] == "VirtualOS_"..sVirtualID or tEventData[1] == "VirtualOS_Event") and (tEventData[2] == sFilter or sFilter == nil) then
  400. table.remove(tEventData,1)
  401. ok,sFilter = coroutine.resume(cor,table.unpack(tEventData))
  402. end
  403. if bShutdown == true then
  404. break
  405. end
  406. end
  407. end
  408.  
  409. while true do
  410. start()
  411. if not(ops.noclear) or bReeboot then
  412. term.setTextColor(colors.white)
  413. term.setBackgroundColor(colors.black)
  414. term.clear()
  415. term.setCursorPos(1,1)
  416. term.setCursorBlink(false)
  417. end
  418. if not bReeboot then
  419. break
  420. else
  421. tTimer = {}
  422. tTimerID = {}
  423. nTimerCount = 0
  424. tAlarm = {}
  425. tAlarmID = {}
  426. nAlarmCount = 0
  427. bShutdown = false
  428. bReeboot = false
  429. end
  430. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement