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, 31*e08d37a3S猫头猫 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'; 44*e08d37a3S猫头猫import {PluginMeta} from './pluginMeta'; 45927dbe93S猫头猫 46927dbe93S猫头猫axios.defaults.timeout = 1500; 47927dbe93S猫头猫 48927dbe93S猫头猫const sha256 = CryptoJs.SHA256; 49927dbe93S猫头猫 50cfa0fc07S猫头猫export enum PluginStateCode { 51927dbe93S猫头猫 /** 版本不匹配 */ 52927dbe93S猫头猫 VersionNotMatch = 'VERSION NOT MATCH', 53927dbe93S猫头猫 /** 无法解析 */ 54927dbe93S猫头猫 CannotParse = 'CANNOT PARSE', 55927dbe93S猫头猫} 56927dbe93S猫头猫 57d5bfeb7eS猫头猫//#region 插件类 58927dbe93S猫头猫export class Plugin { 59927dbe93S猫头猫 /** 插件名 */ 60927dbe93S猫头猫 public name: string; 61927dbe93S猫头猫 /** 插件的hash,作为唯一id */ 62927dbe93S猫头猫 public hash: string; 63927dbe93S猫头猫 /** 插件状态:激活、关闭、错误 */ 64927dbe93S猫头猫 public state: 'enabled' | 'disabled' | 'error'; 65927dbe93S猫头猫 /** 插件支持的搜索类型 */ 66927dbe93S猫头猫 public supportedSearchType?: string; 67927dbe93S猫头猫 /** 插件状态信息 */ 68927dbe93S猫头猫 public stateCode?: PluginStateCode; 69927dbe93S猫头猫 /** 插件的实例 */ 70927dbe93S猫头猫 public instance: IPlugin.IPluginInstance; 71927dbe93S猫头猫 /** 插件路径 */ 72927dbe93S猫头猫 public path: string; 73927dbe93S猫头猫 /** 插件方法 */ 74927dbe93S猫头猫 public methods: PluginMethods; 75d5bfeb7eS猫头猫 /** 用户输入 */ 76d5bfeb7eS猫头猫 public userEnv?: Record<string, string>; 77927dbe93S猫头猫 7874d0cf81S猫头猫 constructor( 7974d0cf81S猫头猫 funcCode: string | (() => IPlugin.IPluginInstance), 8074d0cf81S猫头猫 pluginPath: string, 8174d0cf81S猫头猫 ) { 82927dbe93S猫头猫 this.state = 'enabled'; 83927dbe93S猫头猫 let _instance: IPlugin.IPluginInstance; 84927dbe93S猫头猫 try { 8574d0cf81S猫头猫 if (typeof funcCode === 'string') { 864060c00aS猫头猫 // eslint-disable-next-line no-new-func 87927dbe93S猫头猫 _instance = Function(` 88927dbe93S猫头猫 'use strict'; 89927dbe93S猫头猫 try { 90927dbe93S猫头猫 return ${funcCode}; 91927dbe93S猫头猫 } catch(e) { 92927dbe93S猫头猫 return null; 93927dbe93S猫头猫 } 947d7e864fS猫头猫 `)()({ 957d7e864fS猫头猫 CryptoJs, 967d7e864fS猫头猫 axios, 977d7e864fS猫头猫 dayjs, 987d7e864fS猫头猫 cheerio, 997d7e864fS猫头猫 bigInt, 1007d7e864fS猫头猫 qs, 1017d7e864fS猫头猫 he, 1027d7e864fS猫头猫 CookieManager: { 1037d7e864fS猫头猫 flush: CookieManager.flush, 1047d7e864fS猫头猫 get: CookieManager.get, 1057d7e864fS猫头猫 }, 1067d7e864fS猫头猫 }); 10774d0cf81S猫头猫 } else { 10874d0cf81S猫头猫 _instance = funcCode(); 10974d0cf81S猫头猫 } 110927dbe93S猫头猫 this.checkValid(_instance); 111927dbe93S猫头猫 } catch (e: any) { 112927dbe93S猫头猫 this.state = 'error'; 113927dbe93S猫头猫 this.stateCode = PluginStateCode.CannotParse; 114927dbe93S猫头猫 if (e?.stateCode) { 115927dbe93S猫头猫 this.stateCode = e.stateCode; 116927dbe93S猫头猫 } 117927dbe93S猫头猫 errorLog(`${pluginPath}插件无法解析 `, { 118927dbe93S猫头猫 stateCode: this.stateCode, 119927dbe93S猫头猫 message: e?.message, 120927dbe93S猫头猫 stack: e?.stack, 121927dbe93S猫头猫 }); 122927dbe93S猫头猫 _instance = e?.instance ?? { 123927dbe93S猫头猫 _path: '', 124927dbe93S猫头猫 platform: '', 125927dbe93S猫头猫 appVersion: '', 12620e6a092S猫头猫 async getMediaSource() { 127927dbe93S猫头猫 return null; 128927dbe93S猫头猫 }, 129927dbe93S猫头猫 async search() { 130927dbe93S猫头猫 return {}; 131927dbe93S猫头猫 }, 132927dbe93S猫头猫 async getAlbumInfo() { 133927dbe93S猫头猫 return null; 134927dbe93S猫头猫 }, 135927dbe93S猫头猫 }; 136927dbe93S猫头猫 } 137927dbe93S猫头猫 this.instance = _instance; 138927dbe93S猫头猫 this.path = pluginPath; 139927dbe93S猫头猫 this.name = _instance.platform; 140927dbe93S猫头猫 if (this.instance.platform === '') { 141927dbe93S猫头猫 this.hash = ''; 142927dbe93S猫头猫 } else { 14374d0cf81S猫头猫 if (typeof funcCode === 'string') { 144927dbe93S猫头猫 this.hash = sha256(funcCode).toString(); 14574d0cf81S猫头猫 } else { 14674d0cf81S猫头猫 this.hash = sha256(funcCode.toString()).toString(); 14774d0cf81S猫头猫 } 148927dbe93S猫头猫 } 149927dbe93S猫头猫 150927dbe93S猫头猫 // 放在最后 151927dbe93S猫头猫 this.methods = new PluginMethods(this); 152927dbe93S猫头猫 } 153927dbe93S猫头猫 154927dbe93S猫头猫 private checkValid(_instance: IPlugin.IPluginInstance) { 155927dbe93S猫头猫 /** 版本号校验 */ 156927dbe93S猫头猫 if ( 157927dbe93S猫头猫 _instance.appVersion && 158927dbe93S猫头猫 !satisfies(DeviceInfo.getVersion(), _instance.appVersion) 159927dbe93S猫头猫 ) { 160927dbe93S猫头猫 throw { 161927dbe93S猫头猫 instance: _instance, 162927dbe93S猫头猫 stateCode: PluginStateCode.VersionNotMatch, 163927dbe93S猫头猫 }; 164927dbe93S猫头猫 } 165927dbe93S猫头猫 return true; 166927dbe93S猫头猫 } 167927dbe93S猫头猫} 168d5bfeb7eS猫头猫//#endregion 169927dbe93S猫头猫 170d5bfeb7eS猫头猫//#region 基于插件类封装的方法,供给APP侧直接调用 171927dbe93S猫头猫/** 有缓存等信息 */ 172927dbe93S猫头猫class PluginMethods implements IPlugin.IPluginInstanceMethods { 173927dbe93S猫头猫 private plugin; 174927dbe93S猫头猫 constructor(plugin: Plugin) { 175927dbe93S猫头猫 this.plugin = plugin; 176927dbe93S猫头猫 } 177927dbe93S猫头猫 /** 搜索 */ 178927dbe93S猫头猫 async search<T extends ICommon.SupportMediaType>( 179927dbe93S猫头猫 query: string, 180927dbe93S猫头猫 page: number, 181927dbe93S猫头猫 type: T, 182927dbe93S猫头猫 ): Promise<IPlugin.ISearchResult<T>> { 183927dbe93S猫头猫 if (!this.plugin.instance.search) { 184927dbe93S猫头猫 return { 185927dbe93S猫头猫 isEnd: true, 186927dbe93S猫头猫 data: [], 187927dbe93S猫头猫 }; 188927dbe93S猫头猫 } 189927dbe93S猫头猫 1904060c00aS猫头猫 const result = 1914060c00aS猫头猫 (await this.plugin.instance.search(query, page, type)) ?? {}; 192927dbe93S猫头猫 if (Array.isArray(result.data)) { 193927dbe93S猫头猫 result.data.forEach(_ => { 194927dbe93S猫头猫 resetMediaItem(_, this.plugin.name); 195927dbe93S猫头猫 }); 196927dbe93S猫头猫 return { 197927dbe93S猫头猫 isEnd: result.isEnd ?? true, 198927dbe93S猫头猫 data: result.data, 199927dbe93S猫头猫 }; 200927dbe93S猫头猫 } 201927dbe93S猫头猫 return { 202927dbe93S猫头猫 isEnd: true, 203927dbe93S猫头猫 data: [], 204927dbe93S猫头猫 }; 205927dbe93S猫头猫 } 206927dbe93S猫头猫 207927dbe93S猫头猫 /** 获取真实源 */ 20820e6a092S猫头猫 async getMediaSource( 209927dbe93S猫头猫 musicItem: IMusic.IMusicItemBase, 210927dbe93S猫头猫 retryCount = 1, 21120e6a092S猫头猫 ): Promise<IPlugin.IMediaSourceResult> { 212927dbe93S猫头猫 // 1. 本地搜索 其实直接读mediameta就好了 213927dbe93S猫头猫 const localPath = 2140e4173cdS猫头猫 getInternalData<string>(musicItem, InternalDataType.LOCALPATH) ?? 2150e4173cdS猫头猫 getInternalData<string>( 2160e4173cdS猫头猫 LocalMusicSheet.isLocalMusic(musicItem), 2170e4173cdS猫头猫 InternalDataType.LOCALPATH, 2180e4173cdS猫头猫 ); 2190e4173cdS猫头猫 if (localPath && (await FileSystem.exists(localPath))) { 2200e4173cdS猫头猫 trace('本地播放', localPath); 221927dbe93S猫头猫 return { 222927dbe93S猫头猫 url: localPath, 223927dbe93S猫头猫 }; 224927dbe93S猫头猫 } 2257993f90eS猫头猫 if (musicItem.platform === localPluginPlatform) { 226f5935920S猫头猫 throw new Error('本地音乐不存在'); 227f5935920S猫头猫 } 228927dbe93S猫头猫 // 2. 缓存播放 229927dbe93S猫头猫 const mediaCache = Cache.get(musicItem); 230985f8e75S猫头猫 const pluginCacheControl = 231985f8e75S猫头猫 this.plugin.instance.cacheControl ?? 'no-cache'; 232cfa0fc07S猫头猫 if ( 233cfa0fc07S猫头猫 mediaCache && 234cfa0fc07S猫头猫 mediaCache?.url && 23548f4b873S猫头猫 (pluginCacheControl === CacheControl.Cache || 23648f4b873S猫头猫 (pluginCacheControl === CacheControl.NoCache && 237ef714860S猫头猫 Network.isOffline())) 238cfa0fc07S猫头猫 ) { 2395276aef9S猫头猫 trace('播放', '缓存播放'); 240927dbe93S猫头猫 return { 241927dbe93S猫头猫 url: mediaCache.url, 242927dbe93S猫头猫 headers: mediaCache.headers, 2434060c00aS猫头猫 userAgent: 2444060c00aS猫头猫 mediaCache.userAgent ?? mediaCache.headers?.['user-agent'], 245927dbe93S猫头猫 }; 246927dbe93S猫头猫 } 247927dbe93S猫头猫 // 3. 插件解析 24820e6a092S猫头猫 if (!this.plugin.instance.getMediaSource) { 249927dbe93S猫头猫 return {url: musicItem.url}; 250927dbe93S猫头猫 } 251927dbe93S猫头猫 try { 25248f4b873S猫头猫 const {url, headers} = 25320e6a092S猫头猫 (await this.plugin.instance.getMediaSource(musicItem)) ?? {}; 254927dbe93S猫头猫 if (!url) { 255927dbe93S猫头猫 throw new Error(); 256927dbe93S猫头猫 } 2575276aef9S猫头猫 trace('播放', '插件播放'); 258927dbe93S猫头猫 const result = { 259927dbe93S猫头猫 url, 260927dbe93S猫头猫 headers, 261927dbe93S猫头猫 userAgent: headers?.['user-agent'], 262cfa0fc07S猫头猫 } as IPlugin.IMediaSourceResult; 263927dbe93S猫头猫 26448f4b873S猫头猫 if (pluginCacheControl !== CacheControl.NoStore) { 265927dbe93S猫头猫 Cache.update(musicItem, result); 266752ffc5aS猫头猫 } 267cfa0fc07S猫头猫 268927dbe93S猫头猫 return result; 269927dbe93S猫头猫 } catch (e: any) { 270927dbe93S猫头猫 if (retryCount > 0) { 271927dbe93S猫头猫 await delay(150); 27220e6a092S猫头猫 return this.getMediaSource(musicItem, --retryCount); 273927dbe93S猫头猫 } 274927dbe93S猫头猫 errorLog('获取真实源失败', e?.message); 275927dbe93S猫头猫 throw e; 276927dbe93S猫头猫 } 277927dbe93S猫头猫 } 278927dbe93S猫头猫 279927dbe93S猫头猫 /** 获取音乐详情 */ 280927dbe93S猫头猫 async getMusicInfo( 281927dbe93S猫头猫 musicItem: ICommon.IMediaBase, 28274d0cf81S猫头猫 ): Promise<Partial<IMusic.IMusicItem> | null> { 283927dbe93S猫头猫 if (!this.plugin.instance.getMusicInfo) { 28474d0cf81S猫头猫 return {}; 285927dbe93S猫头猫 } 28674d0cf81S猫头猫 try { 287927dbe93S猫头猫 return ( 288927dbe93S猫头猫 this.plugin.instance.getMusicInfo( 2897993f90eS猫头猫 resetMediaItem(musicItem, undefined, true), 29074d0cf81S猫头猫 ) ?? {} 291927dbe93S猫头猫 ); 29274d0cf81S猫头猫 } catch (e) { 29374d0cf81S猫头猫 return {}; 29474d0cf81S猫头猫 } 295927dbe93S猫头猫 } 296927dbe93S猫头猫 297927dbe93S猫头猫 /** 获取歌词 */ 298927dbe93S猫头猫 async getLyric( 299927dbe93S猫头猫 musicItem: IMusic.IMusicItemBase, 300927dbe93S猫头猫 from?: IMusic.IMusicItemBase, 301927dbe93S猫头猫 ): Promise<ILyric.ILyricSource | null> { 302927dbe93S猫头猫 // 1.额外存储的meta信息 303927dbe93S猫头猫 const meta = MediaMeta.get(musicItem); 304927dbe93S猫头猫 if (meta && meta.associatedLrc) { 305927dbe93S猫头猫 // 有关联歌词 306927dbe93S猫头猫 if ( 307927dbe93S猫头猫 isSameMediaItem(musicItem, from) || 308927dbe93S猫头猫 isSameMediaItem(meta.associatedLrc, musicItem) 309927dbe93S猫头猫 ) { 310927dbe93S猫头猫 // 形成环路,断开当前的环 311927dbe93S猫头猫 await MediaMeta.update(musicItem, { 312927dbe93S猫头猫 associatedLrc: undefined, 313927dbe93S猫头猫 }); 314927dbe93S猫头猫 // 无歌词 315927dbe93S猫头猫 return null; 316927dbe93S猫头猫 } 317927dbe93S猫头猫 // 获取关联歌词 3187a91f04fS猫头猫 const associatedMeta = MediaMeta.get(meta.associatedLrc) ?? {}; 3194060c00aS猫头猫 const result = await this.getLyric( 3207a91f04fS猫头猫 {...meta.associatedLrc, ...associatedMeta}, 3214060c00aS猫头猫 from ?? musicItem, 3224060c00aS猫头猫 ); 323927dbe93S猫头猫 if (result) { 324927dbe93S猫头猫 // 如果有关联歌词,就返回关联歌词,深度优先 325927dbe93S猫头猫 return result; 326927dbe93S猫头猫 } 327927dbe93S猫头猫 } 328927dbe93S猫头猫 const cache = Cache.get(musicItem); 329927dbe93S猫头猫 let rawLrc = meta?.rawLrc || musicItem.rawLrc || cache?.rawLrc; 330927dbe93S猫头猫 let lrcUrl = meta?.lrc || musicItem.lrc || cache?.lrc; 331927dbe93S猫头猫 // 如果存在文本 332927dbe93S猫头猫 if (rawLrc) { 333927dbe93S猫头猫 return { 334927dbe93S猫头猫 rawLrc, 335927dbe93S猫头猫 lrc: lrcUrl, 336927dbe93S猫头猫 }; 337927dbe93S猫头猫 } 338927dbe93S猫头猫 // 2.本地缓存 339927dbe93S猫头猫 const localLrc = 3400e4173cdS猫头猫 meta?.[internalSerializeKey]?.local?.localLrc || 3410e4173cdS猫头猫 cache?.[internalSerializeKey]?.local?.localLrc; 342927dbe93S猫头猫 if (localLrc && (await exists(localLrc))) { 343927dbe93S猫头猫 rawLrc = await readFile(localLrc, 'utf8'); 344927dbe93S猫头猫 return { 345927dbe93S猫头猫 rawLrc, 346927dbe93S猫头猫 lrc: lrcUrl, 347927dbe93S猫头猫 }; 348927dbe93S猫头猫 } 349927dbe93S猫头猫 // 3.优先使用url 350927dbe93S猫头猫 if (lrcUrl) { 351927dbe93S猫头猫 try { 352927dbe93S猫头猫 // 需要超时时间 axios timeout 但是没生效 3532a3194f5S猫头猫 rawLrc = (await axios.get(lrcUrl, {timeout: 1500})).data; 354927dbe93S猫头猫 return { 355927dbe93S猫头猫 rawLrc, 356927dbe93S猫头猫 lrc: lrcUrl, 357927dbe93S猫头猫 }; 358927dbe93S猫头猫 } catch { 359927dbe93S猫头猫 lrcUrl = undefined; 360927dbe93S猫头猫 } 361927dbe93S猫头猫 } 362927dbe93S猫头猫 // 4. 如果地址失效 363927dbe93S猫头猫 if (!lrcUrl) { 364927dbe93S猫头猫 // 插件获得url 365927dbe93S猫头猫 try { 3667a91f04fS猫头猫 let lrcSource; 3677a91f04fS猫头猫 if (from) { 3687a91f04fS猫头猫 lrcSource = await PluginManager.getByMedia( 3697a91f04fS猫头猫 musicItem, 3707a91f04fS猫头猫 )?.instance?.getLyric?.( 371927dbe93S猫头猫 resetMediaItem(musicItem, undefined, true), 372927dbe93S猫头猫 ); 3737a91f04fS猫头猫 } else { 3747a91f04fS猫头猫 lrcSource = await this.plugin.instance?.getLyric?.( 3757a91f04fS猫头猫 resetMediaItem(musicItem, undefined, true), 3767a91f04fS猫头猫 ); 3777a91f04fS猫头猫 } 3787a91f04fS猫头猫 379927dbe93S猫头猫 rawLrc = lrcSource?.rawLrc; 380927dbe93S猫头猫 lrcUrl = lrcSource?.lrc; 381927dbe93S猫头猫 } catch (e: any) { 382927dbe93S猫头猫 trace('插件获取歌词失败', e?.message, 'error'); 383927dbe93S猫头猫 } 384927dbe93S猫头猫 } 385927dbe93S猫头猫 // 5. 最后一次请求 386927dbe93S猫头猫 if (rawLrc || lrcUrl) { 387927dbe93S猫头猫 const filename = `${pathConst.lrcCachePath}${nanoid()}.lrc`; 388927dbe93S猫头猫 if (lrcUrl) { 389927dbe93S猫头猫 try { 3902a3194f5S猫头猫 rawLrc = (await axios.get(lrcUrl, {timeout: 1500})).data; 391927dbe93S猫头猫 } catch {} 392927dbe93S猫头猫 } 393927dbe93S猫头猫 if (rawLrc) { 394927dbe93S猫头猫 await writeFile(filename, rawLrc, 'utf8'); 395927dbe93S猫头猫 // 写入缓存 396927dbe93S猫头猫 Cache.update(musicItem, [ 3970e4173cdS猫头猫 [`${internalSerializeKey}.local.localLrc`, filename], 398927dbe93S猫头猫 ]); 399927dbe93S猫头猫 // 如果有meta 400927dbe93S猫头猫 if (meta) { 401927dbe93S猫头猫 MediaMeta.update(musicItem, [ 4020e4173cdS猫头猫 [`${internalSerializeKey}.local.localLrc`, filename], 403927dbe93S猫头猫 ]); 404927dbe93S猫头猫 } 405927dbe93S猫头猫 return { 406927dbe93S猫头猫 rawLrc, 407927dbe93S猫头猫 lrc: lrcUrl, 408927dbe93S猫头猫 }; 409927dbe93S猫头猫 } 410927dbe93S猫头猫 } 411927dbe93S猫头猫 412927dbe93S猫头猫 return null; 413927dbe93S猫头猫 } 414927dbe93S猫头猫 415927dbe93S猫头猫 /** 获取歌词文本 */ 416927dbe93S猫头猫 async getLyricText( 417927dbe93S猫头猫 musicItem: IMusic.IMusicItem, 418927dbe93S猫头猫 ): Promise<string | undefined> { 419927dbe93S猫头猫 return (await this.getLyric(musicItem))?.rawLrc; 420927dbe93S猫头猫 } 421927dbe93S猫头猫 422927dbe93S猫头猫 /** 获取专辑信息 */ 423927dbe93S猫头猫 async getAlbumInfo( 424927dbe93S猫头猫 albumItem: IAlbum.IAlbumItemBase, 425927dbe93S猫头猫 ): Promise<IAlbum.IAlbumItem | null> { 426927dbe93S猫头猫 if (!this.plugin.instance.getAlbumInfo) { 427927dbe93S猫头猫 return {...albumItem, musicList: []}; 428927dbe93S猫头猫 } 429927dbe93S猫头猫 try { 430927dbe93S猫头猫 const result = await this.plugin.instance.getAlbumInfo( 431927dbe93S猫头猫 resetMediaItem(albumItem, undefined, true), 432927dbe93S猫头猫 ); 4335276aef9S猫头猫 if (!result) { 4345276aef9S猫头猫 throw new Error(); 4355276aef9S猫头猫 } 436927dbe93S猫头猫 result?.musicList?.forEach(_ => { 437927dbe93S猫头猫 resetMediaItem(_, this.plugin.name); 438927dbe93S猫头猫 }); 4395276aef9S猫头猫 4405276aef9S猫头猫 return {...albumItem, ...result}; 4414394410dS猫头猫 } catch (e: any) { 4424394410dS猫头猫 trace('获取专辑信息失败', e?.message); 443927dbe93S猫头猫 return {...albumItem, musicList: []}; 444927dbe93S猫头猫 } 445927dbe93S猫头猫 } 446927dbe93S猫头猫 447927dbe93S猫头猫 /** 查询作者信息 */ 448efb9da24S猫头猫 async getArtistWorks<T extends IArtist.ArtistMediaType>( 449927dbe93S猫头猫 artistItem: IArtist.IArtistItem, 450927dbe93S猫头猫 page: number, 451927dbe93S猫头猫 type: T, 452927dbe93S猫头猫 ): Promise<IPlugin.ISearchResult<T>> { 453efb9da24S猫头猫 if (!this.plugin.instance.getArtistWorks) { 454927dbe93S猫头猫 return { 455927dbe93S猫头猫 isEnd: true, 456927dbe93S猫头猫 data: [], 457927dbe93S猫头猫 }; 458927dbe93S猫头猫 } 459927dbe93S猫头猫 try { 460efb9da24S猫头猫 const result = await this.plugin.instance.getArtistWorks( 461927dbe93S猫头猫 artistItem, 462927dbe93S猫头猫 page, 463927dbe93S猫头猫 type, 464927dbe93S猫头猫 ); 465927dbe93S猫头猫 if (!result.data) { 466927dbe93S猫头猫 return { 467927dbe93S猫头猫 isEnd: true, 468927dbe93S猫头猫 data: [], 469927dbe93S猫头猫 }; 470927dbe93S猫头猫 } 471927dbe93S猫头猫 result.data?.forEach(_ => resetMediaItem(_, this.plugin.name)); 472927dbe93S猫头猫 return { 473927dbe93S猫头猫 isEnd: result.isEnd ?? true, 474927dbe93S猫头猫 data: result.data, 475927dbe93S猫头猫 }; 4764394410dS猫头猫 } catch (e: any) { 4774394410dS猫头猫 trace('查询作者信息失败', e?.message); 478927dbe93S猫头猫 throw e; 479927dbe93S猫头猫 } 480927dbe93S猫头猫 } 48108380090S猫头猫 48208380090S猫头猫 /** 导入歌单 */ 48308380090S猫头猫 async importMusicSheet(urlLike: string): Promise<IMusic.IMusicItem[]> { 48408380090S猫头猫 try { 48508380090S猫头猫 const result = 48608380090S猫头猫 (await this.plugin.instance?.importMusicSheet?.(urlLike)) ?? []; 48708380090S猫头猫 result.forEach(_ => resetMediaItem(_, this.plugin.name)); 48808380090S猫头猫 return result; 4890e4173cdS猫头猫 } catch (e) { 4900e4173cdS猫头猫 console.log(e); 49108380090S猫头猫 return []; 49208380090S猫头猫 } 49308380090S猫头猫 } 4944d9d3c4cS猫头猫 /** 导入单曲 */ 4954d9d3c4cS猫头猫 async importMusicItem(urlLike: string): Promise<IMusic.IMusicItem | null> { 4964d9d3c4cS猫头猫 try { 4974d9d3c4cS猫头猫 const result = await this.plugin.instance?.importMusicItem?.( 4984d9d3c4cS猫头猫 urlLike, 4994d9d3c4cS猫头猫 ); 5004d9d3c4cS猫头猫 if (!result) { 5014d9d3c4cS猫头猫 throw new Error(); 5024d9d3c4cS猫头猫 } 5034d9d3c4cS猫头猫 resetMediaItem(result, this.plugin.name); 5044d9d3c4cS猫头猫 return result; 5054d9d3c4cS猫头猫 } catch { 5064d9d3c4cS猫头猫 return null; 5074d9d3c4cS猫头猫 } 5084d9d3c4cS猫头猫 } 509927dbe93S猫头猫} 510d5bfeb7eS猫头猫//#endregion 5111a5528a0S猫头猫 512927dbe93S猫头猫let plugins: Array<Plugin> = []; 513927dbe93S猫头猫const pluginStateMapper = new StateMapper(() => plugins); 51474d0cf81S猫头猫 515d5bfeb7eS猫头猫//#region 本地音乐插件 51674d0cf81S猫头猫/** 本地插件 */ 51774d0cf81S猫头猫const localFilePlugin = new Plugin(function () { 5180e4173cdS猫头猫 return { 519d5bfeb7eS猫头猫 platform: localPluginPlatform, 52074d0cf81S猫头猫 _path: '', 52174d0cf81S猫头猫 async getMusicInfo(musicBase) { 52274d0cf81S猫头猫 const localPath = getInternalData<string>( 52374d0cf81S猫头猫 musicBase, 52474d0cf81S猫头猫 InternalDataType.LOCALPATH, 5250e4173cdS猫头猫 ); 52674d0cf81S猫头猫 if (localPath) { 52774d0cf81S猫头猫 const coverImg = await Mp3Util.getMediaCoverImg(localPath); 52874d0cf81S猫头猫 return { 52974d0cf81S猫头猫 artwork: coverImg, 53074d0cf81S猫头猫 }; 53174d0cf81S猫头猫 } 53274d0cf81S猫头猫 return null; 53374d0cf81S猫头猫 }, 5347993f90eS猫头猫 async getLyric(musicBase) { 5357993f90eS猫头猫 const localPath = getInternalData<string>( 5367993f90eS猫头猫 musicBase, 5377993f90eS猫头猫 InternalDataType.LOCALPATH, 5387993f90eS猫头猫 ); 5397993f90eS猫头猫 if (localPath) { 5407993f90eS猫头猫 const rawLrc = await Mp3Util.getLyric(localPath); 5417993f90eS猫头猫 return { 5427993f90eS猫头猫 rawLrc, 5437993f90eS猫头猫 }; 5447993f90eS猫头猫 } 5457993f90eS猫头猫 return null; 5467993f90eS猫头猫 }, 54774d0cf81S猫头猫 }; 54874d0cf81S猫头猫}, ''); 5497993f90eS猫头猫localFilePlugin.hash = localPluginHash; 550927dbe93S猫头猫 551d5bfeb7eS猫头猫//#endregion 552d5bfeb7eS猫头猫 553927dbe93S猫头猫async function setup() { 554927dbe93S猫头猫 const _plugins: Array<Plugin> = []; 555927dbe93S猫头猫 try { 556927dbe93S猫头猫 // 加载插件 557927dbe93S猫头猫 const pluginsPaths = await readDir(pathConst.pluginPath); 558927dbe93S猫头猫 for (let i = 0; i < pluginsPaths.length; ++i) { 559927dbe93S猫头猫 const _pluginUrl = pluginsPaths[i]; 5601e263108S猫头猫 trace('初始化插件', _pluginUrl); 5611e263108S猫头猫 if ( 5621e263108S猫头猫 _pluginUrl.isFile() && 5631e263108S猫头猫 (_pluginUrl.name?.endsWith?.('.js') || 5641e263108S猫头猫 _pluginUrl.path?.endsWith?.('.js')) 5651e263108S猫头猫 ) { 566927dbe93S猫头猫 const funcCode = await readFile(_pluginUrl.path, 'utf8'); 567927dbe93S猫头猫 const plugin = new Plugin(funcCode, _pluginUrl.path); 5684060c00aS猫头猫 const _pluginIndex = _plugins.findIndex( 5694060c00aS猫头猫 p => p.hash === plugin.hash, 5704060c00aS猫头猫 ); 571927dbe93S猫头猫 if (_pluginIndex !== -1) { 572927dbe93S猫头猫 // 重复插件,直接忽略 573927dbe93S猫头猫 return; 574927dbe93S猫头猫 } 575927dbe93S猫头猫 plugin.hash !== '' && _plugins.push(plugin); 576927dbe93S猫头猫 } 577927dbe93S猫头猫 } 578927dbe93S猫头猫 579927dbe93S猫头猫 plugins = _plugins; 580927dbe93S猫头猫 pluginStateMapper.notify(); 581*e08d37a3S猫头猫 /** 初始化meta信息 */ 582*e08d37a3S猫头猫 PluginMeta.setupMeta(plugins.map(_ => _.name)); 583927dbe93S猫头猫 } catch (e: any) { 5844060c00aS猫头猫 ToastAndroid.show( 5854060c00aS猫头猫 `插件初始化失败:${e?.message ?? e}`, 5864060c00aS猫头猫 ToastAndroid.LONG, 5874060c00aS猫头猫 ); 5881a5528a0S猫头猫 errorLog('插件初始化失败', e?.message); 589927dbe93S猫头猫 throw e; 590927dbe93S猫头猫 } 591927dbe93S猫头猫} 592927dbe93S猫头猫 593927dbe93S猫头猫// 安装插件 594927dbe93S猫头猫async function installPlugin(pluginPath: string) { 59522c09412S猫头猫 // if (pluginPath.endsWith('.js')) { 596927dbe93S猫头猫 const funcCode = await readFile(pluginPath, 'utf8'); 597927dbe93S猫头猫 const plugin = new Plugin(funcCode, pluginPath); 598927dbe93S猫头猫 const _pluginIndex = plugins.findIndex(p => p.hash === plugin.hash); 599927dbe93S猫头猫 if (_pluginIndex !== -1) { 6004d9d3c4cS猫头猫 throw new Error('插件已安装'); 601927dbe93S猫头猫 } 602927dbe93S猫头猫 if (plugin.hash !== '') { 603927dbe93S猫头猫 const fn = nanoid(); 604927dbe93S猫头猫 const _pluginPath = `${pathConst.pluginPath}${fn}.js`; 605927dbe93S猫头猫 await copyFile(pluginPath, _pluginPath); 606927dbe93S猫头猫 plugin.path = _pluginPath; 607927dbe93S猫头猫 plugins = plugins.concat(plugin); 608927dbe93S猫头猫 pluginStateMapper.notify(); 6094d9d3c4cS猫头猫 return; 610927dbe93S猫头猫 } 6114d9d3c4cS猫头猫 throw new Error('插件无法解析'); 61222c09412S猫头猫 // } 61322c09412S猫头猫 // throw new Error('插件不存在'); 614927dbe93S猫头猫} 615927dbe93S猫头猫 61658992c6bS猫头猫async function installPluginFromUrl(url: string) { 61758992c6bS猫头猫 try { 61858992c6bS猫头猫 const funcCode = (await axios.get(url)).data; 61958992c6bS猫头猫 if (funcCode) { 62058992c6bS猫头猫 const plugin = new Plugin(funcCode, ''); 62158992c6bS猫头猫 const _pluginIndex = plugins.findIndex(p => p.hash === plugin.hash); 62258992c6bS猫头猫 if (_pluginIndex !== -1) { 6238b7ddca8S猫头猫 // 静默忽略 6248b7ddca8S猫头猫 return; 62558992c6bS猫头猫 } 62625c1bd29S猫头猫 const oldVersionPlugin = plugins.find(p => p.name === plugin.name); 62725c1bd29S猫头猫 if (oldVersionPlugin) { 62825c1bd29S猫头猫 if ( 62925c1bd29S猫头猫 compare( 63025c1bd29S猫头猫 oldVersionPlugin.instance.version ?? '', 63125c1bd29S猫头猫 plugin.instance.version ?? '', 63225c1bd29S猫头猫 '>', 63325c1bd29S猫头猫 ) 63425c1bd29S猫头猫 ) { 63525c1bd29S猫头猫 throw new Error('已安装更新版本的插件'); 63625c1bd29S猫头猫 } 63725c1bd29S猫头猫 } 63825c1bd29S猫头猫 63958992c6bS猫头猫 if (plugin.hash !== '') { 64058992c6bS猫头猫 const fn = nanoid(); 64158992c6bS猫头猫 const _pluginPath = `${pathConst.pluginPath}${fn}.js`; 64258992c6bS猫头猫 await writeFile(_pluginPath, funcCode, 'utf8'); 64358992c6bS猫头猫 plugin.path = _pluginPath; 64458992c6bS猫头猫 plugins = plugins.concat(plugin); 64525c1bd29S猫头猫 if (oldVersionPlugin) { 64625c1bd29S猫头猫 plugins = plugins.filter( 64725c1bd29S猫头猫 _ => _.hash !== oldVersionPlugin.hash, 64825c1bd29S猫头猫 ); 64925c1bd29S猫头猫 try { 65025c1bd29S猫头猫 await unlink(oldVersionPlugin.path); 65125c1bd29S猫头猫 } catch {} 65225c1bd29S猫头猫 } 65358992c6bS猫头猫 pluginStateMapper.notify(); 65458992c6bS猫头猫 return; 65558992c6bS猫头猫 } 65674acbfc0S猫头猫 throw new Error('插件无法解析!'); 65758992c6bS猫头猫 } 65825c1bd29S猫头猫 } catch (e: any) { 65958992c6bS猫头猫 errorLog('URL安装插件失败', e); 66025c1bd29S猫头猫 throw new Error(e?.message ?? ''); 66158992c6bS猫头猫 } 66258992c6bS猫头猫} 66358992c6bS猫头猫 664927dbe93S猫头猫/** 卸载插件 */ 665927dbe93S猫头猫async function uninstallPlugin(hash: string) { 666927dbe93S猫头猫 const targetIndex = plugins.findIndex(_ => _.hash === hash); 667927dbe93S猫头猫 if (targetIndex !== -1) { 668927dbe93S猫头猫 try { 66924e5e74aS猫头猫 const pluginName = plugins[targetIndex].name; 670927dbe93S猫头猫 await unlink(plugins[targetIndex].path); 671927dbe93S猫头猫 plugins = plugins.filter(_ => _.hash !== hash); 672927dbe93S猫头猫 pluginStateMapper.notify(); 67324e5e74aS猫头猫 if (plugins.every(_ => _.name !== pluginName)) { 67424e5e74aS猫头猫 await MediaMeta.removePlugin(pluginName); 67524e5e74aS猫头猫 } 676927dbe93S猫头猫 } catch {} 677927dbe93S猫头猫 } 678927dbe93S猫头猫} 679927dbe93S猫头猫 68008882a77S猫头猫async function uninstallAllPlugins() { 68108882a77S猫头猫 await Promise.all( 68208882a77S猫头猫 plugins.map(async plugin => { 68308882a77S猫头猫 try { 68408882a77S猫头猫 const pluginName = plugin.name; 68508882a77S猫头猫 await unlink(plugin.path); 68608882a77S猫头猫 await MediaMeta.removePlugin(pluginName); 68708882a77S猫头猫 } catch (e) {} 68808882a77S猫头猫 }), 68908882a77S猫头猫 ); 69008882a77S猫头猫 plugins = []; 69108882a77S猫头猫 pluginStateMapper.notify(); 692*e08d37a3S猫头猫 693*e08d37a3S猫头猫 /** 清除空余文件,异步做就可以了 */ 694*e08d37a3S猫头猫 readDir(pathConst.pluginPath) 695*e08d37a3S猫头猫 .then(fns => { 696*e08d37a3S猫头猫 fns.forEach(fn => { 697*e08d37a3S猫头猫 unlink(fn.path).catch(emptyFunction); 698*e08d37a3S猫头猫 }); 699*e08d37a3S猫头猫 }) 700*e08d37a3S猫头猫 .catch(emptyFunction); 70108882a77S猫头猫} 70208882a77S猫头猫 70325c1bd29S猫头猫async function updatePlugin(plugin: Plugin) { 70425c1bd29S猫头猫 const updateUrl = plugin.instance.srcUrl; 70525c1bd29S猫头猫 if (!updateUrl) { 70625c1bd29S猫头猫 throw new Error('没有更新源'); 70725c1bd29S猫头猫 } 70825c1bd29S猫头猫 try { 70925c1bd29S猫头猫 await installPluginFromUrl(updateUrl); 71025c1bd29S猫头猫 } catch (e: any) { 71125c1bd29S猫头猫 if (e.message === '插件已安装') { 71225c1bd29S猫头猫 throw new Error('当前已是最新版本'); 71325c1bd29S猫头猫 } else { 71425c1bd29S猫头猫 throw e; 71525c1bd29S猫头猫 } 71625c1bd29S猫头猫 } 71725c1bd29S猫头猫} 71825c1bd29S猫头猫 719927dbe93S猫头猫function getByMedia(mediaItem: ICommon.IMediaBase) { 720927dbe93S猫头猫 return getByName(mediaItem.platform); 721927dbe93S猫头猫} 722927dbe93S猫头猫 723927dbe93S猫头猫function getByHash(hash: string) { 7247993f90eS猫头猫 return hash === localPluginHash 7257993f90eS猫头猫 ? localFilePlugin 7267993f90eS猫头猫 : plugins.find(_ => _.hash === hash); 727927dbe93S猫头猫} 728927dbe93S猫头猫 729927dbe93S猫头猫function getByName(name: string) { 7307993f90eS猫头猫 return name === localPluginPlatform 7310e4173cdS猫头猫 ? localFilePlugin 7320e4173cdS猫头猫 : plugins.find(_ => _.name === name); 733927dbe93S猫头猫} 734927dbe93S猫头猫 735927dbe93S猫头猫function getValidPlugins() { 736927dbe93S猫头猫 return plugins.filter(_ => _.state === 'enabled'); 737927dbe93S猫头猫} 738927dbe93S猫头猫 739efb9da24S猫头猫function getSearchablePlugins() { 740efb9da24S猫头猫 return plugins.filter(_ => _.state === 'enabled' && _.instance.search); 741efb9da24S猫头猫} 742efb9da24S猫头猫 743*e08d37a3S猫头猫function getSortedSearchablePlugins() { 744*e08d37a3S猫头猫 return getSearchablePlugins().sort((a, b) => 745*e08d37a3S猫头猫 (PluginMeta.getPluginMeta(a).order ?? Infinity) - 746*e08d37a3S猫头猫 (PluginMeta.getPluginMeta(b).order ?? Infinity) < 747*e08d37a3S猫头猫 0 748*e08d37a3S猫头猫 ? -1 749*e08d37a3S猫头猫 : 1, 750*e08d37a3S猫头猫 ); 751*e08d37a3S猫头猫} 752*e08d37a3S猫头猫 753*e08d37a3S猫头猫function useSortedPlugins() { 754*e08d37a3S猫头猫 const _plugins = pluginStateMapper.useMappedState(); 755*e08d37a3S猫头猫 const _pluginMetaAll = PluginMeta.usePluginMetaAll(); 756*e08d37a3S猫头猫 757*e08d37a3S猫头猫 return [..._plugins].sort((a, b) => 758*e08d37a3S猫头猫 (_pluginMetaAll[a.name]?.order ?? Infinity) - 759*e08d37a3S猫头猫 (_pluginMetaAll[b.name]?.order ?? Infinity) < 760*e08d37a3S猫头猫 0 761*e08d37a3S猫头猫 ? -1 762*e08d37a3S猫头猫 : 1, 763*e08d37a3S猫头猫 ); 764*e08d37a3S猫头猫} 765*e08d37a3S猫头猫 766927dbe93S猫头猫const PluginManager = { 767927dbe93S猫头猫 setup, 768927dbe93S猫头猫 installPlugin, 76958992c6bS猫头猫 installPluginFromUrl, 77025c1bd29S猫头猫 updatePlugin, 771927dbe93S猫头猫 uninstallPlugin, 772927dbe93S猫头猫 getByMedia, 773927dbe93S猫头猫 getByHash, 774927dbe93S猫头猫 getByName, 775927dbe93S猫头猫 getValidPlugins, 776efb9da24S猫头猫 getSearchablePlugins, 777*e08d37a3S猫头猫 getSortedSearchablePlugins, 7785276aef9S猫头猫 usePlugins: pluginStateMapper.useMappedState, 779*e08d37a3S猫头猫 useSortedPlugins, 78008882a77S猫头猫 uninstallAllPlugins, 7815276aef9S猫头猫}; 782927dbe93S猫头猫 783927dbe93S猫头猫export default PluginManager; 784