Skip to content

Commit

Permalink
Repair common modules
Browse files Browse the repository at this point in the history
  • Loading branch information
colin273 committed Jun 19, 2024
1 parent d9e8097 commit cf54635
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
41 changes: 39 additions & 2 deletions src/renderer/modules/common/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ProgressEvent, Request, Response } from "superagent";
import { waitForProps } from "../webpack";
import { filters, getFunctionBySource, waitForModule } from "../webpack";

interface HTTPAttachment {
file: string | Blob | Buffer;
Expand Down Expand Up @@ -125,4 +125,41 @@ export interface API {
setRequestPatch: (patch: RequestPatch) => void;
}

export default await waitForProps<API>("getAPIBaseURL", "HTTP");
const realApiModule = await waitForModule<Record<string, API[keyof API]>>(
filters.bySource("rateLimitExpirationHandler"),
);
const exportedValues = Object.values(realApiModule);

type APIErrorClass = typeof V6OrEarlierAPIError | typeof APIError;
const exportedClasses = exportedValues.filter((v) => typeof v === "function" && v.prototype);
const v6ErrorClass = exportedClasses.find(
(c) => "getFieldMessage" in (c as APIErrorClass).prototype,
) as typeof V6OrEarlierAPIError;
const v8ErrorClass = exportedClasses.find(
(c) => "hasFieldErrors" in (c as APIErrorClass).prototype,
) as typeof APIError;
const http = exportedValues.find((v) => typeof v === "object") as HTTP;
const invalidFormBodyErrorCode = exportedValues.find((v) => typeof v === "number") as number;

const getAPIBaseURL = getFunctionBySource<API["getAPIBaseURL"]>(
realApiModule,
"GLOBAL_ENV.API_ENDPOINT",
)!;
const convertSkemaError = getFunctionBySource<API["convertSkemaError"]>(realApiModule, "message")!;
// TODO: these suck. Make them better later.
const setAwaitOnline = getFunctionBySource<API["setAwaitOnline"]>(realApiModule, /v\s*=\s*e/)!;
const setRequestPatch = getFunctionBySource<API["setRequestPatch"]>(realApiModule, /g\s*=\s*e/)!;

// "If only, if only," the woodpecker sighs...
//export default await waitForProps<API>("getAPIBaseURL", "HTTP");

export default {
INVALID_FORM_BODY_ERROR_CODE: invalidFormBodyErrorCode,
V6OrEarlierAPIError: v6ErrorClass,
V8APIError: v8ErrorClass,
HTTP: http,
getAPIBaseURL,
convertSkemaError,
setAwaitOnline,
setRequestPatch,
} as API;
19 changes: 16 additions & 3 deletions src/renderer/modules/common/constants.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { virtualMerge } from "src/renderer/util";
import { filters, getExportsForProps, waitForModule, waitForProps } from "../webpack";
import { filters, getExportsForProps, waitForModule } from "../webpack";

type StringConcat = (...rest: string[]) => string;

const ConstantsCommon = await waitForProps<Record<string, unknown>>("Links", "RPCCommands");
const Constants = await waitForProps<Record<string, unknown>>("Endpoints", "Routes");
//const ConstantsCommon = await waitForProps<Record<string, unknown>>("Links", "RPCCommands");
const ConstantsCommon = await waitForModule<Record<string, unknown>>(
filters.bySource("dis.gd/request"),
);
//const Constants = await waitForProps<Record<string, unknown>>("Endpoints", "Routes");
const Constants = await waitForModule<Record<string, unknown>>(
filters.bySource("users/@me/relationships"),
);
export const raw = virtualMerge(ConstantsCommon, Constants);

export const Permissions = getExportsForProps<Record<string, bigint>>(ConstantsCommon, [
Expand Down Expand Up @@ -66,10 +72,16 @@ export const UserFlags = getExportsForProps<Record<string, string | number>>(Con
])!;

// ThemeColor
//Ambiguous: should this be the just-dashed-names or --var(css-var-strings)?
// Go with the latter for now.
/*
export const CSSVariables = await waitForProps<Record<string, string>>(
"TEXT_NORMAL",
"BACKGROUND_PRIMARY",
);
*/
// We *should* be able to do props, but there's so much extra junk with the current search implementation.
export const CSSVariables = await waitForModule(filters.bySource('="var(--background-floating)"'));

interface ColorResponse {
hex: () => string;
Expand Down Expand Up @@ -117,6 +129,7 @@ interface ColorMod {
unsafe_rawColors: Record<string, UnsafeRawColor>;
}

// This could really be a search by props, for unsafe_rawColors.
export const ColorGenerator = await waitForModule<ColorMod>(
filters.bySource(/\w+\.unsafe_rawColors\[\w+\]\.resolve\(\w+\)/),
);
Expand Down
8 changes: 6 additions & 2 deletions src/renderer/modules/common/flux.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ const SnapshotStoreClass = await waitForModule<typeof SnapshotStore>(
filters.bySource("SnapshotStores"),
);

// Disabling FluxHooks search for now.
// This and SnapshotStore really should be split off into separate modules.
/*
interface FluxHooks {
useStateFromStores: <T>(
stores: Store[],
Expand Down Expand Up @@ -221,7 +224,8 @@ const FluxHooks = {
useStateFromStoresArray: FluxHooksMod.useStateFromStoresArray,
useStateFromStoresObject: FluxHooksMod.useStateFromStoresObject,
};
*/

export type Flux = FluxMod & { SnapshotStore: typeof SnapshotStore } & typeof FluxHooks;
export type Flux = FluxMod & { SnapshotStore: typeof SnapshotStore } /* & typeof FluxHooks*/;

export default { ...FluxMod, SnapshotStore: SnapshotStoreClass, ...FluxHooks } as Flux;
export default { ...FluxMod, SnapshotStore: SnapshotStoreClass /*, ...FluxHooks*/ } as Flux;

0 comments on commit cf54635

Please sign in to comment.