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'; 29*7993f90eS猫头猫import { 30*7993f90eS猫头猫 CacheControl, 31*7993f90eS猫头猫 internalSerializeKey, 32*7993f90eS猫头猫 localPluginHash, 33*7993f90eS猫头猫 localPluginPlatform, 34*7993f90eS猫头猫} 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猫头猫 55927dbe93S猫头猫export class Plugin { 56927dbe93S猫头猫 /** 插件名 */ 57927dbe93S猫头猫 public name: string; 58927dbe93S猫头猫 /** 插件的hash,作为唯一id */ 59927dbe93S猫头猫 public hash: string; 60927dbe93S猫头猫 /** 插件状态:激活、关闭、错误 */ 61927dbe93S猫头猫 public state: 'enabled' | 'disabled' | 'error'; 62927dbe93S猫头猫 /** 插件支持的搜索类型 */ 63927dbe93S猫头猫 public supportedSearchType?: string; 64927dbe93S猫头猫 /** 插件状态信息 */ 65927dbe93S猫头猫 public stateCode?: PluginStateCode; 66927dbe93S猫头猫 /** 插件的实例 */ 67927dbe93S猫头猫 public instance: IPlugin.IPluginInstance; 68927dbe93S猫头猫 /** 插件路径 */ 69927dbe93S猫头猫 public path: string; 70927dbe93S猫头猫 /** 插件方法 */ 71927dbe93S猫头猫 public methods: PluginMethods; 72927dbe93S猫头猫 7374d0cf81S猫头猫 constructor( 7474d0cf81S猫头猫 funcCode: string | (() => IPlugin.IPluginInstance), 7574d0cf81S猫头猫 pluginPath: string, 7674d0cf81S猫头猫 ) { 77927dbe93S猫头猫 this.state = 'enabled'; 78927dbe93S猫头猫 let _instance: IPlugin.IPluginInstance; 79927dbe93S猫头猫 try { 8074d0cf81S猫头猫 if (typeof funcCode === 'string') { 814060c00aS猫头猫 // eslint-disable-next-line no-new-func 82927dbe93S猫头猫 _instance = Function(` 83927dbe93S猫头猫 'use strict'; 84927dbe93S猫头猫 try { 85927dbe93S猫头猫 return ${funcCode}; 86927dbe93S猫头猫 } catch(e) { 87927dbe93S猫头猫 return null; 88927dbe93S猫头猫 } 897d7e864fS猫头猫 `)()({ 907d7e864fS猫头猫 CryptoJs, 917d7e864fS猫头猫 axios, 927d7e864fS猫头猫 dayjs, 937d7e864fS猫头猫 cheerio, 947d7e864fS猫头猫 bigInt, 957d7e864fS猫头猫 qs, 967d7e864fS猫头猫 he, 977d7e864fS猫头猫 CookieManager: { 987d7e864fS猫头猫 flush: CookieManager.flush, 997d7e864fS猫头猫 get: CookieManager.get, 1007d7e864fS猫头猫 }, 1017d7e864fS猫头猫 }); 10274d0cf81S猫头猫 } else { 10374d0cf81S猫头猫 _instance = funcCode(); 10474d0cf81S猫头猫 } 105927dbe93S猫头猫 this.checkValid(_instance); 106927dbe93S猫头猫 } catch (e: any) { 107927dbe93S猫头猫 this.state = 'error'; 108927dbe93S猫头猫 this.stateCode = PluginStateCode.CannotParse; 109927dbe93S猫头猫 if (e?.stateCode) { 110927dbe93S猫头猫 this.stateCode = e.stateCode; 111927dbe93S猫头猫 } 112927dbe93S猫头猫 errorLog(`${pluginPath}插件无法解析 `, { 113927dbe93S猫头猫 stateCode: this.stateCode, 114927dbe93S猫头猫 message: e?.message, 115927dbe93S猫头猫 stack: e?.stack, 116927dbe93S猫头猫 }); 117927dbe93S猫头猫 _instance = e?.instance ?? { 118927dbe93S猫头猫 _path: '', 119927dbe93S猫头猫 platform: '', 120927dbe93S猫头猫 appVersion: '', 12120e6a092S猫头猫 async getMediaSource() { 122927dbe93S猫头猫 return null; 123927dbe93S猫头猫 }, 124927dbe93S猫头猫 async search() { 125927dbe93S猫头猫 return {}; 126927dbe93S猫头猫 }, 127927dbe93S猫头猫 async getAlbumInfo() { 128927dbe93S猫头猫 return null; 129927dbe93S猫头猫 }, 130927dbe93S猫头猫 }; 131927dbe93S猫头猫 } 132927dbe93S猫头猫 this.instance = _instance; 133927dbe93S猫头猫 this.path = pluginPath; 134927dbe93S猫头猫 this.name = _instance.platform; 135927dbe93S猫头猫 if (this.instance.platform === '') { 136927dbe93S猫头猫 this.hash = ''; 137927dbe93S猫头猫 } else { 13874d0cf81S猫头猫 if (typeof funcCode === 'string') { 139927dbe93S猫头猫 this.hash = sha256(funcCode).toString(); 14074d0cf81S猫头猫 } else { 14174d0cf81S猫头猫 this.hash = sha256(funcCode.toString()).toString(); 14274d0cf81S猫头猫 } 143927dbe93S猫头猫 } 144927dbe93S猫头猫 145927dbe93S猫头猫 // 放在最后 146927dbe93S猫头猫 this.methods = new PluginMethods(this); 147927dbe93S猫头猫 } 148927dbe93S猫头猫 149927dbe93S猫头猫 private checkValid(_instance: IPlugin.IPluginInstance) { 150927dbe93S猫头猫 /** 版本号校验 */ 151927dbe93S猫头猫 if ( 152927dbe93S猫头猫 _instance.appVersion && 153927dbe93S猫头猫 !satisfies(DeviceInfo.getVersion(), _instance.appVersion) 154927dbe93S猫头猫 ) { 155927dbe93S猫头猫 throw { 156927dbe93S猫头猫 instance: _instance, 157927dbe93S猫头猫 stateCode: PluginStateCode.VersionNotMatch, 158927dbe93S猫头猫 }; 159927dbe93S猫头猫 } 160927dbe93S猫头猫 return true; 161927dbe93S猫头猫 } 162927dbe93S猫头猫} 163927dbe93S猫头猫 164927dbe93S猫头猫/** 有缓存等信息 */ 165927dbe93S猫头猫class PluginMethods implements IPlugin.IPluginInstanceMethods { 166927dbe93S猫头猫 private plugin; 167927dbe93S猫头猫 constructor(plugin: Plugin) { 168927dbe93S猫头猫 this.plugin = plugin; 169927dbe93S猫头猫 } 170927dbe93S猫头猫 /** 搜索 */ 171927dbe93S猫头猫 async search<T extends ICommon.SupportMediaType>( 172927dbe93S猫头猫 query: string, 173927dbe93S猫头猫 page: number, 174927dbe93S猫头猫 type: T, 175927dbe93S猫头猫 ): Promise<IPlugin.ISearchResult<T>> { 176927dbe93S猫头猫 if (!this.plugin.instance.search) { 177927dbe93S猫头猫 return { 178927dbe93S猫头猫 isEnd: true, 179927dbe93S猫头猫 data: [], 180927dbe93S猫头猫 }; 181927dbe93S猫头猫 } 182927dbe93S猫头猫 1834060c00aS猫头猫 const result = 1844060c00aS猫头猫 (await this.plugin.instance.search(query, page, type)) ?? {}; 185927dbe93S猫头猫 if (Array.isArray(result.data)) { 186927dbe93S猫头猫 result.data.forEach(_ => { 187927dbe93S猫头猫 resetMediaItem(_, this.plugin.name); 188927dbe93S猫头猫 }); 189927dbe93S猫头猫 return { 190927dbe93S猫头猫 isEnd: result.isEnd ?? true, 191927dbe93S猫头猫 data: result.data, 192927dbe93S猫头猫 }; 193927dbe93S猫头猫 } 194927dbe93S猫头猫 return { 195927dbe93S猫头猫 isEnd: true, 196927dbe93S猫头猫 data: [], 197927dbe93S猫头猫 }; 198927dbe93S猫头猫 } 199927dbe93S猫头猫 200927dbe93S猫头猫 /** 获取真实源 */ 20120e6a092S猫头猫 async getMediaSource( 202927dbe93S猫头猫 musicItem: IMusic.IMusicItemBase, 203927dbe93S猫头猫 retryCount = 1, 20420e6a092S猫头猫 ): Promise<IPlugin.IMediaSourceResult> { 205927dbe93S猫头猫 // 1. 本地搜索 其实直接读mediameta就好了 206927dbe93S猫头猫 const localPath = 2070e4173cdS猫头猫 getInternalData<string>(musicItem, InternalDataType.LOCALPATH) ?? 2080e4173cdS猫头猫 getInternalData<string>( 2090e4173cdS猫头猫 LocalMusicSheet.isLocalMusic(musicItem), 2100e4173cdS猫头猫 InternalDataType.LOCALPATH, 2110e4173cdS猫头猫 ); 2120e4173cdS猫头猫 if (localPath && (await FileSystem.exists(localPath))) { 2130e4173cdS猫头猫 trace('本地播放', localPath); 214927dbe93S猫头猫 return { 215927dbe93S猫头猫 url: localPath, 216927dbe93S猫头猫 }; 217927dbe93S猫头猫 } 218*7993f90eS猫头猫 if (musicItem.platform === localPluginPlatform) { 219f5935920S猫头猫 throw new Error('本地音乐不存在'); 220f5935920S猫头猫 } 221927dbe93S猫头猫 // 2. 缓存播放 222927dbe93S猫头猫 const mediaCache = Cache.get(musicItem); 22348f4b873S猫头猫 const pluginCacheControl = this.plugin.instance.cacheControl; 224cfa0fc07S猫头猫 if ( 225cfa0fc07S猫头猫 mediaCache && 226cfa0fc07S猫头猫 mediaCache?.url && 22748f4b873S猫头猫 (pluginCacheControl === CacheControl.Cache || 22848f4b873S猫头猫 (pluginCacheControl === CacheControl.NoCache && 229ef714860S猫头猫 Network.isOffline())) 230cfa0fc07S猫头猫 ) { 2315276aef9S猫头猫 trace('播放', '缓存播放'); 232927dbe93S猫头猫 return { 233927dbe93S猫头猫 url: mediaCache.url, 234927dbe93S猫头猫 headers: mediaCache.headers, 2354060c00aS猫头猫 userAgent: 2364060c00aS猫头猫 mediaCache.userAgent ?? mediaCache.headers?.['user-agent'], 237927dbe93S猫头猫 }; 238927dbe93S猫头猫 } 239927dbe93S猫头猫 // 3. 插件解析 24020e6a092S猫头猫 if (!this.plugin.instance.getMediaSource) { 241927dbe93S猫头猫 return {url: musicItem.url}; 242927dbe93S猫头猫 } 243927dbe93S猫头猫 try { 24448f4b873S猫头猫 const {url, headers} = 24520e6a092S猫头猫 (await this.plugin.instance.getMediaSource(musicItem)) ?? {}; 246927dbe93S猫头猫 if (!url) { 247927dbe93S猫头猫 throw new Error(); 248927dbe93S猫头猫 } 2495276aef9S猫头猫 trace('播放', '插件播放'); 250927dbe93S猫头猫 const result = { 251927dbe93S猫头猫 url, 252927dbe93S猫头猫 headers, 253927dbe93S猫头猫 userAgent: headers?.['user-agent'], 254cfa0fc07S猫头猫 } as IPlugin.IMediaSourceResult; 255927dbe93S猫头猫 25648f4b873S猫头猫 if (pluginCacheControl !== CacheControl.NoStore) { 257927dbe93S猫头猫 Cache.update(musicItem, result); 258752ffc5aS猫头猫 } 259cfa0fc07S猫头猫 260927dbe93S猫头猫 return result; 261927dbe93S猫头猫 } catch (e: any) { 262927dbe93S猫头猫 if (retryCount > 0) { 263927dbe93S猫头猫 await delay(150); 26420e6a092S猫头猫 return this.getMediaSource(musicItem, --retryCount); 265927dbe93S猫头猫 } 266927dbe93S猫头猫 errorLog('获取真实源失败', e?.message); 267927dbe93S猫头猫 throw e; 268927dbe93S猫头猫 } 269927dbe93S猫头猫 } 270927dbe93S猫头猫 271927dbe93S猫头猫 /** 获取音乐详情 */ 272927dbe93S猫头猫 async getMusicInfo( 273927dbe93S猫头猫 musicItem: ICommon.IMediaBase, 27474d0cf81S猫头猫 ): Promise<Partial<IMusic.IMusicItem> | null> { 275927dbe93S猫头猫 if (!this.plugin.instance.getMusicInfo) { 27674d0cf81S猫头猫 return {}; 277927dbe93S猫头猫 } 27874d0cf81S猫头猫 try { 279927dbe93S猫头猫 return ( 280927dbe93S猫头猫 this.plugin.instance.getMusicInfo( 281*7993f90eS猫头猫 resetMediaItem(musicItem, undefined, true), 28274d0cf81S猫头猫 ) ?? {} 283927dbe93S猫头猫 ); 28474d0cf81S猫头猫 } catch (e) { 28574d0cf81S猫头猫 console.log(e); 28674d0cf81S猫头猫 return {}; 28774d0cf81S猫头猫 } 288927dbe93S猫头猫 } 289927dbe93S猫头猫 290927dbe93S猫头猫 /** 获取歌词 */ 291927dbe93S猫头猫 async getLyric( 292927dbe93S猫头猫 musicItem: IMusic.IMusicItemBase, 293927dbe93S猫头猫 from?: IMusic.IMusicItemBase, 294927dbe93S猫头猫 ): Promise<ILyric.ILyricSource | null> { 295927dbe93S猫头猫 // 1.额外存储的meta信息 296927dbe93S猫头猫 const meta = MediaMeta.get(musicItem); 297927dbe93S猫头猫 if (meta && meta.associatedLrc) { 298927dbe93S猫头猫 // 有关联歌词 299927dbe93S猫头猫 if ( 300927dbe93S猫头猫 isSameMediaItem(musicItem, from) || 301927dbe93S猫头猫 isSameMediaItem(meta.associatedLrc, musicItem) 302927dbe93S猫头猫 ) { 303927dbe93S猫头猫 // 形成环路,断开当前的环 304927dbe93S猫头猫 await MediaMeta.update(musicItem, { 305927dbe93S猫头猫 associatedLrc: undefined, 306927dbe93S猫头猫 }); 307927dbe93S猫头猫 // 无歌词 308927dbe93S猫头猫 return null; 309927dbe93S猫头猫 } 310927dbe93S猫头猫 // 获取关联歌词 3117a91f04fS猫头猫 const associatedMeta = MediaMeta.get(meta.associatedLrc) ?? {}; 3124060c00aS猫头猫 const result = await this.getLyric( 3137a91f04fS猫头猫 {...meta.associatedLrc, ...associatedMeta}, 3144060c00aS猫头猫 from ?? musicItem, 3154060c00aS猫头猫 ); 316927dbe93S猫头猫 if (result) { 317927dbe93S猫头猫 // 如果有关联歌词,就返回关联歌词,深度优先 318927dbe93S猫头猫 return result; 319927dbe93S猫头猫 } 320927dbe93S猫头猫 } 321927dbe93S猫头猫 const cache = Cache.get(musicItem); 322927dbe93S猫头猫 let rawLrc = meta?.rawLrc || musicItem.rawLrc || cache?.rawLrc; 323927dbe93S猫头猫 let lrcUrl = meta?.lrc || musicItem.lrc || cache?.lrc; 324927dbe93S猫头猫 // 如果存在文本 325927dbe93S猫头猫 if (rawLrc) { 326927dbe93S猫头猫 return { 327927dbe93S猫头猫 rawLrc, 328927dbe93S猫头猫 lrc: lrcUrl, 329927dbe93S猫头猫 }; 330927dbe93S猫头猫 } 331927dbe93S猫头猫 // 2.本地缓存 332927dbe93S猫头猫 const localLrc = 3330e4173cdS猫头猫 meta?.[internalSerializeKey]?.local?.localLrc || 3340e4173cdS猫头猫 cache?.[internalSerializeKey]?.local?.localLrc; 335927dbe93S猫头猫 if (localLrc && (await exists(localLrc))) { 336927dbe93S猫头猫 rawLrc = await readFile(localLrc, 'utf8'); 337927dbe93S猫头猫 return { 338927dbe93S猫头猫 rawLrc, 339927dbe93S猫头猫 lrc: lrcUrl, 340927dbe93S猫头猫 }; 341927dbe93S猫头猫 } 342927dbe93S猫头猫 // 3.优先使用url 343927dbe93S猫头猫 if (lrcUrl) { 344927dbe93S猫头猫 try { 345927dbe93S猫头猫 // 需要超时时间 axios timeout 但是没生效 3462a3194f5S猫头猫 rawLrc = (await axios.get(lrcUrl, {timeout: 1500})).data; 347927dbe93S猫头猫 return { 348927dbe93S猫头猫 rawLrc, 349927dbe93S猫头猫 lrc: lrcUrl, 350927dbe93S猫头猫 }; 351927dbe93S猫头猫 } catch { 352927dbe93S猫头猫 lrcUrl = undefined; 353927dbe93S猫头猫 } 354927dbe93S猫头猫 } 355927dbe93S猫头猫 // 4. 如果地址失效 356927dbe93S猫头猫 if (!lrcUrl) { 357927dbe93S猫头猫 // 插件获得url 358927dbe93S猫头猫 try { 3597a91f04fS猫头猫 let lrcSource; 3607a91f04fS猫头猫 if (from) { 3617a91f04fS猫头猫 lrcSource = await PluginManager.getByMedia( 3627a91f04fS猫头猫 musicItem, 3637a91f04fS猫头猫 )?.instance?.getLyric?.( 364927dbe93S猫头猫 resetMediaItem(musicItem, undefined, true), 365927dbe93S猫头猫 ); 3667a91f04fS猫头猫 } else { 3677a91f04fS猫头猫 lrcSource = await this.plugin.instance?.getLyric?.( 3687a91f04fS猫头猫 resetMediaItem(musicItem, undefined, true), 3697a91f04fS猫头猫 ); 3707a91f04fS猫头猫 } 3717a91f04fS猫头猫 372927dbe93S猫头猫 rawLrc = lrcSource?.rawLrc; 373927dbe93S猫头猫 lrcUrl = lrcSource?.lrc; 374927dbe93S猫头猫 } catch (e: any) { 375927dbe93S猫头猫 trace('插件获取歌词失败', e?.message, 'error'); 376927dbe93S猫头猫 } 377927dbe93S猫头猫 } 378927dbe93S猫头猫 // 5. 最后一次请求 379927dbe93S猫头猫 if (rawLrc || lrcUrl) { 380927dbe93S猫头猫 const filename = `${pathConst.lrcCachePath}${nanoid()}.lrc`; 381927dbe93S猫头猫 if (lrcUrl) { 382927dbe93S猫头猫 try { 3832a3194f5S猫头猫 rawLrc = (await axios.get(lrcUrl, {timeout: 1500})).data; 384927dbe93S猫头猫 } catch {} 385927dbe93S猫头猫 } 386927dbe93S猫头猫 if (rawLrc) { 387927dbe93S猫头猫 await writeFile(filename, rawLrc, 'utf8'); 388927dbe93S猫头猫 // 写入缓存 389927dbe93S猫头猫 Cache.update(musicItem, [ 3900e4173cdS猫头猫 [`${internalSerializeKey}.local.localLrc`, filename], 391927dbe93S猫头猫 ]); 392927dbe93S猫头猫 // 如果有meta 393927dbe93S猫头猫 if (meta) { 394927dbe93S猫头猫 MediaMeta.update(musicItem, [ 3950e4173cdS猫头猫 [`${internalSerializeKey}.local.localLrc`, filename], 396927dbe93S猫头猫 ]); 397927dbe93S猫头猫 } 398927dbe93S猫头猫 return { 399927dbe93S猫头猫 rawLrc, 400927dbe93S猫头猫 lrc: lrcUrl, 401927dbe93S猫头猫 }; 402927dbe93S猫头猫 } 403927dbe93S猫头猫 } 404927dbe93S猫头猫 405927dbe93S猫头猫 return null; 406927dbe93S猫头猫 } 407927dbe93S猫头猫 408927dbe93S猫头猫 /** 获取歌词文本 */ 409927dbe93S猫头猫 async getLyricText( 410927dbe93S猫头猫 musicItem: IMusic.IMusicItem, 411927dbe93S猫头猫 ): Promise<string | undefined> { 412927dbe93S猫头猫 return (await this.getLyric(musicItem))?.rawLrc; 413927dbe93S猫头猫 } 414927dbe93S猫头猫 415927dbe93S猫头猫 /** 获取专辑信息 */ 416927dbe93S猫头猫 async getAlbumInfo( 417927dbe93S猫头猫 albumItem: IAlbum.IAlbumItemBase, 418927dbe93S猫头猫 ): Promise<IAlbum.IAlbumItem | null> { 419927dbe93S猫头猫 if (!this.plugin.instance.getAlbumInfo) { 420927dbe93S猫头猫 return {...albumItem, musicList: []}; 421927dbe93S猫头猫 } 422927dbe93S猫头猫 try { 423927dbe93S猫头猫 const result = await this.plugin.instance.getAlbumInfo( 424927dbe93S猫头猫 resetMediaItem(albumItem, undefined, true), 425927dbe93S猫头猫 ); 4265276aef9S猫头猫 if (!result) { 4275276aef9S猫头猫 throw new Error(); 4285276aef9S猫头猫 } 429927dbe93S猫头猫 result?.musicList?.forEach(_ => { 430927dbe93S猫头猫 resetMediaItem(_, this.plugin.name); 431927dbe93S猫头猫 }); 4325276aef9S猫头猫 4335276aef9S猫头猫 return {...albumItem, ...result}; 4344394410dS猫头猫 } catch (e: any) { 4354394410dS猫头猫 trace('获取专辑信息失败', e?.message); 436927dbe93S猫头猫 return {...albumItem, musicList: []}; 437927dbe93S猫头猫 } 438927dbe93S猫头猫 } 439927dbe93S猫头猫 440927dbe93S猫头猫 /** 查询作者信息 */ 441efb9da24S猫头猫 async getArtistWorks<T extends IArtist.ArtistMediaType>( 442927dbe93S猫头猫 artistItem: IArtist.IArtistItem, 443927dbe93S猫头猫 page: number, 444927dbe93S猫头猫 type: T, 445927dbe93S猫头猫 ): Promise<IPlugin.ISearchResult<T>> { 446efb9da24S猫头猫 if (!this.plugin.instance.getArtistWorks) { 447927dbe93S猫头猫 return { 448927dbe93S猫头猫 isEnd: true, 449927dbe93S猫头猫 data: [], 450927dbe93S猫头猫 }; 451927dbe93S猫头猫 } 452927dbe93S猫头猫 try { 453efb9da24S猫头猫 const result = await this.plugin.instance.getArtistWorks( 454927dbe93S猫头猫 artistItem, 455927dbe93S猫头猫 page, 456927dbe93S猫头猫 type, 457927dbe93S猫头猫 ); 458927dbe93S猫头猫 if (!result.data) { 459927dbe93S猫头猫 return { 460927dbe93S猫头猫 isEnd: true, 461927dbe93S猫头猫 data: [], 462927dbe93S猫头猫 }; 463927dbe93S猫头猫 } 464927dbe93S猫头猫 result.data?.forEach(_ => resetMediaItem(_, this.plugin.name)); 465927dbe93S猫头猫 return { 466927dbe93S猫头猫 isEnd: result.isEnd ?? true, 467927dbe93S猫头猫 data: result.data, 468927dbe93S猫头猫 }; 4694394410dS猫头猫 } catch (e: any) { 4704394410dS猫头猫 trace('查询作者信息失败', e?.message); 471927dbe93S猫头猫 throw e; 472927dbe93S猫头猫 } 473927dbe93S猫头猫 } 47408380090S猫头猫 47508380090S猫头猫 /** 导入歌单 */ 47608380090S猫头猫 async importMusicSheet(urlLike: string): Promise<IMusic.IMusicItem[]> { 47708380090S猫头猫 try { 47808380090S猫头猫 const result = 47908380090S猫头猫 (await this.plugin.instance?.importMusicSheet?.(urlLike)) ?? []; 48008380090S猫头猫 result.forEach(_ => resetMediaItem(_, this.plugin.name)); 48108380090S猫头猫 return result; 4820e4173cdS猫头猫 } catch (e) { 4830e4173cdS猫头猫 console.log(e); 48408380090S猫头猫 return []; 48508380090S猫头猫 } 48608380090S猫头猫 } 4874d9d3c4cS猫头猫 /** 导入单曲 */ 4884d9d3c4cS猫头猫 async importMusicItem(urlLike: string): Promise<IMusic.IMusicItem | null> { 4894d9d3c4cS猫头猫 try { 4904d9d3c4cS猫头猫 const result = await this.plugin.instance?.importMusicItem?.( 4914d9d3c4cS猫头猫 urlLike, 4924d9d3c4cS猫头猫 ); 4934d9d3c4cS猫头猫 if (!result) { 4944d9d3c4cS猫头猫 throw new Error(); 4954d9d3c4cS猫头猫 } 4964d9d3c4cS猫头猫 resetMediaItem(result, this.plugin.name); 4974d9d3c4cS猫头猫 return result; 4984d9d3c4cS猫头猫 } catch { 4994d9d3c4cS猫头猫 return null; 5004d9d3c4cS猫头猫 } 5014d9d3c4cS猫头猫 } 502927dbe93S猫头猫} 5031a5528a0S猫头猫 504927dbe93S猫头猫let plugins: Array<Plugin> = []; 505927dbe93S猫头猫const pluginStateMapper = new StateMapper(() => plugins); 50674d0cf81S猫头猫 50774d0cf81S猫头猫/** 本地插件 */ 50874d0cf81S猫头猫const localFilePlugin = new Plugin(function () { 5090e4173cdS猫头猫 return { 510*7993f90eS猫头猫 platform: localPluginPlatform, //todo 改成常量 51174d0cf81S猫头猫 _path: '', 51274d0cf81S猫头猫 async getMusicInfo(musicBase) { 51374d0cf81S猫头猫 const localPath = getInternalData<string>( 51474d0cf81S猫头猫 musicBase, 51574d0cf81S猫头猫 InternalDataType.LOCALPATH, 5160e4173cdS猫头猫 ); 51774d0cf81S猫头猫 if (localPath) { 51874d0cf81S猫头猫 const coverImg = await Mp3Util.getMediaCoverImg(localPath); 51974d0cf81S猫头猫 return { 52074d0cf81S猫头猫 artwork: coverImg, 52174d0cf81S猫头猫 }; 52274d0cf81S猫头猫 } 52374d0cf81S猫头猫 return null; 52474d0cf81S猫头猫 }, 525*7993f90eS猫头猫 async getLyric(musicBase) { 526*7993f90eS猫头猫 const localPath = getInternalData<string>( 527*7993f90eS猫头猫 musicBase, 528*7993f90eS猫头猫 InternalDataType.LOCALPATH, 529*7993f90eS猫头猫 ); 530*7993f90eS猫头猫 if (localPath) { 531*7993f90eS猫头猫 const rawLrc = await Mp3Util.getLyric(localPath); 532*7993f90eS猫头猫 return { 533*7993f90eS猫头猫 rawLrc, 534*7993f90eS猫头猫 }; 535*7993f90eS猫头猫 } 536*7993f90eS猫头猫 return null; 537*7993f90eS猫头猫 }, 53874d0cf81S猫头猫 }; 53974d0cf81S猫头猫}, ''); 540*7993f90eS猫头猫localFilePlugin.hash = localPluginHash; 541927dbe93S猫头猫 542927dbe93S猫头猫async function setup() { 543927dbe93S猫头猫 const _plugins: Array<Plugin> = []; 544927dbe93S猫头猫 try { 545927dbe93S猫头猫 // 加载插件 546927dbe93S猫头猫 const pluginsPaths = await readDir(pathConst.pluginPath); 547927dbe93S猫头猫 for (let i = 0; i < pluginsPaths.length; ++i) { 548927dbe93S猫头猫 const _pluginUrl = pluginsPaths[i]; 5491e263108S猫头猫 trace('初始化插件', _pluginUrl); 5501e263108S猫头猫 if ( 5511e263108S猫头猫 _pluginUrl.isFile() && 5521e263108S猫头猫 (_pluginUrl.name?.endsWith?.('.js') || 5531e263108S猫头猫 _pluginUrl.path?.endsWith?.('.js')) 5541e263108S猫头猫 ) { 555927dbe93S猫头猫 const funcCode = await readFile(_pluginUrl.path, 'utf8'); 556927dbe93S猫头猫 const plugin = new Plugin(funcCode, _pluginUrl.path); 5574060c00aS猫头猫 const _pluginIndex = _plugins.findIndex( 5584060c00aS猫头猫 p => p.hash === plugin.hash, 5594060c00aS猫头猫 ); 560927dbe93S猫头猫 if (_pluginIndex !== -1) { 561927dbe93S猫头猫 // 重复插件,直接忽略 562927dbe93S猫头猫 return; 563927dbe93S猫头猫 } 564927dbe93S猫头猫 plugin.hash !== '' && _plugins.push(plugin); 565927dbe93S猫头猫 } 566927dbe93S猫头猫 } 567927dbe93S猫头猫 568927dbe93S猫头猫 plugins = _plugins; 569927dbe93S猫头猫 pluginStateMapper.notify(); 570927dbe93S猫头猫 } catch (e: any) { 5714060c00aS猫头猫 ToastAndroid.show( 5724060c00aS猫头猫 `插件初始化失败:${e?.message ?? e}`, 5734060c00aS猫头猫 ToastAndroid.LONG, 5744060c00aS猫头猫 ); 5751a5528a0S猫头猫 errorLog('插件初始化失败', e?.message); 576927dbe93S猫头猫 throw e; 577927dbe93S猫头猫 } 578927dbe93S猫头猫} 579927dbe93S猫头猫 580927dbe93S猫头猫// 安装插件 581927dbe93S猫头猫async function installPlugin(pluginPath: string) { 58222c09412S猫头猫 // if (pluginPath.endsWith('.js')) { 583927dbe93S猫头猫 const funcCode = await readFile(pluginPath, 'utf8'); 584927dbe93S猫头猫 const plugin = new Plugin(funcCode, pluginPath); 585927dbe93S猫头猫 const _pluginIndex = plugins.findIndex(p => p.hash === plugin.hash); 586927dbe93S猫头猫 if (_pluginIndex !== -1) { 5874d9d3c4cS猫头猫 throw new Error('插件已安装'); 588927dbe93S猫头猫 } 589927dbe93S猫头猫 if (plugin.hash !== '') { 590927dbe93S猫头猫 const fn = nanoid(); 591927dbe93S猫头猫 const _pluginPath = `${pathConst.pluginPath}${fn}.js`; 592927dbe93S猫头猫 await copyFile(pluginPath, _pluginPath); 593927dbe93S猫头猫 plugin.path = _pluginPath; 594927dbe93S猫头猫 plugins = plugins.concat(plugin); 595927dbe93S猫头猫 pluginStateMapper.notify(); 5964d9d3c4cS猫头猫 return; 597927dbe93S猫头猫 } 5984d9d3c4cS猫头猫 throw new Error('插件无法解析'); 59922c09412S猫头猫 // } 60022c09412S猫头猫 // throw new Error('插件不存在'); 601927dbe93S猫头猫} 602927dbe93S猫头猫 60358992c6bS猫头猫async function installPluginFromUrl(url: string) { 60458992c6bS猫头猫 try { 60558992c6bS猫头猫 const funcCode = (await axios.get(url)).data; 60658992c6bS猫头猫 if (funcCode) { 60758992c6bS猫头猫 const plugin = new Plugin(funcCode, ''); 60858992c6bS猫头猫 const _pluginIndex = plugins.findIndex(p => p.hash === plugin.hash); 60958992c6bS猫头猫 if (_pluginIndex !== -1) { 6108b7ddca8S猫头猫 // 静默忽略 6118b7ddca8S猫头猫 return; 61258992c6bS猫头猫 } 61325c1bd29S猫头猫 const oldVersionPlugin = plugins.find(p => p.name === plugin.name); 61425c1bd29S猫头猫 if (oldVersionPlugin) { 61525c1bd29S猫头猫 if ( 61625c1bd29S猫头猫 compare( 61725c1bd29S猫头猫 oldVersionPlugin.instance.version ?? '', 61825c1bd29S猫头猫 plugin.instance.version ?? '', 61925c1bd29S猫头猫 '>', 62025c1bd29S猫头猫 ) 62125c1bd29S猫头猫 ) { 62225c1bd29S猫头猫 throw new Error('已安装更新版本的插件'); 62325c1bd29S猫头猫 } 62425c1bd29S猫头猫 } 62525c1bd29S猫头猫 62658992c6bS猫头猫 if (plugin.hash !== '') { 62758992c6bS猫头猫 const fn = nanoid(); 62858992c6bS猫头猫 const _pluginPath = `${pathConst.pluginPath}${fn}.js`; 62958992c6bS猫头猫 await writeFile(_pluginPath, funcCode, 'utf8'); 63058992c6bS猫头猫 plugin.path = _pluginPath; 63158992c6bS猫头猫 plugins = plugins.concat(plugin); 63225c1bd29S猫头猫 if (oldVersionPlugin) { 63325c1bd29S猫头猫 plugins = plugins.filter( 63425c1bd29S猫头猫 _ => _.hash !== oldVersionPlugin.hash, 63525c1bd29S猫头猫 ); 63625c1bd29S猫头猫 try { 63725c1bd29S猫头猫 await unlink(oldVersionPlugin.path); 63825c1bd29S猫头猫 } catch {} 63925c1bd29S猫头猫 } 64058992c6bS猫头猫 pluginStateMapper.notify(); 64158992c6bS猫头猫 return; 64258992c6bS猫头猫 } 64358992c6bS猫头猫 throw new Error('插件无法解析'); 64458992c6bS猫头猫 } 64525c1bd29S猫头猫 } catch (e: any) { 64658992c6bS猫头猫 errorLog('URL安装插件失败', e); 64725c1bd29S猫头猫 throw new Error(e?.message ?? ''); 64858992c6bS猫头猫 } 64958992c6bS猫头猫} 65058992c6bS猫头猫 651927dbe93S猫头猫/** 卸载插件 */ 652927dbe93S猫头猫async function uninstallPlugin(hash: string) { 653927dbe93S猫头猫 const targetIndex = plugins.findIndex(_ => _.hash === hash); 654927dbe93S猫头猫 if (targetIndex !== -1) { 655927dbe93S猫头猫 try { 65624e5e74aS猫头猫 const pluginName = plugins[targetIndex].name; 657927dbe93S猫头猫 await unlink(plugins[targetIndex].path); 658927dbe93S猫头猫 plugins = plugins.filter(_ => _.hash !== hash); 659927dbe93S猫头猫 pluginStateMapper.notify(); 66024e5e74aS猫头猫 if (plugins.every(_ => _.name !== pluginName)) { 66124e5e74aS猫头猫 await MediaMeta.removePlugin(pluginName); 66224e5e74aS猫头猫 } 663927dbe93S猫头猫 } catch {} 664927dbe93S猫头猫 } 665927dbe93S猫头猫} 666927dbe93S猫头猫 66708882a77S猫头猫async function uninstallAllPlugins() { 66808882a77S猫头猫 await Promise.all( 66908882a77S猫头猫 plugins.map(async plugin => { 67008882a77S猫头猫 try { 67108882a77S猫头猫 const pluginName = plugin.name; 67208882a77S猫头猫 await unlink(plugin.path); 67308882a77S猫头猫 await MediaMeta.removePlugin(pluginName); 67408882a77S猫头猫 } catch (e) {} 67508882a77S猫头猫 }), 67608882a77S猫头猫 ); 67708882a77S猫头猫 plugins = []; 67808882a77S猫头猫 pluginStateMapper.notify(); 67908882a77S猫头猫} 68008882a77S猫头猫 68125c1bd29S猫头猫async function updatePlugin(plugin: Plugin) { 68225c1bd29S猫头猫 const updateUrl = plugin.instance.srcUrl; 68325c1bd29S猫头猫 if (!updateUrl) { 68425c1bd29S猫头猫 throw new Error('没有更新源'); 68525c1bd29S猫头猫 } 68625c1bd29S猫头猫 try { 68725c1bd29S猫头猫 await installPluginFromUrl(updateUrl); 68825c1bd29S猫头猫 } catch (e: any) { 68925c1bd29S猫头猫 if (e.message === '插件已安装') { 69025c1bd29S猫头猫 throw new Error('当前已是最新版本'); 69125c1bd29S猫头猫 } else { 69225c1bd29S猫头猫 throw e; 69325c1bd29S猫头猫 } 69425c1bd29S猫头猫 } 69525c1bd29S猫头猫} 69625c1bd29S猫头猫 697927dbe93S猫头猫function getByMedia(mediaItem: ICommon.IMediaBase) { 698927dbe93S猫头猫 return getByName(mediaItem.platform); 699927dbe93S猫头猫} 700927dbe93S猫头猫 701927dbe93S猫头猫function getByHash(hash: string) { 702*7993f90eS猫头猫 return hash === localPluginHash 703*7993f90eS猫头猫 ? localFilePlugin 704*7993f90eS猫头猫 : plugins.find(_ => _.hash === hash); 705927dbe93S猫头猫} 706927dbe93S猫头猫 707927dbe93S猫头猫function getByName(name: string) { 708*7993f90eS猫头猫 return name === localPluginPlatform 7090e4173cdS猫头猫 ? localFilePlugin 7100e4173cdS猫头猫 : plugins.find(_ => _.name === name); 711927dbe93S猫头猫} 712927dbe93S猫头猫 713927dbe93S猫头猫function getValidPlugins() { 714927dbe93S猫头猫 return plugins.filter(_ => _.state === 'enabled'); 715927dbe93S猫头猫} 716927dbe93S猫头猫 717efb9da24S猫头猫function getSearchablePlugins() { 718efb9da24S猫头猫 return plugins.filter(_ => _.state === 'enabled' && _.instance.search); 719efb9da24S猫头猫} 720efb9da24S猫头猫 721927dbe93S猫头猫const PluginManager = { 722927dbe93S猫头猫 setup, 723927dbe93S猫头猫 installPlugin, 72458992c6bS猫头猫 installPluginFromUrl, 72525c1bd29S猫头猫 updatePlugin, 726927dbe93S猫头猫 uninstallPlugin, 727927dbe93S猫头猫 getByMedia, 728927dbe93S猫头猫 getByHash, 729927dbe93S猫头猫 getByName, 730927dbe93S猫头猫 getValidPlugins, 731efb9da24S猫头猫 getSearchablePlugins, 7325276aef9S猫头猫 usePlugins: pluginStateMapper.useMappedState, 73308882a77S猫头猫 uninstallAllPlugins, 7345276aef9S猫头猫}; 735927dbe93S猫头猫 736927dbe93S猫头猫export default PluginManager; 737