2026-07-09 23:09:29 +07:00
|
|
|
import * as jsonStorage from '@revenge-mod/json-storage'
|
|
|
|
|
import { getJsonStorage } from '@revenge-mod/json-storage'
|
|
|
|
|
import { exists, rm } from '@revenge-mod/modules/native/fs'
|
|
|
|
|
import {
|
|
|
|
|
InternalPluginFlags,
|
|
|
|
|
PluginFlags,
|
|
|
|
|
registerPlugin,
|
|
|
|
|
} from '@revenge-mod/plugins/_'
|
2026-07-19 20:19:19 +07:00
|
|
|
import { pluginStorageDirFor } from '@revenge-mod/plugins/constants'
|
2026-07-09 23:09:29 +07:00
|
|
|
import { defineLazyProperty } from '@revenge-mod/utils/object'
|
|
|
|
|
import type { JsonStorage, JsonStorageOptions } from '@revenge-mod/json-storage'
|
|
|
|
|
import type { InitPluginApi, Plugin } from '@revenge-mod/plugins/types'
|
|
|
|
|
import type { AnyObject } from '@revenge-mod/utils/types'
|
|
|
|
|
|
|
|
|
|
type PluginJsonStorageOptions = JsonStorageOptions & {
|
|
|
|
|
file?: string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const storageOptions = new WeakMap<Plugin<any, any>, PluginJsonStorageOptions>()
|
|
|
|
|
|
|
|
|
|
registerPlugin(
|
|
|
|
|
{
|
|
|
|
|
id: 'revenge.api.json-storage',
|
|
|
|
|
name: 'JSON Storage API',
|
|
|
|
|
description: '@revenge-mod/json-storage API for plugins.',
|
|
|
|
|
author: 'Revenge',
|
|
|
|
|
icon: 'PollsIcon',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
preInit({ decorate, unscoped }) {
|
|
|
|
|
unscoped.jsonStorage = jsonStorage
|
|
|
|
|
|
|
|
|
|
decorate((plugin, { jsonStorage }) => {
|
|
|
|
|
if (jsonStorage) storageOptions.set(plugin, jsonStorage)
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
init({ decorate }) {
|
|
|
|
|
const makePluginStorage = (
|
|
|
|
|
plugin: Plugin,
|
|
|
|
|
opts?: PluginJsonStorageOptions,
|
|
|
|
|
) =>
|
|
|
|
|
getJsonStorage(
|
|
|
|
|
jsonStorage.pluginStoragePathFor(
|
|
|
|
|
plugin.manifest.id,
|
|
|
|
|
opts?.file,
|
|
|
|
|
),
|
|
|
|
|
opts,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
decorate(plugin => {
|
|
|
|
|
const opts = storageOptions.get(plugin)
|
|
|
|
|
|
|
|
|
|
if (opts?.load)
|
|
|
|
|
plugin.api.jsonStorage = makePluginStorage(plugin, opts)
|
|
|
|
|
else
|
|
|
|
|
defineLazyProperty(plugin.api, 'jsonStorage', () =>
|
|
|
|
|
makePluginStorage(plugin, opts),
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
PluginFlags.Enabled,
|
|
|
|
|
// biome-ignore format: Don't format this
|
|
|
|
|
InternalPluginFlags.Internal |
|
|
|
|
|
InternalPluginFlags.Essential |
|
|
|
|
|
InternalPluginFlags.API,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
export async function deleteJsonStorageForPlugin(plugin: Plugin<any, any>) {
|
2026-07-19 20:19:19 +07:00
|
|
|
const path = pluginStorageDirFor(plugin.manifest.id)
|
2026-07-09 23:09:29 +07:00
|
|
|
if (await exists(path)) await rm(path)
|
|
|
|
|
|
|
|
|
|
const api = plugin.api as
|
|
|
|
|
| InitPluginApi<{ jsonStorage: AnyObject }>
|
|
|
|
|
| undefined
|
|
|
|
|
|
2026-07-19 20:19:19 +07:00
|
|
|
// Only update already initialized storages
|
|
|
|
|
const storage = Object.getOwnPropertyDescriptor(api, 'jsonStorage')
|
|
|
|
|
?.value as JsonStorage<AnyObject> | undefined
|
|
|
|
|
|
2026-07-09 23:09:29 +07:00
|
|
|
await storage?.get()
|
|
|
|
|
}
|