Skip to content

Instantly share code, notes, and snippets.

@thatcraniumguy
Last active August 29, 2015 14:21
Embed
What would you like to do?
-- 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) == "disk") and {
label = p.getLabel(),
id = p.getID(),
isPresent = p.isPresent(),
hasData = p.hasData(),
hasAudio = p.hasAudio(),
songTitle = p.hasAudio() and p.getAudioTitle() or "none"
} or "none"
}
logfile(" Functions for '"..pName.."' loaded successfully!")
end
return pList
end
-------------------------
-- Main code execution --
-------------------------
-- 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!")
----------------
-- DEBUG CODE --
----------------
local list = getList()
local num = 2 --math.random(1, #list)
local page = list[num].energy
local pages = {
"fluids",
"inventory",
"energy",
"disk"
}
gui.drawScreen(list, num, pages[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment