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'; 16927dbe93S猫头猫import {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'; 23927dbe93S猫头猫import {isSameMediaItem, resetMediaItem} from '@/utils/mediaItem'; 24cfa0fc07S猫头猫import { 25cfa0fc07S猫头猫 CacheControl, 26cfa0fc07S猫头猫 internalSerialzeKey, 27cfa0fc07S猫头猫 internalSymbolKey, 28cfa0fc07S猫头猫} from '@/constants/commonConst'; 29927dbe93S猫头猫import Download from './download'; 30927dbe93S猫头猫import delay from '@/utils/delay'; 314d9d3c4cS猫头猫import * as cheerio from 'cheerio'; 32ef714860S猫头猫import Network from './network'; 33927dbe93S猫头猫 34927dbe93S猫头猫axios.defaults.timeout = 1500; 35927dbe93S猫头猫 36927dbe93S猫头猫const sha256 = CryptoJs.SHA256; 37927dbe93S猫头猫 38cfa0fc07S猫头猫export enum PluginStateCode { 39927dbe93S猫头猫 /** 版本不匹配 */ 40927dbe93S猫头猫 VersionNotMatch = 'VERSION NOT MATCH', 41927dbe93S猫头猫 /** 无法解析 */ 42927dbe93S猫头猫 CannotParse = 'CANNOT PARSE', 43927dbe93S猫头猫} 44927dbe93S猫头猫 45927dbe93S猫头猫export class Plugin { 46927dbe93S猫头猫 /** 插件名 */ 47927dbe93S猫头猫 public name: string; 48927dbe93S猫头猫 /** 插件的hash,作为唯一id */ 49927dbe93S猫头猫 public hash: string; 50927dbe93S猫头猫 /** 插件状态:激活、关闭、错误 */ 51927dbe93S猫头猫 public state: 'enabled' | 'disabled' | 'error'; 52927dbe93S猫头猫 /** 插件支持的搜索类型 */ 53927dbe93S猫头猫 public supportedSearchType?: string; 54927dbe93S猫头猫 /** 插件状态信息 */ 55927dbe93S猫头猫 public stateCode?: PluginStateCode; 56927dbe93S猫头猫 /** 插件的实例 */ 57927dbe93S猫头猫 public instance: IPlugin.IPluginInstance; 58927dbe93S猫头猫 /** 插件路径 */ 59927dbe93S猫头猫 public path: string; 60927dbe93S猫头猫 /** 插件方法 */ 61927dbe93S猫头猫 public methods: PluginMethods; 62927dbe93S猫头猫 63927dbe93S猫头猫 constructor(funcCode: string, pluginPath: string) { 64927dbe93S猫头猫 this.state = 'enabled'; 65927dbe93S猫头猫 let _instance: IPlugin.IPluginInstance; 66927dbe93S猫头猫 try { 674060c00aS猫头猫 // eslint-disable-next-line no-new-func 68927dbe93S猫头猫 _instance = Function(` 69927dbe93S猫头猫 'use strict'; 70927dbe93S猫头猫 try { 71927dbe93S猫头猫 return ${funcCode}; 72927dbe93S猫头猫 } catch(e) { 73927dbe93S猫头猫 return null; 74927dbe93S猫头猫 } 75ef60be1cS猫头猫 `)()({CryptoJs, axios, dayjs, cheerio, bigInt, qs}); 76927dbe93S猫头猫 this.checkValid(_instance); 77927dbe93S猫头猫 } catch (e: any) { 78927dbe93S猫头猫 this.state = 'error'; 79927dbe93S猫头猫 this.stateCode = PluginStateCode.CannotParse; 80927dbe93S猫头猫 if (e?.stateCode) { 81927dbe93S猫头猫 this.stateCode = e.stateCode; 82927dbe93S猫头猫 } 83927dbe93S猫头猫 errorLog(`${pluginPath}插件无法解析 `, { 84927dbe93S猫头猫 stateCode: this.stateCode, 85927dbe93S猫头猫 message: e?.message, 86927dbe93S猫头猫 stack: e?.stack, 87927dbe93S猫头猫 }); 88927dbe93S猫头猫 _instance = e?.instance ?? { 89927dbe93S猫头猫 _path: '', 90927dbe93S猫头猫 platform: '', 91927dbe93S猫头猫 appVersion: '', 9220e6a092S猫头猫 async getMediaSource() { 93927dbe93S猫头猫 return null; 94927dbe93S猫头猫 }, 95927dbe93S猫头猫 async search() { 96927dbe93S猫头猫 return {}; 97927dbe93S猫头猫 }, 98927dbe93S猫头猫 async getAlbumInfo() { 99927dbe93S猫头猫 return null; 100927dbe93S猫头猫 }, 101927dbe93S猫头猫 }; 102927dbe93S猫头猫 } 103927dbe93S猫头猫 this.instance = _instance; 104927dbe93S猫头猫 this.path = pluginPath; 105927dbe93S猫头猫 this.name = _instance.platform; 106927dbe93S猫头猫 if (this.instance.platform === '') { 107927dbe93S猫头猫 this.hash = ''; 108927dbe93S猫头猫 } else { 109927dbe93S猫头猫 this.hash = sha256(funcCode).toString(); 110927dbe93S猫头猫 } 111927dbe93S猫头猫 112927dbe93S猫头猫 // 放在最后 113927dbe93S猫头猫 this.methods = new PluginMethods(this); 114927dbe93S猫头猫 } 115927dbe93S猫头猫 116927dbe93S猫头猫 private checkValid(_instance: IPlugin.IPluginInstance) { 117927dbe93S猫头猫 /** 版本号校验 */ 118927dbe93S猫头猫 if ( 119927dbe93S猫头猫 _instance.appVersion && 120927dbe93S猫头猫 !satisfies(DeviceInfo.getVersion(), _instance.appVersion) 121927dbe93S猫头猫 ) { 122927dbe93S猫头猫 throw { 123927dbe93S猫头猫 instance: _instance, 124927dbe93S猫头猫 stateCode: PluginStateCode.VersionNotMatch, 125927dbe93S猫头猫 }; 126927dbe93S猫头猫 } 127927dbe93S猫头猫 return true; 128927dbe93S猫头猫 } 129927dbe93S猫头猫} 130927dbe93S猫头猫 131927dbe93S猫头猫/** 有缓存等信息 */ 132927dbe93S猫头猫class PluginMethods implements IPlugin.IPluginInstanceMethods { 133927dbe93S猫头猫 private plugin; 134927dbe93S猫头猫 constructor(plugin: Plugin) { 135927dbe93S猫头猫 this.plugin = plugin; 136927dbe93S猫头猫 } 137927dbe93S猫头猫 /** 搜索 */ 138927dbe93S猫头猫 async search<T extends ICommon.SupportMediaType>( 139927dbe93S猫头猫 query: string, 140927dbe93S猫头猫 page: number, 141927dbe93S猫头猫 type: T, 142927dbe93S猫头猫 ): Promise<IPlugin.ISearchResult<T>> { 143927dbe93S猫头猫 if (!this.plugin.instance.search) { 144927dbe93S猫头猫 return { 145927dbe93S猫头猫 isEnd: true, 146927dbe93S猫头猫 data: [], 147927dbe93S猫头猫 }; 148927dbe93S猫头猫 } 149927dbe93S猫头猫 1504060c00aS猫头猫 const result = 1514060c00aS猫头猫 (await this.plugin.instance.search(query, page, type)) ?? {}; 152927dbe93S猫头猫 if (Array.isArray(result.data)) { 153927dbe93S猫头猫 result.data.forEach(_ => { 154927dbe93S猫头猫 resetMediaItem(_, this.plugin.name); 155927dbe93S猫头猫 }); 156927dbe93S猫头猫 return { 157927dbe93S猫头猫 isEnd: result.isEnd ?? true, 158927dbe93S猫头猫 data: result.data, 159927dbe93S猫头猫 }; 160927dbe93S猫头猫 } 161927dbe93S猫头猫 return { 162927dbe93S猫头猫 isEnd: true, 163927dbe93S猫头猫 data: [], 164927dbe93S猫头猫 }; 165927dbe93S猫头猫 } 166927dbe93S猫头猫 167927dbe93S猫头猫 /** 获取真实源 */ 16820e6a092S猫头猫 async getMediaSource( 169927dbe93S猫头猫 musicItem: IMusic.IMusicItemBase, 170927dbe93S猫头猫 retryCount = 1, 17120e6a092S猫头猫 ): Promise<IPlugin.IMediaSourceResult> { 172927dbe93S猫头猫 // 1. 本地搜索 其实直接读mediameta就好了 173927dbe93S猫头猫 const localPath = 174927dbe93S猫头猫 musicItem?.[internalSymbolKey]?.localPath ?? 175927dbe93S猫头猫 Download.getDownloaded(musicItem)?.[internalSymbolKey]?.localPath; 176927dbe93S猫头猫 if (localPath && (await exists(localPath))) { 1775276aef9S猫头猫 trace('播放', '本地播放'); 178927dbe93S猫头猫 return { 179927dbe93S猫头猫 url: localPath, 180927dbe93S猫头猫 }; 181927dbe93S猫头猫 } 182927dbe93S猫头猫 // 2. 缓存播放 183927dbe93S猫头猫 const mediaCache = Cache.get(musicItem); 18448f4b873S猫头猫 const pluginCacheControl = this.plugin.instance.cacheControl; 185cfa0fc07S猫头猫 if ( 186cfa0fc07S猫头猫 mediaCache && 187cfa0fc07S猫头猫 mediaCache?.url && 18848f4b873S猫头猫 (pluginCacheControl === CacheControl.Cache || 18948f4b873S猫头猫 (pluginCacheControl === CacheControl.NoCache && 190ef714860S猫头猫 Network.isOffline())) 191cfa0fc07S猫头猫 ) { 1925276aef9S猫头猫 trace('播放', '缓存播放'); 193927dbe93S猫头猫 return { 194927dbe93S猫头猫 url: mediaCache.url, 195927dbe93S猫头猫 headers: mediaCache.headers, 1964060c00aS猫头猫 userAgent: 1974060c00aS猫头猫 mediaCache.userAgent ?? mediaCache.headers?.['user-agent'], 198927dbe93S猫头猫 }; 199927dbe93S猫头猫 } 200927dbe93S猫头猫 // 3. 插件解析 20120e6a092S猫头猫 if (!this.plugin.instance.getMediaSource) { 202927dbe93S猫头猫 return {url: musicItem.url}; 203927dbe93S猫头猫 } 204927dbe93S猫头猫 try { 20548f4b873S猫头猫 const {url, headers} = 20620e6a092S猫头猫 (await this.plugin.instance.getMediaSource(musicItem)) ?? {}; 207927dbe93S猫头猫 if (!url) { 208927dbe93S猫头猫 throw new Error(); 209927dbe93S猫头猫 } 2105276aef9S猫头猫 trace('播放', '插件播放'); 211927dbe93S猫头猫 const result = { 212927dbe93S猫头猫 url, 213927dbe93S猫头猫 headers, 214927dbe93S猫头猫 userAgent: headers?.['user-agent'], 215cfa0fc07S猫头猫 } as IPlugin.IMediaSourceResult; 216927dbe93S猫头猫 21748f4b873S猫头猫 if (pluginCacheControl !== CacheControl.NoStore) { 218927dbe93S猫头猫 Cache.update(musicItem, result); 219752ffc5aS猫头猫 } 220cfa0fc07S猫头猫 221927dbe93S猫头猫 return result; 222927dbe93S猫头猫 } catch (e: any) { 223927dbe93S猫头猫 if (retryCount > 0) { 224927dbe93S猫头猫 await delay(150); 22520e6a092S猫头猫 return this.getMediaSource(musicItem, --retryCount); 226927dbe93S猫头猫 } 227927dbe93S猫头猫 errorLog('获取真实源失败', e?.message); 228927dbe93S猫头猫 throw e; 229927dbe93S猫头猫 } 230927dbe93S猫头猫 } 231927dbe93S猫头猫 232927dbe93S猫头猫 /** 获取音乐详情 */ 233927dbe93S猫头猫 async getMusicInfo( 234927dbe93S猫头猫 musicItem: ICommon.IMediaBase, 235927dbe93S猫头猫 ): Promise<IMusic.IMusicItem | null> { 236927dbe93S猫头猫 if (!this.plugin.instance.getMusicInfo) { 237927dbe93S猫头猫 return musicItem as IMusic.IMusicItem; 238927dbe93S猫头猫 } 239927dbe93S猫头猫 return ( 240927dbe93S猫头猫 this.plugin.instance.getMusicInfo( 241927dbe93S猫头猫 resetMediaItem(musicItem, undefined, true), 242927dbe93S猫头猫 ) ?? musicItem 243927dbe93S猫头猫 ); 244927dbe93S猫头猫 } 245927dbe93S猫头猫 246927dbe93S猫头猫 /** 获取歌词 */ 247927dbe93S猫头猫 async getLyric( 248927dbe93S猫头猫 musicItem: IMusic.IMusicItemBase, 249927dbe93S猫头猫 from?: IMusic.IMusicItemBase, 250927dbe93S猫头猫 ): Promise<ILyric.ILyricSource | null> { 251927dbe93S猫头猫 // 1.额外存储的meta信息 252927dbe93S猫头猫 const meta = MediaMeta.get(musicItem); 253927dbe93S猫头猫 if (meta && meta.associatedLrc) { 254927dbe93S猫头猫 // 有关联歌词 255927dbe93S猫头猫 if ( 256927dbe93S猫头猫 isSameMediaItem(musicItem, from) || 257927dbe93S猫头猫 isSameMediaItem(meta.associatedLrc, musicItem) 258927dbe93S猫头猫 ) { 259927dbe93S猫头猫 // 形成环路,断开当前的环 260927dbe93S猫头猫 await MediaMeta.update(musicItem, { 261927dbe93S猫头猫 associatedLrc: undefined, 262927dbe93S猫头猫 }); 263927dbe93S猫头猫 // 无歌词 264927dbe93S猫头猫 return null; 265927dbe93S猫头猫 } 266927dbe93S猫头猫 // 获取关联歌词 2677a91f04fS猫头猫 const associatedMeta = MediaMeta.get(meta.associatedLrc) ?? {}; 2684060c00aS猫头猫 const result = await this.getLyric( 2697a91f04fS猫头猫 {...meta.associatedLrc, ...associatedMeta}, 2704060c00aS猫头猫 from ?? musicItem, 2714060c00aS猫头猫 ); 272927dbe93S猫头猫 if (result) { 273927dbe93S猫头猫 // 如果有关联歌词,就返回关联歌词,深度优先 274927dbe93S猫头猫 return result; 275927dbe93S猫头猫 } 276927dbe93S猫头猫 } 277927dbe93S猫头猫 const cache = Cache.get(musicItem); 278927dbe93S猫头猫 let rawLrc = meta?.rawLrc || musicItem.rawLrc || cache?.rawLrc; 279927dbe93S猫头猫 let lrcUrl = meta?.lrc || musicItem.lrc || cache?.lrc; 280927dbe93S猫头猫 // 如果存在文本 281927dbe93S猫头猫 if (rawLrc) { 282927dbe93S猫头猫 return { 283927dbe93S猫头猫 rawLrc, 284927dbe93S猫头猫 lrc: lrcUrl, 285927dbe93S猫头猫 }; 286927dbe93S猫头猫 } 287927dbe93S猫头猫 // 2.本地缓存 288927dbe93S猫头猫 const localLrc = 289927dbe93S猫头猫 meta?.[internalSerialzeKey]?.local?.localLrc || 290927dbe93S猫头猫 cache?.[internalSerialzeKey]?.local?.localLrc; 291927dbe93S猫头猫 if (localLrc && (await exists(localLrc))) { 292927dbe93S猫头猫 rawLrc = await readFile(localLrc, 'utf8'); 293927dbe93S猫头猫 return { 294927dbe93S猫头猫 rawLrc, 295927dbe93S猫头猫 lrc: lrcUrl, 296927dbe93S猫头猫 }; 297927dbe93S猫头猫 } 298927dbe93S猫头猫 // 3.优先使用url 299927dbe93S猫头猫 if (lrcUrl) { 300927dbe93S猫头猫 try { 301927dbe93S猫头猫 // 需要超时时间 axios timeout 但是没生效 3022a3194f5S猫头猫 rawLrc = (await axios.get(lrcUrl, {timeout: 1500})).data; 303927dbe93S猫头猫 return { 304927dbe93S猫头猫 rawLrc, 305927dbe93S猫头猫 lrc: lrcUrl, 306927dbe93S猫头猫 }; 307927dbe93S猫头猫 } catch { 308927dbe93S猫头猫 lrcUrl = undefined; 309927dbe93S猫头猫 } 310927dbe93S猫头猫 } 311927dbe93S猫头猫 // 4. 如果地址失效 312927dbe93S猫头猫 if (!lrcUrl) { 313927dbe93S猫头猫 // 插件获得url 314927dbe93S猫头猫 try { 3157a91f04fS猫头猫 let lrcSource; 3167a91f04fS猫头猫 if (from) { 3177a91f04fS猫头猫 lrcSource = await PluginManager.getByMedia( 3187a91f04fS猫头猫 musicItem, 3197a91f04fS猫头猫 )?.instance?.getLyric?.( 320927dbe93S猫头猫 resetMediaItem(musicItem, undefined, true), 321927dbe93S猫头猫 ); 3227a91f04fS猫头猫 } else { 3237a91f04fS猫头猫 lrcSource = await this.plugin.instance?.getLyric?.( 3247a91f04fS猫头猫 resetMediaItem(musicItem, undefined, true), 3257a91f04fS猫头猫 ); 3267a91f04fS猫头猫 } 3277a91f04fS猫头猫 328927dbe93S猫头猫 rawLrc = lrcSource?.rawLrc; 329927dbe93S猫头猫 lrcUrl = lrcSource?.lrc; 330927dbe93S猫头猫 } catch (e: any) { 331927dbe93S猫头猫 trace('插件获取歌词失败', e?.message, 'error'); 332927dbe93S猫头猫 } 333927dbe93S猫头猫 } 334927dbe93S猫头猫 // 5. 最后一次请求 335927dbe93S猫头猫 if (rawLrc || lrcUrl) { 336927dbe93S猫头猫 const filename = `${pathConst.lrcCachePath}${nanoid()}.lrc`; 337927dbe93S猫头猫 if (lrcUrl) { 338927dbe93S猫头猫 try { 3392a3194f5S猫头猫 rawLrc = (await axios.get(lrcUrl, {timeout: 1500})).data; 340927dbe93S猫头猫 } catch {} 341927dbe93S猫头猫 } 342927dbe93S猫头猫 if (rawLrc) { 343927dbe93S猫头猫 await writeFile(filename, rawLrc, 'utf8'); 344927dbe93S猫头猫 // 写入缓存 345927dbe93S猫头猫 Cache.update(musicItem, [ 346927dbe93S猫头猫 [`${internalSerialzeKey}.local.localLrc`, filename], 347927dbe93S猫头猫 ]); 348927dbe93S猫头猫 // 如果有meta 349927dbe93S猫头猫 if (meta) { 350927dbe93S猫头猫 MediaMeta.update(musicItem, [ 351927dbe93S猫头猫 [`${internalSerialzeKey}.local.localLrc`, filename], 352927dbe93S猫头猫 ]); 353927dbe93S猫头猫 } 354927dbe93S猫头猫 return { 355927dbe93S猫头猫 rawLrc, 356927dbe93S猫头猫 lrc: lrcUrl, 357927dbe93S猫头猫 }; 358927dbe93S猫头猫 } 359927dbe93S猫头猫 } 360927dbe93S猫头猫 361927dbe93S猫头猫 return null; 362927dbe93S猫头猫 } 363927dbe93S猫头猫 364927dbe93S猫头猫 /** 获取歌词文本 */ 365927dbe93S猫头猫 async getLyricText( 366927dbe93S猫头猫 musicItem: IMusic.IMusicItem, 367927dbe93S猫头猫 ): Promise<string | undefined> { 368927dbe93S猫头猫 return (await this.getLyric(musicItem))?.rawLrc; 369927dbe93S猫头猫 } 370927dbe93S猫头猫 371927dbe93S猫头猫 /** 获取专辑信息 */ 372927dbe93S猫头猫 async getAlbumInfo( 373927dbe93S猫头猫 albumItem: IAlbum.IAlbumItemBase, 374927dbe93S猫头猫 ): Promise<IAlbum.IAlbumItem | null> { 375927dbe93S猫头猫 if (!this.plugin.instance.getAlbumInfo) { 376927dbe93S猫头猫 return {...albumItem, musicList: []}; 377927dbe93S猫头猫 } 378927dbe93S猫头猫 try { 379927dbe93S猫头猫 const result = await this.plugin.instance.getAlbumInfo( 380927dbe93S猫头猫 resetMediaItem(albumItem, undefined, true), 381927dbe93S猫头猫 ); 3825276aef9S猫头猫 if (!result) { 3835276aef9S猫头猫 throw new Error(); 3845276aef9S猫头猫 } 385927dbe93S猫头猫 result?.musicList?.forEach(_ => { 386927dbe93S猫头猫 resetMediaItem(_, this.plugin.name); 387927dbe93S猫头猫 }); 3885276aef9S猫头猫 3895276aef9S猫头猫 return {...albumItem, ...result}; 390927dbe93S猫头猫 } catch { 391927dbe93S猫头猫 return {...albumItem, musicList: []}; 392927dbe93S猫头猫 } 393927dbe93S猫头猫 } 394927dbe93S猫头猫 395927dbe93S猫头猫 /** 查询作者信息 */ 396*efb9da24S猫头猫 async getArtistWorks<T extends IArtist.ArtistMediaType>( 397927dbe93S猫头猫 artistItem: IArtist.IArtistItem, 398927dbe93S猫头猫 page: number, 399927dbe93S猫头猫 type: T, 400927dbe93S猫头猫 ): Promise<IPlugin.ISearchResult<T>> { 401*efb9da24S猫头猫 if (!this.plugin.instance.getArtistWorks) { 402927dbe93S猫头猫 return { 403927dbe93S猫头猫 isEnd: true, 404927dbe93S猫头猫 data: [], 405927dbe93S猫头猫 }; 406927dbe93S猫头猫 } 407927dbe93S猫头猫 try { 408*efb9da24S猫头猫 const result = await this.plugin.instance.getArtistWorks( 409927dbe93S猫头猫 artistItem, 410927dbe93S猫头猫 page, 411927dbe93S猫头猫 type, 412927dbe93S猫头猫 ); 413927dbe93S猫头猫 if (!result.data) { 414927dbe93S猫头猫 return { 415927dbe93S猫头猫 isEnd: true, 416927dbe93S猫头猫 data: [], 417927dbe93S猫头猫 }; 418927dbe93S猫头猫 } 419927dbe93S猫头猫 result.data?.forEach(_ => resetMediaItem(_, this.plugin.name)); 420927dbe93S猫头猫 return { 421927dbe93S猫头猫 isEnd: result.isEnd ?? true, 422927dbe93S猫头猫 data: result.data, 423927dbe93S猫头猫 }; 424927dbe93S猫头猫 } catch (e) { 425927dbe93S猫头猫 throw e; 426927dbe93S猫头猫 } 427927dbe93S猫头猫 } 42808380090S猫头猫 42908380090S猫头猫 /** 导入歌单 */ 43008380090S猫头猫 async importMusicSheet(urlLike: string): Promise<IMusic.IMusicItem[]> { 43108380090S猫头猫 try { 43208380090S猫头猫 const result = 43308380090S猫头猫 (await this.plugin.instance?.importMusicSheet?.(urlLike)) ?? []; 43408380090S猫头猫 result.forEach(_ => resetMediaItem(_, this.plugin.name)); 43508380090S猫头猫 return result; 43608380090S猫头猫 } catch { 43708380090S猫头猫 return []; 43808380090S猫头猫 } 43908380090S猫头猫 } 4404d9d3c4cS猫头猫 /** 导入单曲 */ 4414d9d3c4cS猫头猫 async importMusicItem(urlLike: string): Promise<IMusic.IMusicItem | null> { 4424d9d3c4cS猫头猫 try { 4434d9d3c4cS猫头猫 const result = await this.plugin.instance?.importMusicItem?.( 4444d9d3c4cS猫头猫 urlLike, 4454d9d3c4cS猫头猫 ); 4464d9d3c4cS猫头猫 if (!result) { 4474d9d3c4cS猫头猫 throw new Error(); 4484d9d3c4cS猫头猫 } 4494d9d3c4cS猫头猫 resetMediaItem(result, this.plugin.name); 4504d9d3c4cS猫头猫 return result; 4514d9d3c4cS猫头猫 } catch { 4524d9d3c4cS猫头猫 return null; 4534d9d3c4cS猫头猫 } 4544d9d3c4cS猫头猫 } 455927dbe93S猫头猫} 4561a5528a0S猫头猫 457927dbe93S猫头猫let plugins: Array<Plugin> = []; 458927dbe93S猫头猫const pluginStateMapper = new StateMapper(() => plugins); 459927dbe93S猫头猫 460927dbe93S猫头猫async function setup() { 461927dbe93S猫头猫 const _plugins: Array<Plugin> = []; 462927dbe93S猫头猫 try { 463927dbe93S猫头猫 // 加载插件 464927dbe93S猫头猫 const pluginsPaths = await readDir(pathConst.pluginPath); 465927dbe93S猫头猫 for (let i = 0; i < pluginsPaths.length; ++i) { 466927dbe93S猫头猫 const _pluginUrl = pluginsPaths[i]; 4671e263108S猫头猫 trace('初始化插件', _pluginUrl); 4681e263108S猫头猫 if ( 4691e263108S猫头猫 _pluginUrl.isFile() && 4701e263108S猫头猫 (_pluginUrl.name?.endsWith?.('.js') || 4711e263108S猫头猫 _pluginUrl.path?.endsWith?.('.js')) 4721e263108S猫头猫 ) { 473927dbe93S猫头猫 const funcCode = await readFile(_pluginUrl.path, 'utf8'); 474927dbe93S猫头猫 const plugin = new Plugin(funcCode, _pluginUrl.path); 4754060c00aS猫头猫 const _pluginIndex = _plugins.findIndex( 4764060c00aS猫头猫 p => p.hash === plugin.hash, 4774060c00aS猫头猫 ); 478927dbe93S猫头猫 if (_pluginIndex !== -1) { 479927dbe93S猫头猫 // 重复插件,直接忽略 480927dbe93S猫头猫 return; 481927dbe93S猫头猫 } 482927dbe93S猫头猫 plugin.hash !== '' && _plugins.push(plugin); 483927dbe93S猫头猫 } 484927dbe93S猫头猫 } 485927dbe93S猫头猫 486927dbe93S猫头猫 plugins = _plugins; 487927dbe93S猫头猫 pluginStateMapper.notify(); 488927dbe93S猫头猫 } catch (e: any) { 4894060c00aS猫头猫 ToastAndroid.show( 4904060c00aS猫头猫 `插件初始化失败:${e?.message ?? e}`, 4914060c00aS猫头猫 ToastAndroid.LONG, 4924060c00aS猫头猫 ); 4931a5528a0S猫头猫 errorLog('插件初始化失败', e?.message); 494927dbe93S猫头猫 throw e; 495927dbe93S猫头猫 } 496927dbe93S猫头猫} 497927dbe93S猫头猫 498927dbe93S猫头猫// 安装插件 499927dbe93S猫头猫async function installPlugin(pluginPath: string) { 500b50427a2S猫头猫 if (pluginPath.endsWith('.js')) { 501927dbe93S猫头猫 const funcCode = await readFile(pluginPath, 'utf8'); 502927dbe93S猫头猫 const plugin = new Plugin(funcCode, pluginPath); 503927dbe93S猫头猫 const _pluginIndex = plugins.findIndex(p => p.hash === plugin.hash); 504927dbe93S猫头猫 if (_pluginIndex !== -1) { 5054d9d3c4cS猫头猫 throw new Error('插件已安装'); 506927dbe93S猫头猫 } 507927dbe93S猫头猫 if (plugin.hash !== '') { 508927dbe93S猫头猫 const fn = nanoid(); 509927dbe93S猫头猫 const _pluginPath = `${pathConst.pluginPath}${fn}.js`; 510927dbe93S猫头猫 await copyFile(pluginPath, _pluginPath); 511927dbe93S猫头猫 plugin.path = _pluginPath; 512927dbe93S猫头猫 plugins = plugins.concat(plugin); 513927dbe93S猫头猫 pluginStateMapper.notify(); 5144d9d3c4cS猫头猫 return; 515927dbe93S猫头猫 } 5164d9d3c4cS猫头猫 throw new Error('插件无法解析'); 517927dbe93S猫头猫 } 5184d9d3c4cS猫头猫 throw new Error('插件不存在'); 519927dbe93S猫头猫} 520927dbe93S猫头猫 52158992c6bS猫头猫async function installPluginFromUrl(url: string) { 52258992c6bS猫头猫 try { 52358992c6bS猫头猫 const funcCode = (await axios.get(url)).data; 52458992c6bS猫头猫 if (funcCode) { 52558992c6bS猫头猫 const plugin = new Plugin(funcCode, ''); 52658992c6bS猫头猫 const _pluginIndex = plugins.findIndex(p => p.hash === plugin.hash); 52758992c6bS猫头猫 if (_pluginIndex !== -1) { 52858992c6bS猫头猫 throw new Error('插件已安装'); 52958992c6bS猫头猫 } 53058992c6bS猫头猫 if (plugin.hash !== '') { 53158992c6bS猫头猫 const fn = nanoid(); 53258992c6bS猫头猫 const _pluginPath = `${pathConst.pluginPath}${fn}.js`; 53358992c6bS猫头猫 await writeFile(_pluginPath, funcCode, 'utf8'); 53458992c6bS猫头猫 plugin.path = _pluginPath; 53558992c6bS猫头猫 plugins = plugins.concat(plugin); 53658992c6bS猫头猫 pluginStateMapper.notify(); 53758992c6bS猫头猫 return; 53858992c6bS猫头猫 } 53958992c6bS猫头猫 throw new Error('插件无法解析'); 54058992c6bS猫头猫 } 54158992c6bS猫头猫 } catch (e) { 54258992c6bS猫头猫 errorLog('URL安装插件失败', e); 54358992c6bS猫头猫 throw new Error('插件安装失败'); 54458992c6bS猫头猫 } 54558992c6bS猫头猫} 54658992c6bS猫头猫 547927dbe93S猫头猫/** 卸载插件 */ 548927dbe93S猫头猫async function uninstallPlugin(hash: string) { 549927dbe93S猫头猫 const targetIndex = plugins.findIndex(_ => _.hash === hash); 550927dbe93S猫头猫 if (targetIndex !== -1) { 551927dbe93S猫头猫 try { 55224e5e74aS猫头猫 const pluginName = plugins[targetIndex].name; 553927dbe93S猫头猫 await unlink(plugins[targetIndex].path); 554927dbe93S猫头猫 plugins = plugins.filter(_ => _.hash !== hash); 555927dbe93S猫头猫 pluginStateMapper.notify(); 55624e5e74aS猫头猫 if (plugins.every(_ => _.name !== pluginName)) { 55724e5e74aS猫头猫 await MediaMeta.removePlugin(pluginName); 55824e5e74aS猫头猫 } 559927dbe93S猫头猫 } catch {} 560927dbe93S猫头猫 } 561927dbe93S猫头猫} 562927dbe93S猫头猫 563927dbe93S猫头猫function getByMedia(mediaItem: ICommon.IMediaBase) { 564927dbe93S猫头猫 return getByName(mediaItem.platform); 565927dbe93S猫头猫} 566927dbe93S猫头猫 567927dbe93S猫头猫function getByHash(hash: string) { 568927dbe93S猫头猫 return plugins.find(_ => _.hash === hash); 569927dbe93S猫头猫} 570927dbe93S猫头猫 571927dbe93S猫头猫function getByName(name: string) { 572927dbe93S猫头猫 return plugins.find(_ => _.name === name); 573927dbe93S猫头猫} 574927dbe93S猫头猫 575927dbe93S猫头猫function getValidPlugins() { 576927dbe93S猫头猫 return plugins.filter(_ => _.state === 'enabled'); 577927dbe93S猫头猫} 578927dbe93S猫头猫 579*efb9da24S猫头猫function getSearchablePlugins() { 580*efb9da24S猫头猫 return plugins.filter(_ => _.state === 'enabled' && _.instance.search); 581*efb9da24S猫头猫} 582*efb9da24S猫头猫 583927dbe93S猫头猫const PluginManager = { 584927dbe93S猫头猫 setup, 585927dbe93S猫头猫 installPlugin, 58658992c6bS猫头猫 installPluginFromUrl, 587927dbe93S猫头猫 uninstallPlugin, 588927dbe93S猫头猫 getByMedia, 589927dbe93S猫头猫 getByHash, 590927dbe93S猫头猫 getByName, 591927dbe93S猫头猫 getValidPlugins, 592*efb9da24S猫头猫 getSearchablePlugins, 5935276aef9S猫头猫 usePlugins: pluginStateMapper.useMappedState, 5945276aef9S猫头猫}; 595927dbe93S猫头猫 596927dbe93S猫头猫export default PluginManager; 597