tArgs={...}
if OneOS then
OneOS.ToolBarColour=colours.white
OneOS.ToolBarTextColour=colours.grey
end
local t,a=term.getSize()
local l=function(t,e)
local e=10^(e or 0)
return math.floor(t*e+.5)/e
end
InterfaceElements={}
Drawing={
Screen={
Width=t,
Height=a
},
DrawCharacters=function(t,e,a,o,i)
Drawing.WriteStringToBuffer(t,e,a,o,i)
end,
DrawBlankArea=function(e,a,t,i,o)
Drawing.DrawArea(e,a,t,i," ",1,o)
end,
DrawArea=function(t,s,e,n,o,a,i)
if e<0 then
e=e*-1
elseif e==0 then
e=1
end
for e=1,e do
local t=t+e-1
for e=1,n do
local e=s+e-1
Drawing.WriteToBuffer(t,e,o,a,i)
end
end
end,
DrawImage=function(n,s,t,o,i)
if t then
for e=1,i do
if not t[e]then
break
end
for a=1,o do
if not t[e][a]then
break
end
local i=t[e][a]
local o=t.textcol[e][a]or colours.white
local t=t.text[e][a]
Drawing.WriteToBuffer(a+n-1,e+s-1,t,o,i)
end
end
elseif o and i then
Drawing.DrawBlankArea(x,y,o,i,colours.green)
end
end,
LoadImage=function(e)
local t={
text={},
textcol={}
}
local a=fs
if OneOS then
a=OneOS.FS
end
if a.exists(e)then
local a=io.open
if OneOS then
a=OneOS.IO.open
end
local r=a(e,"r")
local i=r:read()
local e=1
while i do
table.insert(t,e,{})
table.insert(t.text,e,{})
table.insert(t.textcol,e,{})
local o=1
local h,s=false,false
local d,n=nil,nil
for a=1,#i do
local a=string.sub(i,a,a)
if a:byte()==30 then
h=true
elseif a:byte()==31 then
s=true
elseif h then
d=Drawing.GetColour(a)
h=false
elseif s then
n=Drawing.GetColour(a)
s=false
else
if a~=" "and n==nil then
n=colours.white
end
t[e][o]=d
t.textcol[e][o]=n
t.text[e][o]=a
o=o+1
end
end
e=e+1
i=r:read()
end
r:close()
end
return t
end,
DrawCharactersCenter=function(e,t,a,o,i,n,s)
a=a or Drawing.Screen.Width
o=o or Drawing.Screen.Height
e=e or 0
t=t or 0
e=math.ceil((a-#i)/2)+e
t=math.floor(o/2)+t
Drawing.DrawCharacters(e,t,i,n,s)
end,
GetColour=function(e)
if e==' 'then
return colours.transparent
end
local e=tonumber(e,16)
if not e then return nil end
e=math.pow(2,e)
return e
end,
Clear=function(e)
e=e or colours.black
Drawing.ClearBuffer()
Drawing.DrawBlankArea(1,1,Drawing.Screen.Width,Drawing.Screen.Height,e)
end,
Buffer={},
BackBuffer={},
DrawBuffer=function()
for e,t in pairs(Drawing.Buffer)do
for t,a in pairs(t)do
local i=true
local o=true
if Drawing.BackBuffer[e]==nil or Drawing.BackBuffer[e][t]==nil or#Drawing.BackBuffer[e][t]~=3 then
o=false
end
if o and Drawing.BackBuffer[e][t][1]==Drawing.Buffer[e][t][1]and Drawing.BackBuffer[e][t][2]==Drawing.Buffer[e][t][2]and Drawing.BackBuffer[e][t][3]==Drawing.Buffer[e][t][3]then
i=false
end
if i then
term.setBackgroundColour(a[3])
term.setTextColour(a[2])
term.setCursorPos(t,e)
term.write(a[1])
end
end
end
Drawing.BackBuffer=Drawing.Buffer
Drawing.Buffer={}
term.setCursorPos(1,1)
end,
ClearBuffer=function()
Drawing.Buffer={}
end,
WriteStringToBuffer=function(i,n,t,a,o)
for e=1,#t do
local t=t:sub(e,e)
Drawing.WriteToBuffer(i+e-1,n,t,a,o)
end
end,
WriteToBuffer=function(t,e,a,i,o)
t=l(t)
e=l(e)
if o==colours.transparent then
Drawing.Buffer[e]=Drawing.Buffer[e]or{}
Drawing.Buffer[e][t]=Drawing.Buffer[e][t]or{"",colours.white,colours.black}
Drawing.Buffer[e][t][1]=a
Drawing.Buffer[e][t][2]=i
else
Drawing.Buffer[e]=Drawing.Buffer[e]or{}
Drawing.Buffer[e][t]={a,i,o}
end
end,
}
Current={
Document=nil,
TextInput=nil,
CursorPos={1,1},
CursorColour=colours.black,
Selection={8,36},
Window=nil,
HeaderText='',
StatusText='',
StatusColour=colours.grey,
StatusScreen=true,
ButtonOne=nil,
ButtonTwo=nil,
Locked=false,
Page='',
PageControls={}
}
isRunning=true
Events={}
Button={
X=1,
Y=1,
Width=0,
Height=0,
BackgroundColour=colours.lightGrey,
TextColour=colours.white,
ActiveBackgroundColour=colours.lightGrey,
Text="",
Parent=nil,
_Click=nil,
Toggle=nil,
AbsolutePosition=function(e)
return e.Parent:AbsolutePosition()
end,
Draw=function(e)
local t=e.BackgroundColour
local o=e.TextColour
if type(t)=='function'then
t=t()
end
if e.Toggle then
o=colours.white
t=e.ActiveBackgroundColour
end
local a=GetAbsolutePosition(e)
Drawing.DrawBlankArea(a.X,a.Y,e.Width,e.Height,t)
Drawing.DrawCharactersCenter(a.X,a.Y,e.Width,e.Height,e.Text,o,t)
end,
Initialise=function(o,i,l,c,t,u,n,h,a,d,r,s)
local e={}
setmetatable(e,{__index=o})
t=t or 1
e.Width=c or#a+2
e.Height=t
e.Y=l
e.X=i
e.Text=a or""
e.BackgroundColour=u or colours.lightGrey
e.TextColour=d or colours.white
e.ActiveBackgroundColour=s or colours.lightBlue
e.Parent=n
e._Click=h
e.Toggle=r
return e
end,
Click=function(e,o,t,a)
if e._Click then
if e:_Click(o,t,a,not e.Toggle)~=false and e.Toggle~=nil then
e.Toggle=not e.Toggle
Draw()
end
return true
else
return false
end
end
}
Label={
X=1,
Y=1,
Width=0,
Height=0,
BackgroundColour=colours.lightGrey,
TextColour=colours.white,
Text="",
Parent=nil,
AbsolutePosition=function(e)
return e.Parent:AbsolutePosition()
end,
Draw=function(e)
local t=e.BackgroundColour
local a=e.TextColour
if e.Toggle then
a=UIColours.MenuBarActive
t=e.ActiveBackgroundColour
end
local t=GetAbsolutePosition(e)
Drawing.DrawCharacters(t.X,t.Y,e.Text,e.TextColour,e.BackgroundColour)
end,
Initialise=function(h,n,s,t,i,a,o)
local e={}
setmetatable(e,{__index=h})
height=height or 1
e.Width=width or#t+2
e.Height=height
e.Y=s
e.X=n
e.Text=t or""
e.BackgroundColour=a or colours.white
e.TextColour=i or colours.black
e.Parent=o
return e
end,
Click=function(e,e,e,e)
return false
end
}
TextBox={
X=1,
Y=1,
Width=0,
Height=0,
BackgroundColour=colours.lightGrey,
TextColour=colours.black,
Parent=nil,
TextInput=nil,
Placeholder='',
AbsolutePosition=function(e)
return e.Parent:AbsolutePosition()
end,
Draw=function(e)
local t=GetAbsolutePosition(e)
Drawing.DrawBlankArea(t.X,t.Y,e.Width,e.Height,e.BackgroundColour)
local a=e.TextInput.Value
if#tostring(a)>(e.Width-2)then
a=a:sub(#a-(e.Width-3))
if Current.TextInput==e.TextInput then
Current.CursorPos={t.X+1+e.Width-2,t.Y}
end
else
if Current.TextInput==e.TextInput then
Current.CursorPos={t.X+1+e.TextInput.CursorPos,t.Y}
end
end
if#tostring(a)==0 then
Drawing.DrawCharacters(t.X+1,t.Y,e.Placeholder,colours.lightGrey,e.BackgroundColour)
else
Drawing.DrawCharacters(t.X+1,t.Y,a,e.TextColour,e.BackgroundColour)
end
term.setCursorBlink(true)
Current.CursorColour=e.TextColour
end,
Initialise=function(s,d,l,u,t,r,a,h,n,o,i)
local e={}
setmetatable(e,{__index=s})
t=t or 1
e.Width=u or#a+2
e.Height=t
e.Y=l
e.X=d
e.TextInput=TextInput:Initialise(a or'',function(e)
if o then
o(e)
end
Draw()
end,i)
e.BackgroundColour=h or colours.lightGrey
e.TextColour=n or colours.black
e.Parent=r
return e
end,
Click=function(e,t,t,t)
Current.Input=e.TextInput
e:Draw()
end
}
TextInput={
Value="",
Change=nil,
CursorPos=nil,
Numerical=false,
IsDocument=nil,
Initialise=function(n,t,o,i,a)
local e={}
setmetatable(e,{__index=n})
e.Value=tostring(t)
e.Change=o
e.CursorPos=#tostring(t)
e.Numerical=i
e.IsDocument=a or false
return e
end,
Insert=function(e,a)
if e.Numerical then
a=tostring(tonumber(a))
end
local t=OrderSelection()
if e.IsDocument and t then
e.Value=string.sub(e.Value,1,t[1]-1)..a..string.sub(e.Value,t[2]+2)
e.CursorPos=t[1]
Current.Selection=nil
else
local o,t=string.gsub(e.Value:sub(1,e.CursorPos),'\n','')
e.Value=string.sub(e.Value,1,e.CursorPos+t)..a..string.sub(e.Value,e.CursorPos+1+t)
e.CursorPos=e.CursorPos+1
end
e.Change(key)
end,
Extract=function(t,o)
local e=OrderSelection()
if t.IsDocument and e then
local i,a=string.gsub(t.Value:sub(e[1],e[2]),'\n','')
local i=string.sub(t.Value,e[1],e[2]+1+a)
if o then
t.Value=string.sub(t.Value,1,e[1]-1)..string.sub(t.Value,e[2]+2+a)
t.CursorPos=e[1]-1
Current.Selection=nil
end
return i
end
end,
Char=function(t,e)
if e=='nil'then
return
end
t:Insert(e)
end,
Key=function(e,t)
if t==keys.enter then
if e.IsDocument then
e.Value=string.sub(e.Value,1,e.CursorPos)..'\n'..string.sub(e.Value,e.CursorPos+1)
e.CursorPos=e.CursorPos+1
end
e.Change(t)
elseif t==keys.left then
if e.CursorPos>0 then
local a=FindColours(string.sub(e.Value,e.CursorPos,e.CursorPos))
e.CursorPos=e.CursorPos-1-a
e.Change(t)
end
elseif t==keys.right then
if e.CursorPos<string.len(e.Value)then
local a=FindColours(string.sub(e.Value,e.CursorPos+1,e.CursorPos+1))
e.CursorPos=e.CursorPos+1+a
e.Change(t)
end
elseif t==keys.backspace then
if e.IsDocument and Current.Selection then
e:Extract(true)
e.Change(t)
elseif e.CursorPos>0 then
local a=FindColours(string.sub(e.Value,e.CursorPos,e.CursorPos))
local i,o=string.gsub(e.Value:sub(1,e.CursorPos),'\n','')
e.Value=string.sub(e.Value,1,e.CursorPos-1-a+o)..string.sub(e.Value,e.CursorPos+1-a+o)
e.CursorPos=e.CursorPos-1-a
e.Change(t)
end
elseif t==keys.home then
e.CursorPos=0
e.Change(t)
elseif t==keys.delete then
if e.IsDocument and Current.Selection then
e:Extract(true)
e.Change(t)
elseif e.CursorPos<string.len(e.Value)then
e.Value=string.sub(e.Value,1,e.CursorPos)..string.sub(e.Value,e.CursorPos+2)
e.Change(t)
end
elseif t==keys["end"]then
e.CursorPos=string.len(e.Value)
e.Change(t)
elseif t==keys.up and e.IsDocument then
if Current.Document.CursorPos then
local a=Current.Document.Pages[Current.Document.CursorPos.Page]
e.CursorPos=a:GetCursorPosFromPoint(Current.Document.CursorPos.Collum+a.MarginX,Current.Document.CursorPos.Line-a.MarginY-1+Current.Document.ScrollBar.Scroll,true)
e.Change(t)
end
elseif t==keys.down and e.IsDocument then
if Current.Document.CursorPos then
local a=Current.Document.Pages[Current.Document.CursorPos.Page]
e.CursorPos=a:GetCursorPosFromPoint(Current.Document.CursorPos.Collum+a.MarginX,Current.Document.CursorPos.Line-a.MarginY+1+Current.Document.ScrollBar.Scroll,true)
e.Change(t)
end
end
end
}
local e=function(e)
return e:sub(1,1):upper()..e:sub(2,-1)
end
local a=peripheral.getNames or function()
local a={}
for t,e in ipairs(rs.getSides())do
if peripheral.isPresent(e)then
table.insert(a,e)
local t=false
if not pcall(function()t=peripheral.call(e,'isWireless')end)then
t=true
end
if peripheral.getType(e)=="modem"and not t then
local e=peripheral.call(e,"getNamesRemote")
for t,e in ipairs(e)do
table.insert(a,e)
end
end
end
end
return a
end
Peripheral={
GetPeripheral=function(t)
for a,e in ipairs(Peripheral.GetPeripherals())do
if e.Type==t then
return e
end
end
end,
Call=function(e,...)
local t={...}
local e=Peripheral.GetPeripheral(e)
peripheral.call(e.Side,unpack(t))
end,
GetPeripherals=function(s)
local i={}
for t,e in ipairs(a())do
local t=peripheral.getType(e):gsub("^%l",string.upper)
local n=string.upper(e:sub(1,1))
if e:find('_')then
n=e:sub(e:find('_')+1)
end
local a=false
for o,e in ipairs(i)do
if e[1]==t..' '..n then
a=true
end
end
if not a then
local a=peripheral.getType(e)
local o=false
if a=='modem'then
if not pcall(function()o=peripheral.call(sSide,'isWireless')end)then
o=true
end
if o then
a='wireless_modem'
t='W '..t
end
end
if not s or a==s then
table.insert(i,{Name=t:sub(1,8)..' '..n,Fullname=t..' ('..e:sub(1,1):upper()..e:sub(2,-1)..')',Side=e,Type=a,Wireless=o})
end
end
end
return i
end,
PresentNamed=function(e)
return peripheral.isPresent(e)
end,
CallType=function(e,...)
local t={...}
local e=Peripheral.GetPeripheral(e)
return peripheral.call(e.Side,unpack(t))
end,
CallNamed=function(t,...)
local e={...}
return peripheral.call(t,unpack(e))
end
}
Wireless={
Channels={
UltimateDoorlockPing=4210,
UltimateDoorlockRequest=4211,
UltimateDoorlockRequestReply=4212,
},
isOpen=function(e)
return Peripheral.CallType('wireless_modem','isOpen',e)
end,
Open=function(e)
if not Wireless.isOpen(e)then
Peripheral.CallType('wireless_modem','open',e)
end
end,
close=function(e)
Peripheral.CallType('wireless_modem','close',e)
end,
closeAll=function()
Peripheral.CallType('wireless_modem','closeAll')
end,
transmit=function(e,t,a)
Peripheral.CallType('wireless_modem','transmit',e,t,textutils.serialize(a))
end,
Present=function()
if Peripheral.GetPeripheral('wireless_modem')==nil then
return false
else
return true
end
end,
FormatMessage=function(a,t,e)
return{
content=textutils.serialize(a),
senderID=os.getComputerID(),
senderName=os.getComputerLabel(),
channel=channel,
replyChannel=reply,
messageID=t or math.random(1e4),
destinationID=e
}
end,
Timeout=function(t,e)
e=e or 1
parallel.waitForAny(t,function()
sleep(e)
end)
end,
RecieveMessage=function(s,h,r)
open(s)
local n=false
local i,a,t,o,e=nil
Timeout(function()
while not n do
i,a,t,o,e=os.pullEvent('modem_message')
if t~=s then
i,a,t,o,e=nil
else
e=textutils.unserialize(e)
e.content=textutils.unserialize(e.content)
if h and h~=e.messageID or(e.destinationID~=nil and e.destinationID~=os.getComputerID())then
i,a,t,o,e=nil
else
n=true
end
end
end
end,
r)
return i,a,t,o,e
end,
Initialise=function()
if Wireless.Present()then
for t,e in pairs(Wireless.Channels)do
Wireless.Open(e)
end
end
end,
HandleMessage=function(i,n,t,a,e,o)
e=textutils.unserialize(e)
e.content=textutils.unserialize(e.content)
if t==Wireless.Channels.Ping then
if e.content=='Ping!'then
SendMessage(a,'Pong!',nil,e.messageID)
end
elseif e.destinationID~=nil and e.destinationID~=os.getComputerID()then
elseif Wireless.Responder then
Wireless.Responder(i,n,t,a,e,o)
end
end,
SendMessage=function(t,i,e,a,o)
e=e or t+1
Wireless.Open(t)
Wireless.Open(e)
local a=Wireless.FormatMessage(i,a,o)
Wireless.transmit(t,e,a)
return a
end,
Ping=function()
local e=SendMessage(Channels.Ping,'Ping!',Channels.PingReply)
RecieveMessage(Channels.PingReply,e.messageID)
end
}
function GetAbsolutePosition(e)
local e=e
local t=0
local a=1
local o=1
while true do
a=a+e.X-1
o=o+e.Y-1
if not e.Parent then
return{X=a,Y=o}
end
e=e.Parent
if t>32 then
return{X=1,Y=1}
end
t=t+1
end
end
function Draw()
Drawing.Clear(colours.white)
if Current.StatusScreen then
Drawing.DrawCharactersCenter(1,-2,nil,nil,Current.HeaderText,colours.blue,colours.white)
Drawing.DrawCharactersCenter(1,-1,nil,nil,'by oeed',colours.lightGrey,colours.white)
Drawing.DrawCharactersCenter(1,1,nil,nil,Current.StatusText,Current.StatusColour,colours.white)
end
if Current.ButtonOne then
Current.ButtonOne:Draw()
end
if Current.ButtonTwo then
Current.ButtonTwo:Draw()
end
for t,e in ipairs(Current.PageControls)do
e:Draw()
end
Drawing.DrawBuffer()
if Current.TextInput and Current.CursorPos and not Current.Menu and not(Current.Window and Current.Document and Current.TextInput==Current.Document.TextInput)and Current.CursorPos[2]>1 then
term.setCursorPos(Current.CursorPos[1],Current.CursorPos[2])
term.setCursorBlink(true)
term.setTextColour(Current.CursorColour)
else
term.setCursorBlink(false)
end
end
MainDraw=Draw
function GenerateFingerprint()
local e=""
for t=1,256 do
local t=math.random(32,126)
e=e..string.char(t)
end
return e
end
function MakeFingerprint()
local e=fs.open('.fingerprint','w')
if e then
e.write(GenerateFingerprint())
end
e.close()
Current.Fingerprint=str
end
local e=nil
function SetText(a,t,e,o)
if a then
Current.HeaderText=a
end
if t then
Current.StatusText=t
end
if e then
Current.StatusColour=e
end
Draw()
if not o then
statusResetTimer=os.startTimer(2)
end
end
function ResetStatus()
if pocket then
if Current.Locked then
SetText('Ultimate Door Lock','Add Wireless Modem to PDA',colours.red,true)
else
SetText('Ultimate Door Lock','Ready',colours.grey,true)
end
else
if Current.Locked then
SetText('Ultimate Door Lock',' Attach a Wireless Modem then reboot',colours.red,true)
else
SetText('Ultimate Door Lock','Ready',colours.grey,true)
end
end
end
function ResetPage()
Wireless.Responder=function()end
pingTimer=nil
Current.PageControls=nil
Current.StatusScreen=false
Current.ButtonOne=nil
Current.ButtonTwo=nil
Current.PageControls={}
CloseDoor()
end
function PocketInitialise()
Current.ButtonOne=Button:Initialise(Drawing.Screen.Width-6,Drawing.Screen.Height-1,nil,nil,nil,nil,Quit,'Quit',colours.black)
if not Wireless.Present()then
Current.Locked=true
ResetStatus()
return
end
Wireless.Initialise()
ResetStatus()
if fs.exists('.fingerprint')then
local e=fs.open('.fingerprint','r')
if e then
Current.Fingerprint=e.readAll()
else
MakeFingerprint()
end
e.close()
else
MakeFingerprint()
end
Wireless.Responder=function(a,a,t,a,e,a)
if t==Wireless.Channels.UltimateDoorlockPing then
Wireless.SendMessage(Wireless.Channels.UltimateDoorlockRequest,Current.Fingerprint,Wireless.Channels.UltimateDoorlockRequestReply,nil,e.senderID)
elseif t==Wireless.Channels.UltimateDoorlockRequestReply then
if e.content==true then
SetText(nil,'Opening Door',colours.green)
else
SetText(nil,' Access Denied',colours.red)
end
end
end
end
function FingerprintIsOnWhitelist(t)
if Current.Settings.Whitelist then
for a,e in ipairs(Current.Settings.Whitelist)do
if e==t then
return true
end
end
end
return false
end
function SaveSettings()
Current.Settings=Current.Settings or{}
local e=fs.open('.settings','w')
if e then
e.write(textutils.serialize(Current.Settings))
end
e.close()
end
local n=nil
function OpenDoor()
if Current.Settings and Current.Settings.RedstoneSide then
SetText(nil,'Opening Door',colours.green)
redstone.setOutput(Current.Settings.RedstoneSide,true)
n=os.startTimer(.6)
end
end
function CloseDoor()
if Current.Settings and Current.Settings.RedstoneSide then
if redstone.getOutput(Current.Settings.RedstoneSide)then
SetText(nil,'Closing Door',colours.orange)
redstone.setOutput(Current.Settings.RedstoneSide,false)
end
end
end
DefaultSettings={
Whitelist={},
RedstoneSide='back',
Distance=10
}
function RegisterPDA(e,o)
if disk.hasData(o)then
local a=fs
if OneOS then
a=OneOS.FS
end
local e=disk.getMountPath(o)
local i=true
if a.exists(e..'/System/')then
e=e..'/System/'
i=false
end
local t=nil
if a.exists(e..'/.fingerprint')then
local e=a.open(e..'/.fingerprint','r')
if e then
local e=e.readAll()
if#e==256 then
t=e
end
end
e.close()
end
if not t then
t=GenerateFingerprint()
local o=a.open(e..'/.fingerprint','w')
o.write(t)
o.close()
if i then
local t=fs.open(shell.getRunningProgram(),'r')
local o=t.readAll()
t.close()
local e=a.open(e..'/startup','w')
e.write(o)
e.close()
end
end
if not FingerprintIsOnWhitelist(t)then
table.insert(Current.Settings.Whitelist,t)
SaveSettings()
end
disk.eject(o)
SetText(nil,'Registered Pocket Computer',colours.green)
end
end
function HostSetup()
ResetPage()
Current.Page='HostSetup'
Current.ButtonTwo=Button:Initialise(Drawing.Screen.Width-6,Drawing.Screen.Height-1,nil,nil,nil,nil,HostStatusPage,'Save',colours.black)
if not Current.Settings then
Current.Settings=DefaultSettings
end
local t={}
local function e(a)
for t,e in ipairs(t)do
if e.Toggle~=nil then
e.Toggle=false
end
end
Current.Settings.RedstoneSide=a.Text:lower()
SaveSettings()
end
table.insert(Current.PageControls,Label:Initialise(2,2,'Redstone Side'))
t={
Button:Initialise(2,4,nil,nil,nil,nil,e,'Back',colours.black,false,colours.green),
Button:Initialise(9,4,nil,nil,nil,nil,e,'Front',colours.black,false,colours.green),
Button:Initialise(2,6,nil,nil,nil,nil,e,'Left',colours.black,false,colours.green),
Button:Initialise(9,6,nil,nil,nil,nil,e,'Right',colours.black,false,colours.green),
Button:Initialise(2,8,nil,nil,nil,nil,e,'Top',colours.black,false,colours.green),
Button:Initialise(8,8,nil,nil,nil,nil,e,'Bottom',colours.black,false,colours.green)
}
for t,e in ipairs(t)do
if e.Text:lower()==Current.Settings.RedstoneSide then
e.Toggle=true
end
table.insert(Current.PageControls,e)
end
local a={}
local function t(e)
for t,e in ipairs(a)do
if e.Toggle~=nil then
e.Toggle=false
end
end
if e.Text=='Small'then
Current.Settings.Distance=5
elseif e.Text=='Normal'then
Current.Settings.Distance=10
elseif e.Text=='Far'then
Current.Settings.Distance=15
end
SaveSettings()
end
table.insert(Current.PageControls,Label:Initialise(23,2,'Opening Distance'))
a={
Button:Initialise(23,4,nil,nil,nil,nil,t,'Small',colours.black,false,colours.green),
Button:Initialise(31,4,nil,nil,nil,nil,t,'Normal',colours.black,false,colours.green),
Button:Initialise(40,4,nil,nil,nil,nil,t,'Far',colours.black,false,colours.green)
}
for t,e in ipairs(a)do
if e.Text=='Small'and Current.Settings.Distance==5 then
e.Toggle=true
elseif e.Text=='Normal'and Current.Settings.Distance==10 then
e.Toggle=true
elseif e.Text=='Far'and Current.Settings.Distance==15 then
e.Toggle=true
end
table.insert(Current.PageControls,e)
end
table.insert(Current.PageControls,Label:Initialise(2,10,'Registered PDAs: '..#Current.Settings.Whitelist))
table.insert(Current.PageControls,Button:Initialise(2,12,nil,nil,nil,nil,function()Current.Settings.Whitelist={}HostSetup()end,'Unregister All',colours.black))
table.insert(Current.PageControls,Label:Initialise(23,6,'Help',colours.black))
local e={
Label:Initialise(23,8,'To register a new PDA simply',colours.black),
Label:Initialise(23,9,'place a Disk Drive next to',colours.black),
Label:Initialise(23,10,'the computer, then put the',colours.black),
Label:Initialise(23,11,'PDA in the Drive, it will',colours.black),
Label:Initialise(23,12,'register automatically. If',colours.black),
Label:Initialise(23,13,'it worked it will eject.',colours.black),
Label:Initialise(23,15,'Make sure you hide this',colours.red),
Label:Initialise(23,16,'computer away from the',colours.red),
Label:Initialise(23,17,'door! (other people)',colours.red)
}
for t,e in ipairs(e)do
table.insert(Current.PageControls,e)
end
table.insert(Current.PageControls,Button:Initialise(2,14,nil,nil,nil,nil,function()
for t=1,6 do
e[t].TextColour=colours.green
end
end,'Register New PDA',colours.black))
end
function HostStatusPage()
ResetPage()
Current.Page='HostStatus'
Current.StatusScreen=true
Current.ButtonOne=Button:Initialise(Drawing.Screen.Width-6,Drawing.Screen.Height-1,nil,nil,nil,nil,Quit,'Quit',colours.black)
Current.ButtonTwo=Button:Initialise(2,Drawing.Screen.Height-1,nil,nil,nil,nil,HostSetup,'Settings/Help',colours.black)
Wireless.Responder=function(o,o,e,o,t,a)
if e==Wireless.Channels.UltimateDoorlockRequest and a<Current.Settings.Distance then
if FingerprintIsOnWhitelist(t.content)then
OpenDoor()
Wireless.SendMessage(Wireless.Channels.UltimateDoorlockRequestReply,true)
else
Wireless.SendMessage(Wireless.Channels.UltimateDoorlockRequestReply,false)
end
end
end
PingPocketComputers()
end
function HostInitialise()
if not Wireless.Present()then
Current.Locked=true
Current.ButtonOne=Button:Initialise(Drawing.Screen.Width-6,Drawing.Screen.Height-1,nil,nil,nil,nil,Quit,'Quit',colours.black)
Current.ButtonTwo=Button:Initialise(2,Drawing.Screen.Height-1,nil,nil,nil,nil,function()os.reboot()end,'Reboot',colours.black)
ResetStatus()
return
end
Wireless.Initialise()
ResetStatus()
if fs.exists('.settings')then
local e=fs.open('.settings','r')
if e then
Current.Settings=textutils.unserialize(e.readAll())
end
e.close()
HostStatusPage()
else
HostSetup()
end
if OneOS then
OneOS.CanClose=function()
CloseDoor()
return true
end
end
end
local t=nil
function PingPocketComputers()
Wireless.SendMessage(Wireless.Channels.UltimateDoorlockPing,'Ping!',Wireless.Channels.UltimateDoorlockRequest)
t=os.startTimer(.5)
end
function Initialise(e)
EventRegister('mouse_click',TryClick)
EventRegister('mouse_drag',function(e,o,t,a)TryClick(e,o,t,a,true)end)
EventRegister('mouse_scroll',Scroll)
EventRegister('key',HandleKey)
EventRegister('char',HandleKey)
EventRegister('timer',Timer)
EventRegister('terminate',function(e)if Close()then error("Terminated",0)end end)
EventRegister('modem_message',Wireless.HandleMessage)
EventRegister('disk',RegisterPDA)
if OneOS then
OneOS.RequestRunAtStartup()
end
if pocket then
PocketInitialise()
else
HostInitialise()
end
Draw()
EventHandler()
end
function Timer(a,e)
if e==t then
PingPocketComputers()
elseif e==n then
CloseDoor()
elseif e==statusResetTimer then
ResetStatus()
end
end
local e=false
function HandleKey(...)
local e={...}
local t=e[1]
local e=e[2]
end
function CheckClick(e,a,t)
if e.X<=a and e.Y<=t and e.X+e.Width>a and e.Y+e.Height>t then
return true
end
end
function DoClick(e,n,o,a,i)
local t=GetAbsolutePosition(e)
t.Width=e.Width
t.Height=e.Height
if e and CheckClick(t,o,a)then
return e:Click(n,o-e.X+1,a-e.Y+1,i)
end
end
function TryClick(i,t,e,a,o)
if Current.ButtonOne then
if DoClick(Current.ButtonOne,t,e,a,o)then
Draw()
return
end
end
if Current.ButtonTwo then
if DoClick(Current.ButtonTwo,t,e,a,o)then
Draw()
return
end
end
for n,i in ipairs(Current.PageControls)do
if DoClick(i,t,e,a,o)then
Draw()
return
end
end
Draw()
end
function Scroll(t,e,t,t)
if Current.Window and Current.Window.OpenButton then
Current.Document.Scroll=Current.Document.Scroll+e
if Current.Window.Scroll<0 then
Current.Window.Scroll=0
elseif Current.Window.Scroll>Current.Window.MaxScroll then
Current.Window.Scroll=Current.Window.MaxScroll
end
Draw()
elseif Current.ScrollBar then
if Current.ScrollBar:DoScroll(e*2)then
Draw()
end
end
end
function EventRegister(e,t)
if not Events[e]then
Events[e]={}
end
table.insert(Events[e],t)
end
function EventHandler()
while isRunning do
local e,h,n,s,i,t,a=os.pullEventRaw()
if Events[e]then
for r,o in ipairs(Events[e])do
o(e,h,n,s,i,t,a)
end
end
end
end
function Quit()
isRunning=false
term.setCursorPos(1,1)
term.setBackgroundColour(colours.black)
term.setTextColour(colours.white)
term.clear()
if OneOS then
OneOS.Close()
end
end
if not term.current then
print('Because it requires pocket computers, Ultimate Door Lock requires ComputerCraft 1.6. Please update to 1.6 to use Ultimate Door Lock.')
elseif not(OneOS and pocket)and term.isColor and term.isColor()then
local t,e=pcall(Initialise)
if e then
CloseDoor()
term.setCursorPos(1,1)
term.setBackgroundColour(colours.black)
term.setTextColour(colours.white)
term.clear()
print('Ultimate Door Lock has crashed')
print('To maintain security, the computer will reboot.')
print('If you are seeing this alot try turning off all Pocket Computers or reinstall.')
print()
print('Error:')
printError(e)
sleep(5)
os.reboot()
end
elseif OneOS and pocket then
term.setCursorPos(1,3)
term.setBackgroundColour(colours.white)
term.setTextColour(colours.blue)
term.clear()
print('OneOS already acts as a door key. Simply place your PDA in the door\'s disk drive to register it.')
print()
print('To setup a door, run this program on an advanced computer (non-pocket).')
print()
print('Click anywhere to quit')
os.pullEvent('mouse_click')
Quit()
else
print('Ultimate Door Lock requires an advanced (gold) computer or pocket computer.')
end