Advertisement
johnnic431

revolutOS Alpha

Jun 19th, 2014
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.43 KB | None | 0 0
  1. --[[
  2.     revolutOS alpha by Sylvyrfysh
  3. ]]
  4.  
  5. --version vars
  6. local maj_build="alpha_1.0";
  7. local rev=5;
  8. local build=32;
  9.  
  10. --putting the version together
  11. local version=maj_build.."."..rev.." (build num "..build..")";
  12.  
  13. --file system info
  14. local dir=".revolutOS/";
  15. local apis=dir.."apis/";
  16. local userdat=dir.."users/";
  17. local sys=dir..".sys/";
  18. local apis=dir.."APIS/";
  19. local cfgFile=dir.."cfg";
  20. local logFile=dir.."log.txt";
  21. local pssFile="/.hpc";
  22. local uFile="/prefs";
  23. local public="rOS-public/";
  24. local pubScrSave=public.."screenshots/";
  25.  
  26. --screen info
  27. local sX,sY=term.getSize();
  28.  
  29. --temp login info
  30. local usnm="";
  31. local pss="";
  32. local desktop={};
  33.  
  34. --user info
  35. local user;
  36.  
  37. --prefs value lookup
  38. prefs={
  39.     --colors
  40.     ["tColor"]=(function()
  41.         if term.isColor() then
  42.             return colors.gray
  43.         else
  44.             return colors.white
  45.         end
  46.     end)(),
  47.     ["bColor"]=(function()
  48.         if term.isColor() then
  49.             return colors.white
  50.         else
  51.             return colors.black
  52.         end
  53.     end)(),
  54.     ["eColor"]=(function()
  55.         if term.isColor() then
  56.             return colors.red
  57.         else
  58.             return colors.white
  59.         end
  60.     end)(),
  61.     ["oColor"]=(function()
  62.         if term.isColor() then
  63.             return colors.blue
  64.         else
  65.             return colors.white
  66.         end
  67.     end)(),
  68. }
  69.  
  70. --Log
  71. logger={
  72.     add=function(message,tag)
  73.         file=fs.open(logFile,"a");
  74.         if tag then
  75.             file.write(tag..string.rep(" ",19-#tag).."> "..message.."\n");
  76.         else
  77.             file.write("tagless"..string.rep(" ",19-#"tagless").."> "..message.."\n");
  78.         end
  79.         file.close();
  80.     end
  81. }
  82.  
  83. --functions
  84. fc={
  85.     boot=function()
  86.         file=fs.open(logFile,"w");
  87.         file.write("revolutOS LOG FILE- ver "..version.."\n");
  88.         file.close();
  89.         if term.isColor() then
  90.             logger.add("Is color","boot:info");
  91.         else
  92.             logger.add("Not color","boot:info");
  93.         end
  94.         os.loadAPI(apis.."sha");
  95.         os.loadAPI(apis.."json");
  96.         os.loadAPI(apis.."graphics");
  97.         t=textutils;
  98.         tm=term;
  99.         eLoginInfo="";
  100.         pc=paintutils;
  101.         fc.wrapPeripherals();
  102.     end,
  103.     loginScreen=function()
  104.         logger.add("Begin login","booted:login");
  105.         fc.p.paintFullScreen(bgCol);
  106.         tm.setCursorPos(3,3);
  107.         tm.setTextColor(colors.white);
  108.         write("Username: ");
  109.         pc.drawLine(13,3,sX-13,3,colors.white);
  110.         tm.setCursorPos(3,5);
  111.         tm.setBackgroundColor(bgCol);
  112.         write("Password: ")
  113.         pc.drawLine(13,5,sX-13,5,colors.white);
  114.         tm.setCursorPos(13,3);
  115.         tm.setTextColor(colors.black);
  116.         write(usnm);
  117.         tm.setCursorPos(13,5);
  118.         for i=1,#pss do
  119.             write("*");
  120.         end
  121.         term.setTextColor(colors.red);
  122.         term.setCursorPos(13,9);
  123.         if eLoginInfo then
  124.             write(eLoginInfo);
  125.         end
  126.         enterpss=false;
  127.         os.startTimer(0.5);
  128.         blink=false;
  129.         while true do
  130.             local event={os.pullEvent()};
  131.             if event[1]:upper()=="TIMER" then
  132.                 os.startTimer(1);
  133.                 blink=not blink;
  134.             elseif enterpss then
  135.                 if event[1]=="char" then
  136.                     pss=pss..event[2];
  137.                 elseif event[1]=="key" then
  138.                     if event[2]==keys.enter then
  139.                         break;
  140.                     elseif event[2]==keys.backspace then
  141.                         pss=pss:sub(1,#pss-1);
  142.                     end
  143.                 end
  144.             else
  145.                 if event[1]=="char" then
  146.                     usnm=usnm..event[2];
  147.                 elseif event[1]=="key" then
  148.                     if event[2]==keys.enter then
  149.                         enterpss=true;
  150.                     elseif event[2]==keys.backspace then
  151.                         usnm=usnm:sub(1,#usnm-1);
  152.                     end
  153.                 end
  154.             end
  155.             pc.drawLine(13,3,sX-13,3,colors.white);
  156.             pc.drawLine(13,5,sX-13,5,colors.white);
  157.             tm.setCursorPos(13,3);
  158.             term.setTextColor(colors.black);
  159.             write(usnm);
  160.             tm.setCursorPos(13,5);
  161.             for i=1,#pss do
  162.                 write("*");
  163.             end
  164.             if blink then
  165.                 if enterpss then
  166.                     pc.drawLine(13+#pss,5,13+#pss,5,colors.green);
  167.                 else
  168.                     pc.drawLine(13+#usnm,3,13+#usnm,3,colors.green);
  169.                 end
  170.             end
  171.         end
  172.         logger.add("End login input","booted:logging_in");
  173.         logger.add("Userfile exists? "..tostring(fs.exists(userdat..usnm)),"booted:logging_in");
  174.         function g()
  175.             if fs.exists(userdat..usnm) then
  176.                 logger.add(userdat..usnm..pssFile,"booted:login_info");
  177.                 w=fs.open(userdat..usnm..pssFile,"r");
  178.                 readPss=w.readLine();
  179.                 if sha.sha256(pss)==readPss then
  180.                     logger.add("Logged in as "..usnm,"booted:login_done");
  181.                     user=usnm;
  182.                     return true;
  183.                 else
  184.                     logger.add("Bad password","booted:login_info");
  185.                     eLoginInfo="Incorrect password.";
  186.                     return false;
  187.                 end
  188.             else
  189.                 logger.add("Bad username","booted:login_info");
  190.                 eLoginInfo="Bad username.";
  191.                 return false;
  192.             end
  193.         end
  194.         if not g() then
  195.             pss="";
  196.             fc.loginScreen();
  197.         else
  198.             pss="";
  199.             fc.loadUserPreferences(user);
  200.             fc.desktop();
  201.         end
  202.     end,
  203.     wrapPeripherals=function()
  204.         for k,v in pairs(rs.getSides()) do
  205.             temp=peripheral.getType(v)
  206.             if temp then
  207.                 logger.add("Found peripheral "..temp.." on side "..v,"boot:peripherals");
  208.                 if temp=="modem" and peripheral.call(v,"isWireless") then
  209.                     modem=peripheral.wrap(v);
  210.                 elseif temp=="modem" and not peripheral.call(v,"isWireless") then
  211.                     w_modem=peripheral.wrap(v);
  212.                 end
  213.             end
  214.         end
  215.     end,
  216.     loadUserPreferences=function(name)
  217.         logger.add("Loading "..user.."'s preferences.","booted:logging_in");
  218.         if fs.exists(userdat..user..uFile) then
  219.             local uf=fs.open(userdat..user..uFile,"r");
  220.             local data=json.decode(uf.readAll());
  221.             uf.close();
  222.             shell.run("clear");
  223.             for t,y in pairs(data.tree) do
  224.                 fc.decodePref(y);
  225.             end
  226.         else
  227.             logger.add("User has no custom prefs. loading default","booted:logging_in");
  228.         end
  229.     end,
  230.     decodePref=function(obj)
  231.         logger.add("Decoding "..obj.name,"booted:logging_in");
  232.         name=obj.name;
  233.         value=obj.value;
  234.         if prefs[name] then
  235.             logger.add(obj.name.." has entry","booted:logging_in");
  236.             prefs[name]=value;
  237.         end
  238.     end,
  239.     desktop=function()
  240.         graphics.paintFullScreen(prefs["bColor"]);
  241.         graphics.paintSizedBorder(1,1,sX,sY-5,prefs["oColor"]);
  242.         pc.drawImage(pc.loadImage(sys.."SysInfoLogo"),1,sY-4);
  243.         pc.drawImage(pc.loadImage(sys.."ShutdownOptions"),7,sY-4);
  244.         term.setCursorPos(sX-(#user),2);
  245.         write(user);
  246.         fc.startEventHandler();
  247.     end,
  248.     eventHandler=function(event)
  249.         logger.add("event handler called","eventhandler");
  250.        
  251.     end,
  252.     startEventHandler=function()
  253.         c=coroutine.create((function()
  254.             while true do
  255.                 x={os.pullEvent()};
  256.                 fc.eventHandler(x);
  257.             end
  258.         end)());
  259.         coroutine.resume(c);
  260.     end,
  261. }
  262.  
  263. if args then
  264.     if not args[1]=="--asapi" then
  265.         fc.boot();
  266.         if #fs.list(".revolutOS/users/")==0 then
  267.             fc.mkusr();
  268.         else
  269.             fc.loginScreen();
  270.         end
  271.     end
  272. else
  273.     fc.boot();
  274.     if #fs.list(".revolutOS/users/")==0 then
  275.         fc.mkusr();
  276.     else
  277.         fc.loginScreen();
  278.     end
  279. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement