rewrite of settings module (1/3)

gm and pcv2 aren't done yet
This commit is contained in:
abUwUser 2022-02-27 21:51:16 -03:00
parent bc0945fdf0
commit 0171bcb748
Signed by untrusted user who does not match committer: abUwUser
GPG Key ID: 4705576FA5CF2F2A
14 changed files with 282 additions and 123 deletions

View File

@ -2,27 +2,27 @@ import { React } from "ittai/webpack";
export default class BDPlugin {
__ittaiInternals = {
getAllSettings: () => {
return globalThis.BdApi.loadData(this.constructor.name, "settings") ?? {};
},
getSetting: (key, defaultValue) => {
return (globalThis.BdApi.loadData(this.constructor.name, "settings") ?? {})[key] ?? defaultValue;
},
setSettings: (newSettings) => {
if (typeof newSettings !== "object") return;
globalThis.BdApi.saveData(
this.constructor.name,
"settings",
Object.assign(this.__ittaiInternals.getAllSettings(), newSettings)
);
},
setSetting: (key, value) => {
globalThis.BdApi.saveData(
this.constructor.name,
"settings",
Object.assign(this.__ittaiInternals.getAllSettings(), {[key]: value})
);
},
// getAllSettings: () => {
// return globalThis.BdApi.loadData(this.constructor.name, "settings") ?? {};
// },
// getSetting: (key, defaultValue) => {
// return (globalThis.BdApi.loadData(this.constructor.name, "settings") ?? {})[key] ?? defaultValue;
// },
// setSettings: (newSettings) => {
// if (typeof newSettings !== "object") return;
// globalThis.BdApi.saveData(
// this.constructor.name,
// "settings",
// Object.assign(this.__ittaiInternals.getAllSettings(), newSettings)
// );
// },
// setSetting: (key, value) => {
// globalThis.BdApi.saveData(
// this.constructor.name,
// "settings",
// Object.assign(this.__ittaiInternals.getAllSettings(), {[key]: value})
// );
// },
setSettingsPanel: (component) => {
this.getSettingsPanel = () => {
try {

View File

@ -6,19 +6,19 @@ export default class GMPlugin {
}
__ittaiInternals = {
getAllSettings: () => {
return this.__ittaiGMSettings;
},
getSetting: (key, defaultValue) => {
return this.__ittaiGMSettings[key] !== undefined ? this.__ittaiGMSettings[key] : defaultValue;
},
setSettings: (newSettings) => {
if (typeof newSettings !== "object") return;
Object.assign(this.__ittaiGMSettings, newSettings);
},
setSetting: (key, value) => {
this.__ittaiGMSettings[key] = value;
},
// getAllSettings: () => {
// return this.__ittaiGMSettings;
// },
// getSetting: (key, defaultValue) => {
// return this.__ittaiGMSettings[key] !== undefined ? this.__ittaiGMSettings[key] : defaultValue;
// },
// setSettings: (newSettings) => {
// if (typeof newSettings !== "object") return;
// Object.assign(this.__ittaiGMSettings, newSettings);
// },
// setSetting: (key, value) => {
// this.__ittaiGMSettings[key] = value;
// },
setSettingsPanel: (component) => {
if (typeof component === "function")
@ -45,7 +45,7 @@ export default class GMPlugin {
return this.stop();
},
getSettings: () => [this.__ittaiGMSettings],
loadSettings: ([_settings]) => { this.__ittaiGMSettings = _settings },
// getSettings: () => [this.__ittaiGMSettings],
// loadSettings: ([_settings]) => { this.__ittaiGMSettings = _settings },
}
}

View File

@ -6,24 +6,24 @@ let Plugin;
if (getClientMod() === "powercordv2") {
Plugin = class Plugin extends require("powercord").entities.Plugin {
__ittaiInternals = {
getAllSettings: () => {
let obj = {};
const keys = this.settings.getKeys();
keys.forEach(k => {
obj[k] = this.settings.get(k);
});
return obj;
},
getSetting: (key, defaultValue) => {
return this.settings.get(key, defaultValue);
},
setSettings: (newSettings) => {
if (typeof newSettings !== "object") return;
Object.keys(newSettings).forEach(k => this.settings.set(k, newSettings[k]));
},
setSetting: (key, value) => {
this.settings.set(key, value);
},
// getAllSettings: () => {
// let obj = {};
// const keys = this.settings.getKeys();
// keys.forEach(k => {
// obj[k] = this.settings.get(k);
// });
// return obj;
// },
// getSetting: (key, defaultValue) => {
// return this.settings.get(key, defaultValue);
// },
// setSettings: (newSettings) => {
// if (typeof newSettings !== "object") return;
// Object.keys(newSettings).forEach(k => this.settings.set(k, newSettings[k]));
// },
// setSetting: (key, value) => {
// this.settings.set(key, value);
// },
setSettingsPanel: (component) => {
// if (typeof component === "function")

View File

@ -1,10 +1,6 @@
import { ReactComponentElement, ReactElement } from "react";
interface PlatformSpecificInternals {
getAllSettings() : Object
getSetting(key: string, defaultValue: any) : any
setSettings(newSettings: Object) : void
setSetting(key: string, value: any) : void
setSettingsPanel(component: ReactComponentElement | ReactElement) : void
removeSettingsPanel() : void
}

View File

@ -1,12 +1,12 @@
import { ComponentElement } from "react";
import SettingsAPI from "../settings"
import { PlatformPlugin } from "./PlatformPlugin";
import * as Settings from "ittai/settings"
export default class Plugin {
__ittaiInternalPlugin: PlatformPlugin
friendlyName: string = "Ittai Plugin"
settings: SettingsAPI;
settings: Settings;
start () : void
stop () : void

View File

@ -4,7 +4,7 @@ import { unpatchAll } from "../patcher";
import BDPlugin from "./BDPlugin";
import GMPlugin from "./GMPlugin";
import PCv2Plugin from "./PCv2Plugin";
import SettingsAPI from "ittai/settings";
// import settings from "ittai/settings";
import * as ittai from "ittai";
@ -19,7 +19,7 @@ export default /**
friendlyName = name
settings = new SettingsAPI(this);
// settings = settings;
start() {}
stop() {}
@ -77,27 +77,37 @@ export default /**
cachedCss = [];
start() {
this.cachedCss.forEach((e, k) => {
const elem = Object.assign(document.createElement("style"), {
innerText: e
try {
this.cachedCss.forEach((e, k) => {
const elem = Object.assign(document.createElement("style"), {
innerText: e
})
elem.setAttribute("ittai", "true");
elem.setAttribute("plugin", name.replace(/ /g, ""));
document.head.appendChild(elem);
delete this.cachedCss[k];
})
elem.setAttribute("ittai", "true");
elem.setAttribute("plugin", name.replace(/ /g, ""));
document.head.appendChild(elem);
delete this.cachedCss[k];
})
return this.instance.start();
return this.instance.start();
} catch (err) {
console.error(err)
throw err
}
}
stop() {
const res = this.instance.stop();
unpatchAll();
if (this.hasSettingsPanel) this.removeSettingsPanel();
document.querySelectorAll(`style[ittai][plugin="${name.replace(/ /g, "")}"]`)?.forEach(e => {
this.cachedCss.push(e.innerText);
e.remove();
});
return res;
try {
const res = this.instance.stop();
unpatchAll();
if (this.hasSettingsPanel) this.removeSettingsPanel();
document.querySelectorAll(`style[ittai][plugin="${name.replace(/ /g, "")}"]`)?.forEach(e => {
this.cachedCss.push(e.innerText);
e.remove();
});
return res;
} catch (err) {
console.error(err)
throw err
}
}
setSettingsPanel(component) {

2
core/index.d.ts vendored
View File

@ -3,9 +3,9 @@ export * as commands from "ittai/commands";
export * as entities from "./entities";
export * as logger from "./logger";
export * as patcher from "./patcher";
export * as settings from "./settings";
export * as stores from "./stores";
export * as utilities from "ittai/utilities";
export * as settings from "./settings";
export * as webpack from "./webpack";
declare module "*.scss"; declare module "*.sass";

View File

@ -5,6 +5,7 @@ export * as commands from "./commands";
export * as entities from "./entities";
export * as logger from "./logger";
export * as patcher from "./patcher";
export * as settings from "./settings";
export * as stores from "./stores";
export * as utilities from "./utilities";
export * as webpack from "./webpack";

5
core/settings/index.d.ts vendored Normal file
View File

@ -0,0 +1,5 @@
export function get(key: string, defaultValue: any): any
export function getAll(): Object<any>
export function set(key: string, value: any): void
export function setMultipleSettings(settings: Object<any>): void
export function overrideSettings(settings: Object<any>): void

150
core/settings/index.js Normal file
View File

@ -0,0 +1,150 @@
import { getClientMod } from "../utilities";
import { name } from "ittai-manifest";
import logger from "ittai/logger";
// let gmSettings;
export const get = (key, defaultValue) => {
switch (getClientMod()) {
case "betterdiscord":
return (globalThis.BdApi.loadData(name, "settings") ?? {})[key] ?? defaultValue;
break;
case "goosemod":
// goosemod.modules[name].goosemodHandlers.getSettings = () => [gmSettings]
// goosemod.modules[name].goosemodHandlers.loadSettings = ([_settings]) => { gmSettings = _settings }
// return gmSettings[key] ?? defaultValue
console.error("settings for " + getClientMod() + " is not implemented yet. we (ittai team) are too lazy to implement it")
break;
case "powercordv2":
// return this.settings.get(key, defaultValue);
console.error("settings for " + getClientMod() + " is not implemented yet. we (ittai team) are too lazy to implement it")
break;
}
}
export const getAll = () => {
switch (getClientMod()) {
case "betterdiscord":
return globalThis.BdApi.loadData(name, "settings") ?? {}
break;
case "goosemod":
// goosemod.modules[name].goosemodHandlers.getSettings = () => [gmSettings]
// goosemod.modules[name].goosemodHandlers.loadSettings = ([_settings]) => { gmSettings = _settings }
// return gmSettings
console.error("settings for " + getClientMod() + " is not implemented yet. we (ittai team) are too lazy to implement it")
break;
case "powercordv2":
// let obj = {};
// const keys = this.settings.getKeys();
// keys.forEach(k => {
// obj[k] = this.settings.get(k);
// });
// return obj;
console.error("settings for " + getClientMod() + " is not implemented yet. we (ittai team) are too lazy to implement it")
break;
}
}
export const set = (key, value) => {
switch(getClientMod()) {
case "betterdiscord":
let setting = get(name) ?? {}
setting?.[key] = value
globalThis.BdApi.saveData(
name,
"settings",
setting
);
break;
case "goosemod":
// goosemod.modules[name].goosemodHandlers.getSettings = () => [gmSettings]
// goosemod.modules[name].goosemodHandlers.loadSettings = ([_settings]) => { gmSettings = _settings }
// gmSettings[key] = value;
console.error("settings for " + getClientMod() + " is not implemented yet. we (ittai team) are too lazy to implement it")
break;
case "powercordv2":
// this.settings.set(key, value);
console.error("settings for " + getClientMod() + " is not implemented yet. we (ittai team) are too lazy to implement it")
break;
}
}
export const setMultipleSettings = (settings) => {
if (typeof settings !== "object") return;
switch (getClientMod()) {
case "betterdiscord":
globalThis.BdApi.saveData(
name,
"settings",
Object.assign(globalThis.BdApi.loadData(name, "settings") ?? {}, settings)
);
break;
case "goosemod":
// goosemod.modules[name].goosemodHandlers.getSettings = () => [gmSettings]
// goosemod.modules[name].goosemodHandlers.loadSettings = ([_settings]) => { gmSettings = _settings }
// gmSettings = Object.assign(gmSettings ?? {}, settings)
// Object.assign(this.__ittaiGMSettings, settings);
console.error("settings for " + getClientMod() + " is not implemented yet. we (ittai team) are too lazy to implement it")
break;
case "powercordv2":
// Object.keys(settings).forEach(k => this.settings.set(k, settings[k]));
console.error("settings for " + getClientMod() + " is not implemented yet. we (ittai team) are too lazy to implement it")
break;
}
}
export const overrideSettings = (settings) => {
if (typeof settings !== "object") return;
switch (getClientMod()) {
case "betterdiscord":
globalThis.BdApi.saveData(
name,
"settings",
settings
);
break;
case "goosemod":
// goosemod.modules[name].goosemodHandlers.getSettings = () => [gmSettings]
// goosemod.modules[name].goosemodHandlers.loadSettings = ([_settings]) => { gmSettings = _settings }
// gmSettings = _settings
console.error("settings for " + getClientMod() + " is not implemented yet. we (ittai team) are too lazy to implement it")
break;
case "powercordv2":
console.error("settings for " + getClientMod() + " is not implemented yet. we (ittai team) are too lazy to implement it")
break;
}
}
export default {
get,
getAll,
set,
setMultipleSettings,
overrideSettings
}
// export default class SettingsAPI {
// constructor(pluginInstance: Plugin) {
// this.__pluginInstance = pluginInstance;
// }
// private __pluginInstance: Plugin
// get (key: string, defaultValue: any) : any {
// return this.__pluginInstance.__ittaiInternalPlugin.__ittaiInternals.getSetting(key, defaultValue); // Yes this is excessive. Too bad.
// }
// getAll () : Object {
// return this.__pluginInstance.__ittaiInternalPlugin.__ittaiInternals.getAllSettings();
// }
// set (key: string, value: any) : void {
// return this.__pluginInstance.__ittaiInternalPlugin.__ittaiInternals.setSetting(key, value);
// }
// setAll (settings: Object) : void {
// return this.__pluginInstance.__ittaiInternalPlugin.__ittaiInternals.setSettings(settings);
// }
// }

View File

@ -1,25 +0,0 @@
import { Plugin } from "../entities";
export default class SettingsAPI {
constructor(pluginInstance: Plugin) {
this.__pluginInstance = pluginInstance;
}
private __pluginInstance: Plugin
get (key: string, defaultValue: any) : any {
return this.__pluginInstance.__ittaiInternalPlugin.__ittaiInternals.getSetting(key, defaultValue); // Yes this is excessive. Too bad.
}
getAll () : Object {
return this.__pluginInstance.__ittaiInternalPlugin.__ittaiInternals.getAllSettings();
}
set (key: string, value: any) : void {
return this.__pluginInstance.__ittaiInternalPlugin.__ittaiInternals.setSetting(key, value);
}
setAll (settings: Object) : void {
return this.__pluginInstance.__ittaiInternalPlugin.__ittaiInternals.setSettings(settings);
}
}

View File

@ -1,43 +1,49 @@
const express = require('express');
const cors = require("cors")
const { createHash } = require("crypto");
const { promises: { readFile } } = require("fs");
const { join } = require("path");
const app = express();
app.get("/module/devmodule.js", async (req, res) => {
const jsFile = await readFile(join(__dirname, "..", "exampleDist", "index.js"), { encoding: "utf8" })
// app.use(cors())
app.get("/module/:id", async (req, res) => {
console.log("hi", req.params.id)
const jsFile = await readFile(join(process.argv[2], "index.js"), { encoding: "utf8" })
res.setHeader("Access-Control-Allow-Origin", "*")
res.setHeader("Content-Type", "application/javascript; charset=utf-8")
res.setHeader("Cache-Control", "no-cache")
res.send(jsFile);
})
app.get("/index.js.map", async (req, res) => {
const jsFile = await readFile(join(__dirname, "..", "exampleDist", "index.js.map"), { encoding: "utf8" })
res.setHeader("Access-Control-Allow-Origin", "*")
res.setHeader("Content-Type", "application/javascript; charset=utf-8")
res.setHeader("Cache-Control", "no-cache")
res.send(jsFile);
})
// app.get("/index.js.map", async (req, res) => {
// console.log(process.argv[2])
// const jsFile = await readFile(join(process.argv[2], "index.js.map"), { encoding: "utf8" })
// res.setHeader("Access-Control-Allow-Origin", "*")
// res.setHeader("Content-Type", "application/javascript; charset=utf-8")
// res.setHeader("Cache-Control", "no-cache")
// res.send(jsFile);
// })
app.get('/', (req, res) => res.redirect('/modules.json'));
app.get("/modules.json", async (req, res) => {
const jsFile = await readFile(join(__dirname, "..", "exampleDist", "index.js"), { encoding: "utf8" })
const jsFile = await readFile(join(process.argv[2], "index.js"), { encoding: "utf8" })
res.setHeader("Access-Control-Allow-Origin", "*")
res.setHeader("Cache-Control", "no-cache")
res.json({
"modules": [
{
"name": "IttaiDevMode",
"description": "Developer mode for Ittai plugins",
"version": "1.0.0",
"name": "IttaiGooseModServer",
"description": "GooseMod server for testing Ittai plugins",
"version": "1.6.9",
"tags": [
"messages"
"ittai"
],
"authors": [
{
"i": "",
"n": "dev",
"n": "Ittai",
"a": ""
}
],
@ -50,11 +56,11 @@ app.get("/modules.json", async (req, res) => {
}
],
"meta": {
"name": "Dev repo",
"description": "local repo"
"name": "Ittai GooseMod server",
"description": "GooseMod server for testing production"
}
})
})
app.listen(3000);
console.log('listening on port 3000');
console.log('Server started.\nGo to GooseMod > Plugins, click the Cloud icon and add http://localhost:3000/modules.json as the URL. Then search for \"Ittai\" and hit Install');

View File

@ -9,6 +9,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.17.2"
}
}

View File

@ -1,9 +1,11 @@
lockfileVersion: 5.3
specifiers:
cors: ^2.8.5
express: ^4.17.2
dependencies:
cors: 2.8.5
express: 4.17.2
packages:
@ -62,6 +64,14 @@ packages:
engines: {node: '>= 0.6'}
dev: false
/cors/2.8.5:
resolution: {integrity: sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==}
engines: {node: '>= 0.10'}
dependencies:
object-assign: 4.1.1
vary: 1.1.2
dev: false
/debug/2.6.9:
resolution: {integrity: sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==}
dependencies:
@ -226,6 +236,11 @@ packages:
engines: {node: '>= 0.6'}
dev: false
/object-assign/4.1.1:
resolution: {integrity: sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=}
engines: {node: '>=0.10.0'}
dev: false
/on-finished/2.3.0:
resolution: {integrity: sha1-IPEzZIGwg811M3mSoWlxqi2QaUc=}
engines: {node: '>= 0.8'}