-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstaller.lua
137 lines (109 loc) · 3.36 KB
/
installer.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
local REPO_URL = "https://raw.github.com/nburek/ComputerCraftCode/master/";
local INSTALL_DIR = "/CCC/";
local programList = {length=0};
local selected = {length=0};
local currentSelection = 1;
local scroll = 0;
local UP_ARROW = 200;
local DOWN_ARROW = 208;
local SPACEBAR = 57;
local ENTER = 28;
local BACKSPACE = 14;
local tX = 39;
local tY = 13;
-- --------------------------------- --
-- UTILITY FUNCTIONS --
-- --------------------------------- --
function getWebpage(url)
ok, data = pcall(function() return http.get(url) end);
return ok, data;
end
function printMenu()
term.setCursorBlink(false);
term.clear();
term.setCursorPos(1,1);
write("Select Programs");
for i=1,(tY-1) do
if (i <= programList.length) then
term.setCursorPos(1,(i+1));
write("[" .. selected[i+scroll] .. "] " .. programList[i+scroll]);
end
end
term.setCursorPos(2, (1 + currentSelection - scroll));
term.setCursorBlink(true);
end
function installProgram(programName)
if (not fs.exists(INSTALL_DIR)) then fs.makeDir(INSTALL_DIR); end
ok, data = getWebpage(REPO_URL .. programName .. ".lua");
if ((not ok) or (data == nil)) then
print("Could not download " .. programName .. " from the repo.");
return;
end
if ( fs.exists(INSTALL_DIR .. programName)) then fs.delete(INSTALL_DIR .. programName); end
local f = fs.open(INSTALL_DIR .. programName, "w");
f.write(data.readAll());
f.close();
data.close();
print("Installed " .. programName);
end
-- --------------------------------- --
-- STARTING THE PROGRAM... --
-- --------------------------------- --
term.clear();
term.setCursorPos(1,1);
tX, tY = term.getSize();
if not http then
print("HTTP is not enabled on this server.");
return;
end;
ok, data = getWebpage(REPO_URL .. "programs.txt");
if (not ok) then
print("Unable to connect to the remote repository.");
print("This may be because github.com is not white-listed on this server.");
return;
end
local line = data.readLine();
while (line ~= nil) do
programList.length = programList.length + 1;
programList[programList.length] = line;
selected.length = selected.length + 1;
selected[selected.length] = " ";
line = data.readLine();
end
while true do
printMenu();
event, keycode = os.pullEvent("key");
if (keycode == UP_ARROW) then
if (currentSelection ~= 1) then
if ((currentSelection-scroll) == 1) then
scroll = scroll - 1;
end
currentSelection = currentSelection - 1;
end
elseif (keycode == DOWN_ARROW) then
if (currentSelection < programList.length) then
if ((currentSelection-scroll+1) == tY) then
scroll = scroll + 1;
end
currentSelection = currentSelection + 1;
end
elseif (keycode == SPACEBAR) then
if (selected[currentSelection] == " ") then
selected[currentSelection] = "X";
else
selected[currentSelection] = " ";
end;
elseif (keycode == ENTER) then
term.clear();
term.setCursorPos(1,1);
for i=1,programList.length do
if (selected[i] == "X") then installProgram(programList[i]); end
end
return;
elseif (keycode == BACKSPACE) then
term.clear();
term.setCursorPos(1,1);
return;
end
os.sleep(0.1);
end