From 7d4f48a446c2a9db50002483d67fb6bfde211b06 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sun, 10 Mar 2024 04:03:40 -0400 Subject: [PATCH] #6 The bones - Added a module to load addons - Added an example module that just extends the socketIO connection to add an console log --- server/addons/example/index.js.ex | 17 +++++++++++++++++ server/modules/addonManager.mjs | 26 ++++++++++++++++++++++++++ server/server.js | 6 +++++- 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 server/addons/example/index.js.ex create mode 100644 server/modules/addonManager.mjs diff --git a/server/addons/example/index.js.ex b/server/addons/example/index.js.ex new file mode 100644 index 0000000..63ba610 --- /dev/null +++ b/server/addons/example/index.js.ex @@ -0,0 +1,17 @@ +// 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 new file mode 100644 index 0000000..558e3b0 --- /dev/null +++ b/server/modules/addonManager.mjs @@ -0,0 +1,26 @@ +import { fileURLToPath } from 'url'; +import fs from 'fs'; +import path from 'path'; + +// Function to load addons from the addons directory +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 + const addonDirectories = await fs.readdirSync(addonsDir, { withFileTypes: true }); + + 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.`); + }) + } + }); +} diff --git a/server/server.js b/server/server.js index c76cf6b..8fdbb90 100644 --- a/server/server.js +++ b/server/server.js @@ -1,4 +1,5 @@ import { nodeIo, app, server } from './modules/socketServer.mjs'; +import { loadAddons } from './modules/addonManager.mjs'; import { serverClient, addEnabledEventListeners } from './discordBot/discordBot.mjs'; import dotenv from 'dotenv'; @@ -6,4 +7,7 @@ dotenv.config() // Add objects to the others serverClient.nodeIo = nodeIo; -nodeIo.serverClient = serverClient; \ No newline at end of file +nodeIo.serverClient = serverClient; + +// Load the addons +loadAddons(nodeIo); \ No newline at end of file