1
0
mirror of https://github.com/kepler155c/opus synced 2025-10-31 07:33:00 +00:00

require improvements, remove global Util

This commit is contained in:
kepler155c@gmail.com
2017-09-05 02:09:31 -04:00
parent 2b1d96ab37
commit ae7508e89b
82 changed files with 506 additions and 455 deletions

View File

@@ -1,6 +1,7 @@
local class = require('class')
local class = require('class')
local Util = require('util')
local TableDB = require('tableDB')
local JSON = require('json')
local JSON = require('json')
-- see https://github.com/Khroki/MCEdit-Unified/blob/master/pymclevel/minecraft.yaml
-- see https://github.com/Khroki/MCEdit-Unified/blob/master/Items/minecraft/blocks.json

View File

@@ -1,5 +1,6 @@
local class = require('class')
local itemDB = require('itemDB')
local class = require('class')
local Util = require('util')
local itemDB = require('itemDB')
local Peripheral = require('peripheral')
local ChestProvider = class()

View File

@@ -1,3 +1,5 @@
local Util = require('util')
local Event = {
uid = 1, -- unique id for handlers
routines = { }, -- coroutines

View File

@@ -1,4 +1,5 @@
local UI = require('ui')
local Util = require('util')
local UI = require('ui')
return function(args)

View File

@@ -1,3 +1,5 @@
local Util = require('util')
local ramfs = { }
function ramfs.mount(dir, nodeType)

View File

@@ -1,4 +1,5 @@
local synchronized = require('sync')
local Util = require('util')
local urlfs = { }

View File

@@ -11,36 +11,6 @@ local DEFAULT_UPATH = 'https://raw.githubusercontent.com/kepler155c/opus/' .. br
local PASTEBIN_URL = 'http://pastebin.com/raw'
local GIT_URL = 'https://raw.githubusercontent.com'
local function standardSearcher(modname, env, shell)
if _G.package.loaded[modname] then
return function()
return _G.package.loaded[modname]
end
end
end
local function shellSearcher(modname, env, shell)
local fname = modname:gsub('%.', '/') .. '.lua'
if shell and type(shell.dir) == 'function' then
local path = shell.resolve(fname)
if fs.exists(path) and not fs.isDir(path) then
return loadfile(path, env)
end
end
end
local function pathSearcher(modname, env, shell)
local fname = modname:gsub('%.', '/') .. '.lua'
for dir in string.gmatch(_G.package.path, "[^:]+") do
local path = fs.combine(dir, fname)
if fs.exists(path) and not fs.isDir(path) then
return loadfile(path, env)
end
end
end
-- fix broken http get
local syncLocks = { }
@@ -81,83 +51,108 @@ local function loadUrl(url)
end
end
-- require('BniCQPVf')
local function pastebinSearcher(modname, env, shell)
if #modname == 8 and not modname:match('%W') then
local url = PASTEBIN_URL .. '/' .. modname
local c = loadUrl(url)
if c then
return load(c, modname, nil, env)
local function requireWrapper(env)
local function standardSearcher(modname, env, shell)
if package.loaded[modname] then
return function()
return package.loaded[modname]
end
end
end
end
-- require('kepler155c.opus.master.sys.apis.util')
local function gitSearcher(modname, env, shell)
local fname = modname:gsub('%.', '/') .. '.lua'
local _, count = fname:gsub("/", "")
if count >= 3 then
local url = GIT_URL .. '/' .. fname
local c = loadUrl(url)
if c then
return load(c, modname, nil, env)
local function shellSearcher(modname, env, shell)
local fname = modname:gsub('%.', '/') .. '.lua'
if shell and type(shell.dir) == 'function' then
local path = shell.resolve(fname)
if fs.exists(path) and not fs.isDir(path) then
return loadfile(path, env)
end
end
end
end
local function urlSearcher(modname, env, shell)
local fname = modname:gsub('%.', '/') .. '.lua'
local function pathSearcher(modname, env, shell)
local fname = modname:gsub('%.', '/') .. '.lua'
if fname:sub(1, 1) ~= '/' then
for entry in string.gmatch(_G.package.upath, "[^;]+") do
local url = entry .. '/' .. fname
for dir in string.gmatch(package.path, "[^:]+") do
local path = fs.combine(dir, fname)
if fs.exists(path) and not fs.isDir(path) then
return loadfile(path, env)
end
end
end
-- require('BniCQPVf')
local function pastebinSearcher(modname, env, shell)
if #modname == 8 and not modname:match('%W') then
local url = PASTEBIN_URL .. '/' .. modname
local c = loadUrl(url)
if c then
return load(c, modname, nil, env)
end
end
end
end
_G.package = {
path = LUA_PATH or 'sys/apis',
upath = LUA_UPATH or DEFAULT_UPATH,
config = '/\n:\n?\n!\n-',
loaded = {
math = math,
string = string,
table = table,
io = io,
os = os,
},
loaders = {
standardSearcher,
shellSearcher,
pathSearcher,
pastebinSearcher,
gitSearcher,
urlSearcher,
}
}
local function requireWrapper(env)
local loaded = { }
return function(modname)
if loaded[modname] then
return loaded[modname]
-- require('kepler155c.opus.master.sys.apis.util')
local function gitSearcher(modname, env, shell)
local fname = modname:gsub('%.', '/') .. '.lua'
local _, count = fname:gsub("/", "")
if count >= 3 then
local url = GIT_URL .. '/' .. fname
local c = loadUrl(url)
if c then
return load(c, modname, nil, env)
end
end
end
for _,searcher in ipairs(_G.package.loaders) do
local function urlSearcher(modname, env, shell)
local fname = modname:gsub('%.', '/') .. '.lua'
if fname:sub(1, 1) ~= '/' then
for entry in string.gmatch(package.upath, "[^;]+") do
local url = entry .. '/' .. fname
local c = loadUrl(url)
if c then
return load(c, modname, nil, env)
end
end
end
end
-- place package and require function into env
package = {
path = LUA_PATH or 'sys/apis',
upath = LUA_UPATH or DEFAULT_UPATH,
config = '/\n:\n?\n!\n-',
loaded = {
math = math,
string = string,
table = table,
io = io,
os = os,
},
loaders = {
standardSearcher,
shellSearcher,
pathSearcher,
pastebinSearcher,
gitSearcher,
urlSearcher,
}
}
function require(modname)
for _,searcher in ipairs(package.loaders) do
local fn, msg = searcher(modname, env, shell)
if fn then
local module, msg = fn(modname, env)
if not module then
error(msg)
end
loaded[modname] = module
package.loaded[modname] = module
return module
end
if msg then
@@ -166,6 +161,8 @@ local function requireWrapper(env)
end
error('Unable to find module ' .. modname)
end
return require -- backwards compatible
end
return function(env)

View File

@@ -1,3 +1,4 @@
local Util = require('util')
local TableDB = require('tableDB')
local itemDB = TableDB({ fileName = 'usr/etc/items.db' })

View File

@@ -1,3 +1,5 @@
local Util = require('util')
local ME = {
jobList = { }
}

View File

@@ -1,5 +1,6 @@
local class = require('class')
local Logger = require('logger')
local class = require('class')
local Util = require('util')
local Logger = require('logger')
local Peripheral = require('peripheral')
local MEProvider = class()

View File

@@ -1,3 +1,5 @@
local Util = require('util')
local Peripheral = { }
local function getDeviceList()

View File

@@ -1,3 +1,4 @@
local Util = require('util')
local Logger = require('logger')
local Profile = {

View File

@@ -1,6 +1,7 @@
local class = require('class')
local class = require('class')
local Util = require('util')
local Peripheral = require('peripheral')
local itemDB = require('itemDB')
local itemDB = require('itemDB')
local RefinedProvider = class()

View File

@@ -1,7 +1,8 @@
local class = require('class')
local class = require('class')
local Util = require('util')
local DEFLATE = require('deflatelua')
local UI = require('ui')
local Point = require('point')
local UI = require('ui')
local Point = require('point')
--[[
Loading and manipulating a schematic

56
sys/apis/security.lua Normal file
View File

@@ -0,0 +1,56 @@
local Config = require('config')
local config = { }
local Security = { }
function Security.verifyPassword(password)
Config.load('os', config)
return config.password and password == config.password
end
function Security.getSecretKey()
Config.load('os', config)
if not config.secretKey then
config.secretKey = math.random(100000, 999999)
Config.update('os', config)
end
return config.secretKey
end
function Security.getPublicKey()
local exchange = {
base = 11,
primeMod = 625210769
}
local function modexp(base, exponent, modulo)
local remainder = base
for i = 1, exponent-1 do
remainder = remainder * remainder
if remainder >= modulo then
remainder = remainder % modulo
end
end
return remainder
end
local secretKey = Security.getSecretKey()
return modexp(exchange.base, secretKey, exchange.primeMod)
end
function Security.updatePassword(password)
Config.load('os', config)
config.password = password
Config.update('os', config)
end
function Security.getPassword()
Config.load('os', config)
return config.password
end
return Security

View File

@@ -1,5 +1,7 @@
local Logger = require('logger')
local Crypto = require('crypto')
local Crypto = require('crypto')
local Logger = require('logger')
local Security = require('security')
local Util = require('util')
local socketClass = { }
@@ -110,7 +112,7 @@ function Socket.connect(host, port)
type = 'OPEN',
shost = socket.shost,
dhost = socket.dhost,
t = Crypto.encrypt({ ts = os.time(), seq = socket.seq }, os.getPublicKey()),
t = Crypto.encrypt({ ts = os.time(), seq = socket.seq }, Security.getPublicKey()),
rseq = socket.wseq,
wseq = socket.rseq,
})

View File

@@ -1,4 +1,5 @@
local class = require('class')
local Util = require('util')
local TableDB = class()
function TableDB:init(args)

View File

@@ -1,3 +1,5 @@
local Util = require('util')
local Terminal = { }
function Terminal.scrollable(ct, size)

View File

@@ -1,7 +1,7 @@
local Util = require('util')
local class = require('class')
local Event = require('event')
local Tween = require('tween')
local Util = require('util')
local class = require('class')
local Event = require('event')
local Tween = require('tween')
local Region = require('region')
local mapColorToGray = {
@@ -3425,7 +3425,7 @@ function UI.NftImage:setImage(image)
end
UI:loadTheme('usr/config/ui.theme')
if os.getVersion and os.getVersion() >= 1.79 then
if Util.getVersion() >= 1.79 then
UI:loadTheme('sys/etc/ext.theme')
end

View File

@@ -69,6 +69,19 @@ function Util.print(pattern, ...)
print(Util.tostring(pattern, ...))
end
function Util.getVersion()
local version
if _CC_VERSION then
version = tonumber(_CC_VERSION:gmatch('[%d]+%.?[%d][%d]', '%1')())
end
if not version and _HOST then
version = tonumber(_HOST:gmatch('[%d]+%.?[%d][%d]', '%1')())
end
return version or 1.7
end
function Util.runFunction(env, fn, ...)
setfenv(fn, env)
setmetatable(env, { __index = _G })