2025-05-08 20:25:11 +07:00
|
|
|
import { Logger } from '@revenge-mod/discord/common'
|
|
|
|
|
|
2025-05-09 21:53:48 +07:00
|
|
|
import { _uapi } from './apis'
|
|
|
|
|
import { PluginFlags as Flag, PluginStatus as Status } from './constants'
|
2025-04-29 21:38:14 +07:00
|
|
|
|
2025-05-08 18:52:57 +07:00
|
|
|
import type {
|
|
|
|
|
InitPluginApi,
|
|
|
|
|
Plugin,
|
|
|
|
|
PluginApi,
|
|
|
|
|
PluginCleanup,
|
|
|
|
|
PluginFlags,
|
|
|
|
|
PluginLifecycles,
|
|
|
|
|
PluginManifest,
|
2025-05-09 21:53:48 +07:00
|
|
|
} from './types'
|
2025-04-29 21:38:14 +07:00
|
|
|
|
2025-05-08 18:52:57 +07:00
|
|
|
const _plugins = new Map<PluginManifest['id'], InternalPlugin>()
|
2025-05-09 21:53:48 +07:00
|
|
|
const _metas = new Map<
|
|
|
|
|
PluginManifest['id'],
|
|
|
|
|
[api: InitPluginApi | PluginApi, promises: Promise<void>[], iflags: InternalPluginFlags]
|
|
|
|
|
>()
|
2025-04-29 21:38:14 +07:00
|
|
|
|
|
|
|
|
export function registerPlugin(
|
2025-05-08 18:52:57 +07:00
|
|
|
manifest: PluginManifest,
|
|
|
|
|
lifecycles: PluginLifecycles,
|
|
|
|
|
flags: PluginFlags,
|
2025-05-09 21:53:48 +07:00
|
|
|
iflags: InternalPluginFlags,
|
2025-04-29 21:38:14 +07:00
|
|
|
) {
|
|
|
|
|
// TODO(plugins): verify plugin manifest
|
|
|
|
|
if (_plugins.has(manifest.id)) throw new Error(`Plugin with ID "${manifest.id}" already registered`)
|
|
|
|
|
|
|
|
|
|
const plugin: InternalPlugin = {
|
|
|
|
|
cleanups: [],
|
|
|
|
|
errors: [],
|
|
|
|
|
manifest,
|
|
|
|
|
lifecycles,
|
2025-05-01 01:18:36 +07:00
|
|
|
status: 0,
|
2025-04-29 21:38:14 +07:00
|
|
|
flags,
|
2025-05-09 21:53:48 +07:00
|
|
|
async disable() {
|
|
|
|
|
// If plugin is not stopped, and is also not stopping, we need to stop it
|
|
|
|
|
if (plugin.status && !(plugin.status & Status.Stopping)) await stopPlugin(plugin)
|
|
|
|
|
|
|
|
|
|
plugin.flags &= ~Flag.Enabled
|
2025-04-29 21:38:14 +07:00
|
|
|
},
|
2025-05-09 21:53:48 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_metas.set(manifest.id, [
|
|
|
|
|
{
|
|
|
|
|
cleanup: (...items) => {
|
|
|
|
|
plugin.cleanups.push(...items)
|
|
|
|
|
},
|
|
|
|
|
get logger() {
|
|
|
|
|
return (this.logger = new Logger(`Revenge > ${manifest.name}`))
|
|
|
|
|
},
|
|
|
|
|
plugin,
|
|
|
|
|
unscoped: _uapi,
|
2025-04-29 21:38:14 +07:00
|
|
|
},
|
2025-05-09 21:53:48 +07:00
|
|
|
[],
|
|
|
|
|
iflags,
|
|
|
|
|
])
|
2025-05-01 01:18:36 +07:00
|
|
|
|
2025-05-09 21:53:48 +07:00
|
|
|
_plugins.set(manifest.id, plugin)
|
|
|
|
|
}
|
2025-05-01 01:18:36 +07:00
|
|
|
|
2025-05-09 21:53:48 +07:00
|
|
|
function handlePluginError(e: unknown, plugin: InternalPlugin) {
|
|
|
|
|
plugin.errors.push(e)
|
|
|
|
|
plugin.flags |= Flag.Errored
|
|
|
|
|
if (plugin.flags & Flag.Enabled) return plugin.disable()
|
|
|
|
|
}
|
2025-04-29 21:38:14 +07:00
|
|
|
|
2025-05-09 21:53:48 +07:00
|
|
|
// function preparePluginStart(id: PluginManifest['id']) {
|
|
|
|
|
// const api = _metas.get(id)?.[1]!
|
|
|
|
|
// api.settings ??= ...
|
|
|
|
|
// }
|
2025-04-29 21:38:14 +07:00
|
|
|
|
2025-05-09 21:53:48 +07:00
|
|
|
export function enablePlugin(plugin: InternalPlugin) {
|
|
|
|
|
// TODO(plugins): write to storage
|
|
|
|
|
plugin.flags |= Flag.Enabled
|
|
|
|
|
}
|
2025-05-01 01:18:36 +07:00
|
|
|
|
2025-05-09 21:53:48 +07:00
|
|
|
export async function initPlugin(plugin: InternalPlugin) {
|
|
|
|
|
const { manifest, lifecycles } = plugin
|
|
|
|
|
const [api, promises] = _metas.get(manifest.id)!
|
2025-05-01 01:18:36 +07:00
|
|
|
|
2025-05-09 21:53:48 +07:00
|
|
|
if (!(plugin.flags & Flag.Enabled)) throw new Error(`Plugin "${manifest.id}" is not enabled`)
|
|
|
|
|
if (plugin.status & (Status.Initing | Status.Inited))
|
|
|
|
|
throw new Error(`Plugin "${manifest.id}" is initializing or already initialized`)
|
|
|
|
|
|
|
|
|
|
plugin.status |= Status.Initing
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const prom = lifecycles.init?.(api)
|
|
|
|
|
promises.push(prom)
|
|
|
|
|
await prom
|
|
|
|
|
|
|
|
|
|
// plugin.disable() already handles, so it's the try block
|
|
|
|
|
plugin.status |= Status.Inited
|
|
|
|
|
plugin.status &= ~Status.Initing
|
|
|
|
|
} catch (e) {
|
|
|
|
|
await handlePluginError(e, plugin)
|
2025-04-29 21:38:14 +07:00
|
|
|
}
|
2025-05-09 21:53:48 +07:00
|
|
|
}
|
2025-04-29 21:38:14 +07:00
|
|
|
|
2025-05-09 21:53:48 +07:00
|
|
|
export async function startPlugin(plugin: InternalPlugin) {
|
|
|
|
|
const { manifest, lifecycles } = plugin
|
|
|
|
|
const [api, promises] = _metas.get(manifest.id)!
|
2025-05-08 18:52:57 +07:00
|
|
|
|
2025-05-09 21:53:48 +07:00
|
|
|
if (!(plugin.flags & Flag.Enabled)) throw new Error(`Plugin "${manifest.id}" is not enabled`)
|
|
|
|
|
if (plugin.status & (Status.Starting | Status.Started))
|
|
|
|
|
throw new Error(`Plugin "${manifest.id}" is starting or already started`)
|
2025-04-29 21:38:14 +07:00
|
|
|
|
2025-05-09 21:53:48 +07:00
|
|
|
// Clear errors from previous runs
|
|
|
|
|
plugin.errors = []
|
|
|
|
|
plugin.status &= ~Flag.Errored
|
|
|
|
|
|
|
|
|
|
plugin.status |= Status.Starting
|
|
|
|
|
|
|
|
|
|
// preparePluginStart(manifest.id)
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const prom = lifecycles.start?.(api as PluginApi)
|
|
|
|
|
promises.push(prom)
|
|
|
|
|
await prom
|
|
|
|
|
|
|
|
|
|
// disablePlugin() already handles cleaning up statuses, so it's in the try block
|
|
|
|
|
plugin.status |= Status.Started
|
|
|
|
|
plugin.status &= ~Status.Starting
|
|
|
|
|
} catch (e) {
|
|
|
|
|
await handlePluginError(e, plugin)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function stopPlugin(plugin: InternalPlugin) {
|
|
|
|
|
const { manifest, lifecycles } = plugin
|
|
|
|
|
const meta = _metas.get(manifest.id)!
|
|
|
|
|
const [api, promises] = meta
|
|
|
|
|
|
|
|
|
|
if (!(plugin.flags & Flag.Enabled)) throw new Error(`Plugin "${manifest.id}" is not enabled`)
|
|
|
|
|
if (plugin.status & Status.Stopping) throw new Error(`Plugin "${manifest.id}" is stopping`)
|
|
|
|
|
|
|
|
|
|
// If the plugin is initializing or starting, we need to wait for it to finish, then we'll stop it
|
|
|
|
|
if (plugin.status & (Status.Initing | Status.Starting)) await Promise.all(promises)
|
|
|
|
|
if (plugin.status & (Status.Inited | Status.Started))
|
|
|
|
|
throw new Error(`Plugin "${manifest.id}" is not initialized started`)
|
|
|
|
|
|
|
|
|
|
// In case the plugin only has init() and stop()
|
|
|
|
|
// preparePluginStart(manifest.id)
|
|
|
|
|
|
|
|
|
|
plugin.status |= Status.Stopping
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await lifecycles.stop?.(api as PluginApi)
|
|
|
|
|
} catch (e) {
|
|
|
|
|
await handlePluginError(e, plugin)
|
|
|
|
|
} finally {
|
|
|
|
|
// Run cleanups
|
|
|
|
|
const results = await Promise.allSettled(plugin.cleanups.map(cleanup => cleanup()))
|
|
|
|
|
for (const result of results)
|
|
|
|
|
if (result.status === 'rejected') {
|
|
|
|
|
await handlePluginError(result.reason, plugin)
|
|
|
|
|
// Some cleanup was unsuccessful, so we need to reload the app
|
|
|
|
|
plugin.flags |= Flag.ReloadRequired
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Clear unnecessary data
|
|
|
|
|
meta[1] = []
|
|
|
|
|
plugin.cleanups = []
|
|
|
|
|
plugin.status = 0
|
|
|
|
|
}
|
2025-04-29 21:38:14 +07:00
|
|
|
}
|
|
|
|
|
|
2025-05-04 16:48:18 +07:00
|
|
|
export function getEnabledPluginsCount() {
|
|
|
|
|
let count = 0
|
2025-05-08 18:52:57 +07:00
|
|
|
for (const plugin of _plugins.values()) if (plugin.flags & Flag.Enabled) count++
|
2025-05-04 16:48:18 +07:00
|
|
|
return count
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-30 18:53:41 +07:00
|
|
|
export async function initPlugins() {
|
2025-04-29 21:38:14 +07:00
|
|
|
const promises: Promise<void>[] = []
|
2025-05-09 21:53:48 +07:00
|
|
|
for (const plugin of _plugins.values()) if (plugin.flags & Flag.Enabled) promises.push(initPlugin(plugin))
|
2025-04-29 21:38:14 +07:00
|
|
|
// These Promises never throw, so it is fine
|
2025-04-30 18:53:41 +07:00
|
|
|
await Promise.all(promises)
|
2025-04-29 21:38:14 +07:00
|
|
|
}
|
|
|
|
|
|
2025-04-30 18:53:41 +07:00
|
|
|
export async function startPlugins() {
|
2025-04-29 21:38:14 +07:00
|
|
|
const promises: Promise<void>[] = []
|
2025-05-09 21:53:48 +07:00
|
|
|
for (const plugin of _plugins.values()) if (plugin.flags & Flag.Enabled) promises.push(startPlugin(plugin))
|
2025-04-29 21:38:14 +07:00
|
|
|
// These Promises never throw, so it is fine
|
2025-04-30 18:53:41 +07:00
|
|
|
await Promise.all(promises)
|
2025-04-29 21:38:14 +07:00
|
|
|
}
|
|
|
|
|
|
2025-05-08 18:52:57 +07:00
|
|
|
export interface InternalPlugin extends Plugin {
|
|
|
|
|
cleanups: PluginCleanup[]
|
2025-04-29 21:38:14 +07:00
|
|
|
}
|
2025-05-01 01:18:36 +07:00
|
|
|
|
|
|
|
|
export const InternalPluginFlags = {
|
|
|
|
|
/**
|
|
|
|
|
* Marks the plugin as internal.
|
|
|
|
|
*/
|
|
|
|
|
Internal: 1 << 0,
|
|
|
|
|
/**
|
|
|
|
|
* Marks the plugin as essential. This means it should not be removed, disabled, or stopped by normal means.
|
|
|
|
|
*/
|
|
|
|
|
Essential: 1 << 1,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type InternalPluginFlags = (typeof InternalPluginFlags)[keyof typeof InternalPluginFlags]
|