// @flow import webpack from "./webpack"; const settingsSym = Symbol("__settings"); // super secret value that shouldnt be exposed, used internally to avoid section conflicts and the like const ovrwrtSctnSym: symbol = Symbol("__overwriteSection"); const settingsView = webpack.find( (m) => m.default && m.default.displayName === "SettingsView" ); type getPredicateSectionsEntry = | { section: "HEADER", label: string, } | { section: "DIVIDER", } | { section: string, label: string, component: Function, }; function patch(args: mixed, ret: getPredicateSectionsEntry[]) { const processedEntries = window.demon[settingsSym].entries.map((e) => ({ section: (e[ovrwrtSctnSym] ? e[ovrwrtSctnSym] : "DEMON_SETTINGS_LOADER_") + e.name, label: e.name, element: e.component, })); 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() { window.demon[settingsSym] = { patch: window.demon.patcher.after( "getPredicateSections", settingsView.default.prototype, patch ), entries: [], }; // debug window.demonunpatch = window.demon[settingsSym].patch; } function unInit(obj: Object) { window.demon[settingsSym].patch(); delete window.demon[settingsSym]; } function unregisterSettingsEntry(name: string) { let s = window.demon[settingsSym]; s.entries = s.entries.filter( (e) => (e[ovrwrtSctnSym] ? 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; window.demon[settingsSym].entries.push(entry); return () => unregisterSettingsEntry(name); } export default { init, unInit, registerSettingsEntry, unregisterSettingsEntry, ovrwrtSctnSymm: ovrwrtSctnSym, };