mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-01-26 00:46:54 +00:00
Some cleanup to the Lua programs
This commit is contained in:
parent
d847a4d9e0
commit
e5cc345f49
@ -2,11 +2,10 @@
|
||||
|
||||
(sources
|
||||
/doc/stub/
|
||||
/src/main/resources/assets/computercraft/lua/bios.lua
|
||||
/src/main/resources/assets/computercraft/lua/rom/
|
||||
/src/main/resources/data/computercraft/lua/bios.lua
|
||||
/src/main/resources/data/computercraft/lua/rom/
|
||||
/src/test/resources/test-rom)
|
||||
|
||||
|
||||
(doc
|
||||
(title "CC: Tweaked")
|
||||
(index doc/index.md)
|
||||
@ -15,13 +14,13 @@
|
||||
(library-path
|
||||
/doc/stub/
|
||||
|
||||
/src/main/resources/assets/computercraft/lua/rom/apis
|
||||
/src/main/resources/assets/computercraft/lua/rom/apis/command
|
||||
/src/main/resources/assets/computercraft/lua/rom/apis/turtle
|
||||
/src/main/resources/data/computercraft/lua/rom/apis
|
||||
/src/main/resources/data/computercraft/lua/rom/apis/command
|
||||
/src/main/resources/data/computercraft/lua/rom/apis/turtle
|
||||
|
||||
/src/main/resources/assets/computercraft/lua/rom/modules/main
|
||||
/src/main/resources/assets/computercraft/lua/rom/modules/command
|
||||
/src/main/resources/assets/computercraft/lua/rom/modules/turtle))
|
||||
/src/main/resources/data/computercraft/lua/rom/modules/main
|
||||
/src/main/resources/data/computercraft/lua/rom/modules/command
|
||||
/src/main/resources/data/computercraft/lua/rom/modules/turtle))
|
||||
|
||||
(at /
|
||||
(linters
|
||||
@ -50,8 +49,8 @@
|
||||
;; We disable the unused global linter in bios.lua and the APIs. In the future
|
||||
;; hopefully we'll get illuaminate to handle this.
|
||||
(at
|
||||
(/src/main/resources/assets/computercraft/lua/bios.lua
|
||||
/src/main/resources/assets/computercraft/lua/rom/apis/)
|
||||
(/src/main/resources/data/computercraft/lua/bios.lua
|
||||
/src/main/resources/data/computercraft/lua/rom/apis/)
|
||||
(linters -var:unused-global)
|
||||
(lint (allow-toplevel-global true)))
|
||||
|
||||
@ -62,17 +61,17 @@
|
||||
|
||||
;; Ensure any fully documented modules stay fully documented.
|
||||
(at
|
||||
(/src/main/resources/assets/computercraft/lua/rom/apis/colors.lua
|
||||
/src/main/resources/assets/computercraft/lua/rom/apis/colours.lua
|
||||
/src/main/resources/assets/computercraft/lua/rom/apis/disk.lua
|
||||
/src/main/resources/assets/computercraft/lua/rom/apis/gps.lua
|
||||
/src/main/resources/assets/computercraft/lua/rom/apis/help.lua
|
||||
/src/main/resources/assets/computercraft/lua/rom/apis/keys.lua
|
||||
/src/main/resources/assets/computercraft/lua/rom/apis/paintutils.lua
|
||||
/src/main/resources/assets/computercraft/lua/rom/apis/parallel.lua
|
||||
/src/main/resources/assets/computercraft/lua/rom/apis/peripheral.lua
|
||||
/src/main/resources/assets/computercraft/lua/rom/apis/rednet.lua
|
||||
/src/main/resources/assets/computercraft/lua/rom/apis/settings.lua
|
||||
/src/main/resources/assets/computercraft/lua/rom/apis/texutils.lua
|
||||
/src/main/resources/assets/computercraft/lua/rom/apis/vector.lua)
|
||||
(/src/main/resources/data/computercraft/lua/rom/apis/colors.lua
|
||||
/src/main/resources/data/computercraft/lua/rom/apis/colours.lua
|
||||
/src/main/resources/data/computercraft/lua/rom/apis/disk.lua
|
||||
/src/main/resources/data/computercraft/lua/rom/apis/gps.lua
|
||||
/src/main/resources/data/computercraft/lua/rom/apis/help.lua
|
||||
/src/main/resources/data/computercraft/lua/rom/apis/keys.lua
|
||||
/src/main/resources/data/computercraft/lua/rom/apis/paintutils.lua
|
||||
/src/main/resources/data/computercraft/lua/rom/apis/parallel.lua
|
||||
/src/main/resources/data/computercraft/lua/rom/apis/peripheral.lua
|
||||
/src/main/resources/data/computercraft/lua/rom/apis/rednet.lua
|
||||
/src/main/resources/data/computercraft/lua/rom/apis/settings.lua
|
||||
/src/main/resources/data/computercraft/lua/rom/apis/texutils.lua
|
||||
/src/main/resources/data/computercraft/lua/rom/apis/vector.lua)
|
||||
(linters doc:undocumented doc:undocumented-arg))
|
||||
|
@ -7,7 +7,6 @@ package dan200.computercraft;
|
||||
|
||||
import dan200.computercraft.api.turtle.event.TurtleAction;
|
||||
import dan200.computercraft.core.apis.AddressPredicate;
|
||||
import dan200.computercraft.core.apis.http.websocket.Websocket;
|
||||
import dan200.computercraft.shared.Config;
|
||||
import dan200.computercraft.shared.computer.blocks.BlockComputer;
|
||||
import dan200.computercraft.shared.computer.core.ClientComputerRegistry;
|
||||
@ -22,9 +21,9 @@ import dan200.computercraft.shared.peripheral.modem.wired.BlockWiredModemFull;
|
||||
import dan200.computercraft.shared.peripheral.modem.wired.ItemBlockCable;
|
||||
import dan200.computercraft.shared.peripheral.modem.wireless.BlockWirelessModem;
|
||||
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.speaker.BlockSpeaker;
|
||||
import dan200.computercraft.shared.peripheral.monitor.MonitorRenderer;
|
||||
import dan200.computercraft.shared.pocket.items.ItemPocketComputer;
|
||||
import dan200.computercraft.shared.pocket.peripherals.PocketModem;
|
||||
import dan200.computercraft.shared.pocket.peripherals.PocketSpeaker;
|
||||
|
@ -95,10 +95,10 @@ public class OSAPI implements ILuaAPI
|
||||
if( time > previousTime || day > previousDay )
|
||||
{
|
||||
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() )
|
||||
{
|
||||
Map.Entry<Integer, Alarm> entry = it.next();
|
||||
Int2ObjectMap.Entry<Alarm> entry = it.next();
|
||||
Alarm alarm = entry.getValue();
|
||||
double t = alarm.m_day * 24.0 + alarm.m_time;
|
||||
if( now >= t )
|
||||
|
@ -14,7 +14,7 @@
|
||||
-- @module commands
|
||||
|
||||
if not commands then
|
||||
error( "Cannot load command API on normal computer", 2 )
|
||||
error("Cannot load command API on normal computer", 2)
|
||||
end
|
||||
|
||||
--- The builtin commands API, without any generated command helper functions
|
||||
@ -23,16 +23,16 @@ end
|
||||
-- overwritten by a command.
|
||||
local native = commands.native or commands
|
||||
|
||||
local function collapseArgs( bJSONIsNBT, ... )
|
||||
local function collapseArgs(bJSONIsNBT, ...)
|
||||
local args = table.pack(...)
|
||||
for i = 1, #args do
|
||||
local arg = args[i]
|
||||
if type(arg) == "boolean" or type(arg) == "number" or type(arg) == "string" then
|
||||
args[i] = tostring(arg)
|
||||
elseif type(arg) == "table" then
|
||||
args[i] = textutils.serialiseJSON( arg, bJSONIsNBT )
|
||||
args[i] = textutils.serialiseJSON(arg, bJSONIsNBT)
|
||||
else
|
||||
error( "Expected string, number, boolean or table", 3 )
|
||||
error("Expected string, number, boolean or table", 3)
|
||||
end
|
||||
end
|
||||
|
||||
@ -42,22 +42,22 @@ end
|
||||
-- Put native functions into the environment
|
||||
local env = _ENV
|
||||
env.native = native
|
||||
for k,v in pairs( native ) do
|
||||
for k, v in pairs(native) do
|
||||
env[k] = v
|
||||
end
|
||||
|
||||
-- Create wrapper functions for all the commands
|
||||
local tAsync = {}
|
||||
local tNonNBTJSONCommands = {
|
||||
[ "tellraw" ] = true,
|
||||
[ "title" ] = true
|
||||
["tellraw"] = true,
|
||||
["title"] = true,
|
||||
}
|
||||
|
||||
local command_mt = {}
|
||||
function command_mt.__call(self, ...)
|
||||
local meta = self[command_mt]
|
||||
local sCommand = collapseArgs( meta.json, table.concat(meta.name, " "), ... )
|
||||
return meta.func( sCommand )
|
||||
local sCommand = collapseArgs(meta.json, table.concat(meta.name, " "), ...)
|
||||
return meta.func(sCommand)
|
||||
end
|
||||
|
||||
function command_mt.__tostring(self)
|
||||
@ -65,6 +65,16 @@ function command_mt.__tostring(self)
|
||||
return ("command %q"):format("/" .. table.concat(meta.name, " "))
|
||||
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)
|
||||
local meta = self[command_mt]
|
||||
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
|
||||
local child_name = { table.unpack(name) }
|
||||
child_name[#child_name + 1] = child
|
||||
|
||||
self[child] = setmetatable({ [command_mt] = {
|
||||
name = child_name,
|
||||
func = meta.func,
|
||||
json = meta.json
|
||||
} }, command_mt)
|
||||
self[child] = mk_command(child_name, meta.json, meta.func)
|
||||
end
|
||||
|
||||
return self[key]
|
||||
end
|
||||
|
||||
for _, sCommandName in ipairs(native.list()) do
|
||||
if env[ sCommandName ] == nil then
|
||||
local bJSONIsNBT = tNonNBTJSONCommands[ sCommandName ] == nil
|
||||
env[ sCommandName ] = setmetatable({ [command_mt] = {
|
||||
name = { sCommandName },
|
||||
func = native.exec,
|
||||
json = bJSONIsNBT
|
||||
} }, command_mt)
|
||||
|
||||
tAsync[ sCommandName ] = setmetatable({ [command_mt] = {
|
||||
name = { sCommandName },
|
||||
func = native.execAsync,
|
||||
json = bJSONIsNBT
|
||||
} }, command_mt)
|
||||
if env[sCommandName] == nil then
|
||||
local bJSONIsNBT = tNonNBTJSONCommands[sCommandName] == nil
|
||||
env[sCommandName] = mk_command({ sCommandName }, bJSONIsNBT, native.exec)
|
||||
tAsync[sCommandName] = mk_command({ sCommandName }, bJSONIsNBT, native.execAsync)
|
||||
end
|
||||
end
|
||||
env.async = tAsync
|
||||
|
@ -131,7 +131,7 @@ tKeys[346] = 'rightAlt'
|
||||
-- tKeys[348] = 'menu'
|
||||
|
||||
local keys = _ENV
|
||||
for nKey, sKey in pairs( tKeys ) do
|
||||
for nKey, sKey in pairs(tKeys) do
|
||||
keys[sKey] = nKey
|
||||
end
|
||||
|
||||
@ -145,7 +145,7 @@ keys.cimcumflex = keys.circumflex --- @local
|
||||
--
|
||||
-- @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.
|
||||
function getName( _nKey )
|
||||
function getName(_nKey)
|
||||
expect(1, _nKey, "number")
|
||||
return tKeys[ _nKey ]
|
||||
return tKeys[_nKey]
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user