1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-10-27 11:57:38 +00:00

Merge branch 'master' into mc-1.14.x

This commit is contained in:
SquidDev
2019-08-04 10:57:20 +01:00
72 changed files with 1329 additions and 498 deletions

View File

@@ -1,48 +1,19 @@
local native_select, native_type = select, type
--- Expect an argument to have a specific type.
-- Load in expect from the module path.
--
-- @tparam int index The 1-based argument index.
-- @param value The argument's value.
-- @tparam string ... The allowed types of the argument.
-- @throws If the value is not one of the allowed types.
local function expect(index, value, ...)
local t = native_type(value)
for i = 1, native_select("#", ...) do
if t == native_select(i, ...) then return true end
end
-- Ideally we'd use require, but that is part of the shell, and so is not
-- available to the BIOS or any APIs. All APIs load this using dofile, but that
-- has not been defined at this point.
local expect
local types = table.pack(...)
for i = types.n, 1, -1 do
if types[i] == "nil" then table.remove(types, i) end
end
do
local h = fs.open("rom/modules/main/cc/expect.lua", "r")
local f, err = loadstring(h.readAll(), "@expect.lua")
h.close()
local type_names
if #types <= 1 then
type_names = tostring(...)
else
type_names = table.concat(types, ", ", 1, #types - 1) .. " or " .. types[#types]
end
-- If we can determine the function name with a high level of confidence, try to include it.
local name
if native_type(debug) == "table" and native_type(debug.getinfo) == "function" then
local ok, info = pcall(debug.getinfo, 3, "nS")
if ok and info.name and #info.name ~= "" and info.what ~= "C" then name = info.name end
end
if name then
error( ("bad argument #%d to '%s' (expected %s, got %s)"):format(index, name, type_names, t), 3 )
else
error( ("bad argument #%d (expected %s, got %s)"):format(index, type_names, t), 3 )
end
if not f then error(err) end
expect = f().expect
end
-- We expose expect in the global table as APIs need to access it, but give it
-- a non-identifier name - meaning it does not show up in auto-completion.
-- expect is an internal function, and should not be used by users.
_G["~expect"] = expect
if _VERSION == "Lua 5.1" then
-- If we're on Lua 5.1, install parts of the Lua 5.2/5.3 API so that programs can be written against it
local type = type
@@ -568,23 +539,28 @@ function read( _sReplaceChar, _tHistory, _fnComplete, _sDefault )
return sLine
end
function loadfile( _sFile, _tEnv )
expect(1, _sFile, "string")
expect(2, _tEnv, "table", "nil")
local file = fs.open( _sFile, "r" )
if file then
local func, err = load( file.readAll(), "@" .. fs.getName( _sFile ), "t", _tEnv )
file.close()
return func, err
function loadfile( filename, mode, env )
-- Support the previous `loadfile(filename, env)` form instead.
if type(mode) == "table" and env == nil then
mode, env = nil, mode
end
return nil, "File not found"
expect(1, filename, "string")
expect(2, mode, "string", "nil")
expect(3, env, "table", "nil")
local file = fs.open( filename, "r" )
if not file then return nil, "File not found" end
local func, err = load( file.readAll(), "@" .. fs.getName( filename ), mode, env )
file.close()
return func, err
end
function dofile( _sFile )
expect(1, _sFile, "string")
local fnFile, e = loadfile( _sFile, _G )
local fnFile, e = loadfile( _sFile, nil, _G )
if fnFile then
return fnFile()
else
@@ -600,7 +576,7 @@ function os.run( _tEnv, _sPath, ... )
local tArgs = table.pack( ... )
local tEnv = _tEnv
setmetatable( tEnv, { __index = _G } )
local fnFile, err = loadfile( _sPath, tEnv )
local fnFile, err = loadfile( _sPath, nil, tEnv )
if fnFile then
local ok, err = pcall( function()
fnFile( table.unpack( tArgs, 1, tArgs.n ) )
@@ -634,7 +610,7 @@ function os.loadAPI( _sPath )
local tEnv = {}
setmetatable( tEnv, { __index = _G } )
local fnAPI, err = loadfile( _sPath, tEnv )
local fnAPI, err = loadfile( _sPath, nil, tEnv )
if fnAPI then
local ok, err = pcall( fnAPI )
if not ok then

View File

@@ -1,4 +1,4 @@
local expect = _G["~expect"]
local expect = dofile("rom/modules/main/cc/expect.lua").expect
-- Colors
white = 1

View File

@@ -1,4 +1,4 @@
local expect = _G["~expect"]
local expect = dofile("rom/modules/main/cc/expect.lua").expect
CHANNEL_GPS = 65534

View File

@@ -1,4 +1,4 @@
local expect = _G["~expect"]
local expect = dofile("rom/modules/main/cc/expect.lua").expect
local sPath = "/rom/help"

View File

@@ -1,6 +1,6 @@
-- Definition for the IO API
local expect, typeOf = _G["~expect"], _G.type
local expect, typeOf = dofile("rom/modules/main/cc/expect.lua").expect, _G.type
--- If we return nil then close the file, as we've reached the end.
-- We use this weird wrapper function as we wish to preserve the varargs

View File

@@ -8,7 +8,7 @@
-- taught me anything, it's that emulating LWJGL's weird key handling is nigh-on
-- impossible.
local expect = _G["~expect"]
local expect = dofile("rom/modules/main/cc/expect.lua").expect
local tKeys = {}
tKeys[32] = 'space'

View File

@@ -1,4 +1,4 @@
local expect = _G["~expect"]
local expect = dofile("rom/modules/main/cc/expect.lua").expect
local function drawPixelInternal( xPos, yPos )
term.setCursorPos( xPos, yPos )

View File

@@ -1,4 +1,4 @@
local expect = _G["~expect"]
local expect = dofile("rom/modules/main/cc/expect.lua").expect
local native = peripheral

View File

@@ -1,4 +1,4 @@
local expect = _G["~expect"]
local expect = dofile("rom/modules/main/cc/expect.lua").expect
CHANNEL_BROADCAST = 65535
CHANNEL_REPEAT = 65533

View File

@@ -1,4 +1,4 @@
local expect = _G["~expect"]
local expect = dofile("rom/modules/main/cc/expect.lua").expect
local tSettings = {}

View File

@@ -1,4 +1,4 @@
local expect = _G["~expect"]
local expect = dofile("rom/modules/main/cc/expect.lua").expect
local native = (term.native and term.native()) or term
local redirectTarget = native

View File

@@ -1,4 +1,4 @@
local expect = _G["~expect"]
local expect = dofile("rom/modules/main/cc/expect.lua").expect
function slowWrite( sText, nRate )
expect(2, nRate, "number", "nil")

View File

@@ -1,4 +1,4 @@
local expect = _G["~expect"]
local expect = dofile("rom/modules/main/cc/expect.lua").expect
local tHex = {
[ colors.white ] = "0",
@@ -388,6 +388,16 @@ function create( parent, nX, nY, nWidth, nHeight, bStartVisible )
return nBackgroundColor
end
function window.getLine(y)
if type(y) ~= "number" then expect(1, y, "number") end
if y < 1 or y > nHeight then
error("Line is out of range.", 2)
end
return tLines[y].text, tLines[y].textColor, tLines[y].backgroundColor
end
-- Other functions
function window.setVisible( bVis )
if type(bVis) ~= "boolean" then expect(1, bVis, "boolean") end

View File

@@ -1,3 +1,20 @@
New features in CC: Tweaked 1.84.0
* Improve validation in rename, copy and delete programs
* Add window.getLine - the inverse of blit
* turtle.refuel no longer consumes more fuel than needed
* Add "cc.expect" module, for improved argument type checks
* Mount the ROM from all mod jars, not just CC's
And several bug fixes:
* Ensure file error messages use the absolute correct path
* Fix NPE when closing a file multiple times.
* Do not load chunks when calling writeDescription.
* Fix the signature of loadfile
* Fix turtles harvesting blocks multiple times
* Improve thread-safety of various peripherals
* Prevent printed pages having massive/malformed titles
# New features in CC: Tweaked 1.83.1
* Add several new MOTD messages (JakobDev)

View File

@@ -1,10 +1,18 @@
New features in CC: Tweaked 1.83.1
New features in CC: Tweaked 1.84.0
* Add several new MOTD messages (JakobDev)
* Improve validation in rename, copy and delete programs
* Add window.getLine - the inverse of blit
* turtle.refuel no longer consumes more fuel than needed
* Add "cc.expect" module, for improved argument type checks
* Mount the ROM from all mod jars, not just CC's
And several bug fixes:
* Fix type check in `rednet.lookup`
* Error if turtle and pocket computer programs are run on the wrong system (JakobDev)
* Do not discard varargs after a nil.
* Ensure file error messages use the absolute correct path
* Fix NPE when closing a file multiple times.
* Do not load chunks when calling writeDescription.
* Fix the signature of loadfile
* Fix turtles harvesting blocks multiple times
* Improve thread-safety of various peripherals
* Prevent printed pages having massive/malformed titles
Type "help changelog" to see the full version history.

View File

@@ -23,3 +23,4 @@ getPosition()
reposition( x, y, width, height )
getPaletteColor( color )
setPaletteColor( color, r, g, b )
getLine()

View File

@@ -1,4 +1,4 @@
local expect = _G["~expect"]
local expect = dofile("rom/modules/main/cc/expect.lua").expect
-- Setup process switching
local parentTerm = term.current()

View File

@@ -329,7 +329,7 @@ local tMenuFuncs = {
printer.setPageTitle( sName.." (page "..nPage..")" )
end
while not printer.newPage() do
while not printer.newPage() do
if printer.getInkLevel() < 1 then
sStatus = "Printer out of ink, please refill"
elseif printer.getPaperLevel() < 1 then
@@ -342,7 +342,6 @@ local tMenuFuncs = {
redrawMenu()
term.redirect( printerTerminal )
local timer = os.startTimer(0.5)
sleep(0.5)
end

View File

@@ -1,4 +1,4 @@
local expect = _G["~expect"]
local expect = dofile("rom/modules/main/cc/expect.lua").expect
local multishell = multishell
local parentShell = shell
@@ -56,7 +56,7 @@ local function createShellEnv( sDir )
sPath = fs.combine(sDir, sPath)
end
if fs.exists(sPath) and not fs.isDir(sPath) then
local fnFile, sError = loadfile( sPath, tEnv )
local fnFile, sError = loadfile( sPath, nil, tEnv )
if fnFile then
return fnFile, sPath
else