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'; 290e4173cdS猫头猫import {CacheControl, internalSerializeKey} from '@/constants/commonConst'; 30927dbe93S猫头猫import delay from '@/utils/delay'; 314d9d3c4cS猫头猫import * as cheerio from 'cheerio'; 327d7e864fS猫头猫import CookieManager from '@react-native-cookies/cookies'; 337d7e864fS猫头猫import he from 'he'; 34ef714860S猫头猫import Network from './network'; 350e4173cdS猫头猫import LocalMusicSheet from './localMusicSheet'; 360e4173cdS猫头猫import {FileSystem} from 'react-native-file-access'; 37927dbe93S猫头猫 38927dbe93S猫头猫axios.defaults.timeout = 1500; 39927dbe93S猫头猫 40927dbe93S猫头猫const sha256 = CryptoJs.SHA256; 41927dbe93S猫头猫 42cfa0fc07S猫头猫export enum PluginStateCode { 43927dbe93S猫头猫 /** 版本不匹配 */ 44927dbe93S猫头猫 VersionNotMatch = 'VERSION NOT MATCH', 45927dbe93S猫头猫 /** 无法解析 */ 46927dbe93S猫头猫 CannotParse = 'CANNOT PARSE', 47927dbe93S猫头猫} 48927dbe93S猫头猫 49927dbe93S猫头猫export class Plugin { 50927dbe93S猫头猫 /** 插件名 */ 51927dbe93S猫头猫 public name: string; 52927dbe93S猫头猫 /** 插件的hash,作为唯一id */ 53927dbe93S猫头猫 public hash: string; 54927dbe93S猫头猫 /** 插件状态:激活、关闭、错误 */ 55927dbe93S猫头猫 public state: 'enabled' | 'disabled' | 'error'; 56927dbe93S猫头猫 /** 插件支持的搜索类型 */ 57927dbe93S猫头猫 public supportedSearchType?: string; 58927dbe93S猫头猫 /** 插件状态信息 */ 59927dbe93S猫头猫 public stateCode?: PluginStateCode; 60927dbe93S猫头猫 /** 插件的实例 */ 61927dbe93S猫头猫 public instance: IPlugin.IPluginInstance; 62927dbe93S猫头猫 /** 插件路径 */ 63927dbe93S猫头猫 public path: string; 64927dbe93S猫头猫 /** 插件方法 */ 65927dbe93S猫头猫 public methods: PluginMethods; 66927dbe93S猫头猫 67927dbe93S猫头猫 constructor(funcCode: string, pluginPath: string) { 68927dbe93S猫头猫 this.state = 'enabled'; 69927dbe93S猫头猫 let _instance: IPlugin.IPluginInstance; 70927dbe93S猫头猫 try { 714060c00aS猫头猫 // eslint-disable-next-line no-new-func 72927dbe93S猫头猫 _instance = Function(` 73927dbe93S猫头猫 'use strict'; 74927dbe93S猫头猫 try { 75927dbe93S猫头猫 return ${funcCode}; 76927dbe93S猫头猫 } catch(e) { 77927dbe93S猫头猫 return null; 78927dbe93S猫头猫 } 797d7e864fS猫头猫 `)()({ 807d7e864fS猫头猫 CryptoJs, 817d7e864fS猫头猫 axios, 827d7e864fS猫头猫 dayjs, 837d7e864fS猫头猫 cheerio, 847d7e864fS猫头猫 bigInt, 857d7e864fS猫头猫 qs, 867d7e864fS猫头猫 he, 877d7e864fS猫头猫 CookieManager: { 887d7e864fS猫头猫 flush: CookieManager.flush, 897d7e864fS猫头猫 get: CookieManager.get, 907d7e864fS猫头猫 }, 917d7e864fS猫头猫 }); 92927dbe93S猫头猫 this.checkValid(_instance); 93927dbe93S猫头猫 } catch (e: any) { 94927dbe93S猫头猫 this.state = 'error'; 95927dbe93S猫头猫 this.stateCode = PluginStateCode.CannotParse; 96927dbe93S猫头猫 if (e?.stateCode) { 97927dbe93S猫头猫 this.stateCode = e.stateCode; 98927dbe93S猫头猫 } 99927dbe93S猫头猫 errorLog(`${pluginPath}插件无法解析 `, { 100927dbe93S猫头猫 stateCode: this.stateCode, 101927dbe93S猫头猫 message: e?.message, 102927dbe93S猫头猫 stack: e?.stack, 103927dbe93S猫头猫 }); 104927dbe93S猫头猫 _instance = e?.instance ?? { 105927dbe93S猫头猫 _path: '', 106927dbe93S猫头猫 platform: '', 107927dbe93S猫头猫 appVersion: '', 10820e6a092S猫头猫 async getMediaSource() { 109927dbe93S猫头猫 return null; 110927dbe93S猫头猫 }, 111927dbe93S猫头猫 async search() { 112927dbe93S猫头猫 return {}; 113927dbe93S猫头猫 }, 114927dbe93S猫头猫 async getAlbumInfo() { 115927dbe93S猫头猫 return null; 116927dbe93S猫头猫 }, 117927dbe93S猫头猫 }; 118927dbe93S猫头猫 } 119927dbe93S猫头猫 this.instance = _instance; 120927dbe93S猫头猫 this.path = pluginPath; 121927dbe93S猫头猫 this.name = _instance.platform; 122927dbe93S猫头猫 if (this.instance.platform === '') { 123927dbe93S猫头猫 this.hash = ''; 124927dbe93S猫头猫 } else { 125927dbe93S猫头猫 this.hash = sha256(funcCode).toString(); 126927dbe93S猫头猫 } 127927dbe93S猫头猫 128927dbe93S猫头猫 // 放在最后 129927dbe93S猫头猫 this.methods = new PluginMethods(this); 130927dbe93S猫头猫 } 131927dbe93S猫头猫 132927dbe93S猫头猫 private checkValid(_instance: IPlugin.IPluginInstance) { 133927dbe93S猫头猫 /** 版本号校验 */ 134927dbe93S猫头猫 if ( 135927dbe93S猫头猫 _instance.appVersion && 136927dbe93S猫头猫 !satisfies(DeviceInfo.getVersion(), _instance.appVersion) 137927dbe93S猫头猫 ) { 138927dbe93S猫头猫 throw { 139927dbe93S猫头猫 instance: _instance, 140927dbe93S猫头猫 stateCode: PluginStateCode.VersionNotMatch, 141927dbe93S猫头猫 }; 142927dbe93S猫头猫 } 143927dbe93S猫头猫 return true; 144927dbe93S猫头猫 } 145927dbe93S猫头猫} 146927dbe93S猫头猫 147927dbe93S猫头猫/** 有缓存等信息 */ 148927dbe93S猫头猫class PluginMethods implements IPlugin.IPluginInstanceMethods { 149927dbe93S猫头猫 private plugin; 150927dbe93S猫头猫 constructor(plugin: Plugin) { 151927dbe93S猫头猫 this.plugin = plugin; 152927dbe93S猫头猫 } 153927dbe93S猫头猫 /** 搜索 */ 154927dbe93S猫头猫 async search<T extends ICommon.SupportMediaType>( 155927dbe93S猫头猫 query: string, 156927dbe93S猫头猫 page: number, 157927dbe93S猫头猫 type: T, 158927dbe93S猫头猫 ): Promise<IPlugin.ISearchResult<T>> { 159927dbe93S猫头猫 if (!this.plugin.instance.search) { 160927dbe93S猫头猫 return { 161927dbe93S猫头猫 isEnd: true, 162927dbe93S猫头猫 data: [], 163927dbe93S猫头猫 }; 164927dbe93S猫头猫 } 165927dbe93S猫头猫 1664060c00aS猫头猫 const result = 1674060c00aS猫头猫 (await this.plugin.instance.search(query, page, type)) ?? {}; 168927dbe93S猫头猫 if (Array.isArray(result.data)) { 169927dbe93S猫头猫 result.data.forEach(_ => { 170927dbe93S猫头猫 resetMediaItem(_, this.plugin.name); 171927dbe93S猫头猫 }); 172927dbe93S猫头猫 return { 173927dbe93S猫头猫 isEnd: result.isEnd ?? true, 174927dbe93S猫头猫 data: result.data, 175927dbe93S猫头猫 }; 176927dbe93S猫头猫 } 177927dbe93S猫头猫 return { 178927dbe93S猫头猫 isEnd: true, 179927dbe93S猫头猫 data: [], 180927dbe93S猫头猫 }; 181927dbe93S猫头猫 } 182927dbe93S猫头猫 183927dbe93S猫头猫 /** 获取真实源 */ 18420e6a092S猫头猫 async getMediaSource( 185927dbe93S猫头猫 musicItem: IMusic.IMusicItemBase, 186927dbe93S猫头猫 retryCount = 1, 18720e6a092S猫头猫 ): Promise<IPlugin.IMediaSourceResult> { 188927dbe93S猫头猫 // 1. 本地搜索 其实直接读mediameta就好了 189927dbe93S猫头猫 const localPath = 1900e4173cdS猫头猫 getInternalData<string>(musicItem, InternalDataType.LOCALPATH) ?? 1910e4173cdS猫头猫 getInternalData<string>( 1920e4173cdS猫头猫 LocalMusicSheet.isLocalMusic(musicItem), 1930e4173cdS猫头猫 InternalDataType.LOCALPATH, 1940e4173cdS猫头猫 ); 1950e4173cdS猫头猫 if (localPath && (await FileSystem.exists(localPath))) { 1960e4173cdS猫头猫 trace('本地播放', localPath); 197927dbe93S猫头猫 return { 198927dbe93S猫头猫 url: localPath, 199927dbe93S猫头猫 }; 200927dbe93S猫头猫 } 201*f5935920S猫头猫 if (musicItem.platform === '本地') { 202*f5935920S猫头猫 throw new Error('本地音乐不存在'); 203*f5935920S猫头猫 } 204927dbe93S猫头猫 // 2. 缓存播放 205927dbe93S猫头猫 const mediaCache = Cache.get(musicItem); 20648f4b873S猫头猫 const pluginCacheControl = this.plugin.instance.cacheControl; 207cfa0fc07S猫头猫 if ( 208cfa0fc07S猫头猫 mediaCache && 209cfa0fc07S猫头猫 mediaCache?.url && 21048f4b873S猫头猫 (pluginCacheControl === CacheControl.Cache || 21148f4b873S猫头猫 (pluginCacheControl === CacheControl.NoCache && 212ef714860S猫头猫 Network.isOffline())) 213cfa0fc07S猫头猫 ) { 2145276aef9S猫头猫 trace('播放', '缓存播放'); 215927dbe93S猫头猫 return { 216927dbe93S猫头猫 url: mediaCache.url, 217927dbe93S猫头猫 headers: mediaCache.headers, 2184060c00aS猫头猫 userAgent: 2194060c00aS猫头猫 mediaCache.userAgent ?? mediaCache.headers?.['user-agent'], 220927dbe93S猫头猫 }; 221927dbe93S猫头猫 } 222927dbe93S猫头猫 // 3. 插件解析 22320e6a092S猫头猫 if (!this.plugin.instance.getMediaSource) { 224927dbe93S猫头猫 return {url: musicItem.url}; 225927dbe93S猫头猫 } 226927dbe93S猫头猫 try { 22748f4b873S猫头猫 const {url, headers} = 22820e6a092S猫头猫 (await this.plugin.instance.getMediaSource(musicItem)) ?? {}; 229927dbe93S猫头猫 if (!url) { 230927dbe93S猫头猫 throw new Error(); 231927dbe93S猫头猫 } 2325276aef9S猫头猫 trace('播放', '插件播放'); 233927dbe93S猫头猫 const result = { 234927dbe93S猫头猫 url, 235927dbe93S猫头猫 headers, 236927dbe93S猫头猫 userAgent: headers?.['user-agent'], 237cfa0fc07S猫头猫 } as IPlugin.IMediaSourceResult; 238927dbe93S猫头猫 23948f4b873S猫头猫 if (pluginCacheControl !== CacheControl.NoStore) { 240927dbe93S猫头猫 Cache.update(musicItem, result); 241752ffc5aS猫头猫 } 242cfa0fc07S猫头猫 243927dbe93S猫头猫 return result; 244927dbe93S猫头猫 } catch (e: any) { 245*f5935920S猫头猫 console.log('eeee', e); 246927dbe93S猫头猫 if (retryCount > 0) { 247927dbe93S猫头猫 await delay(150); 24820e6a092S猫头猫 return this.getMediaSource(musicItem, --retryCount); 249927dbe93S猫头猫 } 250927dbe93S猫头猫 errorLog('获取真实源失败', e?.message); 251927dbe93S猫头猫 throw e; 252927dbe93S猫头猫 } 253927dbe93S猫头猫 } 254927dbe93S猫头猫 255927dbe93S猫头猫 /** 获取音乐详情 */ 256927dbe93S猫头猫 async getMusicInfo( 257927dbe93S猫头猫 musicItem: ICommon.IMediaBase, 258927dbe93S猫头猫 ): Promise<IMusic.IMusicItem | null> { 259927dbe93S猫头猫 if (!this.plugin.instance.getMusicInfo) { 260927dbe93S猫头猫 return musicItem as IMusic.IMusicItem; 261927dbe93S猫头猫 } 262927dbe93S猫头猫 return ( 263927dbe93S猫头猫 this.plugin.instance.getMusicInfo( 264927dbe93S猫头猫 resetMediaItem(musicItem, undefined, true), 265927dbe93S猫头猫 ) ?? musicItem 266927dbe93S猫头猫 ); 267927dbe93S猫头猫 } 268927dbe93S猫头猫 269927dbe93S猫头猫 /** 获取歌词 */ 270927dbe93S猫头猫 async getLyric( 271927dbe93S猫头猫 musicItem: IMusic.IMusicItemBase, 272927dbe93S猫头猫 from?: IMusic.IMusicItemBase, 273927dbe93S猫头猫 ): Promise<ILyric.ILyricSource | null> { 274927dbe93S猫头猫 // 1.额外存储的meta信息 275927dbe93S猫头猫 const meta = MediaMeta.get(musicItem); 276927dbe93S猫头猫 if (meta && meta.associatedLrc) { 277927dbe93S猫头猫 // 有关联歌词 278927dbe93S猫头猫 if ( 279927dbe93S猫头猫 isSameMediaItem(musicItem, from) || 280927dbe93S猫头猫 isSameMediaItem(meta.associatedLrc, musicItem) 281927dbe93S猫头猫 ) { 282927dbe93S猫头猫 // 形成环路,断开当前的环 283927dbe93S猫头猫 await MediaMeta.update(musicItem, { 284927dbe93S猫头猫 associatedLrc: undefined, 285927dbe93S猫头猫 }); 286927dbe93S猫头猫 // 无歌词 287927dbe93S猫头猫 return null; 288927dbe93S猫头猫 } 289927dbe93S猫头猫 // 获取关联歌词 2907a91f04fS猫头猫 const associatedMeta = MediaMeta.get(meta.associatedLrc) ?? {}; 2914060c00aS猫头猫 const result = await this.getLyric( 2927a91f04fS猫头猫 {...meta.associatedLrc, ...associatedMeta}, 2934060c00aS猫头猫 from ?? musicItem, 2944060c00aS猫头猫 ); 295927dbe93S猫头猫 if (result) { 296927dbe93S猫头猫 // 如果有关联歌词,就返回关联歌词,深度优先 297927dbe93S猫头猫 return result; 298927dbe93S猫头猫 } 299927dbe93S猫头猫 } 300927dbe93S猫头猫 const cache = Cache.get(musicItem); 301927dbe93S猫头猫 let rawLrc = meta?.rawLrc || musicItem.rawLrc || cache?.rawLrc; 302927dbe93S猫头猫 let lrcUrl = meta?.lrc || musicItem.lrc || cache?.lrc; 303927dbe93S猫头猫 // 如果存在文本 304927dbe93S猫头猫 if (rawLrc) { 305927dbe93S猫头猫 return { 306927dbe93S猫头猫 rawLrc, 307927dbe93S猫头猫 lrc: lrcUrl, 308927dbe93S猫头猫 }; 309927dbe93S猫头猫 } 310927dbe93S猫头猫 // 2.本地缓存 311927dbe93S猫头猫 const localLrc = 3120e4173cdS猫头猫 meta?.[internalSerializeKey]?.local?.localLrc || 3130e4173cdS猫头猫 cache?.[internalSerializeKey]?.local?.localLrc; 314927dbe93S猫头猫 if (localLrc && (await exists(localLrc))) { 315927dbe93S猫头猫 rawLrc = await readFile(localLrc, 'utf8'); 316927dbe93S猫头猫 return { 317927dbe93S猫头猫 rawLrc, 318927dbe93S猫头猫 lrc: lrcUrl, 319927dbe93S猫头猫 }; 320927dbe93S猫头猫 } 321927dbe93S猫头猫 // 3.优先使用url 322927dbe93S猫头猫 if (lrcUrl) { 323927dbe93S猫头猫 try { 324927dbe93S猫头猫 // 需要超时时间 axios timeout 但是没生效 3252a3194f5S猫头猫 rawLrc = (await axios.get(lrcUrl, {timeout: 1500})).data; 326927dbe93S猫头猫 return { 327927dbe93S猫头猫 rawLrc, 328927dbe93S猫头猫 lrc: lrcUrl, 329927dbe93S猫头猫 }; 330927dbe93S猫头猫 } catch { 331927dbe93S猫头猫 lrcUrl = undefined; 332927dbe93S猫头猫 } 333927dbe93S猫头猫 } 334927dbe93S猫头猫 // 4. 如果地址失效 335927dbe93S猫头猫 if (!lrcUrl) { 336927dbe93S猫头猫 // 插件获得url 337927dbe93S猫头猫 try { 3387a91f04fS猫头猫 let lrcSource; 3397a91f04fS猫头猫 if (from) { 3407a91f04fS猫头猫 lrcSource = await PluginManager.getByMedia( 3417a91f04fS猫头猫 musicItem, 3427a91f04fS猫头猫 )?.instance?.getLyric?.( 343927dbe93S猫头猫 resetMediaItem(musicItem, undefined, true), 344927dbe93S猫头猫 ); 3457a91f04fS猫头猫 } else { 3467a91f04fS猫头猫 lrcSource = await this.plugin.instance?.getLyric?.( 3477a91f04fS猫头猫 resetMediaItem(musicItem, undefined, true), 3487a91f04fS猫头猫 ); 3497a91f04fS猫头猫 } 3507a91f04fS猫头猫 351927dbe93S猫头猫 rawLrc = lrcSource?.rawLrc; 352927dbe93S猫头猫 lrcUrl = lrcSource?.lrc; 353927dbe93S猫头猫 } catch (e: any) { 354927dbe93S猫头猫 trace('插件获取歌词失败', e?.message, 'error'); 355927dbe93S猫头猫 } 356927dbe93S猫头猫 } 357927dbe93S猫头猫 // 5. 最后一次请求 358927dbe93S猫头猫 if (rawLrc || lrcUrl) { 359927dbe93S猫头猫 const filename = `${pathConst.lrcCachePath}${nanoid()}.lrc`; 360927dbe93S猫头猫 if (lrcUrl) { 361927dbe93S猫头猫 try { 3622a3194f5S猫头猫 rawLrc = (await axios.get(lrcUrl, {timeout: 1500})).data; 363927dbe93S猫头猫 } catch {} 364927dbe93S猫头猫 } 365927dbe93S猫头猫 if (rawLrc) { 366927dbe93S猫头猫 await writeFile(filename, rawLrc, 'utf8'); 367927dbe93S猫头猫 // 写入缓存 368927dbe93S猫头猫 Cache.update(musicItem, [ 3690e4173cdS猫头猫 [`${internalSerializeKey}.local.localLrc`, filename], 370927dbe93S猫头猫 ]); 371927dbe93S猫头猫 // 如果有meta 372927dbe93S猫头猫 if (meta) { 373927dbe93S猫头猫 MediaMeta.update(musicItem, [ 3740e4173cdS猫头猫 [`${internalSerializeKey}.local.localLrc`, filename], 375927dbe93S猫头猫 ]); 376927dbe93S猫头猫 } 377927dbe93S猫头猫 return { 378927dbe93S猫头猫 rawLrc, 379927dbe93S猫头猫 lrc: lrcUrl, 380927dbe93S猫头猫 }; 381927dbe93S猫头猫 } 382927dbe93S猫头猫 } 383927dbe93S猫头猫 384927dbe93S猫头猫 return null; 385927dbe93S猫头猫 } 386927dbe93S猫头猫 387927dbe93S猫头猫 /** 获取歌词文本 */ 388927dbe93S猫头猫 async getLyricText( 389927dbe93S猫头猫 musicItem: IMusic.IMusicItem, 390927dbe93S猫头猫 ): Promise<string | undefined> { 391927dbe93S猫头猫 return (await this.getLyric(musicItem))?.rawLrc; 392927dbe93S猫头猫 } 393927dbe93S猫头猫 394927dbe93S猫头猫 /** 获取专辑信息 */ 395927dbe93S猫头猫 async getAlbumInfo( 396927dbe93S猫头猫 albumItem: IAlbum.IAlbumItemBase, 397927dbe93S猫头猫 ): Promise<IAlbum.IAlbumItem | null> { 398927dbe93S猫头猫 if (!this.plugin.instance.getAlbumInfo) { 399927dbe93S猫头猫 return {...albumItem, musicList: []}; 400927dbe93S猫头猫 } 401927dbe93S猫头猫 try { 402927dbe93S猫头猫 const result = await this.plugin.instance.getAlbumInfo( 403927dbe93S猫头猫 resetMediaItem(albumItem, undefined, true), 404927dbe93S猫头猫 ); 4055276aef9S猫头猫 if (!result) { 4065276aef9S猫头猫 throw new Error(); 4075276aef9S猫头猫 } 408927dbe93S猫头猫 result?.musicList?.forEach(_ => { 409927dbe93S猫头猫 resetMediaItem(_, this.plugin.name); 410927dbe93S猫头猫 }); 4115276aef9S猫头猫 4125276aef9S猫头猫 return {...albumItem, ...result}; 4137d7e864fS猫头猫 } catch (e) { 414927dbe93S猫头猫 return {...albumItem, musicList: []}; 415927dbe93S猫头猫 } 416927dbe93S猫头猫 } 417927dbe93S猫头猫 418927dbe93S猫头猫 /** 查询作者信息 */ 419efb9da24S猫头猫 async getArtistWorks<T extends IArtist.ArtistMediaType>( 420927dbe93S猫头猫 artistItem: IArtist.IArtistItem, 421927dbe93S猫头猫 page: number, 422927dbe93S猫头猫 type: T, 423927dbe93S猫头猫 ): Promise<IPlugin.ISearchResult<T>> { 424efb9da24S猫头猫 if (!this.plugin.instance.getArtistWorks) { 425927dbe93S猫头猫 return { 426927dbe93S猫头猫 isEnd: true, 427927dbe93S猫头猫 data: [], 428927dbe93S猫头猫 }; 429927dbe93S猫头猫 } 430927dbe93S猫头猫 try { 431efb9da24S猫头猫 const result = await this.plugin.instance.getArtistWorks( 432927dbe93S猫头猫 artistItem, 433927dbe93S猫头猫 page, 434927dbe93S猫头猫 type, 435927dbe93S猫头猫 ); 436927dbe93S猫头猫 if (!result.data) { 437927dbe93S猫头猫 return { 438927dbe93S猫头猫 isEnd: true, 439927dbe93S猫头猫 data: [], 440927dbe93S猫头猫 }; 441927dbe93S猫头猫 } 442927dbe93S猫头猫 result.data?.forEach(_ => resetMediaItem(_, this.plugin.name)); 443927dbe93S猫头猫 return { 444927dbe93S猫头猫 isEnd: result.isEnd ?? true, 445927dbe93S猫头猫 data: result.data, 446927dbe93S猫头猫 }; 447927dbe93S猫头猫 } catch (e) { 448927dbe93S猫头猫 throw e; 449927dbe93S猫头猫 } 450927dbe93S猫头猫 } 45108380090S猫头猫 45208380090S猫头猫 /** 导入歌单 */ 45308380090S猫头猫 async importMusicSheet(urlLike: string): Promise<IMusic.IMusicItem[]> { 45408380090S猫头猫 try { 45508380090S猫头猫 const result = 45608380090S猫头猫 (await this.plugin.instance?.importMusicSheet?.(urlLike)) ?? []; 45708380090S猫头猫 result.forEach(_ => resetMediaItem(_, this.plugin.name)); 45808380090S猫头猫 return result; 4590e4173cdS猫头猫 } catch (e) { 4600e4173cdS猫头猫 console.log(e); 46108380090S猫头猫 return []; 46208380090S猫头猫 } 46308380090S猫头猫 } 4644d9d3c4cS猫头猫 /** 导入单曲 */ 4654d9d3c4cS猫头猫 async importMusicItem(urlLike: string): Promise<IMusic.IMusicItem | null> { 4664d9d3c4cS猫头猫 try { 4674d9d3c4cS猫头猫 const result = await this.plugin.instance?.importMusicItem?.( 4684d9d3c4cS猫头猫 urlLike, 4694d9d3c4cS猫头猫 ); 4704d9d3c4cS猫头猫 if (!result) { 4714d9d3c4cS猫头猫 throw new Error(); 4724d9d3c4cS猫头猫 } 4734d9d3c4cS猫头猫 resetMediaItem(result, this.plugin.name); 4744d9d3c4cS猫头猫 return result; 4754d9d3c4cS猫头猫 } catch { 4764d9d3c4cS猫头猫 return null; 4774d9d3c4cS猫头猫 } 4784d9d3c4cS猫头猫 } 479927dbe93S猫头猫} 4801a5528a0S猫头猫 481927dbe93S猫头猫let plugins: Array<Plugin> = []; 482927dbe93S猫头猫const pluginStateMapper = new StateMapper(() => plugins); 4830e4173cdS猫头猫const localFilePlugin = new Plugin( 4840e4173cdS猫头猫 `function (){ 4850e4173cdS猫头猫 return { 4860e4173cdS猫头猫 platform: '本地', 4870e4173cdS猫头猫 } 4880e4173cdS猫头猫}`, 4890e4173cdS猫头猫 '', 4900e4173cdS猫头猫); 491927dbe93S猫头猫 492927dbe93S猫头猫async function setup() { 493927dbe93S猫头猫 const _plugins: Array<Plugin> = []; 494927dbe93S猫头猫 try { 495927dbe93S猫头猫 // 加载插件 496927dbe93S猫头猫 const pluginsPaths = await readDir(pathConst.pluginPath); 497927dbe93S猫头猫 for (let i = 0; i < pluginsPaths.length; ++i) { 498927dbe93S猫头猫 const _pluginUrl = pluginsPaths[i]; 4991e263108S猫头猫 trace('初始化插件', _pluginUrl); 5001e263108S猫头猫 if ( 5011e263108S猫头猫 _pluginUrl.isFile() && 5021e263108S猫头猫 (_pluginUrl.name?.endsWith?.('.js') || 5031e263108S猫头猫 _pluginUrl.path?.endsWith?.('.js')) 5041e263108S猫头猫 ) { 505927dbe93S猫头猫 const funcCode = await readFile(_pluginUrl.path, 'utf8'); 506927dbe93S猫头猫 const plugin = new Plugin(funcCode, _pluginUrl.path); 5074060c00aS猫头猫 const _pluginIndex = _plugins.findIndex( 5084060c00aS猫头猫 p => p.hash === plugin.hash, 5094060c00aS猫头猫 ); 510927dbe93S猫头猫 if (_pluginIndex !== -1) { 511927dbe93S猫头猫 // 重复插件,直接忽略 512927dbe93S猫头猫 return; 513927dbe93S猫头猫 } 514927dbe93S猫头猫 plugin.hash !== '' && _plugins.push(plugin); 515927dbe93S猫头猫 } 516927dbe93S猫头猫 } 517927dbe93S猫头猫 518927dbe93S猫头猫 plugins = _plugins; 519927dbe93S猫头猫 pluginStateMapper.notify(); 520927dbe93S猫头猫 } catch (e: any) { 5214060c00aS猫头猫 ToastAndroid.show( 5224060c00aS猫头猫 `插件初始化失败:${e?.message ?? e}`, 5234060c00aS猫头猫 ToastAndroid.LONG, 5244060c00aS猫头猫 ); 5251a5528a0S猫头猫 errorLog('插件初始化失败', e?.message); 526927dbe93S猫头猫 throw e; 527927dbe93S猫头猫 } 528927dbe93S猫头猫} 529927dbe93S猫头猫 530927dbe93S猫头猫// 安装插件 531927dbe93S猫头猫async function installPlugin(pluginPath: string) { 532b50427a2S猫头猫 if (pluginPath.endsWith('.js')) { 533927dbe93S猫头猫 const funcCode = await readFile(pluginPath, 'utf8'); 534927dbe93S猫头猫 const plugin = new Plugin(funcCode, pluginPath); 535927dbe93S猫头猫 const _pluginIndex = plugins.findIndex(p => p.hash === plugin.hash); 536927dbe93S猫头猫 if (_pluginIndex !== -1) { 5374d9d3c4cS猫头猫 throw new Error('插件已安装'); 538927dbe93S猫头猫 } 539927dbe93S猫头猫 if (plugin.hash !== '') { 540927dbe93S猫头猫 const fn = nanoid(); 541927dbe93S猫头猫 const _pluginPath = `${pathConst.pluginPath}${fn}.js`; 542927dbe93S猫头猫 await copyFile(pluginPath, _pluginPath); 543927dbe93S猫头猫 plugin.path = _pluginPath; 544927dbe93S猫头猫 plugins = plugins.concat(plugin); 545927dbe93S猫头猫 pluginStateMapper.notify(); 5464d9d3c4cS猫头猫 return; 547927dbe93S猫头猫 } 5484d9d3c4cS猫头猫 throw new Error('插件无法解析'); 549927dbe93S猫头猫 } 5504d9d3c4cS猫头猫 throw new Error('插件不存在'); 551927dbe93S猫头猫} 552927dbe93S猫头猫 55358992c6bS猫头猫async function installPluginFromUrl(url: string) { 55458992c6bS猫头猫 try { 55558992c6bS猫头猫 const funcCode = (await axios.get(url)).data; 55658992c6bS猫头猫 if (funcCode) { 55758992c6bS猫头猫 const plugin = new Plugin(funcCode, ''); 55858992c6bS猫头猫 const _pluginIndex = plugins.findIndex(p => p.hash === plugin.hash); 55958992c6bS猫头猫 if (_pluginIndex !== -1) { 56058992c6bS猫头猫 throw new Error('插件已安装'); 56158992c6bS猫头猫 } 56225c1bd29S猫头猫 const oldVersionPlugin = plugins.find(p => p.name === plugin.name); 56325c1bd29S猫头猫 if (oldVersionPlugin) { 56425c1bd29S猫头猫 if ( 56525c1bd29S猫头猫 compare( 56625c1bd29S猫头猫 oldVersionPlugin.instance.version ?? '', 56725c1bd29S猫头猫 plugin.instance.version ?? '', 56825c1bd29S猫头猫 '>', 56925c1bd29S猫头猫 ) 57025c1bd29S猫头猫 ) { 57125c1bd29S猫头猫 throw new Error('已安装更新版本的插件'); 57225c1bd29S猫头猫 } 57325c1bd29S猫头猫 } 57425c1bd29S猫头猫 57558992c6bS猫头猫 if (plugin.hash !== '') { 57658992c6bS猫头猫 const fn = nanoid(); 57758992c6bS猫头猫 const _pluginPath = `${pathConst.pluginPath}${fn}.js`; 57858992c6bS猫头猫 await writeFile(_pluginPath, funcCode, 'utf8'); 57958992c6bS猫头猫 plugin.path = _pluginPath; 58058992c6bS猫头猫 plugins = plugins.concat(plugin); 58125c1bd29S猫头猫 if (oldVersionPlugin) { 58225c1bd29S猫头猫 plugins = plugins.filter( 58325c1bd29S猫头猫 _ => _.hash !== oldVersionPlugin.hash, 58425c1bd29S猫头猫 ); 58525c1bd29S猫头猫 try { 58625c1bd29S猫头猫 await unlink(oldVersionPlugin.path); 58725c1bd29S猫头猫 } catch {} 58825c1bd29S猫头猫 } 58958992c6bS猫头猫 pluginStateMapper.notify(); 59058992c6bS猫头猫 return; 59158992c6bS猫头猫 } 59258992c6bS猫头猫 throw new Error('插件无法解析'); 59358992c6bS猫头猫 } 59425c1bd29S猫头猫 } catch (e: any) { 59558992c6bS猫头猫 errorLog('URL安装插件失败', e); 59625c1bd29S猫头猫 throw new Error(e?.message ?? ''); 59758992c6bS猫头猫 } 59858992c6bS猫头猫} 59958992c6bS猫头猫 600927dbe93S猫头猫/** 卸载插件 */ 601927dbe93S猫头猫async function uninstallPlugin(hash: string) { 602927dbe93S猫头猫 const targetIndex = plugins.findIndex(_ => _.hash === hash); 603927dbe93S猫头猫 if (targetIndex !== -1) { 604927dbe93S猫头猫 try { 60524e5e74aS猫头猫 const pluginName = plugins[targetIndex].name; 606927dbe93S猫头猫 await unlink(plugins[targetIndex].path); 607927dbe93S猫头猫 plugins = plugins.filter(_ => _.hash !== hash); 608927dbe93S猫头猫 pluginStateMapper.notify(); 60924e5e74aS猫头猫 if (plugins.every(_ => _.name !== pluginName)) { 61024e5e74aS猫头猫 await MediaMeta.removePlugin(pluginName); 61124e5e74aS猫头猫 } 612927dbe93S猫头猫 } catch {} 613927dbe93S猫头猫 } 614927dbe93S猫头猫} 615927dbe93S猫头猫 61608882a77S猫头猫async function uninstallAllPlugins() { 61708882a77S猫头猫 await Promise.all( 61808882a77S猫头猫 plugins.map(async plugin => { 61908882a77S猫头猫 try { 62008882a77S猫头猫 const pluginName = plugin.name; 62108882a77S猫头猫 await unlink(plugin.path); 62208882a77S猫头猫 await MediaMeta.removePlugin(pluginName); 62308882a77S猫头猫 } catch (e) {} 62408882a77S猫头猫 }), 62508882a77S猫头猫 ); 62608882a77S猫头猫 plugins = []; 62708882a77S猫头猫 pluginStateMapper.notify(); 62808882a77S猫头猫} 62908882a77S猫头猫 63025c1bd29S猫头猫async function updatePlugin(plugin: Plugin) { 63125c1bd29S猫头猫 const updateUrl = plugin.instance.srcUrl; 63225c1bd29S猫头猫 if (!updateUrl) { 63325c1bd29S猫头猫 throw new Error('没有更新源'); 63425c1bd29S猫头猫 } 63525c1bd29S猫头猫 try { 63625c1bd29S猫头猫 await installPluginFromUrl(updateUrl); 63725c1bd29S猫头猫 } catch (e: any) { 63825c1bd29S猫头猫 if (e.message === '插件已安装') { 63925c1bd29S猫头猫 throw new Error('当前已是最新版本'); 64025c1bd29S猫头猫 } else { 64125c1bd29S猫头猫 throw e; 64225c1bd29S猫头猫 } 64325c1bd29S猫头猫 } 64425c1bd29S猫头猫} 64525c1bd29S猫头猫 646927dbe93S猫头猫function getByMedia(mediaItem: ICommon.IMediaBase) { 647927dbe93S猫头猫 return getByName(mediaItem.platform); 648927dbe93S猫头猫} 649927dbe93S猫头猫 650927dbe93S猫头猫function getByHash(hash: string) { 651927dbe93S猫头猫 return plugins.find(_ => _.hash === hash); 652927dbe93S猫头猫} 653927dbe93S猫头猫 654927dbe93S猫头猫function getByName(name: string) { 6550e4173cdS猫头猫 return name === '本地' 6560e4173cdS猫头猫 ? localFilePlugin 6570e4173cdS猫头猫 : plugins.find(_ => _.name === name); 658927dbe93S猫头猫} 659927dbe93S猫头猫 660927dbe93S猫头猫function getValidPlugins() { 661927dbe93S猫头猫 return plugins.filter(_ => _.state === 'enabled'); 662927dbe93S猫头猫} 663927dbe93S猫头猫 664efb9da24S猫头猫function getSearchablePlugins() { 665efb9da24S猫头猫 return plugins.filter(_ => _.state === 'enabled' && _.instance.search); 666efb9da24S猫头猫} 667efb9da24S猫头猫 668927dbe93S猫头猫const PluginManager = { 669927dbe93S猫头猫 setup, 670927dbe93S猫头猫 installPlugin, 67158992c6bS猫头猫 installPluginFromUrl, 67225c1bd29S猫头猫 updatePlugin, 673927dbe93S猫头猫 uninstallPlugin, 674927dbe93S猫头猫 getByMedia, 675927dbe93S猫头猫 getByHash, 676927dbe93S猫头猫 getByName, 677927dbe93S猫头猫 getValidPlugins, 678efb9da24S猫头猫 getSearchablePlugins, 6795276aef9S猫头猫 usePlugins: pluginStateMapper.useMappedState, 68008882a77S猫头猫 uninstallAllPlugins, 6815276aef9S猫头猫}; 682927dbe93S猫头猫 683927dbe93S猫头猫export default PluginManager; 684