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猫头猫 4761aca335S猫头猫axios.defaults.timeout = 2000; 48927dbe93S猫头猫 49927dbe93S猫头猫const sha256 = CryptoJs.SHA256; 50927dbe93S猫头猫 51cfa0fc07S猫头猫export enum PluginStateCode { 52927dbe93S猫头猫 /** 版本不匹配 */ 53927dbe93S猫头猫 VersionNotMatch = 'VERSION NOT MATCH', 54927dbe93S猫头猫 /** 无法解析 */ 55927dbe93S猫头猫 CannotParse = 'CANNOT PARSE', 56927dbe93S猫头猫} 57927dbe93S猫头猫 589c34d637S猫头猫const packages: Record<string, any> = { 599c34d637S猫头猫 cheerio, 609c34d637S猫头猫 'crypto-js': CryptoJs, 619c34d637S猫头猫 axios, 629c34d637S猫头猫 dayjs, 639c34d637S猫头猫 'big-integer': bigInt, 649c34d637S猫头猫 qs, 659c34d637S猫头猫 he, 663b3d6357S猫头猫 '@react-native-cookies/cookies': CookieManager, 679c34d637S猫头猫}; 689c34d637S猫头猫 69b43683eaS猫头猫const _require = (packageName: string) => { 70b43683eaS猫头猫 let pkg = packages[packageName]; 71b43683eaS猫头猫 pkg.default = pkg; 72b43683eaS猫头猫 return pkg; 73b43683eaS猫头猫}; 749c34d637S猫头猫 7553f8cd8eS猫头猫const _consoleBind = function ( 7653f8cd8eS猫头猫 method: 'log' | 'error' | 'info' | 'warn', 7753f8cd8eS猫头猫 ...args: any 7853f8cd8eS猫头猫) { 7953f8cd8eS猫头猫 const fn = console[method]; 8053f8cd8eS猫头猫 if (fn) { 8153f8cd8eS猫头猫 fn(...args); 8253f8cd8eS猫头猫 devLog(method, ...args); 8353f8cd8eS猫头猫 } 8453f8cd8eS猫头猫}; 8553f8cd8eS猫头猫 8653f8cd8eS猫头猫const _console = { 8753f8cd8eS猫头猫 log: _consoleBind.bind(null, 'log'), 8853f8cd8eS猫头猫 warn: _consoleBind.bind(null, 'warn'), 8953f8cd8eS猫头猫 info: _consoleBind.bind(null, 'info'), 9053f8cd8eS猫头猫 error: _consoleBind.bind(null, 'error'), 9153f8cd8eS猫头猫}; 9253f8cd8eS猫头猫 93d5bfeb7eS猫头猫//#region 插件类 94927dbe93S猫头猫export class Plugin { 95927dbe93S猫头猫 /** 插件名 */ 96927dbe93S猫头猫 public name: string; 97927dbe93S猫头猫 /** 插件的hash,作为唯一id */ 98927dbe93S猫头猫 public hash: string; 99927dbe93S猫头猫 /** 插件状态:激活、关闭、错误 */ 100927dbe93S猫头猫 public state: 'enabled' | 'disabled' | 'error'; 101927dbe93S猫头猫 /** 插件支持的搜索类型 */ 102927dbe93S猫头猫 public supportedSearchType?: string; 103927dbe93S猫头猫 /** 插件状态信息 */ 104927dbe93S猫头猫 public stateCode?: PluginStateCode; 105927dbe93S猫头猫 /** 插件的实例 */ 106927dbe93S猫头猫 public instance: IPlugin.IPluginInstance; 107927dbe93S猫头猫 /** 插件路径 */ 108927dbe93S猫头猫 public path: string; 109927dbe93S猫头猫 /** 插件方法 */ 110927dbe93S猫头猫 public methods: PluginMethods; 111e1f817d7S猫头猫 /** TODO 用户输入 */ 112d5bfeb7eS猫头猫 public userEnv?: Record<string, string>; 113927dbe93S猫头猫 11474d0cf81S猫头猫 constructor( 11574d0cf81S猫头猫 funcCode: string | (() => IPlugin.IPluginInstance), 11674d0cf81S猫头猫 pluginPath: string, 11774d0cf81S猫头猫 ) { 118927dbe93S猫头猫 this.state = 'enabled'; 119927dbe93S猫头猫 let _instance: IPlugin.IPluginInstance; 1203b3d6357S猫头猫 const _module: any = {exports: {}}; 121927dbe93S猫头猫 try { 12274d0cf81S猫头猫 if (typeof funcCode === 'string') { 1234060c00aS猫头猫 // eslint-disable-next-line no-new-func 124927dbe93S猫头猫 _instance = Function(` 125927dbe93S猫头猫 'use strict'; 12653f8cd8eS猫头猫 return function(require, __musicfree_require, module, exports, console) { 1279c34d637S猫头猫 ${funcCode} 128927dbe93S猫头猫 } 12953f8cd8eS猫头猫 `)()(_require, _require, _module, _module.exports, _console); 1303b3d6357S猫头猫 if (_module.exports.default) { 1313b3d6357S猫头猫 _instance = _module.exports 1323b3d6357S猫头猫 .default as IPlugin.IPluginInstance; 1333b3d6357S猫头猫 } else { 1349c34d637S猫头猫 _instance = _module.exports as IPlugin.IPluginInstance; 1353b3d6357S猫头猫 } 13674d0cf81S猫头猫 } else { 13774d0cf81S猫头猫 _instance = funcCode(); 13874d0cf81S猫头猫 } 139927dbe93S猫头猫 this.checkValid(_instance); 140927dbe93S猫头猫 } catch (e: any) { 141b43683eaS猫头猫 console.log(e); 142927dbe93S猫头猫 this.state = 'error'; 143927dbe93S猫头猫 this.stateCode = PluginStateCode.CannotParse; 144927dbe93S猫头猫 if (e?.stateCode) { 145927dbe93S猫头猫 this.stateCode = e.stateCode; 146927dbe93S猫头猫 } 147927dbe93S猫头猫 errorLog(`${pluginPath}插件无法解析 `, { 148927dbe93S猫头猫 stateCode: this.stateCode, 149927dbe93S猫头猫 message: e?.message, 150927dbe93S猫头猫 stack: e?.stack, 151927dbe93S猫头猫 }); 152927dbe93S猫头猫 _instance = e?.instance ?? { 153927dbe93S猫头猫 _path: '', 154927dbe93S猫头猫 platform: '', 155927dbe93S猫头猫 appVersion: '', 15620e6a092S猫头猫 async getMediaSource() { 157927dbe93S猫头猫 return null; 158927dbe93S猫头猫 }, 159927dbe93S猫头猫 async search() { 160927dbe93S猫头猫 return {}; 161927dbe93S猫头猫 }, 162927dbe93S猫头猫 async getAlbumInfo() { 163927dbe93S猫头猫 return null; 164927dbe93S猫头猫 }, 165927dbe93S猫头猫 }; 166927dbe93S猫头猫 } 167927dbe93S猫头猫 this.instance = _instance; 168927dbe93S猫头猫 this.path = pluginPath; 169927dbe93S猫头猫 this.name = _instance.platform; 170ab8941d9S猫头猫 if ( 171ab8941d9S猫头猫 this.instance.platform === '' || 172ab8941d9S猫头猫 this.instance.platform === undefined 173ab8941d9S猫头猫 ) { 174927dbe93S猫头猫 this.hash = ''; 175927dbe93S猫头猫 } else { 17674d0cf81S猫头猫 if (typeof funcCode === 'string') { 177927dbe93S猫头猫 this.hash = sha256(funcCode).toString(); 17874d0cf81S猫头猫 } else { 17974d0cf81S猫头猫 this.hash = sha256(funcCode.toString()).toString(); 18074d0cf81S猫头猫 } 181927dbe93S猫头猫 } 182927dbe93S猫头猫 183927dbe93S猫头猫 // 放在最后 184927dbe93S猫头猫 this.methods = new PluginMethods(this); 185927dbe93S猫头猫 } 186927dbe93S猫头猫 187927dbe93S猫头猫 private checkValid(_instance: IPlugin.IPluginInstance) { 188927dbe93S猫头猫 /** 版本号校验 */ 189927dbe93S猫头猫 if ( 190927dbe93S猫头猫 _instance.appVersion && 191927dbe93S猫头猫 !satisfies(DeviceInfo.getVersion(), _instance.appVersion) 192927dbe93S猫头猫 ) { 193927dbe93S猫头猫 throw { 194927dbe93S猫头猫 instance: _instance, 195927dbe93S猫头猫 stateCode: PluginStateCode.VersionNotMatch, 196927dbe93S猫头猫 }; 197927dbe93S猫头猫 } 198927dbe93S猫头猫 return true; 199927dbe93S猫头猫 } 200927dbe93S猫头猫} 201d5bfeb7eS猫头猫//#endregion 202927dbe93S猫头猫 203d5bfeb7eS猫头猫//#region 基于插件类封装的方法,供给APP侧直接调用 204927dbe93S猫头猫/** 有缓存等信息 */ 205927dbe93S猫头猫class PluginMethods implements IPlugin.IPluginInstanceMethods { 206927dbe93S猫头猫 private plugin; 207927dbe93S猫头猫 constructor(plugin: Plugin) { 208927dbe93S猫头猫 this.plugin = plugin; 209927dbe93S猫头猫 } 210927dbe93S猫头猫 /** 搜索 */ 211927dbe93S猫头猫 async search<T extends ICommon.SupportMediaType>( 212927dbe93S猫头猫 query: string, 213927dbe93S猫头猫 page: number, 214927dbe93S猫头猫 type: T, 215927dbe93S猫头猫 ): Promise<IPlugin.ISearchResult<T>> { 216927dbe93S猫头猫 if (!this.plugin.instance.search) { 217927dbe93S猫头猫 return { 218927dbe93S猫头猫 isEnd: true, 219927dbe93S猫头猫 data: [], 220927dbe93S猫头猫 }; 221927dbe93S猫头猫 } 222927dbe93S猫头猫 2234060c00aS猫头猫 const result = 2244060c00aS猫头猫 (await this.plugin.instance.search(query, page, type)) ?? {}; 225927dbe93S猫头猫 if (Array.isArray(result.data)) { 226927dbe93S猫头猫 result.data.forEach(_ => { 227927dbe93S猫头猫 resetMediaItem(_, this.plugin.name); 228927dbe93S猫头猫 }); 229927dbe93S猫头猫 return { 230927dbe93S猫头猫 isEnd: result.isEnd ?? true, 231927dbe93S猫头猫 data: result.data, 232927dbe93S猫头猫 }; 233927dbe93S猫头猫 } 234927dbe93S猫头猫 return { 235927dbe93S猫头猫 isEnd: true, 236927dbe93S猫头猫 data: [], 237927dbe93S猫头猫 }; 238927dbe93S猫头猫 } 239927dbe93S猫头猫 240927dbe93S猫头猫 /** 获取真实源 */ 24120e6a092S猫头猫 async getMediaSource( 242927dbe93S猫头猫 musicItem: IMusic.IMusicItemBase, 243abaede57S猫头猫 quality: IMusic.IQualityKey = 'standard', 244927dbe93S猫头猫 retryCount = 1, 245dc160d50S猫头猫 notUpdateCache = false, 246192ae2b0S猫头猫 ): Promise<IPlugin.IMediaSourceResult | null> { 247927dbe93S猫头猫 // 1. 本地搜索 其实直接读mediameta就好了 248927dbe93S猫头猫 const localPath = 2490e4173cdS猫头猫 getInternalData<string>(musicItem, InternalDataType.LOCALPATH) ?? 2500e4173cdS猫头猫 getInternalData<string>( 2510e4173cdS猫头猫 LocalMusicSheet.isLocalMusic(musicItem), 2520e4173cdS猫头猫 InternalDataType.LOCALPATH, 2530e4173cdS猫头猫 ); 2540e4173cdS猫头猫 if (localPath && (await FileSystem.exists(localPath))) { 2550e4173cdS猫头猫 trace('本地播放', localPath); 256927dbe93S猫头猫 return { 257927dbe93S猫头猫 url: localPath, 258927dbe93S猫头猫 }; 259927dbe93S猫头猫 } 2607993f90eS猫头猫 if (musicItem.platform === localPluginPlatform) { 261f5935920S猫头猫 throw new Error('本地音乐不存在'); 262f5935920S猫头猫 } 263927dbe93S猫头猫 // 2. 缓存播放 264927dbe93S猫头猫 const mediaCache = Cache.get(musicItem); 265985f8e75S猫头猫 const pluginCacheControl = 266985f8e75S猫头猫 this.plugin.instance.cacheControl ?? 'no-cache'; 267cfa0fc07S猫头猫 if ( 268cfa0fc07S猫头猫 mediaCache && 269abaede57S猫头猫 mediaCache?.qualities?.[quality]?.url && 27048f4b873S猫头猫 (pluginCacheControl === CacheControl.Cache || 27148f4b873S猫头猫 (pluginCacheControl === CacheControl.NoCache && 272ef714860S猫头猫 Network.isOffline())) 273cfa0fc07S猫头猫 ) { 2745276aef9S猫头猫 trace('播放', '缓存播放'); 275abaede57S猫头猫 const qualityInfo = mediaCache.qualities[quality]; 276927dbe93S猫头猫 return { 277abaede57S猫头猫 url: qualityInfo.url, 278927dbe93S猫头猫 headers: mediaCache.headers, 2794060c00aS猫头猫 userAgent: 2804060c00aS猫头猫 mediaCache.userAgent ?? mediaCache.headers?.['user-agent'], 281927dbe93S猫头猫 }; 282927dbe93S猫头猫 } 283927dbe93S猫头猫 // 3. 插件解析 28420e6a092S猫头猫 if (!this.plugin.instance.getMediaSource) { 285abaede57S猫头猫 return {url: musicItem?.qualities?.[quality]?.url ?? musicItem.url}; 286927dbe93S猫头猫 } 287927dbe93S猫头猫 try { 288abaede57S猫头猫 const {url, headers} = (await this.plugin.instance.getMediaSource( 289abaede57S猫头猫 musicItem, 290abaede57S猫头猫 quality, 291abaede57S猫头猫 )) ?? {url: musicItem?.qualities?.[quality]?.url}; 292927dbe93S猫头猫 if (!url) { 293a28eac61S猫头猫 throw new Error('NOT RETRY'); 294927dbe93S猫头猫 } 2955276aef9S猫头猫 trace('播放', '插件播放'); 296927dbe93S猫头猫 const result = { 297927dbe93S猫头猫 url, 298927dbe93S猫头猫 headers, 299927dbe93S猫头猫 userAgent: headers?.['user-agent'], 300cfa0fc07S猫头猫 } as IPlugin.IMediaSourceResult; 301927dbe93S猫头猫 302dc160d50S猫头猫 if ( 303dc160d50S猫头猫 pluginCacheControl !== CacheControl.NoStore && 304dc160d50S猫头猫 !notUpdateCache 305dc160d50S猫头猫 ) { 306abaede57S猫头猫 Cache.update(musicItem, [ 307abaede57S猫头猫 ['headers', result.headers], 308abaede57S猫头猫 ['userAgent', result.userAgent], 309abaede57S猫头猫 [`qualities.${quality}.url`, url], 310abaede57S猫头猫 ]); 311752ffc5aS猫头猫 } 312cfa0fc07S猫头猫 313927dbe93S猫头猫 return result; 314927dbe93S猫头猫 } catch (e: any) { 315a28eac61S猫头猫 if (retryCount > 0 && e?.message !== 'NOT RETRY') { 316927dbe93S猫头猫 await delay(150); 317abaede57S猫头猫 return this.getMediaSource(musicItem, quality, --retryCount); 318927dbe93S猫头猫 } 319927dbe93S猫头猫 errorLog('获取真实源失败', e?.message); 320ea6d708fS猫头猫 devLog('error', '获取真实源失败', e, e?.message); 321192ae2b0S猫头猫 return null; 322927dbe93S猫头猫 } 323927dbe93S猫头猫 } 324927dbe93S猫头猫 325927dbe93S猫头猫 /** 获取音乐详情 */ 326927dbe93S猫头猫 async getMusicInfo( 327927dbe93S猫头猫 musicItem: ICommon.IMediaBase, 32874d0cf81S猫头猫 ): Promise<Partial<IMusic.IMusicItem> | null> { 329927dbe93S猫头猫 if (!this.plugin.instance.getMusicInfo) { 330d704daedS猫头猫 return null; 331927dbe93S猫头猫 } 33274d0cf81S猫头猫 try { 333927dbe93S猫头猫 return ( 334927dbe93S猫头猫 this.plugin.instance.getMusicInfo( 3357993f90eS猫头猫 resetMediaItem(musicItem, undefined, true), 336d704daedS猫头猫 ) ?? null 337927dbe93S猫头猫 ); 338ea6d708fS猫头猫 } catch (e: any) { 339ea6d708fS猫头猫 devLog('error', '获取音乐详情失败', e, e?.message); 340d704daedS猫头猫 return null; 34174d0cf81S猫头猫 } 342927dbe93S猫头猫 } 343927dbe93S猫头猫 344927dbe93S猫头猫 /** 获取歌词 */ 345927dbe93S猫头猫 async getLyric( 346927dbe93S猫头猫 musicItem: IMusic.IMusicItemBase, 347927dbe93S猫头猫 from?: IMusic.IMusicItemBase, 348927dbe93S猫头猫 ): Promise<ILyric.ILyricSource | null> { 349927dbe93S猫头猫 // 1.额外存储的meta信息 350927dbe93S猫头猫 const meta = MediaMeta.get(musicItem); 351927dbe93S猫头猫 if (meta && meta.associatedLrc) { 352927dbe93S猫头猫 // 有关联歌词 353927dbe93S猫头猫 if ( 354927dbe93S猫头猫 isSameMediaItem(musicItem, from) || 355927dbe93S猫头猫 isSameMediaItem(meta.associatedLrc, musicItem) 356927dbe93S猫头猫 ) { 357927dbe93S猫头猫 // 形成环路,断开当前的环 358927dbe93S猫头猫 await MediaMeta.update(musicItem, { 359927dbe93S猫头猫 associatedLrc: undefined, 360927dbe93S猫头猫 }); 361927dbe93S猫头猫 // 无歌词 362927dbe93S猫头猫 return null; 363927dbe93S猫头猫 } 364927dbe93S猫头猫 // 获取关联歌词 3657a91f04fS猫头猫 const associatedMeta = MediaMeta.get(meta.associatedLrc) ?? {}; 3664060c00aS猫头猫 const result = await this.getLyric( 3677a91f04fS猫头猫 {...meta.associatedLrc, ...associatedMeta}, 3684060c00aS猫头猫 from ?? musicItem, 3694060c00aS猫头猫 ); 370927dbe93S猫头猫 if (result) { 371927dbe93S猫头猫 // 如果有关联歌词,就返回关联歌词,深度优先 372927dbe93S猫头猫 return result; 373927dbe93S猫头猫 } 374927dbe93S猫头猫 } 375927dbe93S猫头猫 const cache = Cache.get(musicItem); 376927dbe93S猫头猫 let rawLrc = meta?.rawLrc || musicItem.rawLrc || cache?.rawLrc; 377927dbe93S猫头猫 let lrcUrl = meta?.lrc || musicItem.lrc || cache?.lrc; 378927dbe93S猫头猫 // 如果存在文本 379927dbe93S猫头猫 if (rawLrc) { 380927dbe93S猫头猫 return { 381927dbe93S猫头猫 rawLrc, 382927dbe93S猫头猫 lrc: lrcUrl, 383927dbe93S猫头猫 }; 384927dbe93S猫头猫 } 385927dbe93S猫头猫 // 2.本地缓存 386927dbe93S猫头猫 const localLrc = 3870e4173cdS猫头猫 meta?.[internalSerializeKey]?.local?.localLrc || 3880e4173cdS猫头猫 cache?.[internalSerializeKey]?.local?.localLrc; 389927dbe93S猫头猫 if (localLrc && (await exists(localLrc))) { 390927dbe93S猫头猫 rawLrc = await readFile(localLrc, 'utf8'); 391927dbe93S猫头猫 return { 392927dbe93S猫头猫 rawLrc, 393927dbe93S猫头猫 lrc: lrcUrl, 394927dbe93S猫头猫 }; 395927dbe93S猫头猫 } 396927dbe93S猫头猫 // 3.优先使用url 397927dbe93S猫头猫 if (lrcUrl) { 398927dbe93S猫头猫 try { 399927dbe93S猫头猫 // 需要超时时间 axios timeout 但是没生效 40061aca335S猫头猫 rawLrc = (await axios.get(lrcUrl, {timeout: 2000})).data; 401927dbe93S猫头猫 return { 402927dbe93S猫头猫 rawLrc, 403927dbe93S猫头猫 lrc: lrcUrl, 404927dbe93S猫头猫 }; 405927dbe93S猫头猫 } catch { 406927dbe93S猫头猫 lrcUrl = undefined; 407927dbe93S猫头猫 } 408927dbe93S猫头猫 } 409927dbe93S猫头猫 // 4. 如果地址失效 410927dbe93S猫头猫 if (!lrcUrl) { 411927dbe93S猫头猫 // 插件获得url 412927dbe93S猫头猫 try { 4137a91f04fS猫头猫 let lrcSource; 4147a91f04fS猫头猫 if (from) { 4157a91f04fS猫头猫 lrcSource = await PluginManager.getByMedia( 4167a91f04fS猫头猫 musicItem, 4177a91f04fS猫头猫 )?.instance?.getLyric?.( 418927dbe93S猫头猫 resetMediaItem(musicItem, undefined, true), 419927dbe93S猫头猫 ); 4207a91f04fS猫头猫 } else { 4217a91f04fS猫头猫 lrcSource = await this.plugin.instance?.getLyric?.( 4227a91f04fS猫头猫 resetMediaItem(musicItem, undefined, true), 4237a91f04fS猫头猫 ); 4247a91f04fS猫头猫 } 4257a91f04fS猫头猫 426927dbe93S猫头猫 rawLrc = lrcSource?.rawLrc; 427927dbe93S猫头猫 lrcUrl = lrcSource?.lrc; 428927dbe93S猫头猫 } catch (e: any) { 429927dbe93S猫头猫 trace('插件获取歌词失败', e?.message, 'error'); 430ea6d708fS猫头猫 devLog('error', '插件获取歌词失败', e, e?.message); 431927dbe93S猫头猫 } 432927dbe93S猫头猫 } 433927dbe93S猫头猫 // 5. 最后一次请求 434927dbe93S猫头猫 if (rawLrc || lrcUrl) { 435927dbe93S猫头猫 const filename = `${pathConst.lrcCachePath}${nanoid()}.lrc`; 436927dbe93S猫头猫 if (lrcUrl) { 437927dbe93S猫头猫 try { 43861aca335S猫头猫 rawLrc = (await axios.get(lrcUrl, {timeout: 2000})).data; 439927dbe93S猫头猫 } catch {} 440927dbe93S猫头猫 } 441927dbe93S猫头猫 if (rawLrc) { 442927dbe93S猫头猫 await writeFile(filename, rawLrc, 'utf8'); 443927dbe93S猫头猫 // 写入缓存 444927dbe93S猫头猫 Cache.update(musicItem, [ 4450e4173cdS猫头猫 [`${internalSerializeKey}.local.localLrc`, filename], 446927dbe93S猫头猫 ]); 447927dbe93S猫头猫 // 如果有meta 448927dbe93S猫头猫 if (meta) { 449927dbe93S猫头猫 MediaMeta.update(musicItem, [ 4500e4173cdS猫头猫 [`${internalSerializeKey}.local.localLrc`, filename], 451927dbe93S猫头猫 ]); 452927dbe93S猫头猫 } 453927dbe93S猫头猫 return { 454927dbe93S猫头猫 rawLrc, 455927dbe93S猫头猫 lrc: lrcUrl, 456927dbe93S猫头猫 }; 457927dbe93S猫头猫 } 458927dbe93S猫头猫 } 4593a6f67b1S猫头猫 // 6. 如果是本地文件 4603a6f67b1S猫头猫 const isDownloaded = LocalMusicSheet.isLocalMusic(musicItem); 4613a6f67b1S猫头猫 if (musicItem.platform !== localPluginPlatform && isDownloaded) { 4623a6f67b1S猫头猫 const res = await localFilePlugin.instance!.getLyric!(isDownloaded); 4633a6f67b1S猫头猫 if (res) { 4643a6f67b1S猫头猫 return res; 4653a6f67b1S猫头猫 } 4663a6f67b1S猫头猫 } 467ea6d708fS猫头猫 devLog('warn', '无歌词'); 468927dbe93S猫头猫 469927dbe93S猫头猫 return null; 470927dbe93S猫头猫 } 471927dbe93S猫头猫 472927dbe93S猫头猫 /** 获取歌词文本 */ 473927dbe93S猫头猫 async getLyricText( 474927dbe93S猫头猫 musicItem: IMusic.IMusicItem, 475927dbe93S猫头猫 ): Promise<string | undefined> { 476927dbe93S猫头猫 return (await this.getLyric(musicItem))?.rawLrc; 477927dbe93S猫头猫 } 478927dbe93S猫头猫 479927dbe93S猫头猫 /** 获取专辑信息 */ 480927dbe93S猫头猫 async getAlbumInfo( 481927dbe93S猫头猫 albumItem: IAlbum.IAlbumItemBase, 482f9afcc0dS猫头猫 page: number = 1, 483f9afcc0dS猫头猫 ): Promise<IPlugin.IAlbumInfoResult | null> { 484927dbe93S猫头猫 if (!this.plugin.instance.getAlbumInfo) { 485f9afcc0dS猫头猫 return { 486f9afcc0dS猫头猫 albumItem, 487f9afcc0dS猫头猫 musicList: albumItem?.musicList ?? [], 488f9afcc0dS猫头猫 isEnd: true, 489f9afcc0dS猫头猫 }; 490927dbe93S猫头猫 } 491927dbe93S猫头猫 try { 492927dbe93S猫头猫 const result = await this.plugin.instance.getAlbumInfo( 493927dbe93S猫头猫 resetMediaItem(albumItem, undefined, true), 494f9afcc0dS猫头猫 page, 495927dbe93S猫头猫 ); 4965276aef9S猫头猫 if (!result) { 4975276aef9S猫头猫 throw new Error(); 4985276aef9S猫头猫 } 499927dbe93S猫头猫 result?.musicList?.forEach(_ => { 500927dbe93S猫头猫 resetMediaItem(_, this.plugin.name); 50196744680S猫头猫 _.album = albumItem.title; 502927dbe93S猫头猫 }); 5035276aef9S猫头猫 504f9afcc0dS猫头猫 if (page <= 1) { 505f9afcc0dS猫头猫 // 合并信息 506f9afcc0dS猫头猫 return { 507f9afcc0dS猫头猫 albumItem: {...albumItem, ...(result?.albumItem ?? {})}, 508f9afcc0dS猫头猫 isEnd: result.isEnd === false ? false : true, 509f9afcc0dS猫头猫 musicList: result.musicList, 510f9afcc0dS猫头猫 }; 511f9afcc0dS猫头猫 } else { 512f9afcc0dS猫头猫 return { 513f9afcc0dS猫头猫 isEnd: result.isEnd === false ? false : true, 514f9afcc0dS猫头猫 musicList: result.musicList, 515f9afcc0dS猫头猫 }; 516f9afcc0dS猫头猫 } 5174394410dS猫头猫 } catch (e: any) { 5184394410dS猫头猫 trace('获取专辑信息失败', e?.message); 519ea6d708fS猫头猫 devLog('error', '获取专辑信息失败', e, e?.message); 520ea6d708fS猫头猫 521f9afcc0dS猫头猫 return null; 522927dbe93S猫头猫 } 523927dbe93S猫头猫 } 524927dbe93S猫头猫 5255830c002S猫头猫 /** 获取歌单信息 */ 5265830c002S猫头猫 async getMusicSheetInfo( 5275830c002S猫头猫 sheetItem: IMusic.IMusicSheetItem, 5285830c002S猫头猫 page: number = 1, 5295830c002S猫头猫 ): Promise<IPlugin.ISheetInfoResult | null> { 5305281926bS猫头猫 if (!this.plugin.instance.getMusicSheetInfo) { 5315830c002S猫头猫 return { 5325830c002S猫头猫 sheetItem, 5335830c002S猫头猫 musicList: sheetItem?.musicList ?? [], 5345830c002S猫头猫 isEnd: true, 5355830c002S猫头猫 }; 5365830c002S猫头猫 } 5375830c002S猫头猫 try { 5385830c002S猫头猫 const result = await this.plugin.instance?.getMusicSheetInfo?.( 5395830c002S猫头猫 resetMediaItem(sheetItem, undefined, true), 5405830c002S猫头猫 page, 5415830c002S猫头猫 ); 5425830c002S猫头猫 if (!result) { 5435830c002S猫头猫 throw new Error(); 5445830c002S猫头猫 } 5455830c002S猫头猫 result?.musicList?.forEach(_ => { 5465830c002S猫头猫 resetMediaItem(_, this.plugin.name); 5475830c002S猫头猫 }); 5485830c002S猫头猫 5495830c002S猫头猫 if (page <= 1) { 5505830c002S猫头猫 // 合并信息 5515830c002S猫头猫 return { 5525830c002S猫头猫 sheetItem: {...sheetItem, ...(result?.sheetItem ?? {})}, 5535830c002S猫头猫 isEnd: result.isEnd === false ? false : true, 5545830c002S猫头猫 musicList: result.musicList, 5555830c002S猫头猫 }; 5565830c002S猫头猫 } else { 5575830c002S猫头猫 return { 5585830c002S猫头猫 isEnd: result.isEnd === false ? false : true, 5595830c002S猫头猫 musicList: result.musicList, 5605830c002S猫头猫 }; 5615830c002S猫头猫 } 5625830c002S猫头猫 } catch (e: any) { 5635830c002S猫头猫 trace('获取歌单信息失败', e, e?.message); 5645830c002S猫头猫 devLog('error', '获取歌单信息失败', e, e?.message); 5655830c002S猫头猫 5665830c002S猫头猫 return null; 5675830c002S猫头猫 } 5685830c002S猫头猫 } 5695830c002S猫头猫 570927dbe93S猫头猫 /** 查询作者信息 */ 571efb9da24S猫头猫 async getArtistWorks<T extends IArtist.ArtistMediaType>( 572927dbe93S猫头猫 artistItem: IArtist.IArtistItem, 573927dbe93S猫头猫 page: number, 574927dbe93S猫头猫 type: T, 575927dbe93S猫头猫 ): Promise<IPlugin.ISearchResult<T>> { 576efb9da24S猫头猫 if (!this.plugin.instance.getArtistWorks) { 577927dbe93S猫头猫 return { 578927dbe93S猫头猫 isEnd: true, 579927dbe93S猫头猫 data: [], 580927dbe93S猫头猫 }; 581927dbe93S猫头猫 } 582927dbe93S猫头猫 try { 583efb9da24S猫头猫 const result = await this.plugin.instance.getArtistWorks( 584927dbe93S猫头猫 artistItem, 585927dbe93S猫头猫 page, 586927dbe93S猫头猫 type, 587927dbe93S猫头猫 ); 588927dbe93S猫头猫 if (!result.data) { 589927dbe93S猫头猫 return { 590927dbe93S猫头猫 isEnd: true, 591927dbe93S猫头猫 data: [], 592927dbe93S猫头猫 }; 593927dbe93S猫头猫 } 594927dbe93S猫头猫 result.data?.forEach(_ => resetMediaItem(_, this.plugin.name)); 595927dbe93S猫头猫 return { 596927dbe93S猫头猫 isEnd: result.isEnd ?? true, 597927dbe93S猫头猫 data: result.data, 598927dbe93S猫头猫 }; 5994394410dS猫头猫 } catch (e: any) { 6004394410dS猫头猫 trace('查询作者信息失败', e?.message); 601ea6d708fS猫头猫 devLog('error', '查询作者信息失败', e, e?.message); 602ea6d708fS猫头猫 603927dbe93S猫头猫 throw e; 604927dbe93S猫头猫 } 605927dbe93S猫头猫 } 60608380090S猫头猫 60708380090S猫头猫 /** 导入歌单 */ 60808380090S猫头猫 async importMusicSheet(urlLike: string): Promise<IMusic.IMusicItem[]> { 60908380090S猫头猫 try { 61008380090S猫头猫 const result = 61108380090S猫头猫 (await this.plugin.instance?.importMusicSheet?.(urlLike)) ?? []; 61208380090S猫头猫 result.forEach(_ => resetMediaItem(_, this.plugin.name)); 61308380090S猫头猫 return result; 614ea6d708fS猫头猫 } catch (e: any) { 6150e4173cdS猫头猫 console.log(e); 616ea6d708fS猫头猫 devLog('error', '导入歌单失败', e, e?.message); 617ea6d708fS猫头猫 61808380090S猫头猫 return []; 61908380090S猫头猫 } 62008380090S猫头猫 } 6214d9d3c4cS猫头猫 /** 导入单曲 */ 6224d9d3c4cS猫头猫 async importMusicItem(urlLike: string): Promise<IMusic.IMusicItem | null> { 6234d9d3c4cS猫头猫 try { 6244d9d3c4cS猫头猫 const result = await this.plugin.instance?.importMusicItem?.( 6254d9d3c4cS猫头猫 urlLike, 6264d9d3c4cS猫头猫 ); 6274d9d3c4cS猫头猫 if (!result) { 6284d9d3c4cS猫头猫 throw new Error(); 6294d9d3c4cS猫头猫 } 6304d9d3c4cS猫头猫 resetMediaItem(result, this.plugin.name); 6314d9d3c4cS猫头猫 return result; 632ea6d708fS猫头猫 } catch (e: any) { 633ea6d708fS猫头猫 devLog('error', '导入单曲失败', e, e?.message); 634ea6d708fS猫头猫 6354d9d3c4cS猫头猫 return null; 6364d9d3c4cS猫头猫 } 6374d9d3c4cS猫头猫 } 638d52aa40eS猫头猫 /** 获取榜单 */ 63992b6c95aS猫头猫 async getTopLists(): Promise<IMusic.IMusicSheetGroupItem[]> { 640d52aa40eS猫头猫 try { 641d52aa40eS猫头猫 const result = await this.plugin.instance?.getTopLists?.(); 642d52aa40eS猫头猫 if (!result) { 643d52aa40eS猫头猫 throw new Error(); 644d52aa40eS猫头猫 } 645d52aa40eS猫头猫 return result; 646d52aa40eS猫头猫 } catch (e: any) { 647d52aa40eS猫头猫 devLog('error', '获取榜单失败', e, e?.message); 648d52aa40eS猫头猫 return []; 649d52aa40eS猫头猫 } 650d52aa40eS猫头猫 } 651d52aa40eS猫头猫 /** 获取榜单详情 */ 652d52aa40eS猫头猫 async getTopListDetail( 65392b6c95aS猫头猫 topListItem: IMusic.IMusicSheetItemBase, 65492b6c95aS猫头猫 ): Promise<ICommon.WithMusicList<IMusic.IMusicSheetItemBase>> { 655d52aa40eS猫头猫 try { 656d52aa40eS猫头猫 const result = await this.plugin.instance?.getTopListDetail?.( 657d52aa40eS猫头猫 topListItem, 658d52aa40eS猫头猫 ); 659d52aa40eS猫头猫 if (!result) { 660d52aa40eS猫头猫 throw new Error(); 661d52aa40eS猫头猫 } 662d384662fS猫头猫 if (result.musicList) { 663d384662fS猫头猫 result.musicList.forEach(_ => 664d384662fS猫头猫 resetMediaItem(_, this.plugin.name), 665d384662fS猫头猫 ); 666d384662fS猫头猫 } 667d52aa40eS猫头猫 return result; 668d52aa40eS猫头猫 } catch (e: any) { 669d52aa40eS猫头猫 devLog('error', '获取榜单详情失败', e, e?.message); 670d52aa40eS猫头猫 return { 671d52aa40eS猫头猫 ...topListItem, 672d52aa40eS猫头猫 musicList: [], 673d52aa40eS猫头猫 }; 674d52aa40eS猫头猫 } 675d52aa40eS猫头猫 } 676ceb900cdS猫头猫 6775830c002S猫头猫 /** 获取推荐歌单的tag */ 678ceb900cdS猫头猫 async getRecommendSheetTags(): Promise<IPlugin.IGetRecommendSheetTagsResult> { 679ceb900cdS猫头猫 try { 680ceb900cdS猫头猫 const result = 681ceb900cdS猫头猫 await this.plugin.instance?.getRecommendSheetTags?.(); 682ceb900cdS猫头猫 if (!result) { 683ceb900cdS猫头猫 throw new Error(); 684ceb900cdS猫头猫 } 685ceb900cdS猫头猫 return result; 686ceb900cdS猫头猫 } catch (e: any) { 687ceb900cdS猫头猫 devLog('error', '获取推荐歌单失败', e, e?.message); 688ceb900cdS猫头猫 return { 689ceb900cdS猫头猫 data: [], 690ceb900cdS猫头猫 }; 691ceb900cdS猫头猫 } 692ceb900cdS猫头猫 } 6935830c002S猫头猫 /** 获取某个tag的推荐歌单 */ 694ceb900cdS猫头猫 async getRecommendSheetsByTag( 695ceb900cdS猫头猫 tagItem: ICommon.IUnique, 696ceb900cdS猫头猫 page?: number, 697ceb900cdS猫头猫 ): Promise<ICommon.PaginationResponse<IMusic.IMusicSheetItemBase>> { 698ceb900cdS猫头猫 try { 699ceb900cdS猫头猫 const result = 700ceb900cdS猫头猫 await this.plugin.instance?.getRecommendSheetsByTag?.( 701ceb900cdS猫头猫 tagItem, 702ceb900cdS猫头猫 page ?? 1, 703ceb900cdS猫头猫 ); 704ceb900cdS猫头猫 if (!result) { 705ceb900cdS猫头猫 throw new Error(); 706ceb900cdS猫头猫 } 707ceb900cdS猫头猫 if (result.isEnd !== false) { 708ceb900cdS猫头猫 result.isEnd = true; 709ceb900cdS猫头猫 } 710ceb900cdS猫头猫 if (!result.data) { 711ceb900cdS猫头猫 result.data = []; 712ceb900cdS猫头猫 } 713ceb900cdS猫头猫 result.data.forEach(item => resetMediaItem(item, this.plugin.name)); 714ceb900cdS猫头猫 715ceb900cdS猫头猫 return result; 716ceb900cdS猫头猫 } catch (e: any) { 717ceb900cdS猫头猫 devLog('error', '获取推荐歌单详情失败', e, e?.message); 718ceb900cdS猫头猫 return { 719ceb900cdS猫头猫 isEnd: true, 720ceb900cdS猫头猫 data: [], 721ceb900cdS猫头猫 }; 722ceb900cdS猫头猫 } 723ceb900cdS猫头猫 } 724927dbe93S猫头猫} 725d5bfeb7eS猫头猫//#endregion 7261a5528a0S猫头猫 727927dbe93S猫头猫let plugins: Array<Plugin> = []; 728927dbe93S猫头猫const pluginStateMapper = new StateMapper(() => plugins); 72974d0cf81S猫头猫 730d5bfeb7eS猫头猫//#region 本地音乐插件 73174d0cf81S猫头猫/** 本地插件 */ 73274d0cf81S猫头猫const localFilePlugin = new Plugin(function () { 7330e4173cdS猫头猫 return { 734d5bfeb7eS猫头猫 platform: localPluginPlatform, 73574d0cf81S猫头猫 _path: '', 73674d0cf81S猫头猫 async getMusicInfo(musicBase) { 73774d0cf81S猫头猫 const localPath = getInternalData<string>( 73874d0cf81S猫头猫 musicBase, 73974d0cf81S猫头猫 InternalDataType.LOCALPATH, 7400e4173cdS猫头猫 ); 74174d0cf81S猫头猫 if (localPath) { 74274d0cf81S猫头猫 const coverImg = await Mp3Util.getMediaCoverImg(localPath); 74374d0cf81S猫头猫 return { 74474d0cf81S猫头猫 artwork: coverImg, 74574d0cf81S猫头猫 }; 74674d0cf81S猫头猫 } 74774d0cf81S猫头猫 return null; 74874d0cf81S猫头猫 }, 7497993f90eS猫头猫 async getLyric(musicBase) { 7507993f90eS猫头猫 const localPath = getInternalData<string>( 7517993f90eS猫头猫 musicBase, 7527993f90eS猫头猫 InternalDataType.LOCALPATH, 7537993f90eS猫头猫 ); 7543a6f67b1S猫头猫 let rawLrc: string | null = null; 7557993f90eS猫头猫 if (localPath) { 7563a6f67b1S猫头猫 // 读取内嵌歌词 7573a6f67b1S猫头猫 try { 7583a6f67b1S猫头猫 rawLrc = await Mp3Util.getLyric(localPath); 7593a6f67b1S猫头猫 } catch (e) { 7603a6f67b1S猫头猫 console.log('e', e); 7617993f90eS猫头猫 } 7623a6f67b1S猫头猫 if (!rawLrc) { 7633a6f67b1S猫头猫 // 读取配置歌词 7643a6f67b1S猫头猫 const lastDot = localPath.lastIndexOf('.'); 7653a6f67b1S猫头猫 const lrcPath = localPath.slice(0, lastDot) + '.lrc'; 7663a6f67b1S猫头猫 7673a6f67b1S猫头猫 try { 7683a6f67b1S猫头猫 if (await exists(lrcPath)) { 7693a6f67b1S猫头猫 rawLrc = await readFile(lrcPath, 'utf8'); 7703a6f67b1S猫头猫 } 7713a6f67b1S猫头猫 } catch {} 7723a6f67b1S猫头猫 } 7733a6f67b1S猫头猫 } 7743a6f67b1S猫头猫 7753a6f67b1S猫头猫 return rawLrc 7763a6f67b1S猫头猫 ? { 7773a6f67b1S猫头猫 rawLrc, 7783a6f67b1S猫头猫 } 7793a6f67b1S猫头猫 : null; 7807993f90eS猫头猫 }, 78174d0cf81S猫头猫 }; 78274d0cf81S猫头猫}, ''); 7837993f90eS猫头猫localFilePlugin.hash = localPluginHash; 784927dbe93S猫头猫 785d5bfeb7eS猫头猫//#endregion 786d5bfeb7eS猫头猫 787927dbe93S猫头猫async function setup() { 788927dbe93S猫头猫 const _plugins: Array<Plugin> = []; 789927dbe93S猫头猫 try { 790927dbe93S猫头猫 // 加载插件 791927dbe93S猫头猫 const pluginsPaths = await readDir(pathConst.pluginPath); 792927dbe93S猫头猫 for (let i = 0; i < pluginsPaths.length; ++i) { 793927dbe93S猫头猫 const _pluginUrl = pluginsPaths[i]; 7941e263108S猫头猫 trace('初始化插件', _pluginUrl); 7951e263108S猫头猫 if ( 7961e263108S猫头猫 _pluginUrl.isFile() && 7971e263108S猫头猫 (_pluginUrl.name?.endsWith?.('.js') || 7981e263108S猫头猫 _pluginUrl.path?.endsWith?.('.js')) 7991e263108S猫头猫 ) { 800927dbe93S猫头猫 const funcCode = await readFile(_pluginUrl.path, 'utf8'); 801927dbe93S猫头猫 const plugin = new Plugin(funcCode, _pluginUrl.path); 8024060c00aS猫头猫 const _pluginIndex = _plugins.findIndex( 8034060c00aS猫头猫 p => p.hash === plugin.hash, 8044060c00aS猫头猫 ); 805927dbe93S猫头猫 if (_pluginIndex !== -1) { 806927dbe93S猫头猫 // 重复插件,直接忽略 8070c266394S猫头猫 continue; 808927dbe93S猫头猫 } 809927dbe93S猫头猫 plugin.hash !== '' && _plugins.push(plugin); 810927dbe93S猫头猫 } 811927dbe93S猫头猫 } 812927dbe93S猫头猫 813927dbe93S猫头猫 plugins = _plugins; 814927dbe93S猫头猫 pluginStateMapper.notify(); 815e08d37a3S猫头猫 /** 初始化meta信息 */ 816e08d37a3S猫头猫 PluginMeta.setupMeta(plugins.map(_ => _.name)); 817927dbe93S猫头猫 } catch (e: any) { 8184060c00aS猫头猫 ToastAndroid.show( 8194060c00aS猫头猫 `插件初始化失败:${e?.message ?? e}`, 8204060c00aS猫头猫 ToastAndroid.LONG, 8214060c00aS猫头猫 ); 8221a5528a0S猫头猫 errorLog('插件初始化失败', e?.message); 823927dbe93S猫头猫 throw e; 824927dbe93S猫头猫 } 825927dbe93S猫头猫} 826927dbe93S猫头猫 827927dbe93S猫头猫// 安装插件 828927dbe93S猫头猫async function installPlugin(pluginPath: string) { 82922c09412S猫头猫 // if (pluginPath.endsWith('.js')) { 830927dbe93S猫头猫 const funcCode = await readFile(pluginPath, 'utf8'); 831927dbe93S猫头猫 const plugin = new Plugin(funcCode, pluginPath); 832927dbe93S猫头猫 const _pluginIndex = plugins.findIndex(p => p.hash === plugin.hash); 833927dbe93S猫头猫 if (_pluginIndex !== -1) { 8344d9d3c4cS猫头猫 throw new Error('插件已安装'); 835927dbe93S猫头猫 } 836927dbe93S猫头猫 if (plugin.hash !== '') { 837927dbe93S猫头猫 const fn = nanoid(); 838927dbe93S猫头猫 const _pluginPath = `${pathConst.pluginPath}${fn}.js`; 839927dbe93S猫头猫 await copyFile(pluginPath, _pluginPath); 840927dbe93S猫头猫 plugin.path = _pluginPath; 841927dbe93S猫头猫 plugins = plugins.concat(plugin); 842927dbe93S猫头猫 pluginStateMapper.notify(); 8434d9d3c4cS猫头猫 return; 844927dbe93S猫头猫 } 8454d9d3c4cS猫头猫 throw new Error('插件无法解析'); 84622c09412S猫头猫 // } 84722c09412S猫头猫 // throw new Error('插件不存在'); 848927dbe93S猫头猫} 849927dbe93S猫头猫 85058992c6bS猫头猫async function installPluginFromUrl(url: string) { 85158992c6bS猫头猫 try { 85258992c6bS猫头猫 const funcCode = (await axios.get(url)).data; 85358992c6bS猫头猫 if (funcCode) { 85458992c6bS猫头猫 const plugin = new Plugin(funcCode, ''); 85558992c6bS猫头猫 const _pluginIndex = plugins.findIndex(p => p.hash === plugin.hash); 85658992c6bS猫头猫 if (_pluginIndex !== -1) { 8578b7ddca8S猫头猫 // 静默忽略 8588b7ddca8S猫头猫 return; 85958992c6bS猫头猫 } 86025c1bd29S猫头猫 const oldVersionPlugin = plugins.find(p => p.name === plugin.name); 86125c1bd29S猫头猫 if (oldVersionPlugin) { 86225c1bd29S猫头猫 if ( 86325c1bd29S猫头猫 compare( 86425c1bd29S猫头猫 oldVersionPlugin.instance.version ?? '', 86525c1bd29S猫头猫 plugin.instance.version ?? '', 86625c1bd29S猫头猫 '>', 86725c1bd29S猫头猫 ) 86825c1bd29S猫头猫 ) { 86925c1bd29S猫头猫 throw new Error('已安装更新版本的插件'); 87025c1bd29S猫头猫 } 87125c1bd29S猫头猫 } 87225c1bd29S猫头猫 87358992c6bS猫头猫 if (plugin.hash !== '') { 87458992c6bS猫头猫 const fn = nanoid(); 87558992c6bS猫头猫 const _pluginPath = `${pathConst.pluginPath}${fn}.js`; 87658992c6bS猫头猫 await writeFile(_pluginPath, funcCode, 'utf8'); 87758992c6bS猫头猫 plugin.path = _pluginPath; 87858992c6bS猫头猫 plugins = plugins.concat(plugin); 87925c1bd29S猫头猫 if (oldVersionPlugin) { 88025c1bd29S猫头猫 plugins = plugins.filter( 88125c1bd29S猫头猫 _ => _.hash !== oldVersionPlugin.hash, 88225c1bd29S猫头猫 ); 88325c1bd29S猫头猫 try { 88425c1bd29S猫头猫 await unlink(oldVersionPlugin.path); 88525c1bd29S猫头猫 } catch {} 88625c1bd29S猫头猫 } 88758992c6bS猫头猫 pluginStateMapper.notify(); 88858992c6bS猫头猫 return; 88958992c6bS猫头猫 } 89074acbfc0S猫头猫 throw new Error('插件无法解析!'); 89158992c6bS猫头猫 } 89225c1bd29S猫头猫 } catch (e: any) { 893ea6d708fS猫头猫 devLog('error', 'URL安装插件失败', e, e?.message); 89458992c6bS猫头猫 errorLog('URL安装插件失败', e); 89525c1bd29S猫头猫 throw new Error(e?.message ?? ''); 89658992c6bS猫头猫 } 89758992c6bS猫头猫} 89858992c6bS猫头猫 899927dbe93S猫头猫/** 卸载插件 */ 900927dbe93S猫头猫async function uninstallPlugin(hash: string) { 901927dbe93S猫头猫 const targetIndex = plugins.findIndex(_ => _.hash === hash); 902927dbe93S猫头猫 if (targetIndex !== -1) { 903927dbe93S猫头猫 try { 90424e5e74aS猫头猫 const pluginName = plugins[targetIndex].name; 905927dbe93S猫头猫 await unlink(plugins[targetIndex].path); 906927dbe93S猫头猫 plugins = plugins.filter(_ => _.hash !== hash); 907927dbe93S猫头猫 pluginStateMapper.notify(); 90824e5e74aS猫头猫 if (plugins.every(_ => _.name !== pluginName)) { 90924e5e74aS猫头猫 await MediaMeta.removePlugin(pluginName); 91024e5e74aS猫头猫 } 911927dbe93S猫头猫 } catch {} 912927dbe93S猫头猫 } 913927dbe93S猫头猫} 914927dbe93S猫头猫 91508882a77S猫头猫async function uninstallAllPlugins() { 91608882a77S猫头猫 await Promise.all( 91708882a77S猫头猫 plugins.map(async plugin => { 91808882a77S猫头猫 try { 91908882a77S猫头猫 const pluginName = plugin.name; 92008882a77S猫头猫 await unlink(plugin.path); 92108882a77S猫头猫 await MediaMeta.removePlugin(pluginName); 92208882a77S猫头猫 } catch (e) {} 92308882a77S猫头猫 }), 92408882a77S猫头猫 ); 92508882a77S猫头猫 plugins = []; 92608882a77S猫头猫 pluginStateMapper.notify(); 927e08d37a3S猫头猫 928e08d37a3S猫头猫 /** 清除空余文件,异步做就可以了 */ 929e08d37a3S猫头猫 readDir(pathConst.pluginPath) 930e08d37a3S猫头猫 .then(fns => { 931e08d37a3S猫头猫 fns.forEach(fn => { 932e08d37a3S猫头猫 unlink(fn.path).catch(emptyFunction); 933e08d37a3S猫头猫 }); 934e08d37a3S猫头猫 }) 935e08d37a3S猫头猫 .catch(emptyFunction); 93608882a77S猫头猫} 93708882a77S猫头猫 93825c1bd29S猫头猫async function updatePlugin(plugin: Plugin) { 93925c1bd29S猫头猫 const updateUrl = plugin.instance.srcUrl; 94025c1bd29S猫头猫 if (!updateUrl) { 94125c1bd29S猫头猫 throw new Error('没有更新源'); 94225c1bd29S猫头猫 } 94325c1bd29S猫头猫 try { 94425c1bd29S猫头猫 await installPluginFromUrl(updateUrl); 94525c1bd29S猫头猫 } catch (e: any) { 94625c1bd29S猫头猫 if (e.message === '插件已安装') { 94725c1bd29S猫头猫 throw new Error('当前已是最新版本'); 94825c1bd29S猫头猫 } else { 94925c1bd29S猫头猫 throw e; 95025c1bd29S猫头猫 } 95125c1bd29S猫头猫 } 95225c1bd29S猫头猫} 95325c1bd29S猫头猫 954927dbe93S猫头猫function getByMedia(mediaItem: ICommon.IMediaBase) { 9552c595535S猫头猫 return getByName(mediaItem?.platform); 956927dbe93S猫头猫} 957927dbe93S猫头猫 958927dbe93S猫头猫function getByHash(hash: string) { 9597993f90eS猫头猫 return hash === localPluginHash 9607993f90eS猫头猫 ? localFilePlugin 9617993f90eS猫头猫 : plugins.find(_ => _.hash === hash); 962927dbe93S猫头猫} 963927dbe93S猫头猫 964927dbe93S猫头猫function getByName(name: string) { 9657993f90eS猫头猫 return name === localPluginPlatform 9660e4173cdS猫头猫 ? localFilePlugin 9670e4173cdS猫头猫 : plugins.find(_ => _.name === name); 968927dbe93S猫头猫} 969927dbe93S猫头猫 970927dbe93S猫头猫function getValidPlugins() { 971927dbe93S猫头猫 return plugins.filter(_ => _.state === 'enabled'); 972927dbe93S猫头猫} 973927dbe93S猫头猫 9742b80a429S猫头猫function getSearchablePlugins(supportedSearchType?: ICommon.SupportMediaType) { 9752b80a429S猫头猫 return plugins.filter( 9762b80a429S猫头猫 _ => 9772b80a429S猫头猫 _.state === 'enabled' && 9782b80a429S猫头猫 _.instance.search && 979*39ac60f7S猫头猫 (supportedSearchType && _.instance.supportedSearchType 980*39ac60f7S猫头猫 ? _.instance.supportedSearchType.includes(supportedSearchType) 9812b80a429S猫头猫 : true), 9822b80a429S猫头猫 ); 983efb9da24S猫头猫} 984efb9da24S猫头猫 9852b80a429S猫头猫function getSortedSearchablePlugins( 9862b80a429S猫头猫 supportedSearchType?: ICommon.SupportMediaType, 9872b80a429S猫头猫) { 9882b80a429S猫头猫 return getSearchablePlugins(supportedSearchType).sort((a, b) => 989e08d37a3S猫头猫 (PluginMeta.getPluginMeta(a).order ?? Infinity) - 990e08d37a3S猫头猫 (PluginMeta.getPluginMeta(b).order ?? Infinity) < 991e08d37a3S猫头猫 0 992e08d37a3S猫头猫 ? -1 993e08d37a3S猫头猫 : 1, 994e08d37a3S猫头猫 ); 995e08d37a3S猫头猫} 996e08d37a3S猫头猫 99715feccc1S猫头猫function getTopListsablePlugins() { 99815feccc1S猫头猫 return plugins.filter(_ => _.state === 'enabled' && _.instance.getTopLists); 99915feccc1S猫头猫} 100015feccc1S猫头猫 100115feccc1S猫头猫function getSortedTopListsablePlugins() { 100215feccc1S猫头猫 return getTopListsablePlugins().sort((a, b) => 100315feccc1S猫头猫 (PluginMeta.getPluginMeta(a).order ?? Infinity) - 100415feccc1S猫头猫 (PluginMeta.getPluginMeta(b).order ?? Infinity) < 100515feccc1S猫头猫 0 100615feccc1S猫头猫 ? -1 100715feccc1S猫头猫 : 1, 100815feccc1S猫头猫 ); 100915feccc1S猫头猫} 101015feccc1S猫头猫 1011ceb900cdS猫头猫function getRecommendSheetablePlugins() { 1012ceb900cdS猫头猫 return plugins.filter( 1013ceb900cdS猫头猫 _ => _.state === 'enabled' && _.instance.getRecommendSheetsByTag, 1014ceb900cdS猫头猫 ); 1015ceb900cdS猫头猫} 1016ceb900cdS猫头猫 1017ceb900cdS猫头猫function getSortedRecommendSheetablePlugins() { 1018ceb900cdS猫头猫 return getRecommendSheetablePlugins().sort((a, b) => 1019ceb900cdS猫头猫 (PluginMeta.getPluginMeta(a).order ?? Infinity) - 1020ceb900cdS猫头猫 (PluginMeta.getPluginMeta(b).order ?? Infinity) < 1021ceb900cdS猫头猫 0 1022ceb900cdS猫头猫 ? -1 1023ceb900cdS猫头猫 : 1, 1024ceb900cdS猫头猫 ); 1025ceb900cdS猫头猫} 1026ceb900cdS猫头猫 1027e08d37a3S猫头猫function useSortedPlugins() { 1028e08d37a3S猫头猫 const _plugins = pluginStateMapper.useMappedState(); 1029e08d37a3S猫头猫 const _pluginMetaAll = PluginMeta.usePluginMetaAll(); 1030e08d37a3S猫头猫 103134588741S猫头猫 const [sortedPlugins, setSortedPlugins] = useState( 103234588741S猫头猫 [..._plugins].sort((a, b) => 1033e08d37a3S猫头猫 (_pluginMetaAll[a.name]?.order ?? Infinity) - 1034e08d37a3S猫头猫 (_pluginMetaAll[b.name]?.order ?? Infinity) < 1035e08d37a3S猫头猫 0 1036e08d37a3S猫头猫 ? -1 1037e08d37a3S猫头猫 : 1, 103834588741S猫头猫 ), 1039e08d37a3S猫头猫 ); 104034588741S猫头猫 104134588741S猫头猫 useEffect(() => { 1042d4cd40d8S猫头猫 InteractionManager.runAfterInteractions(() => { 104334588741S猫头猫 setSortedPlugins( 104434588741S猫头猫 [..._plugins].sort((a, b) => 104534588741S猫头猫 (_pluginMetaAll[a.name]?.order ?? Infinity) - 104634588741S猫头猫 (_pluginMetaAll[b.name]?.order ?? Infinity) < 104734588741S猫头猫 0 104834588741S猫头猫 ? -1 104934588741S猫头猫 : 1, 105034588741S猫头猫 ), 105134588741S猫头猫 ); 1052d4cd40d8S猫头猫 }); 105334588741S猫头猫 }, [_plugins, _pluginMetaAll]); 105434588741S猫头猫 105534588741S猫头猫 return sortedPlugins; 1056e08d37a3S猫头猫} 1057e08d37a3S猫头猫 1058927dbe93S猫头猫const PluginManager = { 1059927dbe93S猫头猫 setup, 1060927dbe93S猫头猫 installPlugin, 106158992c6bS猫头猫 installPluginFromUrl, 106225c1bd29S猫头猫 updatePlugin, 1063927dbe93S猫头猫 uninstallPlugin, 1064927dbe93S猫头猫 getByMedia, 1065927dbe93S猫头猫 getByHash, 1066927dbe93S猫头猫 getByName, 1067927dbe93S猫头猫 getValidPlugins, 1068efb9da24S猫头猫 getSearchablePlugins, 1069e08d37a3S猫头猫 getSortedSearchablePlugins, 107015feccc1S猫头猫 getTopListsablePlugins, 1071ceb900cdS猫头猫 getSortedRecommendSheetablePlugins, 107215feccc1S猫头猫 getSortedTopListsablePlugins, 10735276aef9S猫头猫 usePlugins: pluginStateMapper.useMappedState, 1074e08d37a3S猫头猫 useSortedPlugins, 107508882a77S猫头猫 uninstallAllPlugins, 10765276aef9S猫头猫}; 1077927dbe93S猫头猫 1078927dbe93S猫头猫export default PluginManager; 1079