-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
243 lines (205 loc) · 8.18 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
const { app, BrowserWindow, ipcMain, session, Tray, nativeImage, shell, Menu, nativeTheme } = require('electron');
const { autoUpdater } = require('electron-updater');
const log = require('electron-log');
const path = require('path');
const fs = require('fs');
const https = require('https');
const os = require('os');
const AppMenu = require('./menu');
const RightMenuapp = require('./right-menu-config');
let mainWindow; // Variável para armazenar a janela principal
let tray = null; // Variável para armazenar o Tray
let rightMenu = Menu.buildFromTemplate(RightMenuapp);
// Função para carregar as traduções
function loadTranslations(language) {
const filePath = path.join(__dirname, `locales/${language}.json`);
return JSON.parse(fs.readFileSync(filePath, 'utf8'));
}
const currentLanguage = 'pt'; // Mude para 'en' se preferir inglês
const translations = loadTranslations(currentLanguage);
ipcMain.on('set-language', (event, language) => {
const translations = loadTranslations(language);
mainWindow.webContents.send('language-updated', translations);
});
// Função para criar a janela principal
function createWindow() {
mainWindow = new BrowserWindow({
width: 1200,
height: 800,
icon: path.join(__dirname, 'build/icon.png'),
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: true,
enableRemoteModule: false,
nodeIntegration: false,
sandbox: true
}
});
mainWindow.maximize();
mainWindow.setResizable(true);
mainWindow.setMaximizable(true);
mainWindow.setMinimizable(true);
//mainWindow.webContents.openDevTools();
new AppMenu(mainWindow);
mainWindow.loadFile(path.join(__dirname, 'frontend', 'login.html'));
// Load Right click menu
mainWindow.webContents.on('context-menu', e => {
rightMenu.popup(mainWindow);
});
const wc = mainWindow.webContents;
// Prevenir navegação em links externos e abrir no navegador padrão
wc.on('will-navigate', (event, url) => {
const parsedUrl = new URL(url);
if (parsedUrl.protocol === 'http:' || parsedUrl.protocol === 'https:') {
event.preventDefault();
shell.openExternal(url); // Abre o link externo no navegador
}
});
// Verificar atualizações assim que a janela for criada
autoUpdater.checkForUpdates();
}
// Função para criar o Tray
function createTray() {
try {
const iconPath = process.env.NODE_ENV === 'development'
? path.join(__dirname, 'build/icon.png')
: path.join(process.resourcesPath, 'build/icon.png');
if (!fs.existsSync(iconPath)) {
const fallbackIconPath = path.join(__dirname, 'build', 'icon.png');
if (!fs.existsSync(fallbackIconPath)) {
throw new Error(`Neither primary nor fallback icons found.`);
}
tray = new Tray(fallbackIconPath);
} else {
tray = new Tray(iconPath);
}
const trayMenu = Menu.buildFromTemplate([
{ label: 'Mostrar Aplicativo', click: () => { mainWindow.show(); } },
{ type: 'separator' },
{
label: 'Redes Sociais',
submenu: [
{ label: 'YouTube', click: () => shell.openExternal('https://www.youtube.com/@renildomarcio') },
{ label: 'GitHub', click: () => shell.openExternal('https://github.com/psycodeliccircus') }
]
},
{ type: 'separator' },
{ label: 'Sair', click: () => { app.quit(); } }
]);
tray.setToolTip('Launcher Mods');
tray.setContextMenu(trayMenu);
tray.on('click', () => {
mainWindow.isVisible() ? mainWindow.hide() : mainWindow.show();
});
} catch (error) {
console.error('Failed to load tray icon:', error.message);
}
}
// Funções relacionadas à atualização
function handleUpdateChecking() {
log.log('Checking for updates.');
}
function handleUpdateAvailable(info) {
log.log('Update available.');
}
function formatBytes(bytes) {
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (bytes === 0) return '0 Bytes';
const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
return `${(bytes / Math.pow(1024, i)).toFixed(2)} ${sizes[i]}`;
}
function handleDownloadProgress(progressObj) {
const transferredFormatted = formatBytes(progressObj.transferred);
const totalFormatted = formatBytes(progressObj.total);
const speedFormatted = formatBytes(progressObj.bytesPerSecond);
const message = `${translations.downloadingUpdate}. Speed: ${speedFormatted}/s - ${~~progressObj.percent}% [${transferredFormatted}/${totalFormatted}]`;
log.log(message);
const swalMessage = `Swal.fire({
title: '${translations.downloadingUpdate}',
html: '${message}',
allowOutsideClick: false,
onBeforeOpen: () => Swal.showLoading()
});`;
mainWindow.webContents.executeJavaScript(swalMessage);
}
function handleUpdateError(err) {
log.log(`Update check failed: ${translations.updateError}`);
}
function handleUpdateNotAvailable(info) {
log.log(`Não há atualizações disponíveis para o launcher.`);
}
function handleUpdateDownloaded(info) {
const swalMessage = `Swal.fire({
title: '${translations.restartingApp}',
html: '${translations.restartingApp}',
allowOutsideClick: false,
onBeforeOpen: () => Swal.showLoading()
});`;
mainWindow.webContents.executeJavaScript(swalMessage);
autoUpdater.quitAndInstall();
}
autoUpdater.on('checking-for-update', handleUpdateChecking);
autoUpdater.on('update-available', handleUpdateAvailable);
autoUpdater.on('download-progress', handleDownloadProgress);
autoUpdater.on('error', handleUpdateError);
autoUpdater.on('update-not-available', handleUpdateNotAvailable);
autoUpdater.on('update-downloaded', handleUpdateDownloaded);
// Evento quando o aplicativo está pronto
app.whenReady().then(() => {
createWindow();
createTray();
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
// Evento de download
ipcMain.on('download-dlc', (event, downloadUrl) => {
const win = BrowserWindow.getFocusedWindow();
const downloadDir = app.getPath('downloads');
win.webContents.downloadURL(downloadUrl);
win.webContents.session.on('will-download', (event, item) => {
const filePath = path.join(downloadDir, item.getFilename());
if (fs.existsSync(filePath)) {
try {
fs.unlinkSync(filePath);
console.log(`Arquivo existente deletado: ${filePath}`);
} catch (error) {
console.error(`Erro ao deletar arquivo existente: ${error.message}`);
}
}
item.setSavePath(filePath);
item.on('updated', (event, state) => {
if (state === 'progressing') {
const progress = Math.round((item.getReceivedBytes() / item.getTotalBytes()) * 100);
win.webContents.send('download-progress', { percent: progress });
}
});
item.on('done', (event, state) => {
if (state === 'completed') {
const swalMessage = `Swal.fire({
title: '${translations.downloadComplete}',
html: '${translations.downloadSuccessMessage.replace('{{fileName}}', item.getFilename())}',
icon: 'success',
confirmButtonText: 'OK'
});`;
mainWindow.webContents.executeJavaScript(swalMessage);
win.webContents.send('download-complete');
} else {
const swalError = `Swal.fire({
title: '${translations.downloadError}',
text: '${translations.downloadFailedMessage}',
icon: 'error',
confirmButtonText: 'OK'
});`;
mainWindow.webContents.executeJavaScript(swalError);
}
});
});
});