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'; 46a84a85c5S猫头猫import {getFileName} from '@/utils/fileUtils'; 47927dbe93S猫头猫 4861aca335S猫头猫axios.defaults.timeout = 2000; 49927dbe93S猫头猫 50927dbe93S猫头猫const sha256 = CryptoJs.SHA256; 51927dbe93S猫头猫 52cfa0fc07S猫头猫export enum PluginStateCode { 53927dbe93S猫头猫 /** 版本不匹配 */ 54927dbe93S猫头猫 VersionNotMatch = 'VERSION NOT MATCH', 55927dbe93S猫头猫 /** 无法解析 */ 56927dbe93S猫头猫 CannotParse = 'CANNOT PARSE', 57927dbe93S猫头猫} 58927dbe93S猫头猫 599c34d637S猫头猫const packages: Record<string, any> = { 609c34d637S猫头猫 cheerio, 619c34d637S猫头猫 'crypto-js': CryptoJs, 629c34d637S猫头猫 axios, 639c34d637S猫头猫 dayjs, 649c34d637S猫头猫 'big-integer': bigInt, 659c34d637S猫头猫 qs, 669c34d637S猫头猫 he, 673b3d6357S猫头猫 '@react-native-cookies/cookies': CookieManager, 689c34d637S猫头猫}; 699c34d637S猫头猫 70b43683eaS猫头猫const _require = (packageName: string) => { 71b43683eaS猫头猫 let pkg = packages[packageName]; 72b43683eaS猫头猫 pkg.default = pkg; 73b43683eaS猫头猫 return pkg; 74b43683eaS猫头猫}; 759c34d637S猫头猫 7653f8cd8eS猫头猫const _consoleBind = function ( 7753f8cd8eS猫头猫 method: 'log' | 'error' | 'info' | 'warn', 7853f8cd8eS猫头猫 ...args: any 7953f8cd8eS猫头猫) { 8053f8cd8eS猫头猫 const fn = console[method]; 8153f8cd8eS猫头猫 if (fn) { 8253f8cd8eS猫头猫 fn(...args); 8353f8cd8eS猫头猫 devLog(method, ...args); 8453f8cd8eS猫头猫 } 8553f8cd8eS猫头猫}; 8653f8cd8eS猫头猫 8753f8cd8eS猫头猫const _console = { 8853f8cd8eS猫头猫 log: _consoleBind.bind(null, 'log'), 8953f8cd8eS猫头猫 warn: _consoleBind.bind(null, 'warn'), 9053f8cd8eS猫头猫 info: _consoleBind.bind(null, 'info'), 9153f8cd8eS猫头猫 error: _consoleBind.bind(null, 'error'), 9253f8cd8eS猫头猫}; 9353f8cd8eS猫头猫 94d5bfeb7eS猫头猫//#region 插件类 95927dbe93S猫头猫export class Plugin { 96927dbe93S猫头猫 /** 插件名 */ 97927dbe93S猫头猫 public name: string; 98927dbe93S猫头猫 /** 插件的hash,作为唯一id */ 99927dbe93S猫头猫 public hash: string; 100927dbe93S猫头猫 /** 插件状态:激活、关闭、错误 */ 101927dbe93S猫头猫 public state: 'enabled' | 'disabled' | 'error'; 102927dbe93S猫头猫 /** 插件状态信息 */ 103927dbe93S猫头猫 public stateCode?: PluginStateCode; 104927dbe93S猫头猫 /** 插件的实例 */ 105927dbe93S猫头猫 public instance: IPlugin.IPluginInstance; 106927dbe93S猫头猫 /** 插件路径 */ 107927dbe93S猫头猫 public path: string; 108927dbe93S猫头猫 /** 插件方法 */ 109927dbe93S猫头猫 public methods: PluginMethods; 110927dbe93S猫头猫 11174d0cf81S猫头猫 constructor( 11274d0cf81S猫头猫 funcCode: string | (() => IPlugin.IPluginInstance), 11374d0cf81S猫头猫 pluginPath: string, 11474d0cf81S猫头猫 ) { 115927dbe93S猫头猫 this.state = 'enabled'; 116927dbe93S猫头猫 let _instance: IPlugin.IPluginInstance; 1173b3d6357S猫头猫 const _module: any = {exports: {}}; 118927dbe93S猫头猫 try { 11974d0cf81S猫头猫 if (typeof funcCode === 'string') { 120e0caf342S猫头猫 // 插件的环境变量 121e0caf342S猫头猫 const env = { 122e0caf342S猫头猫 getUserVariables: () => { 123e0caf342S猫头猫 return ( 124e0caf342S猫头猫 PluginMeta.getPluginMeta(this)?.userVariables ?? {} 125e0caf342S猫头猫 ); 126e0caf342S猫头猫 }, 127e3fa9b3cS猫头猫 os: 'android', 128e0caf342S猫头猫 }; 129e0caf342S猫头猫 1304060c00aS猫头猫 // eslint-disable-next-line no-new-func 131927dbe93S猫头猫 _instance = Function(` 132927dbe93S猫头猫 'use strict'; 133e0caf342S猫头猫 return function(require, __musicfree_require, module, exports, console, env) { 1349c34d637S猫头猫 ${funcCode} 135927dbe93S猫头猫 } 136e0caf342S猫头猫 `)()( 137e0caf342S猫头猫 _require, 138e0caf342S猫头猫 _require, 139e0caf342S猫头猫 _module, 140e0caf342S猫头猫 _module.exports, 141e0caf342S猫头猫 _console, 142e0caf342S猫头猫 env, 143e0caf342S猫头猫 ); 1443b3d6357S猫头猫 if (_module.exports.default) { 1453b3d6357S猫头猫 _instance = _module.exports 1463b3d6357S猫头猫 .default as IPlugin.IPluginInstance; 1473b3d6357S猫头猫 } else { 1489c34d637S猫头猫 _instance = _module.exports as IPlugin.IPluginInstance; 1493b3d6357S猫头猫 } 15074d0cf81S猫头猫 } else { 15174d0cf81S猫头猫 _instance = funcCode(); 15274d0cf81S猫头猫 } 153c2b3a262S猫头猫 // 插件初始化后的一些操作 15495297592S猫头猫 if (Array.isArray(_instance.userVariables)) { 15595297592S猫头猫 _instance.userVariables = _instance.userVariables.filter( 15695297592S猫头猫 it => it?.key, 15795297592S猫头猫 ); 15895297592S猫头猫 } 159927dbe93S猫头猫 this.checkValid(_instance); 160927dbe93S猫头猫 } catch (e: any) { 161b43683eaS猫头猫 console.log(e); 162927dbe93S猫头猫 this.state = 'error'; 163927dbe93S猫头猫 this.stateCode = PluginStateCode.CannotParse; 164927dbe93S猫头猫 if (e?.stateCode) { 165927dbe93S猫头猫 this.stateCode = e.stateCode; 166927dbe93S猫头猫 } 167927dbe93S猫头猫 errorLog(`${pluginPath}插件无法解析 `, { 168927dbe93S猫头猫 stateCode: this.stateCode, 169927dbe93S猫头猫 message: e?.message, 170927dbe93S猫头猫 stack: e?.stack, 171927dbe93S猫头猫 }); 172927dbe93S猫头猫 _instance = e?.instance ?? { 173927dbe93S猫头猫 _path: '', 174927dbe93S猫头猫 platform: '', 175927dbe93S猫头猫 appVersion: '', 17620e6a092S猫头猫 async getMediaSource() { 177927dbe93S猫头猫 return null; 178927dbe93S猫头猫 }, 179927dbe93S猫头猫 async search() { 180927dbe93S猫头猫 return {}; 181927dbe93S猫头猫 }, 182927dbe93S猫头猫 async getAlbumInfo() { 183927dbe93S猫头猫 return null; 184927dbe93S猫头猫 }, 185927dbe93S猫头猫 }; 186927dbe93S猫头猫 } 187927dbe93S猫头猫 this.instance = _instance; 188927dbe93S猫头猫 this.path = pluginPath; 189927dbe93S猫头猫 this.name = _instance.platform; 190ab8941d9S猫头猫 if ( 191ab8941d9S猫头猫 this.instance.platform === '' || 192ab8941d9S猫头猫 this.instance.platform === undefined 193ab8941d9S猫头猫 ) { 194927dbe93S猫头猫 this.hash = ''; 195927dbe93S猫头猫 } else { 19674d0cf81S猫头猫 if (typeof funcCode === 'string') { 197927dbe93S猫头猫 this.hash = sha256(funcCode).toString(); 19874d0cf81S猫头猫 } else { 19974d0cf81S猫头猫 this.hash = sha256(funcCode.toString()).toString(); 20074d0cf81S猫头猫 } 201927dbe93S猫头猫 } 202927dbe93S猫头猫 203927dbe93S猫头猫 // 放在最后 204927dbe93S猫头猫 this.methods = new PluginMethods(this); 205927dbe93S猫头猫 } 206927dbe93S猫头猫 207927dbe93S猫头猫 private checkValid(_instance: IPlugin.IPluginInstance) { 208927dbe93S猫头猫 /** 版本号校验 */ 209927dbe93S猫头猫 if ( 210927dbe93S猫头猫 _instance.appVersion && 211927dbe93S猫头猫 !satisfies(DeviceInfo.getVersion(), _instance.appVersion) 212927dbe93S猫头猫 ) { 213927dbe93S猫头猫 throw { 214927dbe93S猫头猫 instance: _instance, 215927dbe93S猫头猫 stateCode: PluginStateCode.VersionNotMatch, 216927dbe93S猫头猫 }; 217927dbe93S猫头猫 } 218927dbe93S猫头猫 return true; 219927dbe93S猫头猫 } 220927dbe93S猫头猫} 221d5bfeb7eS猫头猫//#endregion 222927dbe93S猫头猫 223d5bfeb7eS猫头猫//#region 基于插件类封装的方法,供给APP侧直接调用 224927dbe93S猫头猫/** 有缓存等信息 */ 225927dbe93S猫头猫class PluginMethods implements IPlugin.IPluginInstanceMethods { 226927dbe93S猫头猫 private plugin; 227927dbe93S猫头猫 constructor(plugin: Plugin) { 228927dbe93S猫头猫 this.plugin = plugin; 229927dbe93S猫头猫 } 230927dbe93S猫头猫 /** 搜索 */ 231927dbe93S猫头猫 async search<T extends ICommon.SupportMediaType>( 232927dbe93S猫头猫 query: string, 233927dbe93S猫头猫 page: number, 234927dbe93S猫头猫 type: T, 235927dbe93S猫头猫 ): Promise<IPlugin.ISearchResult<T>> { 236927dbe93S猫头猫 if (!this.plugin.instance.search) { 237927dbe93S猫头猫 return { 238927dbe93S猫头猫 isEnd: true, 239927dbe93S猫头猫 data: [], 240927dbe93S猫头猫 }; 241927dbe93S猫头猫 } 242927dbe93S猫头猫 2434060c00aS猫头猫 const result = 2444060c00aS猫头猫 (await this.plugin.instance.search(query, page, type)) ?? {}; 245927dbe93S猫头猫 if (Array.isArray(result.data)) { 246927dbe93S猫头猫 result.data.forEach(_ => { 247927dbe93S猫头猫 resetMediaItem(_, this.plugin.name); 248927dbe93S猫头猫 }); 249927dbe93S猫头猫 return { 250927dbe93S猫头猫 isEnd: result.isEnd ?? true, 251927dbe93S猫头猫 data: result.data, 252927dbe93S猫头猫 }; 253927dbe93S猫头猫 } 254927dbe93S猫头猫 return { 255927dbe93S猫头猫 isEnd: true, 256927dbe93S猫头猫 data: [], 257927dbe93S猫头猫 }; 258927dbe93S猫头猫 } 259927dbe93S猫头猫 260927dbe93S猫头猫 /** 获取真实源 */ 26120e6a092S猫头猫 async getMediaSource( 262927dbe93S猫头猫 musicItem: IMusic.IMusicItemBase, 263abaede57S猫头猫 quality: IMusic.IQualityKey = 'standard', 264927dbe93S猫头猫 retryCount = 1, 265dc160d50S猫头猫 notUpdateCache = false, 266192ae2b0S猫头猫 ): Promise<IPlugin.IMediaSourceResult | null> { 267927dbe93S猫头猫 // 1. 本地搜索 其实直接读mediameta就好了 268927dbe93S猫头猫 const localPath = 2690e4173cdS猫头猫 getInternalData<string>(musicItem, InternalDataType.LOCALPATH) ?? 2700e4173cdS猫头猫 getInternalData<string>( 2710e4173cdS猫头猫 LocalMusicSheet.isLocalMusic(musicItem), 2720e4173cdS猫头猫 InternalDataType.LOCALPATH, 2730e4173cdS猫头猫 ); 274a84a85c5S猫头猫 if ( 275a84a85c5S猫头猫 localPath && 276a84a85c5S猫头猫 (localPath.startsWith('content://') || 277a84a85c5S猫头猫 (await FileSystem.exists(localPath))) 278a84a85c5S猫头猫 ) { 2790e4173cdS猫头猫 trace('本地播放', localPath); 280927dbe93S猫头猫 return { 281927dbe93S猫头猫 url: localPath, 282927dbe93S猫头猫 }; 283927dbe93S猫头猫 } 284a84a85c5S猫头猫 console.log('BFFF2'); 285a84a85c5S猫头猫 2867993f90eS猫头猫 if (musicItem.platform === localPluginPlatform) { 287f5935920S猫头猫 throw new Error('本地音乐不存在'); 288f5935920S猫头猫 } 289927dbe93S猫头猫 // 2. 缓存播放 290927dbe93S猫头猫 const mediaCache = Cache.get(musicItem); 291985f8e75S猫头猫 const pluginCacheControl = 292985f8e75S猫头猫 this.plugin.instance.cacheControl ?? 'no-cache'; 293cfa0fc07S猫头猫 if ( 294cfa0fc07S猫头猫 mediaCache && 295abaede57S猫头猫 mediaCache?.qualities?.[quality]?.url && 29648f4b873S猫头猫 (pluginCacheControl === CacheControl.Cache || 29748f4b873S猫头猫 (pluginCacheControl === CacheControl.NoCache && 298ef714860S猫头猫 Network.isOffline())) 299cfa0fc07S猫头猫 ) { 3005276aef9S猫头猫 trace('播放', '缓存播放'); 301abaede57S猫头猫 const qualityInfo = mediaCache.qualities[quality]; 302927dbe93S猫头猫 return { 303abaede57S猫头猫 url: qualityInfo.url, 304927dbe93S猫头猫 headers: mediaCache.headers, 3054060c00aS猫头猫 userAgent: 3064060c00aS猫头猫 mediaCache.userAgent ?? mediaCache.headers?.['user-agent'], 307927dbe93S猫头猫 }; 308927dbe93S猫头猫 } 309927dbe93S猫头猫 // 3. 插件解析 31020e6a092S猫头猫 if (!this.plugin.instance.getMediaSource) { 311abaede57S猫头猫 return {url: musicItem?.qualities?.[quality]?.url ?? musicItem.url}; 312927dbe93S猫头猫 } 313927dbe93S猫头猫 try { 314abaede57S猫头猫 const {url, headers} = (await this.plugin.instance.getMediaSource( 315abaede57S猫头猫 musicItem, 316abaede57S猫头猫 quality, 317abaede57S猫头猫 )) ?? {url: musicItem?.qualities?.[quality]?.url}; 318927dbe93S猫头猫 if (!url) { 319a28eac61S猫头猫 throw new Error('NOT RETRY'); 320927dbe93S猫头猫 } 3215276aef9S猫头猫 trace('播放', '插件播放'); 322927dbe93S猫头猫 const result = { 323927dbe93S猫头猫 url, 324927dbe93S猫头猫 headers, 325927dbe93S猫头猫 userAgent: headers?.['user-agent'], 326cfa0fc07S猫头猫 } as IPlugin.IMediaSourceResult; 327927dbe93S猫头猫 328dc160d50S猫头猫 if ( 329dc160d50S猫头猫 pluginCacheControl !== CacheControl.NoStore && 330dc160d50S猫头猫 !notUpdateCache 331dc160d50S猫头猫 ) { 332abaede57S猫头猫 Cache.update(musicItem, [ 333abaede57S猫头猫 ['headers', result.headers], 334abaede57S猫头猫 ['userAgent', result.userAgent], 335abaede57S猫头猫 [`qualities.${quality}.url`, url], 336abaede57S猫头猫 ]); 337752ffc5aS猫头猫 } 338cfa0fc07S猫头猫 339927dbe93S猫头猫 return result; 340927dbe93S猫头猫 } catch (e: any) { 341a28eac61S猫头猫 if (retryCount > 0 && e?.message !== 'NOT RETRY') { 342927dbe93S猫头猫 await delay(150); 343abaede57S猫头猫 return this.getMediaSource(musicItem, quality, --retryCount); 344927dbe93S猫头猫 } 345927dbe93S猫头猫 errorLog('获取真实源失败', e?.message); 346ea6d708fS猫头猫 devLog('error', '获取真实源失败', e, e?.message); 347192ae2b0S猫头猫 return null; 348927dbe93S猫头猫 } 349927dbe93S猫头猫 } 350927dbe93S猫头猫 351927dbe93S猫头猫 /** 获取音乐详情 */ 352927dbe93S猫头猫 async getMusicInfo( 353927dbe93S猫头猫 musicItem: ICommon.IMediaBase, 35474d0cf81S猫头猫 ): Promise<Partial<IMusic.IMusicItem> | null> { 355927dbe93S猫头猫 if (!this.plugin.instance.getMusicInfo) { 356d704daedS猫头猫 return null; 357927dbe93S猫头猫 } 35874d0cf81S猫头猫 try { 359927dbe93S猫头猫 return ( 360927dbe93S猫头猫 this.plugin.instance.getMusicInfo( 3617993f90eS猫头猫 resetMediaItem(musicItem, undefined, true), 362d704daedS猫头猫 ) ?? null 363927dbe93S猫头猫 ); 364ea6d708fS猫头猫 } catch (e: any) { 365ea6d708fS猫头猫 devLog('error', '获取音乐详情失败', e, e?.message); 366d704daedS猫头猫 return null; 36774d0cf81S猫头猫 } 368927dbe93S猫头猫 } 369927dbe93S猫头猫 370927dbe93S猫头猫 /** 获取歌词 */ 371927dbe93S猫头猫 async getLyric( 372927dbe93S猫头猫 musicItem: IMusic.IMusicItemBase, 373927dbe93S猫头猫 from?: IMusic.IMusicItemBase, 374927dbe93S猫头猫 ): Promise<ILyric.ILyricSource | null> { 375927dbe93S猫头猫 // 1.额外存储的meta信息 376927dbe93S猫头猫 const meta = MediaMeta.get(musicItem); 377927dbe93S猫头猫 if (meta && meta.associatedLrc) { 378927dbe93S猫头猫 // 有关联歌词 379927dbe93S猫头猫 if ( 380927dbe93S猫头猫 isSameMediaItem(musicItem, from) || 381927dbe93S猫头猫 isSameMediaItem(meta.associatedLrc, musicItem) 382927dbe93S猫头猫 ) { 383927dbe93S猫头猫 // 形成环路,断开当前的环 384927dbe93S猫头猫 await MediaMeta.update(musicItem, { 385927dbe93S猫头猫 associatedLrc: undefined, 386927dbe93S猫头猫 }); 387927dbe93S猫头猫 // 无歌词 388927dbe93S猫头猫 return null; 389927dbe93S猫头猫 } 390927dbe93S猫头猫 // 获取关联歌词 3917a91f04fS猫头猫 const associatedMeta = MediaMeta.get(meta.associatedLrc) ?? {}; 3924060c00aS猫头猫 const result = await this.getLyric( 3937a91f04fS猫头猫 {...meta.associatedLrc, ...associatedMeta}, 3944060c00aS猫头猫 from ?? musicItem, 3954060c00aS猫头猫 ); 396927dbe93S猫头猫 if (result) { 397927dbe93S猫头猫 // 如果有关联歌词,就返回关联歌词,深度优先 398927dbe93S猫头猫 return result; 399927dbe93S猫头猫 } 400927dbe93S猫头猫 } 401927dbe93S猫头猫 const cache = Cache.get(musicItem); 402927dbe93S猫头猫 let rawLrc = meta?.rawLrc || musicItem.rawLrc || cache?.rawLrc; 403927dbe93S猫头猫 let lrcUrl = meta?.lrc || musicItem.lrc || cache?.lrc; 404927dbe93S猫头猫 // 如果存在文本 405927dbe93S猫头猫 if (rawLrc) { 406927dbe93S猫头猫 return { 407927dbe93S猫头猫 rawLrc, 408927dbe93S猫头猫 lrc: lrcUrl, 409927dbe93S猫头猫 }; 410927dbe93S猫头猫 } 411927dbe93S猫头猫 // 2.本地缓存 412927dbe93S猫头猫 const localLrc = 4130e4173cdS猫头猫 meta?.[internalSerializeKey]?.local?.localLrc || 4140e4173cdS猫头猫 cache?.[internalSerializeKey]?.local?.localLrc; 415927dbe93S猫头猫 if (localLrc && (await exists(localLrc))) { 416927dbe93S猫头猫 rawLrc = await readFile(localLrc, 'utf8'); 417927dbe93S猫头猫 return { 418927dbe93S猫头猫 rawLrc, 419927dbe93S猫头猫 lrc: lrcUrl, 420927dbe93S猫头猫 }; 421927dbe93S猫头猫 } 422927dbe93S猫头猫 // 3.优先使用url 423927dbe93S猫头猫 if (lrcUrl) { 424927dbe93S猫头猫 try { 425927dbe93S猫头猫 // 需要超时时间 axios timeout 但是没生效 42661aca335S猫头猫 rawLrc = (await axios.get(lrcUrl, {timeout: 2000})).data; 427927dbe93S猫头猫 return { 428927dbe93S猫头猫 rawLrc, 429927dbe93S猫头猫 lrc: lrcUrl, 430927dbe93S猫头猫 }; 431927dbe93S猫头猫 } catch { 432927dbe93S猫头猫 lrcUrl = undefined; 433927dbe93S猫头猫 } 434927dbe93S猫头猫 } 435927dbe93S猫头猫 // 4. 如果地址失效 436927dbe93S猫头猫 if (!lrcUrl) { 437927dbe93S猫头猫 // 插件获得url 438927dbe93S猫头猫 try { 4397a91f04fS猫头猫 let lrcSource; 4407a91f04fS猫头猫 if (from) { 4417a91f04fS猫头猫 lrcSource = await PluginManager.getByMedia( 4427a91f04fS猫头猫 musicItem, 4437a91f04fS猫头猫 )?.instance?.getLyric?.( 444927dbe93S猫头猫 resetMediaItem(musicItem, undefined, true), 445927dbe93S猫头猫 ); 4467a91f04fS猫头猫 } else { 4477a91f04fS猫头猫 lrcSource = await this.plugin.instance?.getLyric?.( 4487a91f04fS猫头猫 resetMediaItem(musicItem, undefined, true), 4497a91f04fS猫头猫 ); 4507a91f04fS猫头猫 } 4517a91f04fS猫头猫 452927dbe93S猫头猫 rawLrc = lrcSource?.rawLrc; 453927dbe93S猫头猫 lrcUrl = lrcSource?.lrc; 454927dbe93S猫头猫 } catch (e: any) { 455927dbe93S猫头猫 trace('插件获取歌词失败', e?.message, 'error'); 456ea6d708fS猫头猫 devLog('error', '插件获取歌词失败', e, e?.message); 457927dbe93S猫头猫 } 458927dbe93S猫头猫 } 459927dbe93S猫头猫 // 5. 最后一次请求 460927dbe93S猫头猫 if (rawLrc || lrcUrl) { 461927dbe93S猫头猫 const filename = `${pathConst.lrcCachePath}${nanoid()}.lrc`; 462927dbe93S猫头猫 if (lrcUrl) { 463927dbe93S猫头猫 try { 46461aca335S猫头猫 rawLrc = (await axios.get(lrcUrl, {timeout: 2000})).data; 465927dbe93S猫头猫 } catch {} 466927dbe93S猫头猫 } 467927dbe93S猫头猫 if (rawLrc) { 468927dbe93S猫头猫 await writeFile(filename, rawLrc, 'utf8'); 469927dbe93S猫头猫 // 写入缓存 470927dbe93S猫头猫 Cache.update(musicItem, [ 4710e4173cdS猫头猫 [`${internalSerializeKey}.local.localLrc`, filename], 472927dbe93S猫头猫 ]); 473927dbe93S猫头猫 // 如果有meta 474927dbe93S猫头猫 if (meta) { 475927dbe93S猫头猫 MediaMeta.update(musicItem, [ 4760e4173cdS猫头猫 [`${internalSerializeKey}.local.localLrc`, filename], 477927dbe93S猫头猫 ]); 478927dbe93S猫头猫 } 479927dbe93S猫头猫 return { 480927dbe93S猫头猫 rawLrc, 481927dbe93S猫头猫 lrc: lrcUrl, 482927dbe93S猫头猫 }; 483927dbe93S猫头猫 } 484927dbe93S猫头猫 } 4853a6f67b1S猫头猫 // 6. 如果是本地文件 4863a6f67b1S猫头猫 const isDownloaded = LocalMusicSheet.isLocalMusic(musicItem); 4873a6f67b1S猫头猫 if (musicItem.platform !== localPluginPlatform && isDownloaded) { 4883a6f67b1S猫头猫 const res = await localFilePlugin.instance!.getLyric!(isDownloaded); 4893a6f67b1S猫头猫 if (res) { 4903a6f67b1S猫头猫 return res; 4913a6f67b1S猫头猫 } 4923a6f67b1S猫头猫 } 493ea6d708fS猫头猫 devLog('warn', '无歌词'); 494927dbe93S猫头猫 495927dbe93S猫头猫 return null; 496927dbe93S猫头猫 } 497927dbe93S猫头猫 498927dbe93S猫头猫 /** 获取歌词文本 */ 499927dbe93S猫头猫 async getLyricText( 500927dbe93S猫头猫 musicItem: IMusic.IMusicItem, 501927dbe93S猫头猫 ): Promise<string | undefined> { 502927dbe93S猫头猫 return (await this.getLyric(musicItem))?.rawLrc; 503927dbe93S猫头猫 } 504927dbe93S猫头猫 505927dbe93S猫头猫 /** 获取专辑信息 */ 506927dbe93S猫头猫 async getAlbumInfo( 507927dbe93S猫头猫 albumItem: IAlbum.IAlbumItemBase, 508f9afcc0dS猫头猫 page: number = 1, 509f9afcc0dS猫头猫 ): Promise<IPlugin.IAlbumInfoResult | null> { 510927dbe93S猫头猫 if (!this.plugin.instance.getAlbumInfo) { 511f9afcc0dS猫头猫 return { 512f9afcc0dS猫头猫 albumItem, 513f2a4767cS猫头猫 musicList: (albumItem?.musicList ?? []).map( 514f2a4767cS猫头猫 resetMediaItem, 515f2a4767cS猫头猫 this.plugin.name, 516f2a4767cS猫头猫 true, 517f2a4767cS猫头猫 ), 518f9afcc0dS猫头猫 isEnd: true, 519f9afcc0dS猫头猫 }; 520927dbe93S猫头猫 } 521927dbe93S猫头猫 try { 522927dbe93S猫头猫 const result = await this.plugin.instance.getAlbumInfo( 523927dbe93S猫头猫 resetMediaItem(albumItem, undefined, true), 524f9afcc0dS猫头猫 page, 525927dbe93S猫头猫 ); 5265276aef9S猫头猫 if (!result) { 5275276aef9S猫头猫 throw new Error(); 5285276aef9S猫头猫 } 529927dbe93S猫头猫 result?.musicList?.forEach(_ => { 530927dbe93S猫头猫 resetMediaItem(_, this.plugin.name); 53196744680S猫头猫 _.album = albumItem.title; 532927dbe93S猫头猫 }); 5335276aef9S猫头猫 534f9afcc0dS猫头猫 if (page <= 1) { 535f9afcc0dS猫头猫 // 合并信息 536f9afcc0dS猫头猫 return { 537f9afcc0dS猫头猫 albumItem: {...albumItem, ...(result?.albumItem ?? {})}, 538f9afcc0dS猫头猫 isEnd: result.isEnd === false ? false : true, 539f9afcc0dS猫头猫 musicList: result.musicList, 540f9afcc0dS猫头猫 }; 541f9afcc0dS猫头猫 } else { 542f9afcc0dS猫头猫 return { 543f9afcc0dS猫头猫 isEnd: result.isEnd === false ? false : true, 544f9afcc0dS猫头猫 musicList: result.musicList, 545f9afcc0dS猫头猫 }; 546f9afcc0dS猫头猫 } 5474394410dS猫头猫 } catch (e: any) { 5484394410dS猫头猫 trace('获取专辑信息失败', e?.message); 549ea6d708fS猫头猫 devLog('error', '获取专辑信息失败', e, e?.message); 550ea6d708fS猫头猫 551f9afcc0dS猫头猫 return null; 552927dbe93S猫头猫 } 553927dbe93S猫头猫 } 554927dbe93S猫头猫 5555830c002S猫头猫 /** 获取歌单信息 */ 5565830c002S猫头猫 async getMusicSheetInfo( 5575830c002S猫头猫 sheetItem: IMusic.IMusicSheetItem, 5585830c002S猫头猫 page: number = 1, 5595830c002S猫头猫 ): Promise<IPlugin.ISheetInfoResult | null> { 5605281926bS猫头猫 if (!this.plugin.instance.getMusicSheetInfo) { 5615830c002S猫头猫 return { 5625830c002S猫头猫 sheetItem, 5635830c002S猫头猫 musicList: sheetItem?.musicList ?? [], 5645830c002S猫头猫 isEnd: true, 5655830c002S猫头猫 }; 5665830c002S猫头猫 } 5675830c002S猫头猫 try { 5685830c002S猫头猫 const result = await this.plugin.instance?.getMusicSheetInfo?.( 5695830c002S猫头猫 resetMediaItem(sheetItem, undefined, true), 5705830c002S猫头猫 page, 5715830c002S猫头猫 ); 5725830c002S猫头猫 if (!result) { 5735830c002S猫头猫 throw new Error(); 5745830c002S猫头猫 } 5755830c002S猫头猫 result?.musicList?.forEach(_ => { 5765830c002S猫头猫 resetMediaItem(_, this.plugin.name); 5775830c002S猫头猫 }); 5785830c002S猫头猫 5795830c002S猫头猫 if (page <= 1) { 5805830c002S猫头猫 // 合并信息 5815830c002S猫头猫 return { 5825830c002S猫头猫 sheetItem: {...sheetItem, ...(result?.sheetItem ?? {})}, 5835830c002S猫头猫 isEnd: result.isEnd === false ? false : true, 5845830c002S猫头猫 musicList: result.musicList, 5855830c002S猫头猫 }; 5865830c002S猫头猫 } else { 5875830c002S猫头猫 return { 5885830c002S猫头猫 isEnd: result.isEnd === false ? false : true, 5895830c002S猫头猫 musicList: result.musicList, 5905830c002S猫头猫 }; 5915830c002S猫头猫 } 5925830c002S猫头猫 } catch (e: any) { 5935830c002S猫头猫 trace('获取歌单信息失败', e, e?.message); 5945830c002S猫头猫 devLog('error', '获取歌单信息失败', e, e?.message); 5955830c002S猫头猫 5965830c002S猫头猫 return null; 5975830c002S猫头猫 } 5985830c002S猫头猫 } 5995830c002S猫头猫 600927dbe93S猫头猫 /** 查询作者信息 */ 601efb9da24S猫头猫 async getArtistWorks<T extends IArtist.ArtistMediaType>( 602927dbe93S猫头猫 artistItem: IArtist.IArtistItem, 603927dbe93S猫头猫 page: number, 604927dbe93S猫头猫 type: T, 605927dbe93S猫头猫 ): Promise<IPlugin.ISearchResult<T>> { 606efb9da24S猫头猫 if (!this.plugin.instance.getArtistWorks) { 607927dbe93S猫头猫 return { 608927dbe93S猫头猫 isEnd: true, 609927dbe93S猫头猫 data: [], 610927dbe93S猫头猫 }; 611927dbe93S猫头猫 } 612927dbe93S猫头猫 try { 613efb9da24S猫头猫 const result = await this.plugin.instance.getArtistWorks( 614927dbe93S猫头猫 artistItem, 615927dbe93S猫头猫 page, 616927dbe93S猫头猫 type, 617927dbe93S猫头猫 ); 618927dbe93S猫头猫 if (!result.data) { 619927dbe93S猫头猫 return { 620927dbe93S猫头猫 isEnd: true, 621927dbe93S猫头猫 data: [], 622927dbe93S猫头猫 }; 623927dbe93S猫头猫 } 624927dbe93S猫头猫 result.data?.forEach(_ => resetMediaItem(_, this.plugin.name)); 625927dbe93S猫头猫 return { 626927dbe93S猫头猫 isEnd: result.isEnd ?? true, 627927dbe93S猫头猫 data: result.data, 628927dbe93S猫头猫 }; 6294394410dS猫头猫 } catch (e: any) { 6304394410dS猫头猫 trace('查询作者信息失败', e?.message); 631ea6d708fS猫头猫 devLog('error', '查询作者信息失败', e, e?.message); 632ea6d708fS猫头猫 633927dbe93S猫头猫 throw e; 634927dbe93S猫头猫 } 635927dbe93S猫头猫 } 63608380090S猫头猫 63708380090S猫头猫 /** 导入歌单 */ 63808380090S猫头猫 async importMusicSheet(urlLike: string): Promise<IMusic.IMusicItem[]> { 63908380090S猫头猫 try { 64008380090S猫头猫 const result = 64108380090S猫头猫 (await this.plugin.instance?.importMusicSheet?.(urlLike)) ?? []; 64208380090S猫头猫 result.forEach(_ => resetMediaItem(_, this.plugin.name)); 64308380090S猫头猫 return result; 644ea6d708fS猫头猫 } catch (e: any) { 6450e4173cdS猫头猫 console.log(e); 646ea6d708fS猫头猫 devLog('error', '导入歌单失败', e, e?.message); 647ea6d708fS猫头猫 64808380090S猫头猫 return []; 64908380090S猫头猫 } 65008380090S猫头猫 } 6514d9d3c4cS猫头猫 /** 导入单曲 */ 6524d9d3c4cS猫头猫 async importMusicItem(urlLike: string): Promise<IMusic.IMusicItem | null> { 6534d9d3c4cS猫头猫 try { 6544d9d3c4cS猫头猫 const result = await this.plugin.instance?.importMusicItem?.( 6554d9d3c4cS猫头猫 urlLike, 6564d9d3c4cS猫头猫 ); 6574d9d3c4cS猫头猫 if (!result) { 6584d9d3c4cS猫头猫 throw new Error(); 6594d9d3c4cS猫头猫 } 6604d9d3c4cS猫头猫 resetMediaItem(result, this.plugin.name); 6614d9d3c4cS猫头猫 return result; 662ea6d708fS猫头猫 } catch (e: any) { 663ea6d708fS猫头猫 devLog('error', '导入单曲失败', e, e?.message); 664ea6d708fS猫头猫 6654d9d3c4cS猫头猫 return null; 6664d9d3c4cS猫头猫 } 6674d9d3c4cS猫头猫 } 668d52aa40eS猫头猫 /** 获取榜单 */ 66992b6c95aS猫头猫 async getTopLists(): Promise<IMusic.IMusicSheetGroupItem[]> { 670d52aa40eS猫头猫 try { 671d52aa40eS猫头猫 const result = await this.plugin.instance?.getTopLists?.(); 672d52aa40eS猫头猫 if (!result) { 673d52aa40eS猫头猫 throw new Error(); 674d52aa40eS猫头猫 } 675d52aa40eS猫头猫 return result; 676d52aa40eS猫头猫 } catch (e: any) { 677d52aa40eS猫头猫 devLog('error', '获取榜单失败', e, e?.message); 678d52aa40eS猫头猫 return []; 679d52aa40eS猫头猫 } 680d52aa40eS猫头猫 } 681d52aa40eS猫头猫 /** 获取榜单详情 */ 682d52aa40eS猫头猫 async getTopListDetail( 68392b6c95aS猫头猫 topListItem: IMusic.IMusicSheetItemBase, 684*956ee1b7S猫头猫 page: number, 685*956ee1b7S猫头猫 ): Promise<IPlugin.ITopListInfoResult> { 686d52aa40eS猫头猫 try { 687d52aa40eS猫头猫 const result = await this.plugin.instance?.getTopListDetail?.( 688d52aa40eS猫头猫 topListItem, 689*956ee1b7S猫头猫 page, 690d52aa40eS猫头猫 ); 691d52aa40eS猫头猫 if (!result) { 692d52aa40eS猫头猫 throw new Error(); 693d52aa40eS猫头猫 } 694d384662fS猫头猫 if (result.musicList) { 695d384662fS猫头猫 result.musicList.forEach(_ => 696d384662fS猫头猫 resetMediaItem(_, this.plugin.name), 697d384662fS猫头猫 ); 698d384662fS猫头猫 } 699*956ee1b7S猫头猫 if (result.isEnd !== false) { 700*956ee1b7S猫头猫 result.isEnd = true; 701*956ee1b7S猫头猫 } 702d52aa40eS猫头猫 return result; 703d52aa40eS猫头猫 } catch (e: any) { 704d52aa40eS猫头猫 devLog('error', '获取榜单详情失败', e, e?.message); 705d52aa40eS猫头猫 return { 706*956ee1b7S猫头猫 isEnd: true, 707*956ee1b7S猫头猫 topListItem: topListItem as IMusic.IMusicSheetItem, 708d52aa40eS猫头猫 musicList: [], 709d52aa40eS猫头猫 }; 710d52aa40eS猫头猫 } 711d52aa40eS猫头猫 } 712ceb900cdS猫头猫 7135830c002S猫头猫 /** 获取推荐歌单的tag */ 714ceb900cdS猫头猫 async getRecommendSheetTags(): Promise<IPlugin.IGetRecommendSheetTagsResult> { 715ceb900cdS猫头猫 try { 716ceb900cdS猫头猫 const result = 717ceb900cdS猫头猫 await this.plugin.instance?.getRecommendSheetTags?.(); 718ceb900cdS猫头猫 if (!result) { 719ceb900cdS猫头猫 throw new Error(); 720ceb900cdS猫头猫 } 721ceb900cdS猫头猫 return result; 722ceb900cdS猫头猫 } catch (e: any) { 723ceb900cdS猫头猫 devLog('error', '获取推荐歌单失败', e, e?.message); 724ceb900cdS猫头猫 return { 725ceb900cdS猫头猫 data: [], 726ceb900cdS猫头猫 }; 727ceb900cdS猫头猫 } 728ceb900cdS猫头猫 } 7295830c002S猫头猫 /** 获取某个tag的推荐歌单 */ 730ceb900cdS猫头猫 async getRecommendSheetsByTag( 731ceb900cdS猫头猫 tagItem: ICommon.IUnique, 732ceb900cdS猫头猫 page?: number, 733ceb900cdS猫头猫 ): Promise<ICommon.PaginationResponse<IMusic.IMusicSheetItemBase>> { 734ceb900cdS猫头猫 try { 735ceb900cdS猫头猫 const result = 736ceb900cdS猫头猫 await this.plugin.instance?.getRecommendSheetsByTag?.( 737ceb900cdS猫头猫 tagItem, 738ceb900cdS猫头猫 page ?? 1, 739ceb900cdS猫头猫 ); 740ceb900cdS猫头猫 if (!result) { 741ceb900cdS猫头猫 throw new Error(); 742ceb900cdS猫头猫 } 743ceb900cdS猫头猫 if (result.isEnd !== false) { 744ceb900cdS猫头猫 result.isEnd = true; 745ceb900cdS猫头猫 } 746ceb900cdS猫头猫 if (!result.data) { 747ceb900cdS猫头猫 result.data = []; 748ceb900cdS猫头猫 } 749ceb900cdS猫头猫 result.data.forEach(item => resetMediaItem(item, this.plugin.name)); 750ceb900cdS猫头猫 751ceb900cdS猫头猫 return result; 752ceb900cdS猫头猫 } catch (e: any) { 753ceb900cdS猫头猫 devLog('error', '获取推荐歌单详情失败', e, e?.message); 754ceb900cdS猫头猫 return { 755ceb900cdS猫头猫 isEnd: true, 756ceb900cdS猫头猫 data: [], 757ceb900cdS猫头猫 }; 758ceb900cdS猫头猫 } 759ceb900cdS猫头猫 } 760927dbe93S猫头猫} 761d5bfeb7eS猫头猫//#endregion 7621a5528a0S猫头猫 763927dbe93S猫头猫let plugins: Array<Plugin> = []; 764927dbe93S猫头猫const pluginStateMapper = new StateMapper(() => plugins); 76574d0cf81S猫头猫 766d5bfeb7eS猫头猫//#region 本地音乐插件 76774d0cf81S猫头猫/** 本地插件 */ 76874d0cf81S猫头猫const localFilePlugin = new Plugin(function () { 7690e4173cdS猫头猫 return { 770d5bfeb7eS猫头猫 platform: localPluginPlatform, 77174d0cf81S猫头猫 _path: '', 77274d0cf81S猫头猫 async getMusicInfo(musicBase) { 77374d0cf81S猫头猫 const localPath = getInternalData<string>( 77474d0cf81S猫头猫 musicBase, 77574d0cf81S猫头猫 InternalDataType.LOCALPATH, 7760e4173cdS猫头猫 ); 77774d0cf81S猫头猫 if (localPath) { 77874d0cf81S猫头猫 const coverImg = await Mp3Util.getMediaCoverImg(localPath); 77974d0cf81S猫头猫 return { 78074d0cf81S猫头猫 artwork: coverImg, 78174d0cf81S猫头猫 }; 78274d0cf81S猫头猫 } 78374d0cf81S猫头猫 return null; 78474d0cf81S猫头猫 }, 7857993f90eS猫头猫 async getLyric(musicBase) { 7867993f90eS猫头猫 const localPath = getInternalData<string>( 7877993f90eS猫头猫 musicBase, 7887993f90eS猫头猫 InternalDataType.LOCALPATH, 7897993f90eS猫头猫 ); 7903a6f67b1S猫头猫 let rawLrc: string | null = null; 7917993f90eS猫头猫 if (localPath) { 7923a6f67b1S猫头猫 // 读取内嵌歌词 7933a6f67b1S猫头猫 try { 7943a6f67b1S猫头猫 rawLrc = await Mp3Util.getLyric(localPath); 7953a6f67b1S猫头猫 } catch (e) { 796a84a85c5S猫头猫 console.log('读取内嵌歌词失败', e); 7977993f90eS猫头猫 } 7983a6f67b1S猫头猫 if (!rawLrc) { 7993a6f67b1S猫头猫 // 读取配置歌词 8003a6f67b1S猫头猫 const lastDot = localPath.lastIndexOf('.'); 8013a6f67b1S猫头猫 const lrcPath = localPath.slice(0, lastDot) + '.lrc'; 8023a6f67b1S猫头猫 8033a6f67b1S猫头猫 try { 8043a6f67b1S猫头猫 if (await exists(lrcPath)) { 8053a6f67b1S猫头猫 rawLrc = await readFile(lrcPath, 'utf8'); 8063a6f67b1S猫头猫 } 8073a6f67b1S猫头猫 } catch {} 8083a6f67b1S猫头猫 } 8093a6f67b1S猫头猫 } 8103a6f67b1S猫头猫 8113a6f67b1S猫头猫 return rawLrc 8123a6f67b1S猫头猫 ? { 8133a6f67b1S猫头猫 rawLrc, 8143a6f67b1S猫头猫 } 8153a6f67b1S猫头猫 : null; 8167993f90eS猫头猫 }, 817a84a85c5S猫头猫 async importMusicItem(urlLike) { 818a84a85c5S猫头猫 let meta: any = {}; 819a84a85c5S猫头猫 try { 820a84a85c5S猫头猫 meta = await Mp3Util.getBasicMeta(urlLike); 821a84a85c5S猫头猫 } catch {} 822a84a85c5S猫头猫 const id = await FileSystem.hash(urlLike, 'MD5'); 823a84a85c5S猫头猫 return { 824a84a85c5S猫头猫 id: id, 825a84a85c5S猫头猫 platform: '本地', 826a84a85c5S猫头猫 title: meta?.title ?? getFileName(urlLike), 827a84a85c5S猫头猫 artist: meta?.artist ?? '未知歌手', 828a84a85c5S猫头猫 duration: parseInt(meta?.duration ?? '0') / 1000, 829a84a85c5S猫头猫 album: meta?.album ?? '未知专辑', 830a84a85c5S猫头猫 artwork: '', 831a84a85c5S猫头猫 [internalSerializeKey]: { 832a84a85c5S猫头猫 localPath: urlLike, 833a84a85c5S猫头猫 }, 834a84a85c5S猫头猫 }; 835a84a85c5S猫头猫 }, 83674d0cf81S猫头猫 }; 83774d0cf81S猫头猫}, ''); 8387993f90eS猫头猫localFilePlugin.hash = localPluginHash; 839927dbe93S猫头猫 840d5bfeb7eS猫头猫//#endregion 841d5bfeb7eS猫头猫 842927dbe93S猫头猫async function setup() { 843927dbe93S猫头猫 const _plugins: Array<Plugin> = []; 844927dbe93S猫头猫 try { 845927dbe93S猫头猫 // 加载插件 846927dbe93S猫头猫 const pluginsPaths = await readDir(pathConst.pluginPath); 847927dbe93S猫头猫 for (let i = 0; i < pluginsPaths.length; ++i) { 848927dbe93S猫头猫 const _pluginUrl = pluginsPaths[i]; 8491e263108S猫头猫 trace('初始化插件', _pluginUrl); 8501e263108S猫头猫 if ( 8511e263108S猫头猫 _pluginUrl.isFile() && 8521e263108S猫头猫 (_pluginUrl.name?.endsWith?.('.js') || 8531e263108S猫头猫 _pluginUrl.path?.endsWith?.('.js')) 8541e263108S猫头猫 ) { 855927dbe93S猫头猫 const funcCode = await readFile(_pluginUrl.path, 'utf8'); 856927dbe93S猫头猫 const plugin = new Plugin(funcCode, _pluginUrl.path); 8574060c00aS猫头猫 const _pluginIndex = _plugins.findIndex( 8584060c00aS猫头猫 p => p.hash === plugin.hash, 8594060c00aS猫头猫 ); 860927dbe93S猫头猫 if (_pluginIndex !== -1) { 861927dbe93S猫头猫 // 重复插件,直接忽略 8620c266394S猫头猫 continue; 863927dbe93S猫头猫 } 864927dbe93S猫头猫 plugin.hash !== '' && _plugins.push(plugin); 865927dbe93S猫头猫 } 866927dbe93S猫头猫 } 867927dbe93S猫头猫 868927dbe93S猫头猫 plugins = _plugins; 869e08d37a3S猫头猫 /** 初始化meta信息 */ 870c2b3a262S猫头猫 await PluginMeta.setupMeta(plugins.map(_ => _.name)); 871c2b3a262S猫头猫 /** 查看一下是否有禁用的标记 */ 872c2b3a262S猫头猫 const allMeta = PluginMeta.getPluginMetaAll() ?? {}; 873c2b3a262S猫头猫 for (let plugin of plugins) { 874c2b3a262S猫头猫 if (allMeta[plugin.name]?.enabled === false) { 875c2b3a262S猫头猫 plugin.state = 'disabled'; 876c2b3a262S猫头猫 } 877c2b3a262S猫头猫 } 878c2b3a262S猫头猫 pluginStateMapper.notify(); 879927dbe93S猫头猫 } catch (e: any) { 8804060c00aS猫头猫 ToastAndroid.show( 8814060c00aS猫头猫 `插件初始化失败:${e?.message ?? e}`, 8824060c00aS猫头猫 ToastAndroid.LONG, 8834060c00aS猫头猫 ); 8841a5528a0S猫头猫 errorLog('插件初始化失败', e?.message); 885927dbe93S猫头猫 throw e; 886927dbe93S猫头猫 } 887927dbe93S猫头猫} 888927dbe93S猫头猫 889e36e2599S猫头猫interface IInstallPluginConfig { 890e36e2599S猫头猫 notCheckVersion?: boolean; 891e36e2599S猫头猫} 892e36e2599S猫头猫 893927dbe93S猫头猫// 安装插件 894e36e2599S猫头猫async function installPlugin( 895e36e2599S猫头猫 pluginPath: string, 896e36e2599S猫头猫 config?: IInstallPluginConfig, 897e36e2599S猫头猫) { 89822c09412S猫头猫 // if (pluginPath.endsWith('.js')) { 899927dbe93S猫头猫 const funcCode = await readFile(pluginPath, 'utf8'); 900e36e2599S猫头猫 901e36e2599S猫头猫 if (funcCode) { 902927dbe93S猫头猫 const plugin = new Plugin(funcCode, pluginPath); 903927dbe93S猫头猫 const _pluginIndex = plugins.findIndex(p => p.hash === plugin.hash); 904927dbe93S猫头猫 if (_pluginIndex !== -1) { 905e36e2599S猫头猫 // 静默忽略 906e36e2599S猫头猫 return plugin; 907927dbe93S猫头猫 } 908e36e2599S猫头猫 const oldVersionPlugin = plugins.find(p => p.name === plugin.name); 909e36e2599S猫头猫 if (oldVersionPlugin && !config?.notCheckVersion) { 910e36e2599S猫头猫 if ( 911e36e2599S猫头猫 compare( 912e36e2599S猫头猫 oldVersionPlugin.instance.version ?? '', 913e36e2599S猫头猫 plugin.instance.version ?? '', 914e36e2599S猫头猫 '>', 915e36e2599S猫头猫 ) 916e36e2599S猫头猫 ) { 917e36e2599S猫头猫 throw new Error('已安装更新版本的插件'); 918e36e2599S猫头猫 } 919e36e2599S猫头猫 } 920e36e2599S猫头猫 921927dbe93S猫头猫 if (plugin.hash !== '') { 922927dbe93S猫头猫 const fn = nanoid(); 923e36e2599S猫头猫 if (oldVersionPlugin) { 924e36e2599S猫头猫 plugins = plugins.filter(_ => _.hash !== oldVersionPlugin.hash); 925e36e2599S猫头猫 try { 926e36e2599S猫头猫 await unlink(oldVersionPlugin.path); 927e36e2599S猫头猫 } catch {} 928e36e2599S猫头猫 } 929927dbe93S猫头猫 const _pluginPath = `${pathConst.pluginPath}${fn}.js`; 930927dbe93S猫头猫 await copyFile(pluginPath, _pluginPath); 931927dbe93S猫头猫 plugin.path = _pluginPath; 932927dbe93S猫头猫 plugins = plugins.concat(plugin); 933927dbe93S猫头猫 pluginStateMapper.notify(); 934a84a85c5S猫头猫 return plugin; 935927dbe93S猫头猫 } 936e36e2599S猫头猫 throw new Error('插件无法解析!'); 937927dbe93S猫头猫 } 938e36e2599S猫头猫 throw new Error('插件无法识别!'); 939c2b3a262S猫头猫} 940c2b3a262S猫头猫 941c2b3a262S猫头猫async function installPluginFromUrl( 942c2b3a262S猫头猫 url: string, 943c2b3a262S猫头猫 config?: IInstallPluginConfig, 944c2b3a262S猫头猫) { 94558992c6bS猫头猫 try { 94658992c6bS猫头猫 const funcCode = (await axios.get(url)).data; 94758992c6bS猫头猫 if (funcCode) { 94858992c6bS猫头猫 const plugin = new Plugin(funcCode, ''); 94958992c6bS猫头猫 const _pluginIndex = plugins.findIndex(p => p.hash === plugin.hash); 95058992c6bS猫头猫 if (_pluginIndex !== -1) { 9518b7ddca8S猫头猫 // 静默忽略 9528b7ddca8S猫头猫 return; 95358992c6bS猫头猫 } 95425c1bd29S猫头猫 const oldVersionPlugin = plugins.find(p => p.name === plugin.name); 955c2b3a262S猫头猫 if (oldVersionPlugin && !config?.notCheckVersion) { 95625c1bd29S猫头猫 if ( 95725c1bd29S猫头猫 compare( 95825c1bd29S猫头猫 oldVersionPlugin.instance.version ?? '', 95925c1bd29S猫头猫 plugin.instance.version ?? '', 96025c1bd29S猫头猫 '>', 96125c1bd29S猫头猫 ) 96225c1bd29S猫头猫 ) { 96325c1bd29S猫头猫 throw new Error('已安装更新版本的插件'); 96425c1bd29S猫头猫 } 96525c1bd29S猫头猫 } 96625c1bd29S猫头猫 96758992c6bS猫头猫 if (plugin.hash !== '') { 96858992c6bS猫头猫 const fn = nanoid(); 96958992c6bS猫头猫 const _pluginPath = `${pathConst.pluginPath}${fn}.js`; 97058992c6bS猫头猫 await writeFile(_pluginPath, funcCode, 'utf8'); 97158992c6bS猫头猫 plugin.path = _pluginPath; 97258992c6bS猫头猫 plugins = plugins.concat(plugin); 97325c1bd29S猫头猫 if (oldVersionPlugin) { 97425c1bd29S猫头猫 plugins = plugins.filter( 97525c1bd29S猫头猫 _ => _.hash !== oldVersionPlugin.hash, 97625c1bd29S猫头猫 ); 97725c1bd29S猫头猫 try { 97825c1bd29S猫头猫 await unlink(oldVersionPlugin.path); 97925c1bd29S猫头猫 } catch {} 98025c1bd29S猫头猫 } 98158992c6bS猫头猫 pluginStateMapper.notify(); 98258992c6bS猫头猫 return; 98358992c6bS猫头猫 } 98474acbfc0S猫头猫 throw new Error('插件无法解析!'); 98558992c6bS猫头猫 } 98625c1bd29S猫头猫 } catch (e: any) { 987ea6d708fS猫头猫 devLog('error', 'URL安装插件失败', e, e?.message); 98858992c6bS猫头猫 errorLog('URL安装插件失败', e); 98925c1bd29S猫头猫 throw new Error(e?.message ?? ''); 99058992c6bS猫头猫 } 99158992c6bS猫头猫} 99258992c6bS猫头猫 993927dbe93S猫头猫/** 卸载插件 */ 994927dbe93S猫头猫async function uninstallPlugin(hash: string) { 995927dbe93S猫头猫 const targetIndex = plugins.findIndex(_ => _.hash === hash); 996927dbe93S猫头猫 if (targetIndex !== -1) { 997927dbe93S猫头猫 try { 99824e5e74aS猫头猫 const pluginName = plugins[targetIndex].name; 999927dbe93S猫头猫 await unlink(plugins[targetIndex].path); 1000927dbe93S猫头猫 plugins = plugins.filter(_ => _.hash !== hash); 1001927dbe93S猫头猫 pluginStateMapper.notify(); 100224e5e74aS猫头猫 if (plugins.every(_ => _.name !== pluginName)) { 100324e5e74aS猫头猫 await MediaMeta.removePlugin(pluginName); 100424e5e74aS猫头猫 } 1005927dbe93S猫头猫 } catch {} 1006927dbe93S猫头猫 } 1007927dbe93S猫头猫} 1008927dbe93S猫头猫 100908882a77S猫头猫async function uninstallAllPlugins() { 101008882a77S猫头猫 await Promise.all( 101108882a77S猫头猫 plugins.map(async plugin => { 101208882a77S猫头猫 try { 101308882a77S猫头猫 const pluginName = plugin.name; 101408882a77S猫头猫 await unlink(plugin.path); 101508882a77S猫头猫 await MediaMeta.removePlugin(pluginName); 101608882a77S猫头猫 } catch (e) {} 101708882a77S猫头猫 }), 101808882a77S猫头猫 ); 101908882a77S猫头猫 plugins = []; 102008882a77S猫头猫 pluginStateMapper.notify(); 1021e08d37a3S猫头猫 1022e08d37a3S猫头猫 /** 清除空余文件,异步做就可以了 */ 1023e08d37a3S猫头猫 readDir(pathConst.pluginPath) 1024e08d37a3S猫头猫 .then(fns => { 1025e08d37a3S猫头猫 fns.forEach(fn => { 1026e08d37a3S猫头猫 unlink(fn.path).catch(emptyFunction); 1027e08d37a3S猫头猫 }); 1028e08d37a3S猫头猫 }) 1029e08d37a3S猫头猫 .catch(emptyFunction); 103008882a77S猫头猫} 103108882a77S猫头猫 103225c1bd29S猫头猫async function updatePlugin(plugin: Plugin) { 103325c1bd29S猫头猫 const updateUrl = plugin.instance.srcUrl; 103425c1bd29S猫头猫 if (!updateUrl) { 103525c1bd29S猫头猫 throw new Error('没有更新源'); 103625c1bd29S猫头猫 } 103725c1bd29S猫头猫 try { 103825c1bd29S猫头猫 await installPluginFromUrl(updateUrl); 103925c1bd29S猫头猫 } catch (e: any) { 104025c1bd29S猫头猫 if (e.message === '插件已安装') { 104125c1bd29S猫头猫 throw new Error('当前已是最新版本'); 104225c1bd29S猫头猫 } else { 104325c1bd29S猫头猫 throw e; 104425c1bd29S猫头猫 } 104525c1bd29S猫头猫 } 104625c1bd29S猫头猫} 104725c1bd29S猫头猫 1048927dbe93S猫头猫function getByMedia(mediaItem: ICommon.IMediaBase) { 10492c595535S猫头猫 return getByName(mediaItem?.platform); 1050927dbe93S猫头猫} 1051927dbe93S猫头猫 1052927dbe93S猫头猫function getByHash(hash: string) { 10537993f90eS猫头猫 return hash === localPluginHash 10547993f90eS猫头猫 ? localFilePlugin 10557993f90eS猫头猫 : plugins.find(_ => _.hash === hash); 1056927dbe93S猫头猫} 1057927dbe93S猫头猫 1058927dbe93S猫头猫function getByName(name: string) { 10597993f90eS猫头猫 return name === localPluginPlatform 10600e4173cdS猫头猫 ? localFilePlugin 10610e4173cdS猫头猫 : plugins.find(_ => _.name === name); 1062927dbe93S猫头猫} 1063927dbe93S猫头猫 1064927dbe93S猫头猫function getValidPlugins() { 1065927dbe93S猫头猫 return plugins.filter(_ => _.state === 'enabled'); 1066927dbe93S猫头猫} 1067927dbe93S猫头猫 10682b80a429S猫头猫function getSearchablePlugins(supportedSearchType?: ICommon.SupportMediaType) { 10692b80a429S猫头猫 return plugins.filter( 10702b80a429S猫头猫 _ => 10712b80a429S猫头猫 _.state === 'enabled' && 10722b80a429S猫头猫 _.instance.search && 107339ac60f7S猫头猫 (supportedSearchType && _.instance.supportedSearchType 107439ac60f7S猫头猫 ? _.instance.supportedSearchType.includes(supportedSearchType) 10752b80a429S猫头猫 : true), 10762b80a429S猫头猫 ); 1077efb9da24S猫头猫} 1078efb9da24S猫头猫 10792b80a429S猫头猫function getSortedSearchablePlugins( 10802b80a429S猫头猫 supportedSearchType?: ICommon.SupportMediaType, 10812b80a429S猫头猫) { 10822b80a429S猫头猫 return getSearchablePlugins(supportedSearchType).sort((a, b) => 1083e08d37a3S猫头猫 (PluginMeta.getPluginMeta(a).order ?? Infinity) - 1084e08d37a3S猫头猫 (PluginMeta.getPluginMeta(b).order ?? Infinity) < 1085e08d37a3S猫头猫 0 1086e08d37a3S猫头猫 ? -1 1087e08d37a3S猫头猫 : 1, 1088e08d37a3S猫头猫 ); 1089e08d37a3S猫头猫} 1090e08d37a3S猫头猫 109115feccc1S猫头猫function getTopListsablePlugins() { 109215feccc1S猫头猫 return plugins.filter(_ => _.state === 'enabled' && _.instance.getTopLists); 109315feccc1S猫头猫} 109415feccc1S猫头猫 109515feccc1S猫头猫function getSortedTopListsablePlugins() { 109615feccc1S猫头猫 return getTopListsablePlugins().sort((a, b) => 109715feccc1S猫头猫 (PluginMeta.getPluginMeta(a).order ?? Infinity) - 109815feccc1S猫头猫 (PluginMeta.getPluginMeta(b).order ?? Infinity) < 109915feccc1S猫头猫 0 110015feccc1S猫头猫 ? -1 110115feccc1S猫头猫 : 1, 110215feccc1S猫头猫 ); 110315feccc1S猫头猫} 110415feccc1S猫头猫 1105ceb900cdS猫头猫function getRecommendSheetablePlugins() { 1106ceb900cdS猫头猫 return plugins.filter( 1107ceb900cdS猫头猫 _ => _.state === 'enabled' && _.instance.getRecommendSheetsByTag, 1108ceb900cdS猫头猫 ); 1109ceb900cdS猫头猫} 1110ceb900cdS猫头猫 1111ceb900cdS猫头猫function getSortedRecommendSheetablePlugins() { 1112ceb900cdS猫头猫 return getRecommendSheetablePlugins().sort((a, b) => 1113ceb900cdS猫头猫 (PluginMeta.getPluginMeta(a).order ?? Infinity) - 1114ceb900cdS猫头猫 (PluginMeta.getPluginMeta(b).order ?? Infinity) < 1115ceb900cdS猫头猫 0 1116ceb900cdS猫头猫 ? -1 1117ceb900cdS猫头猫 : 1, 1118ceb900cdS猫头猫 ); 1119ceb900cdS猫头猫} 1120ceb900cdS猫头猫 1121e08d37a3S猫头猫function useSortedPlugins() { 1122e08d37a3S猫头猫 const _plugins = pluginStateMapper.useMappedState(); 1123e08d37a3S猫头猫 const _pluginMetaAll = PluginMeta.usePluginMetaAll(); 1124e08d37a3S猫头猫 112534588741S猫头猫 const [sortedPlugins, setSortedPlugins] = useState( 112634588741S猫头猫 [..._plugins].sort((a, b) => 1127e08d37a3S猫头猫 (_pluginMetaAll[a.name]?.order ?? Infinity) - 1128e08d37a3S猫头猫 (_pluginMetaAll[b.name]?.order ?? Infinity) < 1129e08d37a3S猫头猫 0 1130e08d37a3S猫头猫 ? -1 1131e08d37a3S猫头猫 : 1, 113234588741S猫头猫 ), 1133e08d37a3S猫头猫 ); 113434588741S猫头猫 113534588741S猫头猫 useEffect(() => { 1136d4cd40d8S猫头猫 InteractionManager.runAfterInteractions(() => { 113734588741S猫头猫 setSortedPlugins( 113834588741S猫头猫 [..._plugins].sort((a, b) => 113934588741S猫头猫 (_pluginMetaAll[a.name]?.order ?? Infinity) - 114034588741S猫头猫 (_pluginMetaAll[b.name]?.order ?? Infinity) < 114134588741S猫头猫 0 114234588741S猫头猫 ? -1 114334588741S猫头猫 : 1, 114434588741S猫头猫 ), 114534588741S猫头猫 ); 1146d4cd40d8S猫头猫 }); 114734588741S猫头猫 }, [_plugins, _pluginMetaAll]); 114834588741S猫头猫 114934588741S猫头猫 return sortedPlugins; 1150e08d37a3S猫头猫} 1151e08d37a3S猫头猫 1152c2b3a262S猫头猫async function setPluginEnabled(plugin: Plugin, enabled?: boolean) { 1153c2b3a262S猫头猫 const target = plugins.find(it => it.hash === plugin.hash); 1154c2b3a262S猫头猫 if (target) { 1155c2b3a262S猫头猫 target.state = enabled ? 'enabled' : 'disabled'; 1156c2b3a262S猫头猫 plugins = [...plugins]; 1157c2b3a262S猫头猫 pluginStateMapper.notify(); 1158c2b3a262S猫头猫 PluginMeta.setPluginMetaProp(plugin, 'enabled', enabled); 1159c2b3a262S猫头猫 } 1160c2b3a262S猫头猫} 1161c2b3a262S猫头猫 1162927dbe93S猫头猫const PluginManager = { 1163927dbe93S猫头猫 setup, 1164927dbe93S猫头猫 installPlugin, 116558992c6bS猫头猫 installPluginFromUrl, 116625c1bd29S猫头猫 updatePlugin, 1167927dbe93S猫头猫 uninstallPlugin, 1168927dbe93S猫头猫 getByMedia, 1169927dbe93S猫头猫 getByHash, 1170927dbe93S猫头猫 getByName, 1171927dbe93S猫头猫 getValidPlugins, 1172efb9da24S猫头猫 getSearchablePlugins, 1173e08d37a3S猫头猫 getSortedSearchablePlugins, 117415feccc1S猫头猫 getTopListsablePlugins, 1175ceb900cdS猫头猫 getSortedRecommendSheetablePlugins, 117615feccc1S猫头猫 getSortedTopListsablePlugins, 11775276aef9S猫头猫 usePlugins: pluginStateMapper.useMappedState, 1178e08d37a3S猫头猫 useSortedPlugins, 117908882a77S猫头猫 uninstallAllPlugins, 1180c2b3a262S猫头猫 setPluginEnabled, 11815276aef9S猫头猫}; 1182927dbe93S猫头猫 1183927dbe93S猫头猫export default PluginManager; 1184