Skip to content

Commit

Permalink
fix(shelterpc): asset resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
SpikeHD committed Dec 1, 2024
1 parent 5bec603 commit 3351a89
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 33 deletions.
43 changes: 10 additions & 33 deletions plugins/shelteRPC/index.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import { createApi, webpackChunk } from '@cumjar/websmack'
import { backend, invoke } from '../../api/api.js';

Check failure on line 2 in plugins/shelteRPC/index.tsx

View workflow job for this annotation

GitHub Actions / tsc-eslint-checks

Extra semicolon
import RegisteredGames from './components/RegisteredGames'
import { css, classes } from './index.scss'

interface AssetCache {
[key: string]: {
id: string;
name: string;
type: number;
}[];
}

const {
flux: {
dispatcher: FluxDispatcher
Expand All @@ -27,6 +20,11 @@ const {
http,
} = shelter

const chunk = webpackChunk()
const wp = chunk && createApi([undefined, ...chunk])
const c = wp.findByCode('getAssetImage: ')
const fetchAssetIds = async (id, asset) => (await c.fetchAssetIds(id, [asset, null]))[0]

let injectedCss = false

if (!injectedCss) {
Expand All @@ -36,8 +34,6 @@ if (!injectedCss) {

let maybeUnregisterGameSettings = [() => {}]

const cachedAssets: AssetCache = {}

let ws: WebSocket
const apps: Record<string, { name: string } | string> = {}

Expand All @@ -48,38 +44,19 @@ async function lookupApp(appId: string): Promise<string> {
return (await http.get(`/oauth2/applications/${appId}/rpc`))?.body || 'Unknown'
}

export const generateAssetId = async (appId: string, asset: string) => {
// get cached assets for the appid if we dont have them already
if (!cachedAssets[appId]) {
const resp = await http.get(`/oauth2/applications/${appId}/assets`)

if (resp.status !== 200) {
console.log('Failed to fetch assets')
}

cachedAssets[appId] = resp.body as AssetCache[typeof appId]
}

const assetId = cachedAssets[appId].find((a) => a.name === asset)?.id

return assetId
}

const isProbablyUrl = (str: string) => str.startsWith('http://') || str.startsWith('https://')

async function handleMessage(e: MessageEvent<string>) {
const data = JSON.parse(e.data)
const assets = data.activity?.assets

if (data.cmd) return handleCmd(data)

if (assets?.large_image && !isProbablyUrl(assets.large_image))
assets.large_image = await generateAssetId(
if (assets?.large_image)
assets.large_image = await fetchAssetIds(
data.activity.application_id,
assets.large_image
)
if (assets?.small_image && !isProbablyUrl(assets.small_image))
assets.small_image = await generateAssetId(
if (assets?.small_image)
assets.small_image = await fetchAssetIds(
data.activity.application_id,
assets.small_image
)
Expand Down
7 changes: 7 additions & 0 deletions plugins/shelteRPC/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "shelterpc",
"dependencies": {
"@cumjar/websmack": "^1.2.0"
},
"type": "module"
}
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3351a89

Please sign in to comment.