1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-12-13 03:30:29 +00:00

Merge pull request #317 from Wojbie/Mixed-lua-side-improvements

Multiple fixes and feature fixes for lua side of CC.
This commit is contained in:
Daniel Ratcliffe 2017-06-28 22:25:18 +01:00 committed by GitHub
commit 14e4d037ad
11 changed files with 135 additions and 87 deletions

View File

@ -729,8 +729,8 @@ if http then
if type( _url ) ~= "string" then
error( "bad argument #1 (expected string, got " .. type( _url ) .. ")", 2 )
end
if _post ~= nil and type( _post ) ~= "table" then
error( "bad argument #2 (expected table, got " .. type( _post ) .. ")", 2 )
if _post ~= nil and type( _post ) ~= "string" then
error( "bad argument #2 (expected string, got " .. type( _post ) .. ")", 2 )
end
if _headers ~= nil and type( _headers ) ~= "table" then
error( "bad argument #3 (expected table, got " .. type( _headers ) .. ")", 2 )
@ -828,7 +828,7 @@ for n,sFile in ipairs( tApis ) do
end
end
if turtle then
if turtle and fs.isDir( "rom/apis/turtle" ) then
-- Load turtle APIs
local tApis = fs.list( "rom/apis/turtle" )
for n,sFile in ipairs( tApis ) do

View File

@ -7,14 +7,14 @@ local tReceivedMessageTimeouts = {}
local tHostnames = {}
function open( sModem )
if type( sModem ) ~= "string" then
error( "expected string", 2 )
end
if peripheral.getType( sModem ) ~= "modem" then
error( "No such modem: "..sModem, 2 )
end
peripheral.call( sModem, "open", os.getComputerID() )
peripheral.call( sModem, "open", CHANNEL_BROADCAST )
if type( sModem ) ~= "string" then
error( "expected string", 2 )
end
if peripheral.getType( sModem ) ~= "modem" then
error( "No such modem: "..sModem, 2 )
end
peripheral.call( sModem, "open", os.getComputerID() )
peripheral.call( sModem, "open", CHANNEL_BROADCAST )
end
function close( sModem )
@ -55,7 +55,7 @@ function isOpen( sModem )
end
end
end
return false
return false
end
function send( nRecipient, message, sProtocol )
@ -93,7 +93,7 @@ function send( nRecipient, message, sProtocol )
end
function broadcast( message, sProtocol )
send( CHANNEL_BROADCAST, message, sProtocol )
send( CHANNEL_BROADCAST, message, sProtocol )
end
function receive( sProtocolFilter, nTimeout )
@ -103,31 +103,31 @@ function receive( sProtocolFilter, nTimeout )
end
-- Start the timer
local timer = nil
local sFilter = nil
if nTimeout then
timer = os.startTimer( nTimeout )
sFilter = nil
else
sFilter = "rednet_message"
end
local timer = nil
local sFilter = nil
if nTimeout then
timer = os.startTimer( nTimeout )
sFilter = nil
else
sFilter = "rednet_message"
end
-- Wait for events
while true do
local sEvent, p1, p2, p3 = os.pullEvent( sFilter )
if sEvent == "rednet_message" then
-- Return the first matching rednet_message
local nSenderID, message, sProtocol = p1, p2, p3
if sProtocolFilter == nil or sProtocol == sProtocolFilter then
return nSenderID, message, sProtocol
end
elseif sEvent == "timer" then
-- Return nil if we timeout
if p1 == timer then
return nil
end
end
end
-- Wait for events
while true do
local sEvent, p1, p2, p3 = os.pullEvent( sFilter )
if sEvent == "rednet_message" then
-- Return the first matching rednet_message
local nSenderID, message, sProtocol = p1, p2, p3
if sProtocolFilter == nil or sProtocol == sProtocolFilter then
return nSenderID, message, sProtocol
end
elseif sEvent == "timer" then
-- Return nil if we timeout
if p1 == timer then
return nil
end
end
end
end
function host( sProtocol, sHostname )
@ -219,41 +219,41 @@ end
local bRunning = false
function run()
if bRunning then
error( "rednet is already running", 2 )
end
bRunning = true
if bRunning then
error( "rednet is already running", 2 )
end
bRunning = true
while bRunning do
local sEvent, p1, p2, p3, p4 = os.pullEventRaw()
if sEvent == "modem_message" then
-- Got a modem message, process it and add it to the rednet event queue
local sModem, nChannel, nReplyChannel, tMessage = p1, p2, p3, p4
if isOpen( sModem ) and ( nChannel == os.getComputerID() or nChannel == CHANNEL_BROADCAST ) then
if type( tMessage ) == "table" and tMessage.nMessageID then
if not tReceivedMessages[ tMessage.nMessageID ] then
tReceivedMessages[ tMessage.nMessageID ] = true
tReceivedMessageTimeouts[ os.startTimer( 30 ) ] = nMessageID
os.queueEvent( "rednet_message", nReplyChannel, tMessage.message, tMessage.sProtocol )
end
end
end
while bRunning do
local sEvent, p1, p2, p3, p4 = os.pullEventRaw()
if sEvent == "modem_message" then
-- Got a modem message, process it and add it to the rednet event queue
local sModem, nChannel, nReplyChannel, tMessage = p1, p2, p3, p4
if isOpen( sModem ) and ( nChannel == os.getComputerID() or nChannel == CHANNEL_BROADCAST ) then
if type( tMessage ) == "table" and tMessage.nMessageID then
if not tReceivedMessages[ tMessage.nMessageID ] then
tReceivedMessages[ tMessage.nMessageID ] = true
tReceivedMessageTimeouts[ os.startTimer( 30 ) ] = tMessage.nMessageID
os.queueEvent( "rednet_message", nReplyChannel, tMessage.message, tMessage.sProtocol )
end
end
end
elseif sEvent == "rednet_message" then
-- Got a rednet message (queued from above), respond to dns lookup
local nSenderID, tMessage, sProtocol = p1, p2, p3
if sProtocol == "dns" and type(tMessage) == "table" and tMessage.sType == "lookup" then
local sHostname = tHostnames[ tMessage.sProtocol ]
if sHostname ~= nil and (tMessage.sHostname == nil or tMessage.sHostname == sHostname) then
rednet.send( nSenderID, {
sType = "lookup response",
sHostname = sHostname,
sProtocol = tMessage.sProtocol,
}, "dns" )
end
end
elseif sEvent == "rednet_message" then
-- Got a rednet message (queued from above), respond to dns lookup
local nSenderID, tMessage, sProtocol = p1, p2, p3
if sProtocol == "dns" and type(tMessage) == "table" and tMessage.sType == "lookup" then
local sHostname = tHostnames[ tMessage.sProtocol ]
if sHostname ~= nil and (tMessage.sHostname == nil or tMessage.sHostname == sHostname) then
rednet.send( nSenderID, {
sType = "lookup response",
sHostname = sHostname,
sProtocol = tMessage.sProtocol,
}, "dns" )
end
end
elseif sEvent == "timer" then
elseif sEvent == "timer" then
-- Got a timer event, use it to clear the event queue
local nTimer = p1
local nMessage = tReceivedMessageTimeouts[ nTimer ]
@ -261,6 +261,6 @@ function run()
tReceivedMessageTimeouts[ nTimer ] = nil
tReceivedMessages[ nMessage ] = nil
end
end
end
end
end
end

View File

@ -56,6 +56,7 @@ function getNames()
for k,v in pairs( tSettings ) do
result[ #result + 1 ] = k
end
table.sort(result)
return result
end

View File

@ -7,7 +7,7 @@ function slowWrite( sText, nRate )
local nSleep = 1 / nRate
sText = tostring( sText )
local x,y = term.getCursorPos(x,y)
local x,y = term.getCursorPos()
local len = string.len( sText )
for n=1,len do
@ -338,6 +338,7 @@ end
local tEmpty = {}
function complete( sSearchText, tSearchTable )
if g_tLuaKeywords[sSearchText] then return tEmpty end
local nStart = 1
local nDot = string.find( sSearchText, ".", nStart, true )
local tTable = tSearchTable or _ENV
@ -352,8 +353,19 @@ function complete( sSearchText, tSearchTable )
return tEmpty
end
end
local nColon = string.find( sSearchText, ":", nStart, true )
if nColon then
local sPart = string.sub( sSearchText, nStart, nColon - 1 )
local value = tTable[ sPart ]
if type( value ) == "table" then
tTable = value
nStart = nColon + 1
else
return tEmpty
end
end
local sPart = string.sub( sSearchText, nStart, nDot )
local sPart = string.sub( sSearchText, nStart )
local nPartLength = string.len( sPart )
local tResults = {}

View File

@ -175,7 +175,7 @@ local nCompletion
local tCompleteEnv = _ENV
local function complete( sLine )
if settings.get( "edit.autocomplete" ) then
local nStartPos = string.find( sLine, "[a-zA-Z0-9_%.]+$" )
local nStartPos = string.find( sLine, "[a-zA-Z0-9_%.:]+$" )
if nStartPos then
sLine = string.sub( sLine, nStartPos )
end
@ -709,7 +709,13 @@ while bRunning do
end
elseif sEvent == "paste" then
if not bMenu and not bReadOnly then
if not bReadOnly then
-- Close menu if open
if bMenu then
bMenu = false
term.setCursorBlink( true )
redrawMenu()
end
-- Input text
local sLine = tLines[y]
tLines[y] = string.sub(sLine,1,x-1) .. param .. string.sub(sLine,x)

View File

@ -57,10 +57,6 @@ elseif sCommand == "host" then
x,y,z = gps.locate( 2, true )
if x == nil then
print( "Run \"gps host <x> <y> <z>\" to set position manually" )
if bCloseChannel then
print( "Closing GPS channel" )
modem.close( gps.CHANNEL_GPS )
end
return
end
end

View File

@ -34,7 +34,7 @@ while bRunning do
local s = read( nil, tCommandHistory, function( sLine )
if settings.get( "lua.autocomplete" ) then
local nStartPos = string.find( sLine, "[a-zA-Z0-9_%.]+$" )
local nStartPos = string.find( sLine, "[a-zA-Z0-9_%.:]+$" )
if nStartPos then
sLine = string.sub( sLine, nStartPos )
end
@ -64,10 +64,10 @@ while bRunning do
end
if func then
local tResults = { pcall( func ) }
local tResults = table.pack( pcall( func ) )
if tResults[1] then
local n = 1
while (tResults[n + 1] ~= nil) or (n <= nForcePrint) do
while n < tResults.n or (n <= nForcePrint) do
local value = tResults[ n + 1 ]
if type( value ) == "table" then
local metatable = getmetatable( value )

View File

@ -310,14 +310,14 @@ elseif sCommand == "join" then
function printMessage( sMessage )
term.redirect( historyWindow )
print()
if string.match( sMessage, "^\*" ) then
if string.match( sMessage, "^%*" ) then
-- Information
term.setTextColour( highlightColour )
write( sMessage )
term.setTextColour( textColour )
else
-- Chat
local sUsernameBit = string.match( sMessage, "^\<[^\>]*\>" )
local sUsernameBit = string.match( sMessage, "^<[^>]*>" )
if sUsernameBit then
term.setTextColour( highlightColour )
write( sUsernameBit )

View File

@ -2,9 +2,12 @@
local tArgs = { ... }
if #tArgs == 0 then
-- "set"
local x,y = term.getCursorPos()
local tSettings = {}
for n,sName in ipairs( settings.getNames() ) do
print( textutils.serialize(sName) .. " is " .. textutils.serialize(settings.get(sName)) )
tSettings[n] = textutils.serialize(sName) .. " is " .. textutils.serialize(settings.get(sName))
end
textutils.pagedPrint(table.concat(tSettings,"\n"),y-3)
elseif #tArgs == 1 then
-- "set foo"

View File

@ -31,6 +31,7 @@ local function createShellEnv( sDir )
table = table,
}
package.path = "?;?.lua;?/init.lua"
package.config = "/\n;\n?\n!\n-"
package.preload = {}
package.loaders = {
function( name )

View File

@ -205,6 +205,35 @@ shell.setCompletionFunction( "rom/programs/http/pastebin.lua", completePastebin
shell.setCompletionFunction( "rom/programs/rednet/chat.lua", completeChat )
shell.setCompletionFunction( "rom/programs/command/exec.lua", completeExec )
if turtle then
local tGoOptions = { "left", "right", "forward", "back", "down", "up" }
local function completeGo( shell, nIndex, sText )
return completeMultipleChoice(sText,tGoOptions)
end
local tTurnOptions = { "left", "right" }
local function completeTurn( shell, nIndex, sText )
if nIndex == 1 then
return completeMultipleChoice( sText, tTurnOptions )
end
end
local tEquipOptions = { "left", "right" }
local function completeEquip( shell, nIndex, sText )
if nIndex == 2 then
return completeMultipleChoice( sText, tEquipOptions )
end
end
local function completeUnequip( shell, nIndex, sText )
if nIndex == 1 then
return completeMultipleChoice( sText, tEquipOptions )
end
end
shell.setCompletionFunction( "rom/programs/turtle/go.lua", completeGo )
shell.setCompletionFunction( "rom/programs/turtle/turn.lua", completeTurn )
shell.setCompletionFunction( "rom/programs/turtle/equip.lua", completeEquip )
shell.setCompletionFunction( "rom/programs/turtle/unequip.lua", completeUnequip )
end
-- Run autorun files
if fs.exists( "/rom/autorun" ) and fs.isDir( "/rom/autorun" ) then
local tFiles = fs.list( "/rom/autorun" )