Wiiplay123

WDOS v1.0

Dec 26th, 2013
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.18 KB | None | 0 0
  1. -- WDOS - Wii's Disk Operating System
  2.  
  3. wdos = {defaultModules = {}, oscolor = colors.lime,version = "WDOS v1.0", shutdown = (function() wdos.printOs("Shutting down...") sleep(2) os.shutdown() end)}
  4. wdos.defaultModules.shutdown = "wdos.shutdown()"
  5. wdos.defaultModules.reboot = "wdos.printOs(\"Rebooting...\") sleep(2) os.reboot()"
  6. wdos.defaultModules.disks = [[
  7. local p = peripheral.getNames()
  8. for k,v in pairs(p) do
  9.     if fs.exists(disk.getMountPath(v).."/wdos_disk") then
  10.         local f = fs.open(disk.getMountPath(v).."/wdos_disk","r")
  11.         local f2 = textutils.unserialize(f.readAll())
  12.         f.close()
  13.         if f2 ~= nil then
  14.             if f2[name] ~= nil then
  15.                 wdos.writeOs(v..": ",colors.white)
  16.                 write(f2[name])
  17.             end
  18.         end
  19.     end
  20. end
  21. ]]
  22. wdos.defaultModules.install = [[
  23. local p = peripheral.getNames()
  24. local tab = {}
  25. for k,v in pairs(p) do
  26.     if fs.exists(disk.getMountPath(v).."/wdos_disk") then
  27.         local f = fs.open(disk.getMountPath(v).."/wdos_disk","r")
  28.         local f2 = textutils.unserialize(f.readAll())
  29.         f.close()
  30.         if f2 ~= nil and fs.exists(disk.getMountPath(v).."/module") then
  31.             if string.lower(f2[name]) == string.lower(({...})[1]) then
  32.                 table.insert(tab,v)
  33.             end
  34.         end
  35.     end
  36. end
  37. if #tab == 1 then
  38.     local f = fs.open(disk.getMountPath(tab[1]).."/wdos_disk","r")
  39.     local f2 = textutils.unserialize(f.readAll())
  40.     f.close()
  41.     if fs.exists(disk.getMountPath(tab[1]).."/module") then
  42.         if string.lower(f2[name]) ~= nil then
  43.             if fs.exists(f2[name]) then
  44.                 fs.delete()
  45.             end
  46.             fs.copy(disk.getMountPath(tab[1]).."/module",".wdos/"..f2[name])   
  47.         end
  48.     end
  49. elseif #tab > 1 then
  50.     local choice = wdos.listSelector("More than one source disk found.")
  51.     local f = fs.open(disk.getMountPath(tab[choice]).."/wdos_disk","r")
  52.     local f2 = textutils.unserialize(f.readAll())
  53.     f.close()
  54.     if f2 ~= nil and fs.exists(disk.getMountPath(tab[1]).."/module") then
  55.         if string.lower(f2[name]) ~= nil then
  56.             if fs.exists(f2[name]) then
  57.                 fs.delete()
  58.             end
  59.             fs.copy(disk.getMountPath(tab[choice]).."/module",".wdos/"..f2[name])  
  60.         end
  61.     end
  62.     if fs.exists(f2[name]) then
  63.         fs.delete()
  64.     end
  65.     fs.copy(disk.getMountPath(tab[1]).."/module",".wdos/"..f2[name])
  66. else
  67.     printError("No such module.")
  68. end
  69. ]]
  70. wdos.defaultModules.uninstall = [[
  71. if ({...})[1] ~= nil then
  72.     if fs.exists(".wdos/"..({...})[1]) and fs.combine(({...})[1],".") ~= ".wdos" then
  73.         fs.delete(".wdos/"..({...})[1])
  74.     end
  75. end
  76. ]]
  77. wdos.defaultModules.run = [[
  78. shell.run(unpack({...}))
  79. ]]
  80. wdos.defaultModules.dir = [[
  81. shell.run("rom/programs/list "..({...})[1])
  82. ]]
  83. function os.pullEvent(filter)
  84.     local e = {os.pullEventRaw(filter)}
  85.     if e[1] == "terminate" then
  86.         wdos.shutdown()
  87.     end
  88.     return unpack(e)
  89. end
  90.  
  91. function wdos.split(pString, pPatte)
  92.     local Tae = {}
  93.     local pPattern = (pPatte ~= nil and pPatte or " ")
  94.     local fpat = "(.-)" .. pPattern
  95.     local last_end = 1
  96.     local s, e, cap = pString:find(fpat, 1)
  97.     while s do
  98.         if s ~= 1 or cap ~= "" then
  99.             table.insert(Tae,cap)
  100.         end
  101.         last_end = e+1
  102.         s, e, cap = pString:find(fpat, last_end)
  103.     end
  104.     if last_end <= #pString then
  105.         cap = pString:sub(last_end)
  106.         table.insert(Tae, cap)
  107.     end
  108.     return Tae
  109. end
  110.  
  111. function wdos.writeOs(txt,ret)
  112.     term.setTextColor(wdos.oscolor)
  113.     term.write(txt)
  114.     if ret ~= nil then
  115.         term.setTextColor(ret)
  116.     end
  117. end
  118.  
  119. function wdos.printOs(txt,ret)
  120.     wdos.writeOs(txt,ret)
  121.     print("")
  122. end
  123.  
  124. function wdos.listSelector(list,msg)
  125.     if type(list) ~= "table" then
  126.         error("Input to wdos.listSelector is not a table!")
  127.     end
  128.     wdos.printOs(msg)
  129.     for k,v in pairs(list) do
  130.         wdos.writeOs(k..". ",colors.white)
  131.         write(v)
  132.     end
  133.     local o = "0"
  134.     repeat
  135.         wdos.writeOs("> ",colors.white)
  136.         o = read()
  137.     until tonumber(o) >= 1 and tonumber(o) <= #tab
  138. end
  139.  
  140. function wdos.defaultCmdHandler(cmd)
  141.     local args = wdos.split(cmd)
  142.     if #args < 1 then
  143.         printError("No such command.")
  144.     end
  145.     local tab = {}
  146.     for i,v in pairs(fs.list(".wdos")) do
  147.         for p,o in pairs(fs.list(".wdos/"..v)) do
  148.             if (o == args[1]) then
  149.                 table.insert(tab,v)
  150.             end
  151.         end
  152.     end
  153.     if #tab > 1 then
  154.         local sel = wdos.listSelector("More than one package with this command found.",tab)
  155.         shell.run(".wdos/"..tab[sel].."/"..args[1],unpack(args,2))
  156.     elseif #tab == 1 then
  157.         shell.run(".wdos/"..tab[1].."/"..args[1],unpack(args,2))
  158.     else
  159.         printError("No such command.")
  160.     end
  161. end
  162.  
  163. function wdos.defaultShell()
  164.     term.clear()
  165.     term.setCursorPos(1,1)
  166.     wdos.printOs(wdos.version)
  167.     while true do
  168.         wdos.writeOs("> ",colors.white)
  169.         local cmd = read()
  170.         wdos.defaultCmdHandler(cmd)
  171.     end
  172. end
  173.  
  174. function wdos.init()
  175.     local tab = {}
  176.     if fs.exists(".wdos") == false then
  177.         fs.makeDir(".wdos")
  178.         fs.makeDir(".wdos/default/")
  179.         for k,v in pairs(wdos.defaultModules) do
  180.             local f = fs.open(".wdos/default/"..k,"w")
  181.             f.write(v)
  182.             f.close()
  183.         end
  184.     end
  185.     for i,v in pairs(fs.list(".wdos")) do
  186.         for p,o in pairs(fs.list(".wdos/"..v)) do
  187.             if (o == "shell") then
  188.                 table.insert(tab,v)
  189.             end
  190.         end
  191.     end
  192.     if #tab > 1 then
  193.         local sel = wdos.listSelector("More than one shell found.",tab)
  194.         shell.run(".wdos/"..tab[sel].."/shell",unpack(args,2))
  195.     elseif #tab == 1 then
  196.         shell.run(".wdos/"..tab[1].."/shell",unpack(args,2))
  197.     else
  198.         wdos.defaultShell()
  199.     end
  200. end
  201.  
  202. wdos.init()
Advertisement
Add Comment
Please, Sign In to add comment