Added dynamic object/plugin loading
Added basalt.addObject(path) and basalt.addPlugin(path) path can also be a folder Added basalt.getAvailableObjects and basalt.getAvailablePlugins to get a list with all added objects/plugins
This commit is contained in:
100
Basalt/main.lua
100
Basalt/main.lua
@@ -1,5 +1,6 @@
|
|||||||
local basaltEvent = require("basaltEvent")()
|
local basaltEvent = require("basaltEvent")()
|
||||||
local _OBJECTS = require("loadObjects")
|
local baseObjects = require("loadObjects")
|
||||||
|
local moddedObjects
|
||||||
local pluginSystem = require("plugin")
|
local pluginSystem = require("plugin")
|
||||||
local utils = require("utils")
|
local utils = require("utils")
|
||||||
local log = require("basaltLogs")
|
local log = require("basaltLogs")
|
||||||
@@ -9,6 +10,7 @@ local count = utils.tableCount
|
|||||||
local moveThrottle = 300
|
local moveThrottle = 300
|
||||||
local dragThrottle = 0
|
local dragThrottle = 0
|
||||||
local renderingThrottle = 0
|
local renderingThrottle = 0
|
||||||
|
local newObjects = {}
|
||||||
|
|
||||||
local baseTerm = term.current()
|
local baseTerm = term.current()
|
||||||
local version = "1.7.0"
|
local version = "1.7.0"
|
||||||
@@ -123,16 +125,15 @@ local bInstance = {
|
|||||||
schedule = schedule,
|
schedule = schedule,
|
||||||
|
|
||||||
stop = stop,
|
stop = stop,
|
||||||
newFrame = Frame,
|
|
||||||
debug = basalt.debug,
|
debug = basalt.debug,
|
||||||
log = basalt.log,
|
log = basalt.log,
|
||||||
|
|
||||||
getObjects = function()
|
getObjects = function()
|
||||||
return _OBJECTS
|
return moddedObjects
|
||||||
end,
|
end,
|
||||||
|
|
||||||
getObject = function(id)
|
getObject = function(id)
|
||||||
return _OBJECTS[id]
|
return moddedObjects[id]
|
||||||
end,
|
end,
|
||||||
|
|
||||||
getDirectory = function()
|
getDirectory = function()
|
||||||
@@ -151,7 +152,7 @@ local function defaultErrorHandler(errMsg)
|
|||||||
|
|
||||||
local text = wrapText("Basalt error: "..errMsg, w)
|
local text = wrapText("Basalt error: "..errMsg, w)
|
||||||
local yPos = 1
|
local yPos = 1
|
||||||
for k,v in pairs(text)do
|
for _,v in pairs(text)do
|
||||||
baseTerm.setCursorPos(1,yPos)
|
baseTerm.setCursorPos(1,yPos)
|
||||||
baseTerm.write(v)
|
baseTerm.write(v)
|
||||||
yPos = yPos + 1
|
yPos = yPos + 1
|
||||||
@@ -303,10 +304,10 @@ local function basaltUpdateEvent(event, ...)
|
|||||||
elseif(event=="timer")and(a[1]==renderingTimer)then
|
elseif(event=="timer")and(a[1]==renderingTimer)then
|
||||||
renderingUpdateTimer()
|
renderingUpdateTimer()
|
||||||
else
|
else
|
||||||
for k, v in pairs(frames) do
|
for _, v in pairs(frames) do
|
||||||
v:eventHandler(event, ...)
|
v:eventHandler(event, ...)
|
||||||
end
|
end
|
||||||
for k, v in pairs(monFrames) do
|
for _, v in pairs(monFrames) do
|
||||||
v:eventHandler(event, ...)
|
v:eventHandler(event, ...)
|
||||||
end
|
end
|
||||||
handleSchedules(event, ...)
|
handleSchedules(event, ...)
|
||||||
@@ -314,13 +315,69 @@ local function basaltUpdateEvent(event, ...)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local loadedObjects = false
|
||||||
|
local loadedPlugins = false
|
||||||
|
local function InitializeBasalt()
|
||||||
|
if not(loadedObjects)then
|
||||||
|
for _,v in pairs(newObjects)do
|
||||||
|
if(fs.exists(v))then
|
||||||
|
if(fs.isDir(v))then
|
||||||
|
local files = fs.list(v)
|
||||||
|
for _,object in pairs(files)do
|
||||||
|
if not(fs.isDir(v.."/"..object))then
|
||||||
|
local name = object:gsub(".lua", "")
|
||||||
|
if(name~="example.lua")and not(name:find(".disabled"))then
|
||||||
|
if(baseObjects[name]==nil)then
|
||||||
|
baseObjects[name] = require(v.."."..object:gsub(".lua", ""))
|
||||||
|
else
|
||||||
|
error("Duplicate object name: "..name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
local name = v:gsub(".lua", "")
|
||||||
|
if(baseObjects[name]==nil)then
|
||||||
|
baseObjects[name] = require(name)
|
||||||
|
else
|
||||||
|
error("Duplicate object name: "..name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
loadedObjects = true
|
||||||
|
end
|
||||||
|
if not(loadedPlugins)then
|
||||||
|
moddedObjects = pluginSystem.loadPlugins(baseObjects, bInstance)
|
||||||
|
local basaltPlugins = pluginSystem.get("basalt")
|
||||||
|
if(basaltPlugins~=nil)then
|
||||||
|
for k,v in pairs(basaltPlugins)do
|
||||||
|
for a,b in pairs(v(basalt))do
|
||||||
|
basalt[a] = b
|
||||||
|
bInstance[a] = b
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local basaltPlugins = pluginSystem.get("basaltInternal")
|
||||||
|
if(basaltPlugins~=nil)then
|
||||||
|
for _,v in pairs(basaltPlugins)do
|
||||||
|
for a,b in pairs(v(basalt))do
|
||||||
|
bInstance[a] = b
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
loadedPlugins = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local function createFrame(name)
|
local function createFrame(name)
|
||||||
|
InitializeBasalt()
|
||||||
for _, v in pairs(frames) do
|
for _, v in pairs(frames) do
|
||||||
if (v:getName() == name) then
|
if (v:getName() == name) then
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
local newFrame = _OBJECTS["BaseFrame"](name, bInstance)
|
local newFrame = moddedObjects["BaseFrame"](name, bInstance)
|
||||||
newFrame:init()
|
newFrame:init()
|
||||||
newFrame:load()
|
newFrame:load()
|
||||||
newFrame:draw()
|
newFrame:draw()
|
||||||
@@ -345,6 +402,28 @@ basalt = {
|
|||||||
return math.floor(collectgarbage("count")+0.5).."KB"
|
return math.floor(collectgarbage("count")+0.5).."KB"
|
||||||
end,
|
end,
|
||||||
|
|
||||||
|
addObject = function(path)
|
||||||
|
if(fs.exists(path))then
|
||||||
|
table.insert(newObjects, path)
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
|
||||||
|
addPlugin = function(path)
|
||||||
|
pluginSystem.addPlugin(path)
|
||||||
|
end,
|
||||||
|
|
||||||
|
getAvailablePlugins = function()
|
||||||
|
return pluginSystem.getAvailablePlugins()
|
||||||
|
end,
|
||||||
|
|
||||||
|
getAvailableObjects = function()
|
||||||
|
local objectNames = {}
|
||||||
|
for k,_ in pairs(baseObjects)do
|
||||||
|
table.insert(objectNames, k)
|
||||||
|
end
|
||||||
|
return objectNames
|
||||||
|
end,
|
||||||
|
|
||||||
setVariable = setVariable,
|
setVariable = setVariable,
|
||||||
getVariable = getVariable,
|
getVariable = getVariable,
|
||||||
|
|
||||||
@@ -465,12 +544,13 @@ basalt = {
|
|||||||
createFrame = createFrame,
|
createFrame = createFrame,
|
||||||
|
|
||||||
addMonitor = function(name)
|
addMonitor = function(name)
|
||||||
|
InitializeBasalt()
|
||||||
for _, v in pairs(frames) do
|
for _, v in pairs(frames) do
|
||||||
if (v:getName() == name) then
|
if (v:getName() == name) then
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
local newFrame = _OBJECTS["MonitorFrame"](name, bInstance)
|
local newFrame = moddedObjects["MonitorFrame"](name, bInstance)
|
||||||
newFrame:init()
|
newFrame:init()
|
||||||
newFrame:load()
|
newFrame:load()
|
||||||
newFrame:draw()
|
newFrame:draw()
|
||||||
@@ -487,8 +567,6 @@ basalt = {
|
|||||||
end,
|
end,
|
||||||
}
|
}
|
||||||
|
|
||||||
_OBJECTS = pluginSystem.addPlugins(_OBJECTS, bInstance)
|
|
||||||
|
|
||||||
local basaltPlugins = pluginSystem.get("basalt")
|
local basaltPlugins = pluginSystem.get("basalt")
|
||||||
if(basaltPlugins~=nil)then
|
if(basaltPlugins~=nil)then
|
||||||
for k,v in pairs(basaltPlugins)do
|
for k,v in pairs(basaltPlugins)do
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
local args = {...}
|
local args = {...}
|
||||||
local plugins = {}
|
local plugins = {}
|
||||||
|
local pluginNames = {}
|
||||||
|
|
||||||
local dir = fs.getDir(args[2] or "Basalt")
|
local dir = fs.getDir(args[2] or "Basalt")
|
||||||
local pluginDir = fs.combine(dir, "plugins")
|
local pluginDir = fs.combine(dir, "plugins")
|
||||||
if(packaged)then
|
if(packaged)then
|
||||||
for k,v in pairs(getProject("plugins"))do
|
for k,v in pairs(getProject("plugins"))do
|
||||||
|
table.insert(pluginNames, k)
|
||||||
local newPlugin = v()
|
local newPlugin = v()
|
||||||
if(type(newPlugin)=="table")then
|
if(type(newPlugin)=="table")then
|
||||||
for a,b in pairs(newPlugin)do
|
for a,b in pairs(newPlugin)do
|
||||||
@@ -18,6 +20,7 @@ if(packaged)then
|
|||||||
end
|
end
|
||||||
if(fs.exists(pluginDir))then
|
if(fs.exists(pluginDir))then
|
||||||
for _,v in pairs(fs.list(pluginDir))do
|
for _,v in pairs(fs.list(pluginDir))do
|
||||||
|
table.insert(pluginNames, v)
|
||||||
local newPlugin = require(v:gsub(".lua", ""))
|
local newPlugin = require(v:gsub(".lua", ""))
|
||||||
if(type(newPlugin)=="table")then
|
if(type(newPlugin)=="table")then
|
||||||
for a,b in pairs(newPlugin)do
|
for a,b in pairs(newPlugin)do
|
||||||
@@ -35,14 +38,60 @@ local function get(name)
|
|||||||
end
|
end
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
--- Gets a plugin list
|
||||||
|
--- @param name string name of plugin list
|
||||||
|
--- @return table plugins
|
||||||
get = get,
|
get = get,
|
||||||
addPlugins = function(objects, basalt)
|
getAvailablePlugins = function()
|
||||||
|
return pluginNames
|
||||||
|
end,
|
||||||
|
|
||||||
|
--- Adds a plugin to basalt's plugin list
|
||||||
|
--- @param path string path to plugin
|
||||||
|
addPlugin = function(path)
|
||||||
|
if(fs.exists(path))then
|
||||||
|
if(fs.isDir(path))then
|
||||||
|
for _,v in pairs(fs.list(path))do
|
||||||
|
table.insert(pluginNames, v)
|
||||||
|
if not(fs.isDir(fs.combine(path, v)))then
|
||||||
|
local pluginName = v:gsub(".lua", "")
|
||||||
|
local newPlugin = require(fs.combine(path, pluginName))
|
||||||
|
if(type(newPlugin)=="table")then
|
||||||
|
for a,b in pairs(newPlugin)do
|
||||||
|
if(type(a)=="string")then
|
||||||
|
if(plugins[a]==nil)then plugins[a] = {} end
|
||||||
|
table.insert(plugins[a], b)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
local newPlugin = require(path:gsub(".lua", ""))
|
||||||
|
table.insert(pluginNames, path:match("[\\/]?([^\\/]-([^%.]+))$"))
|
||||||
|
if(type(newPlugin)=="table")then
|
||||||
|
for a,b in pairs(newPlugin)do
|
||||||
|
if(type(a)=="string")then
|
||||||
|
if(plugins[a]==nil)then plugins[a] = {} end
|
||||||
|
table.insert(plugins[a], b)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
|
||||||
|
--- Loads all available plugins into basalt's objects
|
||||||
|
--- @param objects table objects to load plugins into
|
||||||
|
--- @param basalt table basalt
|
||||||
|
--- @return table objects modified objects
|
||||||
|
loadPlugins = function(objects, basalt)
|
||||||
for k,v in pairs(objects)do
|
for k,v in pairs(objects)do
|
||||||
local plugList = plugins[k]
|
local plugList = plugins[k]
|
||||||
if(plugList~=nil)then
|
if(plugList~=nil)then
|
||||||
objects[k] = function(...)
|
objects[k] = function(...)
|
||||||
local moddedObject = v(...)
|
local moddedObject = v(...)
|
||||||
for a,b in pairs(plugList)do
|
for _,b in pairs(plugList)do
|
||||||
local ext = b(moddedObject, basalt, ...)
|
local ext = b(moddedObject, basalt, ...)
|
||||||
ext.__index = ext
|
ext.__index = ext
|
||||||
moddedObject = setmetatable(ext, moddedObject)
|
moddedObject = setmetatable(ext, moddedObject)
|
||||||
|
|||||||
Reference in New Issue
Block a user