-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
215 lines (184 loc) · 6.87 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
const { app, BrowserWindow, ipcMain, dialog } = require('electron');
const path = require('path');
const fs = require('fs');
const { exec } = require('child_process');
const RPC = require('discord-rpc');
let executablePathEs = null;
let executablePathEn = null;
let originalContent = null;
let mainWindow = null;
const clientId = '1144080613675106315';
RPC.register(clientId);
const rpc = new RPC.Client({ transport: 'ipc' });
rpc.on('ready', () => {
console.log('Discord Rich Presence is ready.');
setMainActivity();
});
rpc.login({ clientId }).catch(console.error);
function setMainActivity() {
rpc.setActivity({
details: 'Using TeachFeel',
state: 'Idle',
startTimestamp: new Date(),
largeImageKey: 'https://github.com/SpanishHgames/teaching-feeling---Launcher/blob/main/.github/img/3f392b5f13227650fd4b39a783ca2c4e.jpg?raw=true',
largeImageText: 'TeachFeel App',
smallImageKey: 'small-image-key',
smallImageText: 'Version 1.0.0',
});
}
function createWindow() {
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: true,
enableRemoteModule: false,
nodeIntegration: false,
},
});
mainWindow.loadFile(path.join(__dirname, 'src', 'html', 'main.html'));
mainWindow.setMenu(null);
loadConfigs();
}
function loadConfigs() {
const configPathEs = path.join(app.getPath('userData'), 'configEs.json');
const configPathEn = path.join(app.getPath('userData'), 'configEn.json');
if (fs.existsSync(configPathEs)) {
const fileDataEs = fs.readFileSync(configPathEs);
const configEs = JSON.parse(fileDataEs);
executablePathEs = configEs.executablePath;
}
if (fs.existsSync(configPathEn)) {
const fileDataEn = fs.readFileSync(configPathEn);
const configEn = JSON.parse(fileDataEn);
executablePathEn = configEn.executablePath;
}
}
app.whenReady().then(createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
ipcMain.handle('open-file-dialog-es', async () => {
const result = await dialog.showOpenDialog({
properties: ['openFile'],
filters: [{ name: 'Ejecutables', extensions: ['exe'] }],
});
if (result.canceled) {
return null;
} else {
executablePathEs = result.filePaths[0];
return executablePathEs;
}
});
ipcMain.handle('open-file-dialog-en', async () => {
const result = await dialog.showOpenDialog({
properties: ['openFile'],
filters: [{ name: 'Executables', extensions: ['exe'] }],
});
if (result.canceled) {
return null;
} else {
executablePathEn = result.filePaths[0];
return executablePathEn;
}
});
ipcMain.handle('save-config-es', async () => {
if (executablePathEs) {
const configPathEs = path.join(app.getPath('userData'), 'configEs.json');
const fileDataEs = { executablePath: executablePathEs };
fs.writeFileSync(configPathEs, JSON.stringify(fileDataEs, null, 2));
return 'Configuración guardada exitosamente para la aplicación en español.';
}
return 'No se ha seleccionado ningún ejecutable para la aplicación en español.';
});
ipcMain.handle('save-config-en', async () => {
if (executablePathEn) {
const configPathEn = path.join(app.getPath('userData'), 'configEn.json');
const fileDataEn = { executablePath: executablePathEn };
fs.writeFileSync(configPathEn, JSON.stringify(fileDataEn, null, 2));
return 'Configuration saved successfully for the application in English.';
}
return 'No executable selected for the application in English.';
});
ipcMain.handle('start-game-es', async () => {
if (!executablePathEs) {
dialog.showMessageBoxSync(mainWindow, {
type: 'error',
title: 'Error',
message: 'No se ha seleccionado ningún ejecutable para la aplicación en español.'
});
return 'No se ha seleccionado ningún ejecutable para la aplicación en español.';
}
return startGame(executablePathEs, 'español');
});
ipcMain.handle('start-game-en', async () => {
if (!executablePathEn) {
dialog.showMessageBoxSync(mainWindow, {
type: 'error',
title: 'Error',
message: 'No executable selected for the application in English.'
});
return 'No executable selected for the application in English.';
}
return startGame(executablePathEn, 'English');
});
function startGame(executablePath, language) {
return new Promise((resolve, reject) => {
if (executablePath) {
const indexPath = path.join(path.dirname(executablePath), 'resources', 'app', 'index.html');
if (fs.existsSync(indexPath)) {
originalContent = fs.readFileSync(indexPath, 'utf8');
const modifiedContent = originalContent.replace('</body>', `
<div style="position: fixed; top: 19px; left: 19px;"><img src="https://github.com/SpanishHgames/teaching-feeling---Launcher/blob/main/src/img/TeachFeel.png?raw=true" width="200px" height="auto"></div>
</body>
`);
fs.writeFileSync(indexPath, modifiedContent);
mainWindow.hide();
exec(`"${executablePath}"`, (error, stdout, stderr) => {
mainWindow.show();
fs.writeFileSync(indexPath, originalContent);
if (error) {
console.error(`Error al ejecutar el archivo: ${error.message}`);
resolve(`Error al iniciar el juego en ${language}.`);
} else if (stderr) {
console.error(`Error: ${stderr}`);
resolve(`Error when starting the game in ${language}.`);
} else {
console.log(`Salida: ${stdout}`);
resolve(`Game successfully launched in ${language}.`);
}
// Reset Discord Rich Presence to main activity
setMainActivity();
});
// Update Discord Rich Presence
rpc.setActivity({
details: `Playing TeachFeel in ${language}`,
state: 'In-Game',
startTimestamp: new Date(),
largeImageKey: 'https://github.com/SpanishHgames/teaching-feeling---Launcher/blob/main/.github/img/3f392b5f13227650fd4b39a783ca2c4e.jpg?raw=true',
largeImageText: 'TeachFeel App',
smallImageKey: 'small-image-key',
smallImageText: `Version 1.0.0 (${language})`,
});
} else {
resolve(`Archivo index no encontrado para la aplicación en ${language}.`);
}
} else {
resolve(`Ruta del ejecutable no establecida para la aplicación en ${language}.`);
}
});
}
ipcMain.handle('get-executable-path-es', () => {
return executablePathEs;
});
ipcMain.handle('get-executable-path-en', () => {
return executablePathEn;
});