This repository was archived by the owner on Apr 28, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.tsx
More file actions
225 lines (199 loc) · 8.02 KB
/
index.tsx
File metadata and controls
225 lines (199 loc) · 8.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import { TableRowIcon } from '@revenge-mod/modules/common/components'
import { registerPlugin } from '@revenge-mod/plugins/internals'
import { sleep } from '@revenge-mod/utils/functions'
import { findInReactTree } from '@revenge-mod/utils/react'
import {
type PressableRowConfig,
type RawRowConfig,
type RowConfig,
type ToggleRowConfig,
customData,
} from '@revenge-mod/ui/settings'
import { type FC, createContext } from 'react'
import AboutSettingsPage from './pages/About'
import ContributorsSettingsPage from './pages/Contributors'
import CustomPageRenderer from './pages/CustomPageRenderer'
import PluginsSettingsPage from './pages/Plugins'
import PluginBrowserPage from './pages/Plugins/Browser'
import RevengeSettingsPage from './pages/Revenge'
import Contributors from './contributors'
import type { PluginContextFor } from '@revenge-mod/plugins'
export type Storage = {
plugins: {
showInternal: boolean
showUnmanageable: boolean
}
}
const plugin = registerPlugin<Storage>(
{
name: 'Settings',
author: 'Revenge',
description: 'Settings menus for Revenge',
id: 'revenge.settings',
version: '1.0.0',
icon: 'SettingsIcon',
},
{
async afterAppRender(context) {
const {
patcher,
revenge: {
assets,
modules,
ui: { settings: sui },
},
} = context
const contributors = new Set<string>()
for (const member of Object.values(Contributors).flat()) {
if (!member.icon || contributors.has(member.name)) continue
contributors.add(member.name)
assets.registerCustom(
{
name: `Revenge.Contributors.${member.name}`,
type: 'webp',
},
member.icon,
)
}
sui.createSection({
name: 'Revenge',
settings: {
Revenge: {
type: 'route',
label: 'Revenge',
icon: assets.getIndexByName('Revenge.RevengeIcon'),
component: RevengeSettingsPage,
},
RevengePlugins: {
type: 'route',
label: 'Plugins',
icon: assets.getIndexByName('Revenge.PluginIcon'),
component: () => (
<PluginContext.Provider value={context}>
<PluginsSettingsPage />
</PluginContext.Provider>
),
},
},
})
sui.createRoute('RevengePluginBrowser', {
type: 'route',
label: 'Plugin Browser',
component: PluginBrowserPage,
icon: assets.getIndexByName('Revenge.PluginIcon'),
parent: 'RevengePlugins',
})
sui.createRoute('RevengeAbout', {
type: 'route',
label: 'About',
component: AboutSettingsPage,
icon: assets.getIndexByName('CircleInformationIcon-primary'),
parent: 'Revenge',
})
sui.createRoute('RevengeContributors', {
type: 'route',
label: 'Contributors',
component: ContributorsSettingsPage,
icon: assets.getIndexByName('FriendsIcon'),
parent: 'Revenge',
})
sui.createRoute('RevengeCustomPage', {
type: 'route',
label: 'Revenge Page',
unsearchable: true,
component: CustomPageRenderer,
predicate: () => false,
})
// Wait single tick
await sleep(0)
const SettingsConstants = modules.findByProps('SETTING_RENDERER_CONFIG')!
const SettingsOverviewScreen = modules.findByName<FC, false>('SettingsOverviewScreen', false)!
const originalRendererConfig = SettingsConstants.SETTING_RENDERER_CONFIG as Record<string, RawRowConfig>
let rendererConfig = originalRendererConfig
Object.defineProperty(SettingsConstants, 'SETTING_RENDERER_CONFIG', {
enumerable: true,
configurable: true,
get: () =>
({
...getCustomSettingRows(),
...rendererConfig,
}) satisfies Record<string, RawRowConfig>,
set: v => (rendererConfig = v),
})
patcher.after(
SettingsOverviewScreen,
'default',
(_, children) => {
const registeredCustomRows = new Set(
Object.values(customData.sections).flatMap(({ settings }) => Object.keys(settings)),
)
const { sections } = findInReactTree(
children as Extract<typeof children, { props: unknown }>,
i => i.props?.sections,
).props as {
sections: Array<{ label: string; settings: string[] }>
}
// This means we've already spliced new sections
if (
sections.findIndex(section =>
section.settings.some(setting => registeredCustomRows.has(setting)),
) !== -1
)
return
let index = -~sections.findIndex(section => section.settings.includes('ACCOUNT')) || 1
for (const key in customData.sections) {
const section = customData.sections[key]!
sections.splice(index++, 0, {
label: section.name,
settings: Object.keys(section.settings),
})
}
},
'addNewSettingsSections',
)
},
initializeStorage: () => ({
plugins: {
showInternal: false,
showUnmanageable: false,
},
}),
},
{ external: false, manageable: false },
)
export const PluginContext = createContext<PluginContextFor<typeof plugin, 'AfterAppRender'>>(null!)
function getCustomSettingRows() {
// OMG, UNBOUND REFERENCE????
return [...Object.values(customData.sections), { name: '(unbound)', settings: customData.rows }]
.map(section =>
Object.entries(section.settings).reduce<Record<string, RawRowConfig>>((rows, [key, row]) => {
rows[key] = transformSettingRowToRawSettingRow(key, row)
return rows
}, {}),
)
.reduce((rows, newRows) => Object.assign(rows, newRows), {})
}
function transformSettingRowToRawSettingRow(key: string, row: RowConfig): RawRowConfig {
return {
title: () => row.label,
parent: row.parent ?? null,
icon: row.icon,
IconComponent: row.icon ? () => TableRowIcon({ source: row.icon! }) : undefined,
unsearchable: row.unsearchable,
screen:
row.type === 'route'
? {
route: key,
getComponent: () => row.component,
}
: undefined!,
onPress: (row as PressableRowConfig).onPress,
useDescription: row.description ? () => row.description! : undefined,
useTrailing: row.trailing ? () => row.trailing! : undefined,
useIsDisabled: typeof row.disabled === 'boolean' ? () => row.disabled! : undefined,
usePredicate: row.predicate,
onValueChange: (row as ToggleRowConfig).onValueChange,
useValue: () => (row as ToggleRowConfig).value,
type: row.type,
} satisfies RawRowConfig
}