mirror of
https://github.com/lqtmcstudio/QZMusic_PC.git
synced 2026-06-20 23:35:06 +08:00
fix: 优化&功能
- 播放核心异常提示 - 支持更改缓存位置
This commit is contained in:
@@ -2,7 +2,7 @@ import { app } from 'electron'
|
||||
import path from 'path'
|
||||
import fs from 'fs'
|
||||
import { createRequire } from 'node:module'
|
||||
import { MessagePlugin } from "tdesign-vue-next";
|
||||
|
||||
|
||||
const require = createRequire(import.meta.url)
|
||||
|
||||
@@ -106,8 +106,124 @@ export class PluginSystem {
|
||||
return result
|
||||
} catch (e: any) {
|
||||
console.log(e)
|
||||
MessagePlugin.error(e).then()
|
||||
console.error(e)
|
||||
return {}
|
||||
}
|
||||
}
|
||||
|
||||
static getAllPlugins(): any[] {
|
||||
try {
|
||||
const pluginsPath = path.join(app.getPath('userData'), 'plugins')
|
||||
if (!fs.existsSync(pluginsPath)) return []
|
||||
|
||||
return fs.readdirSync(pluginsPath).map(dir => {
|
||||
const pluginPath = path.join(pluginsPath, dir, 'index.js')
|
||||
if (fs.existsSync(pluginPath)) {
|
||||
try {
|
||||
// Clear cache to ensure fresh load
|
||||
delete require.cache[require.resolve(pluginPath)]
|
||||
const pluginModule = require(pluginPath)
|
||||
if (pluginModule.pluginInfo) {
|
||||
return {
|
||||
...pluginModule.pluginInfo.info,
|
||||
quality: pluginModule.pluginInfo.quality,
|
||||
_path: dir
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback for current simple plugins if they don't have metadata
|
||||
return {
|
||||
id: dir,
|
||||
name: dir,
|
||||
description: 'No description',
|
||||
version: '0.0.0',
|
||||
_path: dir
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(`[PluginSystem] Failed to load plugin ${dir}:`, e)
|
||||
return null
|
||||
}
|
||||
}
|
||||
return null
|
||||
}).filter(p => p !== null)
|
||||
} catch (e) {
|
||||
console.error('[PluginSystem] getAllPlugins failed:', e)
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
static async installPlugin(filePath: string): Promise<{ success: boolean; message: string }> {
|
||||
try {
|
||||
// Require the file to get plugin info
|
||||
// Notes: We might need to copy it to a temp location if 'require' caches by path strictness,
|
||||
// but for now let's try requiring the source.
|
||||
// If the user selects a file, it's likely outside our project.
|
||||
// Node's require might need valid path.
|
||||
|
||||
// However, we can also just read the file content and do a regex check if we want to be safe,
|
||||
// but the user's plugin example is a JS object.
|
||||
// Let's copy it to a temporary location in userData to rely on 'require'
|
||||
|
||||
const tempId = `temp_${Date.now()}`
|
||||
const tempDir = path.join(app.getPath('userData'), 'temp_plugins', tempId)
|
||||
const tempFile = path.join(tempDir, 'index.js')
|
||||
|
||||
if (!fs.existsSync(tempDir)) {
|
||||
fs.mkdirSync(tempDir, { recursive: true })
|
||||
}
|
||||
|
||||
fs.copyFileSync(filePath, tempFile)
|
||||
|
||||
// Clear cache just in case
|
||||
delete require.cache[require.resolve(tempFile)]
|
||||
const pluginModule = require(tempFile)
|
||||
|
||||
let id = ''
|
||||
if (pluginModule.pluginInfo?.info?.id) {
|
||||
id = pluginModule.pluginInfo.info.id
|
||||
} else if (pluginModule.info?.id) {
|
||||
// Legacy or direct format support
|
||||
id = pluginModule.info.id
|
||||
}
|
||||
|
||||
if (!id) {
|
||||
// Cleanup
|
||||
fs.rmSync(tempDir, { recursive: true, force: true })
|
||||
console.error('[PluginSystem] No plugin ID found in file')
|
||||
return { success: false, message: '插件文件中未找到ID' }
|
||||
}
|
||||
|
||||
// Install to real location
|
||||
const targetDir = path.join(app.getPath('userData'), 'plugins', id)
|
||||
if (fs.existsSync(targetDir)) {
|
||||
fs.rmSync(tempDir, { recursive: true, force: true })
|
||||
return { success: false, message: `插件 ${id} 已存在` }
|
||||
}
|
||||
fs.mkdirSync(targetDir, { recursive: true })
|
||||
|
||||
fs.copyFileSync(filePath, path.join(targetDir, 'index.js'))
|
||||
|
||||
// Cleanup temp
|
||||
fs.rmSync(tempDir, { recursive: true, force: true })
|
||||
|
||||
return { success: true, message: '安装成功' }
|
||||
} catch (e: any) {
|
||||
console.error('[PluginSystem] Install failed:', e)
|
||||
return { success: false, message: e.message || '安装失败' }
|
||||
}
|
||||
}
|
||||
|
||||
static uninstallPlugin(id: string): boolean {
|
||||
try {
|
||||
const pluginPath = path.join(app.getPath('userData'), 'plugins', id)
|
||||
if (fs.existsSync(pluginPath)) {
|
||||
fs.rmSync(pluginPath, { recursive: true, force: true })
|
||||
return true
|
||||
}
|
||||
return false
|
||||
} catch (e) {
|
||||
console.error(`[PluginSystem] Failed to uninstall plugin ${id}:`, e)
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user