You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
2.5 KiB

// @flow
import webpack from "../webpack.js";
import common from "../common.js";
const React = common.React;
const settingsSym = Symbol("__settings");
window.demonSettings = {};
// super secret value that shouldnt be exposed, used internally to avoid section conflicts and the like
const ovrwrtSctnSym: symbol = Symbol("__overwriteSection");
const settingsView = webpack.findByDisplayName("SettingsView");
type getPredicateSectionsEntry =
| {
section: "HEADER",
label: string
}
| {
section: "DIVIDER"
}
| {
section: string,
label: string,
component: Function
};
function patch(args: mixed, ret: getPredicateSectionsEntry[]) {
const processedEntries = window.demonSettings[settingsSym].entries.map(
(e) => ({
section: (e[ovrwrtSctnSym] ?? "DEMON_SETTINGS_LOADER_") + e.name,
label: e.name,
element: e.component
})
);
processedEntries.map((e) => {
console.log(e);
});
const injectionIndex =
2 + ret.findIndex((section) => section.section === "Game Activity");
ret.splice(
injectionIndex,
0,
{ section: "HEADER", label: "Demon" },
...processedEntries,
{ section: "DIVIDER" }
);
return ret;
}
function init() {
const patcher = window.demon.require("patcher");
window.demonSettings[settingsSym] = {
patch: patcher.after(
"getPredicateSections",
settingsView.default.prototype,
patch
),
entries: []
};
// debug
//window.__demontemp__UnpatchSettingsInj = unInit();
}
function unInit() {
window.demonSettings[settingsSym].patch();
delete window.demonSettings[settingsSym];
}
function unregisterSettingsEntry(name: string) {
let s = window.demonSettings[settingsSym];
s.entries = s.entries.filter((e) => (e[ovrwrtSctnSym] ?? e.name) !== name);
}
function registerSettingsEntry(
name: string,
section: ?string,
component: Function
): () => void {
let entry: { [symbol | string]: any } = { name, component };
if (section) entry[ovrwrtSctnSym] = section;
//entry.component.type = entry.component.type.default
console.log(entry);
window.demonSettings[settingsSym].entries.push(entry);
return () => unregisterSettingsEntry(name);
}
export default {
init,
unInit,
registerSettingsEntry,
unregisterSettingsEntry,
ovrwrtSctnSymm: ovrwrtSctnSym
};