1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-25 06:33:23 +00:00

Some cleanup to the Lua programs

This commit is contained in:
SquidDev 2020-04-22 10:00:43 +01:00
parent d847a4d9e0
commit e5cc345f49
5 changed files with 53 additions and 59 deletions

View File

@ -2,11 +2,10 @@
(sources (sources
/doc/stub/ /doc/stub/
/src/main/resources/assets/computercraft/lua/bios.lua /src/main/resources/data/computercraft/lua/bios.lua
/src/main/resources/assets/computercraft/lua/rom/ /src/main/resources/data/computercraft/lua/rom/
/src/test/resources/test-rom) /src/test/resources/test-rom)
(doc (doc
(title "CC: Tweaked") (title "CC: Tweaked")
(index doc/index.md) (index doc/index.md)
@ -15,13 +14,13 @@
(library-path (library-path
/doc/stub/ /doc/stub/
/src/main/resources/assets/computercraft/lua/rom/apis /src/main/resources/data/computercraft/lua/rom/apis
/src/main/resources/assets/computercraft/lua/rom/apis/command /src/main/resources/data/computercraft/lua/rom/apis/command
/src/main/resources/assets/computercraft/lua/rom/apis/turtle /src/main/resources/data/computercraft/lua/rom/apis/turtle
/src/main/resources/assets/computercraft/lua/rom/modules/main /src/main/resources/data/computercraft/lua/rom/modules/main
/src/main/resources/assets/computercraft/lua/rom/modules/command /src/main/resources/data/computercraft/lua/rom/modules/command
/src/main/resources/assets/computercraft/lua/rom/modules/turtle)) /src/main/resources/data/computercraft/lua/rom/modules/turtle))
(at / (at /
(linters (linters
@ -50,8 +49,8 @@
;; We disable the unused global linter in bios.lua and the APIs. In the future ;; We disable the unused global linter in bios.lua and the APIs. In the future
;; hopefully we'll get illuaminate to handle this. ;; hopefully we'll get illuaminate to handle this.
(at (at
(/src/main/resources/assets/computercraft/lua/bios.lua (/src/main/resources/data/computercraft/lua/bios.lua
/src/main/resources/assets/computercraft/lua/rom/apis/) /src/main/resources/data/computercraft/lua/rom/apis/)
(linters -var:unused-global) (linters -var:unused-global)
(lint (allow-toplevel-global true))) (lint (allow-toplevel-global true)))
@ -62,17 +61,17 @@
;; Ensure any fully documented modules stay fully documented. ;; Ensure any fully documented modules stay fully documented.
(at (at
(/src/main/resources/assets/computercraft/lua/rom/apis/colors.lua (/src/main/resources/data/computercraft/lua/rom/apis/colors.lua
/src/main/resources/assets/computercraft/lua/rom/apis/colours.lua /src/main/resources/data/computercraft/lua/rom/apis/colours.lua
/src/main/resources/assets/computercraft/lua/rom/apis/disk.lua /src/main/resources/data/computercraft/lua/rom/apis/disk.lua
/src/main/resources/assets/computercraft/lua/rom/apis/gps.lua /src/main/resources/data/computercraft/lua/rom/apis/gps.lua
/src/main/resources/assets/computercraft/lua/rom/apis/help.lua /src/main/resources/data/computercraft/lua/rom/apis/help.lua
/src/main/resources/assets/computercraft/lua/rom/apis/keys.lua /src/main/resources/data/computercraft/lua/rom/apis/keys.lua
/src/main/resources/assets/computercraft/lua/rom/apis/paintutils.lua /src/main/resources/data/computercraft/lua/rom/apis/paintutils.lua
/src/main/resources/assets/computercraft/lua/rom/apis/parallel.lua /src/main/resources/data/computercraft/lua/rom/apis/parallel.lua
/src/main/resources/assets/computercraft/lua/rom/apis/peripheral.lua /src/main/resources/data/computercraft/lua/rom/apis/peripheral.lua
/src/main/resources/assets/computercraft/lua/rom/apis/rednet.lua /src/main/resources/data/computercraft/lua/rom/apis/rednet.lua
/src/main/resources/assets/computercraft/lua/rom/apis/settings.lua /src/main/resources/data/computercraft/lua/rom/apis/settings.lua
/src/main/resources/assets/computercraft/lua/rom/apis/texutils.lua /src/main/resources/data/computercraft/lua/rom/apis/texutils.lua
/src/main/resources/assets/computercraft/lua/rom/apis/vector.lua) /src/main/resources/data/computercraft/lua/rom/apis/vector.lua)
(linters doc:undocumented doc:undocumented-arg)) (linters doc:undocumented doc:undocumented-arg))

View File

@ -7,7 +7,6 @@
import dan200.computercraft.api.turtle.event.TurtleAction; import dan200.computercraft.api.turtle.event.TurtleAction;
import dan200.computercraft.core.apis.AddressPredicate; import dan200.computercraft.core.apis.AddressPredicate;
import dan200.computercraft.core.apis.http.websocket.Websocket;
import dan200.computercraft.shared.Config; import dan200.computercraft.shared.Config;
import dan200.computercraft.shared.computer.blocks.BlockComputer; import dan200.computercraft.shared.computer.blocks.BlockComputer;
import dan200.computercraft.shared.computer.core.ClientComputerRegistry; import dan200.computercraft.shared.computer.core.ClientComputerRegistry;
@ -22,9 +21,9 @@
import dan200.computercraft.shared.peripheral.modem.wired.ItemBlockCable; import dan200.computercraft.shared.peripheral.modem.wired.ItemBlockCable;
import dan200.computercraft.shared.peripheral.modem.wireless.BlockWirelessModem; import dan200.computercraft.shared.peripheral.modem.wireless.BlockWirelessModem;
import dan200.computercraft.shared.peripheral.monitor.BlockMonitor; import dan200.computercraft.shared.peripheral.monitor.BlockMonitor;
import dan200.computercraft.shared.peripheral.monitor.MonitorRenderer;
import dan200.computercraft.shared.peripheral.printer.BlockPrinter; import dan200.computercraft.shared.peripheral.printer.BlockPrinter;
import dan200.computercraft.shared.peripheral.speaker.BlockSpeaker; import dan200.computercraft.shared.peripheral.speaker.BlockSpeaker;
import dan200.computercraft.shared.peripheral.monitor.MonitorRenderer;
import dan200.computercraft.shared.pocket.items.ItemPocketComputer; import dan200.computercraft.shared.pocket.items.ItemPocketComputer;
import dan200.computercraft.shared.pocket.peripherals.PocketModem; import dan200.computercraft.shared.pocket.peripherals.PocketModem;
import dan200.computercraft.shared.pocket.peripherals.PocketSpeaker; import dan200.computercraft.shared.pocket.peripherals.PocketSpeaker;

View File

@ -95,10 +95,10 @@ public void update()
if( time > previousTime || day > previousDay ) if( time > previousTime || day > previousDay )
{ {
double now = m_day * 24.0 + m_time; double now = m_day * 24.0 + m_time;
Iterator<Map.Entry<Integer, Alarm>> it = m_alarms.entrySet().iterator(); Iterator<Int2ObjectMap.Entry<Alarm>> it = m_alarms.int2ObjectEntrySet().iterator();
while( it.hasNext() ) while( it.hasNext() )
{ {
Map.Entry<Integer, Alarm> entry = it.next(); Int2ObjectMap.Entry<Alarm> entry = it.next();
Alarm alarm = entry.getValue(); Alarm alarm = entry.getValue();
double t = alarm.m_day * 24.0 + alarm.m_time; double t = alarm.m_day * 24.0 + alarm.m_time;
if( now >= t ) if( now >= t )

View File

@ -14,7 +14,7 @@
-- @module commands -- @module commands
if not commands then if not commands then
error( "Cannot load command API on normal computer", 2 ) error("Cannot load command API on normal computer", 2)
end end
--- The builtin commands API, without any generated command helper functions --- The builtin commands API, without any generated command helper functions
@ -23,16 +23,16 @@ end
-- overwritten by a command. -- overwritten by a command.
local native = commands.native or commands local native = commands.native or commands
local function collapseArgs( bJSONIsNBT, ... ) local function collapseArgs(bJSONIsNBT, ...)
local args = table.pack(...) local args = table.pack(...)
for i = 1, #args do for i = 1, #args do
local arg = args[i] local arg = args[i]
if type(arg) == "boolean" or type(arg) == "number" or type(arg) == "string" then if type(arg) == "boolean" or type(arg) == "number" or type(arg) == "string" then
args[i] = tostring(arg) args[i] = tostring(arg)
elseif type(arg) == "table" then elseif type(arg) == "table" then
args[i] = textutils.serialiseJSON( arg, bJSONIsNBT ) args[i] = textutils.serialiseJSON(arg, bJSONIsNBT)
else else
error( "Expected string, number, boolean or table", 3 ) error("Expected string, number, boolean or table", 3)
end end
end end
@ -42,22 +42,22 @@ end
-- Put native functions into the environment -- Put native functions into the environment
local env = _ENV local env = _ENV
env.native = native env.native = native
for k,v in pairs( native ) do for k, v in pairs(native) do
env[k] = v env[k] = v
end end
-- Create wrapper functions for all the commands -- Create wrapper functions for all the commands
local tAsync = {} local tAsync = {}
local tNonNBTJSONCommands = { local tNonNBTJSONCommands = {
[ "tellraw" ] = true, ["tellraw"] = true,
[ "title" ] = true ["title"] = true,
} }
local command_mt = {} local command_mt = {}
function command_mt.__call(self, ...) function command_mt.__call(self, ...)
local meta = self[command_mt] local meta = self[command_mt]
local sCommand = collapseArgs( meta.json, table.concat(meta.name, " "), ... ) local sCommand = collapseArgs(meta.json, table.concat(meta.name, " "), ...)
return meta.func( sCommand ) return meta.func(sCommand)
end end
function command_mt.__tostring(self) function command_mt.__tostring(self)
@ -65,6 +65,16 @@ function command_mt.__tostring(self)
return ("command %q"):format("/" .. table.concat(meta.name, " ")) return ("command %q"):format("/" .. table.concat(meta.name, " "))
end end
local function mk_command(name, json, func)
return setmetatable({
[command_mt] = {
name = name,
func = func,
json = json,
},
}, command_mt)
end
function command_mt.__index(self, key) function command_mt.__index(self, key)
local meta = self[command_mt] local meta = self[command_mt]
if meta.children then return nil end if meta.children then return nil end
@ -74,31 +84,17 @@ function command_mt.__index(self, key)
for _, child in ipairs(native.list(table.unpack(name))) do for _, child in ipairs(native.list(table.unpack(name))) do
local child_name = { table.unpack(name) } local child_name = { table.unpack(name) }
child_name[#child_name + 1] = child child_name[#child_name + 1] = child
self[child] = mk_command(child_name, meta.json, meta.func)
self[child] = setmetatable({ [command_mt] = {
name = child_name,
func = meta.func,
json = meta.json
} }, command_mt)
end end
return self[key] return self[key]
end end
for _, sCommandName in ipairs(native.list()) do for _, sCommandName in ipairs(native.list()) do
if env[ sCommandName ] == nil then if env[sCommandName] == nil then
local bJSONIsNBT = tNonNBTJSONCommands[ sCommandName ] == nil local bJSONIsNBT = tNonNBTJSONCommands[sCommandName] == nil
env[ sCommandName ] = setmetatable({ [command_mt] = { env[sCommandName] = mk_command({ sCommandName }, bJSONIsNBT, native.exec)
name = { sCommandName }, tAsync[sCommandName] = mk_command({ sCommandName }, bJSONIsNBT, native.execAsync)
func = native.exec,
json = bJSONIsNBT
} }, command_mt)
tAsync[ sCommandName ] = setmetatable({ [command_mt] = {
name = { sCommandName },
func = native.execAsync,
json = bJSONIsNBT
} }, command_mt)
end end
end end
env.async = tAsync env.async = tAsync

View File

@ -131,7 +131,7 @@ tKeys[346] = 'rightAlt'
-- tKeys[348] = 'menu' -- tKeys[348] = 'menu'
local keys = _ENV local keys = _ENV
for nKey, sKey in pairs( tKeys ) do for nKey, sKey in pairs(tKeys) do
keys[sKey] = nKey keys[sKey] = nKey
end end
@ -145,7 +145,7 @@ keys.cimcumflex = keys.circumflex --- @local
-- --
-- @tparam number code The key code to look up. -- @tparam number code The key code to look up.
-- @treturn string|nil The name of the key, or `nil` if not a valid key code. -- @treturn string|nil The name of the key, or `nil` if not a valid key code.
function getName( _nKey ) function getName(_nKey)
expect(1, _nKey, "number") expect(1, _nKey, "number")
return tKeys[ _nKey ] return tKeys[_nKey]
end end