Skip to content

Commit

Permalink
Dynamically load plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas-Smyth committed Jan 30, 2021
1 parent 90540f0 commit 95b8baf
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 54 deletions.
20 changes: 14 additions & 6 deletions squad-server/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ import sequelize from 'sequelize';
import Logger from 'core/logger';

import SquadServer from './index.js';
import plugins from './plugins/index.js';
import Plugins from './plugins/index.js';

const { Sequelize } = sequelize;

const __dirname = path.dirname(fileURLToPath(import.meta.url));

export default class SquadServerFactory {
static async buildFromConfig(config) {
const plugins = await Plugins.getPlugins();

for (const plugin of Object.keys(plugins)) {
Logger.setColor(plugin, 'magentaBright');
}
Expand Down Expand Up @@ -160,7 +162,9 @@ export default class SquadServerFactory {
return SquadServerFactory.buildFromConfigString(SquadServerFactory.readConfigFile(configPath));
}

static buildConfig() {
static async buildConfig() {
const plugins = await Plugins.getPlugins();

const templatePath = path.resolve(__dirname, './templates/config-template.json');
const templateString = fs.readFileSync(templatePath, 'utf8');
const template = SquadServerFactory.parseConfig(templateString);
Expand All @@ -183,13 +187,17 @@ export default class SquadServerFactory {
return template;
}

static buildConfigFile() {
static async buildConfigFile() {
const configPath = path.resolve(__dirname, '../config.json');
const config = JSON.stringify(SquadServerFactory.buildConfig(), null, 2);
fs.writeFileSync(configPath, config);
const config = await SquadServerFactory.buildConfig();

const configString = JSON.stringify(config, null, 2);
fs.writeFileSync(configPath, configString);
}

static buildReadmeFile() {
static async buildReadmeFile() {
const plugins = await Plugins.getPlugins();

const pluginKeys = Object.keys(plugins).sort((a, b) =>
a.name < b.name ? -1 : a.name > b.name ? 1 : 0
);
Expand Down
78 changes: 34 additions & 44 deletions squad-server/plugins/index.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,36 @@
import AutoKickUnassigned from './auto-kick-unassigned.js';
import AutoTKWarn from './auto-tk-warn.js';
import ChatCommands from './chat-commands.js';
import DBLog from './db-log.js';
import DiscordAdminBroadcast from './discord-admin-broadcast.js';
import DiscordAdminCamLogs from './discord-admin-cam-logs.js';
import DiscordAdminRequest from './discord-admin-request.js';
import DiscordChat from './discord-chat.js';
import DiscordDebug from './discord-debug.js';
import DiscordRcon from './discord-rcon.js';
import DiscordRoundWinner from './discord-round-winner.js';
import DiscordServerStatus from './discord-server-status.js';
import DiscordSubsystemRestarter from './discord-subsystem-restarter.js';
import DiscordTeamkill from './discord-teamkill.js';
import IntervalledBroadcasts from './intervalled-broadcasts.js';
import SCBLInfo from './scbl-info.js';
import SeedingMode from './seeding-mode.js';
import TeamRandomizer from './team-randomizer.js';

const plugins = [
AutoKickUnassigned,
AutoTKWarn,
ChatCommands,
DBLog,
DiscordAdminBroadcast,
DiscordAdminCamLogs,
DiscordAdminRequest,
DiscordChat,
DiscordDebug,
DiscordRcon,
DiscordRoundWinner,
DiscordServerStatus,
DiscordSubsystemRestarter,
DiscordTeamkill,
IntervalledBroadcasts,
SCBLInfo,
SeedingMode,
TeamRandomizer
];

const pluginsByName = {};
for (const plugin of plugins) {
pluginsByName[plugin.name] = plugin;
import fs from 'fs';

import Logger from 'core/logger';
import path from 'path';
import { fileURLToPath } from 'url';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

class Plugins {
constructor() {
this.plugins = null;
}

async getPlugins(force = false) {
if (this.plugins && !force) return this.plugins;

this.plugins = {};

const dir = await fs.promises.opendir(path.join(__dirname, './'));
for await (const dirent of dir) {
if (!dirent.isFile()) continue;
if (
['index.js', 'base-plugin.js', 'discord-base-plugin.js', 'readme.md'].includes(dirent.name)
)
continue;
Logger.verbose('Plugins', 1, `Loading plugin file ${dirent.name}...`);
const { default: Plugin } = await import(`./${dirent.name}`);

this.plugins[Plugin.name] = Plugin;
}

return this.plugins;
}
}

export default pluginsByName;
export default new Plugins();
7 changes: 5 additions & 2 deletions squad-server/scripts/build-config-file.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import SquadServerFactory from '../factory.js';

console.log('Building config...');
SquadServerFactory.buildConfigFile();
console.log('Done.');
SquadServerFactory.buildConfigFile()
.then(() => {
console.log('Done.');
})
.catch(console.log);
7 changes: 5 additions & 2 deletions squad-server/scripts/build-readme.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import SquadServerFactory from '../factory.js';

console.log('Building readme...');
SquadServerFactory.buildReadmeFile();
console.log('Done.');
SquadServerFactory.buildReadmeFile()
.then(() => {
console.log('Done.');
})
.catch(console.log);

0 comments on commit 95b8baf

Please sign in to comment.