negamartin

gOS

Feb 4th, 2015
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.72 KB | None | 0 0
  1. --gOS by negamartin
  2. function runFree()  --Will run directly from the BIOS, no fussy shell sandbox
  3.     os.shutdown=sd;sd=nil;
  4.     print=np;np=nil;
  5.    
  6.     local ok,err=pcall(function()
  7.  
  8.  
  9.  
  10.  
  11. --Visual stuff
  12. local realWidth;
  13. local realHeight;
  14. realWidth,realHeight=term.getSize();
  15. local xOffset=0;
  16. local yOffset=1;
  17. local fakeWidth=realWidth;
  18. local fakeHeight=realHeight-2;
  19. local isBlinking=false;
  20.  
  21. local nativeSCP=term.setCursorPos;
  22. local nativeGCP=term.getCursorPos;
  23. local nativeClear=term.clear;
  24. local nativeCL=term.clearLine;
  25. local nativeBlink=term.setCursorBlink;
  26. term.setCursorPos=function(x,y)
  27.     if math.floor(x)>fakeWidth or math.floor(y)>fakeHeight or math.floor(x)<1 or math.floor(y)<1 then
  28.         error("Cursor out of bounds",2);
  29.     end
  30.     nativeSCP(x+xOffset,y+yOffset);
  31. end
  32. term.getCursorPos=function()
  33.     local x;
  34.     local y;
  35.     x,y=nativeGCP();
  36.     return x-xOffset,y-yOffset;
  37. end
  38. term.getSize=function()return fakeWidth,fakeHeight; end
  39. term.clear=function()
  40.     local x;
  41.     local y;
  42.     x,y=nativeGCP();
  43.     for i=1+yOffset,yOffset+fakeHeight do
  44.         nativeSCP(1+xOffset,i);
  45.         term.write(string.rep(" ",fakeWidth));
  46.     end
  47.     nativeSCP(x,y);
  48. end
  49. term.clearLine=function()
  50.     local x;
  51.     local y;
  52.     x,y=nativeGCP();
  53.     nativeSCP(1+xOffset,y);
  54.     term.write(string.rep(" ",fakeWidth));
  55.     nativeSCP(x,y);
  56. end
  57. term.setCursorBlink=function(blink)
  58.     if type(blink)~="boolean" then error("Expected boolean",2); end
  59.     isBlinking=blink;
  60.     nativeBlink(blink);
  61. end
  62.  
  63. --Important stuff
  64. function loadCoroutine(filename)
  65.     local func,err=loadfile(filename);
  66.     if func then
  67.         return true,coroutine.create(func);
  68.     else
  69.         return false,err;
  70.     end
  71. end
  72.  
  73. local modem=peripheral.wrap("back");
  74. local running;
  75.  
  76. function redrawOnScreen()
  77.     local x;
  78.     local y;
  79.     x,y=nativeGCP();
  80.     nativeSCP(1,1);
  81.     term.setBackgroundColor(colors.black);
  82.     nativeCL();
  83.     term.setTextColor(colors.white);
  84.     nativeSCP(realWidth-2,1);
  85.     if modem then
  86.         term.write("(o)");
  87.     else
  88.         term.write("(x)");
  89.     end
  90.     nativeSCP((realWidth/2)-(#"(HOME)"/2),realHeight);
  91.     nativeCL();
  92.     term.write("(HOME)");
  93.     nativeSCP(x,y);
  94. end
  95.  
  96. local runningX=1+xOffset;
  97. local runningY=1+yOffset;
  98. local runningBlink=false;
  99. function resumeRunning(ev)
  100.     nativeSCP(runningX,runningY);
  101.     nativeBlink(runningBlink);
  102.     local ok;
  103.     local err;
  104.     ok,err=coroutine.resume(running,unpack(ev));
  105.     if not ok then
  106.         error(err);
  107.     end
  108.     runningBlink=isBlinking;
  109.     runningX,runningY=nativeGCP();
  110. end
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118. local ok;
  119. local ev={};
  120. ok,running=loadCoroutine("launcher.lua");
  121. if not ok then
  122.     error(running);
  123. end
  124. print("Launcher loaded");
  125. redrawOnScreen();
  126. while true do
  127.     if ev[1]=="mouse_click" then
  128.         if ev[3]>xOffset and ev[3]<=xOffset+fakeWidth and ev[4]>yOffset and ev[4]<=yOffset+fakeHeight then
  129.             resumeRunning(ev);
  130.         end
  131.         redrawOnScreen();
  132.     else
  133.         resumeRunning(ev);
  134.     end
  135.     ev={os.pullEvent()};
  136. end
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.    
  171.     end);
  172.     if not ok then
  173.         print(err);
  174.         sleep(3);
  175.     end
  176. end
  177. if not pocket then
  178.     print("gOS is intended for pocket use");
  179.     error();
  180. end
  181. if not term.isColor() then
  182.     print("gOS only works on colored pocket computers");
  183.     print("If you ARE making one, better waste some gold, right?");
  184.     error();
  185. end
  186. --Check End
  187.  
  188. local sd=os.shutdown;
  189. local np=print;
  190.  
  191.  
  192. term.clear();
  193. term.setCursorPos(1,1);
  194. print("Starting gOS...");
  195. local timer=os.startTimer(1);
  196. local start=true;
  197. while true do
  198.     local ev={os.pullEvent()};
  199.     if ev[1]=="timer" and ev[2]==timer then
  200.         start=true;
  201.         break;
  202.     elseif ev[1]=="key" and ev[2]==keys.f12 then
  203.         start=false;
  204.         break;
  205.     end
  206. end
  207. if start then
  208.     print=function()end;
  209.     getfenv(rednet.run).isOpen=error;
  210.     os.shutdown=runFree;
  211.     os.startTimer(0.2);
  212.     os.queueEvent("modem_message");
  213. else
  214.     print("Start Canceled");
  215. end
Advertisement
Add Comment
Please, Sign In to add comment