Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Hideously Smashed Together by Compilr, a Hideous Smash-Stuff-Togetherer, (c) 2014 oeed --
- -- This file REALLLLLLLY isn't suitable to be used for anything other than being executed --
- -- To extract all the files, run: "<filename> --extract" in the Shell --
- local files = {
- system = {
- [ "dabos.log" ] = "[23.739] Initialized successfully.\
- [1.137] Initialized successfully.\
- [1.649] Initialized successfully.[6.737] Initialized successfully.\
- [8.026] crashed[8.147] Initialized successfully.\
- [9.647] crashed[14.171] Initialized successfully.\
- [9.768] Initialized successfully.\
- [10.609] Initialized successfully.\
- [20.357] Initialized successfully.\
- [8.17] Initialized successfully.\
- [15.228] Initialized successfully.\
- [17.085] Initialized successfully.\
- [10.397] Initialized successfully.\
- [14.995] Initialized successfully.\
- [14.469] Initialized successfully.\
- [18.926] Initialized successfully.\
- [23.621] crashed[22.173] Initialized successfully.\
- [23.235] Initialized successfully.\
- [22.385] Initialized successfully.\
- [13.365] Initialized successfully.\
- [1.529] Initialized successfully.\
- [7.99] Initialized successfully.\
- [1.921] Initialized successfully.\
- [23.742] Initialized successfully.\
- [0.149] Initialized successfully.\
- [0.493] Initialized successfully.\
- [8.292] Initialized successfully.\
- [7.784] Initialized successfully.\
- [8.696] Initialized successfully.\
- [10.295] Initialized successfully.\
- [10.472] Initialized successfully.\
- [12.638] Initialized successfully.\
- [14.827] Initialized successfully.\
- [15.059] Initialized successfully.\
- [16.802] Initialized successfully.\
- [17.664] Initialized successfully.\
- [17.728] Initialized successfully.\
- [20.601] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] crashed\
- [7.169] Initialized successfully.\
- [7.169] crashed\
- [7.169] Initialized successfully.\
- [7.169] crashed\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] crashed\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] crashed\
- [7.169] crashed\
- [7.169] crashed\
- [7.169] crashed\
- [7.169] crashed\
- [7.169] crashed\
- [7.169] crashed\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] crashed\
- [7.169] crashed\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] crashed\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] crashed\
- [7.169] crashed\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] Initialized successfully.\
- [7.169] crashed\
- [7.169] crashed\
- [7.169] crashed\
- [7.169] crashed\
- [7.169] crashed\
- [7.169] crashed\
- [7.169] crashed\
- [7.169] crashed\
- [7.169] crashed\
- [7.169] crashed\
- [7.169] crashed\
- [7.169] crashed\
- [7.169] crashed\
- [7.169] crashed\
- [7.169] crashed\
- [7.169] crashed\
- [7.169] crashed\
- [7.169] crashed\
- [7.169] Backing up...[7.169] crashed\
- [7.169] Backing up...[7.169] crashed\
- [7.169] Backing up...[7.169] crashed\
- [7.169] Backing up...[7.169] crashed\
- [7.169] Backing up...[7.169] crashed\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] crashed\
- [7.169] Backing up...\
- [7.169] crashed\
- [7.169] Backing up...\
- [7.169] crashed\
- [7.169] Backing up...\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] crashed\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] crashed\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] crashed\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] crashed\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] crashed\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] crashed\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] crashed\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] crashed\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] crashed\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] crashed\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] crashed\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] crashed\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] crashed\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] crashed\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] crashed\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] crashed or user terminated.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] crashed or user terminated.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] crashed or user terminated.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] crashed or user terminated.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] crashed or user terminated.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] crashed or user terminated.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] crashed or user terminated.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] crashed or user terminated.\
- [7.169] crashed or user terminated.\
- [7.169] crashed or user terminated.\
- [7.169] crashed or user terminated.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] crashed or user terminated.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] crashed or user terminated.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.\
- [7.169] Backing up...\
- [7.169] Backed up\
- [7.169] Initialized successfully.",
- main = "shell.run(\"system/init\")\
- ",
- init = "--Vars\
- print(\"Starting Up...\")\
- version = \"4.3\"\
- running = true\
- _ms = 0\
- _m2s = 0\
- prog_req = nil \
- program_current = nil\
- log = fs.open(\"/system/dabos.log\", \"a\")\
- bkp = fs.open(\"/user/programs/.settings/bkp\", \"r\")\
- bkp = bkp.readAll()\
- _rcl = 0\
- \
- --Backup\
- if bkp == \"yes\" then\
- log.write(\"[\"..os.time()..\"] Backing up...\\n\")\
- print(\"Backing up files...\")\
- if fs.exists(\"/bkp\") then\
- if fs.exists(\"bkp_old\") then fs.delete(\"bkp_old\") end\
- fs.copy(\"/bkp\", \"/bkp_old\")\
- fs.delete(\"/bkp\")\
- end\
- fs.copy(\"system\",\"bkp/system\")\
- fs.copy(\"startup\", \"bkp/startup\")\
- fs.copy(\"user\", \"bkp/user\")\
- print(\"Backed up.\")\
- log.write(\"[\"..os.time()..\"] Backed up\\n\")\
- end\
- --Functions\
- print(\"Setting up system functions...\")\
- clear = function()\
- term.setBackgroundColor(colors.black)\
- term.clear()\
- term.setCursorPos(1, 1)\
- end\
- \
- drawBarMenu1 = function()\
- term.setCursorPos(1, 2)\
- term.setBackgroundColor(colors.lightBlue)\
- term.setTextColor(colors.red)\
- print(\"Shutdown \")\
- print(\"Restart \")\
- term.setTextColor(colors.green)\
- print(\"Run... \")\
- term.setTextColor(colors.yellow)\
- print(\"CraftOS \")\
- term.setTextColor(colors.blue)\
- print(\"Files \")\
- term.setTextColor(colors.black)\
- print(\"LIDE \")\
- if disk.isPresent(\"left\") or disk.isPresent(\"right\") or disk.isPresent(\"back\") or disk.isPresent(\"top\") or disk.isPresent(\"bottom\") then\
- print(\"Eject \")\
- if disk.hasAudio(\"left\") or disk.hasAudio(\"right\") or disk.hasAudio(\"back\") or disk.hasAudio(\"top\") or disk.hasAudio(\"bottom\") then\
- print(\"Music \")\
- end\
- end\
- term.setTextColor(colors.gray)\
- print(\"DabOS \")\
- print(\"V. \"..version)\
- _ms = 1\
- end\
- \
- drawBarMenu2 = function()\
- y = 7\
- term.setCursorPos(10, y+0)\
- print(\"Desktop BG \")\
- if fs.exists(\"/bkp\") then\
- term.setCursorPos(10, y+1)\
- print(\"Restore Bkp\")\
- end\
- _m2s = 1\
- end\
- \
- drawBar = function()\
- term.setCursorPos(1, 1)\
- term.setBackgroundColor(colors.blue)\
- term.clearLine()\
- term.setCursorPos(1, 1)\
- term.setBackgroundColor(colors.white)\
- term.setTextColor(colors.lime)\
- term.write(\" Dab \")\
- end\
- \
- drawMenu = function()\
- _dt = paintutils.loadImage(\"/user/backgrounds/dt\")\
- term.setBackgroundColor(colors.orange)\
- clear()\
- term.setBackgroundColor(colors.orange)\
- paintutils.drawImage(_dt, 0, 1)\
- end\
- \
- program = function()\
- if prog_req == nil or prog_req == \"\" or prog_req == \"exit\" then return 0 end\
- term.setBackgroundColor(colors.black)\
- term.setCursorPos(1, 1)\
- term.clear()\
- shell.run(prog_req)\
- drawMenu()\
- drawBar()\
- drawBarMenu1()\
- end\
- \
- print(\"Building runtime...\")\
- \
- runTime = function()\
- while running do \
- event, button, x, y = os.pullEvent(\"mouse_click\")\
- if _ms == 0 and button == 1 and x < 6 and y == 1 then\
- drawBarMenu1()\
- elseif _ms == 1 and button == 1 and x < 9 and y == 2 then\
- term.setCursorPos(1,2)\
- term.write(\" \")\
- term.setCursorPos(1,2)\
- shell.run(\"shutdown\")\
- elseif _ms == 1 and button == 1 and x < 6 and y == 1 then\
- drawMenu()\
- drawBar()\
- _ms = 0\
- elseif _ms == 1 and button == 1 and x < 9 and y == 4 then\
- term.setCursorPos(1, 4)\
- term.write(\" \")\
- term.setCursorPos(1, 4)\
- term.setBackgroundColor(colors.lightBlue)\
- term.setTextColor(colors.green)\
- prog_req = read()\
- if fs.exists(prog_req) == false then\
- prog_req = \"/user/programs/\"..prog_req\
- end\
- program()\
- drawMenu()\
- drawBar()\
- drawBarMenu1()\
- elseif _ms == 1 and button == 1 and x < 9 and y == 3 then\
- term.setCursorPos(1, 3)\
- term.write(\" \")\
- term.setCursorPos(1, 3)\
- shell.run(\"reboot\")\
- elseif _ms == 1 and button == 1 and x < 9 and y == 7 then\
- prog_req = \"/user/programs/edit\"\
- program()\
- elseif _ms == 1 and button == 1 and x < 9 and y == 5 then\
- term.setBackgroundColor(colors.black)\
- term.clear()\
- term.setCursorPos(1,1)\
- term.setTextColor(colors.green)\
- shell.run(\"shell\")\
- elseif _ms == 1 and button == 1 and x < 9 and y == 6 then\
- prog_req = \"user/programs/fileman\"\
- program()\
- drawMenu()\
- drawBar()\
- drawBarMenu1()\
- elseif _ms == 1 and button == 1 and x < 9 and y == 8 then \
- disk.eject(\"left\")\
- disk.eject(\"right\")\
- disk.eject(\"front\")\
- disk.eject(\"back\")\
- disk.eject(\"top\")\
- disk.eject(\"bottom\")\
- elseif _ms == 1 and button == 1 and x < 9 and y == 9 then\
- disk.playAudio(\"left\")\
- disk.playAudio(\"right\")\
- disk.playAudio(\"top\")\
- disk.playAudio(\"bottom\")\
- disk.playAudio(\"back\")\
- end\
- end\
- end\
- \
- init = function()\
- log.write(\"[\"..os.time()..\"] Initialized successfully.\\n\")\
- drawMenu()\
- drawBar()\
- runTime()\
- log.close()\
- end\
- \
- --Main\
- print(\"Initializing...\")\
- clear()\
- x, y = term.getSize()\
- x = math.floor(x/2-4)\
- y = math.floor(y/2)\
- term.setCursorPos(x,y)\
- term.setBackgroundColor(colors.orange)\
- term.write(\"Welcome!\")\
- sleep(1)\
- run = pcall(init())",
- },
- user = {
- documents = {
- welcome = "Welcome!",
- },
- backgrounds = {
- dabos = "\
- \
- 5555555555 555555 555555 55555 5555\
- 5 55 5 5 5 5 5 5 5\
- 5 5 555555 5 5 5 5 5\
- 5 5 5 5 5 5 5 5 5\
- 5 5 5 5 5 5 5 5 5555\
- 5 5 5 5 555555 5 5 5\
- 5 5 5 5 5 5 5 5 5\
- 5 5 5 5 5 5 5 5 5\
- 5 5 5 5 5 5 5 5 5\
- 5 5 5 5 5 5 5 5 5\
- 5 5 5 5 5 5 5 5 5\
- 5 55 5 5 5 5 5 5 5\
- 5555555555 5 5 555555 55555 5555",
- smile = "\
- 00000000000000000000bbbbbbbbbbbbbbbb\
- 0000000f00000000000bbbbbbbbfbbbbbbbb\
- 0000000f0000000000bbbbbbbbbfbbbbbbbb\
- 0000000f000000000bbbbbbbbbbfbbbbbbbb\
- 0000000f00000000bbbbbbbbbbbfbbbbbbbb\
- 0000000f0000000bbbbbbbbbbbbfbbbbbbbb\
- 0000000f000000bbbbbbbbbbbbbfbbbbbbbb\
- 0000000f000000bbbbbbbbbbbbbfbbbbbbbb\
- 0000000f000000bbbbbbbbbbbbbfbbbbbbbb\
- 00000000000000bbbbbbbbbbbbbbbbbbbbbb\
- 00f0000000000000000bbbbbbbbbbbbbfbbb\
- 00f0000000000000000bbbbbbbbbbbbbfbbb\
- 000f000000000000000bbbbbbbbbbbbfbbbb\
- 000f000000000000000bbbbbbbbbbbbfbbbb\
- 0000fffffffffffffffffffffffffffbbbbb\
- 0000000000000000000bbbbbbbbbbbbbbbbb",
- dt = "\
- \
- 5555555555 555555 555555 55555 5555\
- 5 55 5 5 5 5 5 5 5\
- 5 5 555555 5 5 5 5 5\
- 5 5 5 5 5 5 5 5 5\
- 5 5 5 5 5 5 5 5 5555\
- 5 5 5 5 555555 5 5 5\
- 5 5 5 5 5 5 5 5 5\
- 5 5 5 5 5 5 5 5 5\
- 5 5 5 5 5 5 5 5 5\
- 5 5 5 5 5 5 5 5 5\
- 5 5 5 5 5 5 5 5 5\
- 5 55 5 5 5 5 5 5 5\
- 5555555555 5 5 555555 55555 5555",
- sunnyDay = " 444441333333333333333333333333333333000333333333333\
- 444413333333333333333333333333333300000003333333333\
- 444133333333300333300003333333333333333333333333333\
- 441333333300000033333333330333333333333333333333333\
- 113333333333333333333333330003333333333333333333333\
- 333333333330033333333333333330033333333330033333333\
- 333333333333003333333333333333333333333333333333333\
- 333333033333333333333333333333333333333333333333333\
- 333333003333333333333333333333333333333333333333333\
- 333333333333355555555555555555555533333333333333333\
- 33333333555555d555ddd5ddddd55555dd55555533333333333\
- 333335555ddddddd55555d5555555d55d5ddddd555553333333\
- 333555ddd5d5555ddd555d5dd555d55ddd5d5dddddd5555555d\
- 5555ddddddddddd55d555ddd5555555555555555ddddddddd55\
- dddddddd55dd555ddd5ddd55dd5555ddddd5dd5555dd5dddd5d\
- dddd555dd5555555ddd55dd55dddd55d5ddd55dd5d555ddddd5\
- dd555dddd5555555d5d5555d55ddd55555555555ddddd5ddd5d\
- dddddddddddddddddddddddd55ddddd555dddddddddddd555d5\
- ddddddddd5555555555dddddddddd5ddd5d55dd555555555555",
- dt1 = "\
- \
- 5555555555 555555 555555 55555 5555\
- 5 55 5 5 5 5 5 5 5\
- 5 5 555555 5 5 5 5 5\
- 5 5 5 5 5 5 5 5 5\
- 5 5 5 5 5 5 5 5 5555\
- 5 5 5 5 555555 5 5 5\
- 5 5 5 5 5 5 5 5 5\
- 5 5 5 5 5 5 5 5 5\
- 5 5 5 5 5 5 5 5 5\
- 5 5 5 5 5 5 5 5 5\
- 5 5 5 5 5 5 5 5 5\
- 5 55 5 5 5 5 5 5 5\
- 5555555555 5 5 555555 55555 5555",
- },
- programs = {
- lua = "\
- local tArgs = { ... }\
- if #tArgs > 0 then\
- \009print( \"This is an interactive Lua prompt.\" )\
- \009print( \"To run a lua program, just type its name.\" )\
- \009return\
- end\
- \
- local bRunning = true\
- local tCommandHistory = {}\
- local tEnv = {\
- \009[\"exit\"] = function()\
- \009\009bRunning = false\
- \009end,\
- \009[\"_echo\"] = function( ... )\
- \009 return ...\
- \009end,\
- }\
- setmetatable( tEnv, { __index = _ENV } )\
- \
- if term.isColour() then\
- \009term.setTextColour( colours.yellow )\
- end\
- print( \"Interactive Lua prompt.\" )\
- print( \"Call exit() to exit.\" )\
- term.setTextColour( colours.white )\
- \
- while bRunning do\
- \009--if term.isColour() then\
- \009--\009term.setTextColour( colours.yellow )\
- \009--end\
- \009write( \"lua> \" )\
- \009--term.setTextColour( colours.white )\
- \
- \009local s = read( nil, tCommandHistory, function( sLine )\
- \009\009if settings.get( \"lua.autocomplete\" ) then\
- \009\009 local nStartPos = string.find( sLine, \"[a-zA-Z0-9_%.]+$\" )\
- \009\009 if nStartPos then\
- \009\009 sLine = string.sub( sLine, nStartPos )\
- \009\009 end\
- \009\009 if #sLine > 0 then\
- \009 return textutils.complete( sLine, tEnv )\
- \009 end\
- \009\009end\
- return nil\
- \009end )\
- \009table.insert( tCommandHistory, s )\
- \009\
- \009local nForcePrint = 0\
- \009local func, e = load( s, \"lua\", \"t\", tEnv )\
- \009local func2, e2 = load( \"return _echo(\"..s..\");\", \"lua\", \"t\", tEnv )\
- \009if not func then\
- \009\009if func2 then\
- \009\009\009func = func2\
- \009\009\009e = nil\
- \009\009\009nForcePrint = 1\
- \009\009end\
- \009else\
- \009\009if func2 then\
- \009\009\009func = func2\
- \009\009end\
- \009end\
- \009\
- \009if func then\
- local tResults = { pcall( func ) }\
- if tResults[1] then\
- \009local n = 1\
- \009while (tResults[n + 1] ~= nil) or (n <= nForcePrint) do\
- \009 local value = tResults[ n + 1 ]\
- \009 if type( value ) == \"table\" then\
- \009 local metatable = getmetatable( value )\
- \009 if type(metatable) == \"table\" and type(metatable.__tostring) == \"function\" then\
- \009 print( tostring( value ) )\
- \009 else\
- local ok, serialised = pcall( textutils.serialise, value )\
- if ok then\
- print( serialised )\
- else\
- print( tostring( value ) )\
- end\
- \009 end\
- \009else\
- \009 print( tostring( value ) )\
- \009end\
- \009\009n = n + 1\
- \009end\
- else\
- \009printError( tResults[2] )\
- end\
- else\
- \009printError( e )\
- end\
- \
- end",
- fileman = "--[[\
- \009\009Mouse File Browser\
- \009\009by:\
- \009\009\009Stiepen irc(Kilobyte)\
- \009\009\009Cruor\
- \009\009\009BigSHinyToys\
- \009\009\
- \009\009note: send link to nightin9ale on CC forums\
- --]]\
- \
- local tArgs = {...}\
- local ver = \"1.4\"\
- local sTitle = \"File Browser\"\
- local bugTest, norun, dir, showAll\
- local _tArgs = {}\
- local config = \"mouse.cfg\"\
- \
- local temp\
- if shell and shell.getRunningProgram then\
- \009temp = shell.getRunningProgram()\
- end\
- \
- temp = temp or \"/bla\"\
- local localPath = string.sub(temp,1,#temp-string.len(fs.getName(temp)))\
- temp = nil -- just because not needed\
- \
- -- load config file\
- \
- local configSet = {}\
- local cnf = {}\
- \
- if fs.exists(localPath..\"/\"..config) then\
- \009local file = fs.open(localPath..\"/\"..config,\"r\")\
- \009if file then\
- \009\009local item = file.readLine()\
- \009\009while item do\
- \009\009\009table.insert(cnf,item)\
- \009\009\009item = file.readLine()\
- \009\009end\
- \009\009file.close()\
- \009end\009\
- end\
- \
- for i = 1,10 do\
- \009local test,data = pcall(textutils.unserialize,cnf[i])\
- \009if test then\
- \009\009configSet[i] = data\
- \009else\
- \009\009configSet[i] = nil\
- \009end\
- end\
- cnf = nil\
- \
- -- color configuration work in progress\
- local titleBar = configSet[1] or {txt = colors.black,back = colors.blue}\
- local addressBar = configSet[2] or {txt = colors.black,back = colors.lightGray}\
- local itemWindo = configSet[3] or {txt = colors.black,back = colors.cyan}\
- local rcmList = configSet[4] or {txt = colors.black,back = colors.lightGray} -- rcm = Right Click Menu List\
- local rcmTitle = configSet[5] or {txt = colors.black,back = colors.blue}\
- local dialogTitle = configSet[6] or {txt = colors.black,back = colors.blue}\
- local dialogWindo = configSet[7] or {txt = colors.black,back = colors.white}\
- local scrollCol = configSet[8] or {off = colors.gray, button = colors.gray,back = colors.lightGray}\
- \
- local tIcons = configSet[9] or {\
- \009back = {tCol = \"lightGray\",bCol = \"blue\",txt = \" < \"},\
- \009disk = {tCol = \"lime\",bCol = \"green\",txt = \"[*]\"},\
- \009audio = {tCol = \"yellow\",bCol = \"red\",txt = \"(o)\"},\
- \009folder = {tCol = \"lightGray\",bCol = \"blue\",txt = \"[=]\"},\
- \009file = {tCol = nil ,bCol = nil ,txt = nil}\
- }\
- \
- local customLaunch = configSet[10] or {\
- \009[\"Edit\"] = \"rom/programs/edit\",\
- \009[\"Paint\"] = \"rom/programs/color/paint\"\
- }\
- \
- local function saveCFG(overWrite)\
- \009if not fs.exists(localPath..\"/\"..config) or overWrite then\
- \009\009local cnf = {}\
- \009\009local file = fs.open(localPath..\"/\"..config,\"w\")\
- \009\009if file then\
- \009\009\009file.write(textutils.serialize(titleBar)..\"\\n\")\
- \009\009\009file.write(textutils.serialize(addressBar)..\"\\n\")\
- \009\009\009file.write(textutils.serialize(itemWindo)..\"\\n\")\
- \009\009\009file.write(textutils.serialize(rcmList)..\"\\n\")\
- \009\009\009file.write(textutils.serialize(rcmTitle)..\"\\n\")\
- \009\009\009file.write(textutils.serialize(dialogTitle)..\"\\n\")\
- \009\009\009file.write(textutils.serialize(dialogWindo)..\"\\n\")\
- \009\009\009file.write(textutils.serialize(scrollCol)..\"\\n\")\
- \009\009\009file.write(textutils.serialize(tIcons)..\"\\n\")\
- \009\009\009file.write(textutils.serialize(customLaunch)..\"\\n\")\
- \009\009\009file.close()\
- \009\009elseif overWrite then\
- \009\009end\
- \009end\
- end\
- \
- saveCFG()\
- \
- -- end configuration\
- \
- local function help()\
- \009print([[Usage: browser [-d] [-h] [-a] [-u] [--debug] [--help] [--dir <dir>] [--all] [--update]\
- --debug or -d: enable debug mode\
- --help or -h: display this screen\
- --dir: define initial directory\
- --all or -a: show hidden files\
- --update -u: update]])\
- end\
- \
- local function inBouwndry(clickX,clickY,boxX,boxY,width,hight)\
- \009return ( clickX >= boxX and clickX < boxX + width and clickY >= boxY and clickY < boxY + hight )\
- end\
- \
- local function update()\
- \009print(\"Checking for Updates\")\
- \009local isHTTP = false\
- \009local response\
- \009if http then\
- \009\009isHTTP = true\
- \009\009print(\"http on\")\
- \009\009response = http.get(\"http://pastebin.com/raw.php?i=rLbnyM1U\")\
- \009end\
- \009local flag = false\
- \009local isNewFlag = false\
- \009local newVerID\
- \009if response and isHTTP then\
- \009\009print(\"recived\")\
- \009\009local sInfo = response.readLine()\
- \009\009print(sInfo)\
- \009\009while sInfo do\
- \009\009\009print(sInfo)\
- \009\009\009if flag then\
- \009\009\009\009if sInfo == ver then\
- \009\009\009\009\009print(\"Mouse File Browser is up to date\")\
- \009\009\009\009\009break\
- \009\009\009\009else\
- \009\009\009\009\009newVerID = sInfo\
- \009\009\009\009\009flag = false\
- \009\009\009\009\009isNewFlag = true\
- \009\009\009\009end\
- \009\009\009elseif sInfo == sTitle then\
- \009\009\009\009flag = true\
- \009\009\009elseif isNewFlag then\
- \009\009\009\009isNewFlag = sInfo\
- \009\009\009\009response.close()\
- \009\009\009\009break\
- \009\009\009end\
- \009\009\009sInfo = response.readLine()\
- \009\009end\
- \009\009if isNewFlag then\
- \009\009\009print(\"New vershion avalible \"..newVerID)\
- \009\009\009print('downloading to \\Browser')\
- \009\009\009if fs.exists(\"Browser\") then\
- \009\009\009\009write(\"Browser exists OverWrite Browser Y/N : \")\
- \009\009\009\009local input = string.lower(read())\
- \009\009\009\009while input ~= \"y\" and input ~= \"n\" do\
- \009\009\009\009\009print(\"y or n required\")\
- \009\009\009\009\009input = string.lower(read())\
- \009\009\009\009end\
- \009\009\009\009if input == \"y\" then\
- \009\009\009\009\009print(\"Over Writeing Browser\")\
- \009\009\009\009\009print(\"Downloading new File\")\
- \009\009\009\009\009local response = http.get(\"http://pastebin.com/raw.php?i=\"..isNewFlag)\
- \009\009\009\009\009if response then\
- \009\009\009\009\009\009print(\"file downloaded\")\
- \009\009\009\009\009\009print(\"installing\")\
- \009\009\009\009\009\009fs.delete(\"Browser\")\
- \009\009\009\009\009\009local handel = fs.open(\"Browser\",\"w\")\
- \009\009\009\009\009\009if handel then\
- \009\009\009\009\009\009\009handel.write(response.readAll())\
- \009\009\009\009\009\009\009handel.close()\
- \009\009\009\009\009\009\009print(\"Update Complete\")\
- \009\009\009\009\009\009end\
- \009\009\009\009\009\009response.close()\
- \009\009\009\009\009end\
- \009\009\009\009else\
- \009\009\009\009\009print(\"Update aborted\")\
- \009\009\009\009end\
- \009\009\009else\
- \009\009\009\009print(\"Downloading new File\")\
- \009\009\009\009local response = http.get(\"http://pastebin.com/raw.php?i=\"..isNewFlag)\
- \009\009\009\009if response then\
- \009\009\009\009\009print(\"file downloaded\")\
- \009\009\009\009\009print(\"installing\")\
- \009\009\009\009\009local handel = fs.open(\"Browser\",\"w\")\
- \009\009\009\009\009if handel then\
- \009\009\009\009\009\009handel.write(response.readAll())\
- \009\009\009\009\009\009handel.close()\
- \009\009\009\009\009\009print(\"Update Complete\")\
- \009\009\009\009\009end\
- \009\009\009\009\009response.close()\
- \009\009\009\009end\
- \009\009\009end\
- \009\009end\
- \009elseif isHTTP then\
- \009\009print(\"Error downloading update file Please contact BigSHinyToys on the CC forums\")\
- \009\009print(\"http://www.computercraft.info/forums2/index.php?/topic/5509-advanced-computer-mouse-file-browser/\")\
- \009elseif not isHTTP then\
- \009\009print(\"HTTP API is turned off\")\
- \009\009print(\"Access Computer Craft Configer and change line\")\
- \009\009print([[enableapi_http {\
- # Enable the \"http\" API on Computers\
- general=false\
- }\
- TO THIS :\
- enableapi_http {\
- # Enable the \"http\" API on Computers\
- general=true\
- }]])\
- \009end\
- \009notCMD = false\
- \009norun = true\
- end\
- \
- for a = 1, #tArgs do\
- \009if tArgs[a]:sub(1,2) == \"--\" then\
- \009\009local cmd = tArgs[a]:sub(3):lower()\
- \009\009if cmd == \"debug\" then\
- \009\009\009bugTest = true\
- \009\009elseif cmd == \"help\" then\
- \009\009\009help()\
- \009\009\009norun = true\
- \009\009elseif cmd == \"dir\" then\
- \009\009\009dir = tArgs[a+1]\
- \009\009\009a = a + 1\
- \009\009elseif cmd == \"all\" then\
- \009\009\009showAll = true\
- \009\009elseif cmd == \"update\" then\
- \009\009\009update()\
- \009\009end\
- \009elseif tArgs[a]:sub(1,1) == \"-\" then\
- \009\009for b = 2, #tArgs[a] do\
- \009\009\009cmd = tArgs[a]:sub(b, b)\
- \009\009\009if cmd == \"d\" then\
- \009\009\009\009bugTest = true\
- \009\009\009elseif cmd == \"h\" then\
- \009\009\009\009help()\
- \009\009\009\009norun = true\
- \009\009\009elseif cmd == \"p\" then\
- \009\009\009\009dir = tArgs[a+1]\
- \009\009\009\009a = a + 1\
- \009\009\009elseif cmd == \"a\" then\
- \009\009\009\009showAll = true\
- \009\009\009elseif cmd == \"u\" then\
- \009\009\009\009update()\
- \009\009\009end\
- \009\009end\
- \009else\
- \009\009table.insert(_tArgs, tArgs[a])\
- \009end\
- end\
- \
- if (not dir) and shell and shell.dir then\
- \009dir = shell.dir()\
- end\
- \
- if dir and shell and shell.resolve then\
- \009dir = shell.resolve(dir)\
- end\
- \
- dir = dir or \"/\"\
- \
- if bugTest then -- this is that the var is for testing\
- \009print(\"Dir: \"..dir)\
- \009os.startTimer(4)\
- \009os.pullEvent()\
- end\
- \
- local function clear()\
- \009term.setBackgroundColor(colors.black)\
- \009term.setTextColor(colors.white)\
- \009term.clear()\
- \009term.setCursorBlink(false)\
- \009term.setCursorPos(1,1)\
- end\
- \
- --[[\
- \009\009Code thanks to Cruor \
- \009\009http://www.computercraft.info/forums2/index.php?/topic/5802-support-for-shell/\
- ]]--\
- \
- local function fixArgs(...)\
- \009local tReturn={}\
- \009local str=table.concat({...},\" \")\
- \009local sMatch\
- \009while str and #str>0 do\
- \009\009if string.sub(str,1,1)==\"\\\"\" then\
- \009\009\009sMatch, str=string.match(str, \"\\\"(.-)\\\"%s*(.*)\")\
- \009\009else\
- \009\009\009sMatch, str=string.match(str, \"(%S+)%s*(.*)\")\
- \009\009end\
- \009\009table.insert(tReturn,sMatch)\
- \009end\
- \009return tReturn\
- end\
- \
- --[[ end Cruor function ]]--\
- \
- \
- -- modified read made to play nice with coroutines\
- \
- local function readMOD( _sReplaceChar, _tHistory,_wdth)\
- \009local sLine = \"\"\
- \009term.setCursorBlink( true )\
- \
- \009local nHistoryPos = nil\
- \009local nPos = 0\
- if _sReplaceChar then\
- \009\009_sReplaceChar = string.sub( _sReplaceChar, 1, 1 )\
- \009end\
- \009\
- \009local sx, sy = term.getCursorPos()\009\
- \
- \009local w, h = term.getSize()\
- \009if _wdth and type(_wdth) == \"number\" then\
- \009\009w = sx + _wdth - 1\
- \009end\
- \009\
- \009local function redraw( _sCustomReplaceChar )\
- \009\009local nScroll = 0\
- \009\009if sx + nPos >= w then\
- \009\009\009nScroll = (sx + nPos) - w\
- \009\009end\
- \009\009\009\
- \009\009term.setCursorPos( sx + _wdth - 1, sy )\
- \009\009term.write(\" \")\
- \009\009term.setCursorPos( sx, sy )\
- \009\009local sReplace = _sCustomReplaceChar or _sReplaceChar\
- \009\009if sReplace then\
- \009\009\009term.write( string.rep(sReplace,_wdth) )\
- \009\009else\
- \009\009\009term.write( string.sub( sLine, nScroll + 1 ,nScroll + _wdth) )\
- \009\009end\
- \009\009term.setCursorPos( sx + nPos - nScroll, sy )\
- \009end\
- \009\
- \009while true do\
- \009\009local sEvent, param = os.pullEvent()\
- \009\009if sEvent == \"char\" then\
- \009\009\009sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )\
- \009\009\009nPos = nPos + 1\
- \009\009\009redraw()\
- \009\009\009\
- \009\009elseif sEvent == \"key\" then\
- \009\009\009\
- \009\009\009if param == keys.left then\
- \009\009\009\009-- Left\
- \009\009\009\009if nPos > 0 then\
- \009\009\009\009\009nPos = nPos - 1\
- \009\009\009\009\009redraw()\
- \009\009\009\009end\
- \009\009\009\009\
- \009\009\009elseif param == keys.right then\
- \009\009\009\009-- Right\009\009\009\009\
- \009\009\009\009if nPos < string.len(sLine) then\
- \009\009\009\009\009nPos = nPos + 1\
- \009\009\009\009\009redraw()\
- \009\009\009\009end\
- \009\009\009\
- \009\009\009elseif param == keys.up or param == keys.down then\
- -- Up or down\
- \009\009\009\009if _tHistory then\
- \009\009\009\009\009redraw(\" \");\
- \009\009\009\009\009if param == keys.up then\
- \009\009\009\009\009\009-- Up\
- \009\009\009\009\009\009if nHistoryPos == nil then\
- \009\009\009\009\009\009\009if #_tHistory > 0 then\
- \009\009\009\009\009\009\009\009nHistoryPos = #_tHistory\
- \009\009\009\009\009\009\009end\
- \009\009\009\009\009\009elseif nHistoryPos > 1 then\
- \009\009\009\009\009\009\009nHistoryPos = nHistoryPos - 1\
- \009\009\009\009\009\009end\
- \009\009\009\009\009else\
- \009\009\009\009\009\009-- Down\
- \009\009\009\009\009\009if nHistoryPos == #_tHistory then\
- \009\009\009\009\009\009\009nHistoryPos = nil\
- \009\009\009\009\009\009elseif nHistoryPos ~= nil then\
- \009\009\009\009\009\009\009nHistoryPos = nHistoryPos + 1\
- \009\009\009\009\009\009end\009\009\009\009\009\009\
- \009\009\009\009\009end\
- \009\009\009\009\009\
- \009\009\009\009\009if nHistoryPos then\
- \009sLine = _tHistory[nHistoryPos]\
- \009nPos = string.len( sLine ) \
- else\
- \009\009\009\009\009\009sLine = \"\"\
- \009\009\009\009\009\009nPos = 0\
- \009\009\009\009\009end\
- \009\009\009\009\009redraw()\
- end\
- \009\009\009elseif param == keys.backspace then\
- \009\009\009\009-- Backspace\
- \009\009\009\009if nPos > 0 then\
- \009\009\009\009\009redraw(\" \");\
- \009\009\009\009\009sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )\
- \009\009\009\009\009nPos = nPos - 1\009\009\009\009\009\
- \009\009\009\009\009redraw()\
- \009\009\009\009end\
- \009\009\009elseif param == keys.home then\
- \009\009\009\009-- Home\
- \009\009\009\009nPos = 0\
- \009\009\009\009redraw()\009\009\
- \009\009\009elseif param == keys.delete then\
- \009\009\009\009if nPos < string.len(sLine) then\
- \009\009\009\009\009redraw(\" \");\
- \009\009\009\009\009sLine = string.sub( sLine, 1, nPos ) .. string.sub( sLine, nPos + 2 )\009\009\009\009\
- \009\009\009\009\009redraw()\
- \009\009\009\009end\
- \009\009\009elseif param == keys[\"end\"] then\
- \009\009\009\009-- End\
- \009\009\009\009nPos = string.len(sLine)\
- \009\009\009\009redraw()\
- \009\009\009end\
- \009\009elseif sEvent == \"redraw\" then\
- \009\009\009redraw()\
- \009\009elseif sEvent == \"return\" then\
- \009\009\009term.setCursorBlink( false )\
- \009\009\009return sLine\
- \009\009end\
- \009end\
- \009\
- \009term.setCursorBlink( false )\
- \009\
- \009return sLine\
- end\
- \
- -- end modified read\
- \
- local function printC(posX,posY,textCol,backCol,text)\
- \009term.setCursorPos(posX,posY)\
- \009term.setTextColor(colors[textCol] or textCol)\
- \009term.setBackgroundColor(colors[backCol] or backCol)\
- \009term.write(text)\
- end\
- \
- local function InputBox(title)\
- \009local boxW,boxH = 26,3\
- \009local termX,termY = term.getSize()\
- \009local ofsX,ofsY = math.ceil((termX/2) - (boxW/2)) , math.ceil((termY/2) - (boxH/2)) -- offset from top left\
- \009local options = {\"ok\",\"cancel\"}\
- \009\
- \009local selected = 1\
- \009local space = 0\
- \009local range = {}\
- \009for i = 1,#options do\
- \009\009range[i] = {s = space,f = space + string.len(options[i])}\
- \009\009space = space + string.len(options[i])+3\
- \009end\
- \009local ofC = (boxW/2) - (space/2)\
- \009\
- \009local function drawBox()\
- \009\009printC(ofsX,ofsY,colors.black,colors.blue,string.rep(\" \",boxW))\
- \009\009printC(ofsX+1,ofsY,colors.black,colors.blue,(title or \"User Input\"))\
- \009\009printC(ofsX,ofsY+1,colors.black,colors.white,string.rep(\" \",boxW))\
- \009\009printC(ofsX,ofsY+2,colors.black,colors.white,string.rep(\" \",boxW))\
- \009\009printC(ofsX,ofsY+3,colors.black,colors.white,string.rep(\" \",boxW))\
- \009\009\
- \009\009for i = 1,#options do\
- \009\009\009if i == selected then\
- \009\009\009\009term.setBackgroundColor(colors.lightGray)\
- \009\009\009\009term.setCursorPos(range[i].s + ofC + ofsX - 1,ofsY + 3)\
- \009\009\009\009term.write(\"[\"..options[i]..\"]\")\
- \009\009\009\009term.setBackgroundColor(colors.white)\
- \009\009\009\009term.write(\" \")\
- \009\009\009else\
- \009\009\009\009term.setCursorPos(range[i].s + ofC + ofsX - 1,ofsY + 3)\
- \009\009\009\009term.write(\" \"..options[i]..\" \")\
- \009\009\009end\
- \009\009end\
- \009\009\
- \009\009printC(ofsX+2,ofsY+2,colors.black,colors.lightGray,string.rep(\" \",boxW-4))\
- \009end\
- \009drawBox()\
- \009term.setCursorPos(ofsX+2,ofsY+2)\
- \009local co = coroutine.create(function() return readMOD(nil,nil,boxW - 4) end)\
- \009while true do\
- \009\009local event = {os.pullEvent()}\
- \009\009if event[1] == \"key\" or event[1] == \"char\" then\
- \009\009\009if event[2] == 28 then\
- \009\009\009\009local test,data = coroutine.resume(co,\"return\")\
- \009\009\009\009return data\
- \009\009\009else\
- \009\009\009\009coroutine.resume(co,unpack(event))\
- \009\009\009end\
- \009\009elseif event[1] == \"mouse_click\" then\
- \009\009\009if event[4] == ofsY + 3 then\
- \009\009\009\009for i = 1,#options do\
- \009\009\009\009\009if event[3] >= range[i].s + ofC + ofsX - 1 and event[3] <= range[i].f + ofC + ofsX then\
- \009\009\009\009\009\009if options[i] == \"ok\" then\
- \009\009\009\009\009\009\009local test,data = coroutine.resume(co,\"return\")\
- \009\009\009\009\009\009\009return data\
- \009\009\009\009\009\009elseif options[i] == \"cancel\" then\
- \009\009\009\009\009\009\009return false\
- \009\009\009\009\009\009end\
- \009\009\009\009\009end\
- \009\009\009\009end\
- \009\009\009end\
- \009\009end\
- \009end\
- end\
- \
- local function dialogBox(title,message,options, h, w)\
- \009term.setCursorBlink(false)\
- \009local selected = 1\
- \009title = title or \"\"\
- \009message = message or \"\"\
- \009options = options or {}\
- \009local boxW,boxH = (w or 26), (h or 3)\
- \009local termX,termY = term.getSize()\
- \009local ofsX,ofsY = math.ceil((termX/2) - (boxW/2)) , math.ceil((termY/2) - (boxH/2)) -- offset from top left\
- \009\
- \009local space = 0\
- \009local range = {}\
- \009for i = 1,#options do\
- \009\009range[i] = {s = space,f = space + string.len(options[i])}\
- \009\009space = space + string.len(options[i])+3\
- \009end\
- \009local ofC = math.ceil((boxW/2)) - math.ceil((space/2))\
- \009\
- \009local function drawBox()\
- \009\009printC(ofsX,ofsY,dialogTitle.txt,dialogTitle.back,\" \"..title..string.rep(\" \",boxW-#title-5)..\"_[]\")\
- \009\009term.setBackgroundColor(colors.red)\
- \009\009term.setTextColor(colors.white)\
- \009\009term.write(\"X\")\
- \009\009printC(ofsX,ofsY+1,dialogWindo.txt,dialogWindo.back,string.sub(\" \"..message..string.rep(\" \",boxW),1,boxW))\
- \009\009term.setCursorPos(ofsX,ofsY+2)\
- \009\009term.write(string.rep(\" \",boxW))\
- \009\009term.setCursorPos(ofsX,ofsY+3)\
- \009\009term.write(string.rep(\" \",boxW))\
- \009\009for i = 1,#options do\
- \009\009\009if i == selected then\
- \009\009\009\009printC(range[i].s + ofC + ofsX - 1,ofsY + 3,\"black\",\"lightGray\",\"[\"..options[i]..\"]\")\
- \009\009\009\009term.setBackgroundColor(dialogWindo.back)\
- \009\009\009\009term.setTextColor(dialogWindo.txt)\
- \009\009\009\009term.write(\" \")\
- \009\009\009else\
- \009\009\009\009term.setCursorPos(range[i].s + ofC + ofsX - 1,ofsY + 3)\
- \009\009\009\009term.write(\" \"..options[i]..\" \")\
- \009\009\009end\
- \009\009end\
- \009\009term.setCursorPos(ofsX + ofC + space,ofsY + 3)\
- \009\009term.write(string.rep(\" \",boxW - (ofC + space)))\
- \009\009term.setBackgroundColor(colors.black)\
- \009\009term.setTextColor(colors.white)\009\009\009\
- \009end\
- \009while true do\
- \009\009drawBox()\
- \009\009event = {os.pullEvent()}\
- \009\009if event[1] == \"key\" then\
- \009\009\009if event[2] == 203 then -- left\
- \009\009\009\009selected = selected - 1\
- \009\009\009\009if selected < 1 then\
- \009\009\009\009\009selected = #options\
- \009\009\009\009end\
- \009\009\009elseif event[2] == 205 then -- right\
- \009\009\009\009selected = selected + 1\
- \009\009\009\009if selected > #options then\
- \009\009\009\009\009selected = 1\
- \009\009\009\009end\
- \009\009\009elseif event[2] == 28 then -- enter\
- \009\009\009\009return selected , options[selected]\
- \009\009\009end\
- \009\009elseif event[1] == \"mouse_click\" then\
- \009\009\009\
- \009\009\009if bugTest then term.write(\"M \"..event[2]..\" X \"..event[3]..\" Y \"..event[4]..\" \") end\
- \009\009\009\
- \009\009\009if event[2] == 1 then\
- \009\009\009\009if event[4] == ofsY + 3 then\
- \009\009\009\009\009for i = 1,#options do\
- \009\009\009\009\009\009if event[3] >= range[i].s + ofC + ofsX - 1 and event[3] <= range[i].f + ofC + ofsX then\
- \009\009\009\009\009\009\009return i , options[i]\
- \009\009\009\009\009\009end\
- \009\009\009\009\009end\
- \009\009\009\009end\
- \009\009\009end\
- \009\009end\
- \009end\
- end\
- \
- local flag = true\
- local fSlash = \"/\"\
- local path = {dir:match(\"[^/]+\")}\
- local function stringPath() -- compacted this a lot\
- \009return fSlash..table.concat(path,fSlash)\
- end\
- \
- local function osRunSpaces(sFileLocation,...) -- getRunningProgram() [\"shell\"] = shell\
- \009clear()\
- \009if os.newThread then\
- \009\009os.newThread(false,...)\
- \009else\
- \009local fProg,probblem = loadfile(sFileLocation)\
- \009\009if fProg then\
- \009\009\009local tEnv = {[\"shell\"] = {}}\
- \009\009\009setmetatable(tEnv.shell,{ __index = shell})\
- \009\009\009tEnv.shell.getRunningProgram = function()\
- \009\009\009\009return sFileLocation\
- \009\009\009end\
- \009\009\009setmetatable(tEnv,{ __index = _G})\
- \009\009\009setfenv(fProg,tEnv)\
- \009\009\009local test,probblem = pcall(fProg,...)\
- \009\009\009if not test then\
- \009\009\009\009print(probblem)\
- \009\009\009\009dialogBox(\"ERROR\",tostring(probblem),{\"ok\"},3,30)\
- \009\009\009else\
- \009\009\009\009return true\
- \009\009\009end\
- \009\009else\
- \009\009\009print(probblem)\
- \009\009\009dialogBox(\"ERROR\",tostring(probblem),{\"ok\"},3,30)\
- \009\009end\
- \009end\
- end\
- \
- local function rClickMenu(title,tList,tItem,posX,posY)\
- \
- \009term.setCursorBlink(false)\
- \009local BoxTitle = title\
- \009local choices = {}\
- \009local termX,termY = term.getSize()\
- \009local offX,offY\
- \009\
- \009local width = #BoxTitle + 2\
- \009local hight\
- \009\
- \009for k,v in pairs(tList) do\
- \009\009if v ~= nil then\
- \009\009\009table.insert(choices,k)\
- \009\009end\
- \009\009if width < #k + 2 then\
- \009\009\009width = #k + 2\
- \009\009end\
- \009end\
- \009\
- \009if #choices == 0 then\
- \009\009return\
- \009end\
- \009\
- \009hight = #choices + 1\
- \009table.sort(choices)\
- \009\
- \009offX,offY = math.ceil((termX/2) - (width/2)),math.ceil((termY/2) - (hight/2))\
- \009\
- \009if posX and posY then -- offX,offY = posX,posY\
- \009\009if posX >= termX - width - 1 then\
- \009\009\009offX = termX - width - 1\
- \009\009else\
- \009\009\009offX = posX\
- \009\009end\
- \009\009if posY >= termY - hight then\
- \009\009\009offY = termY - hight\
- \009\009else\
- \009\009\009offY = posY\
- \009\009end\
- \009end\
- \009\
- \009local function reDrawer()\
- \009\009printC(offX,offY,rcmTitle.txt,rcmTitle.back,\" \"..BoxTitle..string.rep(\" \",width - #BoxTitle - 1))\
- \009\009for i = 1,#choices do\
- \009\009\009printC(offX,offY + i,rcmList.txt,rcmList.back,\" \"..choices[i]..string.rep(\" \",width - #choices[i] - 1))\
- \009\009end\
- \009end\
- \009\
- \009while true do\
- \009\009reDrawer()\
- \009\009local event = {os.pullEvent()}\
- \009\009if event[1] == \"mouse_click\" then\
- \009\009\009if event[2] == 1 then -- event[3] = x event[4] = y\
- \009\009\009\009if event[4] > offY and event[4] < hight + offY and event[3] >= offX and event[3] < width + offX then\
- \009\009\009\009\009--dialogBox(\"ERROR:\",type(tList[choices[event[4] - offY]]),{\"ok\"})\
- \009\009\009\009\009if type(tList[choices[event[4] - offY]]) == \"function\" then\
- \009\009\009\009\009\009return tList[choices[event[4] - offY]](tItem)\
- \009\009\009\009\009elseif type(tList[choices[event[4] - offY]]) == \"table\" then\
- \009\009\009\009\009\009return rClickMenu(\"Options\",tList[choices[event[4] - offY]],tItem,event[3],event[4])\
- \009\009\009\009\009elseif type(tList[choices[event[4] - offY]]) == \"string\" then\
- \009\009\009\009\009\009return osRunSpaces(\
- \009\009\009\009\009\009\009\009unpack(\
- \009\009\009\009\009\009\009\009\009fixArgs(\
- \009\009\009\009\009\009\009\009\009\009tList[choices[event[4] - offY]]..\" \\\"\"..stringPath()..fSlash..tItem.n..\"\\\"\"\
- \009\009\009\009\009\009\009\009\009)\
- \009\009\009\009\009\009\009\009)\
- \009\009\009\009\009\009\009)\
- \009\009\009\009\009else\
- \009\009\009\009\009\009dialogBox(\"ERROR:\",\"somthing up with new rMenu\",{\"ok\"})\
- \009\009\009\009\009end\
- \009\009\009\009else\
- \009\009\009\009\009return\
- \009\009\009\009end\
- \009\009\009elseif event[2] == 2 then\
- \009\009\009\009return\
- \009\009\009end\
- \009\009end\
- \009end\
- \009\
- end\
- \
- local function preferences()\
- \009local tItem = {\
- \009\009{txt = \"Title Bar\",it = titleBar},\
- \009\009{txt = \"Address Bar\",it = addressBar},\
- \009\009{txt = \"Item Windo\", it = itemWindo},\
- \009\009{txt = \"Title Right Click Title\",it = rcmTitle},\
- \009\009{txt = \"Right Click Menu\",it = rcmList},\
- \009\009{txt = \"Title Dialog Box\",it = dialogTitle},\
- \009\009{txt = \"Dialog Box\",it = dialogWindo},\
- \009\009{txt = \"Scroll Bar\",it = scrollCol}\
- \009}\
- \009local topL,topR = 13,5\
- \009local width,hight = 23,6\
- \009local bottomL,bottomR = topL + width,topR + hight\
- \009\
- \009local listOffset = 0\
- \009local sel = 1\
- \009local otherSel = 1\
- \009local otherItems = {}\
- \009\
- \009if tItem[sel] then\
- \009\009for k,v in pairs(tItem[sel].it) do\
- \009\009\009table.insert(otherItems,{txt = k,it = v})\
- \009\009end\
- \009end\
- \009\
- \009local function draw()\
- \009\009printC(topL,topR,titleBar.txt,titleBar.back,string.sub(\" Preferences \"..string.rep(\" \",width),1,width))\
- \009\009for i = 0,12,4 do\
- \009\009\009for a = 1,4 do\
- \009\009\009\009--printC(topL + (a*12)-12 ,topR + ((i+4)/4),4,2^(a+i-1),\" \"..tostring(2^(a+i-1)))\
- \009\009\009\009printC(topL + a-1 ,topR + ((i+4)/4),4,2^(a+i-1),\" \")\
- \009\009\009end\
- \009\009end\
- \009\009local sSel = \" \"\
- \009\009for i = 1,hight - 2 do\
- \009\009\009if i == sel - listOffset then\
- \009\009\009\009sSel = \">\"\
- \009\009\009end\
- \009\009\009if tItem[i+listOffset] then\
- \009\009\009\009printC(topL + 4 ,topR + i,colors.black,colors.white,string.sub(sSel..tItem[i+listOffset].txt..string.rep(\" \",width),1,width - 4))\
- \009\009\009else\
- \009\009\009\009printC(topL + 4 ,topR + i,colors.black,colors.white,sSel..string.rep(\" \",width-5))\
- \009\009\009end\
- \009\009\009if i == sel - listOffset then\
- \009\009\009\009sSel = \" \"\
- \009\009\009end\
- \009\009end\
- \009\009term.setCursorPos(topL,topR + hight - 1)\
- \009\009local loop = 1\
- \009\009local length = 0\
- \009\009\009for i = 1,#otherItems do\
- \009\009\009\009if otherSel == i then\
- \009\009\009\009\009sSel = \">\"\
- \009\009\009\009end\
- \009\009\009\009if colors.black == otherItems[i].it or colors.gray == otherItems[i].it then\
- \009\009\009\009\009term.setTextColor(colors.white)\
- \009\009\009\009else\
- \009\009\009\009\009term.setTextColor(colors.black)\
- \009\009\009\009end\
- \009\009\009\009term.setBackgroundColor(otherItems[i].it)\
- \009\009\009\009term.write(sSel..tostring(otherItems[i].txt)..\" \")\
- \009\009\009\009length = length + #otherItems[i].txt + 2\
- \009\009\009\009if otherSel == i then\
- \009\009\009\009\009sSel = \" \"\
- \009\009\009\009end\
- \009\009\009\009loop = loop+1\
- \009\009\009end\
- \009\009term.setBackgroundColor(colors.white)\
- \009\009term.write(string.rep(\" \",width - length))\
- \009end\
- \009while true do\
- \009\009draw()\
- \009\009local event = {os.pullEvent()}\
- \009\009if event[1] == \"mouse_click\" and event[2] == 1 then\
- \009\009\009if inBouwndry(event[3],event[4],topL,topR,width,hight) then\
- \009\009\009\009local inSideX,inSideY = event[3] - topL,event[4] - topR\
- \009\009\009\009if inBouwndry(inSideX+1,inSideY,1,1,4,4) and tItem[sel] then\
- \009\009\009\009\009--[[\
- \009\009\009\009\009term.setCursorPos(1,1)\
- \009\009\009\009\009term.setBackgroundColor(2^(inSideX + ((inSideY*4)-4)))\
- \009\009\009\009\009print(2^(inSideX + ((inSideY*4)-4)),\" \",inSideX + ((inSideY*4)-4),\" \")\
- \009\009\009\009\009]]--\
- \009\009\009\009\009tItem[sel][\"it\"][otherItems[otherSel].txt] = (2^(inSideX + ((inSideY*4)-4)))\
- \009\009\009\009end\
- \009\009\009end\
- \009\009elseif event[1] == \"key\" then\
- \009\009\009if event[2] == 200 then\
- \009\009\009\009sel = sel - 1\
- \009\009\009elseif event[2] == 208 then\
- \009\009\009\009sel = sel + 1\
- \009\009\009elseif event[2] == 203 then\
- \009\009\009\009otherSel = otherSel - 1\
- \009\009\009elseif event[2] == 205 then\
- \009\009\009\009otherSel = otherSel + 1\
- \009\009\009elseif event[2] == 28 then\
- \009\009\009\009if dialogBox(\"Confirm\",\"Save prefrences?\",{\"Yes\",\"No\"}) == 1 then\
- \009\009\009\009\009saveCFG(true)\
- \009\009\009\009end\
- \009\009\009\009return\
- \009\009\009end\
- \009\009end\
- \009\009if sel < 1 then\
- \009\009\009sel = 1\
- \009\009elseif sel > #tItem then\
- \009\009\009sel = #tItem\
- \009\009end\
- \009\009if sel > listOffset + hight - 2 then\
- \009\009\009listOffset = listOffset + 1\
- \009\009elseif sel - listOffset < 1 then\
- \009\009\009listOffset = listOffset - 1\
- \009\009end\
- \009\009\
- \009\009otherItems = {}\
- \009\009if tItem[sel] then\
- \009\009\009for k,v in pairs(tItem[sel].it) do\
- \009\009\009\009table.insert(otherItems,{txt = k,it = v})\
- \009\009\009end\
- \009\009end\
- \009\009\
- \009\009if otherSel < 1 then\
- \009\009\009otherSel = 1\
- \009\009elseif otherSel > #otherItems then\
- \009\009\009otherSel = #otherItems\
- \009\009end\
- \009\009\
- \009\009if bugTest then\
- \009\009\009term.setBackgroundColor(colors.black)\
- \009\009\009term.setTextColor(colors.white)\
- \009\009\009term.setCursorPos(1,1)\
- \009\009\009term.clearLine()\
- \009\009\009term.write(\"sel \"..sel..\" offset \"..listOffset)\
- \009\009end\
- \009end\
- end\
- \
- local function fileSelect(mode) -- save_file open_file browse < not yet implemented\
- \009\
- \009local title = sTitle..\" \"..ver\
- \009local bRun = true\
- \009local clipboard = nil\
- \009local cut = false\
- \009\
- \009local termX,termY = term.getSize()\
- \009local offsetX,offsetY = 1,1\
- \009local hight,width = math.ceil(termY-2),math.ceil(termX-2)\
- \009local oldHight,oldWidth\
- \009\
- \009-- offsets\
- \009local boxOffX,boxOffY = offsetX,offsetY + 2\
- \009local boxH,boxW = hight - 2 ,width - 2\
- \009\
- \009local barX,barY = offsetX + 1,offsetY + 2\
- \009local barH,barW = 1,width - 1\
- \009\
- \009local tbarX,tbarY = offsetX + 1,offsetY + 1\
- \009local tbarH,tbarW = 1,width - 1\
- \009\
- \009local exitX,exitY = offsetX + width - 1 ,offsetY + 1\
- \009\
- \009local pading = string.rep(\" \",boxW)\
- \009local list\
- \009\
- \009local listOff = 0\
- \009\
- \009local sPath\
- \009local tItemList = {}\
- \009\
- \009local function newList()\
- \009\009listOff = 0\
- \009\009flag = true\
- \009\009tItemList = {{n = \"..\", id = \"back\"}} -- adds a back item at top of list\
- \009\009sPath = stringPath()\
- \009\009local folders = {}\
- \009\009local files = {}\
- \009\009local disks = {}\
- \009\009if not fs.exists(sPath) then\
- \009\009\009path = {}\
- \009\009\009sPath = stringPath()\
- \009\009\009dialogBox(\"ERROR:\",\"Path no longer exists\",{\"ok\"})\
- \009\009end\
- \009\009local test,list = pcall(fs.list,sPath) -- stopes fs.list crash\
- \009\009if list == nil then\
- \009\009\009list = {}\
- \009\009\009dialogBox(\"ERROR : \",\"fs.list crashed\",{\"ok\"})\
- \009\009end\
- \009\009if #path == 0 then\
- \009\009\009for i,v in pairs(rs.getSides()) do\
- \009\009\009\009if disk.isPresent(v) then\
- \009\009\009\009\009if disk.hasData(v) then\
- \009\009\009\009\009\009table.insert(tItemList,{n = disk.getMountPath(v), id = \"disk\",s = v})\
- \009\009\009\009\009\009disks[disk.getMountPath(v)] = true\
- \009\009\009\009\009elseif disk.hasAudio(v) then\
- \009\009\009\009\009\009table.insert(tItemList,{n = disk.getAudioTitle(v), id = \"audio\",s = v})\
- \009\009\009\009\009end\
- \009\009\009\009end\
- \009\009\009end\
- \009\009end\
- \009\009for i,v in pairs(list) do\
- \009\009\009if fs.isDir(sPath..fSlash..v) then\
- \009\009\009\009table.insert(folders,v)\
- \009\009\009else\
- \009\009\009\009table.insert(files,v)\
- \009\009\009end\
- \009\009end\
- \009\009table.sort(folders)\
- \009\009table.sort(files)\
- \009\009for i,v in pairs(folders) do\
- \009\009\009if disks[v] == nil then\
- \009\009\009\009table.insert(tItemList,{n = v, id = \"folder\"})\
- \009\009\009end\
- \009\009end\
- \009\009for i,v in pairs(files) do\
- \009\009\009table.insert(tItemList,{n = v, id = \"file\"})\
- \009\009end\
- \009end\
- \009\
- \009local function paste()\
- \009\009if cut then\
- \009\009\009local s, m = pcall(\
- \009\009\009\009function()\
- \009\009\009\009\009fs.move(clipboard[1]..fSlash..clipboard[2], stringPath()..fSlash..clipboard[2])\
- \009\009\009\009\009cut = false\
- \009\009\009\009\009clipboard = nil\
- \009\009\009\009end)\
- \009\009\009if not s then\
- \009\009\009\009dialogBox(\"Error\", (m or \"Couldn't move\"), {\"ok\"}, 4, 30)\
- \009\009\009end\
- \009\009\009if bugTest then\
- \009\009\009\009local x, y = term.getCursorPos()\
- \009\009\009\009term.setCursorPos(1, ({term.getSize()})[2])\
- \009\009\009\009write(\"from \"..clipboard[1]..fSlash..clipboard[2]..\" to \"..stringPath()..fSlash..clipboard[2])\
- \009\009\009end\
- \009\009else\
- \009\009\009local s, m = pcall(function()\
- \009\009\009\009if fs.exists(stringPath()..fSlash..clipboard[2]) then\
- \009\009\009\009\009fs.copy(clipboard[1]..fSlash..clipboard[2], stringPath()..fSlash..\"copy-\"..clipboard[2])\
- \009\009\009\009else\
- \009\009\009\009\009fs.copy(clipboard[1]..fSlash..clipboard[2], stringPath()..fSlash..clipboard[2])\
- \009\009\009\009end\
- \009\009\009end)\
- \009\009\009if not s then\
- \009\009\009\009dialogBox(\"Error\", (m or \"Couldn't copy\"), {\"ok\"}, 4, 30)\
- \009\009\009end\
- \009\009\009if bugTest then\
- \009\009\009\009local x, y = term.getCursorPos()\
- \009\009\009\009term.setCursorPos(1, ({term.getSize()})[2])\
- \009\009\009\009write(\"from \"..clipboard[1]..fSlash..clipboard[2]..\" to \"..stringPath()..fSlash..clipboard[2])\
- \009\009\009end\
- \009\009end\
- \009\009newList()\
- \009end\
- \009\
- \009-- this section bellow handles the right click menu\
- \009\
- \009local tmenu = {\
- \009\009disk = {\
- \009\009\009[\"Open\"] = function(tItem)\
- \009\009\009\009table.insert(path,tItem.n)\
- \009\009\009\009newList()\
- \009\009\009end,\
- \009\009\009[\"Copy\"] = function(tItem)\
- \009\009\009\009clipboard = {stringPath(), tItem.n}\
- \009\009\009\009cut = false\
- \009\009\009end,\
- \009\009\009[\"Eject\"] = function(tItem)\
- \009\009\009\009if dialogBox(\"Confirm\",\"Eject \"..fSlash..tItem.n..\" \"..tItem.s,{\"yes\",\"no\"}) == 1 then\
- \009\009\009\009\009disk.eject(tItem.s)\
- \009\009\009\009\009newList()\
- \009\009\009\009end\
- \009\009\009end,\
- \009\009\009[\"ID label\"] = function(tItem)\
- \009\009\009\009dialogBox(\"ID label\",disk.getDiskID(tItem.s)..\" \"..tostring(disk.getLabel(tItem.s)),{\"ok\"})\
- \009\009\009end,\
- \009\009\009[\"Set label\"] = function(tItem)\
- \009\009\009\009local name = InputBox(\"Label?\")\
- \009\009\009\009if name then\
- \009\009\009\009\009disk.setLabel(tItem.s,name)\
- \009\009\009\009end\
- \009\009\009end,\
- \009\009\009[\"Clear label\"] = function(tItem)\
- \009\009\009\009if dialogBox(\"Confirm\",\"Cleal Label from \"..tItem.s,{\"yes\",\"no\"}) == 1 then\
- \009\009\009\009\009disk.setLabel(tItem.s)\
- \009\009\009\009end\
- \009\009\009end\
- \009\009},\
- \009\009folder = {\
- \009\009\009[\"Open\"] = function(temp)\
- \009\009\009\009table.insert(path,temp.n)\
- \009\009\009\009newList()\
- \009\009\009end,\
- \009\009\009[\"Copy\"] = function(tItem)\
- \009\009\009\009clipboard = {stringPath(), tItem.n}\
- \009\009\009\009cut = false\
- \009\009\009end,\
- \009\009\009[\"Cut\"] = function(tItem)\
- \009\009\009\009clipboard = {stringPath(), tItem.n}\
- \009\009\009\009cut = true\
- \009\009\009end,\
- \009\009\009[\"Delete\"] = function(tItem)\
- \009\009\009\009if dialogBox(\"Confirm\",\"Delete \"..tItem.id..\" \"..tItem.n,{\"yes\",\"no\"}) == 1 then\
- \009\009\009\009\009if fs.isReadOnly(stringPath()..fSlash..tItem.n) then\
- \009\009\009\009\009\009dialogBox(\"ERROR\",tItem.id..\" Is read Only\",{\"ok\"})\
- \009\009\009\009\009else\
- \009\009\009\009\009\009fs.delete(stringPath()..fSlash..tItem.n)\
- \009\009\009\009\009\009newList()\
- \009\009\009\009\009end\
- \009\009\009\009end\
- \009\009\009end,\
- \009\009\009[\"Rename\"] = function(tItem)\
- \009\009\009\009local sName = InputBox(\"New Name\")\
- \009\009\009\009if type(sName) == \"string\" and sName ~= \"\" then\
- \009\009\009\009\009local s, m = pcall(function()\
- \009\009\009\009\009\009fs.move(stringPath()..fSlash..tItem.n,stringPath()..fSlash..sName)\
- \009\009\009\009\009end)\
- \009\009\009\009\009if not s then\
- \009\009\009\009\009\009dialogBox(\"Error\", (m or \"Rename failed\"), {\"ok\"})\
- \009\009\009\009\009end\
- \009\009\009\009end\
- \009\009\009\009newList()\
- \009\009\009end\
- \009\009},\
- \009\009file = {\
- \009\009\009[\"Run\"] = {\
- \009\009\009\009[\"Run\"] = function(tItem)\
- \009\009\009\009\009osRunSpaces(stringPath()..fSlash..tItem.n)\
- \009\009\009\009end,\
- \009\009\009\009[\"Run CMD\"] = function(tItem)\
- \009\009\009\009\009local cmd = InputBox(\"Commands\")\
- \009\009\009\009\009if cmd then\
- \009\009\009\009\009\009osRunSpaces(stringPath()..fSlash..tItem.n,unpack(fixArgs(cmd)))\
- \009\009\009\009\009end\
- \009\009\009\009end,\
- \009\009\009},\
- \009\009\009[\"Open With\"] = customLaunch,\
- \009\009\009[\"Rename\"] = function(tItem)\
- \009\009\009\009local sName = InputBox(\"New Name\")\
- \009\009\009\009if type(sName) == \"string\" and sName ~= \"\" then\
- \009\009\009\009\009local s, m = pcall(function()\
- \009\009\009\009\009\009fs.move(stringPath()..fSlash..tItem.n,stringPath()..fSlash..sName)\
- \009\009\009\009\009end)\
- \009\009\009\009\009if not s then\
- \009\009\009\009\009\009dialogBox(\"Error\", (m or \"Rename failed\"), {\"ok\"})\
- \009\009\009\009\009end\
- \009\009\009\009end\
- \009\009\009\009newList()\
- \009\009\009end,\
- \009\009\009[\"Delete\"] = function(tItem)\
- \009\009\009\009if dialogBox(\"Confirm\",\"Delete \"..tItem.id..\" \"..tItem.n,{\"yes\",\"no\"}) == 1 then\
- \009\009\009\009\009if fs.isReadOnly(stringPath()..fSlash..tItem.n) then\
- \009\009\009\009\009\009dialogBox(\"ERROR\",tItem.id..\" Is read Only\",{\"ok\"})\
- \009\009\009\009\009else\
- \009\009\009\009\009\009fs.delete(stringPath()..fSlash..tItem.n)\
- \009\009\009\009\009\009newList()\
- \009\009\009\009\009end\
- \009\009\009\009end\
- \009\009\009end,\
- \009\009\009[\"Cut\"] = function(tItem)\
- \009\009\009\009clipboard = {stringPath(), tItem.n}\
- \009\009\009\009cut = true\
- \009\009\009end,\
- \009\009\009[\"Copy\"] = function(tItem)\
- \009\009\009\009clipboard = {stringPath(), tItem.n}\
- \009\009\009\009cut = false\
- \009\009\009end\
- \009\009},\
- \009\009audio = {\
- \009\009\009[\"Play\"] = 1,\
- \009\009\009[\"Eject\"] = 1\
- \009\009},\
- \009\009back = {\
- \009\009},\
- \009\009blank = { -- tmenu.blank.Paste = \
- \009\009\009[\"Paste\"] = nil,\
- \009\009\009[\"New File\"] = function()\
- \009\009\009\009local name = InputBox()\
- \009\009\009\009if name then\
- \009\009\009\009\009if fs.exists(stringPath()..fSlash..name) then\
- \009\009\009\009\009\009dialogBox(\"ERROR\",\"Name exists\",{\"ok\"})\
- \009\009\009\009\009else\
- \009\009\009\009\009\009local file = fs.open(stringPath()..fSlash..name,\"w\")\
- \009\009\009\009\009\009if file then\
- \009\009\009\009\009\009\009file.write(\"\")\
- \009\009\009\009\009\009\009file.close()\
- \009\009\009\009\009\009\009newList()\
- \009\009\009\009\009\009else\
- \009\009\009\009\009\009\009dialogBox(\"ERROR\",\"File not created\",{\"ok\"})\
- \009\009\009\009\009\009end\
- \009\009\009\009\009end\
- \009\009\009\009end\
- \009\009\009end,\
- \009\009\009[\"New Folder\"] = function()\
- \009\009\009\009local name = InputBox()\
- \009\009\009\009if name then\
- \009\009\009\009\009if fs.exists(stringPath()..fSlash..name) then\
- \009\009\009\009\009\009dialogBox(\"ERROR\",\"Name exists\",{\"ok\"})\
- \009\009\009\009\009else\
- \009\009\009\009\009\009if pcall(fs.makeDir,stringPath()..fSlash..name) then\
- \009\009\009\009\009\009\009newList()\
- \009\009\009\009\009\009else\
- \009\009\009\009\009\009\009dialogBox(\"ERROR\",\"Access Denied\",{\"ok\"})\
- \009\009\009\009\009\009end\
- \009\009\009\009\009end\
- \009\009\009\009end\
- \009\009\009end,\
- \009\009\009[\"Preferences\"] = preferences\
- \009\009},\
- \009}\
- \009\
- \009-- end right click menu\
- \009\
- \009local function scrollBar(posX,posY)\
- \009\009if posX == boxOffX+boxW+1 and posY > boxOffY and posY <= boxOffY+boxH then\
- \009\009\009if #tItemList > boxH then\
- \009\009\009\009if posY == boxOffY + 1 then\
- \009\009\009\009\009listOff = 0\
- \009\009\009\009elseif posY == boxOffY+boxH then\
- \009\009\009\009\009listOff = #tItemList + 1 - boxH\
- \009\009\009\009else\
- \009\009\009\009\009listOff = math.ceil((posY - boxOffY - 1 )*(((#tItemList - boxH+2)/boxH)))\
- \009\009\009\009end\
- \009\009\009\009flag = true\
- \009\009\009end\
- \009\009end\
- \009end\
- \009\
- \009newList()\
- \009\
- \009while bRun do\
- \009\009if flag then\
- \009\009\009flag = false\
- \009\009\009-- clear\
- \009\009\009if oldHight ~= hight and oldWidth ~= width then\
- \009\009\009\009term.setBackgroundColor(colors.black)\
- \009\009\009\009term.clear()\
- \009\009\009\009oldHight,oldWidth = hight,width\
- \009\009\009end\
- \009\009\009-- draw top title bar\
- \009\009\009local b = tbarW - #title -2\
- \009\009\009if b < 0 then\
- \009\009\009\009b = 0\
- \009\009\009end\
- \009\009\009printC(tbarX,tbarY,titleBar.txt,titleBar.back,string.sub(\" \"..title,1,tbarW)..string.rep(\" \",b))\
- \009\009\009term.setTextColor(colors.white)\
- \009\009\009term.setBackgroundColor(colors.red)\
- \009\009\009term.write(\"X\")\
- \009\009\009\
- \009\009\009-- draw location bar\
- \009\009\009local a = barW - #sPath - 1\
- \009\009\009if a < 0 then\
- \009\009\009\009a = 0\
- \009\009\009end\
- \009\009\009local tmppath = sPath\
- \009\009\009if shell and shell.getDisplayName then\
- \009\009\009\009tmppath = shell.getDisplayName(sPath)\
- \009\009\009\009--dialogBox(\"yay\")\
- \009\009\009else\
- \009\009\009\009--dialogBox(\"moop\")\
- \009\009\009end\
- \009\009\009tmppath = tmppath or sPath\
- \009\009\009local a = barW - #tmppath - 1\
- \009\009\009if a < 0 then\
- \009\009\009\009a = 0\
- \009\009\009end\
- \009\009\009printC(barX,barY,addressBar.txt,addressBar.back,string.sub(\" \"..tmppath,1,barW)..string.rep(\" \",a))\
- \009\009\009\
- \009\009\009-- draw scroll bar\
- \009\009\009if #tItemList > boxH then\
- \009\009\009\009term.setBackgroundColor(scrollCol.back)\
- \009\009\009\009for i = 1,boxH do\
- \009\009\009\009\009term.setCursorPos(boxOffX+boxW+1,i + boxOffY)\
- \009\009\009\009\009local scroll = math.floor( boxH* (listOff/(#tItemList-boxH+2)) )+1\
- \009\009\009\009\009if i == scroll then\
- \009\009\009\009\009\009term.setBackgroundColor(scrollCol.button)\
- \009\009\009\009\009\009term.write(\" \")\
- \009\009\009\009\009\009term.setBackgroundColor(scrollCol.back)\
- \009\009\009\009\009else\
- \009\009\009\009\009\009term.write(\" \")\
- \009\009\009\009\009end\
- \009\009\009\009end\
- \009\009\009else\
- \009\009\009\009term.setBackgroundColor(scrollCol.off)\
- \009\009\009\009for i = 1,boxH do\
- \009\009\009\009\009term.setCursorPos(boxOffX+boxW+1,i + boxOffY)\
- \009\009\009\009\009term.write(\" \")\
- \009\009\009\009end\
- \009\009\009end\
- \009\009\009\
- \009\009\009-- draw main section\
- \
- \009\009\009for i = 1,boxH do -- listOff\
- \009\009\009\009local sel = i+listOff\
- \009\009\009\009if tItemList[sel] then\
- \009\009\009\009\009printC(1+boxOffX,i+boxOffY,(tIcons[tItemList[sel].id].tCol or itemWindo.txt),(tIcons[tItemList[sel].id].bCol or itemWindo.back),( tIcons[tItemList[sel].id].txt or \" \"))\
- \009\009\009\009\009printC(4+boxOffX,i+boxOffY,itemWindo.txt,itemWindo.back,string.sub(\" \"..tItemList[sel].n..pading,1,boxW-3))\
- \009\009\009\009else\
- \009\009\009\009\009printC(1+boxOffX,i+boxOffY,itemWindo.txt,itemWindo.back,pading)\
- \009\009\009\009end\
- \009\009\009end\
- \009\009\009\
- \009\009\009if bugTest then\
- \009\009\009\009printC(1,1,\"black\",\"white\",listOff..\" \"..boxOffY..\" \"..boxH)\
- \009\009\009end\
- \009\009\009\
- \009\009end\
- \009\009\
- \009\009-- react to events\
- \009\009local event = {os.pullEvent()}\
- \009\009\
- \009\009if event[1] == \"mouse_click\" then\
- \009\009\009if inBouwndry(event[3],event[4],boxOffX+1,boxOffY+1,boxW,boxH) then\
- \009\009\009\009local selected = tItemList[event[4]+listOff-boxOffY]\
- \009\009\009\009if selected and inBouwndry(event[3],event[4],boxOffX+1,event[4],#selected.n + 4,1) then\
- \009\009\009\009\009if event[2] == 1 then -- left mouse\
- \009\009\009\009\009\009if selected.id == \"back\" then\
- \009\009\009\009\009\009\009table.remove(path,#path)\
- \009\009\009\009\009\009\009newList()\
- \009\009\009\009\009\009elseif selected.id == \"folder\" or selected.id == \"disk\" then\
- \009\009\009\009\009\009\009table.insert(path,selected.n)\
- \009\009\009\009\009\009\009newList()\
- \009\009\009\009\009\009elseif selected.id == \"file\" then\
- \009\009\009\009\009\009\009if dialogBox(\"Run file ?\",selected.n,{\"yes\",\"no\"}) == 1 then\
- \009\009\009\009\009\009\009\009osRunSpaces(stringPath()..fSlash..selected.n)\
- \009\009\009\009\009\009\009end\
- \009\009\009\009\009\009flag = true\
- \009\009\009\009\009\009end\
- \009\009\009\009\009elseif event[2] == 2 then -- right mouse\
- \009\009\009\009\009\009rClickMenu(\"Options\",tmenu[selected.id],selected,event[3],event[4])\
- \009\009\009\009\009\009flag = true\
- \009\009\009\009\009end\
- \009\009\009\009elseif event[2] == 2 then -- right clicking not on object\
- \009\009\009\009\009if clipboard then\
- \009\009\009\009\009\009tmenu.blank.Paste = paste\
- \009\009\009\009\009else\
- \009\009\009\009\009\009tmenu.blank.Paste = nil\
- \009\009\009\009\009end\
- \009\009\009\009\009rClickMenu(\"Options\",tmenu[\"blank\"],selected,event[3],event[4])\
- \009\009\009\009\009flag = true\
- \009\009\009\009end\
- \009\009\009elseif event[2] == 1 and event[3] == exitX and event[4] == exitY then\
- \009\009\009\009if dialogBox(\"Confirm\",\"Exit application\",{\"yes\",\"no\"}) == 1 then\
- \009\009\009\009\009bRun = false\
- \009\009\009\009end\
- \009\009\009\009flag = true\
- \009\009\009elseif event[2] == 1 then\
- \009\009\009\009scrollBar(event[3],event[4])\
- \009\009\009end\
- \009\009elseif event[1] == \"mouse_scroll\" then -- flag this needs new math\
- \009\009\009local old = listOff\
- \009\009\009listOff = listOff + event[2]\
- \009\009\009if listOff < 0 then\
- \009\009\009\009listOff = 0\
- \009\009\009end\
- \009\009\009if #tItemList + 1 - boxH > 0 and listOff > #tItemList + 1 - boxH then\
- \009\009\009\009listOff = #tItemList + 1 - boxH\
- \009\009\009elseif listOff > 0 and #tItemList + 1 - boxH < 0 then\
- \009\009\009\009listOff = 0\
- \009\009\009end\
- \009\009\009if listOff ~= old then\
- \009\009\009\009flag = true\
- \009\009\009end\
- \009\009\
- \009\009elseif event[1] == \"mouse_drag\" then -- scroll bar\
- \009\009\009scrollBar(event[3],event[4])\
- \009\009elseif event[1] == \"disk\" or event[1] == \"disk_eject\" then\
- \009\009\009newList()\
- \009\009elseif event[1] == \"window_resize\" then\
- \009\009\009termX,termY = term.getSize()\
- \009\009\009offsetX,offsetY = 1,1\
- \009\009\009hight,width = math.ceil(termY-2),math.ceil(termX-2)\
- \009\009\009\
- \009\009\009boxOffX,boxOffY = offsetX,offsetY + 2\
- \009\009\009boxH,boxW = hight - 2 ,width - 2\
- \009\009\009\
- \009\009\009barX,barY = offsetX + 1,offsetY + 2\
- \009\009\009barH,barW = 1,width - 1\
- \009\009\009\
- \009\009\009tbarX,tbarY = offsetX + 1,offsetY + 1\
- \009\009\009tbarH,tbarW = 1,width - 1\
- \009\009\009\
- \009\009\009exitX,exitY = offsetX + width - 1 ,offsetY + 1\
- \009\009\009pading = string.rep(\" \",boxW)\
- \009\009\009\
- \009\009\009flag = true\
- \009\009elseif event[1] == \"redraw\" then\
- \009\009\009flag = true\
- \009\009end\
- \009end\
- end\
- \
- local function main()\
- \009if term.isColor() then\
- \009\009clear()\
- \009\009fileSelect()\
- \009\009clear()\
- \009else\
- \009\009error(\"Not an Advanced Computer (gold) \")\
- \009end\
- end\
- \
- local trash = (norun or main())",
- gps = "\
- local function printUsage()\
- \009print( \"Usages:\" )\
- \009print( \"gps host\" )\
- \009print( \"gps host <x> <y> <z>\" )\
- \009print( \"gps locate\" )\
- end\
- \
- local tArgs = { ... }\
- if #tArgs < 1 then\
- \009printUsage()\
- \009return\
- end\
- \009\
- local sCommand = tArgs[1]\
- if sCommand == \"locate\" then\
- \009-- \"gps locate\"\
- \009-- Just locate this computer (this will print the results)\
- \009gps.locate( 2, true )\
- \009\
- elseif sCommand == \"host\" then\
- \009-- \"gps host\"\
- \009-- Act as a GPS host\
- \009if pocket then\
- \009 print( \"GPS Hosts must be stationary\" )\
- \009 return\
- \009end\
- \
- \009-- Find a modem\
- \009local sModemSide = nil\
- \009for n,sSide in ipairs( rs.getSides() ) do\
- \009\009if peripheral.getType( sSide ) == \"modem\" and peripheral.call( sSide, \"isWireless\" ) then\009\
- \009\009\009sModemSide = sSide\
- \009\009\009break\
- \009\009end\
- \009end\
- \
- \009if sModemSide == nil then\
- \009\009print( \"No wireless modems found. 1 required.\" )\
- \009\009return\
- \009end\
- \
- \009-- Determine position\
- \009local x,y,z\
- \009if #tArgs >= 4 then\
- \009\009-- Position is manually specified\
- \009\009x = tonumber(tArgs[2])\
- \009\009y = tonumber(tArgs[3])\
- \009\009z = tonumber(tArgs[4])\
- \009\009if x == nil or y == nil or z == nil then\
- \009\009\009printUsage()\
- \009\009\009return\
- \009\009end\
- \009\009print( \"Position is \"..x..\",\"..y..\",\"..z )\
- \009else\
- \009\009-- Position is to be determined using locate\009\009\
- \009\009x,y,z = gps.locate( 2, true )\
- \009\009if x == nil then\
- \009\009\009print( \"Run \\\"gps host <x> <y> <z>\\\" to set position manually\" )\
- \009\009\009if bCloseChannel then\
- \009\009\009\009print( \"Closing GPS channel\" )\
- \009\009\009\009modem.close( gps.CHANNEL_GPS )\
- \009\009\009end\
- \009\009\009return\
- \009\009end\
- \009end\
- \009\
- \009-- Open a channel\
- \009local modem = peripheral.wrap( sModemSide )\
- print( \"Opening channel on modem \"..sModemSide )\
- modem.open( gps.CHANNEL_GPS )\
- \
- \009-- Serve requests indefinately\
- \009local nServed = 0\
- \009while true do\
- \009\009local e, p1, p2, p3, p4, p5 = os.pullEvent( \"modem_message\" )\
- \009\009if e == \"modem_message\" then\
- \009\009\009-- We received a message from a modem\
- \009\009\009local sSide, sChannel, sReplyChannel, sMessage, nDistance = p1, p2, p3, p4, p5\
- \009\009\009if sSide == sModemSide and sChannel == gps.CHANNEL_GPS and sMessage == \"PING\" and nDistance then\
- \009\009\009\009-- We received a ping message on the GPS channel, send a response\
- \009\009\009\009modem.transmit( sReplyChannel, gps.CHANNEL_GPS, { x, y, z } )\
- \009\009\009\
- \009\009\009\009-- Print the number of requests handled\
- \009\009\009\009nServed = nServed + 1\
- \009\009\009\009if nServed > 1 then\
- \009\009\009\009\009local x,y = term.getCursorPos()\
- \009\009\009\009\009term.setCursorPos(1,y-1)\
- \009\009\009\009end\
- \009\009\009\009print( nServed..\" GPS requests served\" )\
- \009\009\009end\
- \009\009end\
- \009end\
- \
- \009-- Close the channel\
- print( \"Closing channel\" )\
- modem.close( gps.CHANNEL_GPS )\
- \
- else\
- \009-- \"gps somethingelse\"\
- \009-- Error\
- \009printUsage()\
- \009\
- end",
- adventure = "\
- local tBiomes = {\
- \009\"in a forest\",\
- \009\"in a pine forest\",\
- \009\"knee deep in a swamp\",\
- \009\"in a mountain range\",\
- \009\"in a desert\",\
- \009\"in a grassy plain\",\
- \009\"in frozen tundra\",\
- }\
- \
- local function hasTrees( _nBiome )\
- \009return _nBiome <= 3\
- end\
- \
- local function hasStone( _nBiome )\
- \009return _nBiome == 4\
- end\
- \
- local function hasRivers( _nBiome )\
- \009return _nBiome ~= 3 and _nBiome ~= 5\
- end\
- \
- local items = {\
- \009[\"no tea\"] = {\
- \009\009droppable = false,\
- \009\009desc = \"Pull yourself together man.\",\
- \009},\
- \009[\"a pig\"] = {\
- \009\009heavy = true,\
- \009\009creature = true,\
- \009\009drops = { \"some pork\" },\
- \009\009aliases = { \"pig\" },\
- \009\009desc = \"The pig has a square nose.\",\
- \009},\
- \009[\"a cow\"] = {\
- \009\009heavy = true,\
- \009\009creature = true,\
- \009\009aliases = { \"cow\" },\
- \009\009desc = \"The cow stares at you blankly.\",\
- \009},\
- \009[\"a sheep\"] = {\
- \009\009heavy = true,\
- \009\009creature = true,\
- \009\009hitDrops = { \"some wool\" },\
- \009\009aliases = { \"sheep\" },\
- \009\009desc = \"The sheep is fluffy.\",\
- \009},\
- \009[\"a chicken\"] = {\
- \009\009heavy = true,\
- \009\009creature = true,\
- \009\009drops = { \"some chicken\" },\
- \009\009aliases = { \"chicken\" },\
- \009\009desc = \"The chicken looks delicious.\",\
- \009},\
- \009[\"a creeper\"] = {\
- \009\009heavy = true,\
- \009\009creature = true,\
- \009\009monster = true,\
- \009\009aliases = { \"creeper\" },\
- \009\009desc = \"The creeper needs a hug.\",\
- \009},\
- \009[\"a skeleton\"] = {\
- \009\009heavy = true,\
- \009\009creature = true,\
- \009\009monster = true,\
- \009\009aliases = { \"skeleton\" },\
- \009\009nocturnal = true,\
- \009\009desc = \"The head bone's connected to the neck bone, the neck bone's connected to the chest bone, the chest bone's connected to the arm bone, the arm bone's connected to the bow, and the bow is pointed at you.\",\
- \009},\
- \009[\"a zombie\"] = {\
- \009\009heavy = true,\
- \009\009creature = true,\
- \009\009monster = true,\
- \009\009aliases = { \"zombie\" },\
- \009\009nocturnal = true,\
- \009\009desc = \"All he wants to do is eat your brains.\",\
- \009},\
- \009[\"a spider\"] = {\
- \009\009heavy = true,\
- \009\009creature = true,\
- \009\009monster = true,\
- \009\009aliases = { \"spider\" },\
- \009\009desc = \"Dozens of eyes stare back at you.\",\
- \009},\
- \009[\"a cave entrance\"] = {\
- \009\009heavy = true,\
- \009\009aliases = { \"cave entance\", \"cave\", \"entrance\" },\
- \009\009desc = \"The entrance to the cave is dark, but it looks like you can climb down.\",\
- \009},\
- \009[\"an exit to the surface\"] = {\
- \009\009heavy = true,\
- \009\009aliases = { \"exit to the surface\", \"exit\", \"opening\" },\
- \009\009desc = \"You can just see the sky through the opening.\",\
- \009},\
- \009[\"a river\"] = {\
- \009\009heavy = true,\
- \009\009aliases = { \"river\" },\
- \009\009desc = \"The river flows majestically towards the horizon. It doesn't do anything else.\",\
- \009},\
- \009[\"some wood\"] = {\
- \009\009aliases = { \"wood\" },\
- \009\009material = true,\
- \009\009desc = \"You could easilly craft this wood into planks.\",\
- \009},\
- \009[\"some planks\"] = {\
- \009\009aliases = { \"planks\", \"wooden planks\", \"wood planks\" },\
- \009\009desc = \"You could easilly craft these planks into sticks.\",\
- \009},\
- \009[\"some sticks\"] = {\
- \009\009aliases = { \"sticks\", \"wooden sticks\", \"wood sticks\" },\
- \009\009desc = \"A perfect handle for torches or a pickaxe.\",\
- \009},\
- \009[\"a crafting table\"] = {\
- \009\009aliases = { \"crafting table\", \"craft table\", \"work bench\", \"workbench\", \"crafting bench\", \"table\", },\
- \009\009desc = \"It's a crafting table. I shouldn't tell you this, but these don't actually do anything in this game, you can craft tools whenever you like.\",\
- \009},\
- \009[\"a furnace\"] = {\
- \009\009aliases = { \"furnace\" },\
- \009\009desc = \"It's a furnace. Between you and me, these don't actually do anything in this game.\",\
- \009},\
- \009[\"a wooden pickaxe\"] = {\
- \009\009aliases = { \"pickaxe\", \"pick\", \"wooden pick\", \"wooden pickaxe\", \"wood pick\", \"wood pickaxe\" },\
- \009\009tool = true,\
- \009\009toolLevel = 1,\
- \009\009toolType = \"pick\",\
- \009\009desc = \"The pickaxe looks good for breaking stone and coal.\",\
- \009},\
- \009[\"a stone pickaxe\"] = {\
- \009\009aliases = { \"pickaxe\", \"pick\", \"stone pick\", \"stone pickaxe\" },\
- \009\009tool = true,\
- \009\009toolLevel = 2,\
- \009\009toolType = \"pick\",\
- \009\009desc = \"The pickaxe looks good for breaking iron.\",\
- \009},\
- \009[\"an iron pickaxe\"] = {\
- \009\009aliases = { \"pickaxe\", \"pick\", \"iron pick\", \"iron pickaxe\" },\
- \009\009tool = true,\
- \009\009toolLevel = 3,\
- \009\009toolType = \"pick\",\
- \009\009desc = \"The pickaxe looks strong enough to break diamond.\",\
- \009},\
- \009[\"a diamond pickaxe\"] = {\
- \009\009aliases = { \"pickaxe\", \"pick\", \"diamond pick\", \"diamond pickaxe\" },\
- \009\009tool = true,\
- \009\009toolLevel = 4,\
- \009\009toolType = \"pick\",\
- \009\009desc = \"Best. Pickaxe. Ever.\",\
- \009},\
- \009[\"a wooden sword\"] = {\
- \009\009aliases = { \"sword\", \"wooden sword\", \"wood sword\" },\
- \009\009tool = true,\
- \009\009toolLevel = 1,\
- \009\009toolType = \"sword\",\
- \009\009desc = \"Flimsy, but better than nothing.\",\
- \009},\
- \009[\"a stone sword\"] = {\
- \009\009aliases = { \"sword\", \"stone sword\" },\
- \009\009tool = true,\
- \009\009toolLevel = 2,\
- \009\009toolType = \"sword\",\
- \009\009desc = \"A pretty good sword.\",\
- \009},\
- \009[\"an iron sword\"] = {\
- \009\009aliases = { \"sword\", \"iron sword\" },\
- \009\009tool = true,\
- \009\009toolLevel = 3,\
- \009\009toolType = \"sword\",\
- \009\009desc = \"This sword can slay any enemy.\",\
- \009},\
- \009[\"a diamond sword\"] = {\
- \009\009aliases = { \"sword\", \"diamond sword\" },\
- \009\009tool = true,\
- \009\009toolLevel = 4,\
- \009\009toolType = \"sword\",\
- \009\009desc = \"Best. Sword. Ever.\",\
- \009},\
- \009[\"a wooden shovel\"] = {\
- \009\009aliases = { \"shovel\", \"wooden shovel\", \"wood shovel\" },\
- \009\009tool = true,\
- \009\009toolLevel = 1,\
- \009\009toolType = \"shovel\",\
- \009\009desc = \"Good for digging holes.\",\
- \009},\
- \009[\"a stone shovel\"] = {\
- \009\009aliases = { \"shovel\", \"stone shovel\" },\
- \009\009tool = true,\
- \009\009toolLevel = 2,\
- \009\009toolType = \"shovel\",\
- \009\009desc = \"Good for digging holes.\",\
- \009},\
- \009[\"an iron shovel\"] = {\
- \009\009aliases = { \"shovel\", \"iron shovel\" },\
- \009\009tool = true,\
- \009\009toolLevel = 3,\
- \009\009toolType = \"shovel\",\
- \009\009desc = \"Good for digging holes.\",\
- \009},\
- \009[\"a diamond shovel\"] = {\
- \009\009aliases = { \"shovel\", \"diamond shovel\" },\
- \009\009tool = true,\
- \009\009toolLevel = 4,\
- \009\009toolType = \"shovel\",\
- \009\009desc = \"Good for digging holes.\",\
- \009},\
- \009[\"some coal\"] = {\
- \009\009aliases = { \"coal\" },\
- \009\009ore = true,\
- \009\009toolLevel = 1,\
- \009\009toolType = \"pick\",\
- \009\009desc = \"That coal looks useful for building torches, if only you had a pickaxe to mine it.\",\
- \009},\
- \009[\"some dirt\"] = {\
- \009\009aliases = { \"dirt\" },\
- \009\009material = true,\
- \009\009desc = \"Why not build a mud hut?\",\
- \009},\
- \009[\"some stone\"] = {\
- \009\009aliases = { \"stone\", \"cobblestone\" },\
- \009\009material = true,\
- \009\009ore = true,\
- \009\009infinite = true,\
- \009\009toolLevel = 1,\
- \009\009toolType = \"pick\",\
- \009\009desc = \"Stone is useful for building things, and making stone pickaxes.\",\
- \009},\
- \009[\"some iron\"] = {\
- \009\009aliases = { \"iron\" },\
- \009\009material = true,\
- \009\009ore = true,\
- \009\009toolLevel = 2,\
- \009\009toolType = \"pick\",\
- \009\009desc = \"That iron looks mighty strong, you'll need a stone pickaxe to mine it.\",\
- \009},\
- \009[\"some diamond\"] = {\
- \009\009aliases = { \"diamond\", \"diamonds\" },\
- \009\009material = true,\
- \009\009ore = true,\
- \009\009toolLevel = 3,\
- \009\009toolType = \"pick\",\
- \009\009desc = \"Sparkly, rare, and impossible to mine without an iron pickaxe.\",\
- \009},\
- \009[\"some torches\"] = {\
- \009\009aliases = { \"torches\", \"torch\" },\
- \009\009desc = \"These won't run out for a while.\",\
- \009},\
- \009[\"a torch\"] = {\
- \009\009aliases = { \"torch\" },\
- \009\009desc = \"Fire, fire, burn so bright, won't you light my cave tonight?\",\
- \009},\
- \009[\"some wool\"] = {\
- \009\009aliases = { \"wool\" },\
- \009\009material = true,\
- \009\009desc = \"Soft and good for building.\",\
- \009},\
- \009[\"some pork\"] = {\
- \009\009aliases = { \"pork\", \"porkchops\" },\
- \009\009food = true,\
- \009\009desc = \"Delicious and nutricious.\",\
- \009},\
- \009[\"some chicken\"] = {\
- \009\009aliases = { \"chicken\" },\
- \009\009food = true,\
- \009\009desc = \"Finger licking good.\",\
- \009},\
- }\
- \
- local tAnimals = {\
- \009\"a pig\", \"a cow\", \"a sheep\", \"a chicken\",\
- }\
- \
- local tMonsters = {\
- \009\"a creeper\", \"a skeleton\", \"a zombie\", \"a spider\"\
- }\
- \
- local tRecipes = {\
- \009[\"some planks\"] = { \"some wood\" },\
- \009[\"some sticks\"] = { \"some planks\" },\
- \009[\"some sticks\"] = { \"some planks\" },\
- \009[\"a crafting table\"] = { \"some planks\" },\
- \009[\"a furnace\"] = { \"some stone\" },\
- \009[\"some torches\"] = { \"some sticks\", \"some coal\" },\
- \009\
- \009[\"a wooden pickaxe\"] = { \"some planks\", \"some sticks\" },\
- \009[\"a stone pickaxe\"] = { \"some stone\", \"some sticks\" },\
- \009[\"an iron pickaxe\"] = { \"some iron\", \"some sticks\" },\
- \009[\"a diamond pickaxe\"] = { \"some diamond\", \"some sticks\" },\
- \
- \009[\"a wooden sword\"] = { \"some planks\", \"some sticks\" },\
- \009[\"a stone sword\"] = { \"some stone\", \"some sticks\" },\
- \009[\"an iron sword\"] = { \"some iron\", \"some sticks\" },\
- \009[\"a diamond sword\"] = { \"some diamond\", \"some sticks\" },\
- \
- \009[\"a wooden shovel\"] = { \"some planks\", \"some sticks\" },\
- \009[\"a stone shovel\"] = { \"some stone\", \"some sticks\" },\
- \009[\"an iron shovel\"] = { \"some iron\", \"some sticks\" },\
- \009[\"a diamond shovel\"] = { \"some diamond\", \"some sticks\" },\
- }\
- \
- local tGoWest = {\
- \009\"(life is peaceful there)\",\
- \009\"(lots of open air)\",\
- \009\"(to begin life anew)\",\
- \009\"(this is what we'll do)\",\
- \009\"(sun in winter time)\",\
- \009\"(we will do just fine)\",\
- \009\"(where the skies are blue)\",\
- \009\"(this and more we'll do)\",\
- }\
- local nGoWest = 0\
- \
- local bRunning = true\
- local tMap = { { {}, }, }\
- local x,y,z = 0,0,0\
- local inventory = {\
- \009[\"no tea\"] = items[\"no tea\"],\
- }\
- \
- local nTurn = 0\
- local nTimeInRoom = 0\
- local bInjured = false\
- \
- local tDayCycle = {\
- \009\"It is daytime.\",\
- \009\"It is daytime.\",\
- \009\"It is daytime.\",\
- \009\"It is daytime.\",\
- \009\"It is daytime.\",\
- \009\"It is daytime.\",\
- \009\"It is daytime.\",\
- \009\"It is daytime.\",\
- \009\"The sun is setting.\",\
- \009\"It is night.\",\
- \009\"It is night.\",\
- \009\"It is night.\",\
- \009\"It is night.\",\
- \009\"It is night.\",\
- \009\"The sun is rising.\",\
- }\
- \
- local function getTimeOfDay()\
- \009return math.fmod( math.floor(nTurn/3), #tDayCycle ) + 1\
- end\
- \
- local function isSunny()\
- \009return (getTimeOfDay() < 10)\
- end\
- \
- local function getRoom( x, y, z, dontCreate )\
- \009tMap[x] = tMap[x] or {}\
- \009tMap[x][y] = tMap[x][y] or {}\
- \009if not tMap[x][y][z] and dontCreate ~= true then\
- \009\009local room = {\
- \009\009\009items = {},\
- \009\009\009exits = {},\
- \009\009\009nMonsters = 0,\
- \009\009}\
- \009\009tMap[x][y][z] = room\
- \009\009\
- \009\009if y == 0 then\
- \009\009\009-- Room is above ground\
- \
- \009\009\009-- Pick biome\
- \009\009\009room.nBiome = math.random( 1, #tBiomes )\
- \009\009\009room.trees = hasTrees( room.nBiome )\
- \009\009\
- \009\009\009-- Add animals\
- \009\009\009if math.random(1,3) == 1 then\
- \009\009\009\009for n = 1,math.random(1,2) do\
- \009\009\009\009\009local sAnimal = tAnimals[ math.random( 1, #tAnimals ) ]\
- \009\009\009\009\009room.items[ sAnimal ] = items[ sAnimal ]\
- \009\009\009\009end\
- \009\009\009end\
- \009\009\009\
- \009\009\009-- Add surface ore\
- \009\009\009if math.random(1,5) == 1 or hasStone( room.nBiome ) then\
- \009\009\009\009room.items[ \"some stone\" ] = items[ \"some stone\" ]\
- \009\009\009end\
- \009\009\009if math.random(1,8) == 1 then\
- \009\009\009\009room.items[ \"some coal\" ] = items[ \"some coal\" ]\
- \009\009\009end\
- \009\009\009if math.random(1,8) == 1 and hasRivers( room.nBiome ) then\
- \009\009\009\009room.items[ \"a river\" ] = items[ \"a river\" ]\
- \009\009\009end\
- \
- \009\009\009-- Add exits\
- \009\009\009room.exits = {\
- \009\009\009\009[\"north\"] = true,\
- \009\009\009\009[\"south\"] = true,\
- \009\009\009\009[\"east\"] = true,\
- \009\009\009\009[\"west\"] = true,\
- \009\009\009}\
- \009\009\009if math.random(1,8) == 1 then\
- \009\009\009\009room.exits[\"down\"] = true\
- \009\009\009\009room.items[\"a cave entrance\"] = items[\"a cave entrance\"]\
- \009\009\009end\
- \009\009\009\009\009\009\
- \009\009else\
- \009\009\009-- Room is underground\
- \009\009\009-- Add exits\
- \009\009\009local function tryExit( sDir, sOpp, x, y, z )\
- \009\009\009\009local adj = getRoom( x, y, z, true )\
- \009\009\009\009if adj then\
- \009\009\009\009\009if adj.exits[sOpp] then\
- \009\009\009\009\009\009room.exits[sDir] = true\
- \009\009\009\009\009end\
- \009\009\009\009else\
- \009\009\009\009\009if math.random(1,3) == 1 then\
- \009\009\009\009\009\009room.exits[sDir] = true\
- \009\009\009\009\009end\
- \009\009\009\009end\
- \009\009\009end\
- \009\009\009\
- \009\009\009if y == -1 then\
- \009\009\009\009local above = getRoom( x, y + 1, z )\
- \009\009\009\009if above.exits[\"down\"] then\
- \009\009\009\009\009room.exits[\"up\"] = true\
- \009\009\009\009\009room.items[\"an exit to the surface\"] = items[\"an exit to the surface\"]\
- \009\009\009\009end\
- \009\009\009else\
- \009\009\009\009tryExit( \"up\", \"down\", x, y + 1, z )\
- \009\009\009end\
- \009\009\009\
- \009\009\009if y > -3 then\
- \009\009\009\009tryExit( \"down\", \"up\", x, y - 1, z )\
- \009\009\009end\
- \009\009\009\
- \009\009\009tryExit( \"east\", \"west\", x - 1, y, z )\
- \009\009\009tryExit( \"west\", \"east\", x + 1, y, z )\
- \009\009\009tryExit( \"north\", \"south\", x, y, z + 1 )\
- \009\009\009tryExit( \"south\", \"north\", x, y, z - 1 )\009\
- \009\009\009\
- \009\009\009-- Add ores\
- \009\009\009room.items[ \"some stone\" ] = items[ \"some stone\" ]\
- \009\009\009if math.random(1,3) == 1 then\
- \009\009\009\009room.items[ \"some coal\" ] = items[ \"some coal\" ]\
- \009\009\009end\
- \009\009\009if math.random(1,8) == 1 then\
- \009\009\009\009room.items[ \"some iron\" ] = items[ \"some iron\" ]\
- \009\009\009end\
- \009\009\009if y == -3 and math.random(1,15) == 1 then\
- \009\009\009\009room.items[ \"some diamond\" ] = items[ \"some diamond\" ]\
- \009\009\009end\
- \009\009\009\
- \009\009\009-- Turn out the lights\
- \009\009\009room.dark = true\
- \009\009end\
- \009end\
- \009return tMap[x][y][z]\
- end\
- \
- local function itemize( t )\
- \009local item = next( t )\
- \009if item == nil then\
- \009\009return \"nothing\"\
- \009end\
- \009\
- \009local text = \"\"\
- \009while item do\
- \009\009text = text .. item\
- \009\009\
- \009\009local nextItem = next( t, item )\
- \009\009if nextItem ~= nil then\
- \009\009\009local nextNextItem = next( t, nextItem )\
- \009\009\009if nextNextItem == nil then\
- \009\009\009\009text = text .. \" and \"\
- \009\009\009else\
- \009\009\009\009text = text .. \", \"\
- \009\009\009end\
- \009\009end\
- \009\009item = nextItem\
- \009end\
- \009return text\
- end\
- \
- local function findItem( _tList, _sQuery )\
- \009for sItem, tItem in pairs( _tList ) do\
- \009\009if sItem == _sQuery then\
- \009\009\009return sItem\
- \009\009end\
- \009\009if tItem.aliases ~= nil then\
- \009\009\009for n, sAlias in pairs( tItem.aliases ) do\
- \009\009\009\009if sAlias == _sQuery then\
- \009\009\009\009\009return sItem\
- \009\009\009\009end\
- \009\009\009end\
- \009\009end\
- \009end\
- \009return nil\
- end\
- \
- local tMatches = {\
- \009[\"wait\"] = {\
- \009\009\"wait\",\
- \009},\
- \009[\"look\"] = {\
- \009\009\"look at the ([%a ]+)\",\
- \009\009\"look at ([%a ]+)\",\
- \009\009\"look\",\
- \009\009\"inspect ([%a ]+)\",\
- \009\009\"inspect the ([%a ]+)\",\
- \009\009\"inspect\",\
- \009},\
- \009[\"inventory\"] = {\
- \009\009\"check self\",\
- \009\009\"check inventory\",\
- \009\009\"inventory\",\
- \009\009\"i\",\
- \009},\
- \009[\"go\"] = {\
- \009\009\"go (%a+)\",\
- \009\009\"travel (%a+)\",\
- \009\009\"walk (%a+)\",\
- \009\009\"run (%a+)\",\
- \009\009\"go\",\
- \009},\
- \009[\"dig\"] = {\
- \009\009\"dig (%a+) using ([%a ]+)\",\
- \009\009\"dig (%a+) with ([%a ]+)\",\
- \009\009\"dig (%a+)\",\
- \009\009\"dig\",\
- \009},\
- \009[\"take\"] = {\
- \009\009\"pick up the ([%a ]+)\",\
- \009\009\"pick up ([%a ]+)\",\
- \009\009\"pickup ([%a ]+)\",\
- \009\009\"take the ([%a ]+)\",\
- \009\009\"take ([%a ]+)\",\
- \009\009\"take\",\
- \009},\
- \009[\"drop\"] = {\
- \009\009\"put down the ([%a ]+)\",\
- \009\009\"put down ([%a ]+)\",\
- \009\009\"drop the ([%a ]+)\",\
- \009\009\"drop ([%a ]+)\",\
- \009\009\"drop\",\
- \009},\
- \009[\"place\"] = {\
- \009\009\"place the ([%a ]+)\",\
- \009\009\"place ([%a ]+)\",\
- \009\009\"place\",\
- \009},\
- \009[\"cbreak\"] = {\
- \009\009\"punch the ([%a ]+)\",\
- \009\009\"punch ([%a ]+)\",\
- \009\009\"punch\",\
- \009\009\"break the ([%a ]+) with the ([%a ]+)\",\
- \009\009\"break ([%a ]+) with ([%a ]+) \",\
- \009\009\"break the ([%a ]+)\",\
- \009\009\"break ([%a ]+)\",\
- \009\009\"break\",\
- \009},\
- \009[\"mine\"] = {\
- \009\009\"mine the ([%a ]+) with the ([%a ]+)\",\
- \009\009\"mine ([%a ]+) with ([%a ]+)\",\
- \009\009\"mine ([%a ]+)\",\
- \009\009\"mine\",\
- \009},\
- \009[\"attack\"] = {\
- \009\009\"attack the ([%a ]+) with the ([%a ]+)\",\
- \009\009\"attack ([%a ]+) with ([%a ]+)\",\
- \009\009\"attack ([%a ]+)\",\
- \009\009\"attack\",\
- \009\009\"kill the ([%a ]+) with the ([%a ]+)\",\
- \009\009\"kill ([%a ]+) with ([%a ]+)\",\
- \009\009\"kill ([%a ]+)\",\
- \009\009\"kill\",\
- \009\009\"hit the ([%a ]+) with the ([%a ]+)\",\
- \009\009\"hit ([%a ]+) with ([%a ]+)\",\
- \009\009\"hit ([%a ]+)\",\
- \009\009\"hit\",\
- \009},\
- \009[\"craft\"] = {\
- \009\009\"craft a ([%a ]+)\",\
- \009\009\"craft some ([%a ]+)\",\
- \009\009\"craft ([%a ]+)\",\
- \009\009\"craft\",\
- \009\009\"make a ([%a ]+)\",\
- \009\009\"make some ([%a ]+)\",\
- \009\009\"make ([%a ]+)\",\
- \009\009\"make\",\
- \009},\
- \009[\"build\"] = {\
- \009\009\"build ([%a ]+) out of ([%a ]+)\",\
- \009\009\"build ([%a ]+) from ([%a ]+)\",\
- \009\009\"build ([%a ]+)\",\
- \009\009\"build\",\
- \009},\
- \009[\"eat\"] = {\
- \009\009\"eat a ([%a ]+)\",\
- \009\009\"eat the ([%a ]+)\",\
- \009\009\"eat ([%a ]+)\",\
- \009\009\"eat\",\
- \009},\
- \009[\"help\"] = {\
- \009\009\"help me\",\
- \009\009\"help\",\
- \009},\
- \009[\"exit\"] = {\
- \009\009\"exit\",\
- \009\009\"quit\",\
- \009\009\"goodbye\",\
- \009\009\"good bye\",\
- \009\009\"bye\",\
- \009\009\"farewell\",\
- \009},\
- }\
- \
- local commands = {}\
- local function doCommand( text )\
- \009if text == \"\" then\
- \009\009commands[ \"noinput\" ]()\
- \009\009return\
- \009end\
- \009\
- \009for sCommand, t in pairs( tMatches ) do\
- \009\009for n, sMatch in pairs( t ) do\
- \009\009\009local tCaptures = { string.match( text, \"^\" .. sMatch .. \"$\" ) }\
- \009\009\009if #tCaptures ~= 0 then\
- \009\009\009\009local fnCommand = commands[ sCommand ]\
- \009\009\009\009if #tCaptures == 1 and tCaptures[1] == sMatch then\
- \009\009\009\009\009fnCommand()\
- \009\009\009\009else\
- \009\009\009\009\009fnCommand( table.unpack( tCaptures ) )\
- \009\009\009\009end\
- \009\009\009\009return\
- \009\009\009end\
- \009\009end\
- \009end\
- \009commands[ \"badinput\" ]()\
- end\
- \
- function commands.wait()\
- \009print( \"Time passes...\" )\
- end\
- \
- function commands.look( _sTarget )\
- \009local room = getRoom( x,y,z )\
- \009if room.dark then\
- \009\009print( \"It is pitch dark.\" )\
- \009\009return\
- \009end\
- \
- \009if _sTarget == nil then\
- \009\009-- Look at the world\
- \009\009if y == 0 then\
- \009\009\009io.write( \"You are standing \" .. tBiomes[room.nBiome] .. \". \" )\
- \009\009\009print( tDayCycle[ getTimeOfDay() ] )\
- \009\009else\
- \009\009\009io.write( \"You are underground. \" )\
- \009\009\009if next( room.exits ) ~= nil then\
- \009\009\009\009print( \"You can travel \"..itemize( room.exits )..\".\" )\
- \009\009\009else\
- \009\009\009\009print()\
- \009\009\009end\
- \009\009end\
- \009\009if next( room.items ) ~= nil then\
- \009\009\009print( \"There is \" .. itemize( room.items ) .. \" here.\" )\
- \009\009end\
- \009\009if room.trees then\
- \009\009\009print( \"There are trees here.\" )\
- \009\009end\
- \009\009\
- \009else\
- \009\009-- Look at stuff\
- \009\009if room.trees and (_sTarget == \"tree\" or _sTarget == \"trees\") then\
- \009\009\009print( \"The trees look easy to break.\" )\
- \009\009elseif _sTarget == \"self\" or _sTarget == \"myself\" then\
- \009\009\009print( \"Very handsome.\" )\
- \009\009else\
- \009\009\009local tItem = nil\
- \009\009\009local sItem = findItem( room.items, _sTarget )\
- \009\009\009if sItem then\
- \009\009\009\009tItem = room.items[sItem]\
- \009\009\009else\
- \009\009\009\009sItem = findItem( inventory, _sTarget )\
- \009\009\009\009if sItem then\
- \009\009\009\009\009tItem = inventory[sItem]\
- \009\009\009\009end\
- \009\009\009end\
- \009\009\009\
- \009\009\009if tItem then\
- \009\009\009\009print( tItem.desc or (\"You see nothing special about \"..sItem..\".\") )\
- \009\009\009else\
- \009\009\009\009print( \"You don't see any \".._sTarget..\" here.\" )\
- \009\009\009end\
- \009\009end\
- \009end\
- end\
- \
- function commands.go( _sDir )\
- \009local room = getRoom( x,y,z )\
- \009if _sDir == nil then\
- \009\009print( \"Go where?\" )\
- \009\009return\
- \009end\
- \009\
- \009if nGoWest ~= nil then\
- \009\009if _sDir == \"west\" then\
- \009\009\009nGoWest = nGoWest + 1\
- \009\009\009if nGoWest > #tGoWest then\
- \009\009\009\009nGoWest = 1\
- \009\009\009end\
- \009\009\009print( tGoWest[ nGoWest ] )\
- \009\009else\
- \009\009\009if nGoWest > 0 or nTurn > 6 then\
- \009\009\009\009nGoWest = nil\
- \009\009\009end\
- \009\009end\
- \009end\
- \009\
- \009if room.exits[_sDir] == nil then\
- \009\009print( \"You can't go that way.\" )\
- \009\009return\
- \009end\
- \009\
- \009if _sDir == \"north\" then\
- \009\009z = z + 1\
- \009elseif _sDir == \"south\" then\
- \009\009z = z - 1\
- \009elseif _sDir == \"east\" then\
- \009\009x = x - 1\
- \009elseif _sDir == \"west\" then\
- \009\009x = x + 1\
- \009elseif _sDir == \"up\" then\
- \009\009y = y + 1\
- \009elseif _sDir == \"down\" then\
- \009\009y = y - 1\
- \009else\
- \009\009print( \"I don't understand that direction.\" )\
- \009\009return\
- \009end\
- \009\
- \009nTimeInRoom = 0\
- \009doCommand( \"look\" )\
- end\
- \
- function commands.dig( _sDir, _sTool )\
- \009local room = getRoom( x,y,z )\
- \009if _sDir == nil then\
- \009\009print( \"Dig where?\" )\
- \009\009return\
- \009end\
- \009\
- \009local sTool = nil\
- \009local tTool = nil\
- \009if _sTool ~= nil then\
- \009\009sTool = findItem( inventory, _sTool )\
- \009\009if not sTool then\
- \009\009\009print( \"You're not carrying a \".._sTool..\".\" )\
- \009\009\009return\
- \009\009end\
- \009\009tTool = inventory[ sTool ]\
- \009end\
- \009\
- \009local room = getRoom( x, y, z )\
- \009local bActuallyDigging = (room.exits[ _sDir ] ~= true)\
- \009if bActuallyDigging then\
- \009\009if sTool == nil or tTool.toolType ~= \"pick\" then\
- \009\009\009print( \"You need to use a pickaxe to dig through stone.\" )\
- \009\009\009return\
- \009\009end\
- \009end\
- \009\
- \009if _sDir == \"north\" then\
- \009\009room.exits[\"north\"] = true\
- \009\009z = z + 1\
- \009\009getRoom( x, y, z ).exits[\"south\"] = true\
- \
- \009elseif _sDir == \"south\" then\
- \009\009room.exits[\"south\"] = true\
- \009\009z = z - 1\
- \009\009getRoom( x, y, z ).exits[\"north\"] = true\
- \009\009\
- \009elseif _sDir == \"east\" then\
- \009\009room.exits[\"east\"] = true\
- \009\009x = x - 1\
- \009\009getRoom( x, y, z ).exits[\"west\"] = true\
- \009\009\
- \009elseif _sDir == \"west\" then\
- \009\009room.exits[\"west\"] = true\
- \009\009x = x + 1\
- \009\009getRoom( x, y, z ).exits[\"east\"] = true\
- \009\009\
- \009elseif _sDir == \"up\" then\
- \009\009if y == 0 then\
- \009\009\009print( \"You can't dig that way.\" )\
- \009\009\009return\
- \009\009end\
- \
- \009\009room.exits[\"up\"] = true\
- \009\009if y == -1 then\
- \009\009\009room.items[ \"an exit to the surface\" ] = items[ \"an exit to the surface\" ]\
- \009\009end\
- \009\009y = y + 1\
- \009\009\
- \009\009room = getRoom( x, y, z )\
- \009\009room.exits[\"down\"] = true\
- \009\009if y == 0 then\
- \009\009\009room.items[ \"a cave entrance\" ] = items[ \"a cave entrance\" ]\
- \009\009end\
- \009\009\
- \009elseif _sDir == \"down\" then\
- \009\009if y <= -3 then\
- \009\009\009print( \"You hit bedrock.\" )\
- \009\009\009return\
- \009\009end\
- \
- \009\009room.exits[\"down\"] = true\
- \009\009if y == 0 then\
- \009\009\009room.items[ \"a cave entrance\" ] = items[ \"a cave entrance\" ]\
- \009\009end\
- \009\009y = y - 1\
- \009\009\
- \009\009room = getRoom( x, y, z )\
- \009\009room.exits[\"up\"] = true\
- \009\009if y == -1 then\
- \009\009\009room.items[ \"an exit to the surface\" ] = items[ \"an exit to the surface\" ]\
- \009\009end\
- \009\009\
- \009else\
- \009\009print( \"I don't understand that direction.\" )\
- \009\009return\
- \009end\
- \009\
- \009--\
- \009if bActuallyDigging then\
- \009\009if _sDir == \"down\" and y == -1 or\
- \009\009 _sDir == \"up\" and y == 0 then\
- \009\009\009inventory[ \"some dirt\" ] = items[ \"some dirt\" ]\
- \009\009\009inventory[ \"some stone\" ] = items[ \"some stone\" ]\
- \009\009\009print( \"You dig \".._sDir..\" using \"..sTool..\" and collect some dirt and stone.\" )\
- \009\009else\
- \009\009\009inventory[ \"some stone\" ] = items[ \"some stone\" ]\
- \009\009\009print( \"You dig \".._sDir..\" using \"..sTool..\" and collect some stone.\" )\
- \009\009end\
- \009end\
- \009\
- \009nTimeInRoom = 0\
- \009doCommand( \"look\" )\
- end\
- \
- function commands.inventory()\
- \009print( \"You are carrying \" .. itemize( inventory ) .. \".\" )\
- end\
- \
- function commands.drop( _sItem )\
- \009if _sItem == nil then\
- \009\009print( \"Drop what?\" )\
- \009\009return\
- \009end\
- \009\
- \009local room = getRoom( x,y,z )\
- \009local sItem = findItem( inventory, _sItem )\
- \009if sItem then\
- \009\009local tItem = inventory[ sItem ]\
- \009\009if tItem.droppable == false then\
- \009\009\009print( \"You can't drop that.\" )\
- \009\009else\
- \009\009\009room.items[ sItem ] = tItem\
- \009\009\009inventory[ sItem ] = nil\
- \009\009\009print( \"Dropped.\" )\
- \009\009end\
- \009else\
- \009\009print( \"You don't have a \".._sItem..\".\" )\
- \009end\
- end\
- \
- function commands.place( _sItem )\
- \009if _sItem == nil then\
- \009\009print( \"Place what?\" )\
- \009\009return\
- \009end\
- \009\
- \009if _sItem == \"torch\" or _sItem == \"a torch\" then\
- \009\009local room = getRoom( x,y,z )\
- \009\009if inventory[\"some torches\"] or inventory[\"a torch\"] then\
- \009\009\009inventory[\"a torch\"] = nil\
- \009\009\009room.items[\"a torch\"] = items[\"a torch\"]\
- \009\009\009if room.dark then\
- \009\009\009\009print( \"The cave lights up under the torchflame.\" )\
- \009\009\009\009room.dark = false\
- \009\009\009elseif y == 0 and not isSunny() then\
- \009\009\009\009print( \"The night gets a little brighter.\" )\
- \009\009\009else\
- \009\009\009\009print( \"Placed.\" )\
- \009\009\009end\
- \009\009else\
- \009\009\009print( \"You don't have torches.\" )\
- \009\009end\
- \009\009return\
- \009end\
- \009\
- \009commands.drop( _sItem )\
- end\
- \
- function commands.take( _sItem )\
- \009if _sItem == nil then\
- \009\009print( \"Take what?\" )\
- \009\009return\
- \009end\
- \
- \009local room = getRoom( x,y,z )\
- \009local sItem = findItem( room.items, _sItem )\
- \009if sItem then\
- \009\009local tItem = room.items[ sItem ]\
- \009\009if tItem.heavy == true then\
- \009\009\009print( \"You can't carry \"..sItem..\".\" )\
- \009\009elseif tItem.ore == true then\
- \009\009\009print( \"You need to mine this ore.\" )\
- \009\009else\
- \009\009\009if tItem.infinite ~= true then\
- \009\009\009\009room.items[ sItem ] = nil\
- \009\009\009end\
- \009\009\009inventory[ sItem ] = tItem\
- \009\009\009\
- \009\009\009if inventory[\"some torches\"] and inventory[\"a torch\"] then\
- \009\009\009\009inventory[\"a torch\"] = nil\
- \009\009\009end\
- \009\009\009if sItem == \"a torch\" and y < 0 then\
- \009\009\009\009room.dark = true\
- \009\009\009\009print( \"The cave plunges into darkness.\" )\
- \009\009\009else\
- \009\009\009\009print( \"Taken.\" )\
- \009\009\009end\
- \009\009end\
- \009else\
- \009\009print( \"You don't see a \".._sItem..\" here.\" )\
- \009end\
- end\
- \
- function commands.mine( _sItem, _sTool )\
- \009if _sItem == nil then\
- \009\009print( \"Mine what?\" )\
- \009\009return\
- \009end\
- \009if _sTool == nil then\
- \009\009print( \"Mine \".._sItem..\" with what?\" )\
- \009\009return\
- \009end\009\
- \009commands.cbreak( _sItem, _sTool )\
- end\
- \
- function commands.attack( _sItem, _sTool )\
- \009if _sItem == nil then\
- \009\009print( \"Attack what?\" )\
- \009\009return\
- \009end\
- \009commands.cbreak( _sItem, _sTool )\
- end\
- \
- function commands.cbreak( _sItem, _sTool )\
- \009if _sItem == nil then\
- \009\009print( \"Break what?\" )\
- \009\009return\
- \009end\
- \009\
- \009local sTool = nil\
- \009if _sTool ~= nil then\
- \009\009sTool = findItem( inventory, _sTool )\
- \009\009if sTool == nil then\
- \009\009\009print( \"You're not carrying a \".._sTool..\".\" )\
- \009\009\009return\
- \009\009end\
- \009end\
- \
- \009local room = getRoom( x,y,z )\
- \009if _sItem == \"tree\" or _sItem == \"trees\" or _sItem == \"a tree\" then\
- \009\009print( \"The tree breaks into blocks of wood, which you pick up.\" )\
- \009\009inventory[ \"some wood\" ] = items[ \"some wood\" ]\
- \009\009return\
- \009elseif _sItem == \"self\" or _sItem == \"myself\" then\
- \009\009if term.isColour() then\
- \009\009\009term.setTextColour( colours.red )\
- \009\009end\
- \009\009print( \"You have died.\" )\
- \009\009print( \"Score: &e0\" )\
- \009\009term.setTextColour( colours.white )\
- \009\009bRunning = false\
- \009\009return\
- \009end\
- \009\
- \009local sItem = findItem( room.items, _sItem )\
- \009if sItem then\
- \009\009local tItem = room.items[ sItem ]\
- \009\009if tItem.ore == true then\
- \009\009\009-- Breaking ore\
- \009\009\009if not sTool then\
- \009\009\009\009print( \"You need a tool to break this ore.\" )\
- \009\009\009\009return\
- \009\009\009end\
- \009\009\009local tTool = inventory[ sTool ]\
- \009\009\009if tTool.tool then\
- \009\009\009\009if tTool.toolLevel < tItem.toolLevel then\
- \009\009\009\009\009print( sTool ..\" is not strong enough to break this ore.\" )\
- \009\009\009\009elseif tTool.toolType ~= tItem.toolType then\
- \009\009\009\009\009print( \"You need a different kind of tool to break this ore.\" )\
- \009\009\009\009else\
- \009\009\009\009\009print( \"The ore breaks, dropping \"..sItem..\", which you pick up.\" )\
- \009\009\009\009\009inventory[ sItem ] = items[ sItem ]\
- \009\009\009\009\009if tItem.infinite ~= true then\
- \009\009\009\009\009\009room.items[ sItem ] = nil\
- \009\009\009\009\009end\
- \009\009\009\009end\
- \009\009\009else\
- \009\009\009\009print( \"You can't break \"..sItem..\" with \"..sTool..\".\")\
- \009\009\009end\
- \009\009\009\
- \009\009elseif tItem.creature == true then\
- \009\009\009-- Fighting monsters (or pigs)\
- \009\009\009local toolLevel = 0\
- \009\009\009local tTool = nil\
- \009\009\009if sTool then\
- \009\009\009\009tTool = inventory[ sTool ]\
- \009\009\009\009if tTool.toolType == \"sword\" then\
- \009\009\009\009\009toolLevel = tTool.toolLevel\
- \009\009\009\009end\
- \009\009\009end\
- \009\009\009\009\009\009\
- \009\009\009local tChances = { 0.2, 0.4, 0.55, 0.8, 1 }\
- \009\009\009if math.random() <= tChances[ toolLevel + 1 ] then\
- \009\009\009\009room.items[ sItem ] = nil\
- \009\009\009\009print( \"The \"..tItem.aliases[1]..\" dies.\" )\
- \009\
- \009\009\009\009if tItem.drops then\
- \009\009\009\009\009for n, sDrop in pairs( tItem.drops ) do\
- \009\009\009\009\009\009if not room.items[sDrop] then\
- \009\009\009\009\009\009\009print( \"The \"..tItem.aliases[1]..\" dropped \"..sDrop..\".\" )\
- \009\009\009\009\009\009\009room.items[sDrop] = items[sDrop]\
- \009\009\009\009\009\009end\
- \009\009\009\009\009end\
- \009\009\009\009end\
- \009\009\009\009\
- \009\009\009\009if tItem.monster then\
- \009\009\009\009\009room.nMonsters = room.nMonsters - 1\
- \009\009\009\009end\
- \009\009\009else\
- \009\009\009\009print( \"The \"..tItem.aliases[1]..\" is injured by your blow.\" )\
- \009\009\009end\
- \009\009\009\
- \009\009\009if tItem.hitDrops then\
- \009\009\009\009for n, sDrop in pairs( tItem.hitDrops ) do\
- \009\009\009\009\009if not room.items[sDrop] then\
- \009\009\009\009\009\009print( \"The \"..tItem.aliases[1]..\" dropped \"..sDrop..\".\" )\
- \009\009\009\009\009\009room.items[sDrop] = items[sDrop]\
- \009\009\009\009\009end\
- \009\009\009\009end\
- \009\009\009end\
- \009\009\
- \009\009else\
- \009\009\009print( \"You can't break \"..sItem..\".\" )\
- \009\009end\
- \009else\
- \009\009print( \"You don't see a \".._sItem..\" here.\" )\
- \009end\
- end\
- \
- function commands.craft( _sItem )\
- \009if _sItem == nil then\
- \009\009print( \"Craft what?\" )\
- \009\009return\
- \009end\
- \009\
- \009if _sItem == \"computer\" or _sItem == \"a computer\" then\
- \009\009print( \"By creating a computer in a computer in a computer, you tear a hole in the spacetime continuum from which no mortal being can escape.\" )\
- \009\009if term.isColour() then\
- \009\009\009term.setTextColour( colours.red )\
- \009\009end\
- \009\009print( \"You have died.\" )\
- \009\009print( \"Score: &e0\" )\
- \009\009term.setTextColour( colours.white )\
- \009\009bRunning = false\
- \009\009return\
- \009end\
- \009\
- \009local room = getRoom( x,y,z )\
- \009local sItem = findItem( items, _sItem )\
- \009local tRecipe = (sItem and tRecipes[ sItem ]) or nil\
- \009if tRecipe then\
- \009\009for n,sReq in ipairs( tRecipe ) do\
- \009\009\009if inventory[sReq] == nil then\
- \009\009\009\009print( \"You don't have the items you need to craft \"..sItem..\".\" )\
- \009\009\009\009return\
- \009\009\009end\
- \009\009end\
- \009\009\
- \009\009for n,sReq in ipairs( tRecipe ) do\
- \009\009\009inventory[sReq] = nil\
- \009\009end\
- \009\009inventory[ sItem ] = items[ sItem ]\
- \009\009if inventory[\"some torches\"] and inventory[\"a torch\"] then\
- \009\009\009inventory[\"a torch\"] = nil\
- \009\009end\
- \009\009print( \"Crafted.\" )\
- \009else\
- \009\009print( \"You don't know how to make \"..(sItem or _sItem)..\".\" )\
- \009end\009\
- end\
- \
- function commands.build( _sThing, _sMaterial )\
- \009if _sThing == nil then\
- \009\009print( \"Build what?\" )\
- \009\009return\
- \009end\
- \009\009\
- \009local sMaterial = nil\
- \009if _sMaterial == nil then\
- \009\009for sItem, tItem in pairs( inventory ) do\
- \009\009\009if tItem.material then\
- \009\009\009\009sMaterial = sItem\
- \009\009\009\009break\
- \009\009\009end\
- \009\009end\
- \009\009if sMaterial == nil then\
- \009\009\009print( \"You don't have any building materials.\" )\
- \009\009\009return\
- \009\009end\
- \009else\
- \009\009sMaterial = findItem( inventory, _sMaterial )\
- \009\009if not sMaterial then\
- \009\009\009print( \"You don't have any \".._sMaterial )\
- \009\009\009return\
- \009\009end\
- \009\009\
- \009\009if inventory[sMaterial].material ~= true then\
- \009\009\009print( sMaterial..\" is not a good building material.\" )\
- \009\009\009return\
- \009\009end\
- \009end\
- \009\
- \009local alias = nil\
- \009if string.sub(_sThing, 1, 1) == \"a\" then\
- \009\009alias = string.match( _sThing, \"a ([%a ]+)\" )\
- \009end\
- \009\
- \009local room = getRoom( x,y,z )\
- \009inventory[sMaterial] = nil\
- \009room.items[ _sThing ] = {\
- \009\009heavy = true,\
- \009\009aliases = { alias },\
- \009\009desc = \"As you look at your creation (made from \"..sMaterial..\"), you feel a swelling sense of pride.\",\
- \009}\
- \
- \009print( \"Your construction is complete.\" )\
- end\
- \
- function commands.help()\
- \009local sText = \
- \009\009\"Welcome to adventure, the greatest text adventure game on CraftOS. \" ..\
- \009\009\"To get around the world, type actions, and the adventure will \" ..\
- \009\009\"be read back to you. The actions availiable to you are go, look, inspect, inventory, \" ..\
- \009\009\"take, drop, place, punch, attack, mine, dig, craft, build, eat and exit.\"\
- \009print( sText )\
- end\
- \
- function commands.eat( _sItem )\
- \009if _sItem == nil then\
- \009\009print( \"Eat what?\" )\
- \009\009return\
- \009end\
- \
- \009local sItem = findItem( inventory, _sItem )\
- \009if not sItem then\
- \009\009print( \"You don't have any \".._sItem..\".\" )\
- \009\009return\
- \009end\
- \009\
- \009local tItem = inventory[sItem]\
- \009if tItem.food then\
- \009\009print( \"That was delicious!\" )\
- \009\009inventory[sItem] = nil\
- \009\009\
- \009\009if bInjured then\
- \009\009\009print( \"You are no longer injured.\" )\
- \009\009\009bInjured = false\
- \009\009end\
- \009else\
- \009\009print( \"You can't eat \"..sItem..\".\" )\
- \009end\
- end\
- \
- function commands.exit()\
- \009bRunning = false\
- end\
- \
- function commands.badinput()\
- \009local tResponses = {\
- \009\009\"I don't understand.\",\
- \009\009\"I don't understand you.\",\
- \009\009\"You can't do that.\",\
- \009\009\"Nope.\",\
- \009\009\"Huh?\",\
- \009\009\"Say again?\",\
- \009\009\"That's crazy talk.\",\
- \009\009\"Speak clearly.\",\
- \009\009\"I'll think about it.\",\
- \009\009\"Let me get back to you on that one.\",\
- \009\009\"That doesn't make any sense.\",\
- \009\009\"What?\",\
- \009}\
- \009print( tResponses[ math.random(1,#tResponses) ] )\
- end\
- \
- function commands.noinput()\
- \009local tResponses = {\
- \009\009\"Speak up.\",\
- \009\009\"Enunciate.\",\
- \009\009\"Project your voice.\",\
- \009\009\"Don't be shy.\",\
- \009\009\"Use your words.\",\
- \009}\
- \009print( tResponses[ math.random(1,#tResponses) ] )\
- end\
- \
- local function simulate()\
- \009local bNewMonstersThisRoom = false\
- \009\
- \009-- Spawn monsters in nearby rooms\
- \009for sx = -2,2 do\
- \009\009for sy = -1,1 do\
- \009\009\009for sz = -2,2 do\
- \009\009\009\009local h = y + sy\
- \009\009\009\009if h >= -3 and h <= 0 then\
- \009\009\009\009\009local room = getRoom( x + sx, h, z + sz )\
- \009\009\009\009\009\
- \009\009\009\009\009-- Spawn monsters\
- \009\009\009\009\009if room.nMonsters < 2 and\
- \009\009\009\009\009 ((h == 0 and not isSunny() and not room.items[\"a torch\"]) or room.dark) and\
- \009\009\009\009\009 math.random(1,6) == 1 then\
- \009\009\009\009\009 \
- \009\009\009\009\009\009local sMonster = tMonsters[ math.random(1,#tMonsters) ]\
- \009\009\009\009\009\009if room.items[ sMonster ] == nil then\
- \009\009\009\009\009 \009\009room.items[ sMonster ] = items[ sMonster ]\
- \009\009\009\009\009 \009\009room.nMonsters = room.nMonsters + 1\
- \009\009\009\009\009 \009\009\
- \009\009\009\009\009 \009\009if sx == 0 and sy == 0 and sz == 0 and not room.dark then\
- \009\009\009\009\009 \009\009\009print( \"From the shadows, \"..sMonster..\" appears.\" )\
- \009\009\009\009\009 \009\009\009bNewMonstersThisRoom = true\
- \009\009\009\009\009 \009\009end\
- \009\009\009\009\009\009end\009\
- \009\009\009\009\009end\
- \009\009\009\009\009\
- \009\009\009\009\009-- Burn monsters\
- \009\009\009\009\009if h == 0 and isSunny() then\
- \009\009\009\009\009\009for n,sMonster in ipairs( tMonsters ) do\
- \009\009\009\009\009\009\009if room.items[sMonster] and items[sMonster].nocturnal then\
- \009\009\009\009\009\009\009\009room.items[sMonster] = nil\
- \009\009\009\009\009\009 \009\009if sx == 0 and sy == 0 and sz == 0 and not room.dark then\
- \009\009\009\009\009\009 \009\009\009print( \"With the sun high in the sky, the \"..items[sMonster].aliases[1]..\" bursts into flame and dies.\" )\
- \009\009\009\009\009\009 \009\009end\
- \009\009\009\009\009\009 \009\009room.nMonsters = room.nMonsters - 1\
- \009\009\009\009\009\009 \009end\
- \009\009\009\009\009\009end\
- \009\009\009\009\009end\009\
- \009\009\009\009end\
- \009\009\009end\
- \009\009end\
- \009end\
- \
- \009-- Make monsters attack\
- \009local room = getRoom( x, y, z )\
- \009if nTimeInRoom >= 2 and not bNewMonstersThisRoom then\
- \009\009for n,sMonster in ipairs( tMonsters ) do\
- \009\009\009if room.items[sMonster] then\
- \009\009\009\009if math.random(1,4) == 1 and\
- \009\009\009\009 not (y == 0 and isSunny() and (sMonster == \"a spider\")) then\
- \009\009\009\009\009if sMonster == \"a creeper\" then\
- \009\009\009\009\009\009if room.dark then\
- \009\009\009\009\009\009\009print( \"A creeper explodes.\" )\
- \009\009\009\009\009\009else\
- \009\009\009\009\009\009\009print( \"The creeper explodes.\" )\
- \009\009\009\009\009\009end\
- \009\009\009\009\009\009room.items[sMonster] = nil\
- \009\009\009\009\009\009room.nMonsters = room.nMonsters - 1\
- \009\009\009\009\009else\
- \009\009\009\009\009\009if room.dark then\
- \009\009\009\009\009\009\009print( \"A \"..items[sMonster].aliases[1]..\" attacks you.\" )\
- \009\009\009\009\009\009else\
- \009\009\009\009\009\009\009print( \"The \"..items[sMonster].aliases[1]..\" attacks you.\" )\
- \009\009\009\009\009\009end\
- \009\009\009\009\009end\
- \009\009\009\009\009\
- \009\009\009\009\009if bInjured then\
- \009\009\009\009\009\009if term.isColour() then\
- \009\009\009\009\009\009\009term.setTextColour( colours.red )\
- \009\009\009\009\009\009end\
- \009\009\009\009\009\009print( \"You have died.\" )\
- \009\009\009\009\009\009print( \"Score: &e0\" )\
- \009\009\009\009\009\009term.setTextColour( colours.white )\
- \009\009\009\009\009\009bRunning = false\
- \009\009\009\009\009\009return\
- \009\009\009\009\009else\
- \009\009\009\009\009\009bInjured = true\
- \009\009\009\009\009end\
- \009\009\009\009\009\
- \009\009\009\009\009break\
- \009\009\009\009end\
- \009\009\009end\
- \009\009end\
- \009end\
- \009\
- \009-- Always print this\
- \009if bInjured then\
- \009\009if term.isColour() then\
- \009\009\009term.setTextColour( colours.red )\
- \009\009end\
- \009\009print( \"You are injured.\" )\
- \009\009term.setTextColour( colours.white )\
- \009end\
- \009\
- \009-- Advance time\
- \009nTurn = nTurn + 1\
- \009nTimeInRoom = nTimeInRoom + 1\
- end\
- \
- doCommand( \"look\" )\
- simulate()\
- \
- local tCommandHistory = {}\
- while bRunning do\
- \009if term.isColour() then\
- \009\009term.setTextColour( colours.yellow )\
- \009end\
- write( \"? \" )\
- \009term.setTextColour( colours.white )\
- \009\009\
- local sRawLine = read( nil, tCommandHistory )\
- table.insert( tCommandHistory, sRawLine )\
- \
- local sLine = nil\
- \009for match in string.gmatch(sRawLine, \"%a+\") do\
- \009\009if sLine then\
- \009\009\009sLine = sLine .. \" \" .. string.lower(match)\
- \009\009else\
- \009\009\009sLine = string.lower(match)\
- \009\009end\
- \009end\
- \009\
- \009doCommand( sLine or \"\" )\
- if bRunning then\
- \009 simulate()\
- \009end\
- end",
- edit = "if OneOS then\
- OneOS.ToolBarColour=colours.grey\
- OneOS.ToolBarTextColour=colours.white\
- end\
- local l=io\
- if OneOS then\
- l=OneOS.IO\
- end\
- local c=fs\
- if OneOS then\
- c=OneOS.FS\
- end\
- local F=\"1.0\"\
- local G={...}\
- local n,T=term.getSize()\
- local e=2\
- local B=20\
- local E=true\
- local m=.4\
- local u=nil\
- local t={}\
- local s={}\
- local d={}\
- local S=\"luaide_distractionEvent\"\
- local M=\"https://raw.github.com/GravityScore/LuaIDE/master/luaide.lua\"\
- local C=\"/\"..shell.getRunningProgram()\
- local z=\"/.LuaIDE-Theme\"\
- local function b()return term.isColor and term.isColor()end\
- local function p(e)\
- local a,t=term.getSize()\
- local t={replaceChar=nil,history=nil,visibleLength=nil,textLength=nil,\
- liveUpdates=nil,exitOnKey=nil}\
- if not e then e={}end\
- for t,a in pairs(t)do if not e[t]then e[t]=a end end\
- if e.replaceChar then e.replaceChar=e.replaceChar:sub(1,1)end\
- if not e.visibleLength then e.visibleLength=a end\
- local s,h=term.getCursorPos()\
- local a=\"\"\
- local t=0\
- local i=nil\
- local function n(i)\
- local o=0\
- if e.visibleLength and s+t>e.visibleLength+1 then\
- o=(s+t)-(e.visibleLength+1)\
- end\
- term.setCursorPos(s,h)\
- local e=i or e.replaceChar\
- if e then term.write(string.rep(e,a:len()-o))\
- else term.write(a:sub(o+1,-1))end\
- term.setCursorPos(s+t-o,h)\
- end\
- local function s(t,...)\
- if type(e.liveUpdates)==\"function\"then\
- local i,o=term.getCursorPos()\
- local t,e=e.liveUpdates(a,t,...)\
- if t==true and e==nil then\
- term.setCursorBlink(false)\
- return a\
- elseif t==true and e~=nil then\
- term.setCursorBlink(false)\
- return e\
- end\
- term.setCursorPos(i,o)\
- end\
- end\
- term.setCursorBlink(true)\
- while true do\
- local h,o,r,l,d,u=os.pullEvent()\
- if h==\"char\"then\
- local s=false\
- if e.textLength and a:len()<e.textLength then s=true\
- elseif not e.textLength then s=true end\
- local i=true\
- if not e.grantPrint and e.refusePrint then\
- local t={}\
- if type(e.refusePrint)==\"table\"then\
- for a,e in pairs(e.refusePrint)do\
- table.insert(t,tostring(e):sub(1,1))\
- end\
- elseif type(e.refusePrint)==\"string\"then\
- for e in e.refusePrint:gmatch(\".\")do\
- table.insert(t,e)\
- end\
- end\
- for t,e in pairs(t)do if o==e then i=false end end\
- elseif e.grantPrint then\
- i=false\
- local t={}\
- if type(e.grantPrint)==\"table\"then\
- for a,e in pairs(e.grantPrint)do\
- table.insert(t,tostring(e):sub(1,1))\
- end\
- elseif type(e.grantPrint)==\"string\"then\
- for e in e.grantPrint:gmatch(\".\")do\
- table.insert(t,e)\
- end\
- end\
- for t,e in pairs(t)do if o==e then i=true end end\
- end\
- if s and i then\
- a=a:sub(1,t)..o..a:sub(t+1,-1)\
- t=t+1\
- n()\
- end\
- elseif h==\"key\"then\
- if o==keys.enter then break\
- elseif o==keys.left then if t>0 then t=t-1 n()end\
- elseif o==keys.right then if t<a:len()then t=t+1 n()end\
- elseif(o==keys.up or o==keys.down)and e.history then\
- n(\" \")\
- if o==keys.up then\
- if i==nil and#e.history>0 then\
- i=#e.history\
- elseif i>1 then\
- i=i-1\
- end\
- elseif o==keys.down then\
- if i==#e.history then i=nil\
- elseif i~=nil then i=i+1 end\
- end\
- if e.history and i then\
- a=e.history[i]\
- t=a:len()\
- else\
- a=\"\"\
- t=0\
- end\
- n()\
- local e=s(\"history\")\
- if e then return e end\
- elseif o==keys.backspace and t>0 then\
- n(\" \")\
- a=a:sub(1,t-1)..a:sub(t+1,-1)\
- t=t-1\
- n()\
- local e=s(\"delete\")\
- if e then return e end\
- elseif o==keys.home then\
- t=0\
- n()\
- elseif o==keys.delete and t<a:len()then\
- n(\" \")\
- a=a:sub(1,t)..a:sub(t+2,-1)\
- n()\
- local e=s(\"delete\")\
- if e then return e end\
- elseif o==keys[\"end\"]then\
- t=a:len()\
- n()\
- elseif e.exitOnKey then\
- if o==e.exitOnKey or(e.exitOnKey==\"control\"and\
- (o==29 or o==157))then\
- term.setCursorBlink(false)\
- return nil\
- end\
- end\
- end\
- local e=s(h,o,r,l,d,u)\
- if e then return e end\
- end\
- term.setCursorBlink(false)\
- if a~=nil then a=a:gsub(\"^%s*(.-)%s*$\",\"%1\")end\
- return a\
- end\
- local N={\
- background=\"gray\",\
- backgroundHighlight=\"lightGray\",\
- prompt=\"cyan\",\
- promptHighlight=\"lightBlue\",\
- err=\"red\",\
- errHighlight=\"pink\",\
- editorBackground=\"gray\",\
- editorLineHightlight=\"lightBlue\",\
- editorLineNumbers=\"gray\",\
- editorLineNumbersHighlight=\"lightGray\",\
- editorError=\"pink\",\
- editorErrorHighlight=\"red\",\
- textColor=\"white\",\
- conditional=\"yellow\",\
- constant=\"orange\",\
- [\"function\"]=\"magenta\",\
- string=\"red\",\
- comment=\"lime\"\
- }\
- local U={\
- background=\"black\",\
- backgroundHighlight=\"black\",\
- prompt=\"black\",\
- promptHighlight=\"black\",\
- err=\"black\",\
- errHighlight=\"black\",\
- editorBackground=\"black\",\
- editorLineHightlight=\"black\",\
- editorLineNumbers=\"black\",\
- editorLineNumbersHighlight=\"white\",\
- editorError=\"black\",\
- editorErrorHighlight=\"black\",\
- textColor=\"white\",\
- conditional=\"white\",\
- constant=\"white\",\
- [\"function\"]=\"white\",\
- string=\"white\",\
- comment=\"white\"\
- }\
- local D={\
- {\"Water (Default)\",\"https://raw.github.com/GravityScore/LuaIDE/master/themes/default.txt\"},\
- {\"Fire\",\"https://raw.github.com/GravityScore/LuaIDE/master/themes/fire.txt\"},\
- {\"Sublime Text 2\",\"https://raw.github.com/GravityScore/LuaIDE/master/themes/st2.txt\"},\
- {\"Midnight\",\"https://raw.github.com/GravityScore/LuaIDE/master/themes/midnight.txt\"},\
- {\"TheOriginalBIT\",\"https://raw.github.com/GravityScore/LuaIDE/master/themes/bit.txt\"},\
- {\"Superaxander\",\"https://raw.github.com/GravityScore/LuaIDE/master/themes/superaxander.txt\"},\
- {\"Forest\",\"https://raw.github.com/GravityScore/LuaIDE/master/themes/forest.txt\"},\
- {\"Night\",\"https://raw.github.com/GravityScore/LuaIDE/master/themes/night.txt\"},\
- {\"Original\",\"https://raw.github.com/GravityScore/LuaIDE/master/themes/original.txt\"},\
- }\
- local function L(e)\
- local t=io.open(e)\
- local e=t:read(\"*l\")\
- local a={}\
- while e~=nil do\
- local i,o=string.match(e,\"^(%a+)=(%a+)\")\
- if i and o then a[i]=o end\
- e=t:read(\"*l\")\
- end\
- t:close()\
- return a\
- end\
- if b()then t=N\
- else t=U end\
- local function f(e,t)\
- if type(e)==\"table\"then for t,e in pairs(e)do f(e)end\
- else\
- local o,a=term.getCursorPos()\
- local o,i=term.getSize()\
- term.setCursorPos(o/2-e:len()/2+(#e%2==0 and 1 or 0),t or a)\
- print(e)\
- end\
- end\
- local function w(e)\
- term.setTextColor(colors[t.textColor])\
- term.setBackgroundColor(colors[t.background])\
- term.clear()\
- term.setBackgroundColor(colors[t.backgroundHighlight])\
- for e=2,4 do term.setCursorPos(1,e)term.clearLine()end\
- term.setCursorPos(3,3)\
- term.write(e)\
- end\
- local function I(e,a)\
- local function i(i,o,i,t,a,i,i)\
- if b()and o==\"mouse_click\"and t>=n/2-e/2 and t<=n/2-e/2+10\
- and a>=13 and a<=15 then\
- return true,\"\"\
- end\
- end\
- if not a then a=\"\"end\
- term.setTextColor(colors[t.textColor])\
- term.setBackgroundColor(colors[t.promptHighlight])\
- for t=8,10 do\
- term.setCursorPos(n/2-e/2,t)\
- term.write(string.rep(\" \",e))\
- end\
- if b()then\
- term.setBackgroundColor(colors[t.errHighlight])\
- for t=13,15 do\
- term.setCursorPos(n/2-e/2+1,t)\
- term.write(string.rep(\" \",10))\
- end\
- term.setCursorPos(n/2-e/2+2,14)\
- term.write(\"> Cancel\")\
- end\
- term.setBackgroundColor(colors[t.promptHighlight])\
- term.setCursorPos(n/2-e/2+1,9)\
- term.write(\"> \"..a)\
- return p({visibleLength=n/2+e/2,liveUpdates=i})\
- end\
- local function q(a,i,h)\
- local function o(o)\
- for a,e in ipairs(a)do\
- if a==o then term.setBackgroundColor(e.highlight or colors[t.promptHighlight])\
- else term.setBackgroundColor(e.bg or colors[t.prompt])end\
- term.setTextColor(e.tc or colors[t.textColor])\
- for t=-1,1 do\
- term.setCursorPos(e[2],e[3]+t)\
- term.write(string.rep(\" \",e[1]:len()+4))\
- end\
- term.setCursorPos(e[2],e[3])\
- if a==o then\
- term.setBackgroundColor(e.highlight or colors[t.promptHighlight])\
- term.write(\" > \")\
- else term.write(\" - \")end\
- term.write(e[1]..\" \")\
- end\
- end\
- local r=i==\"horizontal\"and 203 or 200\
- local d=i==\"horizontal\"and 205 or 208\
- local e=1\
- o(e)\
- while true do\
- local t,i,n,s=os.pullEvent()\
- if t==\"key\"and i==28 then\
- return a[e][1]\
- elseif t==\"key\"and i==r and e>1 then\
- e=e-1\
- o(e)\
- elseif t==\"key\"and i==d and((err==true and e<#a-1)or(e<#a))then\
- e=e+1\
- o(e)\
- elseif h and t==\"key\"and i==203 and e>2 then\
- e=e-2\
- o(e)\
- elseif h and t==\"key\"and i==205 and e<3 then\
- e=e+2\
- o(e)\
- elseif t==\"mouse_click\"then\
- for t,e in ipairs(a)do\
- if n>=e[2]-1 and n<=e[2]+e[1]:len()+3 and s>=e[3]-1 and s<=e[3]+1 then\
- return a[t][1]\
- end\
- end\
- end\
- end\
- end\
- local function Y(r)\
- local function i(e,s,a)\
- for e,a in ipairs(e)do\
- local i=colors[t.prompt]\
- local o=colors[t.promptHighlight]\
- if a:find(\"Back\")or a:find(\"Return\")then\
- i=colors[t.err]\
- o=colors[t.errHighlight]\
- end\
- if e==s then term.setBackgroundColor(o)\
- else term.setBackgroundColor(i)end\
- term.setTextColor(colors[t.textColor])\
- for t=-1,1 do\
- term.setCursorPos(3,(e*4)+t+4)\
- term.write(string.rep(\" \",n-13))\
- end\
- term.setCursorPos(3,e*4+4)\
- if e==s then\
- term.setBackgroundColor(o)\
- term.write(\" > \")\
- else term.write(\" - \")end\
- term.write(a..\" \")\
- end\
- end\
- local function d(t,a,o)\
- local e={}\
- for o=1,o do\
- local t=t[o+a-1]\
- if t then table.insert(e,t)end\
- end\
- return e\
- end\
- local t=1\
- local e=1\
- local o=3\
- local a=d(r,e,o)\
- i(a,t,e)\
- while true do\
- local s,h,l,u=os.pullEvent()\
- if s==\"mouse_click\"then\
- for e,t in ipairs(a)do\
- if l>=3 and l<=n-11 and u>=e*4+3 and u<=e*4+5 then return t end\
- end\
- elseif s==\"key\"and h==200 then\
- if t>1 then\
- t=t-1\
- i(a,t,e)\
- elseif e>1 then\
- e=e-1\
- a=d(r,e,o)\
- i(a,t,e)\
- end\
- elseif s==\"key\"and h==208 then\
- if t<o then\
- t=t+1\
- i(a,t,e)\
- elseif e+o-1<#r then\
- e=e+1\
- a=d(r,e,o)\
- i(a,t,e)\
- end\
- elseif s==\"mouse_scroll\"then\
- os.queueEvent(\"key\",h==-1 and 200 or 208)\
- elseif s==\"key\"and h==28 then\
- return a[t]\
- end\
- end\
- end\
- function monitorKeyboardShortcuts()\
- local n,i=nil,nil\
- local o=false\
- local a=false\
- while true do\
- local t,e=os.pullEvent()\
- if t==\"key\"and(e==42 or e==52)then\
- a=true\
- i=os.startTimer(m)\
- elseif t==\"key\"and(e==29 or e==157 or e==219 or e==220)then\
- E=false\
- o=true\
- n=os.startTimer(m)\
- elseif t==\"key\"and o then\
- local t=nil\
- for t,o in pairs(keys)do\
- if o==e then\
- if a then os.queueEvent(\"shortcut\",\"ctrl shift\",t:lower())\
- else os.queueEvent(\"shortcut\",\"ctrl\",t:lower())end\
- sleep(.005)\
- E=true\
- end\
- end\
- if a then os.queueEvent(\"shortcut\",\"ctrl shift\",e)\
- else os.queueEvent(\"shortcut\",\"ctrl\",e)end\
- elseif t==\"timer\"and e==n then\
- E=true\
- o=false\
- elseif t==\"timer\"and e==i then\
- a=false\
- end\
- end\
- end\
- local function P(e,t)\
- for a=1,3 do\
- local e=http.get(e)\
- if e then\
- local a=e.readAll()\
- e.close()\
- if t then\
- local e=io.open(t,\"w\")\
- e:write(a)\
- e:close()\
- end\
- return true\
- end\
- end\
- return false\
- end\
- local function y(e,a)\
- local t=e:sub(1,e:len()-c.getName(e):len())\
- if not c.exists(t)then c.makeDir(t)end\
- if not c.isDir(e)and not c.isReadOnly(e)then\
- local t=\"\"\
- for a,e in pairs(a)do t=t..e..\"\\n\"end\
- local e=l.open(e,\"w\")\
- e:write(t)\
- e:close()\
- return true\
- else return false end\
- end\
- local function J(e)\
- if not c.exists(e)then\
- local t=e:sub(1,e:len()-c.getName(e):len())\
- if not c.exists(t)then c.makeDir(t)end\
- local e=l.open(e,\"w\")\
- e:write(\"\")\
- e:close()\
- end\
- local t={}\
- if c.exists(e)and not c.isDir(e)then\
- local e=l.open(e,\"r\")\
- if e then\
- local a=e:read(\"*l\")\
- while a do\
- table.insert(t,a)\
- a=e:read(\"*l\")\
- end\
- e:close()\
- end\
- else return nil end\
- if#t<1 then table.insert(t,\"\")end\
- return t\
- end\
- s.lua={}\
- s.brainfuck={}\
- s.none={}\
- s.lua.helpTips={\
- \"A function you tried to call doesn't exist.\",\
- \"You made a typo.\",\
- \"The index of an array is nil.\",\
- \"The wrong variable type was passed.\",\
- \"A function/variable doesn't exist.\",\
- \"You missed an 'end'.\",\
- \"You missed a 'then'.\",\
- \"You declared a variable incorrectly.\",\
- \"One of your variables is mysteriously nil.\"\
- }\
- s.lua.defaultHelpTips={\
- 2,5\
- }\
- s.lua.errors={\
- [\"Attempt to call nil.\"]={1,2},\
- [\"Attempt to index nil.\"]={3,2},\
- [\".+ expected, got .+\"]={4,2,9},\
- [\"'end' expected\"]={6,2},\
- [\"'then' expected\"]={7,2},\
- [\"'=' expected\"]={8,2}\
- }\
- s.lua.keywords={\
- [\"and\"]=\"conditional\",\
- [\"break\"]=\"conditional\",\
- [\"do\"]=\"conditional\",\
- [\"else\"]=\"conditional\",\
- [\"elseif\"]=\"conditional\",\
- [\"end\"]=\"conditional\",\
- [\"for\"]=\"conditional\",\
- [\"function\"]=\"conditional\",\
- [\"if\"]=\"conditional\",\
- [\"in\"]=\"conditional\",\
- [\"local\"]=\"conditional\",\
- [\"not\"]=\"conditional\",\
- [\"or\"]=\"conditional\",\
- [\"repeat\"]=\"conditional\",\
- [\"return\"]=\"conditional\",\
- [\"then\"]=\"conditional\",\
- [\"until\"]=\"conditional\",\
- [\"while\"]=\"conditional\",\
- [\"true\"]=\"constant\",\
- [\"false\"]=\"constant\",\
- [\"nil\"]=\"constant\",\
- [\"print\"]=\"function\",\
- [\"write\"]=\"function\",\
- [\"sleep\"]=\"function\",\
- [\"pairs\"]=\"function\",\
- [\"ipairs\"]=\"function\",\
- [\"loadstring\"]=\"function\",\
- [\"loadfile\"]=\"function\",\
- [\"dofile\"]=\"function\",\
- [\"rawset\"]=\"function\",\
- [\"rawget\"]=\"function\",\
- [\"setfenv\"]=\"function\",\
- [\"getfenv\"]=\"function\",\
- }\
- s.lua.parseError=function(e)\
- local t={filename=\"unknown\",line=-1,display=\"Unknown!\",err=\"\"}\
- if e and e~=\"\"then\
- t.err=e\
- if e:find(\":\")then\
- t.filename=e:sub(1,e:find(\":\")-1):gsub(\"^%s*(.-)%s*$\",\"%1\")\
- e=(e:sub(e:find(\":\")+1)..\"\"):gsub(\"^%s*(.-)%s*$\",\"%1\")\
- if e:find(\":\")then\
- t.line=e:sub(1,e:find(\":\")-1)\
- e=e:sub(e:find(\":\")+2):gsub(\"^%s*(.-)%s*$\",\"%1\")..\"\"\
- end\
- end\
- t.display=e:sub(1,1):upper()..e:sub(2,-1)..\".\"\
- end\
- return t\
- end\
- s.lua.getCompilerErrors=function(e)\
- e=\"local function ee65da6af1cb6f63fee9a081246f2fd92b36ef2(...)\\n\\n\"..e..\"\\n\\nend\"\
- local t,e=loadstring(e)\
- if not e then\
- local a,t=pcall(t)\
- if t then e=t end\
- end\
- if e then\
- local t=e:find(\"]\",1,true)\
- if t then e=\"string\"..e:sub(t+1,-1)end\
- local e=s.lua.parseError(e)\
- if tonumber(e.line)then e.line=tonumber(e.line)end\
- return e\
- else return s.lua.parseError(nil)end\
- end\
- s.lua.run=function(e,a)\
- local t,e=OneOS.LoadFile(e)\
- setfenv(t,getfenv())\
- if not e then\
- _,e=pcall(function()t(unpack(a))end)\
- end\
- return e\
- end\
- s.brainfuck.helpTips={\
- \"Well idk...\",\
- \"Isn't this the whole point of the language?\",\
- \"Ya know... Not being able to debug it?\",\
- \"You made a typo.\"\
- }\
- s.brainfuck.defaultHelpTips={\
- 1,2,3\
- }\
- s.brainfuck.errors={\
- [\"No matching '['\"]={1,2,3,4}\
- }\
- s.brainfuck.keywords={}\
- s.brainfuck.parseError=function(e)\
- local t={filename=\"unknown\",line=-1,display=\"Unknown!\",err=\"\"}\
- if e and e~=\"\"then\
- t.err=e\
- t.line=e:sub(1,e:find(\":\")-1)\
- e=e:sub(e:find(\":\")+2):gsub(\"^%s*(.-)%s*$\",\"%1\")..\"\"\
- t.display=e:sub(1,1):upper()..e:sub(2,-1)..\".\"\
- end\
- return t\
- end\
- s.brainfuck.mapLoops=function(o)\
- local t={}\
- local e=1\
- local a=1\
- for o in string.gmatch(o,\".\")do\
- if o==\"[\"then\
- t[e]=true\
- elseif o==\"]\"then\
- local o=false\
- for a=e,1,-1 do\
- if t[a]==true then\
- t[a]=e\
- o=true\
- end\
- end\
- if not o then\
- return a..\": No matching '['\"\
- end\
- end\
- if o==\"\\n\"then a=a+1 end\
- e=e+1\
- end\
- return t\
- end\
- s.brainfuck.getCompilerErrors=function(e)\
- local e=s.brainfuck.mapLoops(e)\
- if type(e)==\"string\"then return s.brainfuck.parseError(e)\
- else return s.brainfuck.parseError(nil)end\
- end\
- s.brainfuck.run=function(e)\
- local e=OneOS.IO.open(e,\"r\")\
- local h=e:read(\"*a\")\
- e:close()\
- local t={}\
- local e=1\
- local a=1\
- local i=s.brainfuck.mapLoops(h)\
- if type(i)==\"string\"then return i end\
- while true do\
- local o=h:sub(a,a)\
- if o==\">\"then\
- e=e+1\
- if not t[tostring(e)]then t[tostring(e)]=0 end\
- elseif o==\"<\"then\
- if not t[tostring(e)]then t[tostring(e)]=0 end\
- e=e-1\
- if not t[tostring(e)]then t[tostring(e)]=0 end\
- elseif o==\"+\"then\
- if not t[tostring(e)]then t[tostring(e)]=0 end\
- t[tostring(e)]=t[tostring(e)]+1\
- elseif o==\"-\"then\
- if not t[tostring(e)]then t[tostring(e)]=0 end\
- t[tostring(e)]=t[tostring(e)]-1\
- elseif o==\".\"then\
- if not t[tostring(e)]then t[tostring(e)]=0 end\
- if term.getCursorPos()>=n then print(\"\")end\
- write(string.char(math.max(1,t[tostring(e)])))\
- elseif o==\",\"then\
- if not t[tostring(e)]then t[tostring(e)]=0 end\
- term.setCursorBlink(true)\
- local o,a=os.pullEvent(\"char\")\
- term.setCursorBlink(false)\
- t[tostring(e)]=string.byte(a)\
- if term.getCursorPos()>=n then print(\"\")end\
- write(a)\
- elseif o==\"/\"then\
- if not t[tostring(e)]then t[tostring(e)]=0 end\
- if term.getCursorPos()>=n then print(\"\")end\
- write(t[tostring(e)])\
- elseif o==\"[\"then\
- if t[tostring(e)]==0 then\
- for e,t in pairs(i)do\
- if e==a then a=t end\
- end\
- end\
- elseif o==\"]\"then\
- for e,t in pairs(i)do\
- if t==a then a=e-1 end\
- end\
- end\
- a=a+1\
- if a>h:len()then print(\"\")break end\
- end\
- end\
- s.none.helpTips={}\
- s.none.defaultHelpTips={}\
- s.none.errors={}\
- s.none.keywords={}\
- s.none.parseError=function(e)\
- return{filename=\"\",line=-1,display=\"\",err=\"\"}\
- end\
- s.none.getCompilerErrors=function(e)\
- return s.none.parseError(nil)\
- end\
- s.none.run=function(e)end\
- d=s.lua\
- local function h(a)\
- w(\"LuaIDE - Error Help\")\
- local e=nil\
- for o,t in pairs(d.errors)do\
- if a.display:find(o)then e=t break end\
- end\
- term.setBackgroundColor(colors[t.err])\
- for e=6,8 do\
- term.setCursorPos(5,e)\
- term.write(string.rep(\" \",35))\
- end\
- term.setBackgroundColor(colors[t.prompt])\
- for e=10,18 do\
- term.setCursorPos(5,e)\
- term.write(string.rep(\" \",46))\
- end\
- if e then\
- term.setBackgroundColor(colors[t.err])\
- term.setCursorPos(6,7)\
- term.write(\"Error Help\")\
- term.setBackgroundColor(colors[t.prompt])\
- for t,e in ipairs(e)do\
- term.setCursorPos(7,t+10)\
- term.write(\"- \"..d.helpTips[e])\
- end\
- else\
- term.setBackgroundColor(colors[t.err])\
- term.setCursorPos(6,7)\
- term.write(\"No Error Tips Available!\")\
- term.setBackgroundColor(colors[t.prompt])\
- term.setCursorPos(6,11)\
- term.write(\"There are no error tips available, but\")\
- term.setCursorPos(6,12)\
- term.write(\"you could see if it was any of these:\")\
- for t,e in ipairs(d.defaultHelpTips)do\
- term.setCursorPos(7,t+12)\
- term.write(\"- \"..d.helpTips[e])\
- end\
- end\
- q({{\"Back\",n-8,7}},\"horizontal\")\
- end\
- local function m(a,i,o)\
- local e={}\
- if o then\
- w(\"LuaIDE - Run \"..fs.getName(a))\
- local t=I(n-13,fs.getName(a)..\" \")\
- for t in string.gmatch(t,\"[^ \\t]+\")do e[#e+1]=t:gsub(\"^%s*(.-)%s*$\",\"%1\")end\
- end\
- y(a,i)\
- term.setCursorBlink(false)\
- term.setBackgroundColor(colors.black)\
- term.setTextColor(colors.white)\
- term.clear()\
- term.setCursorPos(1,1)\
- local e=d.run(a,e)\
- term.setBackgroundColor(colors.black)\
- print(\"\\n\")\
- if e then\
- if b()then term.setTextColor(colors.red)end\
- f(\"The program has crashed!\")\
- end\
- term.setTextColor(colors.white)\
- f(\"Press any key to return to LuaIDE...\")\
- while true do\
- local e=os.pullEvent()\
- if e==\"key\"then break end\
- end\
- os.queueEvent(S)\
- os.pullEvent()\
- if e then\
- if d==s.lua and e:find(\"]\")then\
- e=fs.getName(a)..e:sub(e:find(\"]\",1,true)+1,-1)\
- end\
- while true do\
- w(\"LuaIDE - Error!\")\
- term.setBackgroundColor(colors[t.err])\
- for e=6,8 do\
- term.setCursorPos(3,e)\
- term.write(string.rep(\" \",n-5))\
- end\
- term.setCursorPos(4,7)\
- term.write(\"The program has crashed!\")\
- term.setBackgroundColor(colors[t.prompt])\
- for e=10,14 do\
- term.setCursorPos(3,e)\
- term.write(string.rep(\" \",n-5))\
- end\
- local a=d.parseError(e)\
- term.setCursorPos(4,11)\
- term.write(\"Line: \"..a.line)\
- term.setCursorPos(4,12)\
- term.write(\"Error:\")\
- term.setCursorPos(5,13)\
- local e=a.display\
- local o=nil\
- if e:len()>n-8 then\
- for t=e:len(),1,-1 do\
- if e:sub(t,t)==\" \"then\
- o=e:sub(t+1,-1)\
- e=e:sub(1,t)\
- break\
- end\
- end\
- end\
- term.write(e)\
- if o then\
- term.setCursorPos(5,14)\
- term.write(o)\
- end\
- local e=q({{\"Error Help\",n/2-15,17},{\"Go To Line\",n/2+2,17}},\
- \"horizontal\")\
- if e==\"Error Help\"then\
- h(a)\
- elseif e==\"Go To Line\"then\
- os.queueEvent(S)\
- os.pullEvent()\
- return\"go to\",tonumber(a.line)\
- end\
- end\
- end\
- end\
- local function j()\
- term.setBackgroundColor(colors[t.backgroundHighlight])\
- term.setCursorPos(2,1)\
- term.clearLine()\
- term.write(\"Line: \")\
- local e=p({visibleLength=n-2})\
- local e=tonumber(e)\
- if e and e>0 then return e\
- else\
- term.setCursorPos(2,1)\
- term.clearLine()\
- term.write(\"Not a line number!\")\
- sleep(1.6)\
- return nil\
- end\
- end\
- local function k()\
- local a={\
- \"[Lua] Brainfuck None \",\
- \" Lua [Brainfuck] None \",\
- \" Lua Brainfuck [None]\"\
- }\
- local e=1\
- term.setCursorBlink(false)\
- term.setBackgroundColor(colors[t.backgroundHighlight])\
- term.setCursorPos(2,1)\
- term.clearLine()\
- term.write(a[e])\
- while true do\
- local o,t,o,o=os.pullEvent(\"key\")\
- if t==203 then\
- e=math.max(1,e-1)\
- term.setCursorPos(2,1)\
- term.clearLine()\
- term.write(a[e])\
- elseif t==205 then\
- e=math.min(#a,e+1)\
- term.setCursorPos(2,1)\
- term.clearLine()\
- term.write(a[e])\
- elseif t==28 then\
- if e==1 then d=s.lua\
- elseif e==2 then d=s.brainfuck\
- elseif e==3 then d=s.none end\
- term.setCursorBlink(true)\
- return\
- end\
- end\
- end\
- local W=2\
- local o={}\
- local a={}\
- local g={\
- \"if%s+.+%s+then%s*$\",\
- \"for%s+.+%s+do%s*$\",\
- \"while%s+.+%s+do%s*$\",\
- \"repeat%s*$\",\
- \"function%s+[a-zA-Z_0-9]\\(.*\\)%s*$\"\
- }\
- local v={\
- \"end\",\
- \"until%s+.+\"\
- }\
- local p={\
- \"else%s*$\",\
- \"elseif%s+.+%s+then%s*$\"\
- }\
- local function i(e)\
- for t,e in pairs(e)do\
- local t=e[\"lineStart\"]\
- local a=e[\"lineEnd\"]\
- local o=e[\"charStart\"]\
- local e=e[\"charEnd\"]\
- if line>=t and line<=a then\
- if line==t then return o<charNumb\
- elseif line==a then return e>charNumb\
- else return true end\
- end\
- end\
- end\
- local function r(e,e)\
- if i(o)then return true end\
- if i(a)then return true end\
- return false\
- end\
- local function l(i,t,e,a)\
- o[#o+1]={}\
- o[#o].lineStart=i\
- o[#o].lineEnd=t\
- o[#o].charStart=e\
- o[#o].charEnd=a\
- end\
- local function h(e,t,o,i)\
- a[#a+1]={}\
- a[#a].lineStart=e\
- a[#a].lineEnd=t\
- a[#a].charStart=o\
- a[#a].charEnd=i\
- end\
- local function e(e)\
- local i=false\
- local t=false\
- for e=1,#e do\
- if content[e]:find(\"%-%-%[%[\")and not t and not i then\
- local t=content[e]:find(\"%-%-%[%[\")\
- l(e,nil,t,nil)\
- i=true\
- elseif content[e]:find(\"%-%-%[=%[\")and not t and not i then\
- local t=content[e]:find(\"%-%-%[=%[\")\
- l(e,nil,t,nil)\
- i=true\
- elseif content[e]:find(\"%[%[\")and not t and not i then\
- local a=content[e]:find(\"%[%[\")\
- h(e,nil,a,nil)\
- t=true\
- elseif content[e]:find(\"%[=%[\")and not t and not i then\
- local a=content[e]:find(\"%[=%[\")\
- h(e,nil,a,nil)\
- t=true\
- end\
- if content[e]:find(\"%]%]\")and t and not i then\
- local i,o=content[e]:find(\"%]%]\")\
- a[#a].lineEnd=e\
- a[#a].charEnd=o\
- t=false\
- elseif content[e]:find(\"%]=%]\")and t and not i then\
- local i,o=content[e]:find(\"%]=%]\")\
- a[#a].lineEnd=e\
- a[#a].charEnd=o\
- t=false\
- end\
- if content[e]:find(\"%]%]\")and not t and i then\
- local a,t=content[e]:find(\"%]%]\")\
- o[#o].lineEnd=e\
- o[#o].charEnd=t\
- i=false\
- elseif content[e]:find(\"%]=%]\")and not t and i then\
- local a,t=content[e]:find(\"%]=%]\")\
- o[#o].lineEnd=e\
- o[#o].charEnd=t\
- i=false\
- end\
- if content[e]:find(\"%-%-\")and not t and not i then\
- local t=content[e]:find(\"%-%-\")\
- l(e,e,t,-1)\
- elseif content[e]:find(\"'\")and not t and not i then\
- local a,t=content[e]:find(\"'\")\
- local t=content[e]:sub(t+1,string.len(content[e]))\
- local o,t=t:find(\"'\")\
- h(e,e,a,t)\
- elseif content[e]:find('\"')and not t and not i then\
- local a,t=content[e]:find('\"')\
- local t=content[e]:sub(t+1,string.len(content[e]))\
- local o,t=t:find('\"')\
- h(e,e,a,t)\
- end\
- end\
- end\
- local function h(a)\
- local e=nil\
- if d~=s.lua then\
- e=\"Cannot indent languages other than Lua!\"\
- elseif d.getCompilerErrors(table.concat(a,\"\\n\")).line~=-1 then\
- e=\"Cannot indent a program with errors!\"\
- end\
- if e then\
- term.setCursorBlink(false)\
- term.setCursorPos(2,1)\
- term.setBackgroundColor(colors[t.backgroundHighlight])\
- term.clearLine()\
- term.write(e)\
- sleep(1.6)\
- return a\
- end\
- local s={}\
- local t=0\
- for a,e in pairs(a)do\
- local o=false\
- local i=false\
- for n,t in pairs(g)do\
- if e:find(t)and not r(a,e:find(t))then\
- o=true\
- end\
- if e:find(t:sub(1,-2))and not r(a,e:find(t))then\
- i=true\
- end\
- end\
- local n=false\
- if not o then\
- for s,o in pairs(v)do\
- if e:find(o)and not r(a,e:find(o))and not i then\
- t=math.max(0,t-1)\
- n=true\
- end\
- end\
- end\
- if not n then\
- for n,i in pairs(p)do\
- if e:find(i)and not r(a,e:find(i))then\
- o=true\
- t=math.max(0,t-1)\
- end\
- end\
- end\
- s[a]=string.rep(\" \",t*W)..e\
- if o then t=t+1 end\
- end\
- return s\
- end\
- local _={\
- [1]={\"File\",\
- \"New File ^+N\",\
- \"Open File ^+O\",\
- \"Save File ^+S\",\
- \"Close ^+W\",\
- \"Print ^+P\",\
- \"Quit ^+Q\"\
- },[2]={\"Edit\",\
- \"Cut Line ^+X\",\
- \"Copy Line ^+C\",\
- \"Paste Line ^+V\",\
- \"Delete Line\",\
- \"Clear Line\"\
- },[3]={\"Functions\",\
- \"Go To Line ^+G\",\
- \"Re-Indent ^+I\",\
- \"Set Syntax ^+E\",\
- \"Start of Line ^+<\",\
- \"End of Line ^+>\"\
- },[4]={\"Run\",\
- \"Run Program ^+R\",\
- \"Run w/ Args ^+Shift+R\"\
- }\
- }\
- local K={\
- [\"ctrl n\"]=\"New File ^+N\",\
- [\"ctrl o\"]=\"Open File ^+O\",\
- [\"ctrl s\"]=\"Save File ^+S\",\
- [\"ctrl w\"]=\"Close ^+W\",\
- [\"ctrl p\"]=\"Print ^+P\",\
- [\"ctrl q\"]=\"Quit ^+Q\",\
- [\"ctrl x\"]=\"Cut Line ^+X\",\
- [\"ctrl c\"]=\"Copy Line ^+C\",\
- [\"ctrl v\"]=\"Paste Line ^+V\",\
- [\"ctrl g\"]=\"Go To Line ^+G\",\
- [\"ctrl i\"]=\"Re-Indent ^+I\",\
- [\"ctrl e\"]=\"Set Syntax ^+E\",\
- [\"ctrl 203\"]=\"Start of Line ^+<\",\
- [\"ctrl 205\"]=\"End of Line ^+>\",\
- [\"ctrl r\"]=\"Run Program ^+R\",\
- [\"ctrl shift r\"]=\"Run w/ Args ^+Shift+R\"\
- }\
- local H={\
- [\"New File ^+N\"]=function(t,e)y(t,e)return\"new\"end,\
- [\"Open File ^+O\"]=function(e,t)y(e,t)return\"open\"end,\
- [\"Save File ^+S\"]=function(e,t)y(e,t)end,\
- [\"Close ^+W\"]=function(e,t)y(e,t)return\"menu\"end,\
- [\"Print ^+P\"]=function(e,t)y(e,t)return nil end,\
- [\"Quit ^+Q\"]=function(e,t)y(e,t)return\"exit\"end,\
- [\"Cut Line ^+X\"]=function(a,e,t)\
- u=e[t]table.remove(e,t)return nil,e end,\
- [\"Copy Line ^+C\"]=function(a,t,e)u=t[e]end,\
- [\"Paste Line ^+V\"]=function(a,e,t)\
- if u then table.insert(e,t,u)end return nil,e end,\
- [\"Delete Line\"]=function(a,e,t)table.remove(e,t)return nil,e end,\
- [\"Clear Line\"]=function(a,e,t)e[t]=\"\"return nil,e,\"cursor\"end,\
- [\"Go To Line ^+G\"]=function()return nil,\"go to\",j()end,\
- [\"Re-Indent ^+I\"]=function(t,e)\
- local a=h(e)y(t,e)return nil,a\
- end,\
- [\"Set Syntax ^+E\"]=function(t,e)\
- k()\
- if d==s.brainfuck and e[1]~=\"-- Syntax: Brainfuck\"then\
- table.insert(e,1,\"-- Syntax: Brainfuck\")\
- return nil,e\
- end\
- end,\
- [\"Start of Line ^+<\"]=function()os.queueEvent(\"key\",199)end,\
- [\"End of Line ^+>\"]=function()os.queueEvent(\"key\",207)end,\
- [\"Run Program ^+R\"]=function(t,e)\
- y(t,e)\
- return nil,m(t,e,false)\
- end,\
- [\"Run w/ Args ^+Shift+R\"]=function(t,e)\
- y(t,e)\
- return nil,m(t,e,true)\
- end,\
- }\
- local function A(a)\
- term.setCursorPos(1,1)\
- term.setTextColor(colors[t.textColor])\
- term.setBackgroundColor(colors[t.backgroundHighlight])\
- term.clearLine()\
- local e=0\
- for a,t in pairs(_)do\
- term.setCursorPos(3+e,1)\
- term.write(t[1])\
- e=e+t[1]:len()+3\
- end\
- if a then\
- local o={}\
- local e=1\
- for i,t in pairs(_)do\
- if a==t[1]then\
- o=t\
- break\
- end\
- e=e+t[1]:len()+3\
- end\
- e=e+1\
- local a={}\
- for e=2,#o do\
- table.insert(a,o[e])\
- end\
- local t=1\
- for a,e in pairs(a)do if e:len()+2>t then t=e:len()+2 end end\
- for a,o in ipairs(a)do\
- term.setCursorPos(e,a+1)\
- term.write(string.rep(\" \",t))\
- term.setCursorPos(e+1,a+1)\
- term.write(o)\
- end\
- term.setCursorPos(e,#a+2)\
- term.write(string.rep(\" \",t))\
- return a,t\
- end\
- end\
- local function Q(o,e)\
- local e=0\
- local a=nil\
- for i,t in pairs(_)do\
- if o>=e+3 and o<=e+t[1]:len()+2 then\
- a=t[1]\
- break\
- end\
- e=e+t[1]:len()+3\
- end\
- local e=e+2\
- if not a then return false end\
- term.setCursorBlink(false)\
- term.setCursorPos(e,1)\
- term.setBackgroundColor(colors[t.background])\
- term.write(string.rep(\" \",a:len()+2))\
- term.setCursorPos(e+1,1)\
- term.write(a)\
- sleep(.1)\
- local h,s=A(a)\
- local n=true\
- local l,d=term.getCursorPos()\
- while type(n)~=\"string\"do\
- local r,d,i,o=os.pullEvent()\
- if r==\"mouse_click\"then\
- if i<e-1 or i>e+s-1 then break\
- elseif o>#h+2 then break\
- elseif o==1 then break end\
- for r,h in ipairs(h)do\
- if o==r+1 and i>=e and i<=e+s-2 then\
- term.setCursorPos(e,o)\
- term.setBackgroundColor(colors[t.background])\
- term.write(string.rep(\" \",s))\
- term.setCursorPos(e+1,o)\
- term.write(h)\
- sleep(.1)\
- A(a)\
- n=h\
- break\
- end\
- end\
- end\
- end\
- term.setCursorPos(l,d)\
- term.setCursorBlink(true)\
- return n\
- end\
- local V={\
- \"if%s+.+%s+then%s*$\",\
- \"for%s+.+%s+do%s*$\",\
- \"while%s+.+%s+do%s*$\",\
- \"repeat%s*$\",\
- \"function%s+[a-zA-Z_0-9]?\\(.*\\)%s*$\",\
- \"=%s*function%s*\\(.*\\)%s*$\",\
- \"else%s*$\",\
- \"elseif%s+.+%s+then%s*$\"\
- }\
- local O={\
- [\"(\"]=\")\",\
- [\"{\"]=\"}\",\
- [\"[\"]=\"]\",\
- [\"\\\"\"]=\"\\\"\",\
- [\"'\"]=\"'\",\
- }\
- local a,e=0,0\
- local x,g=0,T-1\
- local l,h=0,1\
- local m,i=0,0\
- local o={}\
- local r=d.parseError(nil)\
- local v=true\
- local j=os.clock()\
- local function u(o,e,a)\
- local e=string.match(o,e)\
- if e then\
- if type(a)==\"number\"then term.setTextColor(a)\
- elseif type(a)==\"function\"then term.setTextColor(a(e))end\
- term.write(e)\
- term.setTextColor(colors[t.textColor])\
- return o:sub(e:len()+1,-1)\
- end\
- return nil\
- end\
- local function R(e)\
- if d==s.lua then\
- while e:len()>0 do\
- e=u(e,\"^%-%-%[%[.-%]%]\",colors[t.comment])or\
- u(e,\"^%-%-.*\",colors[t.comment])or\
- u(e,\"^\\\".*[^\\\\]\\\"\",colors[t.string])or\
- u(e,\"^\\'.*[^\\\\]\\'\",colors[t.string])or\
- u(e,\"^%[%[.-%]%]\",colors[t.string])or\
- u(e,\"^[%w_]+\",function(e)\
- if d.keywords[e]then\
- return colors[t[d.keywords[e]]]\
- end\
- return colors[t.textColor]\
- end)or\
- u(e,\"^[^%w_]\",colors[t.textColor])\
- end\
- else term.write(e)end\
- end\
- local function k()\
- term.setTextColor(colors[t.textColor])\
- term.setBackgroundColor(colors[t.editorBackground])\
- term.clear()\
- A()\
- l,h=tostring(#o):len()+1,1\
- x,g=n-l,T-1\
- for a=1,g do\
- local n=o[i+a]\
- if n then\
- local o=string.rep(\" \",l-1-tostring(i+a):len())..tostring(i+a)\
- local s=n:sub(m+1,x+m+1)\
- o=o..\":\"\
- if r.line==i+a then o=string.rep(\" \",l-2)..\"!:\"end\
- term.setCursorPos(1,a+h)\
- term.setBackgroundColor(colors[t.editorBackground])\
- if i+a==e then\
- if i+a==r.line and os.clock()-j>3 then\
- term.setBackgroundColor(colors[t.editorErrorHighlight])\
- else term.setBackgroundColor(colors[t.editorLineHightlight])end\
- term.clearLine()\
- elseif i+a==r.line then\
- term.setBackgroundColor(colors[t.editorError])\
- term.clearLine()\
- end\
- term.setCursorPos(1-m+l,a+h)\
- if i+a==e then\
- if i+a==r.line and os.clock()-j>3 then\
- term.setBackgroundColor(colors[t.editorErrorHighlight])\
- else term.setBackgroundColor(colors[t.editorLineHightlight])end\
- elseif i+a==r.line then term.setBackgroundColor(colors[t.editorError])\
- else term.setBackgroundColor(colors[t.editorBackground])end\
- if i+a==r.line then\
- if v then term.write(n)\
- else term.write(r.display)end\
- else R(n)end\
- term.setCursorPos(1,a+h)\
- if i+a==e then\
- if i+a==r.line and os.clock()-j>3 then\
- term.setBackgroundColor(colors[t.editorError])\
- else term.setBackgroundColor(colors[t.editorLineNumbersHighlight])end\
- elseif i+a==r.line then\
- term.setBackgroundColor(colors[t.editorErrorHighlight])\
- else term.setBackgroundColor(colors[t.editorLineNumbers])end\
- term.write(o)\
- end\
- end\
- term.setCursorPos(a-m+l,e-i+h)\
- end\
- local function p(...)\
- local n={...}\
- l=tostring(#o):len()+1\
- for n,a in pairs(n)do\
- local o=o[a]\
- if o then\
- local n=string.rep(\" \",l-1-tostring(a):len())..tostring(a)\
- local s=o:sub(m+1,x+m+1)\
- n=n..\":\"\
- if r.line==a then n=string.rep(\" \",l-2)..\"!:\"end\
- term.setCursorPos(1,(a-i)+h)\
- term.setBackgroundColor(colors[t.editorBackground])\
- if a==e then\
- if a==r.line and os.clock()-j>3 then\
- term.setBackgroundColor(colors[t.editorErrorHighlight])\
- else term.setBackgroundColor(colors[t.editorLineHightlight])end\
- elseif a==r.line then\
- term.setBackgroundColor(colors[t.editorError])\
- end\
- term.clearLine()\
- term.setCursorPos(1-m+l,(a-i)+h)\
- if a==e then\
- if a==r.line and os.clock()-j>3 then\
- term.setBackgroundColor(colors[t.editorErrorHighlight])\
- else term.setBackgroundColor(colors[t.editorLineHightlight])end\
- elseif a==r.line then term.setBackgroundColor(colors[t.editorError])\
- else term.setBackgroundColor(colors[t.editorBackground])end\
- if a==r.line then\
- if v then term.write(o)\
- else term.write(r.display)end\
- else R(o)end\
- term.setCursorPos(1,(a-i)+h)\
- if a==e then\
- if a==r.line and os.clock()-j>3 then\
- term.setBackgroundColor(colors[t.editorError])\
- else term.setBackgroundColor(colors[t.editorLineNumbersHighlight])end\
- elseif a==r.line then\
- term.setBackgroundColor(colors[t.editorErrorHighlight])\
- else term.setBackgroundColor(colors[t.editorLineNumbers])end\
- term.write(n)\
- end\
- end\
- term.setCursorPos(a-m+l,e-i+h)\
- end\
- local function u(n,o,s)\
- local a,t=n-m,o-i\
- local e=false\
- if a<1 then\
- m=n-1\
- a=1\
- e=true\
- elseif a>x then\
- m=n-x\
- a=x\
- e=true\
- end if t<1 then\
- i=o-1\
- t=1\
- e=true\
- elseif t>g then\
- i=o-g\
- t=g\
- e=true\
- end if e or s then k()end\
- term.setCursorPos(a+l,t+h)\
- end\
- local function R(t,n)\
- if type(t)==\"string\"and H[t]then\
- local n,t,i=H[t](n,o,e)\
- if type(n)==\"string\"then term.setCursorBlink(false)return n end\
- if type(t)==\"table\"then\
- if#o<1 then table.insert(o,\"\")end\
- e=math.min(e,#o)\
- a=math.min(a,o[e]:len()+1)\
- o=t\
- elseif type(t)==\"string\"then\
- if t==\"go to\"and i then\
- a,e=1,math.min(#o,i)\
- u(a,e)\
- end\
- end\
- end\
- term.setCursorBlink(true)\
- k()\
- term.setCursorPos(a-m+l,e-i+h)\
- end\
- local function H(q)\
- a,e=1,1\
- l,h=0,1\
- m,i=0,0\
- o=J(q)\
- if not o then return\"menu\"end\
- if o[1]==\"-- Syntax: Brainfuck\"then\
- d=s.brainfuck\
- end\
- local z=os.clock()\
- local c=os.clock()\
- local x=os.clock()\
- local b=false\
- k()\
- term.setCursorPos(a+l,e+h)\
- term.setCursorBlink(true)\
- local j=os.startTimer(3)\
- while true do\
- local s,n,f,w=os.pullEvent()\
- if s==\"key\"and E then\
- if n==200 and e>1 then\
- a,e=math.min(a,o[e-1]:len()+1),e-1\
- p(e,e+1)\
- u(a,e)\
- elseif n==208 and e<#o then\
- a,e=math.min(a,o[e+1]:len()+1),e+1\
- p(e,e-1)\
- u(a,e)\
- elseif n==203 and a>1 then\
- a=a-1\
- local t=false\
- if e-i+h<h+1 then t=true end\
- u(a,e,t)\
- elseif n==205 and a<o[e]:len()+1 then\
- a=a+1\
- local t=false\
- if e-i+h<h+1 then t=true end\
- u(a,e,t)\
- elseif(n==28 or n==156)and(v and true or e+i-1==\
- r.line)then\
- local i=nil\
- for a,t in pairs(V)do\
- if o[e]:find(t)then i=t end\
- end\
- local n,t=o[e]:find(\"^[ ]+\")\
- if not t then t=0 end\
- if i then\
- table.insert(o,e+1,string.rep(\" \",t+2))\
- if not i:find(\"else\",1,true)and not i:find(\"elseif\",1,true)then\
- table.insert(o,e+2,string.rep(\" \",t)..\
- (i:find(\"repeat\",1,true)and\"until \"or i:find(\"{\",1,true)and\"}\"or\
- \"end\"))\
- end\
- a,e=t+3,e+1\
- u(a,e,true)\
- else\
- local i=o[e]\
- o[e]=o[e]:sub(1,a-1)\
- table.insert(o,e+1,string.rep(\" \",t)..i:sub(a,-1))\
- a,e=t+1,e+1\
- u(a,e,true)\
- end\
- elseif n==14 and(v and true or e+i-1==r.line)then\
- if a>1 then\
- local t=false\
- for i,n in pairs(O)do\
- if o[e]:sub(a-1,a-1)==i then t=true end\
- end\
- o[e]=o[e]:sub(1,a-2)..o[e]:sub(a+(t and 1 or 0),-1)\
- p(e)\
- a=a-1\
- u(a,e)\
- elseif e>1 then\
- local t=o[e-1]:len()+1\
- o[e-1]=o[e-1]..o[e]\
- table.remove(o,e)\
- a,e=t,e-1\
- u(a,e,true)\
- end\
- elseif n==199 then\
- a=1\
- local t=false\
- if e-i+h<h+1 then t=true end\
- u(a,e,t)\
- elseif n==207 then\
- a=o[e]:len()+1\
- local t=false\
- if e-i+h<h+1 then t=true end\
- u(a,e,t)\
- elseif n==211 and(v and true or e+i-1==r.line)then\
- if a<o[e]:len()+1 then\
- o[e]=o[e]:sub(1,a-1)..o[e]:sub(a+1)\
- local t=false\
- if e-i+h<h+1 then t=true end\
- p(e)\
- u(a,e,t)\
- elseif e<#o then\
- o[e]=o[e]..o[e+1]\
- table.remove(o,e+1)\
- k()\
- u(a,e)\
- end\
- elseif n==15 and(v and true or e+i-1==r.line)then\
- o[e]=string.rep(\" \",W)..o[e]\
- a=a+2\
- local t=false\
- if e-i+h<h+1 then t=true end\
- p(e)\
- u(a,e,t)\
- elseif n==201 then\
- e=math.min(math.max(e-g,1),#o)\
- a=math.min(o[e]:len()+1,a)\
- u(a,e,true)\
- elseif n==209 then\
- e=math.min(math.max(e+g,1),#o)\
- a=math.min(o[e]:len()+1,a)\
- u(a,e,true)\
- end\
- elseif s==\"char\"and E and(v and true or\
- e+i-1==r.line)then\
- local t=false\
- for s,i in pairs(O)do\
- if n==i and o[e]:find(s,1,true)and o[e]:sub(a,a)==i then\
- t=true\
- end\
- end\
- local s=false\
- if not t then\
- for t,i in pairs(O)do\
- if n==t and o[e]:sub(a,a)~=t then n=n..i s=true end\
- end\
- o[e]=o[e]:sub(1,a-1)..n..o[e]:sub(a,-1)\
- end\
- a=a+(s and 1 or n:len())\
- local t=false\
- if e-i+h<h+1 then t=true end\
- p(e)\
- u(a,e,t)\
- elseif s==\"mouse_click\"and n==1 then\
- if w>1 then\
- if f<=l and w-h==r.line-i then\
- v=not v\
- p(r.line)\
- else\
- local t=e\
- e=math.min(math.max(i+w-h,1),#o)\
- a=math.min(math.max(m+f-l,1),o[e]:len()+1)\
- if t~=e then p(t,e)end\
- u(a,e)\
- end\
- else\
- local e=Q(f,w)\
- if e then\
- local e=R(e,q)\
- if e then return e end\
- end\
- end\
- elseif s==\"shortcut\"then\
- local a=K[n..\" \"..f]\
- if a then\
- local e=nil\
- local o=0\
- for i,t in ipairs(_)do\
- for o,t in pairs(t)do\
- if t==a then\
- e=_[i][1]\
- break\
- end\
- end\
- if e then break end\
- o=o+t[1]:len()+3\
- end\
- local o=o+2\
- term.setCursorBlink(false)\
- term.setCursorPos(o,1)\
- term.setBackgroundColor(colors[t.background])\
- term.write(string.rep(\" \",e:len()+2))\
- term.setCursorPos(o+1,1)\
- term.write(e)\
- sleep(.1)\
- A()\
- local e=R(a,q)\
- if e then return e end\
- end\
- elseif s==\"mouse_scroll\"then\
- if n==-1 and i>0 then\
- i=i-1\
- if os.clock()-c>5e-4 then\
- k()\
- term.setCursorPos(a-m+l,e-i+h)\
- end\
- c=os.clock()\
- b=true\
- elseif n==1 and i<#o-g then\
- i=i+1\
- if os.clock()-c>5e-4 then\
- k()\
- term.setCursorPos(a-m+l,e-i+h)\
- end\
- c=os.clock()\
- b=true\
- end\
- elseif s==\"timer\"and n==j then\
- p(e)\
- j=os.startTimer(3)\
- end\
- if b and os.clock()-c>.1 then\
- k()\
- term.setCursorPos(a-m+l,e-i+h)\
- b=false\
- end\
- if os.clock()-z>B then\
- y(q,o)\
- z=os.clock()\
- end\
- if os.clock()-x>1 then\
- local a=r\
- r=d.parseError(nil)\
- local e=\"\"\
- for a,t in pairs(o)do e=e..t..\"\\n\"end\
- r=d.getCompilerErrors(e)\
- r.line=math.min(r.line-2,#o)\
- if r~=a then k()end\
- x=os.clock()\
- end\
- end\
- return\"menu\"\
- end\
- local function r()\
- local a=n-13\
- w(\"Lua IDE - New File\")\
- local e=I(a,\"/\")\
- if not e or e==\"\"then return\"menu\"end\
- e=\"/\"..e\
- w(\"Lua IDE - New File\")\
- term.setTextColor(colors[t.textColor])\
- term.setBackgroundColor(colors[t.promptHighlight])\
- for e=8,10 do\
- term.setCursorPos(n/2-a/2,e)\
- term.write(string.rep(\" \",a))\
- end\
- term.setCursorPos(1,9)\
- if c.isDir(e)then\
- f(\"Cannot Edit a Directory!\")\
- sleep(1.6)\
- return\"menu\"\
- elseif c.exists(e)then\
- f(\"File Already Exists!\")\
- local t=q({{\"Open\",n/2-9,14},{\"Cancel\",n/2+2,14}},\"horizontal\")\
- if t==\"Open\"then return\"edit\",e\
- elseif t==\"Cancel\"then return\"menu\"end\
- else return\"edit\",e end\
- end\
- local function h()\
- local a=n-13\
- w(\"Lua IDE - Open File\")\
- local e=I(a,\"/\")\
- if not e or e==\"\"then return\"menu\"end\
- e=\"/\"..e\
- w(\"Lua IDE - New File\")\
- term.setTextColor(colors[t.textColor])\
- term.setBackgroundColor(colors[t.promptHighlight])\
- for e=8,10 do\
- term.setCursorPos(n/2-a/2,e)\
- term.write(string.rep(\" \",a))\
- end\
- term.setCursorPos(1,9)\
- if c.isDir(e)then\
- f(\"Cannot Open a Directory!\")\
- sleep(1.6)\
- return\"menu\"\
- elseif not c.exists(e)then\
- f(\"File Doesn't Exist!\")\
- local t=q({{\"Create\",n/2-11,14},{\"Cancel\",n/2+2,14}},\"horizontal\")\
- if t==\"Create\"then return\"edit\",e\
- elseif t==\"Cancel\"then return\"menu\"end\
- else return\"edit\",e end\
- end\
- local function e()\
- local function e(e)\
- w(\"LuaIDE - Update\")\
- term.setBackgroundColor(colors[t.prompt])\
- term.setTextColor(colors[t.textColor])\
- for t=8,10 do\
- term.setCursorPos(n/2-(e:len()+4),t)\
- write(string.rep(\" \",e:len()+4))\
- end\
- term.setCursorPos(n/2-(e:len()+4),9)\
- term.write(\" - \"..e..\" \")\
- term.setBackgroundColor(colors[t.errHighlight])\
- for e=8,10 do\
- term.setCursorPos(n/2+2,e)\
- term.write(string.rep(\" \",10))\
- end\
- term.setCursorPos(n/2+2,9)\
- term.write(\" > Cancel \")\
- end\
- if not http then\
- e(\"HTTP API Disabled!\")\
- sleep(1.6)\
- return\"settings\"\
- end\
- e(\"Updating...\")\
- local i=os.startTimer(10)\
- http.request(M)\
- while true do\
- local t,o,a,s=os.pullEvent()\
- if(t==\"key\"and o==28)or\
- (t==\"mouse_click\"and a>=n/2+2 and a<=n/2+12 and s==9)then\
- e(\"Cancelled\")\
- sleep(1.6)\
- break\
- elseif t==\"http_success\"and o==M then\
- local a=a.readAll()\
- local t=io.open(C,\"r\")\
- local o=t:read(\"*a\")\
- t:close()\
- if o~=a then\
- e(\"Update Found\")\
- sleep(1.6)\
- local t=io.open(C,\"w\")\
- t:write(a)\
- t:close()\
- e(\"Click to Exit\")\
- while true do\
- local e=os.pullEvent()\
- if e==\"mouse_click\"or(not b()and e==\"key\")then break end\
- end\
- return\"exit\"\
- else\
- e(\"No Updates Found!\")\
- sleep(1.6)\
- break\
- end\
- elseif t==\"http_failure\"or(t==\"timer\"and o==i)then\
- e(\"Update Failed!\")\
- sleep(1.6)\
- break\
- end\
- end\
- return\"settings\"\
- end\
- local function o()\
- w(\"LuaIDE - Theme\")\
- if b()then\
- local e={\"Back\"}\
- for a,t in pairs(D)do table.insert(e,t[1])end\
- local a=Y(e)\
- local e=nil\
- for o,t in pairs(D)do if t[1]==a then e=t[2]end end\
- if not e then return\"settings\"end\
- if a==\"Dawn (Default)\"then\
- term.setBackgroundColor(colors[t.backgroundHighlight])\
- term.setCursorPos(3,3)\
- term.clearLine()\
- term.write(\"LuaIDE - Loaded Theme!\")\
- sleep(1.6)\
- fs.delete(z)\
- t=N\
- return\"menu\"\
- end\
- term.setBackgroundColor(colors[t.backgroundHighlight])\
- term.setCursorPos(3,3)\
- term.clearLine()\
- term.write(\"LuaIDE - Downloading...\")\
- fs.delete(\"/.LuaIDE_temp_theme_file\")\
- P(e,\"/.LuaIDE_temp_theme_file\")\
- local e=L(\"/.LuaIDE_temp_theme_file\")\
- term.setCursorPos(3,3)\
- term.clearLine()\
- if e then\
- term.write(\"LuaIDE - Loaded Theme!\")\
- fs.delete(z)\
- fs.move(\"/.LuaIDE_temp_theme_file\",z)\
- t=e\
- sleep(1.6)\
- return\"menu\"\
- end\
- term.write(\"LuaIDE - Could Not Load Theme!\")\
- fs.delete(\"/.LuaIDE_temp_theme_file\")\
- sleep(1.6)\
- return\"settings\"\
- else\
- term.setCursorPos(1,8)\
- f(\"Themes are not available on\")\
- f(\"normal computers!\")\
- end\
- end\
- local function s()\
- w(\"LuaIDE - Settings\")\
- local e=q({{\"Change Theme\",n/2-17,8},{\"Return to Menu\",n/2-22,13},\
- {\"Exit IDE\",n/2+2,13,bg=colors[t.err],\
- highlight=colors[t.errHighlight]}},\"vertical\",true)\
- if e==\"Change Theme\"then return o()\
- elseif e==\"Return to Menu\"then return\"menu\"\
- elseif e==\"Exit IDE\"then return\"exit\"end\
- end\
- local function i()\
- w(\"Welcome to LuaIDE \"..F)\
- local e=q({{\"New File\",n/2-13,8},{\"Open File\",n/2-14,13},\
- {\"Settings\",n/2+2,8},{\"Exit IDE\",n/2+2,13,bg=colors[t.err],\
- highlight=colors[t.errHighlight]}},\"vertical\",true)\
- if e==\"New File\"then return\"new\"\
- elseif e==\"Open File\"then return\"open\"\
- elseif e==\"Settings\"then return\"settings\"\
- elseif e==\"Exit IDE\"then return\"exit\"end\
- end\
- local function o(a)\
- local e,t=\"menu\",nil\
- if type(a)==\"table\"and#a>0 then\
- local a=\"/\"..shell.resolve(a[1])\
- if c.isDir(a)then print(\"Cannot edit a directory.\")end\
- e,t=\"edit\",a\
- end\
- while true do\
- if e==\"menu\"then e=i()end\
- if e==\"new\"then e,t=r()\
- elseif e==\"open\"then e,t=h()\
- elseif e==\"settings\"then e=s()\
- end if e==\"exit\"then break end\
- if e==\"edit\"and t then e=H(t)end\
- end\
- end\
- if fs.exists(z)then t=L(z)end\
- if not t and b()then t=N\
- elseif not t then t=U end\
- local a,e=pcall(function()\
- parallel.waitForAny(function()o(G)end,monitorKeyboardShortcuts)\
- end)\
- if e and not e:find(\"Terminated\")then\
- term.setCursorBlink(false)\
- w(\"LuaIDE - Crash! D:\")\
- term.setBackgroundColor(colors[t.err])\
- for e=6,8 do\
- term.setCursorPos(5,e)\
- term.write(string.rep(\" \",36))\
- end\
- term.setCursorPos(6,7)\
- term.write(\"LuaIDE Has Crashed! D:\")\
- term.setBackgroundColor(colors[t.background])\
- term.setCursorPos(2,10)\
- print(e)\
- term.setBackgroundColor(colors[t.prompt])\
- local a,e=term.getCursorPos()\
- for e=e+1,e+4 do\
- term.setCursorPos(5,e)\
- term.write(string.rep(\" \",36))\
- end\
- term.setCursorPos(6,e+2)\
- term.write(\"Please report this error to\")\
- term.setCursorPos(6,e+3)\
- term.write(\"GravityScore! \")\
- term.setBackgroundColor(colors[t.background])\
- if b()then f(\"Click to Exit...\",T-1)\
- else f(\"Press Any Key to Exit...\",T-1)end\
- while true do\
- local e=os.pullEvent()\
- if e==\"mouse_click\"or(not b()and e==\"key\")then break end\
- end\
- os.queueEvent(S)\
- os.pullEvent()\
- end\
- term.setBackgroundColor(colors.black)\
- term.setTextColor(colors.white)\
- term.clear()\
- term.setCursorPos(1,1)\
- f(\"Thank You for Using Lua IDE \"..F)\
- f(\"Made by GravityScore\")",
- paint = "-- Paint created by nitrogenfingers (edited by dan200)\
- -- http://www.youtube.com/user/NitrogenFingers\
- \
- ------------\
- -- Fields --\
- ------------\
- \
- -- The width and height of the terminal\
- local w,h = term.getSize()\
- \
- -- The selected colours on the left and right mouse button, and the colour of the canvas\
- local leftColour, rightColour = colours.white, nil\
- local canvasColour = colours.black\
- \
- -- The values stored in the canvas\
- local canvas = {}\
- \
- -- The menu options\
- local mChoices = { \"Save\",\"Exit\" }\
- \
- -- The message displayed in the footer bar\
- local fMessage = \"Press Ctrl to access menu\"\
- \
- -------------------------\
- -- Initialisation --\
- -------------------------\
- \
- -- Determine if we can even run this\
- if not term.isColour() then\
- \009print(\"Requires an Advanced Computer\")\
- \009return\
- end\
- \
- -- Determines if the file exists, and can be edited on this computer\
- local tArgs = {...}\
- if #tArgs == 0 then\
- \009print(\"Usage: paint <path>\")\
- \009return\
- end\
- local sPath = shell.resolve(tArgs[1])\
- local bReadOnly = fs.isReadOnly(sPath)\
- if fs.exists(sPath) and fs.isDir(sPath) then\
- \009print(\"Cannot edit a directory.\")\
- \009return\
- end\
- \
- ---------------\
- -- Functions --\
- ---------------\
- \
- local function getCanvasPixel( x, y )\
- if canvas[y] then\
- return canvas[y][x]\
- end\
- return nil\
- end\
- \
- --[[\
- \009Converts a colour value to a text character\
- \009params: colour = the number to convert to a hex value\
- \009returns: a string representing the chosen colour\
- ]]\
- local function getCharOf( colour )\
- \009-- Incorrect values always convert to nil\
- \009if type(colour) == \"number\" then\
- \009\009local value = math.floor( math.log(colour) / math.log(2) ) + 1\
- \009\009if value >= 1 and value <= 16 then\
- \009\009\009return string.sub( \"0123456789abcdef\", value, value )\
- \009\009end\
- \009end\
- \009return \" \"\
- end\009\
- \
- --[[\
- \009Converts a text character to colour value\
- \009params: char = the char (from string.byte) to convert to number\
- \009returns: the colour number of the hex value\
- ]]\
- local tColourLookup = {}\
- for n=1,16 do\
- \009tColourLookup[ string.byte( \"0123456789abcdef\",n,n ) ] = 2^(n-1)\
- end\
- local function getColourOf( char )\
- \009-- Values not in the hex table are transparent (canvas coloured)\
- \009return tColourLookup[char]\
- end\
- \
- --[[ \
- \009Loads the file into the canvas\
- \009params: path = the path of the file to open\
- \009returns: nil\
- ]]\
- local function load(path)\
- \009-- Load the file\
- \009if fs.exists(path) then\
- \009\009local file = fs.open(sPath, \"r\")\
- \009\009local sLine = file.readLine()\
- \009\009while sLine do\
- \009\009\009local line = {}\
- \009\009\009for x=1,w-2 do\
- \009\009\009\009line[x] = getColourOf( string.byte(sLine,x,x) )\
- \009\009\009end\
- \009\009\009table.insert( canvas, line )\
- \009\009\009sLine = file.readLine()\
- \009\009end\
- \009\009file.close()\
- \009end\
- end\
- \
- --[[ \
- \009Saves the current canvas to file \
- \009params: path = the path of the file to save\
- \009returns: true if save was successful, false otherwise\
- ]]\
- local function save(path)\
- -- Open file\
- \009local sDir = string.sub(sPath, 1, #sPath - #fs.getName(sPath))\
- \009if not fs.exists(sDir) then\
- \009\009fs.makeDir(sDir)\
- \009end\
- \
- \009local file = fs.open( path, \"w\" )\
- \009if not file then\
- \009 return false\
- \009end\
- \
- -- Encode (and trim)\
- \009local tLines = {}\
- \009local nLastLine = 0\
- \009for y=1,h-1 do\
- \009 local sLine = \"\"\
- \009 local nLastChar = 0\
- \009\009for x=1,w-2 do\
- \009\009 local c = getCharOf( getCanvasPixel( x, y ) )\
- \009\009 sLine = sLine .. c\
- \009\009 if c ~= \" \" then\
- \009\009 nLastChar = x\
- \009\009 end\
- \009\009end\
- \009\009sLine = string.sub( sLine, 1, nLastChar )\
- \009\009tLines[y] = sLine\
- \009\009if string.len( sLine ) > 0 then\
- \009\009 nLastLine = y\
- \009\009end\
- \009end\
- \
- -- Save out\
- \009for n=1,nLastLine do\
- \009 file.writeLine( tLines[ n ] )\
- \009end\
- \009file.close()\
- \009return true\
- end\
- \
- --[[ \
- \009Draws colour picker sidebar, the pallette and the footer\
- \009returns: nil\
- ]]\
- local function drawInterface()\
- \009-- Footer\
- \009term.setCursorPos(1, h)\
- \009term.setBackgroundColour(colours.black)\
- \009term.setTextColour(colours.yellow)\
- \009term.clearLine()\
- \009term.write(fMessage)\
- \009\
- \009-- Colour Picker\
- \009for i=1,16 do\
- \009\009term.setCursorPos(w-1, i)\
- \009\009term.setBackgroundColour( 2^(i-1) )\
- \009\009term.write(\" \")\
- \009end\
- \
- \009term.setCursorPos(w-1, 17)\
- \009term.setBackgroundColour( canvasColour )\
- \009term.setTextColour( colours.grey )\
- \009term.write(\"\\127\\127\")\
- \009\009\009\
- \009-- Left and Right Selected Colours\
- \009for i=18,18 do\
- \009\009term.setCursorPos(w-1, i)\
- \009\009if leftColour ~= nil then\
- \009\009\009term.setBackgroundColour( leftColour )\
- \009\009\009term.write(\" \")\
- \009\009else\
- \009\009\009term.setBackgroundColour( canvasColour )\
- \009\009\009term.setTextColour( colours.grey )\
- \009\009\009term.write(\"\\127\")\
- \009\009end\
- \009\009if rightColour ~= nil then\
- \009\009\009term.setBackgroundColour( rightColour )\
- \009\009\009term.write(\" \")\
- \009\009else\
- \009\009\009term.setBackgroundColour( canvasColour )\
- \009\009\009term.setTextColour( colours.grey )\
- \009\009\009term.write(\"\\127\")\
- \009\009end\
- \009end\
- \
- \009-- Padding\
- \009term.setBackgroundColour( canvasColour )\
- \009for i=20,h-1 do\
- \009\009term.setCursorPos(w-1, i)\
- \009\009term.write(\" \")\
- \009end\
- end\
- \
- --[[ \
- \009Converts a single pixel of a single line of the canvas and draws it\
- \009returns: nil\
- ]]\
- local function drawCanvasPixel( x, y )\
- \009local pixel = getCanvasPixel( x, y )\
- \009if pixel then\
- \009\009term.setBackgroundColour( pixel or canvasColour )\
- \009\009term.setCursorPos(x, y)\
- \009\009term.write(\" \")\
- \009else\
- \009\009term.setBackgroundColour( canvasColour )\
- \009\009term.setTextColour( colours.grey )\
- \009\009term.setCursorPos(x, y)\
- term.write(\"\\127\")\
- \009end\
- end\
- \
- --[[ \
- \009Converts each colour in a single line of the canvas and draws it\
- \009returns: nil\
- ]]\
- local function drawCanvasLine( y )\
- \009for x = 1, w-2 do\
- \009\009drawCanvasPixel( x, y )\
- \009end\
- end\
- \
- --[[ \
- \009Converts each colour in the canvas and draws it\
- \009returns: nil\
- ]]\
- local function drawCanvas()\
- \009for y = 1, h-1 do\
- \009\009drawCanvasLine( y )\
- \009end\
- end\
- \
- --[[\
- \009Draws menu options and handles input from within the menu.\
- \009returns: true if the program is to be exited; false otherwise\
- ]]\
- local function accessMenu()\
- \009-- Selected menu option\
- \009local selection = 1\
- \009\
- \009term.setBackgroundColour(colours.black)\
- \009while true do\
- \009\009-- Draw the menu\
- \009\009term.setCursorPos(1,h)\
- \009\009term.clearLine()\
- \009\009term.setTextColour(colours.white)\
- \009\009for k,v in pairs(mChoices) do\
- \009\009\009if selection==k then \
- \009\009\009\009term.setTextColour(colours.yellow)\
- \009\009\009\009local ox,_ = term.getCursorPos()\
- \009\009\009\009term.write(\"[\"..string.rep(\" \",#v)..\"]\")\
- \009\009\009\009term.setCursorPos(ox+1,h)\
- \009\009\009\009term.setTextColour(colours.white)\
- \009\009\009\009term.write(v)\
- \009\009\009\009term.setCursorPos(term.getCursorPos()+1,h)\
- \009\009\009else\
- \009\009\009\009term.write(\" \"..v..\" \")\
- \009\009\009end\
- \009\009end\
- \009\009\
- \009\009-- Handle input in the menu\
- \009\009local id,key = os.pullEvent(\"key\")\
- \009\009if id == \"key\" then\
- \009\009\009-- S and E are shortcuts\
- \009\009\009if key == keys.s then\
- \009\009\009\009selection = 1\
- \009\009\009\009key = keys.enter\
- \009\009\009elseif key == keys.e then\
- \009\009\009\009selection = 2\
- \009\009\009\009key = keys.enter\
- \009\009\009end\
- \009\009\
- \009\009\009if key == keys.right then\
- \009\009\009\009-- Move right\
- \009\009\009\009selection = selection + 1\
- \009\009\009\009if selection > #mChoices then\
- \009\009\009\009\009selection = 1\
- \009\009\009\009end\
- \009\009\009\009\
- \009\009\009elseif key == keys.left and selection > 1 then\
- \009\009\009\009-- Move left\
- \009\009\009\009selection = selection - 1\
- \009\009\009\009if selection < 1 then\
- \009\009\009\009\009selection = #mChoices\
- \009\009\009\009end\
- \009\009\009\009\
- \009\009\009elseif key == keys.enter then\
- \009\009\009\009-- Select an option\
- \009\009\009\009if mChoices[selection]==\"Save\" then \
- \009\009\009\009\009if bReadOnly then \
- \009\009\009\009\009\009fMessage = \"Access Denied\"\
- \009\009\009\009\009\009return false\
- \009\009\009\009\009end\
- \009\009\009\009\009local success = save(sPath)\
- \009\009\009\009\009if success then\
- \009\009\009\009\009\009fMessage = \"Saved to \"..sPath\
- \009\009\009\009\009else\
- \009\009\009\009\009\009fMessage = \"Error saving to \"..sPath\
- \009\009\009\009\009end\
- \009\009\009\009\009return false\
- \009\009\009\009elseif mChoices[selection]==\"Exit\" then \
- \009\009\009\009\009return true\
- \009\009\009\009end\
- \009\009\009elseif key == keys.leftCtrl or keys == keys.rightCtrl then\
- \009\009\009\009-- Cancel the menu\
- \009\009\009\009return false \
- \009\009\009end\
- \009\009end\
- \009end\
- end\
- \
- --[[ \
- \009Runs the main thread of execution. Draws the canvas and interface, and handles\
- \009mouse and key events.\
- \009returns: nil\
- ]]\
- local function handleEvents()\
- \009local programActive = true\
- \009while programActive do\
- \009\009local id,p1,p2,p3 = os.pullEvent()\
- \009\009if id==\"mouse_click\" or id==\"mouse_drag\" then\
- \009\009\009if p2 >= w-1 and p3 >= 1 and p3 <= 17 then\
- \009\009\009\009if id ~= \"mouse_drag\" then\
- \009\009\009\009\009-- Selecting an items in the colour picker\
- \009\009\009\009\009if p3 <= 16 then\
- \009\009\009\009\009\009if p1==1 then\
- \009\009\009\009\009\009\009leftColour = 2^(p3-1)\
- \009\009\009\009\009\009else\
- \009\009\009\009\009\009\009rightColour = 2^(p3-1)\
- \009\009\009\009\009\009end\
- \009\009\009\009\009else\
- \009\009\009\009\009\009if p1==1 then\
- \009\009\009\009\009\009\009leftColour = nil\
- \009\009\009\009\009\009else\
- \009\009\009\009\009\009\009rightColour = nil\
- \009\009\009\009\009\009end\
- \009\009\009\009\009end\
- \009\009\009\009\009--drawCanvas()\
- \009\009\009\009\009drawInterface()\
- \009\009\009\009end\
- \009\009\009elseif p2 < w-1 and p3 <= h-1 then\
- \009\009\009\009-- Clicking on the canvas\
- \009\009\009\009local paintColour = nil\
- \009\009\009\009if p1==1 then\
- \009\009\009\009\009paintColour = leftColour\
- \009\009\009\009elseif p1==2 then\
- \009\009\009\009\009paintColour = rightColour\
- \009\009\009\009end\
- \009\009\009\009if not canvas[p3] then\
- canvas[p3] = {}\
- \009\009\009end\
- canvas[p3][p2] = paintColour\
- \
- \009\009\009\009drawCanvasPixel( p2, p3 )\
- \009\009\009end\
- \009\009elseif id==\"key\" then\
- \009\009\009if p1==keys.leftCtrl or p1==keys.rightCtrl then\
- \009\009\009\009programActive = not accessMenu()\
- \009\009\009\009drawInterface()\
- \009\009\009end\
- \009\009elseif id==\"term_resize\" then\
- \009\009 w,h = term.getSize()\
- drawCanvas()\
- drawInterface()\
- end\
- \009end\
- end\
- \
- -- Init\
- load(sPath)\
- drawCanvas()\
- drawInterface()\
- \
- -- Main loop\
- handleEvents()\
- \
- -- Shutdown\
- term.setBackgroundColour(colours.black)\
- term.setTextColour(colours.white)\
- term.clear()\
- term.setCursorPos(1,1)",
- worm = "\
- -- Display the start screen\
- local w,h = term.getSize()\
- \
- local titleColour, headingColour, textColour, wormColour, fruitColour\
- if term.isColour() then\
- titleColour = colours.red\
- \009headingColour = colours.yellow\
- \009textColour = colours.white\
- \009wormColour = colours.green\
- \009fruitColour = colours.red\
- else\
- titleColour = colours.white\
- \009headingColour = colours.white\
- \009textColour = colours.white\
- \009wormColour = colours.white\
- \009fruitColour = colours.white\
- end\
- \
- local function printCentred( y, s )\
- \009local x = math.floor((w - string.len(s)) / 2)\
- \009term.setCursorPos(x,y)\
- \009--term.clearLine()\
- \009term.write( s )\
- end\
- \
- local xVel,yVel = 1,0\
- local xPos, yPos = math.floor(w/2), math.floor(h/2)\
- local pxVel, pyVel = nil, nil\
- \
- local nLength = 1\
- local nExtraLength = 6\
- local bRunning = true\
- \
- local tailX,tailY = xPos,yPos\
- local nScore = 0\
- local nDifficulty = 2\
- local nSpeed, nInterval\
- \
- -- Setup the screen\
- local screen = {}\
- for x=1,w do\
- \009screen[x] = {}\
- \009for y=1,h do\
- \009\009screen[x][y] = {}\
- \009end\
- end\
- screen[xPos][yPos] = { snake = true }\
- \
- local nFruit = 1\
- local tFruits = {\
- \009\"A\", \"B\", \"C\", \"D\", \"E\", \"F\", \"G\", \"H\",\
- \009\"I\", \"J\", \"K\", \"L\", \"M\", \"N\", \"O\", \"P\",\
- \009\"Q\", \"R\", \"S\", \"T\", \"U\", \"V\", \"W\", \"X\",\
- \009\"Y\", \"Z\",\
- \009\"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\", \"h\",\
- \009\"i\", \"j\", \"k\", \"l\", \"m\", \"n\", \"o\", \"p\",\
- \009\"q\", \"r\", \"s\", \"t\", \"u\", \"v\", \"w\", \"x\",\
- \009\"y\", \"z\",\
- \009\"1\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\", \"0\",\
- \009\"@\", \"$\", \"%\", \"#\", \"&\", \"!\", \"?\", \"+\", \"*\", \"~\"\
- }\
- \
- local function addFruit()\
- \009while true do\
- \009\009local x = math.random(1,w)\
- \009\009local y = math.random(2,h)\
- \009\009local fruit = screen[x][y]\
- \009\009if fruit.snake == nil and fruit.wall == nil and fruit.fruit == nil then\
- \009\009\009screen[x][y] = { fruit = true }\
- \009\009\009term.setCursorPos(x,y)\
- \009\009\009term.setBackgroundColour( fruitColour )\
- \009\009\009term.write(\" \")\
- \009\009\009term.setBackgroundColour( colours.black )\
- \009\009\009break\
- \009\009end\
- \009end\
- \009\
- \009nFruit = nFruit + 1\
- \009if nFruit > #tFruits then\
- \009\009nFruit = 1\
- \009end\
- end\
- \
- local function drawMenu()\
- \009term.setTextColour( headingColour )\
- \009term.setCursorPos(1,1)\
- \009term.write( \"SCORE \" )\
- \009\
- \009term.setTextColour( textColour )\
- \009term.setCursorPos(7,1)\
- \009term.write( tostring(nScore) )\
- \
- \009term.setTextColour( headingColour )\
- \009term.setCursorPos(w-11,1)\
- \009term.write( \"DIFFICULTY \")\
- \
- \009term.setTextColour( textColour )\
- \009term.setCursorPos(w,1)\
- \009term.write( tostring(nDifficulty or \"?\") ) \
- \
- \009term.setTextColour( colours.white )\
- end\
- \
- local function update( )\
- \009local x,y = xPos,yPos\
- \009if pxVel and pyVel then\
- \009\009xVel, yVel = pxVel, pyVel\
- \009\009pxVel, pyVel = nil, nil\
- \009end\
- \
- \009-- Remove the tail\
- \009if nExtraLength == 0 then\
- \009\009local tail = screen[tailX][tailY]\
- \009\009screen[tailX][tailY] = {}\
- \009\009term.setCursorPos(tailX,tailY)\
- \009\009term.write(\" \")\
- \009\009tailX = tail.nextX\
- \009\009tailY = tail.nextY\
- \009else\
- \009\009nExtraLength = nExtraLength - 1\
- \009end\
- \009\
- \009-- Update the head\
- \009local head = screen[xPos][yPos]\
- \009local newXPos = xPos + xVel\
- \009local newYPos = yPos + yVel\
- \009if newXPos < 1 then\
- \009\009newXPos = w\
- \009elseif newXPos > w then\
- \009\009newXPos = 1\
- \009end\
- \009if newYPos < 2 then\
- \009\009newYPos = h\
- \009elseif newYPos > h then\
- \009\009newYPos = 2\
- \009end\
- \009\
- \009local newHead = screen[newXPos][newYPos]\
- \009term.setCursorPos(1,1);\
- \009print( newHead.snake )\
- \009if newHead.snake == true or newHead.wall == true then\
- \009\009bRunning = false\
- \009\009\
- \009else\
- \009\009if newHead.fruit == true then\
- \009\009\009nScore = nScore + 10\
- \009\009\009nExtraLength = nExtraLength + 1\
- \009\009\009addFruit()\
- \009\009end\
- \009\009xPos = newXPos\
- \009\009yPos = newYPos\
- \009\009head.nextX = newXPos\
- \009\009head.nextY = newYPos\
- \009\009screen[newXPos][newYPos] = { snake = true }\
- \009\009\
- \009end\
- \009\
- \009term.setCursorPos(xPos,yPos)\
- \009term.setBackgroundColour( wormColour )\
- \009term.write(\" \")\
- \009term.setBackgroundColour( colours.black )\
- \
- \009drawMenu()\
- end\
- \
- -- Display the frontend\
- term.clear()\
- local function drawFrontend()\
- \009--term.setTextColour( titleColour )\
- --printCentred( math.floor(h/2) - 4, \" W O R M \" )\
- \
- \009term.setTextColour( headingColour )\
- \009printCentred( math.floor(h/2) - 3, \"\" )\
- \009printCentred( math.floor(h/2) - 2, \" SELECT DIFFICULTY \" )\
- \009printCentred( math.floor(h/2) - 1, \"\" )\
- \009\
- \009printCentred( math.floor(h/2) + 0, \" \" )\
- \009printCentred( math.floor(h/2) + 1, \" \" )\
- \009printCentred( math.floor(h/2) + 2, \" \" )\
- \009printCentred( math.floor(h/2) - 1 + nDifficulty, \" [ ] \" )\
- \
- \009term.setTextColour( textColour )\
- \009printCentred( math.floor(h/2) + 0, \"EASY\" )\
- \009printCentred( math.floor(h/2) + 1, \"MEDIUM\" )\
- \009printCentred( math.floor(h/2) + 2, \"HARD\" )\
- \009printCentred( math.floor(h/2) + 3, \"\" )\
- \
- \009term.setTextColour( colours.white )\
- end\
- \
- drawMenu()\
- drawFrontend()\
- while true do\
- \009local e,key = os.pullEvent( \"key\" )\
- \009if key == keys.up or key == keys.w then\
- \009\009-- Up\
- \009\009if nDifficulty > 1 then\
- \009\009\009nDifficulty = nDifficulty - 1\
- \009\009\009drawMenu()\
- \009\009\009drawFrontend()\
- \009\009end\
- \009elseif key == keys.down or key == keys.s then\
- \009\009-- Down\
- \009\009if nDifficulty < 3 then\
- \009\009\009nDifficulty = nDifficulty + 1\
- \009\009\009drawMenu()\
- \009\009\009drawFrontend()\
- \009\009end\
- \009elseif key == keys.enter then\
- \009\009-- Enter\
- \009\009break\
- \009end\
- end\
- \
- local tSpeeds = { 5, 10, 25 }\
- nSpeed = tSpeeds[nDifficulty]\
- nInterval = 1 / nSpeed\
- \
- -- Grow the snake to its intended size\
- term.clear()\
- drawMenu()\
- screen[tailX][tailY].snake = true\
- while nExtraLength > 0 do\
- \009update()\
- end\
- addFruit()\
- addFruit()\
- \
- -- Play the game\
- local timer = os.startTimer(0)\
- while bRunning do\
- \009local event, p1, p2 = os.pullEvent()\
- \009if event == \"timer\" and p1 == timer then\
- \009\009timer = os.startTimer(nInterval)\
- \009\009update( false )\
- \009\
- \009elseif event == \"key\" then\
- \009\009local key = p1\
- \009\009if key == keys.up or key == keys.w then\
- \009\009\009-- Up\
- \009\009\009if yVel == 0 then\
- \009\009\009\009pxVel,pyVel = 0,-1\
- \009\009\009end\
- \009\009elseif key == keys.down or key == keys.s then\
- \009\009\009-- Down\
- \009\009\009if yVel == 0 then\
- \009\009\009\009pxVel,pyVel = 0,1\
- \009\009\009end\
- \009\009elseif key == keys.left or key == keys.a then\
- \009\009\009-- Left\
- \009\009\009if xVel == 0 then\
- \009\009\009\009pxVel,pyVel = -1,0\
- \009\009\009end\
- \009\009\
- \009\009elseif key == keys.right or key == keys.d then\
- \009\009\009-- Right\
- \009\009\009if xVel == 0 then\
- \009\009\009\009pxVel,pyVel = 1,0\
- \009\009\009end\
- \009\009\
- \009\009end\009\
- \009end\
- end\
- \
- -- Display the gameover screen\
- term.setTextColour( headingColour )\
- printCentred( math.floor(h/2) - 2, \" \" )\
- printCentred( math.floor(h/2) - 1, \" G A M E O V E R \" )\
- \
- term.setTextColour( textColour )\
- printCentred( math.floor(h/2) + 0, \" \" )\
- printCentred( math.floor(h/2) + 1, \" FINAL SCORE \"..nScore..\" \" )\
- printCentred( math.floor(h/2) + 2, \" \" )\
- term.setTextColour( colours.white )\
- \
- local timer = os.startTimer(2.5)\
- repeat\
- \009local e,p = os.pullEvent()\
- \009if e == \"timer\" and p == timer then\
- \009\009term.setTextColour( textColour )\
- \009\009printCentred( math.floor(h/2) + 2, \" PRESS ANY KEY \" )\
- \009\009printCentred( math.floor(h/2) + 3, \" \" )\
- \009\009term.setTextColour( colours.white )\
- \009end\
- until e == \"char\"\
- \
- term.clear()\
- term.setCursorPos(1,1)\
- \
- \009\009",
- falling = "--[[\
- Falling - Based on Tetris by Alexey Pajitnov\
- This version written by Gopher, at the request of Dan200, for\
- ComputerCraft v1.6. No particular rights are reserved.\
- --]]\
- \
- local function colorass(c,bw)\
- return term.isColor() and c or bw\
- end\
- \
- local block_s1= {\
- {\
- { 1,0,0,0, },\
- { 1,1,0,0, },\
- { 0,1,0,0, },\
- { 0,0,0,0, },\
- },\
- {\
- { 0,0,0,0, },\
- { 0,1,1,0, },\
- { 1,1,0,0, },\
- { 0,0,0,0, },\
- },\
- ch=colorass(\" \",\"{}\"),\
- fg=colorass(colors.blue,colors.black),\
- bg=colorass(colors.cyan,colors.white),\
- }\
- local block_s2= {\
- {\
- { 0,1,0,0, },\
- { 1,1,0,0, },\
- { 1,0,0,0, },\
- { 0,0,0,0, },\
- },\
- {\
- { 0,0,0,0, },\
- { 1,1,0,0, },\
- { 0,1,1,0, },\
- { 0,0,0,0, },\
- },\
- ch=colorass(\" \",\"{}\"),\
- fg=colorass(colors.green,colors.black),\
- bg=colorass(colors.lime,colors.white),\
- }\
- local block_line = {\
- {\
- { 0,1,0,0, },\
- { 0,1,0,0, },\
- { 0,1,0,0, },\
- { 0,1,0,0, },\
- },\
- {\
- { 0,0,0,0, },\
- { 1,1,1,1, },\
- { 0,0,0,0, },\
- { 0,0,0,0, },\
- },\
- ch=colorass(\" \",\"[]\"),\
- fg=colorass(colors.pink,colors.black),\
- bg=colorass(colors.red,colors.white),\
- }\
- local block_square = {\
- {\
- { 1,1,0,0, },\
- { 1,1,0,0, },\
- { 0,0,0,0, },\
- { 0,0,0,0, },\
- },\
- ch=colorass(\" \",\"[]\"),\
- fg=colorass(colors.lightBlue,colors.black),\
- bg=colorass(colors.blue,colors.white),\
- }\
- local block_L1 = {\
- {\
- { 1,1,0,0, },\
- { 0,1,0,0, },\
- { 0,1,0,0, },\
- { 0,0,0,0, },\
- },\
- {\
- { 0,0,0,0, },\
- { 1,1,1,0, },\
- { 1,0,0,0, },\
- { 0,0,0,0, },\
- },\
- {\
- { 0,1,0,0, },\
- { 0,1,0,0, },\
- { 0,1,1,0, },\
- { 0,0,0,0, },\
- },\
- {\
- { 0,0,1,0, },\
- { 1,1,1,0, },\
- { 0,0,0,0, },\
- { 0,0,0,0, },\
- },\
- ch=colorass(\" \",\"()\"),\
- fg=colorass(colors.orange,colors.black),\
- bg=colorass(colors.yellow,colors.white),\
- }\
- local block_L2 = {\
- {\
- { 0,1,0,0, },\
- { 0,1,0,0, },\
- { 1,1,0,0, },\
- { 0,0,0,0, },\
- },\
- {\
- { 0,0,0,0, },\
- { 1,1,1,0, },\
- { 0,0,1,0, },\
- { 0,0,0,0, },\
- },\
- {\
- { 0,1,1,0, },\
- { 0,1,0,0, },\
- { 0,1,0,0, },\
- { 0,0,0,0, },\
- },\
- {\
- { 1,0,0,0, },\
- { 1,1,1,0, },\
- { 0,0,0,0, },\
- { 0,0,0,0, },\
- },\
- ch=colorass(\" \",\"()\"),\
- fg=colorass(colors.brown,colors.black),\
- bg=colorass(colors.orange,colors.white),\
- }\
- local block_T = {\
- {\
- { 0,1,0,0, },\
- { 1,1,0,0, },\
- { 0,1,0,0, },\
- { 0,0,0,0, },\
- },\
- {\
- { 0,0,0,0, },\
- { 1,1,1,0, },\
- { 0,1,0,0, },\
- { 0,0,0,0, },\
- },\
- {\
- { 0,1,0,0, },\
- { 0,1,1,0, },\
- { 0,1,0,0, },\
- { 0,0,0,0, },\
- },\
- {\
- { 0,1,0,0, },\
- { 1,1,1,0, },\
- { 0,0,0,0, },\
- { 0,0,0,0, },\
- },\
- ch=colorass(\" \",\"<>\"),\
- fg=colorass(colors.cyan,colors.black),\
- bg=colorass(colors.purple,colors.white),\
- }\
- \
- local blocks={ block_line, block_square, block_s1, block_s2, block_L1, block_L2, block_T}\
- \
- local points={4,10,30,120}\
- \
- local function lpad(text,amt)\
- text=tostring(text)\
- return string.rep(\" \",amt-#text)..text\
- end\
- \
- local width,height=term.getSize()\
- \
- if height<19 or width<26 then\
- print(\"Your screen is too small to play :(\")\
- return\
- end\
- \
- \
- local speedsByLevel={\
- 1.2,\
- 1.0,\
- .8,\
- .65,\
- .5,\
- .4,\
- .3,\
- .25,\
- .2,\
- .15,\
- .1,\
- .05,}\
- \
- local level=1\
- \
- local function playGame()\
- local score=0\
- local lines=0\
- local initialLevel=level\
- local next=blocks[math.random(1,#blocks)]\
- \
- local pit={}\
- \
- \
- local heightAdjust=0\
- \
- if height<=19 then\
- heightAdjust=1\
- end\
- \
- \
- \
- local function drawScreen()\
- term.setTextColor(colors.white)\
- term.setBackgroundColor(colors.black)\
- term.clear()\
- \
- term.setTextColor(colors.black)\
- term.setBackgroundColor(colorass(colors.lightGray, colors.white))\
- term.setCursorPos(22,2)\
- term.write(\"Score\") --score\
- term.setCursorPos(22,5)\
- term.write(\"Level\") --level\
- term.setCursorPos(22,8)\
- term.write(\"Lines\") --lines\
- term.setCursorPos(22,12)\
- term.write(\"Next\") --next\
- \
- term.setCursorPos(21,1)\
- term.write(\" \")\
- term.setCursorPos(21,2)\
- term.write(\" \") --score\
- term.setCursorPos(21,3)\
- term.write(\" \")\
- term.setCursorPos(21,4)\
- term.write(\" \")\
- term.setCursorPos(21,5)\
- term.write(\" \") --level\
- term.setCursorPos(21,6)\
- term.write(\" \")\
- term.setCursorPos(21,7)\
- term.write(\" \")\
- term.setCursorPos(21,8)\
- term.write(\" \") --lines\
- term.setCursorPos(21,9)\
- term.write(\" \")\
- term.setCursorPos(21,10)\
- term.write(\" \")\
- term.setCursorPos(21,11)\
- term.write(\" \")\
- term.setCursorPos(21,12)\
- term.write(\" \") --next\
- term.setCursorPos(26,12)\
- term.write(\" \") --next\
- term.setCursorPos(21,13)\
- term.write(\" \")\
- term.setCursorPos(21,14)\
- term.write(\" \")\
- term.setCursorPos(21,15)\
- term.write(\" \")\
- term.setCursorPos(21,16)\
- term.write(\" \")\
- term.setCursorPos(21,17)\
- term.write(\" \")\
- term.setCursorPos(21,18)\
- term.write(\" \")\
- term.setCursorPos(21,19)\
- term.write(\" \")\
- term.setCursorPos(21,20)\
- term.write(\" \")\
- end\
- \
- local function updateNumbers()\
- term.setTextColor(colors.white)\
- term.setBackgroundColor(colors.black)\
- \
- term.setCursorPos(22,3)\
- term.write(lpad(score,5)) --score\
- term.setCursorPos(22,6)\
- term.write(lpad(level,5)) --level\
- term.setCursorPos(22,9)\
- term.write(lpad(lines,5)) --lines\
- end\
- \
- local function drawBlockAt(block,xp,yp,rot)\
- term.setTextColor(block.fg)\
- term.setBackgroundColor(block.bg)\
- for y=1,4 do\
- for x=1,4 do\
- if block[rot][y][x]==1 then\
- term.setCursorPos((xp+x)*2-3,yp+y-1-heightAdjust)\
- term.write(block.ch)\
- end\
- end\
- end\
- end\
- \
- local function eraseBlockAt(block,xp,yp,rot)\
- term.setTextColor(colors.white)\
- term.setBackgroundColor(colors.black)\
- for y=1,4 do\
- for x=1,4 do\
- if block[rot][y][x]==1 then\
- term.setCursorPos((xp+x)*2-3,yp+y-1-heightAdjust)\
- term.write(\" \")\
- end\
- end\
- end\
- end\
- \
- local function testBlockAt(block,xp,yp,rot)\
- for y=1,4 do\
- local ty=yp+y-1\
- for x=1,4 do\
- local tx=xp+x-1\
- if block[rot][y][x]==1 then\
- if tx>10 or tx<1 or ty>20 or pit[ty][tx]~=0 then\
- return true\
- end\
- end\
- end\
- end\
- end\
- \
- local function pitBlock(block,xp,yp,rot)\
- for y=1,4 do\
- for x=1,4 do\
- if block[rot][y][x]==1 then\
- pit[yp+y-1][xp+x-1]=block\
- end\
- end\
- end\
- end\
- \
- \
- local function clearPit()\
- for row=1,20 do\
- pit[row]={}\
- for col=1,10 do\
- pit[row][col]=0\
- end\
- end\
- end\
- \
- \
- \
- drawScreen()\
- updateNumbers()\
- \
- --declare & init the pit\
- clearPit()\
- \
- \
- \
- local halt=false\
- local dropSpeed=speedsByLevel[math.min(level,12)]\
- \
- \
- local curBlock=next\
- next=blocks[math.random(1,7)]\
- \
- local curX, curY, curRot=4, 1, 1\
- local dropTimer=os.startTimer(dropSpeed)\
- \
- drawBlockAt(next,11.5,15+heightAdjust,1)\
- drawBlockAt(curBlock,curX,curY,curRot)\
- \
- local function redrawPit()\
- for r=1+heightAdjust,20 do\
- term.setCursorPos(1,r-heightAdjust)\
- for c=1,10 do\
- if pit[r][c]==0 then\
- term.setTextColor(colors.black)\
- term.setBackgroundColor(colors.black)\
- term.write(\" \")\
- else\
- term.setTextColor(pit[r][c].fg)\
- term.setBackgroundColor(pit[r][c].bg)\
- term.write(pit[r][c].ch)\
- end\
- end\
- end\
- end\
- \
- local function hidePit()\
- for r=1+heightAdjust,20 do\
- term.setCursorPos(1,r-heightAdjust)\
- term.setTextColor(colors.black)\
- term.setBackgroundColor(colors.black)\
- term.write(\" \")\
- end\
- end\
- \
- local function msgBox(message)\
- local x=math.floor((17-#message)/2)\
- term.setBackgroundColor(colorass(colors.lightGray,colors.white))\
- term.setTextColor(colors.black)\
- term.setCursorPos(x,9)\
- term.write(\"+\"..string.rep(\"-\",#message+2)..\"+\")\
- term.setCursorPos(x,10)\
- term.write(\"|\")\
- term.setCursorPos(x+#message+3,10)\
- term.write(\"|\")\
- term.setCursorPos(x,11)\
- term.write(\"+\"..string.rep(\"-\",#message+2)..\"+\")\
- term.setTextColor(colors.white)\
- term.setBackgroundColor(colors.black)\
- term.setCursorPos(x+1,10)\
- term.write(\" \"..message..\" \")\
- end\
- \
- local function clearRows()\
- local rows={}\
- for r=1,20 do\
- local count=0\
- for c=1,10 do\
- if pit[r][c]~=0 then\
- count=count+1\
- else\
- break\
- end\
- end\
- if count==10 then\
- rows[#rows+1]=r\
- end\
- end\
- \
- if #rows>0 then\
- for i=1,4 do\
- sleep(.1)\
- for r=1,#rows do\
- r=rows[r]\
- term.setCursorPos(1,r-heightAdjust)\
- for c=1,10 do\
- term.setTextColor(pit[r][c].bg)\
- term.setBackgroundColor(pit[r][c].fg)\
- term.write(pit[r][c].ch)\
- end\
- end\
- sleep(.1)\
- for r=1,#rows do\
- r=rows[r]\
- term.setCursorPos(1,r-heightAdjust)\
- for c=1,10 do\
- term.setTextColor(pit[r][c].fg)\
- term.setBackgroundColor(pit[r][c].bg)\
- term.write(pit[r][c].ch)\
- end\
- end\
- end\
- --now remove the rows and drop everythign else\
- term.setBackgroundColor(colors.black)\
- for r=1,#rows do\
- r=rows[r]\
- term.setCursorPos(1,r-heightAdjust)\
- term.write(\" \")\
- end\
- sleep(.25)\
- for r=1,#rows do\
- table.remove(pit,rows[r])\
- table.insert(pit,1,{0,0,0,0,0,0,0,0,0,0})\
- end\
- redrawPit()\
- lines=lines+#rows\
- score=score+points[#rows]*math.min(level,20)\
- level=math.floor(lines/10)+initialLevel\
- dropSpeed=speedsByLevel[math.min(level,12)]\
- updateNumbers()\
- end\
- sleep(.25)\
- end\
- \
- local function blockFall()\
- local result = false\
- if testBlockAt(curBlock,curX,curY+1,curRot) then\
- pitBlock(curBlock,curX,curY,curRot)\
- --detect rows that clear\
- clearRows(rows)\
- \
- curBlock=next\
- curX=4\
- curY=1\
- curRot=1\
- if testBlockAt(curBlock,curX,curY,curRot) then\
- halt=true\
- end\
- drawBlockAt(curBlock,curX,curY,curRot)\
- eraseBlockAt(next,11.5,15+heightAdjust,1)\
- next=blocks[math.random(1,7)]\
- drawBlockAt(next,11.5,15+heightAdjust,1)\
- return true\
- else\
- eraseBlockAt(curBlock,curX,curY,curRot)\
- curY=curY+1\
- drawBlockAt(curBlock,curX,curY,curRot)\
- return false\
- end\
- end\
- \
- \
- while not halt do\
- local e={os.pullEvent()}\
- if e[1]==\"timer\" then\
- if e[2]==dropTimer then\
- blockFall()\
- dropTimer=os.startTimer(dropSpeed)\
- end\
- elseif e[1]==\"key\" then\
- local key=e[2]\
- local dx,dy,dr=0,0,0\
- if key==keys.left or key==keys.a then\
- dx=-1\
- elseif key==keys.right or key==keys.d then\
- dx=1\
- elseif key==keys.up or key==keys.w then\
- dr=1\
- elseif key==keys.down or key==keys.s then\
- while not blockFall() do end\
- dropTimer=os.startTimer(dropSpeed)\
- elseif key==keys.space then\
- hidePit()\
- msgBox(\"Paused\")\
- while ({os.pullEvent(\"key\")})[2]~=keys.space do end\
- redrawPit()\
- drawBlockAt(curBlock,curX,curY,curRot)\
- dropTimer=os.startTimer(dropSpeed)\
- end\
- if dx+dr~=0 then\
- if not testBlockAt(curBlock,curX+dx,curY+dy,(dr>0 and curRot%#curBlock+dr or curRot)) then\
- eraseBlockAt(curBlock,curX,curY,curRot)\
- curX=curX+dx\
- curY=curY+dy\
- curRot=dr==0 and curRot or (curRot%#curBlock+dr)\
- drawBlockAt(curBlock,curX,curY,curRot)\
- end\
- end\
- elseif e[1]==\"term_resize\" then\
- local w,h=term.getSize()\
- if h==20 then\
- heightAdjust=0\
- else\
- heightAdjust=1\
- end\
- redrawPit()\
- drawBlockAt(curBlock,curX,curY,curRot)\
- end\
- end\
- \
- msgBox(\"Game Over!\")\
- while true do\
- local _,k=os.pullEvent(\"key\")\
- if k==keys.space or k==keys.enter then\
- break\
- end\
- end\
- \
- level = math.min(level,9)\
- end\
- \
- \
- local selected=1\
- local playersDetected=false\
- \
- local function drawMenu()\
- term.setBackgroundColor(colors.black)\
- term.setTextColor(colorass(colors.red,colors.white))\
- term.clear()\
- \
- local cx,cy=math.floor(width/2),math.floor(height/2)\
- \
- term.setCursorPos(cx-6,cy-2)\
- term.write(\"F A L L I N G\")\
- \
- if playersDetected then\
- if selected==0 then\
- term.setTextColor(colorass(colors.blue,colors.black))\
- term.setBackgroundColor(colorass(colors.gray,colors.white))\
- else\
- term.setTextColor(colorass(colors.lightBlue,colors.white))\
- term.setBackgroundColor(colors.black)\
- end\
- term.setCursorPos(cx-12,cy)\
- term.write(\" Play head-to-head game! \")\
- end\
- \
- term.setCursorPos(cx-10,cy+1)\
- if selected==1 then\
- term.setTextColor(colorass(colors.blue,colors.black))\
- term.setBackgroundColor(colorass(colors.lightGray,colors.white))\
- else\
- term.setTextColor(colorass(colors.lightBlue,colors.white))\
- term.setBackgroundColor(colors.black)\
- end\
- term.write(\" Play from level: <\" .. level .. \"> \")\
- \
- term.setCursorPos(cx-3,cy+3)\
- if selected==2 then\
- term.setTextColor(colorass(colors.blue,colors.black))\
- term.setBackgroundColor(colorass(colors.lightGray,colors.white))\
- else\
- term.setTextColor(colorass(colors.lightBlue,colors.white))\
- term.setBackgroundColor(colors.black)\
- end\
- term.write(\" Quit \")\
- end\
- \
- \
- local function runMenu()\
- drawMenu()\
- \
- while true do\
- local event={os.pullEvent()}\
- if event[1]==\"key\" then\
- local key=event[2]\
- if key==keys.right or key==keys.d and selected==1 then\
- level=math.min(level+1,9)\
- drawMenu()\
- elseif key==keys.left or key==keys.a and selected==1 then\
- level=math.max(level-1,1)\
- drawMenu()\
- elseif key>=keys.one and key<=keys.nine and selected==1 then\
- level=(key-keys.one) + 1\
- drawMenu()\
- elseif key==keys.up or key==keys.w then\
- selected=selected-1\
- if selected==0 then\
- selected=2\
- end\
- drawMenu()\
- elseif key==keys.down or key==keys.s then\
- selected=selected%2+1\
- drawMenu()\
- elseif key==keys.enter or key==keys.space then\
- break --begin play!\
- end\
- end\
- end\
- end\
- \
- while true do\
- runMenu()\
- if selected==2 then\
- break\
- end\
- \
- playGame()\
- end\
- \
- \
- term.setTextColor(colors.white)\
- term.setBackgroundColor(colors.black)\
- term.clear()\
- term.setCursorPos(1,1)",
- monitor = "\
- local function printUsage()\
- \009print( \"Usage: monitor <name> <program> <arguments>\" )\
- \009return\
- end\
- \
- local tArgs = { ... }\
- if #tArgs < 2 then\
- \009printUsage()\
- \009return\
- end\
- \
- local sName = tArgs[1]\
- if peripheral.getType( sName ) ~= \"monitor\" then\
- \009print( \"No monitor named \".. sName )\
- \009return\
- end\
- \
- local sProgram = tArgs[2]\
- local sPath = shell.resolveProgram( sProgram )\
- if sPath == nil then\
- \009print( \"No such program: \"..sProgram )\
- \009return\
- end\
- \
- print( \"Running \"..sProgram..\" on monitor \"..sName )\
- \
- local monitor = peripheral.wrap( sName )\
- local previousTerm = term.redirect( monitor )\
- \
- local co = coroutine.create( function()\
- shell.run( sProgram, table.unpack( tArgs, 3 ) )\
- end )\
- \
- local function resume( ... )\
- local ok, param = coroutine.resume( co, ... )\
- if not ok then\
- \009printError( param )\
- end\
- return param\
- end\
- \
- local ok, param = pcall( function()\
- \009local sFilter = resume()\
- \009while coroutine.status( co ) ~= \"dead\" do\
- \009\009local tEvent = { os.pullEventRaw() }\
- \009\009if sFilter == nil or tEvent[1] == sFilter or tEvent[1] == \"terminate\" then\
- \009\009\009sFilter = resume( table.unpack( tEvent ) )\
- \009\009end\
- \009\009if coroutine.status( co ) ~= \"dead\" and (sFilter == nil or sFilter == \"mouse_click\") then\
- \009\009\009if tEvent[1] == \"monitor_touch\" and tEvent[2] == sName then\
- \009\009\009\009sFilter = resume( \"mouse_click\", 1, table.unpack( tEvent, 3 ) )\
- \009\009\009end\
- \009\009end\
- \009\009if coroutine.status( co ) ~= \"dead\" and (sFilter == nil or sFilter == \"term_resize\") then\
- \009\009\009if tEvent[1] == \"monitor_resize\" and tEvent[2] == sName then\
- \009\009\009\009sFilter = resume( \"term_resize\" )\
- \009\009\009end\
- \009\009end\
- \009end\
- end )\
- \
- term.redirect( previousTerm )\
- if not ok then\
- \009printError( param )\
- end\
- \009",
- [ ".settings" ] = {
- bkp = "yes",
- bg_choose = "if arg ~= nil then\
- fs.copy(\"user/backgrounds/dt, user/backgrounds/dt1\")\
- fs.delete(\"user/backgrounds/dt\")\
- fs.copy(\"user/backgrounds\"..arg[0], \"user/backgrounds/dt\")\
- shell.exit()\
- end\
- term.setCursorPos(10, 7)\
- term.setBackgroundColor(colors.lightBlue)\
- term.write(\"New BG: \")\
- term.setCursorPos(18, 7)\
- file = read()\
- if file ~= \"\" then\
- fs.copy(\"user/backgrounds/dt\", \"user/backgrounds/dt1\")\
- fs.delete(\"user/backgrounds/dt\")\
- fs.copy(\"user/backgrounds/\"..file, \"user/backgrounds/dt\")\
- end",
- restoreBkp = "term.setCursorPos(1, 1)\
- term.write(?Backup? This will delete all your files. [y/n] ?)\
- answer = read()\
- if answer ~= y then shell.exit() end\
- term.setCursorPos(1, 2)\
- if fs.exists(\"bkp/\") then\
- fs.delete(\"/system\")\
- fs.delete(\"/user\")\
- fs.delete(\"/startup\")\
- fs.copy(\"bkp/system\", \"/system\")\
- fs.copy(\"bkp/user\", \"/user\")\
- fs.copy(\"bkp/startup\", \"/startup\")\
- end",
- },
- [ "mouse.cfg" ] = "{\
- txt = 32768,\
- back = 2048,\
- }\
- {\
- txt = 32768,\
- back = 256,\
- }\
- {\
- txt = 32768,\
- back = 512,\
- }\
- {\
- txt = 32768,\
- back = 256,\
- }\
- {\
- txt = 32768,\
- back = 2048,\
- }\
- {\
- txt = 32768,\
- back = 2048,\
- }\
- {\
- txt = 32768,\
- back = 1,\
- }\
- {\
- back = 256,\
- button = 128,\
- off = 128,\
- }\
- {\
- folder = {\
- txt = \"[=]\",\
- bCol = \"blue\",\
- tCol = \"lightGray\",\
- },\
- audio = {\
- txt = \"(o)\",\
- bCol = \"red\",\
- tCol = \"yellow\",\
- },\
- disk = {\
- txt = \"[*]\",\
- bCol = \"green\",\
- tCol = \"lime\",\
- },\
- file = {},\
- back = {\
- txt = \" < \",\
- bCol = \"blue\",\
- tCol = \"lightGray\",\
- },\
- }\
- {\
- Paint = \"user/programs/paint\",\
- Edit = \"user/programs/edit\",\
- LIDE = \"user/programs/edit\",\
- DesktopBG = \"user/programs/.settings/bg_choose\",\
- }",
- viewer = "term.setBackgroundColor(colors.green)\
- term.clear()\
- if arg == nil then\
- print(\"No specified file.\")\
- shell.exit()\
- end\
- if fs.exists(arg[0]) == false then\
- print(\"No such file or directory.\")\
- shell.exit()\
- end\
- image = paintutils.loadImage(arg[0])\
- paintutils.drawImage(image, 1, 1)\
- while true do\
- event, button, x, y = os.pullEvent(\"mouse_click\")\
- if button == 1 then shell.exit() end\
- end",
- },
- },
- startup = "term.clear()\
- status, err = pcall shell.run(\"system/init\")\
- sleep(1)\
- log = fs.open(\"/system/dabos.log\", \"a\")\
- log.write(\"[\"..os.time()..\"] crashed or user terminated.\\n\")\
- log.close()\
- term.setBackgroundColor(colors.blue)\
- term.clear()\
- term.setCursorPos(1, 1)\
- term.setTextColor(colors.red)\
- print(\"DabOS crashed... =(\")\
- term.setCursorPos(1, 15)\
- print(err)\
- print(\"Restarting in 5 seconds...\")\
- sleep(5)\
- os.reboot()\
- \
- ",
- appstore = "local _jstr = [[\
- local base = _G\
- \
- -----------------------------------------------------------------------------\
- -- Module declaration\
- -----------------------------------------------------------------------------\
- \
- -- Public functions\
- \
- -- Private functions\
- local decode_scanArray\
- local decode_scanComment\
- local decode_scanConstant\
- local decode_scanNumber\
- local decode_scanObject\
- local decode_scanString\
- local decode_scanWhitespace\
- local encodeString\
- local isArray\
- local isEncodable\
- \
- -----------------------------------------------------------------------------\
- -- PUBLIC FUNCTIONS\
- -----------------------------------------------------------------------------\
- --- Encodes an arbitrary Lua object / variable.\
- -- @param v The Lua object / variable to be JSON encoded.\
- -- @return String containing the JSON encoding in internal Lua string format (i.e. not unicode)\
- function encode (v)\
- -- Handle nil values\
- if v==nil then\
- return \"null\"\
- end\
- \
- local vtype = base.type(v) \
- \
- -- Handle strings\
- if vtype=='string' then \
- return '\"' .. encodeString(v) .. '\"' -- Need to handle encoding in string\
- end\
- \
- -- Handle booleans\
- if vtype=='number' or vtype=='boolean' then\
- return base.tostring(v)\
- end\
- \
- -- Handle tables\
- if vtype=='table' then\
- local rval = {}\
- -- Consider arrays separately\
- local bArray, maxCount = isArray(v)\
- if bArray then\
- for i = 1,maxCount do\
- table.insert(rval, encode(v[i]))\
- end\
- else -- An object, not an array\
- for i,j in base.pairs(v) do\
- if isEncodable(i) and isEncodable(j) then\
- table.insert(rval, '\"' .. encodeString(i) .. '\":' .. encode(j))\
- end\
- end\
- end\
- if bArray then\
- return '[' .. table.concat(rval,',') ..']'\
- else\
- return '{' .. table.concat(rval,',') .. '}'\
- end\
- end\
- \
- -- Handle null values\
- if vtype=='function' and v==null then\
- return 'null'\
- end\
- \
- base.assert(false,'encode attempt to encode unsupported type ' .. vtype .. ':' .. base.tostring(v))\
- end\
- \
- \
- --- Decodes a JSON string and returns the decoded value as a Lua data structure / value.\
- -- @param s The string to scan.\
- -- @param [startPos] Optional starting position where the JSON string is located. Defaults to 1.\
- -- @param Lua object, number The object that was scanned, as a Lua table / string / number / boolean or nil,\
- -- and the position of the first character after\
- -- the scanned JSON object.\
- function decode(s, startPos)\
- startPos = startPos and startPos or 1\
- startPos = decode_scanWhitespace(s,startPos)\
- base.assert(startPos<=string.len(s), 'Unterminated JSON encoded object found at position in [' .. s .. ']')\
- local curChar = string.sub(s,startPos,startPos)\
- -- Object\
- if curChar=='{' then\
- return decode_scanObject(s,startPos)\
- end\
- -- Array\
- if curChar=='[' then\
- return decode_scanArray(s,startPos)\
- end\
- -- Number\
- if string.find(\"+-0123456789.e\", curChar, 1, true) then\
- return decode_scanNumber(s,startPos)\
- end\
- -- String\
- if curChar=='\"' or curChar==\"'\" then\
- return decode_scanString(s,startPos)\
- end\
- if string.sub(s,startPos,startPos+1)=='/*' then\
- return decode(s, decode_scanComment(s,startPos))\
- end\
- -- Otherwise, it must be a constant\
- return decode_scanConstant(s,startPos)\
- end\
- \
- --- The null function allows one to specify a null value in an associative array (which is otherwise\
- -- discarded if you set the value with 'nil' in Lua. Simply set t = { first=json.null }\
- function null()\
- return null -- so json.null() will also return null ;-)\
- end\
- -----------------------------------------------------------------------------\
- -- Internal, PRIVATE functions.\
- -- Following a Python-like convention, I have prefixed all these 'PRIVATE'\
- -- functions with an underscore.\
- -----------------------------------------------------------------------------\
- \
- --- Scans an array from JSON into a Lua object\
- -- startPos begins at the start of the array.\
- -- Returns the array and the next starting position\
- -- @param s The string being scanned.\
- -- @param startPos The starting position for the scan.\
- -- @return table, int The scanned array as a table, and the position of the next character to scan.\
- function decode_scanArray(s,startPos)\
- local array = {} -- The return value\
- local stringLen = string.len(s)\
- base.assert(string.sub(s,startPos,startPos)=='[','decode_scanArray called but array does not start at position ' .. startPos .. ' in string:\\n'..s )\
- startPos = startPos + 1\
- -- Infinite loop for array elements\
- repeat\
- startPos = decode_scanWhitespace(s,startPos)\
- base.assert(startPos<=stringLen,'JSON String ended unexpectedly scanning array.')\
- local curChar = string.sub(s,startPos,startPos)\
- if (curChar==']') then\
- return array, startPos+1\
- end\
- if (curChar==',') then\
- startPos = decode_scanWhitespace(s,startPos+1)\
- end\
- base.assert(startPos<=stringLen, 'JSON String ended unexpectedly scanning array.')\
- object, startPos = decode(s,startPos)\
- table.insert(array,object)\
- until false\
- end\
- \
- --- Scans a comment and discards the comment.\
- -- Returns the position of the next character following the comment.\
- -- @param string s The JSON string to scan.\
- -- @param int startPos The starting position of the comment\
- function decode_scanComment(s, startPos)\
- base.assert( string.sub(s,startPos,startPos+1)=='/*', \"decode_scanComment called but comment does not start at position \" .. startPos)\
- local endPos = string.find(s,'*/',startPos+2)\
- base.assert(endPos~=nil, \"Unterminated comment in string at \" .. startPos)\
- return endPos+2 \
- end\
- \
- --- Scans for given constants: true, false or null\
- -- Returns the appropriate Lua type, and the position of the next character to read.\
- -- @param s The string being scanned.\
- -- @param startPos The position in the string at which to start scanning.\
- -- @return object, int The object (true, false or nil) and the position at which the next character should be\
- -- scanned.\
- function decode_scanConstant(s, startPos)\
- local consts = { [\"true\"] = true, [\"false\"] = false, [\"null\"] = nil }\
- local constNames = {\"true\",\"false\",\"null\"}\
- \
- for i,k in base.pairs(constNames) do\
- --print (\"[\" .. string.sub(s,startPos, startPos + string.len(k) -1) ..\"]\", k)\
- if string.sub(s,startPos, startPos + string.len(k) -1 )==k then\
- return consts[k], startPos + string.len(k)\
- end\
- end\
- base.assert(nil, 'Failed to scan constant from string ' .. s .. ' at starting position ' .. startPos)\
- end\
- \
- --- Scans a number from the JSON encoded string.\
- -- (in fact, also is able to scan numeric +- eqns, which is not\
- -- in the JSON spec.)\
- -- Returns the number, and the position of the next character\
- -- after the number.\
- -- @param s The string being scanned.\
- -- @param startPos The position at which to start scanning.\
- -- @return number, int The extracted number and the position of the next character to scan.\
- function decode_scanNumber(s,startPos)\
- local endPos = startPos+1\
- local stringLen = string.len(s)\
- local acceptableChars = \"+-0123456789.e\"\
- while (string.find(acceptableChars, string.sub(s,endPos,endPos), 1, true)\
- and endPos<=stringLen\
- ) do\
- endPos = endPos + 1\
- end\
- local stringValue = 'return ' .. string.sub(s,startPos, endPos-1)\
- local stringEval = base.loadstring(stringValue)\
- base.assert(stringEval, 'Failed to scan number [ ' .. stringValue .. '] in JSON string at position ' .. startPos .. ' : ' .. endPos)\
- return stringEval(), endPos\
- end\
- \
- --- Scans a JSON object into a Lua object.\
- -- startPos begins at the start of the object.\
- -- Returns the object and the next starting position.\
- -- @param s The string being scanned.\
- -- @param startPos The starting position of the scan.\
- -- @return table, int The scanned object as a table and the position of the next character to scan.\
- function decode_scanObject(s,startPos)\
- local object = {}\
- local stringLen = string.len(s)\
- local key, value\
- base.assert(string.sub(s,startPos,startPos)=='{','decode_scanObject called but object does not start at position ' .. startPos .. ' in string:\\n' .. s)\
- startPos = startPos + 1\
- repeat\
- startPos = decode_scanWhitespace(s,startPos)\
- base.assert(startPos<=stringLen, 'JSON string ended unexpectedly while scanning object.')\
- local curChar = string.sub(s,startPos,startPos)\
- if (curChar=='}') then\
- return object,startPos+1\
- end\
- if (curChar==',') then\
- startPos = decode_scanWhitespace(s,startPos+1)\
- end\
- base.assert(startPos<=stringLen, 'JSON string ended unexpectedly scanning object.')\
- -- Scan the key\
- key, startPos = decode(s,startPos)\
- base.assert(startPos<=stringLen, 'JSON string ended unexpectedly searching for value of key ' .. key)\
- startPos = decode_scanWhitespace(s,startPos)\
- base.assert(startPos<=stringLen, 'JSON string ended unexpectedly searching for value of key ' .. key)\
- base.assert(string.sub(s,startPos,startPos)==':','JSON object key-value assignment mal-formed at ' .. startPos)\
- startPos = decode_scanWhitespace(s,startPos+1)\
- base.assert(startPos<=stringLen, 'JSON string ended unexpectedly searching for value of key ' .. key)\
- value, startPos = decode(s,startPos)\
- object[key]=value\
- until false -- infinite loop while key-value pairs are found\
- end\
- \
- --- Scans a JSON string from the opening inverted comma or single quote to the\
- -- end of the string.\
- -- Returns the string extracted as a Lua string,\
- -- and the position of the next non-string character\
- -- (after the closing inverted comma or single quote).\
- -- @param s The string being scanned.\
- -- @param startPos The starting position of the scan.\
- -- @return string, int The extracted string as a Lua string, and the next character to parse.\
- function decode_scanString(s,startPos)\
- base.assert(startPos, 'decode_scanString(..) called without start position')\
- local startChar = string.sub(s,startPos,startPos)\
- base.assert(startChar==\"'\" or startChar=='\"','decode_scanString called for a non-string')\
- local escaped = false\
- local endPos = startPos + 1\
- local bEnded = false\
- local stringLen = string.len(s)\
- repeat\
- local curChar = string.sub(s,endPos,endPos)\
- -- Character escaping is only used to escape the string delimiters\
- if not escaped then\
- if curChar=='\\\\' then\
- escaped = true\
- else\
- bEnded = curChar==startChar\
- end\
- else\
- -- If we're escaped, we accept the current character come what may\
- escaped = false\
- end\
- endPos = endPos + 1\
- base.assert(endPos <= stringLen+1, \"String decoding failed: unterminated string at position \" .. endPos)\
- until bEnded\
- local stringValue = 'return ' .. string.sub(s, startPos, endPos-1)\
- local stringEval = base.loadstring(stringValue)\
- base.assert(stringEval, 'Failed to load string [ ' .. stringValue .. '] in JSON4Lua.decode_scanString at position ' .. startPos .. ' : ' .. endPos)\
- return stringEval(), endPos \
- end\
- \
- --- Scans a JSON string skipping all whitespace from the current start position.\
- -- Returns the position of the first non-whitespace character, or nil if the whole end of string is reached.\
- -- @param s The string being scanned\
- -- @param startPos The starting position where we should begin removing whitespace.\
- -- @return int The first position where non-whitespace was encountered, or string.len(s)+1 if the end of string\
- -- was reached.\
- function decode_scanWhitespace(s,startPos)\
- local whitespace=\" \\n\\r\\t\"\
- local stringLen = string.len(s)\
- while ( string.find(whitespace, string.sub(s,startPos,startPos), 1, true) and startPos <= stringLen) do\
- startPos = startPos + 1\
- end\
- return startPos\
- end\
- \
- --- Encodes a string to be JSON-compatible.\
- -- This just involves back-quoting inverted commas, back-quotes and newlines, I think ;-)\
- -- @param s The string to return as a JSON encoded (i.e. backquoted string)\
- -- @return The string appropriately escaped.\
- function encodeString(s)\
- s = string.gsub(s,'\\\\','\\\\\\\\')\
- s = string.gsub(s,'\"','\\\\\"')\
- s = string.gsub(s,\"'\",\"\\\\'\")\
- s = string.gsub(s,'\\n','\\\\n')\
- s = string.gsub(s,'\\t','\\\\t')\
- return s\
- end\
- \
- -- Determines whether the given Lua type is an array or a table / dictionary.\
- -- We consider any table an array if it has indexes 1..n for its n items, and no\
- -- other data in the table.\
- -- I think this method is currently a little 'flaky', but can't think of a good way around it yet...\
- -- @param t The table to evaluate as an array\
- -- @return boolean, number True if the table can be represented as an array, false otherwise. If true,\
- -- the second returned value is the maximum\
- -- number of indexed elements in the array.\
- function isArray(t)\
- -- Next we count all the elements, ensuring that any non-indexed elements are not-encodable\
- -- (with the possible exception of 'n')\
- local maxIndex = 0\
- for k,v in base.pairs(t) do\
- if (base.type(k)=='number' and math.floor(k)==k and 1<=k) then -- k,v is an indexed pair\
- if (not isEncodable(v)) then return false end -- All array elements must be encodable\
- maxIndex = math.max(maxIndex,k)\
- else\
- if (k=='n') then\
- if v ~= table.getn(t) then return false end -- False if n does not hold the number of elements\
- else -- Else of (k=='n')\
- if isEncodable(v) then return false end\
- end -- End of (k~='n')\
- end -- End of k,v not an indexed pair\
- end -- End of loop across all pairs\
- return true, maxIndex\
- end\
- \
- --- Determines whether the given Lua object / table / variable can be JSON encoded. The only\
- -- types that are JSON encodable are: string, boolean, number, nil, table and json.null.\
- -- In this implementation, all other types are ignored.\
- -- @param o The object to examine.\
- -- @return boolean True if the object should be JSON encoded, false if it should be ignored.\
- function isEncodable(o)\
- local t = base.type(o)\
- return (t=='string' or t=='boolean' or t=='number' or t=='nil' or t=='table') or (t=='function' and o==null)\
- end\
- ]]\
- \
- local _api = [[\
- \
- local function contains(table, element)\
- for _, value in pairs(table) do\
- if value == element then\
- return true\
- end\
- end\
- return false\
- end\
- \
- local apiURL = \"http://appstore.olivercooper.me/api/\"\
- \
- local function checkHTTP()\
- if http then\
- return true\
- else\
- return false\
- end\
- end\
- \
- local function requireHTTP()\
- if checkHTTP() then\
- return true\
- else\
- error(\"The 'http' API is not enabled!\")\
- end\
- end\
- \
- function doRequest(command, subcommand, values)\
- values = values or {}\
- requireHTTP()\
- \
- local url = apiURL .. \"?command=\" .. command ..\"&subcommand=\" .. subcommand\
- for k, v in pairs(values) do\
- url = url .. \"&\" .. k .. \"=\" .. v\
- end\
- local request = http.get(url)\
- if request then\
- local response = request.readAll()\
- request.close()\
- if response == \"<h2>The server is too busy at the moment.</h2><p>Please reload this page few seconds later.</p>\" then\
- error(\"Server is too busy at the moment.\")\
- end\
- return textutils.unserialize(response)\
- end\
- return nil\
- end\
- \
- function getAllApplications()\
- return doRequest('application', 'all')\
- end\
- \
- function getTopCharts()\
- return doRequest('application', 'topcharts')\
- end\
- \
- function getApplicationsInCategory(name)\
- return doRequest('application', 'category', {name = name})\
- end\
- \
- function getFeaturedApplications()\
- return doRequest('application', 'featured')\
- end\
- \
- function getApplication(id)\
- return doRequest('application', 'get', {id = id})\
- end\
- \
- function getCategories(id)\
- return doRequest('application', 'categories')\
- end\
- \
- function addApplication(username, password, serializeddata, name, description, sdescription, category)\
- return doRequest('application', 'add', {username = username, password = password, serializeddata = serializeddata, name = name, description = description, sdescription = sdescription, category = category})\
- end\
- \
- function addChangeLogToApplication(id, username, password, changelog, version)\
- return doRequest('application', 'addchangelog', {id = id, username = username, password = password, changelog = changelog, version = version})\
- end\
- \
- function downloadApplication(id)\
- return doRequest('application', 'download', {id = id})\
- end\
- \
- function searchApplications(name)\
- return doRequest('application', 'search', {name = name})\
- end\
- \
- function getAllNews()\
- return doRequest('news', 'all')\
- end\
- \
- function getNews(id)\
- return doRequest('news', 'get', {id = id})\
- end\
- \
- function getInstalledApplications(id)\
- return doRequest('computer', 'get', {id = id})\
- end\
- \
- local function resolve( _sPath )\
- local sStartChar = string.sub( _sPath, 1, 1 )\
- if sStartChar == \"/\" or sStartChar == \"\\\\\" then\
- return fs.combine( \"\", _sPath )\
- else\
- return fs.combine( sDir, _sPath )\
- end\
- end\
- \
- function saveApplicationIcon(id, path)\
- local app = getApplication(id)\
- local icon = app.icon\
- local _fs = fs\
- if OneOS then\
- _fs = OneOS.FS\
- end\
- local h = _fs.open(path, 'w')\
- h.write(icon)\
- h.close()\
- end\
- --Downloads and installs an application\
- --id = the id of the application\
- --path = the path is the name of the folder/file it'll be copied too\
- --removeSpaces = removes spaces from the name (useful if its being run from the shell)\
- --alwaysFolder = be default if there is only one file it will save it as a single file, if true files will always be placed in a folder\
- --fullPath = if true the given path will not be changed, if false the program name will be appended\
- function installApplication(id, path, removeSpaces, alwaysFolder, fullPath)\
- local package = downloadApplication(id)\
- if type(package) ~= 'string' or #package == 0 then\
- error('The application did not download correctly or is empty. Try again.')\
- end\
- local pack = JSON.decode(package)\
- if pack then\
- \
- local _fs = fs\
- if OneOS then\
- _fs = OneOS.FS\
- end\
- local function makeFile(_path,_content)\
- sleep(0)\
- local file=_fs.open(_path,\"w\")\
- file.write(_content)\
- file.close()\
- end\
- local function makeFolder(_path,_content)\
- _fs.makeDir(_path)\
- for k,v in pairs(_content) do\
- if type(v)==\"table\" then\
- makeFolder(_path..\"/\"..k,v)\
- else\
- makeFile(_path..\"/\"..k,v)\
- end\
- end\
- end\
- \
- local app = getApplication(id)\
- local appName = app['name']\
- local keyCount = 0\
- for k, v in pairs(pack) do\
- keyCount = keyCount + 1\
- end\
- if removeSpaces then\
- appName = appName:gsub(\" \", \"\")\
- end\
- local location = path..'/'\
- if not fullPath then\
- location = location .. appName\
- end\
- if keyCount == 1 and not alwaysFolder then\
- makeFile(location, pack['startup'])\
- else\
- makeFolder(location, pack)\
- location = location .. '/startup'\
- end\
- \
- return location\
- else\
- error('The application appears to be corrupt. Try downloading it again.')\
- end\
- end\
- \
- function registerComputer(realid, username, password)\
- return doRequest('computer', 'register', {realid = realid, username = username, password = password})\
- end\
- \
- function getAllComments(type, id)\
- return doRequest('comment', 'get', {ctype = type, ctypeid = id})\
- end\
- \
- function getComment(id)\
- return doRequest('comment', 'get', {id = id})\
- end\
- \
- function deleteComment(id, username, password)\
- return doRequest('comment', 'delete', {id = id, username = username, password = password})\
- end\
- \
- function addComments()\
- return doRequest('comment', 'get', {id = id})\
- end\
- \
- function getUser()\
- return doRequest('user', 'get', {id = id})\
- end\
- \
- function registerUser(username, password, email, mcusername)\
- return doRequest('user', 'register', {username = username, password = password, email = email, mcusername = mcusername})\
- end\
- \
- function testConnection()\
- local ok = false\
- parallel.waitForAny(function()\
- if http and http.get(apiURL) then\
- ok = true\
- end\
- end,function()\
- sleep(10)\
- end)\
- return ok \
- end\
- ]]\
- \
- local function loadJSON()\
- local sName = 'JSON'\
- \
- local tEnv = {}\
- setmetatable( tEnv, { __index = _G } )\
- local fnAPI, err = loadstring(_jstr)\
- if fnAPI then\
- setfenv( fnAPI, tEnv )\
- fnAPI()\
- else\
- printError( err )\
- return false\
- end\
- \
- local tAPI = {}\
- for k,v in pairs( tEnv ) do\
- tAPI[k] = v\
- end\
- \
- _G[sName] = tAPI\
- return true\
- end\
- \
- local function loadAPI()\
- local sName = 'api'\
- \
- local tEnv = {}\
- setmetatable( tEnv, { __index = _G } )\
- local fnAPI, err = loadstring(_api)\
- if fnAPI then\
- setfenv( fnAPI, tEnv )\
- fnAPI()\
- else\
- printError( err )\
- return false\
- end\
- \
- local tAPI = {}\
- for k,v in pairs( tEnv ) do\
- tAPI[k] = v\
- end\
- \
- _G[sName] = tAPI\
- return true\
- end\
- \
- local tArgs = {...}\
- \
- loadJSON()\
- loadAPI()\
- \
- Settings = {\
- InstallLocation = '/', --if you have a folder you'd like programs to be installed to (for an OS) change this (e.g. /Programs/)\
- AlwaysFolder = false, --when false if there is only one file it will save it as a single file, if true files will always be placed in a folder\
- }\
- \
- local isMenuVisible = false\
- local currentPage = ''\
- local listItems = {}\
- \
- local isRunning = true\
- local currentScroll = 0\
- local maxScroll = 0\
- local pageHeight = 0\
- \
- local searchBox = nil\
- local featuredBannerTimer = nil\
- \
- Values = {\
- ToolbarHeight = 2,\
- }\
- \
- Current = {\
- CursorBlink = false,\
- CursorPos = {},\
- CursorColour = colours.black\
- }\
- \
- local function split(str, sep)\
- local sep, fields = sep or \":\", {}\
- local pattern = string.format(\"([^%s]+)\", sep)\
- str:gsub(pattern, function(c) fields[#fields+1] = c end)\
- return fields\
- end\
- --This is my drawing API, is is pretty much identical to what drives PearOS.\
- local _w, _h = term.getSize()\
- Drawing = {\
- \
- Screen = {\
- Width = _w,\
- Height = _h\
- },\
- \
- DrawCharacters = function (x, y, characters, textColour,bgColour)\
- Drawing.WriteStringToBuffer(x, y, characters, textColour, bgColour)\
- end,\
- \
- DrawBlankArea = function (x, y, w, h, colour)\
- Drawing.DrawArea (x, y, w, h, \" \", 1, colour)\
- end,\
- \
- DrawArea = function (x, y, w, h, character, textColour, bgColour)\
- --width must be greater than 1, other wise we get a stack overflow\
- if w < 0 then\
- w = w * -1\
- elseif w == 0 then\
- w = 1\
- end\
- \
- for ix = 1, w do\
- local currX = x + ix - 1\
- for iy = 1, h do\
- local currY = y + iy - 1\
- Drawing.WriteToBuffer(currX, currY, character, textColour, bgColour)\
- end\
- end\
- end,\
- \
- LoadImage = function(str)\
- local image = {\
- text = {},\
- textcol = {}\
- }\
- local tLines = split(str, '\\n')\
- for num, sLine in ipairs(tLines) do\
- table.insert(image, num, {})\
- table.insert(image.text, num, {})\
- table.insert(image.textcol, num, {})\
- \
- --As we're no longer 1-1, we keep track of what index to write to\
- local writeIndex = 1\
- --Tells us if we've hit a 30 or 31 (BG and FG respectively)- next char specifies the curr colour\
- local bgNext, fgNext = false, false\
- --The current background and foreground colours\
- local currBG, currFG = nil,nil\
- for i=1,#sLine do\
- local nextChar = string.sub(sLine, i, i)\
- if nextChar:byte() == 30 then\
- bgNext = true\
- elseif nextChar:byte() == 31 then\
- fgNext = true\
- elseif bgNext then\
- currBG = Drawing.GetColour(nextChar)\
- bgNext = false\
- elseif fgNext then\
- currFG = Drawing.GetColour(nextChar)\
- fgNext = false\
- else\
- if nextChar ~= \" \" and currFG == nil then\
- currFG = colours.white\
- end\
- image[num][writeIndex] = currBG\
- image.textcol[num][writeIndex] = currFG\
- image.text[num][writeIndex] = nextChar\
- writeIndex = writeIndex + 1\
- end\
- end\
- num = num+1\
- end\
- return image\
- end,\
- \
- DrawImage = function(_x,_y,tImage, w, h)\
- if tImage then\
- for y = 1, h do\
- if not tImage[y] then\
- break\
- end\
- for x = 1, w do\
- if not tImage[y][x] then\
- break\
- end\
- local bgColour = tImage[y][x]\
- local textColour = tImage.textcol[y][x] or colours.white\
- local char = tImage.text[y][x]\
- Drawing.WriteToBuffer(x+_x-1, y+_y-1, char, textColour, bgColour)\
- end\
- end\
- elseif w and h then\
- Drawing.DrawBlankArea(_x, _y, w, h, colours.lightGrey)\
- end\
- end,\
- \
- DrawCharactersCenter = function(x, y, w, h, characters, textColour,bgColour)\
- w = w or Drawing.Screen.Width\
- h = h or Drawing.Screen.Height\
- x = x or math.floor((w - #characters) / 2)\
- y = y or math.floor(h / 2)\
- \
- Drawing.DrawCharacters(x, y, characters, textColour, bgColour)\
- end,\
- \
- GetColour = function(hex)\
- local value = tonumber(hex, 16)\
- if not value then return nil end\
- value = math.pow(2,value)\
- return value\
- end,\
- \
- Clear = function (_colour)\
- _colour = _colour or colours.black\
- Drawing.ClearBuffer()\
- Drawing.DrawBlankArea(1, 1, Drawing.Screen.Width, Drawing.Screen.Height, _colour)\
- end,\
- \
- Buffer = {},\
- BackBuffer = {},\
- \
- DrawBuffer = function()\
- for y,row in pairs(Drawing.Buffer) do\
- for x,pixel in pairs(row) do\
- local shouldDraw = true\
- local hasBackBuffer = true\
- if Drawing.BackBuffer[y] == nil or Drawing.BackBuffer[y][x] == nil or #Drawing.BackBuffer[y][x] ~= 3 then\
- hasBackBuffer = false\
- end\
- if hasBackBuffer and Drawing.BackBuffer[y][x][1] == Drawing.Buffer[y][x][1] and Drawing.BackBuffer[y][x][2] == Drawing.Buffer[y][x][2] and Drawing.BackBuffer[y][x][3] == Drawing.Buffer[y][x][3] then\
- shouldDraw = false\
- end\
- if shouldDraw then\
- term.setBackgroundColour(pixel[3])\
- term.setTextColour(pixel[2])\
- term.setCursorPos(x, y)\
- term.write(pixel[1])\
- end\
- end\
- end\
- Drawing.BackBuffer = Drawing.Buffer\
- Drawing.Buffer = {}\
- end,\
- \
- ClearBuffer = function()\
- Drawing.Buffer = {}\
- end,\
- \
- Offset = {\
- X = 0,\
- Y = 0,\
- },\
- \
- SetOffset = function(x, y)\
- Drawing.Offset.X = x\
- Drawing.Offset.Y = y\
- end,\
- \
- ClearOffset = function()\
- Drawing.Offset = {\
- X = 0,\
- Y = 0,\
- }\
- end,\
- \
- WriteStringToBuffer = function (x, y, characters, textColour,bgColour)\
- for i = 1, #tostring(characters) do\
- local character = tostring(characters):sub(i,i)\
- Drawing.WriteToBuffer(x + i - 1, y, character, textColour, bgColour)\
- end\
- end,\
- \
- WriteToBuffer = function(x, y, character, textColour,bgColour)\
- x = x + Drawing.Offset.X\
- y = y + Drawing.Offset.Y\
- Drawing.Buffer[y] = Drawing.Buffer[y] or {}\
- Drawing.Buffer[y][x] = {character, textColour, bgColour}\
- end\
- \
- }\
- \
- SearchPage = {\
- X = 0,\
- Y = 0,\
- Width = 0,\
- Height = 3,\
- Text = \"\",\
- Placeholder = \"Search...\",\
- CursorPos = 1,\
- \
- Draw = function(self)\
- Drawing.DrawBlankArea(self.X+1, self.Y+1, self.Width-6, self.Height, colours.grey)\
- Drawing.DrawBlankArea(self.X, self.Y, self.Width-6, self.Height, colours.white)\
- \
- \
- Drawing.DrawBlankArea(self.X + self.Width - 5 + 1, self.Y + 1, 6, self.Height, colours.grey)\
- Drawing.DrawBlankArea(self.X + self.Width - 5, self.Y, 6, self.Height, colours.blue)\
- Drawing.DrawCharacters(self.X + self.Width - 3, self.Y + 1, \"GO\", colours.white, colours.blue)\
- \
- RegisterClick(self.X + self.Width - 5, self.Y, 6, self.Height, function() \
- ChangePage('Search Results', self.Text)\
- end)\
- \
- if self.Text == \"\" then\
- Drawing.DrawCharacters(self.X+1, self.Y+1, self.Placeholder, colours.lightGrey, colours.white)\
- else\
- Drawing.DrawCharacters(self.X+1, self.Y+1, self.Text, colours.black, colours.white)\
- end\
- \
- Current.CursorBlink = true\
- Current.CursorPos = {self.X+self.CursorPos, self.Y+3}\
- Current.CursorColour = colours.black\
- \
- end,\
- \
- Initialise = function(self)\
- local new = {} -- the new instance\
- setmetatable( new, {__index = self} )\
- new.Y = math.floor((Drawing.Screen.Height - 1 - new.Height) / 2)\
- new.X = 2\
- new.Width = Drawing.Screen.Width - 4\
- return new\
- end\
- }\
- \
- ListItem = {\
- X = 0,\
- Y = 0,\
- XMargin = 1,\
- YMargin = 1,\
- Width = 0,\
- Height = 6,\
- AppID = 0,\
- Title = '',\
- Author = '',\
- Rating = 0,\
- Description = {},\
- Icon = {},\
- Downloads = 0,\
- Category = '?',\
- Version = 1,\
- Type = 0, --0 = app list item, 1 = more info, 2 category\
- \
- CalculateWrapping = function(self, text)\
- \
- local numberOfLines = false\
- \
- if self.Type == 0 then\
- numberOfLines = 2\
- end\
- \
- local textWidth = self.Width - 8\
- \
- local lines = {''}\
- for word, space in text:gmatch('(%S+)(%s*)') do\
- local temp = lines[#lines] .. word .. space:gsub('\\n','')\
- if #temp > textWidth then\
- table.insert(lines, '')\
- end\
- if space:find('\\n') then\
- lines[#lines] = lines[#lines] .. word\
- \
- space = space:gsub('\\n', function()\
- table.insert(lines, '')\
- return ''\
- end)\
- else\
- lines[#lines] = lines[#lines] .. word .. space\
- end\
- end\
- \
- if not numberOfLines then\
- return lines\
- else\
- local _lines = {}\
- for i, v in ipairs(lines) do\
- _lines[i] = v\
- if i >= numberOfLines then\
- return _lines\
- end\
- end\
- return _lines\
- end\
- end,\
- Draw = function(self)\
- if self.Y + Drawing.Offset.Y >= Drawing.Screen.Height + 1 or self.Y + Drawing.Offset.Y + self.Height <= 1 then\
- return\
- end\
- --register clicks\
- --install\
- \
- local installPos = 1\
- \
- if self.Type == 1 then\
- installPos = 2\
- end\
- \
- RegisterClick(self.Width - 7, self.Y + Drawing.Offset.Y + installPos - 1, 9, 1, function()\
- Load(\"Installing App\", function()\
- api.installApplication(tonumber(self.AppID), Settings.InstallLocation..'/', true, Settings.AlwaysFolder)\
- --api.saveApplicationIcon(tonumber(self.AppID), Settings.InstallLocation..'/'..self.Title..\"/icon\")\
- end)\
- Load(\"Application Installed!\", function()\
- sleep(1)\
- end)\
- end)\
- \
- --more info\
- if self.Type == 0 then\
- RegisterClick(self.X, self.Y + Drawing.Offset.Y, self.Width, self.Height, function()\
- ChangePage('more-info',self.AppID)\
- end)\
- elseif self.Type == 2 then\
- RegisterClick(self.X, self.Y + Drawing.Offset.Y, self.Width, self.Height, function()\
- ChangePage('Category Items',self.Title)\
- end)\
- end\
- \
- Drawing.DrawBlankArea(self.X+1, self.Y+1, self.Width, self.Height, colours.grey)\
- Drawing.DrawBlankArea(self.X, self.Y, self.Width, self.Height, colours.white)\
- \
- --Drawing.DrawBlankArea(self.X+1, self.Y+1, 6, 4, colours.green)\
- Drawing.DrawCharacters(self.X + 8, self.Y + 1, self.Title, colours.black, colours.white)\
- if self.Type ~= 2 then\
- Drawing.DrawCharacters(self.X + 8, self.Y + 2, \"by \"..self.Author, colours.grey, colours.white)\
- Drawing.DrawCharacters(self.Width - 8, self.Y + installPos - 1, \" Install \", colours.white, colours.green)\
- end\
- \
- Drawing.DrawImage(self.X+1, self.Y+1, self.Icon, 4, 3)\
- \
- \
- if self.Type == 1 then\
- Drawing.DrawCharacters(self.X, self.Y + 6, \"Category\", colours.grey, colours.white)\
- Drawing.DrawCharacters(math.ceil(self.X+(8-#self.Category)/2), self.Y + 7, self.Category, colours.grey, colours.white)\
- \
- Drawing.DrawCharacters(self.X+1, self.Y + 9, \"Dwnlds\", colours.grey, colours.white)\
- Drawing.DrawCharacters(math.ceil(self.X+(8-#tostring(self.Downloads))/2), self.Y + 10, tostring(self.Downloads), colours.grey, colours.white)\
- \
- Drawing.DrawCharacters(self.X+1, self.Y + 12, \"Version\", colours.grey, colours.white)\
- Drawing.DrawCharacters(math.ceil(self.X+(8-#tostring(self.Version))/2), self.Y + 13, tostring(self.Version), colours.grey, colours.white)\
- \
- end\
- \
- if self.Type ~= 2 then\
- --draw the rating\
- local starColour = colours.yellow\
- local halfColour = colours.lightGrey\
- local emptyColour = colours.lightGrey\
- \
- local sX = self.X + 8 + #(\"by \"..self.Author) + 1\
- local sY = self.Y + 2\
- \
- local s1C = emptyColour\
- local s1S = \" \"\
- \
- local s2C = emptyColour\
- local s2S = \" \"\
- \
- local s3C = emptyColour\
- local s3S = \" \"\
- \
- local s4C = emptyColour\
- local s4S = \" \"\
- \
- local s5C = emptyColour\
- local s5S = \" \"\
- \
- if self.Rating >= .5 then\
- s1C = halfColour\
- s1S = \"#\"\
- end\
- \
- if self.Rating >= 1 then\
- s1C = starColour\
- s1S = \" \"\
- end\
- \
- \
- if self.Rating >= 1.5 then\
- s2C = halfColour\
- s2S = \"#\"\
- end\
- \
- if self.Rating >= 2 then\
- s2C = starColour\
- s2S = \" \"\
- end\
- \
- \
- if self.Rating >= 2.5 then\
- s3C = halfColour\
- s3S = \"#\"\
- end\
- \
- if self.Rating >= 3 then\
- s3C = starColour\
- s3S = \" \"\
- end\
- \
- \
- if self.Rating >= 3.5 then\
- s4C = halfColour\
- s4S = \"#\"\
- end\
- \
- if self.Rating >= 4 then\
- s4C = starColour\
- s4S = \" \"\
- end\
- \
- \
- if self.Rating >= 4.5 then\
- s5C = halfColour\
- s5S = \"#\"\
- end\
- \
- if self.Rating == 5 then\
- s5C = starColour\
- s5S = \" \"\
- end\
- \
- Drawing.DrawCharacters(sX, sY, s1S, starColour, s1C)\
- Drawing.DrawCharacters(sX + 2, sY, s2S, starColour, s2C)\
- Drawing.DrawCharacters(sX + 4, sY, s3S, starColour, s3C)\
- Drawing.DrawCharacters(sX + 6, sY, s4S, starColour, s4C)\
- Drawing.DrawCharacters(sX + 8, sY, s5S, starColour, s5C)\
- end\
- \
- local descPos = 2\
- \
- \
- \
- if self.Type == 1 then\
- descPos = 3\
- elseif self.Type == 2 then\
- descPos = 1\
- end\
- \
- for _,line in ipairs(self.Description) do\
- Drawing.DrawCharacters(self.X + 8, self.Y + descPos + _, line, colours.lightGrey, colours.white)\
- end\
- end,\
- Initialise = function(self, y, appid, title, icon, description, author, rating, version, category, downloads, Type)\
- Type = Type or 0\
- local new = {} -- the new instance\
- setmetatable( new, {__index = self} )\
- new.Y = y\
- new.Type = Type\
- new:UpdateSize()\
- new.AppID = appid\
- new.Title = title\
- new.Icon = Drawing.LoadImage(icon)\
- new.Icon[5] = nil\
- new.Description = new:CalculateWrapping(description)\
- new.Author = author\
- new.Rating = rating\
- new.Version = version\
- new.Category = category\
- new.Downloads = downloads\
- return new\
- end,\
- UpdateSize = function(self)\
- self.X = self.XMargin + 1\
- self.Width = Drawing.Screen.Width - 2 * self.XMargin - 2\
- \
- if self.Type == 1 then\
- self.Height = 15\
- end\
- end,\
- }\
- \
- Clicks = {\
- \
- }\
- \
- function RegisterClick(x, y, width, height, action)\
- table.insert(Clicks,{\
- X = x,\
- Y = y,\
- Width = width,\
- Height = height,\
- Action = action\
- })\
- end\
- \
- function Load(title, func)\
- Drawing.DrawBlankArea(1, 1, Drawing.Screen.Width+1, Drawing.Screen.Height+1, colours.lightGrey)\
- Drawing.DrawCharactersCenter(nil, Drawing.Screen.Height/2, nil, nil, title, colours.white, colours.lightGrey)\
- isLoading = true\
- parallel.waitForAny(function()\
- func()\
- isLoading = false\
- end, DisplayLoader)\
- end\
- \
- function DisplayLoader()\
- local maxStep = 100 -- about 10 seconds, timeout\
- local currStep = 0\
- local loadStep = 0\
- while isLoading do\
- local Y = Drawing.Screen.Height/2 + 2\
- local cX = Drawing.Screen.Width/2\
- \
- Drawing.DrawCharacters(cX-3, Y, ' ', colours.black, colours.grey)\
- Drawing.DrawCharacters(cX-1, Y, ' ', colours.black, colours.grey)\
- Drawing.DrawCharacters(cX+1, Y, ' ', colours.black, colours.grey)\
- Drawing.DrawCharacters(cX+3, Y, ' ', colours.black, colours.grey)\
- \
- if loadStep ~= -1 then\
- Drawing.DrawCharacters(cX-3 + (loadStep * 2), Y, ' ', colours.black, colours.white) \
- end\
- \
- loadStep = loadStep + 1\
- if loadStep >= 4 then\
- loadStep = -1\
- end\
- \
- currStep = currStep + 1\
- if currStep >= maxStep then\
- isLoading = false\
- error('Load timeout. Check your internet connection and try again. The server may also be down, try again in 10 minutes.')\
- end\
- \
- Drawing.DrawBuffer()\
- sleep(0.15)\
- end\
- end\
- \
- function ChangePage(title, arg)\
- ClearCurrentPage()\
- if title == 'Top Charts' then\
- LoadList(api.getTopCharts)\
- elseif title == 'Search Results' then\
- LoadList(function() return api.searchApplications(arg) end)\
- elseif title == \"Featured\" then\
- LoadFeatured()\
- elseif title == \"Categories\" then\
- LoadCategories()\
- elseif title == \"more-info\" then\
- LoadAboutApp(arg)\
- elseif title == \"Search\" then\
- LoadSearch()\
- elseif title == \"Category Items\" then\
- LoadList(function() return api.getApplicationsInCategory(arg) end)\
- end\
- \
- currentPage = title\
- \
- maxScroll = getMaxScroll()\
- end\
- \
- function LoadAboutApp(id)\
- Load(\"Loading Application\", function()\
- --ClearCurrentPage()\
- local app = api.getApplication(id)\
- local item = ListItem:Initialise(1, app.id, app.name, app.icon, app.description, app.user.username, app.stars, app.version, app.category, app.downloads, 1)\
- table.insert(listItems, item)\
- end)\
- \
- end\
- \
- function LoadFeatured()\
- Load(\"Loading\", function()\
- local tApps = api.getFeaturedApplications()\
- \
- --all items\
- for i, app in ipairs(tApps) do\
- local item = ListItem:Initialise(1+(i-1)*(ListItem.Height + 2), \
- app.id, app.name, app.icon, app.description,\
- app.user.username, app.stars, app.version,\
- app.category, app.downloads)\
- table.insert(listItems, item)\
- end\
- end) \
- end\
- \
- function LoadCategories()\
- Load(\"Loading\", function()\
- local tApps = api.getCategories()\
- local i = 1\
- for name, category in pairs(tApps) do\
- local item = ListItem:Initialise(1+(i-1)*(ListItem.Height + 2), \
- 0, name, category.icon, category.description, nil, nil, nil, nil, nil, 2)\
- table.insert(listItems, item)\
- i = i + 1\
- end\
- end)\
- end\
- \
- function LoadSearch(id)\
- local item = SearchPage:Initialise()\
- searchBox = item\
- --featuredBannerTimer = os.startTimer(5)\
- table.insert(listItems, item)\
- \
- end\
- \
- function ClearCurrentPage()\
- --listItems = {}\
- for i,v in ipairs(listItems) do listItems[i]=nil end\
- currentScroll = 0\
- searchBox = nil\
- featuredBannerTimer = nil\
- \
- Current.CursorBlink = false\
- Draw()\
- end\
- \
- function LoadList(func)\
- Load(\"Loading\", function()\
- local tApps = func()\
- if tApps == nil then\
- error('Can not connect to the App Store server.')\
- elseif type(tApps) ~= 'table' then\
- error('The server is too busy. Try again in a few minutes.')\
- end\
- for i, app in ipairs(tApps) do\
- local item = ListItem:Initialise(1+(i-1)*(ListItem.Height + 2), \
- app.id, app.name, app.icon, app.description,\
- app.user.username, app.stars, app.version,\
- app.category, app.downloads)\
- table.insert(listItems, item)\
- end\
- end)\
- end\
- \
- function Draw()\
- Clicks = {}\
- Drawing.Clear(colours.lightGrey)\
- DrawList()\
- DrawToolbar()\
- \
- --DrawScrollbar()\
- \
- Drawing.DrawBuffer()\
- \
- if Current.CursorPos and Current.CursorPos[1] and Current.CursorPos[2] then\
- term.setCursorPos(unpack(Current.CursorPos))\
- end\
- term.setTextColour(Current.CursorColour)\
- term.setCursorBlink(Current.CursorBlink)\
- end\
- \
- function DrawList()\
- Drawing.SetOffset(0, -currentScroll + 2)\
- for i, v in ipairs(listItems) do\
- v:Draw()\
- end\
- Drawing.ClearOffset()\
- \
- if getMaxScroll() ~= 0 then\
- DrawScrollBar(Drawing.Screen.Width, currentScroll, getMaxScroll())\
- end\
- end\
- \
- --[[\
- function DrawScrollbar()\
- \
- local scrollBarHeight = Drawing.Screen.Height - 1\
- local scrollBarPosition = 0\
- \
- if pageHeight > 0 and maxScroll > 0 then\
- scrollBarHeight = (Drawing.Screen.Height / pageHeight) * (Drawing.Screen.Height - 1)\
- scrollBarPosition = (currentScroll / pageHeight) * (Drawing.Screen.Height - 1)\
- end\
- \
- Drawing.DrawBlankArea(Drawing.Screen.Width, scrollBarPosition + 2, 1, scrollBarHeight, colours.blue)\
- \
- \
- Drawing.DrawCharacters(Drawing.Screen.Width, scrollBarPosition + 2, \"-\", colours.black,colours.white)\
- \
- Drawing.DrawCharacters(Drawing.Screen.Width-1, 2, \"+\", colours.black,colours.white)\
- \
- --Drawing.DrawBuffer()\
- \
- Drawing.DrawBlankArea(51, 2, 1, 18, colours.green)\
- \
- \
- end\
- ]]--\
- \
- function DrawScrollBar(x, current, max)\
- local fullHeight = Drawing.Screen.Height - 3\
- local barHeight = (fullHeight - max)\
- if barHeight < 5 then\
- barHeight = 5\
- end\
- Drawing.DrawBlankArea(x, 4, 1, fullHeight, colours.grey)\
- Drawing.DrawBlankArea(x, 4+current, 1, barHeight, colours.lightGrey)\
- end\
- \
- function DrawToolbar()\
- Drawing.DrawBlankArea(1, 1, Drawing.Screen.Width, 1, colours.white)\
- local items = {\
- {\
- active = false,\
- title = \"Featured\"\
- },\
- {\
- active = false,\
- title = \"Top Charts\"\
- },\
- {\
- active = false,\
- title = \"Categories\"\
- },\
- {\
- active = false,\
- title = \"Search\"\
- }\
- }\
- local itemsLength = 0\
- local itemsString = \"\"\
- for i, v in ipairs(items) do\
- itemsLength = itemsLength + #v.title + 3\
- itemsString = itemsString .. v.title .. \" | \"\
- end\
- itemsLength = itemsLength - 3\
- \
- local itemX = (Drawing.Screen.Width - itemsLength) / 2\
- \
- for i, v in ipairs(items) do\
- local border = \" | \"\
- if i == #items then\
- border = \"\"\
- end\
- Drawing.DrawCharacters(itemX, 1, v.title .. border, colours.blue, colours.white)\
- RegisterClick(itemX-1, 1, #v.title + 2, 1, function()\
- ChangePage(v.title)\
- end)\
- itemX = itemX + #(v.title .. border)\
- end\
- Drawing.DrawCharacters(Drawing.Screen.Width, 1, \"X\", colours.white, colours.red)\
- \
- RegisterClick(Drawing.Screen.Width, 1, 1, 1, function()\
- if OneOS then\
- OneOS.Close()\
- end\
- isRunning = false\
- term.setBackgroundColour(colours.black)\
- term.setTextColour(colours.white)\
- term.clear()\
- term.setCursorPos(1,1)\
- print = _print\
- print('Thanks for using the App Store!')\
- print('(c) oeed 2013 - 2014')\
- end)\
- end\
- \
- function getMaxScroll()\
- local totalHeight = 0\
- for i, v in ipairs(listItems) do\
- totalHeight = totalHeight + v.Height + 2\
- end\
- \
- local s = totalHeight - Drawing.Screen.Height + 2\
- \
- if s < 0 then\
- s = 0\
- end\
- \
- pageHeight = totalHeight\
- \
- return s\
- end\
- \
- function setScroll(iScroll)\
- maxScroll = getMaxScroll()\
- currentScroll = iScroll\
- if currentScroll < 0 then\
- currentScroll = 0\
- elseif currentScroll > maxScroll then\
- currentScroll = maxScroll\
- end\
- end\
- \
- function EventHandler()\
- while isRunning do\
- local event, arg, x, y = os.pullEvent()\
- \
- if event == \"mouse_scroll\" then\
- setScroll(currentScroll + (arg * 3))\
- Draw()\
- elseif event == \"timer\" then\
- if arg == featuredBannerTimer and currentPage == 'Featured' then\
- \
- --featuredBannerTimer = os.startTimer(5)\
- listItems[1]:NextPage()\
- Draw()\
- end\
- elseif event == \"char\" then\
- if currentPage == 'Search' then\
- searchBox.Text = searchBox.Text .. arg\
- searchBox.CursorPos = searchBox.CursorPos + 1\
- Draw()\
- end\
- elseif event == \"key\" then\
- if arg == keys.down then\
- setScroll(currentScroll + 3)\
- Draw()\
- \
- elseif arg == keys.up then\
- setScroll(currentScroll - 3)\
- Draw()\
- end\
- \
- if arg == keys.backspace and currentPage == 'Search' then\
- searchBox.Text = string.sub(searchBox.Text,0,#searchBox.Text-1)\
- searchBox.CursorPos = searchBox.CursorPos - 1\
- if searchBox.CursorPos < 1 then\
- searchBox.CursorPos = 1\
- end\
- Draw()\
- elseif arg == keys.enter and currentPage == 'Search' then\
- ChangePage('Search Results', searchBox.Text)\
- Draw()\
- end\
- \
- elseif event == \"mouse_click\" then\
- local clicked = false\
- for i = 1, #Clicks do\
- local v = Clicks[(#Clicks - i) + 1]\
- if not clicked and x >= v.X and (v.X + v.Width) > x and y >= v.Y and (v.Y + v.Height) > y then\
- clicked = true\
- \
- local iMV = isMenuVisible\
- v:Action()\
- \
- if iMV == isMenuVisible then\
- isMenuVisible = false\
- end\
- \
- Draw()\
- end\
- end\
- \
- if not clicked then\
- isMenuVisible = false\
- Draw()\
- end\
- end\
- \
- \
- \
- end\
- end\
- \
- function TidyPath(path)\
- if fs.exists(path) and fs.isDir(path) then\
- path = path .. '/'\
- end\
- \
- path, n = path:gsub(\"//\", \"/\")\
- while n > 0 do\
- path, n = path:gsub(\"//\", \"/\")\
- end\
- return path\
- end\
- \
- function Initialise()\
- if tArgs and tArgs[1] then\
- if tArgs[1] == 'install' and tArgs[2] and tonumber(tArgs[2]) then\
- print('Connecting...')\
- if api.testConnection() then\
- print('Downloading program...')\
- local path = tArgs[3] or shell.dir()\
- local location = api.installApplication(tonumber(tArgs[2]), path, true)\
- if location then\
- print('Program installed!')\
- print(\"Type '\"..TidyPath(location)..\"' to run it.\")\
- else\
- printError('Download failed. Check the ID and try again.')\
- end\
- else\
- printError('Could not connect to the App Store.')\
- printError('Check your connection and try again.')\
- end\
- elseif tArgs[1] == 'submit' and tArgs[2] and fs.exists(shell.resolve(tArgs[2])) then\
- print('Packaging...')\
- local pkg = Package(shell.resolve(tArgs[2]))\
- if pkg then\
- print('Connecting...')\
- if api.testConnection() then\
- print('Uploading...')\
- local str = JSON.encode(pkg)\
- str = str:gsub(\"\\\\'\",\"'\")\
- local h = http.post('http://appstore.olivercooper.me/submitPreupload.php', \
- \"file=\"..textutils.urlEncode(str));\
- if h then\
- local id = h.readAll()\
- if id:sub(1,2) == 'OK' then\
- print('Your program has been uploaded.')\
- print('It\\'s unique ID is: '..id:sub(3))\
- print('Go to ccappstore.com/submit/ and select \"In Game\" as the upload option and enter the above code.')\
- else\
- printError('The server rejected the file. Try again or PM oeed. ('..h.getResponseCode()..' error)')\
- end\
- else\
- printError('Could not submit file.')\
- end\
- else\
- printError('Could not connect to the App Store.')\
- printError('Check your connection and try again.')\
- end\
- end\
- else\
- print('Useage: appstore install <app id> <path (optional)>')\
- print('Or: appstore submit <path>') \
- end\
- else\
- Load('Connecting', api.testConnection)\
- ChangePage('Top Charts')\
- Draw()\
- EventHandler()\
- end\
- end\
- \
- function addFile(package, path, name)\
- if name == '.DS_Store' or shell.resolve(path) == shell.resolve(shell.getRunningProgram()) then\
- return package\
- end\
- local h = fs.open(path, 'r')\
- if not h then\
- error('Failed reading file: '..path)\
- end\
- package[name] = h.readAll()\
- h.close()\
- return package\
- end\
- \
- function addFolder(package, path, master)\
- local subPkg = {}\
- \
- if path:sub(1,4) == '/rom' then\
- return package\
- end\
- for i, v in ipairs(fs.list(path)) do\
- if fs.isDir(path..'/'..v) then\
- subPkg = addFolder(subPkg, path..'/'..v)\
- else\
- subPkg = addFile(subPkg, path..'/'..v, v)\
- end\
- end\
- \
- if master then\
- package = subPkg\
- else\
- package[fs.getName(path)] = subPkg\
- end\
- return package\
- end\
- \
- function Package(path)\
- local pkg = {}\
- if fs.isDir(path) then\
- pkg = addFolder(pkg, path, true)\
- else\
- pkg = addFile(pkg, path, 'startup')\
- end\
- if not pkg['startup'] then\
- print('You must have a file named startup in your program. This is the file used to start the program.')\
- else\
- return pkg\
- end\
- end\
- \
- if term.isColor and term.isColor() then\
- local httpTest = nil\
- if http then\
- httpTest = true-- http.get('http://appstore.olivercooper.me/api/')\
- end\
- if httpTest == nil then\
- print = _print\
- term.setBackgroundColor(colours.grey)\
- term.setTextColor(colours.white)\
- term.clear()\
- term.setCursorPos(3, 3)\
- \
- print(\"Could not connect to the App Store server!\\n\\n\")\
- \
- term.setTextColor(colours.white)\
- print(\"Try the following steps:\")\
- term.setTextColor(colours.lightGrey)\
- print(' - Ensure you have enabled the HTTP API')\
- print(' - Check your internet connection is working')\
- print(' - Retrying again in 10 minutes')\
- print(' - Get assistance on the forum page')\
- print()\
- print()\
- print()\
- term.setTextColor(colours.white)\
- print(\" Click anywhere to exit...\")\
- os.pullEvent(\"mouse_click\")\
- OneOS.Close()\
- \
- else\
- \
- -- Run main function\
- local _, err = pcall(Initialise)\
- if err then\
- print = _print\
- term.setBackgroundColor(colours.lightGrey)\
- term.setTextColor(colours.white)\
- term.clear()\
- \
- \
- term.setBackgroundColor(colours.grey)\
- term.setCursorPos(1, 2)\
- term.clearLine()\
- term.setCursorPos(1, 3)\
- term.clearLine()\
- term.setCursorPos(1, 4)\
- term.clearLine()\
- term.setCursorPos(3, 3)\
- \
- print(\"The ComputerCraft App Store has crashed!\\n\\n\")\
- \
- term.setBackgroundColour(colours.lightGrey)\
- print(\"Try repeating what you just did, if this is the second time you've seen this message go to\")\
- term.setTextColour(colours.black)\
- print(\"http://appstore.olivercooper.me/help/crash/\\n\")\
- term.setTextColour(colours.white) \
- print(\"The error was:\")\
- \
- term.setTextColour(colours.black)\
- print(\" \" .. tostring(err) .. \"\\n\\n\")\
- \
- term.setTextColour(colours.white)\
- print(\" Click anywhere to exit...\")\
- os.pullEvent(\"mouse_click\")\
- if OneOS then\
- OneOS.Close()\
- end\
- term.setTextColour(colours.white)\
- term.setBackgroundColour(colours.black)\
- term.clear()\
- term.setCursorPos(1,1)\
- end\
- end\
- else\
- print('The App Store requires an Advanced (gold) Computer!')\
- end",
- exit = "shell.run(\"startup\")",
- }
- local function run(tArgs)
- local fnFile, err = loadstring(files['startup'], 'startup')
- if err then
- error(err)
- end
- local function split(str, pat)
- local t = {}
- local fpat = "(.-)" .. pat
- local last_end = 1
- local s, e, cap = str:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(t,cap)
- end
- last_end = e+1
- s, e, cap = str:find(fpat, last_end)
- end
- if last_end <= #str then
- cap = str:sub(last_end)
- table.insert(t, cap)
- end
- return t
- end
- local function resolveTreeForPath(path, single)
- local _files = files
- local parts = split(path, '/')
- if parts then
- for i, v in ipairs(parts) do
- if #v > 0 then
- if _files[v] then
- _files = _files[v]
- else
- _files = nil
- break
- end
- end
- end
- elseif #path > 0 and path ~= '/' then
- _files = _files[path]
- end
- if not single or type(_files) == 'string' then
- return _files
- end
- end
- local oldFs = fs
- local env
- env = {
- fs = {
- list = function(path)
- local list = {}
- if fs.exists(path) then
- list = fs.list(path)
- end
- for k, v in pairs(resolveTreeForPath(path)) do
- if not fs.exists(path .. '/' ..k) then
- table.insert(list, k)
- end
- end
- return list
- end,
- exists = function(path)
- if fs.exists(path) then
- return true
- elseif resolveTreeForPath(path) then
- return true
- else
- return false
- end
- end,
- isDir = function(path)
- if fs.isDir(path) then
- return true
- else
- local tree = resolveTreeForPath(path)
- if tree and type(tree) == 'table' then
- return true
- else
- return false
- end
- end
- end,
- isReadOnly = function(path)
- if not fs.isReadOnly(path) then
- return false
- else
- return true
- end
- end,
- getName = fs.getName,
- getSize = fs.getSize,
- getFreespace = fs.getFreespace,
- makeDir = fs.makeDir,
- move = fs.move,
- copy = fs.copy,
- delete = fs.delete,
- combine = fs.combine,
- open = function(path, mode)
- if fs.exists(path) then
- return fs.open(path, mode)
- elseif type(resolveTreeForPath(path)) == 'string' then
- local handle = {close = function()end}
- if mode == 'r' then
- local content = resolveTreeForPath(path)
- handle.readAll = function()
- return content
- end
- local line = 1
- local lines = split(content, '\n')
- handle.readLine = function()
- if line > #lines then
- return nil
- else
- return lines[line]
- end
- line = line + 1
- end
- return handle
- else
- error('Cannot write to read-only file (compilr archived).')
- end
- else
- return fs.open(path, mode)
- end
- end
- },
- io = {
- input = io.input,
- output = io.output,
- type = io.type,
- close = io.close,
- write = io.write,
- flush = io.flush,
- lines = io.lines,
- read = io.read,
- open = function(path, mode)
- if fs.exists(path) then
- return io.open(path, mode)
- elseif type(resolveTreeForPath(path)) == 'string' then
- local content = resolveTreeForPath(path)
- local f = fs.open(path, 'w')
- f.write(content)
- f.close()
- if mode == 'r' then
- return io.open(path, mode)
- else
- error('Cannot write to read-only file (compilr archived).')
- end
- else
- return io.open(path, mode)
- end
- end
- },
- loadfile = function( _sFile )
- local file = env.fs.open( _sFile, "r" )
- if file then
- local func, err = loadstring( file.readAll(), fs.getName( _sFile ) )
- file.close()
- return func, err
- end
- return nil, "File not found: ".._sFile
- end,
- dofile = function( _sFile )
- local fnFile, e = env.loadfile( _sFile )
- if fnFile then
- setfenv( fnFile, getfenv(2) )
- return fnFile()
- else
- error( e, 2 )
- end
- end
- }
- setmetatable( env, { __index = _G } )
- local tAPIsLoading = {}
- env.os.loadAPI = function( _sPath )
- local sName = fs.getName( _sPath )
- if tAPIsLoading[sName] == true then
- printError( "API "..sName.." is already being loaded" )
- return false
- end
- tAPIsLoading[sName] = true
- local tEnv = {}
- setmetatable( tEnv, { __index = env } )
- local fnAPI, err = env.loadfile( _sPath )
- if fnAPI then
- setfenv( fnAPI, tEnv )
- fnAPI()
- else
- printError( err )
- tAPIsLoading[sName] = nil
- return false
- end
- local tAPI = {}
- for k,v in pairs( tEnv ) do
- tAPI[k] = v
- end
- env[sName] = tAPI
- tAPIsLoading[sName] = nil
- return true
- end
- env.shell = shell
- setfenv( fnFile, env )
- fnFile(unpack(tArgs))
- end
- local function extract()
- local function node(path, tree)
- if type(tree) == 'table' then
- fs.makeDir(path)
- for k, v in pairs(tree) do
- node(path .. '/' .. k, v)
- end
- else
- local f = fs.open(path, 'w')
- if f then
- f.write(tree)
- f.close()
- end
- end
- end
- node('', files)
- end
- local tArgs = {...}
- if #tArgs == 1 and tArgs[1] == '--extract' then
- extract()
- else
- run(tArgs)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement