forked from vendetta-mod/Vendetta
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
105 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { addJSXCallback } from "@lib/api/jsx"; | ||
import { after } from "@lib/api/patcher"; | ||
import { findByName } from "@metro"; | ||
import { FC, useEffect, useState } from "react"; | ||
import { ImageSourcePropType, ViewStyle } from "react-native"; | ||
|
||
import { defineCorePlugin } from ".."; | ||
|
||
interface BunnyBadge { | ||
label: string; | ||
url: string; | ||
} | ||
|
||
const useBadgesModule = findByName("useBadges", false); | ||
|
||
export default defineCorePlugin({ | ||
manifest: { | ||
id: "bunny.badges", | ||
name: "Badges", | ||
version: "1.0.0", | ||
description: "Adds badges to user's profile", | ||
authors: [{ name: "pylixonly" }] | ||
}, | ||
start() { | ||
const propHolder = {} as Record<string, any>; | ||
const badgeCache = {} as Record<string, BunnyBadge[]>; | ||
|
||
addJSXCallback("RenderedBadge", (_, ret) => { | ||
if (ret.props.id.match(/bunny-\d+-\d+/)) { | ||
Object.assign(ret.props, propHolder[ret.props.id]); | ||
} | ||
}); | ||
|
||
after("default", useBadgesModule, ([user], r) => { | ||
const [badges, setBadges] = useState<BunnyBadge[]>(user ? badgeCache[user.userId] ??= [] : []); | ||
|
||
useEffect(() => { | ||
if (user) { | ||
fetch(`https://raw.githubusercontent.com/pyoncord/badges/refs/heads/main/${user.userId}.json`) | ||
.then(r => r.json()) | ||
.then(badges => setBadges(badgeCache[user.userId] = badges)); | ||
} | ||
}, [user]); | ||
|
||
badges.forEach((badges, i) => { | ||
propHolder[`bunny-${user.userId}-${i}`] = { | ||
source: { uri: badges.url }, | ||
id: `bunny-${i}`, | ||
label: badges.label | ||
}; | ||
|
||
r.push({ | ||
id: `bunny-${user.userId}-${i}`, | ||
description: badges.label, | ||
icon: "2ba85e8026a8614b640c2837bcdfe21b", | ||
}); | ||
}); | ||
}); | ||
} | ||
}); |
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,40 @@ | ||
import { after } from "@lib/api/patcher"; | ||
import { findByPropsLazy } from "@metro"; | ||
|
||
type Callback = (Component: any, ret: JSX.Element) => void; | ||
const callbacks = new Map<string, Callback[]>(); | ||
|
||
const jsxRuntime = findByPropsLazy("jsx", "jsxs"); | ||
|
||
export function addJSXCallback(Component: string, callback: Callback) { | ||
if (!callbacks.has(Component)) callbacks.set(Component, []); | ||
callbacks.get(Component)!.push(callback); | ||
} | ||
|
||
export function removeJSXCallback(Component: string, callback: Callback) { | ||
if (!callbacks.has(Component)) return; | ||
const cbs = callbacks.get(Component)!; | ||
cbs.splice(cbs.indexOf(callback), 1); | ||
if (cbs.length === 0) callbacks.delete(Component); | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export function patchJSX() { | ||
// Only a simple name check for now | ||
const callback = ([Component, props]: any[], ret: any) => { | ||
if (typeof Component === "function" && callbacks.has(Component.name)) { | ||
const cbs = callbacks.get(Component.name)!; | ||
for (const cb of cbs) ret = cb(Component, ret); | ||
return ret; | ||
} | ||
}; | ||
|
||
const patches = [ | ||
after("jsx", jsxRuntime, callback), | ||
after("jsxs", jsxRuntime, callback) | ||
]; | ||
|
||
return () => patches.forEach(unpatch => unpatch()); | ||
} |