From bee95ed999e18d5e2f85f8924102410bda565bc2 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sun, 10 Mar 2024 04:50:16 -0400 Subject: [PATCH] #6 - Added a config system to the addons - Added options in the config for dynamic variables - Updated example and code to use new config and options --- server/addons/example/config.json | 7 +++++++ server/addons/example/index.js | 17 +++++++++++++++++ server/addons/example/index.js.ex | 17 ----------------- server/modules/addonManager.mjs | 21 +++++++++++++-------- 4 files changed, 37 insertions(+), 25 deletions(-) create mode 100644 server/addons/example/config.json create mode 100644 server/addons/example/index.js delete mode 100644 server/addons/example/index.js.ex diff --git a/server/addons/example/config.json b/server/addons/example/config.json new file mode 100644 index 0000000..f6cff28 --- /dev/null +++ b/server/addons/example/config.json @@ -0,0 +1,7 @@ +{ + "name": "Addon 1", + "enabled": false, + "options": { + "eventName": "connection" + } + } \ No newline at end of file diff --git a/server/addons/example/index.js b/server/addons/example/index.js new file mode 100644 index 0000000..2ba6e20 --- /dev/null +++ b/server/addons/example/index.js @@ -0,0 +1,17 @@ +// addons/addon1/index.js + +// Function called by the main application to initialize the addon +export function initialize(nodeIo, config) { + console.log(`Initializing ${config.name}`); + + // Call other functions within the addon module + registerSocketEvents(nodeIo, config); + // Call additional initialization functions if needed +} + +// Function to register Socket.IO event handlers +function registerSocketEvents(nodeIo, config) { + nodeIo.on(config.options.eventName, (data) => { + console.log(`Received event "${config.options.eventName}" from client:`, data); + }); +} diff --git a/server/addons/example/index.js.ex b/server/addons/example/index.js.ex deleted file mode 100644 index 63ba610..0000000 --- a/server/addons/example/index.js.ex +++ /dev/null @@ -1,17 +0,0 @@ -// Function called by the main application to initialize the addon -export function initialize(io) { - console.log('Initializing addon1'); - - // Call other functions within the addon module - registerSocketEvents(io); - // Call additional initialization functions if needed -} - -// Function to register Socket.IO event handlers -function registerSocketEvents(io) { - io.on('connection', (socket) => { - console.log('A client connected from addon1'); - - // Add more event handlers if needed - }); -} \ No newline at end of file diff --git a/server/modules/addonManager.mjs b/server/modules/addonManager.mjs index 558e3b0..ca63c51 100644 --- a/server/modules/addonManager.mjs +++ b/server/modules/addonManager.mjs @@ -6,7 +6,7 @@ import path from 'path'; export const loadAddons = async (nodeIo) => { const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); - + const addonsDir = path.join(__dirname, '../addons'); // Read the directory containing addon modules @@ -14,13 +14,18 @@ export const loadAddons = async (nodeIo) => { addonDirectories.forEach(addonDir => { if (addonDir.isDirectory()) { - const addonPath = path.join(addonsDir, addonDir.name, 'index.js'); - - import(`file://${addonPath}`).then(addon => { - console.log("Loading addon: ", addon); - addon.initialize(nodeIo); - console.log(`Addon ${addonDir.name} loaded.`); - }) + const addonConfigPath = path.join(addonsDir, addonDir.name, 'config.json'); + if (fs.existsSync(addonConfigPath)) { + const addonConfig = JSON.parse(fs.readFileSync(addonConfigPath, 'utf-8')); + if (addonConfig.enabled) { + const addonIndexPath = path.join(addonsDir, addonDir.name, 'index.js'); + import(`file://${addonIndexPath}`).then(addonModule => { + console.log("Loading addon: ", addonModule); + addonModule.initialize(nodeIo, addonConfig); + console.log(`Addon ${addonConfig.name} loaded.`); + }); + } + } } }); }