-
Notifications
You must be signed in to change notification settings - Fork 0
/
main
218 lines (214 loc) · 8.19 KB
/
main
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
-- Viewer
-- A comprehensive peripheral visualizer for Minecraft
-- Debug tool
local function logfile(input)
if not fs.exists("/logs") then
fs.makeDir("/logs")
else
local file = fs.open("/logs/viewer.log", "a")
file.write("[Main] "..input.."\n")
file.close()
end
end
local function getSettings()
-- Retrieve settings from saved file, or if none exists, create settings from default list
if not fs.exists("settings") then
-- Retrieve up to date settings from GitHub
logfile("Main settings not found!")
logfile("Using default settings; Running updater")
shell.run("install")
else
logfile("Main settings found!")
end
-- Load settings
os.loadAPI("settings")
logfile("Main settings loaded")
end
local function getList()
local pList = {}
logfile(#peripheral.getNames().." available peripherals found:")
for num,pName in ipairs(peripheral.getNames()) do
--[[ Debug code to retrieve list of connected peripherals and save to a file
local pFile = fs.open("list", "a")
pFile.write(peripheral.getType(pName)..' = ""\n')
pFile.close()
-- End debug code]]
local p = peripheral.wrap(pName)
pList[num] = {
name = pName,
label = peripheral.getType(pName),
methods = p.listMethods and p.listMethods() or "unknown",
fluids = p.getTankInfo and p.getTankInfo("unknown") or "none",
inventory = p.getInventoryName and {
inventory_name = p.getInventoryName and p.getInventoryName() or "none",
max_slots = p.getInventorySize and p.getInventorySize() or "none",
slots_used = p.getAllStacks and (function() local count = 0 for _ in pairs(p.getAllStacks()) do count = count + 1 end return count end)() or "none",
items_stored = p.getAllStacks and p.getAllStacks() or "none"
} or "none",
energy = p.getEnergyStored and {
max = p.getMaxEnergyStored() or "none",
stored = p.getEnergyStored() or "none"
} or "none",
disk = (peripheral.getType(pName) == "drive") and {
label = disk.getLabel(pName),
id = (disk.getID(pName) ~= nil) and disk.getID(pName) or "none",
is_present = tostring(disk.isPresent(pName)),
has_data = (disk.hasData(pName) ~= nil) and tostring(disk.hasData(pName)) or "false",
has_audio = disk.hasAudio(pName) and "true" or "false",
song_title = disk.hasAudio(pName) and disk.getAudioTitle(pName) or "none"
} or "none"
}
logfile(" Functions for '"..pName.."' loaded successfully!")
end
return pList
end
-------------------------
-- Main code execution --
-------------------------
-- Variables
local pages = {
"general",
"fluids",
"inventory",
"energy",
"disk"
}
local num = 1
local fluidNum = 1
local page = pages[1]
local popup = false
-- Clear log files
if not fs.exists("/logs") then
fs.mkdir("/logs")
else
if fs.exists("/logs/viewer.log") then
fs.delete ("/logs/viewer.log")
end
end
-- Check for dependencies
if not http then
logfile("HTTP API not enabled! Please enable the HTTP API in the ComputerCraft config file, or ask your server administrator to do so.")
print("HTTP API not enabled! Please enable the HTTP API in\nthe ComputerCraft config file, or ask your server/nadministrator to do so.")
return
end
-- Load bundled APIs
logfile("Reloading APIs")
os.unloadAPI("settings")
os.loadAPI("settings")
os.unloadAPI("alias")
os.loadAPI("alias")
os.unloadAPI("gui")
os.loadAPI("gui")
logfile("Reloaded APIs successfully!")
-- Get list of attached peripherals
local list = getList()
-- Main execution loop
while true do
local termX, termY = term.getSize()
local menu = (settings.gui.menuLocation == "top") and 1 or termY
gui.drawScreen(list, num, page, fluidNum)
local events = {os.pullEvent()}
-- Check for peripheral attach/detach events
if events[1] == "peripheral" then
list = getList()
local pName = events[2]
logfile("Peripheral addition detected.")
logfile("Peripheral added: "..pName)
logfile("Peripheral type: "..peripheral.getType(pName))
elseif events[1] == "peripheral_detach" then
list = getList()
local pName = events[2]
logfile("Peripheral removal detected.")
logfile("Peripheral removed: "..pName)
elseif events[1] == "mouse_click" then
-- Debug for mouse click position
term.setBackgroundColor(settings.gui.menuBarColor)
term.setTextColor(settings.gui.menuTextColor)
term.setCursorPos(termX - string.len(events[3].."x, "..events[4].."y"), settings.gui.menuLocation == "top" and termY or 1)
write(events[3].."x, "..events[4].."y")
term.setBackgroundColor(colors.black)
-- Debug for mouse click position
-- Check location of mouse click, and act accordingly
if events[2] == 1 then
-- Left click
if events[3] == termX and events[4] == 1 then
-- Close program
local opt = ""
gui.drawPopup({"INFO", "Thank you for using Viewer", "Please feel free to view my Github page", "https://github.com/thatcraniumguy/"})
term.setCursorPos(1,1)
term.setTextColor(colors.white)
term.setBackgroundColor(colors.black)
term.clear()
return
elseif events[3] >= 1 and events[3] <= 14 and events[4] >= 4 and events[4] <= termY - 1 then
-- Peripheral selection
selection = (events[4] - 3)
if selection > #list then
-- Skip number change
else
-- Change number
num = selection
end
elseif events[3] >= 23 and events[3] <= (16 + string.len("Page: "..page)) and events[4] == menu then
-- Change info section page
local popupTable = pages
local opt = page
table.insert(popupTable, 1, "Information page ")
page = gui.drawPopup(popupTable, opt)
table.remove(popupTable, 1)
elseif page == "fluids" and list[num][page][fluidNum] then
if events[3] >= 27 and events[3] <= (string.len(list[num][page][fluidNum].contents.rawName) + 26) and events[4] == 8 then
-- Change fluid number page if needed
local fluidOpts = {"Selected "}
for i = 1, #list[num][page] do
table.insert(fluidOpts, i + 1, list[num][page][i].contents.rawName)
end
fluidNum = gui.drawPopup(fluidOpts, fluidNum)
for i = 1, #list[num][page] do
if list[num][page][i].contents.rawName == fluidNum then
fluidNum = i
break
end
end
end
else
-- Repeat loop
end
elseif events[2] == 2 then
-- Right click
end
elseif events[1] == "mouse_scroll" then
-- Mouse scrolled
if events[4] == 1 then
-- Up
num = num - 1
elseif events[4] == -1 then
-- Down
num = num + 1
end
-- If the number is higher or lower than the list of peripherals, then return it to the end or start
if num > #list then
num = #list
elseif num < 1 then
num = 1
end
elseif events[1] == "key" then
-- Handle keypresses
if events[2] == 200 then
num = num - 1
elseif events[2] == 208
num = num + 1
end
-- If the number is higher or lower than the list of peripherals, then return it to the end or start
if num > #list then
num = #list
elseif num < 1 then
num = 1
end
else
-- Ignore remaining events
end
end
----------------
-- DEBUG CODE --
----------------