-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from LosersUnited/feat-code-injection
Add replacement implementation stores for client mods that don't have specific functions
- Loading branch information
Showing
10 changed files
with
442 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { FunctionImplementation, __requireInternal } from "./index.js"; | ||
import { IModImplementation } from "../api/ModImplementation.js"; | ||
|
||
// const reservedData = {} as { [key: string]: any }; | ||
|
||
export let targetMod: IModImplementation; | ||
|
||
const implementationStore = { | ||
getModule: new FunctionImplementation({ | ||
depends: [], | ||
supplies: "getModule", | ||
// get data() { | ||
// return reservedData[this.supplies]; | ||
// }, | ||
// set data(v: any) { | ||
// reservedData[this.supplies] = v; | ||
// }, | ||
data: {}, | ||
func(filter: (mod: any) => boolean) { | ||
if (this.data.req == undefined) { | ||
// @ts-expect-error Non-standard property | ||
window.webpackChunkdiscord_app.push( | ||
[[Symbol()], | ||
{}, | ||
(r: { c: any; }) => | ||
this.data = this.data ?? { | ||
req: r, | ||
}, | ||
]); | ||
// @ts-expect-error Non-standard property | ||
window.webpackChunkdiscord_app.pop(); | ||
} | ||
// @ts-expect-error too lazy | ||
return Object.values(this.data.req).find(filter)?.exports; | ||
}, | ||
}), | ||
getModuleRawToExportedWrapper: new FunctionImplementation({ | ||
depends: [], | ||
supplies: "getModule", | ||
data: null, | ||
isWrapper: true, | ||
func(filter: (mod: any) => boolean) { | ||
const originalGetModule = __requireInternal(targetMod, "WebpackApi", "getModule", true); | ||
return originalGetModule!((x: { exports: any; }) => Object.values(x?.exports || {})?.some(filter)); | ||
}, | ||
}), | ||
getByStrings: new FunctionImplementation({ | ||
depends: ["getModule"], | ||
supplies: "getByStrings", | ||
data: null, | ||
func(...strings) { | ||
/* __requireInternal(targetMod, "WebpackApi", "test")!(); */ | ||
const getModule = __requireInternal(targetMod, "WebpackApi", "getModule"); | ||
if (!getModule) | ||
throw new Error("Unimplemented"); | ||
return getModule((module: any) => { | ||
if (!module?.toString || typeof (module?.toString) !== "function") return; // Not stringable | ||
let moduleString = ""; | ||
try { moduleString = module?.toString([]); } | ||
catch (err) { moduleString = module?.toString(); } | ||
if (!moduleString) return false; // Could not create string | ||
for (const s of strings) { | ||
if (!moduleString.includes(s)) return false; | ||
} | ||
return true; | ||
}); | ||
}, | ||
}), | ||
test: new FunctionImplementation({ | ||
data: null, | ||
depends: ["getByStrings"], | ||
supplies: "test", | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
func(...args: any[]) { | ||
debugger; | ||
return "the test worked"; | ||
}, | ||
}), | ||
} as { [key: string]: FunctionImplementation }; | ||
export { | ||
implementationStore, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { IModImplementation } from "../api/ModImplementation"; | ||
import { getKeyValue } from "../utils.js"; | ||
|
||
export interface IFunctionImplementation { | ||
supplies: string, | ||
depends: string[], | ||
data: any, | ||
func: (...args: any[]) => any, | ||
isWrapper?: boolean, | ||
} | ||
class FunctionImplementation implements IFunctionImplementation { | ||
supplies: string; | ||
depends: string[]; | ||
data: any; | ||
func: (...args: any[]) => any; | ||
isWrapper?: boolean | undefined; | ||
// constructor(supplies: string, depends: string[], data: any, func: (...args: any[]) => any) { | ||
// this.supplies = supplies; | ||
// this.depends = depends; | ||
// this.data = data; | ||
// this.func = func; | ||
// } | ||
constructor(options: IFunctionImplementation) { | ||
const { supplies, depends, data, func, isWrapper } = options; | ||
this.supplies = supplies!; | ||
this.depends = depends!; | ||
Object.defineProperty(this, "data", { value: data, enumerable: data !== null }); | ||
this.func = func!; | ||
// this.isWrapper = isWrapper === true; | ||
Object.defineProperty(this, "isWrapper", { value: isWrapper, enumerable: isWrapper === true }); | ||
} | ||
} | ||
export { | ||
FunctionImplementation, | ||
}; | ||
// import * as WebpackImplementations from "./Webpack.js"; | ||
import { createFunctionFromObjectProperty } from "../api/RuntimeGenerators.js"; | ||
import { readdirSync } from "fs"; | ||
import * as url from 'url'; | ||
import * as path from 'path'; | ||
import { IMPLEMENTATION_STORES_PATH_SOURCE, IMPLEMENTATION_STORES_PATH_VAR_NAME } from "../constants.js"; | ||
const __dirname = url.fileURLToPath(new URL('.', import.meta.url)); | ||
|
||
export const implementationStores = { | ||
// "Webpack": WebpackImplementations, | ||
} as { [category: string]: { implementationStore: { [key: string]: FunctionImplementation }, targetMod: IModImplementation } }; | ||
export async function initStores() { | ||
const stores = readdirSync(`${__dirname}`).filter(x => !x.startsWith("index.")); | ||
console.log(stores); | ||
for (let index = 0; index < stores.length; index++) { | ||
const filler = import(url.pathToFileURL(`${__dirname}/${stores[index]}`).href); | ||
const mod = await filler; | ||
implementationStores[path.parse(stores[index]).name] = mod; | ||
} | ||
console.log("done"); | ||
} | ||
export function doesImplement(mod: IModImplementation, category: string, method: string) { | ||
const categoryObj = getKeyValue(mod, category as keyof IModImplementation); | ||
return getKeyValue(categoryObj, method as never) != undefined; | ||
} | ||
|
||
export function __requireInternal(mod: IModImplementation, category: string, method: string, ignoreWrappers: boolean = false) { | ||
if (doesImplement(mod, category, method)) { | ||
const categoryObj = getKeyValue(mod, category as keyof IModImplementation); | ||
const result = getKeyValue(categoryObj, method as never); | ||
if (ignoreWrappers) | ||
return result; | ||
else { | ||
if (result["wrapperName"]) { | ||
method = result["wrapperName"]; | ||
} | ||
else | ||
return result; | ||
} | ||
} | ||
if (implementationStores[category].targetMod !== undefined) | ||
implementationStores[category].targetMod = mod; | ||
const foundImplementation = implementationStores[category].implementationStore[method]; | ||
if (foundImplementation == undefined) | ||
return null; // depends failed | ||
return createFunctionFromObjectProperty(`${IMPLEMENTATION_STORES_PATH_SOURCE}.${IMPLEMENTATION_STORES_PATH_VAR_NAME}.${category}`, method); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const IMPLEMENTATION_STORES_PATH_SOURCE = "globalThis"; | ||
export const IMPLEMENTATION_STORES_PATH_VAR_NAME = "implementationStores"; | ||
export const IMPLEMENTATION_STORES_PATH_REQ = IMPLEMENTATION_STORES_PATH_VAR_NAME + "_require"; |
Oops, something went wrong.