xref: /MusicFree/src/core/pluginManager.ts (revision d52aa40ee35db6a95adf7802ea10540bf51594a8)
14060c00aS猫头猫import {
2927dbe93S猫头猫    copyFile,
3927dbe93S猫头猫    exists,
4927dbe93S猫头猫    readDir,
5927dbe93S猫头猫    readFile,
6927dbe93S猫头猫    unlink,
7927dbe93S猫头猫    writeFile,
8927dbe93S猫头猫} from 'react-native-fs';
9927dbe93S猫头猫import CryptoJs from 'crypto-js';
10927dbe93S猫头猫import dayjs from 'dayjs';
11927dbe93S猫头猫import axios from 'axios';
12ef60be1cS猫头猫import bigInt from 'big-integer';
13ef60be1cS猫头猫import qs from 'qs';
14d4cd40d8S猫头猫import {InteractionManager, ToastAndroid} from 'react-native';
15927dbe93S猫头猫import pathConst from '@/constants/pathConst';
1625c1bd29S猫头猫import {compare, satisfies} from 'compare-versions';
17927dbe93S猫头猫import DeviceInfo from 'react-native-device-info';
18927dbe93S猫头猫import StateMapper from '@/utils/stateMapper';
19927dbe93S猫头猫import MediaMeta from './mediaMeta';
20927dbe93S猫头猫import {nanoid} from 'nanoid';
21ea6d708fS猫头猫import {devLog, errorLog, trace} from '../utils/log';
22927dbe93S猫头猫import Cache from './cache';
23cfa0fc07S猫头猫import {
240e4173cdS猫头猫    getInternalData,
250e4173cdS猫头猫    InternalDataType,
260e4173cdS猫头猫    isSameMediaItem,
270e4173cdS猫头猫    resetMediaItem,
280e4173cdS猫头猫} from '@/utils/mediaItem';
297993f90eS猫头猫import {
307993f90eS猫头猫    CacheControl,
31e08d37a3S猫头猫    emptyFunction,
327993f90eS猫头猫    internalSerializeKey,
337993f90eS猫头猫    localPluginHash,
347993f90eS猫头猫    localPluginPlatform,
357993f90eS猫头猫} from '@/constants/commonConst';
36927dbe93S猫头猫import delay from '@/utils/delay';
374d9d3c4cS猫头猫import * as cheerio from 'cheerio';
387d7e864fS猫头猫import CookieManager from '@react-native-cookies/cookies';
397d7e864fS猫头猫import he from 'he';
40ef714860S猫头猫import Network from './network';
410e4173cdS猫头猫import LocalMusicSheet from './localMusicSheet';
420e4173cdS猫头猫import {FileSystem} from 'react-native-file-access';
4374d0cf81S猫头猫import Mp3Util from '@/native/mp3Util';
44e08d37a3S猫头猫import {PluginMeta} from './pluginMeta';
4534588741S猫头猫import {useEffect, useState} from 'react';
46927dbe93S猫头猫
47927dbe93S猫头猫axios.defaults.timeout = 1500;
48927dbe93S猫头猫
49927dbe93S猫头猫const sha256 = CryptoJs.SHA256;
50927dbe93S猫头猫
51cfa0fc07S猫头猫export enum PluginStateCode {
52927dbe93S猫头猫    /** 版本不匹配 */
53927dbe93S猫头猫    VersionNotMatch = 'VERSION NOT MATCH',
54927dbe93S猫头猫    /** 无法解析 */
55927dbe93S猫头猫    CannotParse = 'CANNOT PARSE',
56927dbe93S猫头猫}
57927dbe93S猫头猫
58d5bfeb7eS猫头猫//#region 插件类
59927dbe93S猫头猫export class Plugin {
60927dbe93S猫头猫    /** 插件名 */
61927dbe93S猫头猫    public name: string;
62927dbe93S猫头猫    /** 插件的hash,作为唯一id */
63927dbe93S猫头猫    public hash: string;
64927dbe93S猫头猫    /** 插件状态:激活、关闭、错误 */
65927dbe93S猫头猫    public state: 'enabled' | 'disabled' | 'error';
66927dbe93S猫头猫    /** 插件支持的搜索类型 */
67927dbe93S猫头猫    public supportedSearchType?: string;
68927dbe93S猫头猫    /** 插件状态信息 */
69927dbe93S猫头猫    public stateCode?: PluginStateCode;
70927dbe93S猫头猫    /** 插件的实例 */
71927dbe93S猫头猫    public instance: IPlugin.IPluginInstance;
72927dbe93S猫头猫    /** 插件路径 */
73927dbe93S猫头猫    public path: string;
74927dbe93S猫头猫    /** 插件方法 */
75927dbe93S猫头猫    public methods: PluginMethods;
76d5bfeb7eS猫头猫    /** 用户输入 */
77d5bfeb7eS猫头猫    public userEnv?: Record<string, string>;
78927dbe93S猫头猫
7974d0cf81S猫头猫    constructor(
8074d0cf81S猫头猫        funcCode: string | (() => IPlugin.IPluginInstance),
8174d0cf81S猫头猫        pluginPath: string,
8274d0cf81S猫头猫    ) {
83927dbe93S猫头猫        this.state = 'enabled';
84927dbe93S猫头猫        let _instance: IPlugin.IPluginInstance;
85927dbe93S猫头猫        try {
8674d0cf81S猫头猫            if (typeof funcCode === 'string') {
874060c00aS猫头猫                // eslint-disable-next-line no-new-func
88927dbe93S猫头猫                _instance = Function(`
89927dbe93S猫头猫            'use strict';
90927dbe93S猫头猫            try {
91927dbe93S猫头猫              return ${funcCode};
92927dbe93S猫头猫            } catch(e) {
93927dbe93S猫头猫              return null;
94927dbe93S猫头猫            }
957d7e864fS猫头猫          `)()({
967d7e864fS猫头猫                    CryptoJs,
977d7e864fS猫头猫                    axios,
987d7e864fS猫头猫                    dayjs,
997d7e864fS猫头猫                    cheerio,
1007d7e864fS猫头猫                    bigInt,
1017d7e864fS猫头猫                    qs,
1027d7e864fS猫头猫                    he,
1037d7e864fS猫头猫                    CookieManager: {
1047d7e864fS猫头猫                        flush: CookieManager.flush,
1057d7e864fS猫头猫                        get: CookieManager.get,
1067d7e864fS猫头猫                    },
1077d7e864fS猫头猫                });
10874d0cf81S猫头猫            } else {
10974d0cf81S猫头猫                _instance = funcCode();
11074d0cf81S猫头猫            }
111927dbe93S猫头猫            this.checkValid(_instance);
112927dbe93S猫头猫        } catch (e: any) {
113927dbe93S猫头猫            this.state = 'error';
114927dbe93S猫头猫            this.stateCode = PluginStateCode.CannotParse;
115927dbe93S猫头猫            if (e?.stateCode) {
116927dbe93S猫头猫                this.stateCode = e.stateCode;
117927dbe93S猫头猫            }
118927dbe93S猫头猫            errorLog(`${pluginPath}插件无法解析 `, {
119927dbe93S猫头猫                stateCode: this.stateCode,
120927dbe93S猫头猫                message: e?.message,
121927dbe93S猫头猫                stack: e?.stack,
122927dbe93S猫头猫            });
123927dbe93S猫头猫            _instance = e?.instance ?? {
124927dbe93S猫头猫                _path: '',
125927dbe93S猫头猫                platform: '',
126927dbe93S猫头猫                appVersion: '',
12720e6a092S猫头猫                async getMediaSource() {
128927dbe93S猫头猫                    return null;
129927dbe93S猫头猫                },
130927dbe93S猫头猫                async search() {
131927dbe93S猫头猫                    return {};
132927dbe93S猫头猫                },
133927dbe93S猫头猫                async getAlbumInfo() {
134927dbe93S猫头猫                    return null;
135927dbe93S猫头猫                },
136927dbe93S猫头猫            };
137927dbe93S猫头猫        }
138927dbe93S猫头猫        this.instance = _instance;
139927dbe93S猫头猫        this.path = pluginPath;
140927dbe93S猫头猫        this.name = _instance.platform;
141927dbe93S猫头猫        if (this.instance.platform === '') {
142927dbe93S猫头猫            this.hash = '';
143927dbe93S猫头猫        } else {
14474d0cf81S猫头猫            if (typeof funcCode === 'string') {
145927dbe93S猫头猫                this.hash = sha256(funcCode).toString();
14674d0cf81S猫头猫            } else {
14774d0cf81S猫头猫                this.hash = sha256(funcCode.toString()).toString();
14874d0cf81S猫头猫            }
149927dbe93S猫头猫        }
150927dbe93S猫头猫
151927dbe93S猫头猫        // 放在最后
152927dbe93S猫头猫        this.methods = new PluginMethods(this);
153927dbe93S猫头猫    }
154927dbe93S猫头猫
155927dbe93S猫头猫    private checkValid(_instance: IPlugin.IPluginInstance) {
156927dbe93S猫头猫        /** 版本号校验 */
157927dbe93S猫头猫        if (
158927dbe93S猫头猫            _instance.appVersion &&
159927dbe93S猫头猫            !satisfies(DeviceInfo.getVersion(), _instance.appVersion)
160927dbe93S猫头猫        ) {
161927dbe93S猫头猫            throw {
162927dbe93S猫头猫                instance: _instance,
163927dbe93S猫头猫                stateCode: PluginStateCode.VersionNotMatch,
164927dbe93S猫头猫            };
165927dbe93S猫头猫        }
166927dbe93S猫头猫        return true;
167927dbe93S猫头猫    }
168927dbe93S猫头猫}
169d5bfeb7eS猫头猫//#endregion
170927dbe93S猫头猫
171d5bfeb7eS猫头猫//#region 基于插件类封装的方法,供给APP侧直接调用
172927dbe93S猫头猫/** 有缓存等信息 */
173927dbe93S猫头猫class PluginMethods implements IPlugin.IPluginInstanceMethods {
174927dbe93S猫头猫    private plugin;
175927dbe93S猫头猫    constructor(plugin: Plugin) {
176927dbe93S猫头猫        this.plugin = plugin;
177927dbe93S猫头猫    }
178927dbe93S猫头猫    /** 搜索 */
179927dbe93S猫头猫    async search<T extends ICommon.SupportMediaType>(
180927dbe93S猫头猫        query: string,
181927dbe93S猫头猫        page: number,
182927dbe93S猫头猫        type: T,
183927dbe93S猫头猫    ): Promise<IPlugin.ISearchResult<T>> {
184927dbe93S猫头猫        if (!this.plugin.instance.search) {
185927dbe93S猫头猫            return {
186927dbe93S猫头猫                isEnd: true,
187927dbe93S猫头猫                data: [],
188927dbe93S猫头猫            };
189927dbe93S猫头猫        }
190927dbe93S猫头猫
1914060c00aS猫头猫        const result =
1924060c00aS猫头猫            (await this.plugin.instance.search(query, page, type)) ?? {};
193927dbe93S猫头猫        if (Array.isArray(result.data)) {
194927dbe93S猫头猫            result.data.forEach(_ => {
195927dbe93S猫头猫                resetMediaItem(_, this.plugin.name);
196927dbe93S猫头猫            });
197927dbe93S猫头猫            return {
198927dbe93S猫头猫                isEnd: result.isEnd ?? true,
199927dbe93S猫头猫                data: result.data,
200927dbe93S猫头猫            };
201927dbe93S猫头猫        }
202927dbe93S猫头猫        return {
203927dbe93S猫头猫            isEnd: true,
204927dbe93S猫头猫            data: [],
205927dbe93S猫头猫        };
206927dbe93S猫头猫    }
207927dbe93S猫头猫
208927dbe93S猫头猫    /** 获取真实源 */
20920e6a092S猫头猫    async getMediaSource(
210927dbe93S猫头猫        musicItem: IMusic.IMusicItemBase,
211abaede57S猫头猫        quality: IMusic.IQualityKey = 'standard',
212927dbe93S猫头猫        retryCount = 1,
213dc160d50S猫头猫        notUpdateCache = false,
214192ae2b0S猫头猫    ): Promise<IPlugin.IMediaSourceResult | null> {
215927dbe93S猫头猫        // 1. 本地搜索 其实直接读mediameta就好了
216927dbe93S猫头猫        const localPath =
2170e4173cdS猫头猫            getInternalData<string>(musicItem, InternalDataType.LOCALPATH) ??
2180e4173cdS猫头猫            getInternalData<string>(
2190e4173cdS猫头猫                LocalMusicSheet.isLocalMusic(musicItem),
2200e4173cdS猫头猫                InternalDataType.LOCALPATH,
2210e4173cdS猫头猫            );
2220e4173cdS猫头猫        if (localPath && (await FileSystem.exists(localPath))) {
2230e4173cdS猫头猫            trace('本地播放', localPath);
224927dbe93S猫头猫            return {
225927dbe93S猫头猫                url: localPath,
226927dbe93S猫头猫            };
227927dbe93S猫头猫        }
2287993f90eS猫头猫        if (musicItem.platform === localPluginPlatform) {
229f5935920S猫头猫            throw new Error('本地音乐不存在');
230f5935920S猫头猫        }
231927dbe93S猫头猫        // 2. 缓存播放
232927dbe93S猫头猫        const mediaCache = Cache.get(musicItem);
233985f8e75S猫头猫        const pluginCacheControl =
234985f8e75S猫头猫            this.plugin.instance.cacheControl ?? 'no-cache';
235cfa0fc07S猫头猫        if (
236cfa0fc07S猫头猫            mediaCache &&
237abaede57S猫头猫            mediaCache?.qualities?.[quality]?.url &&
23848f4b873S猫头猫            (pluginCacheControl === CacheControl.Cache ||
23948f4b873S猫头猫                (pluginCacheControl === CacheControl.NoCache &&
240ef714860S猫头猫                    Network.isOffline()))
241cfa0fc07S猫头猫        ) {
2425276aef9S猫头猫            trace('播放', '缓存播放');
243abaede57S猫头猫            const qualityInfo = mediaCache.qualities[quality];
244927dbe93S猫头猫            return {
245abaede57S猫头猫                url: qualityInfo.url,
246927dbe93S猫头猫                headers: mediaCache.headers,
2474060c00aS猫头猫                userAgent:
2484060c00aS猫头猫                    mediaCache.userAgent ?? mediaCache.headers?.['user-agent'],
249927dbe93S猫头猫            };
250927dbe93S猫头猫        }
251927dbe93S猫头猫        // 3. 插件解析
25220e6a092S猫头猫        if (!this.plugin.instance.getMediaSource) {
253abaede57S猫头猫            return {url: musicItem?.qualities?.[quality]?.url ?? musicItem.url};
254927dbe93S猫头猫        }
255927dbe93S猫头猫        try {
256abaede57S猫头猫            const {url, headers} = (await this.plugin.instance.getMediaSource(
257abaede57S猫头猫                musicItem,
258abaede57S猫头猫                quality,
259abaede57S猫头猫            )) ?? {url: musicItem?.qualities?.[quality]?.url};
260927dbe93S猫头猫            if (!url) {
261a28eac61S猫头猫                throw new Error('NOT RETRY');
262927dbe93S猫头猫            }
2635276aef9S猫头猫            trace('播放', '插件播放');
264927dbe93S猫头猫            const result = {
265927dbe93S猫头猫                url,
266927dbe93S猫头猫                headers,
267927dbe93S猫头猫                userAgent: headers?.['user-agent'],
268cfa0fc07S猫头猫            } as IPlugin.IMediaSourceResult;
269927dbe93S猫头猫
270dc160d50S猫头猫            if (
271dc160d50S猫头猫                pluginCacheControl !== CacheControl.NoStore &&
272dc160d50S猫头猫                !notUpdateCache
273dc160d50S猫头猫            ) {
274abaede57S猫头猫                Cache.update(musicItem, [
275abaede57S猫头猫                    ['headers', result.headers],
276abaede57S猫头猫                    ['userAgent', result.userAgent],
277abaede57S猫头猫                    [`qualities.${quality}.url`, url],
278abaede57S猫头猫                ]);
279752ffc5aS猫头猫            }
280cfa0fc07S猫头猫
281927dbe93S猫头猫            return result;
282927dbe93S猫头猫        } catch (e: any) {
283a28eac61S猫头猫            if (retryCount > 0 && e?.message !== 'NOT RETRY') {
284927dbe93S猫头猫                await delay(150);
285abaede57S猫头猫                return this.getMediaSource(musicItem, quality, --retryCount);
286927dbe93S猫头猫            }
287927dbe93S猫头猫            errorLog('获取真实源失败', e?.message);
288ea6d708fS猫头猫            devLog('error', '获取真实源失败', e, e?.message);
289192ae2b0S猫头猫            return null;
290927dbe93S猫头猫        }
291927dbe93S猫头猫    }
292927dbe93S猫头猫
293927dbe93S猫头猫    /** 获取音乐详情 */
294927dbe93S猫头猫    async getMusicInfo(
295927dbe93S猫头猫        musicItem: ICommon.IMediaBase,
29674d0cf81S猫头猫    ): Promise<Partial<IMusic.IMusicItem> | null> {
297927dbe93S猫头猫        if (!this.plugin.instance.getMusicInfo) {
298d704daedS猫头猫            return null;
299927dbe93S猫头猫        }
30074d0cf81S猫头猫        try {
301927dbe93S猫头猫            return (
302927dbe93S猫头猫                this.plugin.instance.getMusicInfo(
3037993f90eS猫头猫                    resetMediaItem(musicItem, undefined, true),
304d704daedS猫头猫                ) ?? null
305927dbe93S猫头猫            );
306ea6d708fS猫头猫        } catch (e: any) {
307ea6d708fS猫头猫            devLog('error', '获取音乐详情失败', e, e?.message);
308d704daedS猫头猫            return null;
30974d0cf81S猫头猫        }
310927dbe93S猫头猫    }
311927dbe93S猫头猫
312927dbe93S猫头猫    /** 获取歌词 */
313927dbe93S猫头猫    async getLyric(
314927dbe93S猫头猫        musicItem: IMusic.IMusicItemBase,
315927dbe93S猫头猫        from?: IMusic.IMusicItemBase,
316927dbe93S猫头猫    ): Promise<ILyric.ILyricSource | null> {
317927dbe93S猫头猫        // 1.额外存储的meta信息
318927dbe93S猫头猫        const meta = MediaMeta.get(musicItem);
319927dbe93S猫头猫        if (meta && meta.associatedLrc) {
320927dbe93S猫头猫            // 有关联歌词
321927dbe93S猫头猫            if (
322927dbe93S猫头猫                isSameMediaItem(musicItem, from) ||
323927dbe93S猫头猫                isSameMediaItem(meta.associatedLrc, musicItem)
324927dbe93S猫头猫            ) {
325927dbe93S猫头猫                // 形成环路,断开当前的环
326927dbe93S猫头猫                await MediaMeta.update(musicItem, {
327927dbe93S猫头猫                    associatedLrc: undefined,
328927dbe93S猫头猫                });
329927dbe93S猫头猫                // 无歌词
330927dbe93S猫头猫                return null;
331927dbe93S猫头猫            }
332927dbe93S猫头猫            // 获取关联歌词
3337a91f04fS猫头猫            const associatedMeta = MediaMeta.get(meta.associatedLrc) ?? {};
3344060c00aS猫头猫            const result = await this.getLyric(
3357a91f04fS猫头猫                {...meta.associatedLrc, ...associatedMeta},
3364060c00aS猫头猫                from ?? musicItem,
3374060c00aS猫头猫            );
338927dbe93S猫头猫            if (result) {
339927dbe93S猫头猫                // 如果有关联歌词,就返回关联歌词,深度优先
340927dbe93S猫头猫                return result;
341927dbe93S猫头猫            }
342927dbe93S猫头猫        }
343927dbe93S猫头猫        const cache = Cache.get(musicItem);
344927dbe93S猫头猫        let rawLrc = meta?.rawLrc || musicItem.rawLrc || cache?.rawLrc;
345927dbe93S猫头猫        let lrcUrl = meta?.lrc || musicItem.lrc || cache?.lrc;
346927dbe93S猫头猫        // 如果存在文本
347927dbe93S猫头猫        if (rawLrc) {
348927dbe93S猫头猫            return {
349927dbe93S猫头猫                rawLrc,
350927dbe93S猫头猫                lrc: lrcUrl,
351927dbe93S猫头猫            };
352927dbe93S猫头猫        }
353927dbe93S猫头猫        // 2.本地缓存
354927dbe93S猫头猫        const localLrc =
3550e4173cdS猫头猫            meta?.[internalSerializeKey]?.local?.localLrc ||
3560e4173cdS猫头猫            cache?.[internalSerializeKey]?.local?.localLrc;
357927dbe93S猫头猫        if (localLrc && (await exists(localLrc))) {
358927dbe93S猫头猫            rawLrc = await readFile(localLrc, 'utf8');
359927dbe93S猫头猫            return {
360927dbe93S猫头猫                rawLrc,
361927dbe93S猫头猫                lrc: lrcUrl,
362927dbe93S猫头猫            };
363927dbe93S猫头猫        }
364927dbe93S猫头猫        // 3.优先使用url
365927dbe93S猫头猫        if (lrcUrl) {
366927dbe93S猫头猫            try {
367927dbe93S猫头猫                // 需要超时时间 axios timeout 但是没生效
3682a3194f5S猫头猫                rawLrc = (await axios.get(lrcUrl, {timeout: 1500})).data;
369927dbe93S猫头猫                return {
370927dbe93S猫头猫                    rawLrc,
371927dbe93S猫头猫                    lrc: lrcUrl,
372927dbe93S猫头猫                };
373927dbe93S猫头猫            } catch {
374927dbe93S猫头猫                lrcUrl = undefined;
375927dbe93S猫头猫            }
376927dbe93S猫头猫        }
377927dbe93S猫头猫        // 4. 如果地址失效
378927dbe93S猫头猫        if (!lrcUrl) {
379927dbe93S猫头猫            // 插件获得url
380927dbe93S猫头猫            try {
3817a91f04fS猫头猫                let lrcSource;
3827a91f04fS猫头猫                if (from) {
3837a91f04fS猫头猫                    lrcSource = await PluginManager.getByMedia(
3847a91f04fS猫头猫                        musicItem,
3857a91f04fS猫头猫                    )?.instance?.getLyric?.(
386927dbe93S猫头猫                        resetMediaItem(musicItem, undefined, true),
387927dbe93S猫头猫                    );
3887a91f04fS猫头猫                } else {
3897a91f04fS猫头猫                    lrcSource = await this.plugin.instance?.getLyric?.(
3907a91f04fS猫头猫                        resetMediaItem(musicItem, undefined, true),
3917a91f04fS猫头猫                    );
3927a91f04fS猫头猫                }
3937a91f04fS猫头猫
394927dbe93S猫头猫                rawLrc = lrcSource?.rawLrc;
395927dbe93S猫头猫                lrcUrl = lrcSource?.lrc;
396927dbe93S猫头猫            } catch (e: any) {
397927dbe93S猫头猫                trace('插件获取歌词失败', e?.message, 'error');
398ea6d708fS猫头猫                devLog('error', '插件获取歌词失败', e, e?.message);
399927dbe93S猫头猫            }
400927dbe93S猫头猫        }
401927dbe93S猫头猫        // 5. 最后一次请求
402927dbe93S猫头猫        if (rawLrc || lrcUrl) {
403927dbe93S猫头猫            const filename = `${pathConst.lrcCachePath}${nanoid()}.lrc`;
404927dbe93S猫头猫            if (lrcUrl) {
405927dbe93S猫头猫                try {
4062a3194f5S猫头猫                    rawLrc = (await axios.get(lrcUrl, {timeout: 1500})).data;
407927dbe93S猫头猫                } catch {}
408927dbe93S猫头猫            }
409927dbe93S猫头猫            if (rawLrc) {
410927dbe93S猫头猫                await writeFile(filename, rawLrc, 'utf8');
411927dbe93S猫头猫                // 写入缓存
412927dbe93S猫头猫                Cache.update(musicItem, [
4130e4173cdS猫头猫                    [`${internalSerializeKey}.local.localLrc`, filename],
414927dbe93S猫头猫                ]);
415927dbe93S猫头猫                // 如果有meta
416927dbe93S猫头猫                if (meta) {
417927dbe93S猫头猫                    MediaMeta.update(musicItem, [
4180e4173cdS猫头猫                        [`${internalSerializeKey}.local.localLrc`, filename],
419927dbe93S猫头猫                    ]);
420927dbe93S猫头猫                }
421927dbe93S猫头猫                return {
422927dbe93S猫头猫                    rawLrc,
423927dbe93S猫头猫                    lrc: lrcUrl,
424927dbe93S猫头猫                };
425927dbe93S猫头猫            }
426927dbe93S猫头猫        }
4273a6f67b1S猫头猫        // 6. 如果是本地文件
4283a6f67b1S猫头猫        const isDownloaded = LocalMusicSheet.isLocalMusic(musicItem);
4293a6f67b1S猫头猫        if (musicItem.platform !== localPluginPlatform && isDownloaded) {
4303a6f67b1S猫头猫            const res = await localFilePlugin.instance!.getLyric!(isDownloaded);
4313a6f67b1S猫头猫            if (res) {
4323a6f67b1S猫头猫                return res;
4333a6f67b1S猫头猫            }
4343a6f67b1S猫头猫        }
435ea6d708fS猫头猫        devLog('warn', '无歌词');
436927dbe93S猫头猫
437927dbe93S猫头猫        return null;
438927dbe93S猫头猫    }
439927dbe93S猫头猫
440927dbe93S猫头猫    /** 获取歌词文本 */
441927dbe93S猫头猫    async getLyricText(
442927dbe93S猫头猫        musicItem: IMusic.IMusicItem,
443927dbe93S猫头猫    ): Promise<string | undefined> {
444927dbe93S猫头猫        return (await this.getLyric(musicItem))?.rawLrc;
445927dbe93S猫头猫    }
446927dbe93S猫头猫
447927dbe93S猫头猫    /** 获取专辑信息 */
448927dbe93S猫头猫    async getAlbumInfo(
449927dbe93S猫头猫        albumItem: IAlbum.IAlbumItemBase,
450927dbe93S猫头猫    ): Promise<IAlbum.IAlbumItem | null> {
451927dbe93S猫头猫        if (!this.plugin.instance.getAlbumInfo) {
452927dbe93S猫头猫            return {...albumItem, musicList: []};
453927dbe93S猫头猫        }
454927dbe93S猫头猫        try {
455927dbe93S猫头猫            const result = await this.plugin.instance.getAlbumInfo(
456927dbe93S猫头猫                resetMediaItem(albumItem, undefined, true),
457927dbe93S猫头猫            );
4585276aef9S猫头猫            if (!result) {
4595276aef9S猫头猫                throw new Error();
4605276aef9S猫头猫            }
461927dbe93S猫头猫            result?.musicList?.forEach(_ => {
462927dbe93S猫头猫                resetMediaItem(_, this.plugin.name);
463927dbe93S猫头猫            });
4645276aef9S猫头猫
4655276aef9S猫头猫            return {...albumItem, ...result};
4664394410dS猫头猫        } catch (e: any) {
4674394410dS猫头猫            trace('获取专辑信息失败', e?.message);
468ea6d708fS猫头猫            devLog('error', '获取专辑信息失败', e, e?.message);
469ea6d708fS猫头猫
470927dbe93S猫头猫            return {...albumItem, musicList: []};
471927dbe93S猫头猫        }
472927dbe93S猫头猫    }
473927dbe93S猫头猫
474927dbe93S猫头猫    /** 查询作者信息 */
475efb9da24S猫头猫    async getArtistWorks<T extends IArtist.ArtistMediaType>(
476927dbe93S猫头猫        artistItem: IArtist.IArtistItem,
477927dbe93S猫头猫        page: number,
478927dbe93S猫头猫        type: T,
479927dbe93S猫头猫    ): Promise<IPlugin.ISearchResult<T>> {
480efb9da24S猫头猫        if (!this.plugin.instance.getArtistWorks) {
481927dbe93S猫头猫            return {
482927dbe93S猫头猫                isEnd: true,
483927dbe93S猫头猫                data: [],
484927dbe93S猫头猫            };
485927dbe93S猫头猫        }
486927dbe93S猫头猫        try {
487efb9da24S猫头猫            const result = await this.plugin.instance.getArtistWorks(
488927dbe93S猫头猫                artistItem,
489927dbe93S猫头猫                page,
490927dbe93S猫头猫                type,
491927dbe93S猫头猫            );
492927dbe93S猫头猫            if (!result.data) {
493927dbe93S猫头猫                return {
494927dbe93S猫头猫                    isEnd: true,
495927dbe93S猫头猫                    data: [],
496927dbe93S猫头猫                };
497927dbe93S猫头猫            }
498927dbe93S猫头猫            result.data?.forEach(_ => resetMediaItem(_, this.plugin.name));
499927dbe93S猫头猫            return {
500927dbe93S猫头猫                isEnd: result.isEnd ?? true,
501927dbe93S猫头猫                data: result.data,
502927dbe93S猫头猫            };
5034394410dS猫头猫        } catch (e: any) {
5044394410dS猫头猫            trace('查询作者信息失败', e?.message);
505ea6d708fS猫头猫            devLog('error', '查询作者信息失败', e, e?.message);
506ea6d708fS猫头猫
507927dbe93S猫头猫            throw e;
508927dbe93S猫头猫        }
509927dbe93S猫头猫    }
51008380090S猫头猫
51108380090S猫头猫    /** 导入歌单 */
51208380090S猫头猫    async importMusicSheet(urlLike: string): Promise<IMusic.IMusicItem[]> {
51308380090S猫头猫        try {
51408380090S猫头猫            const result =
51508380090S猫头猫                (await this.plugin.instance?.importMusicSheet?.(urlLike)) ?? [];
51608380090S猫头猫            result.forEach(_ => resetMediaItem(_, this.plugin.name));
51708380090S猫头猫            return result;
518ea6d708fS猫头猫        } catch (e: any) {
5190e4173cdS猫头猫            console.log(e);
520ea6d708fS猫头猫            devLog('error', '导入歌单失败', e, e?.message);
521ea6d708fS猫头猫
52208380090S猫头猫            return [];
52308380090S猫头猫        }
52408380090S猫头猫    }
5254d9d3c4cS猫头猫    /** 导入单曲 */
5264d9d3c4cS猫头猫    async importMusicItem(urlLike: string): Promise<IMusic.IMusicItem | null> {
5274d9d3c4cS猫头猫        try {
5284d9d3c4cS猫头猫            const result = await this.plugin.instance?.importMusicItem?.(
5294d9d3c4cS猫头猫                urlLike,
5304d9d3c4cS猫头猫            );
5314d9d3c4cS猫头猫            if (!result) {
5324d9d3c4cS猫头猫                throw new Error();
5334d9d3c4cS猫头猫            }
5344d9d3c4cS猫头猫            resetMediaItem(result, this.plugin.name);
5354d9d3c4cS猫头猫            return result;
536ea6d708fS猫头猫        } catch (e: any) {
537ea6d708fS猫头猫            devLog('error', '导入单曲失败', e, e?.message);
538ea6d708fS猫头猫
5394d9d3c4cS猫头猫            return null;
5404d9d3c4cS猫头猫        }
5414d9d3c4cS猫头猫    }
542*d52aa40eS猫头猫    /** 获取榜单 */
543*d52aa40eS猫头猫    async getTopLists(): Promise<IMusic.IMusicTopListGroupItem[]> {
544*d52aa40eS猫头猫        try {
545*d52aa40eS猫头猫            const result = await this.plugin.instance?.getTopLists?.();
546*d52aa40eS猫头猫            if (!result) {
547*d52aa40eS猫头猫                throw new Error();
548*d52aa40eS猫头猫            }
549*d52aa40eS猫头猫            return result;
550*d52aa40eS猫头猫        } catch (e: any) {
551*d52aa40eS猫头猫            devLog('error', '获取榜单失败', e, e?.message);
552*d52aa40eS猫头猫            return [];
553*d52aa40eS猫头猫        }
554*d52aa40eS猫头猫    }
555*d52aa40eS猫头猫    /** 获取榜单详情 */
556*d52aa40eS猫头猫    async getTopListDetail(
557*d52aa40eS猫头猫        topListItem: IMusic.IMusicTopListItem,
558*d52aa40eS猫头猫    ): Promise<ICommon.WithMusicList<IMusic.IMusicTopListItem>> {
559*d52aa40eS猫头猫        try {
560*d52aa40eS猫头猫            const result = await this.plugin.instance?.getTopListDetail?.(
561*d52aa40eS猫头猫                topListItem,
562*d52aa40eS猫头猫            );
563*d52aa40eS猫头猫            if (!result) {
564*d52aa40eS猫头猫                throw new Error();
565*d52aa40eS猫头猫            }
566*d52aa40eS猫头猫            // resetMediaItem(result, this.plugin.name);
567*d52aa40eS猫头猫            return result;
568*d52aa40eS猫头猫        } catch (e: any) {
569*d52aa40eS猫头猫            devLog('error', '获取榜单详情失败', e, e?.message);
570*d52aa40eS猫头猫            return {
571*d52aa40eS猫头猫                ...topListItem,
572*d52aa40eS猫头猫                musicList: [],
573*d52aa40eS猫头猫            };
574*d52aa40eS猫头猫        }
575*d52aa40eS猫头猫    }
576927dbe93S猫头猫}
577d5bfeb7eS猫头猫//#endregion
5781a5528a0S猫头猫
579927dbe93S猫头猫let plugins: Array<Plugin> = [];
580927dbe93S猫头猫const pluginStateMapper = new StateMapper(() => plugins);
58174d0cf81S猫头猫
582d5bfeb7eS猫头猫//#region 本地音乐插件
58374d0cf81S猫头猫/** 本地插件 */
58474d0cf81S猫头猫const localFilePlugin = new Plugin(function () {
5850e4173cdS猫头猫    return {
586d5bfeb7eS猫头猫        platform: localPluginPlatform,
58774d0cf81S猫头猫        _path: '',
58874d0cf81S猫头猫        async getMusicInfo(musicBase) {
58974d0cf81S猫头猫            const localPath = getInternalData<string>(
59074d0cf81S猫头猫                musicBase,
59174d0cf81S猫头猫                InternalDataType.LOCALPATH,
5920e4173cdS猫头猫            );
59374d0cf81S猫头猫            if (localPath) {
59474d0cf81S猫头猫                const coverImg = await Mp3Util.getMediaCoverImg(localPath);
59574d0cf81S猫头猫                return {
59674d0cf81S猫头猫                    artwork: coverImg,
59774d0cf81S猫头猫                };
59874d0cf81S猫头猫            }
59974d0cf81S猫头猫            return null;
60074d0cf81S猫头猫        },
6017993f90eS猫头猫        async getLyric(musicBase) {
6027993f90eS猫头猫            const localPath = getInternalData<string>(
6037993f90eS猫头猫                musicBase,
6047993f90eS猫头猫                InternalDataType.LOCALPATH,
6057993f90eS猫头猫            );
6063a6f67b1S猫头猫            let rawLrc: string | null = null;
6077993f90eS猫头猫            if (localPath) {
6083a6f67b1S猫头猫                // 读取内嵌歌词
6093a6f67b1S猫头猫                try {
6103a6f67b1S猫头猫                    rawLrc = await Mp3Util.getLyric(localPath);
6113a6f67b1S猫头猫                } catch (e) {
6123a6f67b1S猫头猫                    console.log('e', e);
6137993f90eS猫头猫                }
6143a6f67b1S猫头猫                if (!rawLrc) {
6153a6f67b1S猫头猫                    // 读取配置歌词
6163a6f67b1S猫头猫                    const lastDot = localPath.lastIndexOf('.');
6173a6f67b1S猫头猫                    const lrcPath = localPath.slice(0, lastDot) + '.lrc';
6183a6f67b1S猫头猫
6193a6f67b1S猫头猫                    try {
6203a6f67b1S猫头猫                        if (await exists(lrcPath)) {
6213a6f67b1S猫头猫                            rawLrc = await readFile(lrcPath, 'utf8');
6223a6f67b1S猫头猫                        }
6233a6f67b1S猫头猫                    } catch {}
6243a6f67b1S猫头猫                }
6253a6f67b1S猫头猫            }
6263a6f67b1S猫头猫
6273a6f67b1S猫头猫            return rawLrc
6283a6f67b1S猫头猫                ? {
6293a6f67b1S猫头猫                      rawLrc,
6303a6f67b1S猫头猫                  }
6313a6f67b1S猫头猫                : null;
6327993f90eS猫头猫        },
63374d0cf81S猫头猫    };
63474d0cf81S猫头猫}, '');
6357993f90eS猫头猫localFilePlugin.hash = localPluginHash;
636927dbe93S猫头猫
637d5bfeb7eS猫头猫//#endregion
638d5bfeb7eS猫头猫
639927dbe93S猫头猫async function setup() {
640927dbe93S猫头猫    const _plugins: Array<Plugin> = [];
641927dbe93S猫头猫    try {
642927dbe93S猫头猫        // 加载插件
643927dbe93S猫头猫        const pluginsPaths = await readDir(pathConst.pluginPath);
644927dbe93S猫头猫        for (let i = 0; i < pluginsPaths.length; ++i) {
645927dbe93S猫头猫            const _pluginUrl = pluginsPaths[i];
6461e263108S猫头猫            trace('初始化插件', _pluginUrl);
6471e263108S猫头猫            if (
6481e263108S猫头猫                _pluginUrl.isFile() &&
6491e263108S猫头猫                (_pluginUrl.name?.endsWith?.('.js') ||
6501e263108S猫头猫                    _pluginUrl.path?.endsWith?.('.js'))
6511e263108S猫头猫            ) {
652927dbe93S猫头猫                const funcCode = await readFile(_pluginUrl.path, 'utf8');
653927dbe93S猫头猫                const plugin = new Plugin(funcCode, _pluginUrl.path);
6544060c00aS猫头猫                const _pluginIndex = _plugins.findIndex(
6554060c00aS猫头猫                    p => p.hash === plugin.hash,
6564060c00aS猫头猫                );
657927dbe93S猫头猫                if (_pluginIndex !== -1) {
658927dbe93S猫头猫                    // 重复插件,直接忽略
659927dbe93S猫头猫                    return;
660927dbe93S猫头猫                }
661927dbe93S猫头猫                plugin.hash !== '' && _plugins.push(plugin);
662927dbe93S猫头猫            }
663927dbe93S猫头猫        }
664927dbe93S猫头猫
665927dbe93S猫头猫        plugins = _plugins;
666927dbe93S猫头猫        pluginStateMapper.notify();
667e08d37a3S猫头猫        /** 初始化meta信息 */
668e08d37a3S猫头猫        PluginMeta.setupMeta(plugins.map(_ => _.name));
669927dbe93S猫头猫    } catch (e: any) {
6704060c00aS猫头猫        ToastAndroid.show(
6714060c00aS猫头猫            `插件初始化失败:${e?.message ?? e}`,
6724060c00aS猫头猫            ToastAndroid.LONG,
6734060c00aS猫头猫        );
6741a5528a0S猫头猫        errorLog('插件初始化失败', e?.message);
675927dbe93S猫头猫        throw e;
676927dbe93S猫头猫    }
677927dbe93S猫头猫}
678927dbe93S猫头猫
679927dbe93S猫头猫// 安装插件
680927dbe93S猫头猫async function installPlugin(pluginPath: string) {
68122c09412S猫头猫    // if (pluginPath.endsWith('.js')) {
682927dbe93S猫头猫    const funcCode = await readFile(pluginPath, 'utf8');
683927dbe93S猫头猫    const plugin = new Plugin(funcCode, pluginPath);
684927dbe93S猫头猫    const _pluginIndex = plugins.findIndex(p => p.hash === plugin.hash);
685927dbe93S猫头猫    if (_pluginIndex !== -1) {
6864d9d3c4cS猫头猫        throw new Error('插件已安装');
687927dbe93S猫头猫    }
688927dbe93S猫头猫    if (plugin.hash !== '') {
689927dbe93S猫头猫        const fn = nanoid();
690927dbe93S猫头猫        const _pluginPath = `${pathConst.pluginPath}${fn}.js`;
691927dbe93S猫头猫        await copyFile(pluginPath, _pluginPath);
692927dbe93S猫头猫        plugin.path = _pluginPath;
693927dbe93S猫头猫        plugins = plugins.concat(plugin);
694927dbe93S猫头猫        pluginStateMapper.notify();
6954d9d3c4cS猫头猫        return;
696927dbe93S猫头猫    }
6974d9d3c4cS猫头猫    throw new Error('插件无法解析');
69822c09412S猫头猫    // }
69922c09412S猫头猫    // throw new Error('插件不存在');
700927dbe93S猫头猫}
701927dbe93S猫头猫
70258992c6bS猫头猫async function installPluginFromUrl(url: string) {
70358992c6bS猫头猫    try {
70458992c6bS猫头猫        const funcCode = (await axios.get(url)).data;
70558992c6bS猫头猫        if (funcCode) {
70658992c6bS猫头猫            const plugin = new Plugin(funcCode, '');
70758992c6bS猫头猫            const _pluginIndex = plugins.findIndex(p => p.hash === plugin.hash);
70858992c6bS猫头猫            if (_pluginIndex !== -1) {
7098b7ddca8S猫头猫                // 静默忽略
7108b7ddca8S猫头猫                return;
71158992c6bS猫头猫            }
71225c1bd29S猫头猫            const oldVersionPlugin = plugins.find(p => p.name === plugin.name);
71325c1bd29S猫头猫            if (oldVersionPlugin) {
71425c1bd29S猫头猫                if (
71525c1bd29S猫头猫                    compare(
71625c1bd29S猫头猫                        oldVersionPlugin.instance.version ?? '',
71725c1bd29S猫头猫                        plugin.instance.version ?? '',
71825c1bd29S猫头猫                        '>',
71925c1bd29S猫头猫                    )
72025c1bd29S猫头猫                ) {
72125c1bd29S猫头猫                    throw new Error('已安装更新版本的插件');
72225c1bd29S猫头猫                }
72325c1bd29S猫头猫            }
72425c1bd29S猫头猫
72558992c6bS猫头猫            if (plugin.hash !== '') {
72658992c6bS猫头猫                const fn = nanoid();
72758992c6bS猫头猫                const _pluginPath = `${pathConst.pluginPath}${fn}.js`;
72858992c6bS猫头猫                await writeFile(_pluginPath, funcCode, 'utf8');
72958992c6bS猫头猫                plugin.path = _pluginPath;
73058992c6bS猫头猫                plugins = plugins.concat(plugin);
73125c1bd29S猫头猫                if (oldVersionPlugin) {
73225c1bd29S猫头猫                    plugins = plugins.filter(
73325c1bd29S猫头猫                        _ => _.hash !== oldVersionPlugin.hash,
73425c1bd29S猫头猫                    );
73525c1bd29S猫头猫                    try {
73625c1bd29S猫头猫                        await unlink(oldVersionPlugin.path);
73725c1bd29S猫头猫                    } catch {}
73825c1bd29S猫头猫                }
73958992c6bS猫头猫                pluginStateMapper.notify();
74058992c6bS猫头猫                return;
74158992c6bS猫头猫            }
74274acbfc0S猫头猫            throw new Error('插件无法解析!');
74358992c6bS猫头猫        }
74425c1bd29S猫头猫    } catch (e: any) {
745ea6d708fS猫头猫        devLog('error', 'URL安装插件失败', e, e?.message);
74658992c6bS猫头猫        errorLog('URL安装插件失败', e);
74725c1bd29S猫头猫        throw new Error(e?.message ?? '');
74858992c6bS猫头猫    }
74958992c6bS猫头猫}
75058992c6bS猫头猫
751927dbe93S猫头猫/** 卸载插件 */
752927dbe93S猫头猫async function uninstallPlugin(hash: string) {
753927dbe93S猫头猫    const targetIndex = plugins.findIndex(_ => _.hash === hash);
754927dbe93S猫头猫    if (targetIndex !== -1) {
755927dbe93S猫头猫        try {
75624e5e74aS猫头猫            const pluginName = plugins[targetIndex].name;
757927dbe93S猫头猫            await unlink(plugins[targetIndex].path);
758927dbe93S猫头猫            plugins = plugins.filter(_ => _.hash !== hash);
759927dbe93S猫头猫            pluginStateMapper.notify();
76024e5e74aS猫头猫            if (plugins.every(_ => _.name !== pluginName)) {
76124e5e74aS猫头猫                await MediaMeta.removePlugin(pluginName);
76224e5e74aS猫头猫            }
763927dbe93S猫头猫        } catch {}
764927dbe93S猫头猫    }
765927dbe93S猫头猫}
766927dbe93S猫头猫
76708882a77S猫头猫async function uninstallAllPlugins() {
76808882a77S猫头猫    await Promise.all(
76908882a77S猫头猫        plugins.map(async plugin => {
77008882a77S猫头猫            try {
77108882a77S猫头猫                const pluginName = plugin.name;
77208882a77S猫头猫                await unlink(plugin.path);
77308882a77S猫头猫                await MediaMeta.removePlugin(pluginName);
77408882a77S猫头猫            } catch (e) {}
77508882a77S猫头猫        }),
77608882a77S猫头猫    );
77708882a77S猫头猫    plugins = [];
77808882a77S猫头猫    pluginStateMapper.notify();
779e08d37a3S猫头猫
780e08d37a3S猫头猫    /** 清除空余文件,异步做就可以了 */
781e08d37a3S猫头猫    readDir(pathConst.pluginPath)
782e08d37a3S猫头猫        .then(fns => {
783e08d37a3S猫头猫            fns.forEach(fn => {
784e08d37a3S猫头猫                unlink(fn.path).catch(emptyFunction);
785e08d37a3S猫头猫            });
786e08d37a3S猫头猫        })
787e08d37a3S猫头猫        .catch(emptyFunction);
78808882a77S猫头猫}
78908882a77S猫头猫
79025c1bd29S猫头猫async function updatePlugin(plugin: Plugin) {
79125c1bd29S猫头猫    const updateUrl = plugin.instance.srcUrl;
79225c1bd29S猫头猫    if (!updateUrl) {
79325c1bd29S猫头猫        throw new Error('没有更新源');
79425c1bd29S猫头猫    }
79525c1bd29S猫头猫    try {
79625c1bd29S猫头猫        await installPluginFromUrl(updateUrl);
79725c1bd29S猫头猫    } catch (e: any) {
79825c1bd29S猫头猫        if (e.message === '插件已安装') {
79925c1bd29S猫头猫            throw new Error('当前已是最新版本');
80025c1bd29S猫头猫        } else {
80125c1bd29S猫头猫            throw e;
80225c1bd29S猫头猫        }
80325c1bd29S猫头猫    }
80425c1bd29S猫头猫}
80525c1bd29S猫头猫
806927dbe93S猫头猫function getByMedia(mediaItem: ICommon.IMediaBase) {
8072c595535S猫头猫    return getByName(mediaItem?.platform);
808927dbe93S猫头猫}
809927dbe93S猫头猫
810927dbe93S猫头猫function getByHash(hash: string) {
8117993f90eS猫头猫    return hash === localPluginHash
8127993f90eS猫头猫        ? localFilePlugin
8137993f90eS猫头猫        : plugins.find(_ => _.hash === hash);
814927dbe93S猫头猫}
815927dbe93S猫头猫
816927dbe93S猫头猫function getByName(name: string) {
8177993f90eS猫头猫    return name === localPluginPlatform
8180e4173cdS猫头猫        ? localFilePlugin
8190e4173cdS猫头猫        : plugins.find(_ => _.name === name);
820927dbe93S猫头猫}
821927dbe93S猫头猫
822927dbe93S猫头猫function getValidPlugins() {
823927dbe93S猫头猫    return plugins.filter(_ => _.state === 'enabled');
824927dbe93S猫头猫}
825927dbe93S猫头猫
826efb9da24S猫头猫function getSearchablePlugins() {
827efb9da24S猫头猫    return plugins.filter(_ => _.state === 'enabled' && _.instance.search);
828efb9da24S猫头猫}
829efb9da24S猫头猫
830e08d37a3S猫头猫function getSortedSearchablePlugins() {
831e08d37a3S猫头猫    return getSearchablePlugins().sort((a, b) =>
832e08d37a3S猫头猫        (PluginMeta.getPluginMeta(a).order ?? Infinity) -
833e08d37a3S猫头猫            (PluginMeta.getPluginMeta(b).order ?? Infinity) <
834e08d37a3S猫头猫        0
835e08d37a3S猫头猫            ? -1
836e08d37a3S猫头猫            : 1,
837e08d37a3S猫头猫    );
838e08d37a3S猫头猫}
839e08d37a3S猫头猫
840e08d37a3S猫头猫function useSortedPlugins() {
841e08d37a3S猫头猫    const _plugins = pluginStateMapper.useMappedState();
842e08d37a3S猫头猫    const _pluginMetaAll = PluginMeta.usePluginMetaAll();
843e08d37a3S猫头猫
84434588741S猫头猫    const [sortedPlugins, setSortedPlugins] = useState(
84534588741S猫头猫        [..._plugins].sort((a, b) =>
846e08d37a3S猫头猫            (_pluginMetaAll[a.name]?.order ?? Infinity) -
847e08d37a3S猫头猫                (_pluginMetaAll[b.name]?.order ?? Infinity) <
848e08d37a3S猫头猫            0
849e08d37a3S猫头猫                ? -1
850e08d37a3S猫头猫                : 1,
85134588741S猫头猫        ),
852e08d37a3S猫头猫    );
85334588741S猫头猫
85434588741S猫头猫    useEffect(() => {
855d4cd40d8S猫头猫        InteractionManager.runAfterInteractions(() => {
85634588741S猫头猫            setSortedPlugins(
85734588741S猫头猫                [..._plugins].sort((a, b) =>
85834588741S猫头猫                    (_pluginMetaAll[a.name]?.order ?? Infinity) -
85934588741S猫头猫                        (_pluginMetaAll[b.name]?.order ?? Infinity) <
86034588741S猫头猫                    0
86134588741S猫头猫                        ? -1
86234588741S猫头猫                        : 1,
86334588741S猫头猫                ),
86434588741S猫头猫            );
865d4cd40d8S猫头猫        });
86634588741S猫头猫    }, [_plugins, _pluginMetaAll]);
86734588741S猫头猫
86834588741S猫头猫    return sortedPlugins;
869e08d37a3S猫头猫}
870e08d37a3S猫头猫
871927dbe93S猫头猫const PluginManager = {
872927dbe93S猫头猫    setup,
873927dbe93S猫头猫    installPlugin,
87458992c6bS猫头猫    installPluginFromUrl,
87525c1bd29S猫头猫    updatePlugin,
876927dbe93S猫头猫    uninstallPlugin,
877927dbe93S猫头猫    getByMedia,
878927dbe93S猫头猫    getByHash,
879927dbe93S猫头猫    getByName,
880927dbe93S猫头猫    getValidPlugins,
881efb9da24S猫头猫    getSearchablePlugins,
882e08d37a3S猫头猫    getSortedSearchablePlugins,
8835276aef9S猫头猫    usePlugins: pluginStateMapper.useMappedState,
884e08d37a3S猫头猫    useSortedPlugins,
88508882a77S猫头猫    uninstallAllPlugins,
8865276aef9S猫头猫};
887927dbe93S猫头猫
888927dbe93S猫头猫export default PluginManager;
889