mirror of
https://github.com/Pax1601/DCSOlympus.git
synced 2025-10-29 16:56:34 +00:00
Implemented basic Plugin handling
This commit is contained in:
@@ -1,36 +0,0 @@
|
||||
import { OlympusApp } from "../olympusapp";
|
||||
const templateParser = require( "ejs" );
|
||||
|
||||
export abstract class Plugin {
|
||||
|
||||
#olympusApp!:OlympusApp;
|
||||
protected name = "";
|
||||
#templateParser:any;
|
||||
|
||||
constructor( olympusApp:OlympusApp, pluginName:string ) {
|
||||
|
||||
const regex = "^[a-zA-Z][a-zA-Z\d]{4,}"
|
||||
|
||||
if ( new RegExp( regex ).test( pluginName ) === false ) {
|
||||
throw new Error( `Plugin names must match regex: ${regex}` );
|
||||
}
|
||||
|
||||
this.name = pluginName;
|
||||
this.#olympusApp = olympusApp;
|
||||
this.#templateParser = templateParser;
|
||||
|
||||
}
|
||||
|
||||
getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
getOlympusApp() {
|
||||
return this.#olympusApp;
|
||||
}
|
||||
|
||||
getTemplateParser() {
|
||||
return this.#templateParser;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,13 +1,55 @@
|
||||
import { OlympusApp } from "../olympusapp";
|
||||
import path from "path";
|
||||
import { Manager } from "../other/manager";
|
||||
import { getApp } from "..";
|
||||
|
||||
export class PluginsManager extends Manager {
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
export class PluginManager extends Manager {
|
||||
|
||||
constructor( olympusApp:OlympusApp ) {
|
||||
|
||||
super( olympusApp );
|
||||
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open('GET', "/plugins/list", true);
|
||||
xhr.responseType = 'json';
|
||||
xhr.onload = () => {
|
||||
var status = xhr.status;
|
||||
if (status === 200) {
|
||||
this.#loadPlugins(xhr.response);
|
||||
} else {
|
||||
console.error(`Error retrieving plugins`)
|
||||
}
|
||||
};
|
||||
xhr.send();
|
||||
}
|
||||
|
||||
#loadPlugins(pluginsFolders: string[]) {
|
||||
pluginsFolders.forEach((pluginName: string) => {
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open('GET', path.join("/plugins", pluginName, "index.js"), true);
|
||||
xhr.responseType = 'text';
|
||||
xhr.onload = () => {
|
||||
var status = xhr.status;
|
||||
if (status === 200) {
|
||||
/* Inject the plugin style */
|
||||
var link = document.createElement("link");
|
||||
link.href = path.join("/plugins", pluginName, "style.css");
|
||||
link.type = "text/css";
|
||||
link.rel = "stylesheet";
|
||||
document.getElementsByTagName("head")[0].appendChild(link);
|
||||
|
||||
/* Evaluate the plugin javascript */
|
||||
eval(xhr.response);
|
||||
const plugin = globalThis.getOlympusPlugin() as OlympusPlugin;
|
||||
console.log(plugin.getName() + " loaded correctly");
|
||||
|
||||
if (plugin.initialize(getApp())) {
|
||||
console.log(plugin.getName() + " initialized correctly");
|
||||
this.add(pluginName, plugin);
|
||||
}
|
||||
|
||||
} else {
|
||||
console.error(`Error retrieving plugin from ${pluginName}`)
|
||||
}
|
||||
};
|
||||
xhr.send();
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user