-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
149 lines (131 loc) · 4.57 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
'use strict';
const { app, BrowserWindow } = require('electron');
const storage = require('electron-json-storage');
const { ipcMain } = require('electron');
const { Menu } = require('electron');
const { shell } = require('electron');
// require('electron-debug')({ showDevTools: true });
const { execFile } = require('child_process');
const path = require('path');
var mainWindow = null;
var gamesData = {};
/* APP EVENTS */
app.on('ready', () => {
prepareMainMenu();
// only loads the app windows after game data is loaded from JSON
storage.get('config', function (error, data) {
if (error) {
// hard quit if data is not loaded
app.quit();
throw error;
}
if (data.arcade_name == undefined) {
console.log("A valid config file was not found, loading example file.")
data = exampleConfigFile();
storage.set('config', data);
}
gamesData = data;
console.log(path.join(__dirname, 'app/img/icon.png'));
// main window setup
mainWindow = new BrowserWindow({
width: gamesData.screen_width,
height: gamesData.screen_height,
backgroundColor: gamesData.bg_color,
fullscreen: gamesData.is_fullscreen,
fullscreenable: gamesData.is_fullscreen,
autoHideMenuBar: true,
resizable: false,
icon: path.join(__dirname, 'app/img/icon.png')
});
// open arcade window
mainWindow.loadURL('file://' + __dirname + '/app/index.html');
});
});
// make sure the app quits
app.on('window-all-closed', () => {
app.quit();
});
// make sure the app quits
app.on('close-app', (arg) => {
app.quit();
});
/* IPC MAIN EVENTS */
// when the app window opens
ipcMain.on('load-games-data', (event, arg) => {
// load games list from config file and send to renderer
event.returnValue = gamesData;
});
// when receives a game-chosen method from the renderer process
ipcMain.on('start-game', (event, arg) => {
// runs the game's executable file
execProcess(gamesData.games[arg].exec_path, () => {
mainWindow.restore();
// notifies renderer when game finishes
event.sender.send('game-finished');
});
});
/* HELPER FUNCTIONS */
function execProcess(pathExe, cb) {
let child = execFile(pathExe);
// when the game process ends, do something
child.on('exit', (code, signal) => {
cb();
});
}
function prepareMainMenu() {
const template = [
{
label: 'Setup',
submenu: [
{
label: 'Show config file in Explorer',
accelerator: 'CmdOrCtrl+S',
click(item, focusedWindow) {
console.log("Preparing to show config file.");
storage.has('config', (error, hasKey) => {
if (error) throw error;
if (!hasKey) {
storage.set('config', exampleConfigFile(), () => {
console.log("Creating and showing example config file.");
shell.showItemInFolder(app.getPath('userData') + '/storage/config.json');
});
return;
}
console.log("Showing config file in Explorer.");
shell.showItemInFolder(app.getPath('userData') + '/storage/config.json');
});
}
}
]
}
];
let mainMenu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(mainMenu);
}
function exampleConfigFile() {
return {
"arcade_name": "Example arcade name",
"bg_color": "#ff0",
"bg_url": "",
"select_button_id": 0,
"exit_button_id": 1,
"left_button_id": 14,
"right_button_id": 15,
"stick_index": 0,
"screen_width": 1200,
"screen_height": 650,
"is_fullscreen": false,
"sfx_change_game": "an_absolute_path_to_audio_file.wav",
"sfx_start_game": "an_absolute_path_to_audio_file.wav",
"music_menu": "an_absolute_path_to_audio_file.ogg",
"games": [
{
"id": "game_unique_id",
"name": "Example game",
"info": "Any info you want.",
"exec_path": "an_absolute_path_to_executable",
"cover_path": "an_absolute_path_to_cover",
}
]
};
}