JustDoesGames

Windowscraft

Jan 5th, 2020
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.18 KB | None | 0 0
  1.  
  2. -- Windowscraft --
  3.  
  4. --[[
  5.  
  6. This is free and unencumbered software released into the public domain.
  7.  
  8. Anyone is free to copy, modify, publish, use, compile, sell, or
  9. distribute this software, either in source code form or as a compiled
  10. binary, for any purpose, commercial or non-commercial, and by any
  11. means.
  12.  
  13. In jurisdictions that recognize copyright laws, the author or authors
  14. of this software dedicate any and all copyright interest in the
  15. software to the public domain. We make this dedication for the benefit
  16. of the public at large and to the detriment of our heirs and
  17. successors. We intend this dedication to be an overt act of
  18. relinquishment in perpetuity of all present and future rights to this
  19. software under copyright law.
  20.  
  21. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  24. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  25. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  26. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  27. OTHER DEALINGS IN THE SOFTWARE.
  28.  
  29. For more information, please refer to <http://unlicense.org/>
  30.  
  31. ]]
  32. local function error(e) -- put this before everything because load order issues
  33.     term.setBackgroundColor(colors.blue) term.setTextColor(colors.white) term.setCursorPos(1,1) term.clear()
  34.     print("Windowscraft has crashed!")
  35.     print("")
  36.     print("Error Report:") term.setTextColor(colors.lime)
  37.     print(e)
  38.     print("")
  39.     sleep(1) setText("white")
  40.     print("Press any key to reboot...")
  41.     os.pullEvent("key")
  42.     os.reboot()
  43. end
  44.  
  45. -- ENGINE V1 *** MAIN ENGINE *** *** MAIN ENGINE *** *** MAIN ENGINE *** ENGINE V1 --
  46. -- FUNCTIONS --
  47. local w,h = term.getSize()
  48. function valid(value,expected) if type(value) == expected then return true else return false end end
  49. function verify(value, expected) return valid(value,expected) end
  50. function clr() term.clear() end
  51. function cp(x,y) if not valid(x,"number") then return error("Expected x to be a number, got "..type(x)) end if not valid(y,"number") then return error("Expected y to be a number, got "..type(y)) end term.setCursorPos(x,y) end
  52. function setText(col) if not valid(col,"string") then return error("Expected col to be a string, got "..type(x)) else return term.setTextColor(colors[col]) end end
  53. function setBack(col) if not valid(col,"string") then return error("Expected col to be a string, got "..type(x)) else return term.setBackgroundColor(colors[col]) end end
  54. function notnil(vari) if vari == nil then return false else return true end end
  55. function lt(tab) if not valid(tab,"table") then return error("Expected tab to be a table, got "..type(tab)) end local lt = 0 for i=1, #tab do if string.len(tab[i]) > lt then lt = string.len(tab[i]) end end return lt end
  56. function pullEvents(...) -- for pulling multiple events at a time, first event get priority
  57.     local args = {...}
  58.     --if #args == 0 then return os.pullEvent() end
  59.     while true do
  60.         sleep(.000000001)
  61.         event, arg1, arg2, arg3, arg4 = os.pullEvent()
  62.         for i=1, #args do
  63.             if args[i] == event then
  64.                 return event, arg1, arg2, arg3, arg4
  65.             end
  66.         end
  67.     end
  68. end
  69. function getTableSize(image) -- Returns width and length of a table
  70.     if not valid(image,"table") then return error("Expected table for image, got "..type(image)) end
  71.     local lng = 0
  72.     for i=1, #image do
  73.         if string.len(image[i]) > lng then
  74.             lng = string.len(image[i])
  75.         end
  76.     end
  77.     return lng, #image
  78. end
  79. -- FUNCTIONS --
  80. -- COLLISION --
  81. col = {}
  82. function createCol(name,x,y,x2,y2,status)
  83.     name = name or "name_"..#col table.insert(col,{}) status = status or true
  84.     col[#col].x, col[#col].y, col[#col].x2, col[#col].y2, col[#col].name, col[#col].status = x, y, x2, y2, name, status
  85. end
  86. function colExists(name)
  87.     for i=1, #col do
  88.         if col[i].name == name then return i end
  89.     end
  90.     return false
  91. end
  92. function removeCol(name)
  93.     if colExists(name) ~= false then table.remove(col, colExists(name)) end
  94. end
  95. function moveCol(name,newx,newy)
  96.     if not colExists(name) then return error("Collision does not exists") end
  97.     name = colExists(name)
  98.     col[name].x2,col[name].y2 = col[name].x2+newx-col[name].x, col[name].y2+newy-col[name].y
  99.     col[name].x,col[name].y = newx,newy
  100. end
  101. function drawCol(name, color)
  102.     if not notnil(color) then if col[name].status then color = colors.green else color = colors.red end end
  103.     for i=1, #col do
  104.         if name == col[i].name then
  105.             return paintutils.drawFilledBox(col[i].x,col[i].y,col[i].x2,col[i].y2,color)
  106.         end
  107.     end
  108.     return false
  109. end
  110. function checkCol(x,y)
  111.     for i=1, #col do
  112.         if x >= col[i].x and x <= col[i].x2 and y >= col[i].y and y <= col[i].y2 then
  113.             return col[i].name
  114.         end
  115.     end
  116.     return false
  117. end
  118. -- COLLISION --
  119. -- COLOR RELATED --
  120. function renderImage(image,x,y) -- used to display custom images fast and easy
  121.     if not valid(image,"table") then return error("Expected table for image, got "..type(image)) end
  122.     if not valid(x,"number") then return error("Expected number for x, got "..type(x)) end
  123.     if not valid(y,"number") then return error("Expected number for y, got "..type(y)) end
  124.    
  125.     cp(x,y)
  126.     for i=1, #image do
  127.         term.blit(string.rep(" ", #image[i]),string.rep("0", #image[i]),image[i])
  128.         cp(x,y+i)
  129.     end
  130. end
  131. function loadImage(image)
  132.     if not valid(image,"string") then return error("Expected string for image, got "..type(image)) end
  133.     if not fs.exists(image) then return false end
  134.     local img = {}
  135.     local file = fs.open(image, "r")
  136.     local tmp = ""
  137.     repeat
  138.         tmp = file.readLine()
  139.         if notnil(tmp) then
  140.             img[#img+1] = tmp
  141.         end
  142.     until not notnil(tmp)
  143.     file.close()
  144.     return img
  145. end
  146. -- COLOR RELATED --
  147. -- RCM --
  148. local rcm_data = {}
  149. rcm_data.menu = {"Test 1", "Test 2", "Test 3"}
  150. rcm_data.active = false
  151. rcm_data.x, rcm_data.y = 1,1
  152. function rcm_click(x,y)
  153.     if x >= rcm_data.x and x <= rcm_data.x+lt(rcm_data.menu) and y >= rcm_data.y and y <= rcm_data.y+#rcm_data.menu then
  154.         return true
  155.     end
  156.     return false
  157. end
  158. function setRcmMenu(tab)
  159.     if not valid(tab,"table") then return error("Expected tab to be a table, got "..type(tab)) end
  160.     rcm_data.menu = tab
  161. end
  162. function rcm(x,y,options,wid,hei)
  163.     if not valid(x,"number") then return error("Expected number for x, got "..type(x)) end
  164.     if not valid(y,"number") then return error("Expected number for y, got "..type(y)) end
  165.     if not valid(options,"table") then return error("Expected table for image, got "..type(options)) end
  166.     local lnt = lt(options) -- short for longest term (used to set width of menu)
  167.     local w,h if not notnil(wid) or not notnil(hei) then w,h = term.getSize() else w,h = wid,hei end
  168.     if x >= w-lnt then
  169.         if y > h-#options-1 then
  170.             paintutils.drawBox(x-1,y-1,x-lnt-1,y-#options-1,colors.lightGray)
  171.             paintutils.drawFilledBox(x,y,x-lnt,y-#options,colors.white)
  172.             --cp(x,y) write("1")
  173.             setText("black")
  174.             for i=1, #options do
  175.                 cp(x-lnt,y-#options+i-1) write(options[i])
  176.             end
  177.         else
  178.             paintutils.drawBox(x-lnt-1,y+1,x-1,y+#options+1,colors.lightGray)
  179.             paintutils.drawFilledBox(x-lnt,y,x,y+#options,colors.white)
  180.             --cp(x,y) write("2")
  181.             setText("black")
  182.             for i=1, #options do
  183.                 cp(x-lnt,y+i-1) write(options[i])
  184.             end
  185.         end
  186.     elseif x < w-lnt then
  187.         if y > h-#options-1 then
  188.             paintutils.drawBox(x+1,y-#options-1,x+lnt+1,y-1,colors.lightGray)
  189.             paintutils.drawFilledBox(x,y-#options,x+lnt,y,colors.white)
  190.             --cp(x,y) write("3")
  191.             setText("black")
  192.             for i=1, #options do
  193.                 cp(x,y-#options+i-1) write(options[i])
  194.             end
  195.         else
  196.             paintutils.drawBox(x+1,y+1,x+lnt+1,y+#options+1,colors.lightGray)
  197.             paintutils.drawFilledBox(x,y,x+lnt,y+#options,colors.white)
  198.             --cp(x,y) write("4")
  199.             setText("black")
  200.             for i=1, #options do
  201.                 cp(x,y+i-1) write(options[i])
  202.             end
  203.         end
  204.     end
  205.     sleep(.0001)
  206. end
  207. -- RCM --
  208. -- WINDOWS & SHORTCUTS --
  209. process = {}
  210. local processLimit = 5
  211. for i=1, processLimit do process[i] = 0 end
  212. function createWindow(name, fullscreen, program)
  213.     local selectedProcess = 0
  214.     for i=1, #process do
  215.         if process[#process-i+1] == 0 then selectedProcess = #process-i+1 end
  216.     end
  217.     if selectedProcess == 0 then return false end
  218.     if not valid(program,"function") then return false end
  219.     if fullscreen then
  220.         process[selectedProcess] = window.create(term.current(),1,1,w,h)
  221.     else
  222.         process[selectedProcess] = window.create(term.current(),1,2,w,h-1)
  223.     end
  224.     process[selectedProcess].name = name
  225.     process[selectedProcess].program = program
  226. end
  227. function deleteWindow(name)
  228.     if windowExists(name) then
  229.         if type(name) == "string" then name = getWindow(name) end
  230.         process[name] = 0
  231.     end
  232. end
  233. function createPopup(name, program)
  234.     --
  235. end
  236. function getWindow(id) -- returns info for window if exists.
  237.     if process[id] ~= 0 then return process[id].name, process[id].fullscreen else return false end
  238. end
  239. function windowExists(id) -- only returns true or false.
  240.     if type(id) == "number" then
  241.         if process[id] ~= 0 then return true else return false end
  242.     elseif type(id) == "string" then
  243.         for i=1, #process do
  244.             if process[i] ~= 0 then if process[i].name == id then return true end end
  245.         end
  246.         return false
  247.     end
  248. end
  249. function taskManager()
  250.     setBack("black") setText("white") cp(1,1) clr() print("test") sleep(5)
  251. end
  252. -- WINDOWS & SHORTCUTS --
  253. -- SHELL SECURITY --
  254. shell = {
  255.     run = function(program)
  256.         if not notnil(program) or not fs.exists(program) then return error("No such program.") end
  257.         local file = fs.open(program,"r")
  258.         local file_info = file.readAll() file.close()
  259.         local syntax = {"fs","error","shell.run","return"}
  260.         local trustLevel = {}
  261.         for i=1, #syntax do trustLevel[i] = false end
  262.         local run = true
  263.         for i=1, #syntax do
  264.             if notnil(string.find(file_info,syntax[i])) then trustLevel[i] = true run = false end
  265.         end
  266.         if run then
  267.             os.run({}, program)
  268.         else
  269.             sleep(.5)
  270.             for i=1, #syntax do
  271.                 print(syntax[i].." - "..tostring(trustLevel[i]))
  272.             end
  273.             print("Press 'enter' to run. otherwise continue...")
  274.             a,i = pullEvents("key") if i == keys.enter then os.run({}, program) end
  275.         end
  276.     end
  277. }
  278. -- SHELL SECURITY --
  279. -- ENGINE V1 *** MAIN ENGINE *** *** MAIN ENGINE *** *** MAIN ENGINE *** ENGINE V1 --
  280.  
  281. local dir = "Windowscraft"
  282. local images = {
  283.     {
  284.         filename = "boot.nfp",
  285.         "eeeeeddddd",
  286.         "eeeeeddddd",
  287.         "eeeeeddddd",
  288.         "eeeeeddddd",
  289.         "bbbbb11111",
  290.         "bbbbb11111",
  291.         "bbbbb11111",
  292.         "bbbbb11111",
  293.     },
  294. }
  295. local raw_images = {
  296.     desktop = {}
  297. }
  298. for i=1, h-1 do -- default blue
  299.     table.insert(raw_images["desktop"], string.rep("9",w))
  300. end
  301.  
  302. function updateBackgroundImage()
  303.     if fs.exists(dir.."/images/desktop.nfp") then
  304.         local img = loadImage(dir.."/images/desktop.nfp")
  305.         local iw,ih = getTableSize(img)
  306.         if iw == w and ih == h-1 then
  307.             raw_images["desktop"] = img
  308.         end
  309.     end
  310. end
  311. updateBackgroundImage()
  312.  
  313. function boot()
  314.     setText("white") setBack("black") clr()
  315.     for i=1, #images do
  316.         if not fs.exists(dir.."/images/"..images[i].filename) then
  317.             local file = fs.open(dir.."/images/"..images[i].filename, "w")
  318.             for ii=1, #images[i] do
  319.                 file.writeLine(images[i][ii])
  320.             end
  321.             file.close()
  322.         end
  323.     end
  324.     for i=1, #images do
  325.         images[string.gsub(images[i].filename,".nfp","")] = paintutils.loadImage(dir.."/images/"..images[i].filename)
  326.     end
  327.     paintutils.drawImage(images["boot"],w/2-(#images["boot"][1]/2),h/2-(#images["boot"]/2)) sleep(1.5)
  328.     cp(w/2-(#images["boot"][1]/2),h/2-(#images["boot"]/2)-2)
  329.     paintutils.drawLine(w/2-(#images["boot"][1]/2), h/2+(#images["boot"]/2)+2, w/2+(#images["boot"][1]/2)-1,h/2+(#images["boot"]/2)+2, colors.gray) sleep(.5)
  330.     for i=1, #images["boot"][1] do
  331.         paintutils.drawPixel(w/2-(#images["boot"][1]/2)+i-1, h/2+(#images["boot"]/2)+2,colors.white)
  332.         sleep(math.random(2,4)*.1)
  333.     end
  334.     setText("white") setBack("black") clr() sleep(1)
  335. end
  336.  
  337. local functions = {
  338.     debug = function() return error("Debug Crash") end,
  339.     exit = function() running = false end,
  340.     debugwindow = function() createWindow("Window Test",false,function() taskManager() end) end,
  341. }
  342.  
  343. function forceUpdate()
  344.     update = false
  345.     renderImage(raw_images["desktop"],1,1)
  346.     paintutils.drawLine(1,h,w,h,colors.white)
  347.     paintutils.drawPixel(1,h,colors.blue)
  348.     local current = term.current()
  349.     for i=1, #process do
  350.         if process[i] ~= 0 then
  351.             --updateWindow(i)
  352.         end
  353.     end
  354.     term.redirect(current)
  355.     if rcm_data.active == true then
  356.         rcm(rcm_data.x,rcm_data.y,rcm_data.menu,w,h-1)
  357.     end
  358. end
  359.  
  360. running = true
  361. local update = true
  362.  
  363. setBack("black") clr()
  364. function runtime()
  365.     setRcmMenu({"Debug", "Exit", "Debug Window"})
  366.     while running do
  367.         if update then
  368.             forceUpdate()
  369.         end
  370.         a,i,x,y = pullEvents("mouse_click", "mouse_drag")
  371.         if a == "mouse_click" then
  372.             if i == 1 then
  373.                 if rcm_data.active then
  374.                     if rcm_click(x,y) then
  375.                         rcm_data.active = false if notnil(functions[string.gsub(string.lower(rcm_data.menu[y-rcm_data.y+1] or "")," ","")]) then functions[string.gsub(string.lower(rcm_data.menu[y-rcm_data.y+1] or "")," ","")]() end
  376.                     end
  377.                     rcm_data.active = false
  378.                     update = true
  379.                 else
  380.                     if checkCol(x,y) ~= false then
  381.                         moveWindow(1,x,y)
  382.                     end
  383.                 end
  384.                 rcm_data.active = false
  385.             elseif i == 2 then
  386.                 rcm_data.active = true rcm_data.x,rcm_data.y = x,y update = true
  387.             end
  388.         elseif a == "mouse_drag" then
  389.            
  390.         end
  391.     end
  392. end
  393. function engine_1() -- this will run windows that run programs that keep runtime running. runtime controls actions to the window
  394.     en = 1
  395.     while running do
  396.         sleep(1)
  397.         if type(process[en]) == "table" then
  398.             term.redirect(process[en])
  399.             process[en].program()
  400.             term.redirect(term.native())
  401.             deleteWindow(process[en].name) forceUpdate()
  402.         end
  403.     end
  404. end
  405.  
  406. function engine_2() -- this will run windows that run programs that keep runtime running. runtime controls actions to the window
  407.     en = 2
  408.     while running do
  409.         sleep(1)
  410.         if type(process[en]) == "table" then
  411.             term.redirect(process[en])
  412.             process[en].program()
  413.             term.redirect(term.native())
  414.             deleteWindow(process[en].name) forceUpdate()
  415.         end
  416.     end
  417. end
  418.  
  419. function engine_3() -- this will run windows that run programs that keep runtime running. runtime controls actions to the window
  420.     en = 3
  421.     while running do
  422.         sleep(1)
  423.         if type(process[en]) == "table" then
  424.             term.redirect(process[en])
  425.             process[en].program()
  426.             term.redirect(term.native())
  427.             deleteWindow(process[en].name) forceUpdate()
  428.         end
  429.     end
  430. end
  431.  
  432. function engine_4() -- this will run windows that run programs that keep runtime running. runtime controls actions to the window
  433.     en = 4
  434.     while running do
  435.         sleep(1)
  436.         if type(process[en]) == "table" then
  437.             term.redirect(process[en])
  438.             process[en].program()
  439.             term.redirect(term.native())
  440.             deleteWindow(process[en].name) forceUpdate()
  441.         end
  442.     end
  443. end
  444.  
  445. function engine_5() -- this will run windows that run programs that keep runtime running. runtime controls actions to the window
  446.     en = 5
  447.     while running do
  448.         sleep(1)
  449.         if type(process[en]) == "table" then
  450.             term.redirect(process[en])
  451.             process[en].program()
  452.             term.redirect(term.native())
  453.             deleteWindow(process[en].name) forceUpdate()
  454.         end
  455.     end
  456. end
  457.  
  458.  
  459. boot() local r,e
  460.  
  461. parallel.waitForAny(function() r,e = pcall(runtime) end, engine_1, engine_2, engine_3, engine_4, engine_5)
  462. if not r then error(e) end
  463. setBack("black") setText("white") cp(1,1) clr() print("Windowscraft") sleep(.1)
Advertisement
Add Comment
Please, Sign In to add comment