2025-08-06 14:15:15 +08:00
|
|
|
|
const { app, BrowserWindow, ipcMain } = require("electron");
|
|
|
|
|
const path = require("path");
|
2025-08-07 01:15:18 +08:00
|
|
|
|
const { spawn } = require('child_process')
|
|
|
|
|
|
|
|
|
|
// 音效播放器
|
|
|
|
|
function playAudioFile(filePath) {
|
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
|
spawn('cmd', ['/c', `start "" "${filePath}"`])
|
|
|
|
|
} else if (process.platform === 'darwin') {
|
|
|
|
|
spawn('afplay', [filePath])
|
|
|
|
|
} else {
|
|
|
|
|
spawn('aplay', [filePath])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ipcMain.on('play-sound', (_, soundFile) => {
|
2025-08-08 17:35:33 +08:00
|
|
|
|
let soundPath;
|
|
|
|
|
|
|
|
|
|
// 判断是开发环境还是生产环境
|
|
|
|
|
if (process.env.NODE_ENV === 'development') {
|
2025-08-08 17:42:41 +08:00
|
|
|
|
// 在开发模式下,直接指向 renderer/public 里的文件
|
|
|
|
|
soundPath = path.join(__dirname, '../renderer/public/assets/sounds', soundFile);
|
2025-08-08 17:35:33 +08:00
|
|
|
|
} else {
|
2025-08-08 17:42:41 +08:00
|
|
|
|
// 在生产模式下,Vite 会把 public 里的文件复制到 dist 文件夹
|
2025-08-08 17:35:33 +08:00
|
|
|
|
soundPath = path.join(__dirname, '../renderer/dist/assets/sounds', soundFile);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log('Main process trying to play sound at path:', soundPath);
|
|
|
|
|
|
2025-08-07 01:15:18 +08:00
|
|
|
|
if (require('fs').existsSync(soundPath)) {
|
2025-08-08 17:35:33 +08:00
|
|
|
|
playAudioFile(soundPath);
|
2025-08-07 01:15:18 +08:00
|
|
|
|
} else {
|
2025-08-08 17:35:33 +08:00
|
|
|
|
console.error('Sound file not found:', soundPath);
|
2025-08-07 01:15:18 +08:00
|
|
|
|
}
|
2025-08-08 17:35:33 +08:00
|
|
|
|
});
|
2025-08-05 23:30:00 +08:00
|
|
|
|
|
2025-08-08 20:06:52 +08:00
|
|
|
|
ipcMain.handle('get-sound-path', (_, soundFile) => {
|
|
|
|
|
let soundPath;
|
|
|
|
|
|
|
|
|
|
if (process.env.NODE_ENV === 'development') {
|
|
|
|
|
soundPath = path.join(__dirname, '../renderer/public/assets/sounds', soundFile);
|
|
|
|
|
} else {
|
|
|
|
|
soundPath = path.join(__dirname, '../renderer/dist/assets/sounds', soundFile);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (require('fs').existsSync(soundPath)) {
|
|
|
|
|
// 返回一个可供 web 环境使用的 file 协议 URL
|
|
|
|
|
return `file://${soundPath}`;
|
|
|
|
|
} else {
|
|
|
|
|
console.error('Sound file not found:', soundPath);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2025-08-06 14:15:15 +08:00
|
|
|
|
let mainWindow;
|
2025-08-05 23:30:00 +08:00
|
|
|
|
|
|
|
|
|
function createWindow() {
|
|
|
|
|
mainWindow = new BrowserWindow({
|
|
|
|
|
width: 300,
|
|
|
|
|
height: 300,
|
2025-08-09 23:32:55 +08:00
|
|
|
|
transparent: true, // 开启透明窗口
|
|
|
|
|
frame: false, // 无边框窗口
|
|
|
|
|
resizable: false, // 禁止调整大小
|
2025-08-05 23:30:00 +08:00
|
|
|
|
webPreferences: {
|
2025-08-06 14:15:15 +08:00
|
|
|
|
preload: path.join(__dirname, "preload.js"),
|
|
|
|
|
contextIsolation: true,
|
2025-08-09 23:32:55 +08:00
|
|
|
|
webSecurity: false,
|
2025-08-06 14:15:15 +08:00
|
|
|
|
},
|
|
|
|
|
});
|
2025-08-05 23:30:00 +08:00
|
|
|
|
|
2025-08-06 14:15:15 +08:00
|
|
|
|
if (process.env.NODE_ENV === "development") {
|
|
|
|
|
mainWindow.loadURL("http://localhost:5173");
|
2025-08-06 07:44:19 +08:00
|
|
|
|
} else {
|
2025-08-06 14:15:15 +08:00
|
|
|
|
mainWindow.loadFile(path.join(__dirname, "../renderer/dist/index.html"));
|
2025-08-06 07:44:19 +08:00
|
|
|
|
}
|
2025-08-05 23:30:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-08-06 14:15:15 +08:00
|
|
|
|
app.whenReady().then(createWindow);
|