Skip to content

Commit 4699170

Browse files
committed
Füge Terminal-Funktionalität hinzu und refaktoriere den Code für den Minecraft Launcher
1 parent 3ca6dab commit 4699170

File tree

9 files changed

+115
-51
lines changed

9 files changed

+115
-51
lines changed

main.js

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,66 @@
11
import electron from 'electron';
22
import path from 'path';
33
import { fileURLToPath } from 'url';
4+
import { exec } from 'child_process';
5+
import os from 'os';
46

57
const __filename = fileURLToPath(import.meta.url);
6-
const { app, BrowserWindow } = electron;
8+
const { app, BrowserWindow, ipcMain } = electron;
79
const __dirname = path.dirname(__filename);
810

911
let mainWindow;
12+
function openTerminalWindow(command) {
13+
const termWin = new BrowserWindow({
14+
width: 800,
15+
height: 600,
16+
webPreferences: {
17+
preload: path.join(__dirname, 'terminalpreload.cjs'),
18+
contextIsolation: true
19+
}
20+
});
1021

22+
termWin.loadFile('terminal.html');
23+
24+
ipcMain.handle('load-terminal', async () => {
25+
const cmd = exec(command, async (error, stdout, stderr) => {
26+
if (error) {
27+
console.error(`Error executing command: ${error.message}`);
28+
}
29+
if (stderr) {
30+
console.error(`stderr: ${stderr}`);
31+
}
32+
if (stdout) {
33+
ipcMain.emit('terminal.incomingData', null, stdout);
34+
}
35+
}
36+
);
37+
}
38+
);
39+
}
1140
const createWindow = () => {
12-
mainWindow = new BrowserWindow({
13-
width: 800,
14-
height: 600,
15-
webPreferences: {
16-
preload: path.join(__dirname, 'preload.js'), // Optional for secure communication
17-
nodeIntegration: true, // Enable Node.js APIs
18-
},
19-
});
20-
mainWindow.loadFile('index.html');
41+
ipcMain.handle('launch-minecraft', async () => {
42+
openTerminalWindow('node index.js');
43+
});
44+
mainWindow = new BrowserWindow({
45+
width: 800,
46+
height: 600,
47+
webPreferences: {
48+
preload: path.join(__dirname, 'preload.cjs')
49+
},
50+
});
51+
mainWindow.loadFile('index.html');
2152
};
2253

23-
console.log(app)
2454
app.on('ready', createWindow);
2555

2656
app.on('window-all-closed', () => {
27-
if (process.platform !== 'darwin') {
28-
app.quit();
29-
}
57+
if (process.platform !== 'darwin') {
58+
app.quit();
59+
}
3060
});
3161

3262
app.on('activate', () => {
33-
if (BrowserWindow.getAllWindows().length === 0) {
34-
createWindow();
35-
}
63+
if (BrowserWindow.getAllWindows().length === 0) {
64+
createWindow();
65+
}
3666
});

package-lock.json

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"test": "echo \"Error: no test specified\" && exit 1"
1212
},
1313
"dependencies": {
14+
"@xterm/xterm": "^5.5.0",
1415
"adm-zip": "^0.5.16",
1516
"axios": "^1.9.0",
1617
"cli-progress": "^3.12.0",

preload.cjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const { contextBridge } = require('electron');
2+
const { ipcRenderer } = require('electron');
3+
4+
contextBridge.exposeInMainWorld('api', {
5+
launchMinecraft: () => {
6+
ipcRenderer.invoke('launch-minecraft');
7+
},
8+
});

preload.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

renderer.js

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,3 @@
1-
import { exec } from 'child_process';
2-
3-
document.getElementById('launch-btn').addEventListener('click', () => {
4-
exec('node index.js', (error, stdout, stderr) => {
5-
if (error) {
6-
console.error(`Error: ${error.message}`);
7-
alert('Failed to launch Minecraft!');
8-
return;
9-
}
10-
if (stderr) {
11-
console.error(`Stderr: ${stderr}`);
12-
}
13-
console.log(`Stdout: ${stdout}`);
14-
alert('Minecraft launched successfully!');
15-
});
16-
});
1+
document.getElementById('launch-btn').addEventListener('click', async () => {
2+
api.launchMinecraft()
3+
})

terminal.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Electron Terminal</title>
8+
<link rel="stylesheet" href="node_modules/@xterm/xterm/css/xterm.css" />
9+
</head>
10+
<body>
11+
<div id="terminal" style="width:100%; height:100%;" class="xterm"></div>
12+
<script src="terminal_renderer.js"></script>
13+
</body>
14+
</html>

terminal_renderer.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
terminalAPI.sendCommand();
2+
3+
const terminal = document.getElementById('terminal');
4+
terminalAPI.onIncomingData((data) => {
5+
console.log(data)
6+
const pre = document.createElement('pre');
7+
pre.textContent = data;
8+
terminal.appendChild(pre);
9+
terminal.scrollTop = terminal.scrollHeight;
10+
}
11+
);
12+
terminalAPI.onClose(() => {
13+
const pre = document.createElement('pre');
14+
pre.textContent = 'Terminal closed';
15+
terminal.appendChild(pre);
16+
terminal.scrollTop = terminal.scrollHeight;
17+
}
18+
);

terminalpreload.cjs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const { contextBridge, ipcRenderer } = require('electron');
2+
3+
contextBridge.exposeInMainWorld('terminalAPI', {
4+
sendCommand: () => {
5+
ipcRenderer.invoke('load-terminal');
6+
},
7+
onIncomingData: (callback) => {
8+
ipcRenderer.on('terminal.incomingData', (event, data) => {
9+
callback(data);
10+
});
11+
},
12+
onClose: (callback) => {
13+
ipcRenderer.on('terminal.close', (event) => {
14+
callback();
15+
});
16+
}
17+
});

0 commit comments

Comments
 (0)