plugin settings now working

master^2
Cain Atkinson 2 years ago
parent 529582bb3b
commit c913445708

@ -1,13 +1,14 @@
import flow from 'rollup-plugin-flow'
import { minify } from 'rollup-plugin-esbuild'
import { nodeResolve } from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import flow from "rollup-plugin-flow";
import { minify } from "rollup-plugin-esbuild";
import { nodeResolve } from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import { defineConfig } from "rollup";
export default {
input: 'src/index.js',
plugins: [flow(), commonjs(), nodeResolve(), minify()],
output: {
file: 'dist/build.js',
format: "iife"
}
}
export default defineConfig({
input: "src/index.js",
plugins: [flow(), commonjs(), nodeResolve(), minify()],
output: {
file: "dist/build.js",
format: "iife",
},
});

@ -1,6 +1,7 @@
// @flow
import { get, set } from 'idb-keyval';
import logger from './utils/logger.js'
import { get, set } from "idb-keyval";
import logger from "./utils/logger.js";
import settingsInj from "./settingsInj.js";
const pluginsSym = Symbol("__plugins");
@ -43,62 +44,72 @@ async function addPlugin(iife: string, metadata: Object): Promise<boolean> {
}
async function delPlugin(name: string): Promise<boolean> {
const globalSettings = await get("demoncord")
if (globalSettings["plugin"][name] === undefined) {
logger.error("Cannot remove non-existant plugin!", ["Plugins"])
return false
} else {
globalSettings["plugin"][name] = undefined
delete globalSettings["plugin"][name]
}
await set("demoncord", globalSettings)
return true
const globalSettings = await get("demoncord");
if (globalSettings["plugin"][name] === undefined) {
logger.error("Cannot remove non-existant plugin!", ["Plugins"]);
return false;
} else {
globalSettings["plugin"][name] = undefined;
delete globalSettings["plugin"][name];
}
await set("demoncord", globalSettings);
return true;
}
async function startPlugin(name: string): Promise<boolean> {
const globalSettings = await get("demoncord")
if (globalSettings["plugin"][name] === undefined) {
logger.error("Cannot start non-existant plugin!", ["Plugins"])
return false
} else {
logger.log(`Starting ${name}...`, ["Plugins"])
const plug = globalSettings["plugin"][name]
const exports: Object = (0, eval)(plug.iife)
const onStart: (ctx: Object)=>void = exports.onStart
let ctx = {} // ctx is how you're going to store things that need to be accessed in both onStart and onStop. dumb, i know
onStart(ctx)
logger.log(`Started ${name}!`, ["Plugins"])
window.demon[pluginsSym][name] = {status: 1, ctx: ctx}
return true
}
const globalSettings = await get("demoncord");
if (globalSettings["plugin"][name] === undefined) {
logger.error("Cannot start non-existant plugin!", ["Plugins"]);
return false;
} else {
logger.log(`Starting ${name}...`, ["Plugins"]);
const plug = globalSettings["plugin"][name];
const exports: Object = (0, eval)(plug.iife);
const onStart: (ctx: Object) => void = exports.onStart;
let ctx = {}; // ctx is how you're going to store things that need to be accessed in both onStart and onStop. dumb, i know
onStart(ctx);
if (exports.settings)
settingsInj.registerSettingsEntry(
name,
"DEMON_PLUGIN_SETTINGS_" + name,
exports.settings
);
logger.log(`Started ${name}!`, ["Plugins"]);
window.demon[pluginsSym][name] = { status: 1, ctx: ctx };
return true;
}
}
async function stopPlugin(name: string): Promise<boolean> {
const globalSettings = await get("demoncord")
if (globalSettings["plugin"][name] === undefined ) {
logger.error("Cannot stop non-existant or non-running plugin!", ["Plugins"])
return false
} else {
logger.log(`Stopping ${name}...`, ["Plugins"])
const plug = globalSettings["plugin"][name]
const exports: Object = (0, eval)(plug.iife)
const onStop: (ctx: Object)=>void = exports.onStop
onStop(window.demon[pluginsSym][name].ctx);
logger.log(`Stopped ${name}!`, ["Plugins"])
return true
}
const globalSettings = await get("demoncord");
if (globalSettings["plugin"][name] === undefined) {
logger.error("Cannot stop non-existant or non-running plugin!", [
"Plugins",
]);
return false;
} else {
logger.log(`Stopping ${name}...`, ["Plugins"]);
const plug = globalSettings["plugin"][name];
const exports: Object = (0, eval)(plug.iife);
const onStop: (ctx: Object) => void = exports.onStop;
onStop(window.demon[pluginsSym][name].ctx);
settingsInj.unregisterSettingsEntry("DEMON_PLUGIN_SETTINGS_" + name);
logger.log(`Stopped ${name}!`, ["Plugins"]);
return true;
}
}
async function togglePlugin(name: string): Promise<boolean> {
const globalSettings = await get("demoncord")
if (globalSettings["plugin"][name] === undefined) {
logger.error("Cannot toggle non-existant plugin!", ["Plugins"])
return false
} else {
globalSettings["plugin"][name].enabled = !globalSettings["plugin"][name].enabled
await set("demoncord", globalSettings)
return true
}
const globalSettings = await get("demoncord");
if (globalSettings["plugin"][name] === undefined) {
logger.error("Cannot toggle non-existant plugin!", ["Plugins"]);
return false;
} else {
globalSettings["plugin"][name].enabled =
!globalSettings["plugin"][name].enabled;
await set("demoncord", globalSettings);
return true;
}
}
export default {

@ -27,7 +27,9 @@ type getPredicateSectionsEntry =
function patch(args: mixed, ret: getPredicateSectionsEntry[]) {
const processedEntries = window.demon[settingsSym].entries.map((e) => ({
section: e[ovrwrtSctnSym] ?? "DEMON_SETTINGS_LOADER_" + e.name,
section:
(e[ovrwrtSctnSym] ? e[ovrwrtSctnSym] : "DEMON_SETTINGS_LOADER_") +
e.name,
label: e.name,
element: e.component,
}));
@ -65,13 +67,18 @@ function unInit(obj: Object) {
function unregisterSettingsEntry(name: string) {
let s = window.demon[settingsSym];
s.entries = s.entries.filter((e) => e.name !== name);
s.entries = s.entries.filter(
(e) => (e[ovrwrtSctnSym] ? e[ovrwrtSctnSym] : e.name) !== name
);
}
function registerSettingsEntry(name: string, section: string, component: Function): () => void {
let entry = { name, component };
if (section)
entry[ovrwrtSctnSym] = section;
function registerSettingsEntry(
name: string,
section: string,
component: Function
): () => void {
let entry: { [symbol | string]: any } = { name, component };
if (section) entry[ovrwrtSctnSym] = section;
window.demon[settingsSym].entries.push(entry);
return () => unregisterSettingsEntry(name);
}

@ -4,7 +4,7 @@ import webpack from "./api/webpack";
import common from "./api/common";
import commands from "./api/commands";
import plugins from "./api/plugins";
import settingsInj from "./api/settingsInjection";
import settingsInj from "./api/settingsInj";
async function init(obj: Object): Promise<void> {
const patcher = new Patcher();

Loading…
Cancel
Save