-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathmain.js
More file actions
745 lines (670 loc) · 22.7 KB
/
main.js
File metadata and controls
745 lines (670 loc) · 22.7 KB
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
const electron = require('electron');
const path = require('path');
const child_process = require('child_process');
const app = electron.app; // Module to control application life.
const BrowserWindow = electron.BrowserWindow; // Module to create native browser window.
const Menu = electron.Menu;
const Notification = electron.Notification;
const protocol = electron.protocol;
const parseArgs = require('minimist');
const isDev = require('electron-is-dev');
const ipcMain = electron.ipcMain;
const autoUpdater = require('electron-updater').autoUpdater;
const log = require('electron-log');
const { uploadLocalFile } = require('./LocalFileUploader');
const { serveFolder, stopServer, stopAllServers } = require('./ServeFolder');
const {
startDebuggerServer,
sendMessage,
closeAllConnections,
closeServer: closeDebuggerServer,
stopAllServers: stopAllDebuggerServers,
} = require('./DebuggerServer');
const {
buildElectronMenuFromDeclarativeTemplate,
buildPlaceholderMainMenu,
} = require('./MainMenu');
const { loadExternalEditorWindow } = require('./LocalExternalEditorWindow');
const { load, registerGdideProtocol } = require('./Utils/UrlLoader');
const throttle = require('lodash.throttle');
const { findLocalIp } = require('./Utils/LocalNetworkIpFinder');
const setUpDiscordRichPresence = require('./DiscordRichPresence');
const {
downloadLocalFile,
saveLocalFileFromArrayBuffer,
} = require('./LocalFileDownloader');
const {
openPreviewWindow,
closePreviewWindow,
closePreviewWindowsForParent,
closeAllPreviewWindows,
} = require('./PreviewWindow');
const {
setupLocalGDJSDevelopmentWatcher,
closeLocalGDJSDevelopmentWatcher,
onLocalGDJSDevelopmentWatcherRuntimeUpdated,
} = require('./LocalGDJSDevelopmentWatcher');
const { setupWatcher, disableWatcher } = require('./LocalFilesystemWatcher');
// Initialize `@electron/remote` module
require('@electron/remote/main').initialize();
log.info('GDevelop Electron app starting...');
// Logs made with electron-logs can be found
// on Linux: ~/.config/<app name>/log.log
// on OS X: ~/Library/Logs/<app name>/log.log
// on Windows: %USERPROFILE%\AppData\Roaming\<app name>\log.log
autoUpdater.logger = log;
autoUpdater.logger.transports.file.level = 'info';
autoUpdater.autoDownload = false;
// Keep a global reference of the window objects, if you don't, the windows will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindows = new Set();
let mainWindow = null; // Primary window reference for backwards compatibility
let windowCounter = 0; // Counter for creating unique session partitions
// Parse arguments (knowing that in dev, we run electron with an argument,
// so have to ignore one more).
const args = parseArgs(process.argv.slice(isDev ? 2 : 1), {
// "Officially" supported arguments and their types:
boolean: ['dev-tools', 'disable-update-check'],
string: '_', // Files are always strings
});
const devTools = !!args['dev-tools'];
// See registerGdideProtocol (used for HTML modules support)
protocol.registerSchemesAsPrivileged([{ scheme: 'gdide' }]);
// Notifications on Microsoft Windows platforms show the app user model id.
// If not set, defaults to `electron.app.{app.name}`.
if (process.platform === 'win32') {
app.setAppUserModelId('gdevelop.ide');
}
// Single instance lock - prevents multiple Electron processes
// This solves Firebase IndexedDB locking issues while still allowing multiple windows
const gotTheLock = app.requestSingleInstanceLock();
if (!gotTheLock) {
// Second instance attempted - quit immediately
app.quit();
} else {
// First instance - handle second-instance events by creating new windows
app.on('second-instance', (event, commandLine, workingDirectory) => {
// User tried to launch app again - create a new window instead
const secondInstanceArgs = parseArgs(commandLine.slice(isDev ? 2 : 1), {
boolean: ['dev-tools', 'disable-update-check'],
string: '_',
});
// Create a new window in the existing process
createNewWindow(secondInstanceArgs);
});
}
// Quit when all windows are closed.
app.on('window-all-closed', function() {
// Best-effort cleanup - ignore any errors to prevent crashes during shutdown
try {
stopAllServers();
} catch (e) {
// Ignore errors during shutdown
}
try {
stopAllDebuggerServers();
} catch (e) {
// Ignore errors during shutdown
}
// Close all remaining preview windows
try {
closeAllPreviewWindows();
} catch (e) {
// Ignore errors during shutdown
}
app.quit();
});
// Function to create a new GDevelop window
function createNewWindow(windowArgs = args) {
const isIntegrated = windowArgs.mode === 'integrated';
if (isIntegrated && app.dock) {
app.dock.hide();
}
// Create the browser window.
const options = {
width: windowArgs.width || 800,
height: windowArgs.height || 600,
x: windowArgs.x,
y: windowArgs.y,
titleBarStyle: 'hidden',
titleBarOverlay: {
color: '#000000',
symbolColor: '#ffffff',
},
trafficLightPosition: { x: 12, y: 12 },
webPreferences: {
webSecurity: false, // Allow to access to local files,
// Allow Node.js API access in renderer process, as long
// as we've not removed dependency on it and on "@electron/remote".
nodeIntegration: true,
contextIsolation: false,
},
enableLargerThanScreen: true,
backgroundColor: '#000',
};
// First window (windowCounter === 0) uses default storage for backwards compatibility
// Additional windows get unique partitions for independent auth AND separate renderer processes
const windowNumber = windowCounter;
if (windowCounter > 0) {
// Create a unique partition for this window so it has independent auth state
// Each partition gets its own storage (IndexedDB, localStorage, cookies, etc.)
options.webPreferences.partition = `persist:gdevelop-window-${windowCounter}`;
log.info(
`Creating window #${windowCounter} with partition: persist:gdevelop-window-${windowCounter}`
);
} else {
log.info(
`Creating window #${windowCounter} with default partition (backwards compatibility)`
);
}
windowCounter++;
if (isIntegrated) {
options.acceptFirstMouse = true;
options.skipTaskbar = true;
options.hasShadow = false;
options.frame = false;
options.minimizable = false;
options.movable = false;
options.resizable = false;
options.fullscreenable = false;
options.show = false;
}
const newWindow = new BrowserWindow(options);
if (!isIntegrated) newWindow.maximize();
// Capture window ID and whether this is the primary window before it can be destroyed
const windowId = newWindow.id;
const isPrimaryWindow = windowNumber === 0;
log.info(
`Created window with Electron ID: ${windowId}, window number: ${windowNumber}, isPrimary: ${isPrimaryWindow}`
);
// Track this window
mainWindows.add(newWindow);
// Set as primary window if this is the first one
if (isPrimaryWindow) {
mainWindow = newWindow;
}
// Enable `@electron/remote` module for renderer process
require('@electron/remote/main').enable(newWindow.webContents);
// Log process ID to verify separate renderer processes
newWindow.webContents.once('did-finish-load', () => {
newWindow.webContents
.executeJavaScript('process.pid')
.then(pid => {
log.info(
`Window ${windowId} (window number ${windowNumber}) is running in renderer process PID: ${pid}`
);
})
.catch(err => {
log.warn('Could not get renderer process PID:', err);
});
});
if (isDev)
newWindow.webContents.session.loadExtension(
path.join(__dirname, 'extensions/ReactDeveloperTools/4.24.3_0/')
);
// Load the index.html of the app.
load({
window: newWindow,
isDev,
path: '/index.html',
devTools,
});
newWindow.on('closed', function() {
// Remove from tracked windows
mainWindows.delete(newWindow);
// If this was the primary window, set a new primary
if (isPrimaryWindow) {
mainWindow = mainWindows.values().next().value || null;
}
// Close preview windows belonging to this window
closePreviewWindowsForParent(windowId);
// Stop the server and debugger for this window
stopServer(windowId, () => {});
closeDebuggerServer(windowId);
});
// Prevent any navigation inside the window
newWindow.webContents.on('will-navigate', (e, url) => {
if (url !== newWindow.webContents.getURL()) {
console.info('Opening in browser (because of will-navigate):', url);
e.preventDefault();
electron.shell.openExternal(url);
}
});
// Prevent opening any website or url inside Electron
newWindow.webContents.setWindowOpenHandler(details => {
console.info('Opening in browser (because of new window): ', details.url);
electron.shell.openExternal(details.url);
return { action: 'deny' };
});
return newWindow;
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
app.on('ready', function() {
registerGdideProtocol({ isDev });
// Create the first window
createNewWindow(args);
// Expose program arguments (to be accessed by windows)
global['args'] = args;
Menu.setApplicationMenu(buildPlaceholderMainMenu());
// Set up dock menu (macOS) for creating new windows
if (app.dock) {
const dockMenu = Menu.buildFromTemplate([
{
label: 'New window',
click: () => {
createNewWindow(args);
},
},
]);
app.dock.setMenu(dockMenu);
}
// Set up jump list tasks (Windows) for creating new windows
if (process.platform === 'win32') {
app.setUserTasks([
{
program: process.execPath,
arguments: '',
iconPath: process.execPath,
iconIndex: 0,
title: 'New window',
description: 'Open a new GDevelop window',
},
]);
}
ipcMain.on('set-main-menu', (event, mainMenuTemplate) => {
const window = BrowserWindow.fromWebContents(event.sender);
Menu.setApplicationMenu(
buildElectronMenuFromDeclarativeTemplate(
window || mainWindow,
mainMenuTemplate
)
);
});
ipcMain.handle('preview-open', async (event, options) => {
const parentWindow = BrowserWindow.fromWebContents(event.sender);
return openPreviewWindow({
parentWindow: parentWindow,
previewBrowserWindowOptions: options.previewBrowserWindowOptions,
previewGameIndexHtmlPath: options.previewGameIndexHtmlPath,
alwaysOnTop: options.alwaysOnTop,
hideMenuBar: options.hideMenuBar,
numberOfWindows: options.numberOfWindows,
captureOptions: options.captureOptions,
openEvent: event,
});
});
ipcMain.handle('preview-close', async (event, options) => {
return closePreviewWindow(options.windowId);
});
ipcMain.handle('preview-close-all', async () => {
return closeAllPreviewWindows();
});
// Piskel image editor
ipcMain.handle('piskel-load', (event, externalEditorInput) => {
const parentWindow = BrowserWindow.fromWebContents(event.sender);
return loadExternalEditorWindow({
parentWindow: parentWindow,
devTools,
indexSubPath: 'piskel/piskel-electron-index.html',
externalEditorInput,
});
});
// JFXR sound effect generator
ipcMain.handle('jfxr-load', (event, externalEditorInput) => {
const parentWindow = BrowserWindow.fromWebContents(event.sender);
return loadExternalEditorWindow({
parentWindow: parentWindow,
devTools,
indexSubPath: 'jfxr/jfxr-electron-index.html',
externalEditorInput,
});
});
// Yarn Dialogue Tree Editor
ipcMain.handle('yarn-load', (event, externalEditorInput) => {
const parentWindow = BrowserWindow.fromWebContents(event.sender);
return loadExternalEditorWindow({
parentWindow: parentWindow,
devTools,
indexSubPath: 'yarn/yarn-electron-index.html',
externalEditorInput,
});
});
// LocalFileUploader events:
ipcMain.on('local-file-upload', (event, localFilePath, uploadOptions) => {
log.info(
'Received event local-file-upload with localFilePath=',
localFilePath
);
uploadLocalFile(
localFilePath,
uploadOptions,
throttle((current, max) => {
event.sender.send('local-file-upload-progress', current, max);
}, 300)
).then(
() => {
log.info('Local file upload successfully done');
event.sender.send('local-file-upload-done', null);
},
uploadError => {
log.error('Local file upload errored', uploadError);
event.sender.send('local-file-upload-done', uploadError);
}
);
});
// Titlebar handling:
ipcMain.handle(
'titlebar-set-overlay-options',
async (event, overlayOptions) => {
const window = BrowserWindow.fromWebContents(event.sender);
if (!window) return;
// setTitleBarOverlay seems not defined on macOS.
if (window.setTitleBarOverlay) window.setTitleBarOverlay(overlayOptions);
if (window.setBackgroundColor)
window.setBackgroundColor(overlayOptions.color);
}
);
// Window maximize toggle (for double-click on titlebar):
ipcMain.handle('window-maximize-toggle', async event => {
const window = BrowserWindow.fromWebContents(event.sender);
if (!window) return;
if (window.isMaximized()) {
window.unmaximize();
} else {
window.maximize();
}
});
// LocalFileDownloader events:
ipcMain.handle('local-file-download', async (event, url, outputPath) => {
const result = await downloadLocalFile(url, outputPath);
return result;
});
ipcMain.handle(
'local-file-save-from-arraybuffer',
async (event, arrayBuffer, outputPath) => {
const result = await saveLocalFileFromArrayBuffer(
arrayBuffer,
outputPath
);
return result;
}
);
// LocalFilesystemWatcher events:
ipcMain.handle(
'local-filesystem-watcher-setup',
(event, folderPath, options) => {
const subscriptionId = setupWatcher(
folderPath,
changedFilePath => {
event.sender.send('project-file-changed', changedFilePath);
},
options
);
return subscriptionId;
}
);
ipcMain.handle(
'local-filesystem-watcher-disable',
(event, subscriptionId) => {
disableWatcher(subscriptionId);
}
);
// ServeFolder events:
ipcMain.on('serve-folder', (event, options) => {
const window = BrowserWindow.fromWebContents(event.sender);
const windowId = window ? window.id : 'unknown';
log.info(
'Received event to server folder with options=',
options,
'for window',
windowId
);
serveFolder({ ...options, windowId }, (err, serverParams) => {
// Using JSON to copy the config strips unserializable properties
// (like middleware functions) automatically.
const configCopy = JSON.parse(JSON.stringify(serverParams));
event.sender.send('serve-folder-done', err, configCopy);
});
});
ipcMain.on('stop-server', event => {
const window = BrowserWindow.fromWebContents(event.sender);
const windowId = window ? window.id : 'unknown';
log.info('Received event to stop server for window', windowId);
stopServer(windowId, err => {
event.sender.send('stop-server-done', err);
});
});
ipcMain.on('get-local-network-ip', event => {
event.sender.send('local-network-ip', findLocalIp());
});
// LocalGDJSDevelopmentWatcher events:
ipcMain.on('setup-local-gdjs-development-watcher', event => {
setupLocalGDJSDevelopmentWatcher();
});
ipcMain.on('close-local-gdjs-development-watcher', event => {
closeLocalGDJSDevelopmentWatcher();
});
onLocalGDJSDevelopmentWatcherRuntimeUpdated(() => {
log.info(
'Notifying all editor windows that the GDJS runtime has been updated.'
);
mainWindows.forEach(window => {
window.webContents.send(
'local-gdjs-development-watcher-runtime-updated',
null
);
});
});
// DebuggerServer events:
ipcMain.on('debugger-start-server', (event, options) => {
const window = BrowserWindow.fromWebContents(event.sender);
const windowId = window ? window.id : 'unknown';
log.info(
'Received event to start debugger server with options=',
options,
'for window',
windowId
);
startDebuggerServer(windowId, {
onMessage: ({ id, message }) => {
if (!event.sender.isDestroyed()) {
event.sender.send('debugger-message-received', { id, message });
}
},
onError: error => {
if (!event.sender.isDestroyed()) {
event.sender.send('debugger-error-received', error);
}
},
onConnectionClose: ({ id }) => {
if (!event.sender.isDestroyed()) {
event.sender.send('debugger-connection-closed', { id });
}
},
onConnectionOpen: ({ id }) => {
if (!event.sender.isDestroyed()) {
event.sender.send('debugger-connection-opened', { id });
}
},
onConnectionError: ({ id, errorMessage }) => {
if (!event.sender.isDestroyed()) {
event.sender.send('debugger-connection-errored', {
id,
errorMessage,
});
}
},
onListening: ({ address }) => {
if (!event.sender.isDestroyed()) {
event.sender.send('debugger-start-server-done', { address });
}
},
});
});
ipcMain.on('debugger-send-message', (event, message) => {
const window = BrowserWindow.fromWebContents(event.sender);
const windowId = window ? window.id : 'unknown';
sendMessage(windowId, message, err => {
if (!event.sender.isDestroyed()) {
event.sender.send('debugger-send-message-done', err);
}
});
});
ipcMain.on('debugger-close-all-connections', event => {
const window = BrowserWindow.fromWebContents(event.sender);
const windowId = window ? window.id : 'unknown';
closeAllConnections(windowId);
});
// Track whether the current update check was triggered explicitly by the user,
// so that errors are only surfaced to the user for manual checks.
let isExplicitUpdateCheck = false;
ipcMain.on('updates-check-and-download', (event, { explicit } = {}) => {
// This will immediately download an update, then install when the
// app quits.
isExplicitUpdateCheck = !!explicit;
log.info('Starting check for updates (with auto-download if any)');
autoUpdater.autoDownload = true;
autoUpdater.checkForUpdatesAndNotify().catch(err => {
log.error('Error checking for updates:', err);
});
});
ipcMain.on('updates-check', (event, { explicit } = {}) => {
isExplicitUpdateCheck = !!explicit;
log.info('Starting check for updates (without auto-download)');
autoUpdater.autoDownload = false;
autoUpdater.checkForUpdates().catch(err => {
log.error('Error checking for updates:', err);
});
});
ipcMain.on('updates-install-and-quit', () => {
autoUpdater.quitAndInstall();
});
function sendUpdateStatus(status) {
log.info(status);
mainWindows.forEach(window => {
if (!window.isDestroyed()) {
window.webContents.send('update-status', status);
}
});
}
autoUpdater.on('checking-for-update', () => {
sendUpdateStatus({
message: 'Checking for update...',
status: 'checking-for-update',
});
});
autoUpdater.on('update-available', info => {
sendUpdateStatus({
message: 'Update available.',
status: 'update-available',
info,
});
});
autoUpdater.on('update-not-available', info => {
sendUpdateStatus({
message: 'Update not available.',
status: 'update-not-available',
});
});
autoUpdater.on('error', err => {
if (isExplicitUpdateCheck) {
sendUpdateStatus({
message: 'Error in auto-updater. ' + err,
status: 'error',
err,
});
} else {
log.error('Background update check failed:', err);
}
});
autoUpdater.on('download-progress', progressObj => {
let logMessage = 'Download speed: ' + progressObj.bytesPerSecond;
logMessage = logMessage + ' - Downloaded ' + progressObj.percent + '%';
logMessage =
logMessage +
' (' +
progressObj.transferred +
'/' +
progressObj.total +
')';
sendUpdateStatus({
message: logMessage,
status: 'download-progress',
bytesPerSecond: progressObj.bytesPerSecond,
percent: progressObj.percent,
transferred: progressObj.transferred,
total: progressObj.total,
});
});
autoUpdater.on('update-downloaded', info => {
sendUpdateStatus({
message: 'Update downloaded',
status: 'update-downloaded',
info,
});
});
setUpDiscordRichPresence(ipcMain);
// npm script execution in external terminal (cross-platform)
ipcMain.on('run-npm-script', (event, { projectPath, npmScript }) => {
log.info(`Running npm script "${npmScript}" in ${projectPath}`);
const platform = process.platform;
const npmCommand = `npm run ${npmScript}`;
try {
if (platform === 'win32') {
// Windows: open cmd window that stays open after npm command
child_process
.spawn(
'cmd.exe',
[
'/c',
'start',
'cmd.exe',
'/k',
`cd ${projectPath} && ${npmCommand}`,
],
{
detached: true,
stdio: 'ignore',
}
)
.unref();
} else if (platform === 'darwin') {
const escapedPath = projectPath.replace(/'/g, "'\\''");
const script = `tell application "Terminal" to do script "cd '${escapedPath}' && ${npmCommand}"`;
child_process.spawn('osascript', ['-e', script], {
detached: true,
stdio: 'ignore',
});
} else {
// Linux: try common terminal emulators
const bashCommand = `cd "${projectPath}" && ${npmCommand}; exec bash`;
const terminals = [
{
cmd: 'x-terminal-emulator',
args: ['-e', 'bash', '-c', bashCommand],
},
{ cmd: 'gnome-terminal', args: ['--', 'bash', '-c', bashCommand] },
{ cmd: 'konsole', args: ['-e', 'bash', '-c', bashCommand] },
{ cmd: 'xterm', args: ['-e', 'bash', '-c', bashCommand] },
];
const tryTerminal = index => {
if (index >= terminals.length) {
log.error('No terminal emulator found');
return;
}
const terminal = terminals[index];
const proc = child_process.spawn(terminal.cmd, terminal.args, {
detached: true,
stdio: 'ignore',
});
proc.on('error', () => tryTerminal(index + 1));
proc.unref();
};
tryTerminal(0);
}
} catch (err) {
log.error('Failed to run npm script:', err);
}
});
});