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'; 14927dbe93S猫头猫import {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'; 21927dbe93S猫头猫import {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, 317993f90eS猫头猫 internalSerializeKey, 327993f90eS猫头猫 localPluginHash, 337993f90eS猫头猫 localPluginPlatform, 347993f90eS猫头猫} from '@/constants/commonConst'; 35927dbe93S猫头猫import delay from '@/utils/delay'; 364d9d3c4cS猫头猫import * as cheerio from 'cheerio'; 377d7e864fS猫头猫import CookieManager from '@react-native-cookies/cookies'; 387d7e864fS猫头猫import he from 'he'; 39ef714860S猫头猫import Network from './network'; 400e4173cdS猫头猫import LocalMusicSheet from './localMusicSheet'; 410e4173cdS猫头猫import {FileSystem} from 'react-native-file-access'; 4274d0cf81S猫头猫import Mp3Util from '@/native/mp3Util'; 43927dbe93S猫头猫 44927dbe93S猫头猫axios.defaults.timeout = 1500; 45927dbe93S猫头猫 46927dbe93S猫头猫const sha256 = CryptoJs.SHA256; 47927dbe93S猫头猫 48cfa0fc07S猫头猫export enum PluginStateCode { 49927dbe93S猫头猫 /** 版本不匹配 */ 50927dbe93S猫头猫 VersionNotMatch = 'VERSION NOT MATCH', 51927dbe93S猫头猫 /** 无法解析 */ 52927dbe93S猫头猫 CannotParse = 'CANNOT PARSE', 53927dbe93S猫头猫} 54927dbe93S猫头猫 55d5bfeb7eS猫头猫//#region 插件类 56927dbe93S猫头猫export class Plugin { 57927dbe93S猫头猫 /** 插件名 */ 58927dbe93S猫头猫 public name: string; 59927dbe93S猫头猫 /** 插件的hash,作为唯一id */ 60927dbe93S猫头猫 public hash: string; 61927dbe93S猫头猫 /** 插件状态:激活、关闭、错误 */ 62927dbe93S猫头猫 public state: 'enabled' | 'disabled' | 'error'; 63927dbe93S猫头猫 /** 插件支持的搜索类型 */ 64927dbe93S猫头猫 public supportedSearchType?: string; 65927dbe93S猫头猫 /** 插件状态信息 */ 66927dbe93S猫头猫 public stateCode?: PluginStateCode; 67927dbe93S猫头猫 /** 插件的实例 */ 68927dbe93S猫头猫 public instance: IPlugin.IPluginInstance; 69927dbe93S猫头猫 /** 插件路径 */ 70927dbe93S猫头猫 public path: string; 71927dbe93S猫头猫 /** 插件方法 */ 72927dbe93S猫头猫 public methods: PluginMethods; 73d5bfeb7eS猫头猫 /** 用户输入 */ 74d5bfeb7eS猫头猫 public userEnv?: Record<string, string>; 75927dbe93S猫头猫 7674d0cf81S猫头猫 constructor( 7774d0cf81S猫头猫 funcCode: string | (() => IPlugin.IPluginInstance), 7874d0cf81S猫头猫 pluginPath: string, 7974d0cf81S猫头猫 ) { 80927dbe93S猫头猫 this.state = 'enabled'; 81927dbe93S猫头猫 let _instance: IPlugin.IPluginInstance; 82927dbe93S猫头猫 try { 8374d0cf81S猫头猫 if (typeof funcCode === 'string') { 844060c00aS猫头猫 // eslint-disable-next-line no-new-func 85927dbe93S猫头猫 _instance = Function(` 86927dbe93S猫头猫 'use strict'; 87927dbe93S猫头猫 try { 88927dbe93S猫头猫 return ${funcCode}; 89927dbe93S猫头猫 } catch(e) { 90927dbe93S猫头猫 return null; 91927dbe93S猫头猫 } 927d7e864fS猫头猫 `)()({ 937d7e864fS猫头猫 CryptoJs, 947d7e864fS猫头猫 axios, 957d7e864fS猫头猫 dayjs, 967d7e864fS猫头猫 cheerio, 977d7e864fS猫头猫 bigInt, 987d7e864fS猫头猫 qs, 997d7e864fS猫头猫 he, 1007d7e864fS猫头猫 CookieManager: { 1017d7e864fS猫头猫 flush: CookieManager.flush, 1027d7e864fS猫头猫 get: CookieManager.get, 1037d7e864fS猫头猫 }, 1047d7e864fS猫头猫 }); 10574d0cf81S猫头猫 } else { 10674d0cf81S猫头猫 _instance = funcCode(); 10774d0cf81S猫头猫 } 108927dbe93S猫头猫 this.checkValid(_instance); 109927dbe93S猫头猫 } catch (e: any) { 110927dbe93S猫头猫 this.state = 'error'; 111927dbe93S猫头猫 this.stateCode = PluginStateCode.CannotParse; 112927dbe93S猫头猫 if (e?.stateCode) { 113927dbe93S猫头猫 this.stateCode = e.stateCode; 114927dbe93S猫头猫 } 115927dbe93S猫头猫 errorLog(`${pluginPath}插件无法解析 `, { 116927dbe93S猫头猫 stateCode: this.stateCode, 117927dbe93S猫头猫 message: e?.message, 118927dbe93S猫头猫 stack: e?.stack, 119927dbe93S猫头猫 }); 120927dbe93S猫头猫 _instance = e?.instance ?? { 121927dbe93S猫头猫 _path: '', 122927dbe93S猫头猫 platform: '', 123927dbe93S猫头猫 appVersion: '', 12420e6a092S猫头猫 async getMediaSource() { 125927dbe93S猫头猫 return null; 126927dbe93S猫头猫 }, 127927dbe93S猫头猫 async search() { 128927dbe93S猫头猫 return {}; 129927dbe93S猫头猫 }, 130927dbe93S猫头猫 async getAlbumInfo() { 131927dbe93S猫头猫 return null; 132927dbe93S猫头猫 }, 133927dbe93S猫头猫 }; 134927dbe93S猫头猫 } 135927dbe93S猫头猫 this.instance = _instance; 136927dbe93S猫头猫 this.path = pluginPath; 137927dbe93S猫头猫 this.name = _instance.platform; 138927dbe93S猫头猫 if (this.instance.platform === '') { 139927dbe93S猫头猫 this.hash = ''; 140927dbe93S猫头猫 } else { 14174d0cf81S猫头猫 if (typeof funcCode === 'string') { 142927dbe93S猫头猫 this.hash = sha256(funcCode).toString(); 14374d0cf81S猫头猫 } else { 14474d0cf81S猫头猫 this.hash = sha256(funcCode.toString()).toString(); 14574d0cf81S猫头猫 } 146927dbe93S猫头猫 } 147927dbe93S猫头猫 148927dbe93S猫头猫 // 放在最后 149927dbe93S猫头猫 this.methods = new PluginMethods(this); 150927dbe93S猫头猫 } 151927dbe93S猫头猫 152927dbe93S猫头猫 private checkValid(_instance: IPlugin.IPluginInstance) { 153927dbe93S猫头猫 /** 版本号校验 */ 154927dbe93S猫头猫 if ( 155927dbe93S猫头猫 _instance.appVersion && 156927dbe93S猫头猫 !satisfies(DeviceInfo.getVersion(), _instance.appVersion) 157927dbe93S猫头猫 ) { 158927dbe93S猫头猫 throw { 159927dbe93S猫头猫 instance: _instance, 160927dbe93S猫头猫 stateCode: PluginStateCode.VersionNotMatch, 161927dbe93S猫头猫 }; 162927dbe93S猫头猫 } 163927dbe93S猫头猫 return true; 164927dbe93S猫头猫 } 165927dbe93S猫头猫} 166d5bfeb7eS猫头猫//#endregion 167927dbe93S猫头猫 168d5bfeb7eS猫头猫//#region 基于插件类封装的方法,供给APP侧直接调用 169927dbe93S猫头猫/** 有缓存等信息 */ 170927dbe93S猫头猫class PluginMethods implements IPlugin.IPluginInstanceMethods { 171927dbe93S猫头猫 private plugin; 172927dbe93S猫头猫 constructor(plugin: Plugin) { 173927dbe93S猫头猫 this.plugin = plugin; 174927dbe93S猫头猫 } 175927dbe93S猫头猫 /** 搜索 */ 176927dbe93S猫头猫 async search<T extends ICommon.SupportMediaType>( 177927dbe93S猫头猫 query: string, 178927dbe93S猫头猫 page: number, 179927dbe93S猫头猫 type: T, 180927dbe93S猫头猫 ): Promise<IPlugin.ISearchResult<T>> { 181927dbe93S猫头猫 if (!this.plugin.instance.search) { 182927dbe93S猫头猫 return { 183927dbe93S猫头猫 isEnd: true, 184927dbe93S猫头猫 data: [], 185927dbe93S猫头猫 }; 186927dbe93S猫头猫 } 187927dbe93S猫头猫 1884060c00aS猫头猫 const result = 1894060c00aS猫头猫 (await this.plugin.instance.search(query, page, type)) ?? {}; 190927dbe93S猫头猫 if (Array.isArray(result.data)) { 191927dbe93S猫头猫 result.data.forEach(_ => { 192927dbe93S猫头猫 resetMediaItem(_, this.plugin.name); 193927dbe93S猫头猫 }); 194927dbe93S猫头猫 return { 195927dbe93S猫头猫 isEnd: result.isEnd ?? true, 196927dbe93S猫头猫 data: result.data, 197927dbe93S猫头猫 }; 198927dbe93S猫头猫 } 199927dbe93S猫头猫 return { 200927dbe93S猫头猫 isEnd: true, 201927dbe93S猫头猫 data: [], 202927dbe93S猫头猫 }; 203927dbe93S猫头猫 } 204927dbe93S猫头猫 205927dbe93S猫头猫 /** 获取真实源 */ 20620e6a092S猫头猫 async getMediaSource( 207927dbe93S猫头猫 musicItem: IMusic.IMusicItemBase, 208927dbe93S猫头猫 retryCount = 1, 20920e6a092S猫头猫 ): Promise<IPlugin.IMediaSourceResult> { 210927dbe93S猫头猫 // 1. 本地搜索 其实直接读mediameta就好了 211927dbe93S猫头猫 const localPath = 2120e4173cdS猫头猫 getInternalData<string>(musicItem, InternalDataType.LOCALPATH) ?? 2130e4173cdS猫头猫 getInternalData<string>( 2140e4173cdS猫头猫 LocalMusicSheet.isLocalMusic(musicItem), 2150e4173cdS猫头猫 InternalDataType.LOCALPATH, 2160e4173cdS猫头猫 ); 2170e4173cdS猫头猫 if (localPath && (await FileSystem.exists(localPath))) { 2180e4173cdS猫头猫 trace('本地播放', localPath); 219927dbe93S猫头猫 return { 220927dbe93S猫头猫 url: localPath, 221927dbe93S猫头猫 }; 222927dbe93S猫头猫 } 2237993f90eS猫头猫 if (musicItem.platform === localPluginPlatform) { 224f5935920S猫头猫 throw new Error('本地音乐不存在'); 225f5935920S猫头猫 } 226927dbe93S猫头猫 // 2. 缓存播放 227927dbe93S猫头猫 const mediaCache = Cache.get(musicItem); 228*985f8e75S猫头猫 const pluginCacheControl = 229*985f8e75S猫头猫 this.plugin.instance.cacheControl ?? 'no-cache'; 230cfa0fc07S猫头猫 if ( 231cfa0fc07S猫头猫 mediaCache && 232cfa0fc07S猫头猫 mediaCache?.url && 23348f4b873S猫头猫 (pluginCacheControl === CacheControl.Cache || 23448f4b873S猫头猫 (pluginCacheControl === CacheControl.NoCache && 235ef714860S猫头猫 Network.isOffline())) 236cfa0fc07S猫头猫 ) { 2375276aef9S猫头猫 trace('播放', '缓存播放'); 238927dbe93S猫头猫 return { 239927dbe93S猫头猫 url: mediaCache.url, 240927dbe93S猫头猫 headers: mediaCache.headers, 2414060c00aS猫头猫 userAgent: 2424060c00aS猫头猫 mediaCache.userAgent ?? mediaCache.headers?.['user-agent'], 243927dbe93S猫头猫 }; 244927dbe93S猫头猫 } 245927dbe93S猫头猫 // 3. 插件解析 24620e6a092S猫头猫 if (!this.plugin.instance.getMediaSource) { 247927dbe93S猫头猫 return {url: musicItem.url}; 248927dbe93S猫头猫 } 249927dbe93S猫头猫 try { 25048f4b873S猫头猫 const {url, headers} = 25120e6a092S猫头猫 (await this.plugin.instance.getMediaSource(musicItem)) ?? {}; 252927dbe93S猫头猫 if (!url) { 253927dbe93S猫头猫 throw new Error(); 254927dbe93S猫头猫 } 2555276aef9S猫头猫 trace('播放', '插件播放'); 256927dbe93S猫头猫 const result = { 257927dbe93S猫头猫 url, 258927dbe93S猫头猫 headers, 259927dbe93S猫头猫 userAgent: headers?.['user-agent'], 260cfa0fc07S猫头猫 } as IPlugin.IMediaSourceResult; 261927dbe93S猫头猫 26248f4b873S猫头猫 if (pluginCacheControl !== CacheControl.NoStore) { 263927dbe93S猫头猫 Cache.update(musicItem, result); 264752ffc5aS猫头猫 } 265cfa0fc07S猫头猫 266927dbe93S猫头猫 return result; 267927dbe93S猫头猫 } catch (e: any) { 268927dbe93S猫头猫 if (retryCount > 0) { 269927dbe93S猫头猫 await delay(150); 27020e6a092S猫头猫 return this.getMediaSource(musicItem, --retryCount); 271927dbe93S猫头猫 } 272927dbe93S猫头猫 errorLog('获取真实源失败', e?.message); 273927dbe93S猫头猫 throw e; 274927dbe93S猫头猫 } 275927dbe93S猫头猫 } 276927dbe93S猫头猫 277927dbe93S猫头猫 /** 获取音乐详情 */ 278927dbe93S猫头猫 async getMusicInfo( 279927dbe93S猫头猫 musicItem: ICommon.IMediaBase, 28074d0cf81S猫头猫 ): Promise<Partial<IMusic.IMusicItem> | null> { 281927dbe93S猫头猫 if (!this.plugin.instance.getMusicInfo) { 28274d0cf81S猫头猫 return {}; 283927dbe93S猫头猫 } 28474d0cf81S猫头猫 try { 285927dbe93S猫头猫 return ( 286927dbe93S猫头猫 this.plugin.instance.getMusicInfo( 2877993f90eS猫头猫 resetMediaItem(musicItem, undefined, true), 28874d0cf81S猫头猫 ) ?? {} 289927dbe93S猫头猫 ); 29074d0cf81S猫头猫 } catch (e) { 29174d0cf81S猫头猫 console.log(e); 29274d0cf81S猫头猫 return {}; 29374d0cf81S猫头猫 } 294927dbe93S猫头猫 } 295927dbe93S猫头猫 296927dbe93S猫头猫 /** 获取歌词 */ 297927dbe93S猫头猫 async getLyric( 298927dbe93S猫头猫 musicItem: IMusic.IMusicItemBase, 299927dbe93S猫头猫 from?: IMusic.IMusicItemBase, 300927dbe93S猫头猫 ): Promise<ILyric.ILyricSource | null> { 301927dbe93S猫头猫 // 1.额外存储的meta信息 302927dbe93S猫头猫 const meta = MediaMeta.get(musicItem); 303927dbe93S猫头猫 if (meta && meta.associatedLrc) { 304927dbe93S猫头猫 // 有关联歌词 305927dbe93S猫头猫 if ( 306927dbe93S猫头猫 isSameMediaItem(musicItem, from) || 307927dbe93S猫头猫 isSameMediaItem(meta.associatedLrc, musicItem) 308927dbe93S猫头猫 ) { 309927dbe93S猫头猫 // 形成环路,断开当前的环 310927dbe93S猫头猫 await MediaMeta.update(musicItem, { 311927dbe93S猫头猫 associatedLrc: undefined, 312927dbe93S猫头猫 }); 313927dbe93S猫头猫 // 无歌词 314927dbe93S猫头猫 return null; 315927dbe93S猫头猫 } 316927dbe93S猫头猫 // 获取关联歌词 3177a91f04fS猫头猫 const associatedMeta = MediaMeta.get(meta.associatedLrc) ?? {}; 3184060c00aS猫头猫 const result = await this.getLyric( 3197a91f04fS猫头猫 {...meta.associatedLrc, ...associatedMeta}, 3204060c00aS猫头猫 from ?? musicItem, 3214060c00aS猫头猫 ); 322927dbe93S猫头猫 if (result) { 323927dbe93S猫头猫 // 如果有关联歌词,就返回关联歌词,深度优先 324927dbe93S猫头猫 return result; 325927dbe93S猫头猫 } 326927dbe93S猫头猫 } 327927dbe93S猫头猫 const cache = Cache.get(musicItem); 328927dbe93S猫头猫 let rawLrc = meta?.rawLrc || musicItem.rawLrc || cache?.rawLrc; 329927dbe93S猫头猫 let lrcUrl = meta?.lrc || musicItem.lrc || cache?.lrc; 330927dbe93S猫头猫 // 如果存在文本 331927dbe93S猫头猫 if (rawLrc) { 332927dbe93S猫头猫 return { 333927dbe93S猫头猫 rawLrc, 334927dbe93S猫头猫 lrc: lrcUrl, 335927dbe93S猫头猫 }; 336927dbe93S猫头猫 } 337927dbe93S猫头猫 // 2.本地缓存 338927dbe93S猫头猫 const localLrc = 3390e4173cdS猫头猫 meta?.[internalSerializeKey]?.local?.localLrc || 3400e4173cdS猫头猫 cache?.[internalSerializeKey]?.local?.localLrc; 341927dbe93S猫头猫 if (localLrc && (await exists(localLrc))) { 342927dbe93S猫头猫 rawLrc = await readFile(localLrc, 'utf8'); 343927dbe93S猫头猫 return { 344927dbe93S猫头猫 rawLrc, 345927dbe93S猫头猫 lrc: lrcUrl, 346927dbe93S猫头猫 }; 347927dbe93S猫头猫 } 348927dbe93S猫头猫 // 3.优先使用url 349927dbe93S猫头猫 if (lrcUrl) { 350927dbe93S猫头猫 try { 351927dbe93S猫头猫 // 需要超时时间 axios timeout 但是没生效 3522a3194f5S猫头猫 rawLrc = (await axios.get(lrcUrl, {timeout: 1500})).data; 353927dbe93S猫头猫 return { 354927dbe93S猫头猫 rawLrc, 355927dbe93S猫头猫 lrc: lrcUrl, 356927dbe93S猫头猫 }; 357927dbe93S猫头猫 } catch { 358927dbe93S猫头猫 lrcUrl = undefined; 359927dbe93S猫头猫 } 360927dbe93S猫头猫 } 361927dbe93S猫头猫 // 4. 如果地址失效 362927dbe93S猫头猫 if (!lrcUrl) { 363927dbe93S猫头猫 // 插件获得url 364927dbe93S猫头猫 try { 3657a91f04fS猫头猫 let lrcSource; 3667a91f04fS猫头猫 if (from) { 3677a91f04fS猫头猫 lrcSource = await PluginManager.getByMedia( 3687a91f04fS猫头猫 musicItem, 3697a91f04fS猫头猫 )?.instance?.getLyric?.( 370927dbe93S猫头猫 resetMediaItem(musicItem, undefined, true), 371927dbe93S猫头猫 ); 3727a91f04fS猫头猫 } else { 3737a91f04fS猫头猫 lrcSource = await this.plugin.instance?.getLyric?.( 3747a91f04fS猫头猫 resetMediaItem(musicItem, undefined, true), 3757a91f04fS猫头猫 ); 3767a91f04fS猫头猫 } 3777a91f04fS猫头猫 378927dbe93S猫头猫 rawLrc = lrcSource?.rawLrc; 379927dbe93S猫头猫 lrcUrl = lrcSource?.lrc; 380927dbe93S猫头猫 } catch (e: any) { 381927dbe93S猫头猫 trace('插件获取歌词失败', e?.message, 'error'); 382927dbe93S猫头猫 } 383927dbe93S猫头猫 } 384927dbe93S猫头猫 // 5. 最后一次请求 385927dbe93S猫头猫 if (rawLrc || lrcUrl) { 386927dbe93S猫头猫 const filename = `${pathConst.lrcCachePath}${nanoid()}.lrc`; 387927dbe93S猫头猫 if (lrcUrl) { 388927dbe93S猫头猫 try { 3892a3194f5S猫头猫 rawLrc = (await axios.get(lrcUrl, {timeout: 1500})).data; 390927dbe93S猫头猫 } catch {} 391927dbe93S猫头猫 } 392927dbe93S猫头猫 if (rawLrc) { 393927dbe93S猫头猫 await writeFile(filename, rawLrc, 'utf8'); 394927dbe93S猫头猫 // 写入缓存 395927dbe93S猫头猫 Cache.update(musicItem, [ 3960e4173cdS猫头猫 [`${internalSerializeKey}.local.localLrc`, filename], 397927dbe93S猫头猫 ]); 398927dbe93S猫头猫 // 如果有meta 399927dbe93S猫头猫 if (meta) { 400927dbe93S猫头猫 MediaMeta.update(musicItem, [ 4010e4173cdS猫头猫 [`${internalSerializeKey}.local.localLrc`, filename], 402927dbe93S猫头猫 ]); 403927dbe93S猫头猫 } 404927dbe93S猫头猫 return { 405927dbe93S猫头猫 rawLrc, 406927dbe93S猫头猫 lrc: lrcUrl, 407927dbe93S猫头猫 }; 408927dbe93S猫头猫 } 409927dbe93S猫头猫 } 410927dbe93S猫头猫 411927dbe93S猫头猫 return null; 412927dbe93S猫头猫 } 413927dbe93S猫头猫 414927dbe93S猫头猫 /** 获取歌词文本 */ 415927dbe93S猫头猫 async getLyricText( 416927dbe93S猫头猫 musicItem: IMusic.IMusicItem, 417927dbe93S猫头猫 ): Promise<string | undefined> { 418927dbe93S猫头猫 return (await this.getLyric(musicItem))?.rawLrc; 419927dbe93S猫头猫 } 420927dbe93S猫头猫 421927dbe93S猫头猫 /** 获取专辑信息 */ 422927dbe93S猫头猫 async getAlbumInfo( 423927dbe93S猫头猫 albumItem: IAlbum.IAlbumItemBase, 424927dbe93S猫头猫 ): Promise<IAlbum.IAlbumItem | null> { 425927dbe93S猫头猫 if (!this.plugin.instance.getAlbumInfo) { 426927dbe93S猫头猫 return {...albumItem, musicList: []}; 427927dbe93S猫头猫 } 428927dbe93S猫头猫 try { 429927dbe93S猫头猫 const result = await this.plugin.instance.getAlbumInfo( 430927dbe93S猫头猫 resetMediaItem(albumItem, undefined, true), 431927dbe93S猫头猫 ); 4325276aef9S猫头猫 if (!result) { 4335276aef9S猫头猫 throw new Error(); 4345276aef9S猫头猫 } 435927dbe93S猫头猫 result?.musicList?.forEach(_ => { 436927dbe93S猫头猫 resetMediaItem(_, this.plugin.name); 437927dbe93S猫头猫 }); 4385276aef9S猫头猫 4395276aef9S猫头猫 return {...albumItem, ...result}; 4404394410dS猫头猫 } catch (e: any) { 4414394410dS猫头猫 trace('获取专辑信息失败', e?.message); 442927dbe93S猫头猫 return {...albumItem, musicList: []}; 443927dbe93S猫头猫 } 444927dbe93S猫头猫 } 445927dbe93S猫头猫 446927dbe93S猫头猫 /** 查询作者信息 */ 447efb9da24S猫头猫 async getArtistWorks<T extends IArtist.ArtistMediaType>( 448927dbe93S猫头猫 artistItem: IArtist.IArtistItem, 449927dbe93S猫头猫 page: number, 450927dbe93S猫头猫 type: T, 451927dbe93S猫头猫 ): Promise<IPlugin.ISearchResult<T>> { 452efb9da24S猫头猫 if (!this.plugin.instance.getArtistWorks) { 453927dbe93S猫头猫 return { 454927dbe93S猫头猫 isEnd: true, 455927dbe93S猫头猫 data: [], 456927dbe93S猫头猫 }; 457927dbe93S猫头猫 } 458927dbe93S猫头猫 try { 459efb9da24S猫头猫 const result = await this.plugin.instance.getArtistWorks( 460927dbe93S猫头猫 artistItem, 461927dbe93S猫头猫 page, 462927dbe93S猫头猫 type, 463927dbe93S猫头猫 ); 464927dbe93S猫头猫 if (!result.data) { 465927dbe93S猫头猫 return { 466927dbe93S猫头猫 isEnd: true, 467927dbe93S猫头猫 data: [], 468927dbe93S猫头猫 }; 469927dbe93S猫头猫 } 470927dbe93S猫头猫 result.data?.forEach(_ => resetMediaItem(_, this.plugin.name)); 471927dbe93S猫头猫 return { 472927dbe93S猫头猫 isEnd: result.isEnd ?? true, 473927dbe93S猫头猫 data: result.data, 474927dbe93S猫头猫 }; 4754394410dS猫头猫 } catch (e: any) { 4764394410dS猫头猫 trace('查询作者信息失败', e?.message); 477927dbe93S猫头猫 throw e; 478927dbe93S猫头猫 } 479927dbe93S猫头猫 } 48008380090S猫头猫 48108380090S猫头猫 /** 导入歌单 */ 48208380090S猫头猫 async importMusicSheet(urlLike: string): Promise<IMusic.IMusicItem[]> { 48308380090S猫头猫 try { 48408380090S猫头猫 const result = 48508380090S猫头猫 (await this.plugin.instance?.importMusicSheet?.(urlLike)) ?? []; 48608380090S猫头猫 result.forEach(_ => resetMediaItem(_, this.plugin.name)); 48708380090S猫头猫 return result; 4880e4173cdS猫头猫 } catch (e) { 4890e4173cdS猫头猫 console.log(e); 49008380090S猫头猫 return []; 49108380090S猫头猫 } 49208380090S猫头猫 } 4934d9d3c4cS猫头猫 /** 导入单曲 */ 4944d9d3c4cS猫头猫 async importMusicItem(urlLike: string): Promise<IMusic.IMusicItem | null> { 4954d9d3c4cS猫头猫 try { 4964d9d3c4cS猫头猫 const result = await this.plugin.instance?.importMusicItem?.( 4974d9d3c4cS猫头猫 urlLike, 4984d9d3c4cS猫头猫 ); 4994d9d3c4cS猫头猫 if (!result) { 5004d9d3c4cS猫头猫 throw new Error(); 5014d9d3c4cS猫头猫 } 5024d9d3c4cS猫头猫 resetMediaItem(result, this.plugin.name); 5034d9d3c4cS猫头猫 return result; 5044d9d3c4cS猫头猫 } catch { 5054d9d3c4cS猫头猫 return null; 5064d9d3c4cS猫头猫 } 5074d9d3c4cS猫头猫 } 508927dbe93S猫头猫} 509d5bfeb7eS猫头猫//#endregion 5101a5528a0S猫头猫 511927dbe93S猫头猫let plugins: Array<Plugin> = []; 512927dbe93S猫头猫const pluginStateMapper = new StateMapper(() => plugins); 51374d0cf81S猫头猫 514d5bfeb7eS猫头猫//#region 本地音乐插件 51574d0cf81S猫头猫/** 本地插件 */ 51674d0cf81S猫头猫const localFilePlugin = new Plugin(function () { 5170e4173cdS猫头猫 return { 518d5bfeb7eS猫头猫 platform: localPluginPlatform, 51974d0cf81S猫头猫 _path: '', 52074d0cf81S猫头猫 async getMusicInfo(musicBase) { 52174d0cf81S猫头猫 const localPath = getInternalData<string>( 52274d0cf81S猫头猫 musicBase, 52374d0cf81S猫头猫 InternalDataType.LOCALPATH, 5240e4173cdS猫头猫 ); 52574d0cf81S猫头猫 if (localPath) { 52674d0cf81S猫头猫 const coverImg = await Mp3Util.getMediaCoverImg(localPath); 52774d0cf81S猫头猫 return { 52874d0cf81S猫头猫 artwork: coverImg, 52974d0cf81S猫头猫 }; 53074d0cf81S猫头猫 } 53174d0cf81S猫头猫 return null; 53274d0cf81S猫头猫 }, 5337993f90eS猫头猫 async getLyric(musicBase) { 5347993f90eS猫头猫 const localPath = getInternalData<string>( 5357993f90eS猫头猫 musicBase, 5367993f90eS猫头猫 InternalDataType.LOCALPATH, 5377993f90eS猫头猫 ); 5387993f90eS猫头猫 if (localPath) { 5397993f90eS猫头猫 const rawLrc = await Mp3Util.getLyric(localPath); 5407993f90eS猫头猫 return { 5417993f90eS猫头猫 rawLrc, 5427993f90eS猫头猫 }; 5437993f90eS猫头猫 } 5447993f90eS猫头猫 return null; 5457993f90eS猫头猫 }, 54674d0cf81S猫头猫 }; 54774d0cf81S猫头猫}, ''); 5487993f90eS猫头猫localFilePlugin.hash = localPluginHash; 549927dbe93S猫头猫 550d5bfeb7eS猫头猫//#endregion 551d5bfeb7eS猫头猫 552927dbe93S猫头猫async function setup() { 553927dbe93S猫头猫 const _plugins: Array<Plugin> = []; 554927dbe93S猫头猫 try { 555927dbe93S猫头猫 // 加载插件 556927dbe93S猫头猫 const pluginsPaths = await readDir(pathConst.pluginPath); 557927dbe93S猫头猫 for (let i = 0; i < pluginsPaths.length; ++i) { 558927dbe93S猫头猫 const _pluginUrl = pluginsPaths[i]; 5591e263108S猫头猫 trace('初始化插件', _pluginUrl); 5601e263108S猫头猫 if ( 5611e263108S猫头猫 _pluginUrl.isFile() && 5621e263108S猫头猫 (_pluginUrl.name?.endsWith?.('.js') || 5631e263108S猫头猫 _pluginUrl.path?.endsWith?.('.js')) 5641e263108S猫头猫 ) { 565927dbe93S猫头猫 const funcCode = await readFile(_pluginUrl.path, 'utf8'); 566927dbe93S猫头猫 const plugin = new Plugin(funcCode, _pluginUrl.path); 5674060c00aS猫头猫 const _pluginIndex = _plugins.findIndex( 5684060c00aS猫头猫 p => p.hash === plugin.hash, 5694060c00aS猫头猫 ); 570927dbe93S猫头猫 if (_pluginIndex !== -1) { 571927dbe93S猫头猫 // 重复插件,直接忽略 572927dbe93S猫头猫 return; 573927dbe93S猫头猫 } 574927dbe93S猫头猫 plugin.hash !== '' && _plugins.push(plugin); 575927dbe93S猫头猫 } 576927dbe93S猫头猫 } 577927dbe93S猫头猫 578927dbe93S猫头猫 plugins = _plugins; 579927dbe93S猫头猫 pluginStateMapper.notify(); 580927dbe93S猫头猫 } catch (e: any) { 5814060c00aS猫头猫 ToastAndroid.show( 5824060c00aS猫头猫 `插件初始化失败:${e?.message ?? e}`, 5834060c00aS猫头猫 ToastAndroid.LONG, 5844060c00aS猫头猫 ); 5851a5528a0S猫头猫 errorLog('插件初始化失败', e?.message); 586927dbe93S猫头猫 throw e; 587927dbe93S猫头猫 } 588927dbe93S猫头猫} 589927dbe93S猫头猫 590927dbe93S猫头猫// 安装插件 591927dbe93S猫头猫async function installPlugin(pluginPath: string) { 59222c09412S猫头猫 // if (pluginPath.endsWith('.js')) { 593927dbe93S猫头猫 const funcCode = await readFile(pluginPath, 'utf8'); 594927dbe93S猫头猫 const plugin = new Plugin(funcCode, pluginPath); 595927dbe93S猫头猫 const _pluginIndex = plugins.findIndex(p => p.hash === plugin.hash); 596927dbe93S猫头猫 if (_pluginIndex !== -1) { 5974d9d3c4cS猫头猫 throw new Error('插件已安装'); 598927dbe93S猫头猫 } 599927dbe93S猫头猫 if (plugin.hash !== '') { 600927dbe93S猫头猫 const fn = nanoid(); 601927dbe93S猫头猫 const _pluginPath = `${pathConst.pluginPath}${fn}.js`; 602927dbe93S猫头猫 await copyFile(pluginPath, _pluginPath); 603927dbe93S猫头猫 plugin.path = _pluginPath; 604927dbe93S猫头猫 plugins = plugins.concat(plugin); 605927dbe93S猫头猫 pluginStateMapper.notify(); 6064d9d3c4cS猫头猫 return; 607927dbe93S猫头猫 } 6084d9d3c4cS猫头猫 throw new Error('插件无法解析'); 60922c09412S猫头猫 // } 61022c09412S猫头猫 // throw new Error('插件不存在'); 611927dbe93S猫头猫} 612927dbe93S猫头猫 61358992c6bS猫头猫async function installPluginFromUrl(url: string) { 61458992c6bS猫头猫 try { 61558992c6bS猫头猫 const funcCode = (await axios.get(url)).data; 61658992c6bS猫头猫 if (funcCode) { 61758992c6bS猫头猫 const plugin = new Plugin(funcCode, ''); 61858992c6bS猫头猫 const _pluginIndex = plugins.findIndex(p => p.hash === plugin.hash); 61958992c6bS猫头猫 if (_pluginIndex !== -1) { 6208b7ddca8S猫头猫 // 静默忽略 6218b7ddca8S猫头猫 return; 62258992c6bS猫头猫 } 62325c1bd29S猫头猫 const oldVersionPlugin = plugins.find(p => p.name === plugin.name); 62425c1bd29S猫头猫 if (oldVersionPlugin) { 62525c1bd29S猫头猫 if ( 62625c1bd29S猫头猫 compare( 62725c1bd29S猫头猫 oldVersionPlugin.instance.version ?? '', 62825c1bd29S猫头猫 plugin.instance.version ?? '', 62925c1bd29S猫头猫 '>', 63025c1bd29S猫头猫 ) 63125c1bd29S猫头猫 ) { 63225c1bd29S猫头猫 throw new Error('已安装更新版本的插件'); 63325c1bd29S猫头猫 } 63425c1bd29S猫头猫 } 63525c1bd29S猫头猫 63658992c6bS猫头猫 if (plugin.hash !== '') { 63758992c6bS猫头猫 const fn = nanoid(); 63858992c6bS猫头猫 const _pluginPath = `${pathConst.pluginPath}${fn}.js`; 63958992c6bS猫头猫 await writeFile(_pluginPath, funcCode, 'utf8'); 64058992c6bS猫头猫 plugin.path = _pluginPath; 64158992c6bS猫头猫 plugins = plugins.concat(plugin); 64225c1bd29S猫头猫 if (oldVersionPlugin) { 64325c1bd29S猫头猫 plugins = plugins.filter( 64425c1bd29S猫头猫 _ => _.hash !== oldVersionPlugin.hash, 64525c1bd29S猫头猫 ); 64625c1bd29S猫头猫 try { 64725c1bd29S猫头猫 await unlink(oldVersionPlugin.path); 64825c1bd29S猫头猫 } catch {} 64925c1bd29S猫头猫 } 65058992c6bS猫头猫 pluginStateMapper.notify(); 65158992c6bS猫头猫 return; 65258992c6bS猫头猫 } 65358992c6bS猫头猫 throw new Error('插件无法解析'); 65458992c6bS猫头猫 } 65525c1bd29S猫头猫 } catch (e: any) { 65658992c6bS猫头猫 errorLog('URL安装插件失败', e); 65725c1bd29S猫头猫 throw new Error(e?.message ?? ''); 65858992c6bS猫头猫 } 65958992c6bS猫头猫} 66058992c6bS猫头猫 661927dbe93S猫头猫/** 卸载插件 */ 662927dbe93S猫头猫async function uninstallPlugin(hash: string) { 663927dbe93S猫头猫 const targetIndex = plugins.findIndex(_ => _.hash === hash); 664927dbe93S猫头猫 if (targetIndex !== -1) { 665927dbe93S猫头猫 try { 66624e5e74aS猫头猫 const pluginName = plugins[targetIndex].name; 667927dbe93S猫头猫 await unlink(plugins[targetIndex].path); 668927dbe93S猫头猫 plugins = plugins.filter(_ => _.hash !== hash); 669927dbe93S猫头猫 pluginStateMapper.notify(); 67024e5e74aS猫头猫 if (plugins.every(_ => _.name !== pluginName)) { 67124e5e74aS猫头猫 await MediaMeta.removePlugin(pluginName); 67224e5e74aS猫头猫 } 673927dbe93S猫头猫 } catch {} 674927dbe93S猫头猫 } 675927dbe93S猫头猫} 676927dbe93S猫头猫 67708882a77S猫头猫async function uninstallAllPlugins() { 67808882a77S猫头猫 await Promise.all( 67908882a77S猫头猫 plugins.map(async plugin => { 68008882a77S猫头猫 try { 68108882a77S猫头猫 const pluginName = plugin.name; 68208882a77S猫头猫 await unlink(plugin.path); 68308882a77S猫头猫 await MediaMeta.removePlugin(pluginName); 68408882a77S猫头猫 } catch (e) {} 68508882a77S猫头猫 }), 68608882a77S猫头猫 ); 68708882a77S猫头猫 plugins = []; 68808882a77S猫头猫 pluginStateMapper.notify(); 68908882a77S猫头猫} 69008882a77S猫头猫 69125c1bd29S猫头猫async function updatePlugin(plugin: Plugin) { 69225c1bd29S猫头猫 const updateUrl = plugin.instance.srcUrl; 69325c1bd29S猫头猫 if (!updateUrl) { 69425c1bd29S猫头猫 throw new Error('没有更新源'); 69525c1bd29S猫头猫 } 69625c1bd29S猫头猫 try { 69725c1bd29S猫头猫 await installPluginFromUrl(updateUrl); 69825c1bd29S猫头猫 } catch (e: any) { 69925c1bd29S猫头猫 if (e.message === '插件已安装') { 70025c1bd29S猫头猫 throw new Error('当前已是最新版本'); 70125c1bd29S猫头猫 } else { 70225c1bd29S猫头猫 throw e; 70325c1bd29S猫头猫 } 70425c1bd29S猫头猫 } 70525c1bd29S猫头猫} 70625c1bd29S猫头猫 707927dbe93S猫头猫function getByMedia(mediaItem: ICommon.IMediaBase) { 708927dbe93S猫头猫 return getByName(mediaItem.platform); 709927dbe93S猫头猫} 710927dbe93S猫头猫 711927dbe93S猫头猫function getByHash(hash: string) { 7127993f90eS猫头猫 return hash === localPluginHash 7137993f90eS猫头猫 ? localFilePlugin 7147993f90eS猫头猫 : plugins.find(_ => _.hash === hash); 715927dbe93S猫头猫} 716927dbe93S猫头猫 717927dbe93S猫头猫function getByName(name: string) { 7187993f90eS猫头猫 return name === localPluginPlatform 7190e4173cdS猫头猫 ? localFilePlugin 7200e4173cdS猫头猫 : plugins.find(_ => _.name === name); 721927dbe93S猫头猫} 722927dbe93S猫头猫 723927dbe93S猫头猫function getValidPlugins() { 724927dbe93S猫头猫 return plugins.filter(_ => _.state === 'enabled'); 725927dbe93S猫头猫} 726927dbe93S猫头猫 727efb9da24S猫头猫function getSearchablePlugins() { 728efb9da24S猫头猫 return plugins.filter(_ => _.state === 'enabled' && _.instance.search); 729efb9da24S猫头猫} 730efb9da24S猫头猫 731927dbe93S猫头猫const PluginManager = { 732927dbe93S猫头猫 setup, 733927dbe93S猫头猫 installPlugin, 73458992c6bS猫头猫 installPluginFromUrl, 73525c1bd29S猫头猫 updatePlugin, 736927dbe93S猫头猫 uninstallPlugin, 737927dbe93S猫头猫 getByMedia, 738927dbe93S猫头猫 getByHash, 739927dbe93S猫头猫 getByName, 740927dbe93S猫头猫 getValidPlugins, 741efb9da24S猫头猫 getSearchablePlugins, 7425276aef9S猫头猫 usePlugins: pluginStateMapper.useMappedState, 74308882a77S猫头猫 uninstallAllPlugins, 7445276aef9S猫头猫}; 745927dbe93S猫头猫 746927dbe93S猫头猫export default PluginManager; 747