2024-08-26 16:44:51 +08:00
|
|
|
// src/electron/main.ts
|
|
|
|
// 需要先載入
|
|
|
|
import "module-alias/register";
|
|
|
|
|
|
|
|
// // 必載入
|
|
|
|
// import "../Utils/catan.ts";
|
|
|
|
|
2024-08-25 22:34:21 +08:00
|
|
|
import dotenv from 'dotenv';
|
2024-08-24 23:29:04 +08:00
|
|
|
import { app, BrowserWindow, ipcMain } from 'electron';
|
2024-08-30 15:37:33 +08:00
|
|
|
import fs from 'fs';
|
2024-08-24 23:29:04 +08:00
|
|
|
import * as path from 'path';
|
|
|
|
import { WebSocketServer } from 'ws';
|
2024-08-26 17:17:41 +08:00
|
|
|
import { BaseEnumerator } from "../CatanEngine/CoroutineV2/Core/BaseEnumerator";
|
|
|
|
import { NetConnector } from "../script/Engine/CatanEngine/NetManagerV2/NetConnector";
|
2024-08-24 23:29:04 +08:00
|
|
|
|
2024-08-26 16:44:51 +08:00
|
|
|
onload();
|
|
|
|
|
|
|
|
function onload() {
|
|
|
|
BaseEnumerator.Init();
|
2024-08-30 15:37:33 +08:00
|
|
|
setLog();
|
2024-08-26 16:44:51 +08:00
|
|
|
// Load environment variables from .env file
|
2024-08-30 15:37:33 +08:00
|
|
|
const filePath = ".env.dev";
|
|
|
|
// 检查文件是否存在
|
|
|
|
if (fs.existsSync(filePath)) {
|
|
|
|
dotenv.config({ path: `.env.dev` });
|
|
|
|
} else {
|
|
|
|
dotenv.config({ path: path.resolve(__dirname, `../../.env.prod`) });
|
|
|
|
}
|
2024-08-26 16:44:51 +08:00
|
|
|
}
|
2024-08-25 22:34:21 +08:00
|
|
|
|
2024-08-24 23:29:04 +08:00
|
|
|
let server: WebSocketServer | null = null;
|
2024-08-25 22:34:21 +08:00
|
|
|
let port: number = process.env.PORT ? parseInt(process.env.PORT, 10) : 8080; // 默认端口为 8080
|
2024-08-24 23:29:04 +08:00
|
|
|
|
|
|
|
function createWindow() {
|
|
|
|
const win = new BrowserWindow({
|
|
|
|
width: 800,
|
|
|
|
height: 600,
|
|
|
|
webPreferences: {
|
|
|
|
preload: path.join(__dirname, 'preload.js'),
|
|
|
|
contextIsolation: true,
|
|
|
|
nodeIntegration: false,
|
|
|
|
// contextIsolation: false, // 使渲染進程能夠使用 Node.js
|
|
|
|
// nodeIntegration: true, // 使渲染進程可以使用 Node.js 模組
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
win.loadFile(path.join(__dirname, 'index.html'));
|
2024-08-25 22:34:21 +08:00
|
|
|
// win.webContents.openDevTools();
|
2024-08-24 23:29:04 +08:00
|
|
|
|
|
|
|
return win;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mainWindow: BrowserWindow | null = null;
|
|
|
|
|
|
|
|
app.whenReady().then(() => {
|
|
|
|
mainWindow = createWindow();
|
|
|
|
|
|
|
|
app.on('activate', () => {
|
|
|
|
if (BrowserWindow.getAllWindows().length === 0) {
|
|
|
|
createWindow();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
app.on('window-all-closed', () => {
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
app.quit();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// 启动 WebSocket 服务器
|
2024-08-30 15:37:33 +08:00
|
|
|
ipcMain.on('start-websocket', (event: Electron.IpcMainEvent, portNumber: number) => {
|
2024-08-24 23:29:04 +08:00
|
|
|
if (server) {
|
2024-08-30 15:37:33 +08:00
|
|
|
closeServer(event);
|
2024-08-24 23:29:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
port = portNumber || 8080;
|
|
|
|
|
2024-08-30 15:37:33 +08:00
|
|
|
server = new WebSocketServer({ port });
|
2024-08-24 23:29:04 +08:00
|
|
|
|
2024-08-26 16:44:51 +08:00
|
|
|
server.on('connection', NetConnector.OnWebSocketConnection);
|
2024-08-24 23:29:04 +08:00
|
|
|
|
2024-08-30 15:37:33 +08:00
|
|
|
event.reply('websocket-status', `WebSocket server started on port ${port}`);
|
|
|
|
});
|
2024-08-24 23:29:04 +08:00
|
|
|
|
2024-08-30 15:37:33 +08:00
|
|
|
// 关闭 WebSocket 服务器
|
|
|
|
ipcMain.on('stop-websocket', (event: Electron.IpcMainEvent) => {
|
|
|
|
closeServer(event);
|
|
|
|
});
|
2024-08-26 16:44:51 +08:00
|
|
|
|
2024-08-30 15:37:33 +08:00
|
|
|
// 打开开发者工具
|
|
|
|
ipcMain.on('json-reload', () => {
|
|
|
|
NetConnector.clients.forEach(client => {
|
|
|
|
client.jsons = {};
|
|
|
|
});
|
|
|
|
console.log(`重載成功`);
|
|
|
|
});
|
2024-08-24 23:29:04 +08:00
|
|
|
|
2024-08-30 15:37:33 +08:00
|
|
|
// 打开开发者工具
|
|
|
|
ipcMain.on('open-devtools', () => {
|
|
|
|
mainWindow.webContents.openDevTools();
|
2024-08-24 23:29:04 +08:00
|
|
|
});
|
|
|
|
|
2024-08-30 15:37:33 +08:00
|
|
|
function closeServer(event: Electron.IpcMainEvent) {
|
2024-08-24 23:29:04 +08:00
|
|
|
if (server) {
|
|
|
|
server.clients.forEach(client => {
|
|
|
|
if (client.readyState === 1) {
|
|
|
|
client.close();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
server.close((err) => {
|
|
|
|
if (err) {
|
|
|
|
event.reply('websocket-status', 'Failed to stop WebSocket server');
|
|
|
|
}
|
|
|
|
});
|
2024-08-30 15:37:33 +08:00
|
|
|
event.reply('websocket-status', 'WebSocket server stopped');
|
2024-08-24 23:29:04 +08:00
|
|
|
} else {
|
|
|
|
event.reply('websocket-status', 'No WebSocket server is running');
|
|
|
|
}
|
2024-08-30 15:37:33 +08:00
|
|
|
}
|
2024-08-26 16:44:51 +08:00
|
|
|
|
|
|
|
// Create a function to send log messages to the renderer
|
|
|
|
function sendLogToRenderer(window: BrowserWindow, message: string, color: string) {
|
|
|
|
window.webContents.send('log-message', [message, color]);
|
|
|
|
}
|
|
|
|
|
2024-08-30 15:37:33 +08:00
|
|
|
function setLog() {
|
|
|
|
// 重寫 console.log 方法以便將訊息發送到渲染進程
|
|
|
|
const originalConsoleLog = console.log;
|
|
|
|
console.log = (...args: any[]) => {
|
|
|
|
const message = args.join(' ');
|
|
|
|
originalConsoleLog(message); // 保留原始行為
|
|
|
|
if (BrowserWindow.getAllWindows().length > 0) {
|
|
|
|
sendLogToRenderer(BrowserWindow.getAllWindows()[0], message, "green"); // 發送訊息到渲染進程
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// 重写 console.warn
|
|
|
|
const originalConsoleWarn = console.warn;
|
|
|
|
console.warn = (...args: any[]) => {
|
|
|
|
const message = args.join(' ');
|
|
|
|
originalConsoleWarn(message);
|
|
|
|
if (BrowserWindow.getAllWindows().length > 0) {
|
|
|
|
sendLogToRenderer(BrowserWindow.getAllWindows()[0], message, "yellow"); // 發送訊息到渲染進程
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// 重写 console.error
|
|
|
|
const originalConsoleError = console.error;
|
|
|
|
console.error = (...args: any[]) => {
|
|
|
|
const message = args.join(' ');
|
|
|
|
originalConsoleError(message);
|
|
|
|
if (BrowserWindow.getAllWindows().length > 0) {
|
|
|
|
sendLogToRenderer(BrowserWindow.getAllWindows()[0], message, "red"); // 發送訊息到渲染進程
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|