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猫头猫 55*d5bfeb7eS猫头猫//#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; 73*d5bfeb7eS猫头猫 /** 用户输入 */ 74*d5bfeb7eS猫头猫 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猫头猫} 166*d5bfeb7eS猫头猫//#endregion 167927dbe93S猫头猫 168*d5bfeb7eS猫头猫//#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); 22848f4b873S猫头猫 const pluginCacheControl = this.plugin.instance.cacheControl; 229cfa0fc07S猫头猫 if ( 230cfa0fc07S猫头猫 mediaCache && 231cfa0fc07S猫头猫 mediaCache?.url && 23248f4b873S猫头猫 (pluginCacheControl === CacheControl.Cache || 23348f4b873S猫头猫 (pluginCacheControl === CacheControl.NoCache && 234ef714860S猫头猫 Network.isOffline())) 235cfa0fc07S猫头猫 ) { 2365276aef9S猫头猫 trace('播放', '缓存播放'); 237927dbe93S猫头猫 return { 238927dbe93S猫头猫 url: mediaCache.url, 239927dbe93S猫头猫 headers: mediaCache.headers, 2404060c00aS猫头猫 userAgent: 2414060c00aS猫头猫 mediaCache.userAgent ?? mediaCache.headers?.['user-agent'], 242927dbe93S猫头猫 }; 243927dbe93S猫头猫 } 244927dbe93S猫头猫 // 3. 插件解析 24520e6a092S猫头猫 if (!this.plugin.instance.getMediaSource) { 246927dbe93S猫头猫 return {url: musicItem.url}; 247927dbe93S猫头猫 } 248927dbe93S猫头猫 try { 24948f4b873S猫头猫 const {url, headers} = 25020e6a092S猫头猫 (await this.plugin.instance.getMediaSource(musicItem)) ?? {}; 251927dbe93S猫头猫 if (!url) { 252927dbe93S猫头猫 throw new Error(); 253927dbe93S猫头猫 } 2545276aef9S猫头猫 trace('播放', '插件播放'); 255927dbe93S猫头猫 const result = { 256927dbe93S猫头猫 url, 257927dbe93S猫头猫 headers, 258927dbe93S猫头猫 userAgent: headers?.['user-agent'], 259cfa0fc07S猫头猫 } as IPlugin.IMediaSourceResult; 260927dbe93S猫头猫 26148f4b873S猫头猫 if (pluginCacheControl !== CacheControl.NoStore) { 262927dbe93S猫头猫 Cache.update(musicItem, result); 263752ffc5aS猫头猫 } 264cfa0fc07S猫头猫 265927dbe93S猫头猫 return result; 266927dbe93S猫头猫 } catch (e: any) { 267927dbe93S猫头猫 if (retryCount > 0) { 268927dbe93S猫头猫 await delay(150); 26920e6a092S猫头猫 return this.getMediaSource(musicItem, --retryCount); 270927dbe93S猫头猫 } 271927dbe93S猫头猫 errorLog('获取真实源失败', e?.message); 272927dbe93S猫头猫 throw e; 273927dbe93S猫头猫 } 274927dbe93S猫头猫 } 275927dbe93S猫头猫 276927dbe93S猫头猫 /** 获取音乐详情 */ 277927dbe93S猫头猫 async getMusicInfo( 278927dbe93S猫头猫 musicItem: ICommon.IMediaBase, 27974d0cf81S猫头猫 ): Promise<Partial<IMusic.IMusicItem> | null> { 280927dbe93S猫头猫 if (!this.plugin.instance.getMusicInfo) { 28174d0cf81S猫头猫 return {}; 282927dbe93S猫头猫 } 28374d0cf81S猫头猫 try { 284927dbe93S猫头猫 return ( 285927dbe93S猫头猫 this.plugin.instance.getMusicInfo( 2867993f90eS猫头猫 resetMediaItem(musicItem, undefined, true), 28774d0cf81S猫头猫 ) ?? {} 288927dbe93S猫头猫 ); 28974d0cf81S猫头猫 } catch (e) { 29074d0cf81S猫头猫 console.log(e); 29174d0cf81S猫头猫 return {}; 29274d0cf81S猫头猫 } 293927dbe93S猫头猫 } 294927dbe93S猫头猫 295927dbe93S猫头猫 /** 获取歌词 */ 296927dbe93S猫头猫 async getLyric( 297927dbe93S猫头猫 musicItem: IMusic.IMusicItemBase, 298927dbe93S猫头猫 from?: IMusic.IMusicItemBase, 299927dbe93S猫头猫 ): Promise<ILyric.ILyricSource | null> { 300927dbe93S猫头猫 // 1.额外存储的meta信息 301927dbe93S猫头猫 const meta = MediaMeta.get(musicItem); 302927dbe93S猫头猫 if (meta && meta.associatedLrc) { 303927dbe93S猫头猫 // 有关联歌词 304927dbe93S猫头猫 if ( 305927dbe93S猫头猫 isSameMediaItem(musicItem, from) || 306927dbe93S猫头猫 isSameMediaItem(meta.associatedLrc, musicItem) 307927dbe93S猫头猫 ) { 308927dbe93S猫头猫 // 形成环路,断开当前的环 309927dbe93S猫头猫 await MediaMeta.update(musicItem, { 310927dbe93S猫头猫 associatedLrc: undefined, 311927dbe93S猫头猫 }); 312927dbe93S猫头猫 // 无歌词 313927dbe93S猫头猫 return null; 314927dbe93S猫头猫 } 315927dbe93S猫头猫 // 获取关联歌词 3167a91f04fS猫头猫 const associatedMeta = MediaMeta.get(meta.associatedLrc) ?? {}; 3174060c00aS猫头猫 const result = await this.getLyric( 3187a91f04fS猫头猫 {...meta.associatedLrc, ...associatedMeta}, 3194060c00aS猫头猫 from ?? musicItem, 3204060c00aS猫头猫 ); 321927dbe93S猫头猫 if (result) { 322927dbe93S猫头猫 // 如果有关联歌词,就返回关联歌词,深度优先 323927dbe93S猫头猫 return result; 324927dbe93S猫头猫 } 325927dbe93S猫头猫 } 326927dbe93S猫头猫 const cache = Cache.get(musicItem); 327927dbe93S猫头猫 let rawLrc = meta?.rawLrc || musicItem.rawLrc || cache?.rawLrc; 328927dbe93S猫头猫 let lrcUrl = meta?.lrc || musicItem.lrc || cache?.lrc; 329927dbe93S猫头猫 // 如果存在文本 330927dbe93S猫头猫 if (rawLrc) { 331927dbe93S猫头猫 return { 332927dbe93S猫头猫 rawLrc, 333927dbe93S猫头猫 lrc: lrcUrl, 334927dbe93S猫头猫 }; 335927dbe93S猫头猫 } 336927dbe93S猫头猫 // 2.本地缓存 337927dbe93S猫头猫 const localLrc = 3380e4173cdS猫头猫 meta?.[internalSerializeKey]?.local?.localLrc || 3390e4173cdS猫头猫 cache?.[internalSerializeKey]?.local?.localLrc; 340927dbe93S猫头猫 if (localLrc && (await exists(localLrc))) { 341927dbe93S猫头猫 rawLrc = await readFile(localLrc, 'utf8'); 342927dbe93S猫头猫 return { 343927dbe93S猫头猫 rawLrc, 344927dbe93S猫头猫 lrc: lrcUrl, 345927dbe93S猫头猫 }; 346927dbe93S猫头猫 } 347927dbe93S猫头猫 // 3.优先使用url 348927dbe93S猫头猫 if (lrcUrl) { 349927dbe93S猫头猫 try { 350927dbe93S猫头猫 // 需要超时时间 axios timeout 但是没生效 3512a3194f5S猫头猫 rawLrc = (await axios.get(lrcUrl, {timeout: 1500})).data; 352927dbe93S猫头猫 return { 353927dbe93S猫头猫 rawLrc, 354927dbe93S猫头猫 lrc: lrcUrl, 355927dbe93S猫头猫 }; 356927dbe93S猫头猫 } catch { 357927dbe93S猫头猫 lrcUrl = undefined; 358927dbe93S猫头猫 } 359927dbe93S猫头猫 } 360927dbe93S猫头猫 // 4. 如果地址失效 361927dbe93S猫头猫 if (!lrcUrl) { 362927dbe93S猫头猫 // 插件获得url 363927dbe93S猫头猫 try { 3647a91f04fS猫头猫 let lrcSource; 3657a91f04fS猫头猫 if (from) { 3667a91f04fS猫头猫 lrcSource = await PluginManager.getByMedia( 3677a91f04fS猫头猫 musicItem, 3687a91f04fS猫头猫 )?.instance?.getLyric?.( 369927dbe93S猫头猫 resetMediaItem(musicItem, undefined, true), 370927dbe93S猫头猫 ); 3717a91f04fS猫头猫 } else { 3727a91f04fS猫头猫 lrcSource = await this.plugin.instance?.getLyric?.( 3737a91f04fS猫头猫 resetMediaItem(musicItem, undefined, true), 3747a91f04fS猫头猫 ); 3757a91f04fS猫头猫 } 3767a91f04fS猫头猫 377927dbe93S猫头猫 rawLrc = lrcSource?.rawLrc; 378927dbe93S猫头猫 lrcUrl = lrcSource?.lrc; 379927dbe93S猫头猫 } catch (e: any) { 380927dbe93S猫头猫 trace('插件获取歌词失败', e?.message, 'error'); 381927dbe93S猫头猫 } 382927dbe93S猫头猫 } 383927dbe93S猫头猫 // 5. 最后一次请求 384927dbe93S猫头猫 if (rawLrc || lrcUrl) { 385927dbe93S猫头猫 const filename = `${pathConst.lrcCachePath}${nanoid()}.lrc`; 386927dbe93S猫头猫 if (lrcUrl) { 387927dbe93S猫头猫 try { 3882a3194f5S猫头猫 rawLrc = (await axios.get(lrcUrl, {timeout: 1500})).data; 389927dbe93S猫头猫 } catch {} 390927dbe93S猫头猫 } 391927dbe93S猫头猫 if (rawLrc) { 392927dbe93S猫头猫 await writeFile(filename, rawLrc, 'utf8'); 393927dbe93S猫头猫 // 写入缓存 394927dbe93S猫头猫 Cache.update(musicItem, [ 3950e4173cdS猫头猫 [`${internalSerializeKey}.local.localLrc`, filename], 396927dbe93S猫头猫 ]); 397927dbe93S猫头猫 // 如果有meta 398927dbe93S猫头猫 if (meta) { 399927dbe93S猫头猫 MediaMeta.update(musicItem, [ 4000e4173cdS猫头猫 [`${internalSerializeKey}.local.localLrc`, filename], 401927dbe93S猫头猫 ]); 402927dbe93S猫头猫 } 403927dbe93S猫头猫 return { 404927dbe93S猫头猫 rawLrc, 405927dbe93S猫头猫 lrc: lrcUrl, 406927dbe93S猫头猫 }; 407927dbe93S猫头猫 } 408927dbe93S猫头猫 } 409927dbe93S猫头猫 410927dbe93S猫头猫 return null; 411927dbe93S猫头猫 } 412927dbe93S猫头猫 413927dbe93S猫头猫 /** 获取歌词文本 */ 414927dbe93S猫头猫 async getLyricText( 415927dbe93S猫头猫 musicItem: IMusic.IMusicItem, 416927dbe93S猫头猫 ): Promise<string | undefined> { 417927dbe93S猫头猫 return (await this.getLyric(musicItem))?.rawLrc; 418927dbe93S猫头猫 } 419927dbe93S猫头猫 420927dbe93S猫头猫 /** 获取专辑信息 */ 421927dbe93S猫头猫 async getAlbumInfo( 422927dbe93S猫头猫 albumItem: IAlbum.IAlbumItemBase, 423927dbe93S猫头猫 ): Promise<IAlbum.IAlbumItem | null> { 424927dbe93S猫头猫 if (!this.plugin.instance.getAlbumInfo) { 425927dbe93S猫头猫 return {...albumItem, musicList: []}; 426927dbe93S猫头猫 } 427927dbe93S猫头猫 try { 428927dbe93S猫头猫 const result = await this.plugin.instance.getAlbumInfo( 429927dbe93S猫头猫 resetMediaItem(albumItem, undefined, true), 430927dbe93S猫头猫 ); 4315276aef9S猫头猫 if (!result) { 4325276aef9S猫头猫 throw new Error(); 4335276aef9S猫头猫 } 434927dbe93S猫头猫 result?.musicList?.forEach(_ => { 435927dbe93S猫头猫 resetMediaItem(_, this.plugin.name); 436927dbe93S猫头猫 }); 4375276aef9S猫头猫 4385276aef9S猫头猫 return {...albumItem, ...result}; 4394394410dS猫头猫 } catch (e: any) { 4404394410dS猫头猫 trace('获取专辑信息失败', e?.message); 441927dbe93S猫头猫 return {...albumItem, musicList: []}; 442927dbe93S猫头猫 } 443927dbe93S猫头猫 } 444927dbe93S猫头猫 445927dbe93S猫头猫 /** 查询作者信息 */ 446efb9da24S猫头猫 async getArtistWorks<T extends IArtist.ArtistMediaType>( 447927dbe93S猫头猫 artistItem: IArtist.IArtistItem, 448927dbe93S猫头猫 page: number, 449927dbe93S猫头猫 type: T, 450927dbe93S猫头猫 ): Promise<IPlugin.ISearchResult<T>> { 451efb9da24S猫头猫 if (!this.plugin.instance.getArtistWorks) { 452927dbe93S猫头猫 return { 453927dbe93S猫头猫 isEnd: true, 454927dbe93S猫头猫 data: [], 455927dbe93S猫头猫 }; 456927dbe93S猫头猫 } 457927dbe93S猫头猫 try { 458efb9da24S猫头猫 const result = await this.plugin.instance.getArtistWorks( 459927dbe93S猫头猫 artistItem, 460927dbe93S猫头猫 page, 461927dbe93S猫头猫 type, 462927dbe93S猫头猫 ); 463927dbe93S猫头猫 if (!result.data) { 464927dbe93S猫头猫 return { 465927dbe93S猫头猫 isEnd: true, 466927dbe93S猫头猫 data: [], 467927dbe93S猫头猫 }; 468927dbe93S猫头猫 } 469927dbe93S猫头猫 result.data?.forEach(_ => resetMediaItem(_, this.plugin.name)); 470927dbe93S猫头猫 return { 471927dbe93S猫头猫 isEnd: result.isEnd ?? true, 472927dbe93S猫头猫 data: result.data, 473927dbe93S猫头猫 }; 4744394410dS猫头猫 } catch (e: any) { 4754394410dS猫头猫 trace('查询作者信息失败', e?.message); 476927dbe93S猫头猫 throw e; 477927dbe93S猫头猫 } 478927dbe93S猫头猫 } 47908380090S猫头猫 48008380090S猫头猫 /** 导入歌单 */ 48108380090S猫头猫 async importMusicSheet(urlLike: string): Promise<IMusic.IMusicItem[]> { 48208380090S猫头猫 try { 48308380090S猫头猫 const result = 48408380090S猫头猫 (await this.plugin.instance?.importMusicSheet?.(urlLike)) ?? []; 48508380090S猫头猫 result.forEach(_ => resetMediaItem(_, this.plugin.name)); 48608380090S猫头猫 return result; 4870e4173cdS猫头猫 } catch (e) { 4880e4173cdS猫头猫 console.log(e); 48908380090S猫头猫 return []; 49008380090S猫头猫 } 49108380090S猫头猫 } 4924d9d3c4cS猫头猫 /** 导入单曲 */ 4934d9d3c4cS猫头猫 async importMusicItem(urlLike: string): Promise<IMusic.IMusicItem | null> { 4944d9d3c4cS猫头猫 try { 4954d9d3c4cS猫头猫 const result = await this.plugin.instance?.importMusicItem?.( 4964d9d3c4cS猫头猫 urlLike, 4974d9d3c4cS猫头猫 ); 4984d9d3c4cS猫头猫 if (!result) { 4994d9d3c4cS猫头猫 throw new Error(); 5004d9d3c4cS猫头猫 } 5014d9d3c4cS猫头猫 resetMediaItem(result, this.plugin.name); 5024d9d3c4cS猫头猫 return result; 5034d9d3c4cS猫头猫 } catch { 5044d9d3c4cS猫头猫 return null; 5054d9d3c4cS猫头猫 } 5064d9d3c4cS猫头猫 } 507927dbe93S猫头猫} 508*d5bfeb7eS猫头猫//#endregion 5091a5528a0S猫头猫 510927dbe93S猫头猫let plugins: Array<Plugin> = []; 511927dbe93S猫头猫const pluginStateMapper = new StateMapper(() => plugins); 51274d0cf81S猫头猫 513*d5bfeb7eS猫头猫//#region 本地音乐插件 51474d0cf81S猫头猫/** 本地插件 */ 51574d0cf81S猫头猫const localFilePlugin = new Plugin(function () { 5160e4173cdS猫头猫 return { 517*d5bfeb7eS猫头猫 platform: localPluginPlatform, 51874d0cf81S猫头猫 _path: '', 51974d0cf81S猫头猫 async getMusicInfo(musicBase) { 52074d0cf81S猫头猫 const localPath = getInternalData<string>( 52174d0cf81S猫头猫 musicBase, 52274d0cf81S猫头猫 InternalDataType.LOCALPATH, 5230e4173cdS猫头猫 ); 52474d0cf81S猫头猫 if (localPath) { 52574d0cf81S猫头猫 const coverImg = await Mp3Util.getMediaCoverImg(localPath); 52674d0cf81S猫头猫 return { 52774d0cf81S猫头猫 artwork: coverImg, 52874d0cf81S猫头猫 }; 52974d0cf81S猫头猫 } 53074d0cf81S猫头猫 return null; 53174d0cf81S猫头猫 }, 5327993f90eS猫头猫 async getLyric(musicBase) { 5337993f90eS猫头猫 const localPath = getInternalData<string>( 5347993f90eS猫头猫 musicBase, 5357993f90eS猫头猫 InternalDataType.LOCALPATH, 5367993f90eS猫头猫 ); 5377993f90eS猫头猫 if (localPath) { 5387993f90eS猫头猫 const rawLrc = await Mp3Util.getLyric(localPath); 5397993f90eS猫头猫 return { 5407993f90eS猫头猫 rawLrc, 5417993f90eS猫头猫 }; 5427993f90eS猫头猫 } 5437993f90eS猫头猫 return null; 5447993f90eS猫头猫 }, 54574d0cf81S猫头猫 }; 54674d0cf81S猫头猫}, ''); 5477993f90eS猫头猫localFilePlugin.hash = localPluginHash; 548927dbe93S猫头猫 549*d5bfeb7eS猫头猫//#endregion 550*d5bfeb7eS猫头猫 551927dbe93S猫头猫async function setup() { 552927dbe93S猫头猫 const _plugins: Array<Plugin> = []; 553927dbe93S猫头猫 try { 554927dbe93S猫头猫 // 加载插件 555927dbe93S猫头猫 const pluginsPaths = await readDir(pathConst.pluginPath); 556927dbe93S猫头猫 for (let i = 0; i < pluginsPaths.length; ++i) { 557927dbe93S猫头猫 const _pluginUrl = pluginsPaths[i]; 5581e263108S猫头猫 trace('初始化插件', _pluginUrl); 5591e263108S猫头猫 if ( 5601e263108S猫头猫 _pluginUrl.isFile() && 5611e263108S猫头猫 (_pluginUrl.name?.endsWith?.('.js') || 5621e263108S猫头猫 _pluginUrl.path?.endsWith?.('.js')) 5631e263108S猫头猫 ) { 564927dbe93S猫头猫 const funcCode = await readFile(_pluginUrl.path, 'utf8'); 565927dbe93S猫头猫 const plugin = new Plugin(funcCode, _pluginUrl.path); 5664060c00aS猫头猫 const _pluginIndex = _plugins.findIndex( 5674060c00aS猫头猫 p => p.hash === plugin.hash, 5684060c00aS猫头猫 ); 569927dbe93S猫头猫 if (_pluginIndex !== -1) { 570927dbe93S猫头猫 // 重复插件,直接忽略 571927dbe93S猫头猫 return; 572927dbe93S猫头猫 } 573927dbe93S猫头猫 plugin.hash !== '' && _plugins.push(plugin); 574927dbe93S猫头猫 } 575927dbe93S猫头猫 } 576927dbe93S猫头猫 577927dbe93S猫头猫 plugins = _plugins; 578927dbe93S猫头猫 pluginStateMapper.notify(); 579927dbe93S猫头猫 } catch (e: any) { 5804060c00aS猫头猫 ToastAndroid.show( 5814060c00aS猫头猫 `插件初始化失败:${e?.message ?? e}`, 5824060c00aS猫头猫 ToastAndroid.LONG, 5834060c00aS猫头猫 ); 5841a5528a0S猫头猫 errorLog('插件初始化失败', e?.message); 585927dbe93S猫头猫 throw e; 586927dbe93S猫头猫 } 587927dbe93S猫头猫} 588927dbe93S猫头猫 589927dbe93S猫头猫// 安装插件 590927dbe93S猫头猫async function installPlugin(pluginPath: string) { 59122c09412S猫头猫 // if (pluginPath.endsWith('.js')) { 592927dbe93S猫头猫 const funcCode = await readFile(pluginPath, 'utf8'); 593927dbe93S猫头猫 const plugin = new Plugin(funcCode, pluginPath); 594927dbe93S猫头猫 const _pluginIndex = plugins.findIndex(p => p.hash === plugin.hash); 595927dbe93S猫头猫 if (_pluginIndex !== -1) { 5964d9d3c4cS猫头猫 throw new Error('插件已安装'); 597927dbe93S猫头猫 } 598927dbe93S猫头猫 if (plugin.hash !== '') { 599927dbe93S猫头猫 const fn = nanoid(); 600927dbe93S猫头猫 const _pluginPath = `${pathConst.pluginPath}${fn}.js`; 601927dbe93S猫头猫 await copyFile(pluginPath, _pluginPath); 602927dbe93S猫头猫 plugin.path = _pluginPath; 603927dbe93S猫头猫 plugins = plugins.concat(plugin); 604927dbe93S猫头猫 pluginStateMapper.notify(); 6054d9d3c4cS猫头猫 return; 606927dbe93S猫头猫 } 6074d9d3c4cS猫头猫 throw new Error('插件无法解析'); 60822c09412S猫头猫 // } 60922c09412S猫头猫 // throw new Error('插件不存在'); 610927dbe93S猫头猫} 611927dbe93S猫头猫 61258992c6bS猫头猫async function installPluginFromUrl(url: string) { 61358992c6bS猫头猫 try { 61458992c6bS猫头猫 const funcCode = (await axios.get(url)).data; 61558992c6bS猫头猫 if (funcCode) { 61658992c6bS猫头猫 const plugin = new Plugin(funcCode, ''); 61758992c6bS猫头猫 const _pluginIndex = plugins.findIndex(p => p.hash === plugin.hash); 61858992c6bS猫头猫 if (_pluginIndex !== -1) { 6198b7ddca8S猫头猫 // 静默忽略 6208b7ddca8S猫头猫 return; 62158992c6bS猫头猫 } 62225c1bd29S猫头猫 const oldVersionPlugin = plugins.find(p => p.name === plugin.name); 62325c1bd29S猫头猫 if (oldVersionPlugin) { 62425c1bd29S猫头猫 if ( 62525c1bd29S猫头猫 compare( 62625c1bd29S猫头猫 oldVersionPlugin.instance.version ?? '', 62725c1bd29S猫头猫 plugin.instance.version ?? '', 62825c1bd29S猫头猫 '>', 62925c1bd29S猫头猫 ) 63025c1bd29S猫头猫 ) { 63125c1bd29S猫头猫 throw new Error('已安装更新版本的插件'); 63225c1bd29S猫头猫 } 63325c1bd29S猫头猫 } 63425c1bd29S猫头猫 63558992c6bS猫头猫 if (plugin.hash !== '') { 63658992c6bS猫头猫 const fn = nanoid(); 63758992c6bS猫头猫 const _pluginPath = `${pathConst.pluginPath}${fn}.js`; 63858992c6bS猫头猫 await writeFile(_pluginPath, funcCode, 'utf8'); 63958992c6bS猫头猫 plugin.path = _pluginPath; 64058992c6bS猫头猫 plugins = plugins.concat(plugin); 64125c1bd29S猫头猫 if (oldVersionPlugin) { 64225c1bd29S猫头猫 plugins = plugins.filter( 64325c1bd29S猫头猫 _ => _.hash !== oldVersionPlugin.hash, 64425c1bd29S猫头猫 ); 64525c1bd29S猫头猫 try { 64625c1bd29S猫头猫 await unlink(oldVersionPlugin.path); 64725c1bd29S猫头猫 } catch {} 64825c1bd29S猫头猫 } 64958992c6bS猫头猫 pluginStateMapper.notify(); 65058992c6bS猫头猫 return; 65158992c6bS猫头猫 } 65258992c6bS猫头猫 throw new Error('插件无法解析'); 65358992c6bS猫头猫 } 65425c1bd29S猫头猫 } catch (e: any) { 65558992c6bS猫头猫 errorLog('URL安装插件失败', e); 65625c1bd29S猫头猫 throw new Error(e?.message ?? ''); 65758992c6bS猫头猫 } 65858992c6bS猫头猫} 65958992c6bS猫头猫 660927dbe93S猫头猫/** 卸载插件 */ 661927dbe93S猫头猫async function uninstallPlugin(hash: string) { 662927dbe93S猫头猫 const targetIndex = plugins.findIndex(_ => _.hash === hash); 663927dbe93S猫头猫 if (targetIndex !== -1) { 664927dbe93S猫头猫 try { 66524e5e74aS猫头猫 const pluginName = plugins[targetIndex].name; 666927dbe93S猫头猫 await unlink(plugins[targetIndex].path); 667927dbe93S猫头猫 plugins = plugins.filter(_ => _.hash !== hash); 668927dbe93S猫头猫 pluginStateMapper.notify(); 66924e5e74aS猫头猫 if (plugins.every(_ => _.name !== pluginName)) { 67024e5e74aS猫头猫 await MediaMeta.removePlugin(pluginName); 67124e5e74aS猫头猫 } 672927dbe93S猫头猫 } catch {} 673927dbe93S猫头猫 } 674927dbe93S猫头猫} 675927dbe93S猫头猫 67608882a77S猫头猫async function uninstallAllPlugins() { 67708882a77S猫头猫 await Promise.all( 67808882a77S猫头猫 plugins.map(async plugin => { 67908882a77S猫头猫 try { 68008882a77S猫头猫 const pluginName = plugin.name; 68108882a77S猫头猫 await unlink(plugin.path); 68208882a77S猫头猫 await MediaMeta.removePlugin(pluginName); 68308882a77S猫头猫 } catch (e) {} 68408882a77S猫头猫 }), 68508882a77S猫头猫 ); 68608882a77S猫头猫 plugins = []; 68708882a77S猫头猫 pluginStateMapper.notify(); 68808882a77S猫头猫} 68908882a77S猫头猫 69025c1bd29S猫头猫async function updatePlugin(plugin: Plugin) { 69125c1bd29S猫头猫 const updateUrl = plugin.instance.srcUrl; 69225c1bd29S猫头猫 if (!updateUrl) { 69325c1bd29S猫头猫 throw new Error('没有更新源'); 69425c1bd29S猫头猫 } 69525c1bd29S猫头猫 try { 69625c1bd29S猫头猫 await installPluginFromUrl(updateUrl); 69725c1bd29S猫头猫 } catch (e: any) { 69825c1bd29S猫头猫 if (e.message === '插件已安装') { 69925c1bd29S猫头猫 throw new Error('当前已是最新版本'); 70025c1bd29S猫头猫 } else { 70125c1bd29S猫头猫 throw e; 70225c1bd29S猫头猫 } 70325c1bd29S猫头猫 } 70425c1bd29S猫头猫} 70525c1bd29S猫头猫 706927dbe93S猫头猫function getByMedia(mediaItem: ICommon.IMediaBase) { 707927dbe93S猫头猫 return getByName(mediaItem.platform); 708927dbe93S猫头猫} 709927dbe93S猫头猫 710927dbe93S猫头猫function getByHash(hash: string) { 7117993f90eS猫头猫 return hash === localPluginHash 7127993f90eS猫头猫 ? localFilePlugin 7137993f90eS猫头猫 : plugins.find(_ => _.hash === hash); 714927dbe93S猫头猫} 715927dbe93S猫头猫 716927dbe93S猫头猫function getByName(name: string) { 7177993f90eS猫头猫 return name === localPluginPlatform 7180e4173cdS猫头猫 ? localFilePlugin 7190e4173cdS猫头猫 : plugins.find(_ => _.name === name); 720927dbe93S猫头猫} 721927dbe93S猫头猫 722927dbe93S猫头猫function getValidPlugins() { 723927dbe93S猫头猫 return plugins.filter(_ => _.state === 'enabled'); 724927dbe93S猫头猫} 725927dbe93S猫头猫 726efb9da24S猫头猫function getSearchablePlugins() { 727efb9da24S猫头猫 return plugins.filter(_ => _.state === 'enabled' && _.instance.search); 728efb9da24S猫头猫} 729efb9da24S猫头猫 730927dbe93S猫头猫const PluginManager = { 731927dbe93S猫头猫 setup, 732927dbe93S猫头猫 installPlugin, 73358992c6bS猫头猫 installPluginFromUrl, 73425c1bd29S猫头猫 updatePlugin, 735927dbe93S猫头猫 uninstallPlugin, 736927dbe93S猫头猫 getByMedia, 737927dbe93S猫头猫 getByHash, 738927dbe93S猫头猫 getByName, 739927dbe93S猫头猫 getValidPlugins, 740efb9da24S猫头猫 getSearchablePlugins, 7415276aef9S猫头猫 usePlugins: pluginStateMapper.useMappedState, 74208882a77S猫头猫 uninstallAllPlugins, 7435276aef9S猫头猫}; 744927dbe93S猫头猫 745927dbe93S猫头猫export default PluginManager; 746