This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
ChiknNuggets's profile picture

access denied....

Started by ChiknNuggets, 06 January 2013 - 07:43 PM
ChiknNuggets #1
Posted 06 January 2013 - 08:43 PM
Spoiler

-- Declerations --
local InstallDirectory = "/installed/"

-- images have to be 14 wide and 7 high or less
local missingicon = {
"77777777777777";
"7eeeee55eeeee7";
"7eeee5ee5eeee7";
"7eeeeee5eeeee7";
"7eeeeeeeeeeee7";
"7eeeeee5eeeee7";
"77777777777777";
}

local tColourLookup = {}
for n=1,16 do
tColourLookup[ string.byte( "0123456789abcdef",n,n ) ] = 2^(n-1)
end

function deleteDialog(slot)
title = "Delete "..filelist[slot].."?"
local button1 = "Confirm"
local button2 = "Cancel"
local titlecolor = colors.blue

local w,h = term.getSize()
x = (w-39)/2
y = (h-6)/2
term.setTextColor(colors.white)
term.setBackgroundColor(titlecolor)
term.setCursorPos(x,y)
if #title % 2 == 0 then write(string.rep(" ",(40-#title)/2)..title..string.rep(" ",(40-#title)/2))
else write(string.rep(" ",(40-#title)/2)..title..string.rep(" ",((40-#title)/2)+.5)) end
term.setCursorPos(x,y+1)
term.setBackgroundColor(colors.lightGray)
for i= 1,5 do term.setCursorPos(x,y+i);write(string.rep(" ",40)) end
term.setCursorPos(x+2,y+2)
term.setTextColor(colors.black)
write("Confirm removal of "..filelist[slot].."?")
term.setBackgroundColor(colors.lightBlue)
term.setCursorPos(x+2,y+4)
write(" "..button1.." ")
term.setCursorPos(x+38- #button2 -2,y+4)
write(" "..button2.." ")
term.setBackgroundColor(colors.gray)
term.setCursorPos(x+2,y+2)
term.setTextColor(colors.white)
repeat e,p1,p2,p3 = os.pullEvent() 
until (e == "mouse_click" and ((p2 >= x + 38 - #button2 and p2 <= x + 37 and p3 == y+3.5) or (p2 >= x + 2 and p2 <= x + 3 + #button1 and p3 == y+3.5)))
if (p2 >= x + 2 and p2 <= x + 3 + #button1 and p3 == y+3.5) then 
if fs.exists(InstallDirectory..filelist[slot]) then fs.delete(InstallDirectory..filelist[slot]) end
if fs.exists(InstallDirectory..filelist[slot]..".ico") then fs.delete(InstallDirectory..filelist[slot]..".ico") end

end
end

function refreshFileList()
filelist = fs.list(InstallDirectory)
local i=1
while i<= #filelist do
if string.lower(string.sub(filelist[i],#filelist[i]-3,#filelist[i])) == ".ico" then
table.remove(filelist,i)
else i=i+1 end
end
end
local tColourLookup = {}
for n=1,16 do
tColourLookup[ string.byte( "0123456789abcdef",n,n ) ] = 2^(n-1)
end

function loadIcon( sPath )
local tImage = {}
if fs.exists( sPath ) then
local file = io.open(sPath, "r" )
local sLine = file:read()
while sLine do
local tLine = {}
sLine = string.sub(sLine,1,14)
for x=1,sLine:len() do
tLine[x] = tColourLookup[ string.byte(sLine,x,x) ] or 0
end
table.insert( tImage, tLine )
if #tImage == 7 then return tImage end
sLine = file:read()
end
file:close()
return tImage
end
return nil
end


function popupMenu(x, y,title, ...)
        local args = {...}
        local objects = {}
        term.setTextColor(colors.white)
  term.setBackgroundColor(colors.blue)
        term.setCursorPos(x,y)
        write("  "..title.."   ")
        term.setTextColor(colors.black)
  term.setBackgroundColor(colors.lightGray)
        for i,v in pairs(args) do
                objects[i] = {
                        x = x;
                        y = i + y;
                        text = v;
                }
        end
        menulength = #title +5
        for _, option in pairs(objects) do
         itemlength = #option.text + 2
                term.setCursorPos(option.x, option.y)
                term.write("  "..option.text..string.rep(" ",menulength-itemlength))
        end
        local _, button, mx, my = os.pullEvent('mouse_click')
        for _, option in pairs(objects) do
                if mx >= option.x and mx < option.x + menulength
                and my == option.y then
                        return option.text
                end
        end
end

local function getColourOf(hex)
        local value = tonumber(hex, 16)
        if not value then return nil end
        value = math.pow(2,value)
        return value
end

function drawPicture(image, xinit, yinit, alpha)
if not alpha then alpha = 1 end
for y=1,#image do
for x=1,#image[y] do
term.setCursorPos(xinit + x-1, yinit + y-1)
local col = getColourOf(string.sub(image[y], x, x))
if not col then col = alpha end
term.setBackgroundColour(col)
term.write(" ")
end
end
end

function RenameDialog(title,slot)

local button1 = "Confirm"
local button2 = "Cancel"
local titlecolor = colors.blue

local w,h = term.getSize()
x = (w-27)/2
y = (h-6)/2
term.setTextColor(colors.white)
term.setBackgroundColor(titlecolor)
term.setCursorPos(x,y)
if #title % 2 == 0 then write(string.rep(" ",(28-#title)/2)..title..string.rep(" ",(28-#title)/2))
else write(string.rep(" ",(28-#title)/2)..title..string.rep(" ",((28-#title)/2)+.5)) end
term.setCursorPos(x,y+1)
term.setBackgroundColor(colors.lightGray)
for i= 1,5 do term.setCursorPos(x,y+i);write(string.rep(" ",28)) end
term.setCursorPos(x+2,y+2)
term.setBackgroundColor(colors.gray)
write(string.rep(" ",24))
term.setBackgroundColor(colors.lightBlue)
term.setTextColor(colors.black)
term.setCursorPos(x+2,y+4)
write(" "..button1.." ")
term.setCursorPos(x+26- #button2 -2,y+4)
write(" "..button2.." ")
term.setBackgroundColor(colors.gray)
term.setCursorPos(x+2,y+2)
input = ""
term.setTextColor(colors.white)
term.setCursorBlink(true)
repeat
e,p1,p2,p3 = os.pullEvent()
     if e=="char" then
if #input < 12 then
            input = input .. p1
             term.write(p1)
       end
     elseif e=="key" and p1==keys.backspace and #input > 0 then
       input = input:sub(1,#input-1)
       local x1,y1 = term.getCursorPos()
       term.setCursorPos(x1-1,y1)
       term.write(" ")
       term.setCursorPos(x1-1,y1)
     end
until (e == "mouse_click" and ((p2 >= x + 24 - #button2 and p2 <= x + 25 and p3 == y+3.5) or (p2 >= x + 2 and p2 <= x + 3 + #button1 and p3 == y+3.5))) or (e == "key" and p1 == keys.enter)
term.setCursorBlink(false)
if e == "mouse_click" and (p2 >= x + 24 - #button2 and p2 <= x + 25 and p3 == y+3.5) then drawIcons(currenticonnumber,#filelist)
elseif (e == "mouse_click" and (p2 >= x + 2 and p2 <= x + 3 + #button1 and p3 == y+3.5)) or (e == "key" and p1 == keys.enter) and input ~= "" then 
if fs.exists(InstallDirectory..input) and string.lower(InstallDirectory..filelist[slot]) == string.lower(InstallDirectory..input) then 
fs.move(InstallDirectory..filelist[slot],InstallDirectory..input..".temp")
fs.move(InstallDirectory..input..".temp",InstallDirectory..input)
if fs.exists(InstallDirectory..filelist[slot]..".ico") and fs.exists(InstallDirectory..input) then
fs.move(InstallDirectory..filelist[slot]..".ico", InstallDirectory..input..".ico"..".temp")
fs.move(InstallDirectory..filelist[slot]..".ico"..".temp", InstallDirectory..input..".ico") 
end
elseif fs.exists(InstallDirectory..input) and string.lower(InstallDirectory..filelist[slot]) ~= string.lower(InstallDirectory..input) then
term.setBackgroundColor(colors.red)
term.setCursorPos(x+2,y+2)
write(string.rep(" ",24))
term.setCursorPos(x+2,y+2)
write("App Name allready Taken")
sleep(3)
else 
fs.move(InstallDirectory..filelist[slot],InstallDirectory..input)
if fs.exists(InstallDirectory..filelist[slot]..".ico") and fs.exists(InstallDirectory..input) then
fs.move(InstallDirectory..filelist[slot]..".ico", InstallDirectory..input..".ico") 
end
end
end
end



function findSlot(x,y)
if (x >= 3 and x <= 16) and (y >=2 and y <= 8) and filelist[currenticonnumber] ~= nil then slot = currenticonnumber
elseif (x >= 19 and x <=32) and (y >=2 and y <= 8) and filelist[currenticonnumber+1] ~= nil then slot = currenticonnumber+1
elseif (x >= 35 and x <=48) and (y >=2 and y <= 8) and filelist[currenticonnumber+2] ~= nil then slot = currenticonnumber+2
elseif (x >= 3 and x <= 16) and (y >=10 and y <= 16) and filelist[currenticonnumber+3] ~= nil then slot = currenticonnumber+3
elseif (x >= 19 and x <=32) and (y >=10 and y <= 16) and filelist[currenticonnumber+4] ~= nil then slot = currenticonnumber+4
elseif (x >= 35 and x <=48) and (y >=10 and y <= 16) and filelist[currenticonnumber+5] ~= nil then slot = currenticonnumber+5 
else slot = 0 end 
return slot
end

function drawIcons(x,y)
term.setBackgroundColor(colors.black)
term.clear()
term.setCursorPos(1,1)
for i=x,y do
if filelist[i] ~= nil then
if fs.exists(InstallDirectory..filelist[i]..".ico") then
if i == x then 
paintutils.drawImage(loadIcon(InstallDirectory..filelist[i]..".ico"),3,2)
if #filelist[i] > 12 then term.setCursorPos(3,8);term.setBackgroundColor(colors.gray);term.setTextColor(colors.white);print(" "..string.sub(filelist[i],1,9).."... ") else term.setBackgroundColor(colors.gray);term.setTextColor(colors.white);term.setCursorPos(9-(#filelist[i]/2),8);print(" "..filelist[i].." ") end
end
if i == x+1 then 
paintutils.drawImage(loadIcon(InstallDirectory..filelist[i]..".ico"),19,2) 
if #filelist[i] > 12 then term.setCursorPos(19,8);term.setBackgroundColor(colors.gray);term.setTextColor(colors.white);print(" "..string.sub(filelist[i],1,9).."... ") else term.setBackgroundColor(colors.gray);term.setTextColor(colors.white);term.setCursorPos(25-(#filelist[i]/2),8);print(" "..filelist[i].." ") end
end
if i == x+2 then 
paintutils.drawImage(loadIcon(InstallDirectory..filelist[i]..".ico"),35,2)
if #filelist[i] > 12 then term.setCursorPos(35,8);term.setBackgroundColor(colors.gray);term.setTextColor(colors.white);print(" "..string.sub(filelist[i],1,9).."... ") else term.setBackgroundColor(colors.gray);term.setTextColor(colors.white);term.setCursorPos(41-(#filelist[i]/2),8);print(" "..filelist[i].." ") end 
end

if i == x+3 then 
paintutils.drawImage(loadIcon(InstallDirectory..filelist[i]..".ico"),3,10) 
if #filelist[i] > 12 then term.setCursorPos(3,6);term.setBackgroundColor(colors.gray);term.setTextColor(colors.white);print(" "..string.sub(filelist[i],1,9).."... ") else term.setBackgroundColor(colors.gray);term.setTextColor(colors.white);term.setCursorPos(9-(#filelist[i]/2),16);print(" "..filelist[i].." ") end
end

if i == x+4 then 
paintutils.drawImage(loadIcon(InstallDirectory..filelist[i]..".ico"),19,10)
if #filelist[i] > 12 then term.setCursorPos(19,16);term.setBackgroundColor(colors.gray);term.setTextColor(colors.white);print(" "..string.sub(filelist[i],1,9).."... ") else term.setBackgroundColor(colors.gray);term.setTextColor(colors.white);term.setCursorPos(25-(#filelist[i]/2),16);print(" "..filelist[i].." ") end 
end
if i == x+5 then 
paintutils.drawImage(loadIcon(InstallDirectory..filelist[i]..".ico"),35,10) 
if #filelist[i] > 12 then term.setCursorPos(35,16);term.setBackgroundColor(colors.gray);term.setTextColor(colors.white);print(" "..string.sub(filelist[i],1,9).."... ") else term.setBackgroundColor(colors.gray);term.setTextColor(colors.white);term.setCursorPos(41-(#filelist[i]/2),16);print(" "..filelist[i].." ") end
end
else 
if i == x then 
drawPicture(missingicon,3,2)
if #filelist[i] > 12 then term.setCursorPos(3,8);term.setBackgroundColor(colors.gray);term.setTextColor(colors.white);print(" "..string.sub(filelist[i],1,9).."... ") else term.setBackgroundColor(colors.gray);term.setTextColor(colors.white);term.setCursorPos(9-(#filelist[i]/2),8);print(" "..filelist[i].." ") end
end
if i == x+1 then 
drawPicture(missingicon,19,2)
if #filelist[i] > 12 then term.setCursorPos(19,8);term.setBackgroundColor(colors.gray);term.setTextColor(colors.white);print(" "..string.sub(filelist[i],1,9).."... ") else term.setBackgroundColor(colors.gray);term.setTextColor(colors.white);term.setCursorPos(25-(#filelist[i]/2),8);print(" "..filelist[i].." ") end
end
if i == x+2 then 
drawPicture(missingicon,35,2)
if #filelist[i] > 12 then term.setCursorPos(35,8);term.setBackgroundColor(colors.gray);term.setTextColor(colors.white);print(" "..string.sub(filelist[i],1,9).."... ") else term.setBackgroundColor(colors.gray);term.setTextColor(colors.white);term.setCursorPos(41-(#filelist[i]/2),8);print(" "..filelist[i].." ") end
end
if i == x+3 then 
drawPicture(missingicon,3,10)
if #filelist[i] > 12 then term.setCursorPos(3,16);term.setBackgroundColor(colors.gray);term.setTextColor(colors.white);print(" "..string.sub(filelist[i],1,9).."... ") else term.setBackgroundColor(colors.gray);term.setTextColor(colors.white);term.setCursorPos(9-(#filelist[i]/2),16);print(" "..filelist[i].." ") end
end
if i == x+4 then 
drawPicture(missingicon,19,10)
if #filelist[i] > 12 then term.setCursorPos(19,16);term.setBackgroundColor(colors.gray);term.setTextColor(colors.white);print(" "..string.sub(filelist[i],1,9).."... ") else term.setBackgroundColor(colors.gray);term.setTextColor(colors.white);term.setCursorPos(25-(#filelist[i]/2),16);print(" "..filelist[i].." ") end 
end
if i == x+5 then 
drawPicture(missingicon,35,10) 
if #filelist[i] > 12 then term.setCursorPos(35,16);term.setBackgroundColor(colors.gray);term.setTextColor(colors.white);print(" "..string.sub(filelist[i],1,9).."... ") else term.setBackgroundColor(colors.gray);term.setTextColor(colors.white);term.setCursorPos(41-(#filelist[i]/2),16);print(" "..filelist[i].." ") end
end
end
end
end
end


term.clear()
term.setCursorPos(1,1)
refreshFileList()
currenticonnumber = 1
drawIcons(currenticonnumber,#filelist)
while true do
local event,p1,p2,p3 = os.pullEvent()
local x = p2;y = p3
if event == "mouse_scroll" then
if p1 == 1 and currenticonnumber +3 <= #filelist then currenticonnumber = currenticonnumber + 3
elseif p1 == -1 and currenticonnumber -3 >= 1 then currenticonnumber = currenticonnumber - 3 end
drawIcons(currenticonnumber,#filelist)
elseif event == "mouse_click" and p1 == 1 then
local x = p2;y = p3
term.setBackgroundColor(colors.black);term.setTextColor(colors.white);
local input = findSlot(x,y)
if input ~= 0 then shell.run(InstallDirectory..filelist[input]) end
drawIcons(currenticonnumber,#filelist)
elseif event == "mouse_click" and p1 == 2 and findSlot(x,y) ~= 0 then
local input = popupMenu(x,y,"Options","Run","Rename","Edit","Delete","Edit Icon","New Item")
drawIcons(currenticonnumber,#filelist)
if input == 'Run' then
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
  local input = findSlot(x,y)
if input ~= 0 then shell.run(InstallDirectory..filelist[input]) end
elseif input == "Rename" then
RenameDialog("Rename "..filelist[findSlot(x,y)],findSlot(x,y))
refreshFileList()
elseif input == "Edit" then
shell.run("edit",InstallDirectory..filelist[findSlot(x,y)])
elseif input == "Delete" then
deleteDialog(findSlot(x,y))
refreshFileList()
elseif input == "Edit Icon" then
shell.run("paint",InstallDirectory..filelist[findSlot(x,y)]..".ico")
elseif input == "New Item" then
    
end
drawIcons(currenticonnumber,#filelist)
elseif event == "mouse_click" and p1 ==2 and findSlot(x,y) == 0 then
local input = popupMenu(x,y,"Options","New Item")
if input == "New Item" then

end
drawIcons(currenticonnumber,#filelist)
  end
end


Got this code it detects files in a folder in this case /installed/ and anyway the folder has 2 files, lets say bob and bob.ico for some reason when i try and delete the bob.ico i get access denied on line 52 can some please debug and see whats going on?
Kingdaro #2
Posted 06 January 2013 - 08:52 PM
That usually happens when you don't close a file after you've opened it.
ChiknNuggets #3
Posted 06 January 2013 - 09:56 PM
ahh thank you very much, so simple and yet i didnt notice, i added a claus that exited the icon part early if it was over 14 long so it never closed it, thank you.