This repository has been archived by the owner on Jul 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.js
executable file
·290 lines (270 loc) · 12.1 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
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
/**
* main.js - NodeJS script that runs the app.
*
* @author Ad5001
* @version 1.0.0
* @license NTOSL (Custom) - View LICENSE.md in the root of the project
* @copyright (C) Ad5001 2017
* @package PocketMine Server Manager
*/
const electron = require('electron');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
const termColors = require("./assets/js/lib/formatingCodes.js");
// Outputing message of usage if no gui is specified. I know, it's a little hard to read...
if (process.argv.indexOf("--no-gui") !== -1) {
console.log(`${termColors.COLOR_YELLOW}You are running PocketMine Server Manager in ${termColors.FORMAT_ITALIC}"no gui mode"${termColors.FORMAT_RESET}${termColors.COLOR_YELLOW}.
This means ${termColors.FORMAT_ITALIC}no${termColors.FORMAT_RESET}${termColors.COLOR_YELLOW} window will be showed and everything will be done using commands.
${termColors.COLOR_GREEN}${termColors.FORMAT_BOLD}Launch a server:${termColors.FORMAT_RESET}${termColors.COLOR_ORANGE} ${process.argv[0]} --start <server name>
${termColors.COLOR_GREEN}${termColors.FORMAT_BOLD}Stop a server:${termColors.FORMAT_RESET}${termColors.COLOR_ORANGE} ${process.argv[0]} --stop <server name>
${termColors.COLOR_GREEN}${termColors.FORMAT_BOLD}Launch a GUI with server infos:${termColors.FORMAT_RESET}${termColors.COLOR_ORANGE} ${process.argv[0]} --view <server name>
${termColors.COLOR_GREEN}${termColors.FORMAT_BOLD}Launch the GUI:${termColors.FORMAT_RESET}${termColors.COLOR_ORANGE} ${process.argv[0]} --launch-gui
${termColors.COLOR_GREEN}${termColors.FORMAT_BOLD}Launch the GUI with os windows:${termColors.FORMAT_RESET}${termColors.COLOR_ORANGE} ${process.argv[0]} --launch-gui-os-win
${termColors.COLOR_GREEN}${termColors.FORMAT_BOLD}Quit the GUI:${termColors.FORMAT_RESET}${termColors.COLOR_ORANGE} ${process.argv[0]} --quit-gui
${termColors.COLOR_GREEN}${termColors.FORMAT_BOLD}Quit app:${termColors.FORMAT_RESET}${termColors.COLOR_ORANGE} ${process.argv[0]} --quit${termColors.FORMAT_RESET}`);
}
if (process.argv.indexOf("--debug") == -1) require('daemon-plus')(); // creates new child process, exists the parent
if (process.env.XDG_CURRENT_DESKTOP == "Unity:Unity7") process.env.XDG_CURRENT_DESKTOP = "Unity"; // Fixing tray with Ubuntu 17.04
const path = require('path');
const fs = require('fs');
const url = require('url');
const os = require('os');
const http = require('http');
const ipcMain = electron.ipcMain;
const Menu = electron.Menu;
const MenuItem = electron.MenuItem;
const php = require("./main/php");
const server = require("./main/server");
const tray = require("./main/tray");
require("./main/ipc")(this);
exports.tray = tray;
exports.server = server;
exports.php = php;
exports.servers = {};
var viewPage;
// Single instance, Windows Jump list & command line relaunching.
const shouldQuit = app.makeSingleInstance((commandLine, workingDirectory) => {
var used = false;
var start = false;
var stop = false;
var view = false;
commandLine.forEach(function(cmd) {
switch (cmd) {
case "--start":
start = true;
break;
case "--stop":
stop = true;
break;
case "--view":
view = true;
break;
case "--quit":
setTimeout(app.quit, 100); // Wait so no relaunch
break;
case "--relaunch":
app.quit(); // Relaunches the app.
break;
case "--launch-gui":
if (exports.mainWindow) {
exports.mainWindow.focus();
} else {
createWindow(true);
}
break;
case "--launch-gui-os-win":
if (exports.mainWindow) {
exports.mainWindow.webContents.executeJavaScript("window.close();", true, function() {
createWindow(true, true);
});
} else {
createWindow(true, true);
}
break;
case "--quit-gui":
if (exports.mainWindow) exports.mainWindow.webContents.executeJavaScript("window.close();", true);
if (!tray.tray.isDestroyed()) tray.tray.destroy();
break;
default:
if (start) {
exports.servers[cmd].start();
if (tray.tray) tray.removeStopServer(cmd);
start = false;
used = true;
} else if (stop) {
exports.servers[cmd].stop();
if (tray.tray) tray.removeStartServer(cmd);
stop = false;
used = true;
} else if (view) {
if (exports.mainWindow === null) {
exports.createWindow(true);
viewPage = "serverInfos#" + cmd;
} else if (defined) {
exports.mainWindow.webContents.executeJavaScript(`document.getElementById('frame').contentWindow.location = 'serverInfos.html#${cmd}'`);
exports.mainWindow.focus();
} else {
viewPage = "serverInfos#" + cmd;
}
used = true;
}
break;
}
});
if (!used) {
if (exports.mainWindow === null) exports.createWindow(true);
exports.mainWindow.focus();
}
});
if (shouldQuit) {
app.quit();
}
// Making folders
exports.appFolder = path.join(os.homedir(), ".pocketmine");
exports.serverFolder = path.join(exports.appFolder, "servers");
exports.phpFolder = path.join(exports.appFolder, "php");
exports.pharsFolder = path.join(os.homedir(), ".cache", "PSM");
try {
fs.accessSync(exports.appFolder);
} catch (e) { // No .pocketmine folder
fs.mkdirSync(exports.appFolder);
fs.mkdirSync(exports.serverFolder);
}
try {
fs.accessSync(path.join(os.homedir(), ".cache"));
} catch (e) {
fs.mkdirSync(path.join(os.homedir(), ".cache"));
}
try {
fs.accessSync(exports.pharsFolder);
} catch (e) {
fs.mkdirSync(exports.pharsFolder);
}
var this2 = this;
// Creates the window
function createWindow(forceLaunch = false, forceFrame = false) {
if (process.argv.indexOf("--no-gui") == -1 || forceLaunch) {
// Create the browser window.
var framed = process.argv.indexOf("--use-os-windows") !== -1 || forceFrame;
exports.mainWindow = new BrowserWindow({ width: 800, height: 600, title: "PocketMine Server Manager", frame: framed, icon: path.join(__dirname, "assets", "icons", "icon.png") });
exports.mainWindow.webContents.app = this2;
// and load the index.html of the app.
exports.mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'assets', !defined ? 'loading.html' : 'index.html'),
protocol: 'file:',
slashes: true
}))
exports.mainWindow.on('closed', function() {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
exports.mainWindow = null
});
// When the app is launched, just download the app.
exports.mainWindow.webContents.on("did-finish-load", define)
require('./main/menus');
if (!tray.tray) tray.addTray(php);
} else {
delete process.argv[process.argv.indexOf("--no-gui")]; // Remove the arg, we don't need it anymore.
setTimeout(function() { define() }, 1000); // Defining the rest of variables b4 calling this.
}
}
exports.createWindow = createWindow;
app.on('ready', function() { createWindow() });
app.on('window-all-closed', function() {
// Not killing process unitl used by force killing. Let servers running.
});
app.on('activate', function() {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (exports.mainWindow === null) {
createWindow();
}
if (exports.mainWindow.isMinimized()) exports.mainWindow.restore();
exports.mainWindow.focus();
});
// Defines everything when window loads
var defined = false;
function define() {
if (defined) {
if (viewPage) {
exports.mainWindow.webContents.executeJavaScript(`document.getElementById('frame').contentWindow.location = '${viewPage}'`, function() {
if (exports.mainWindow.isMinimized()) exports.mainWindow.restore();
exports.mainWindow.focus();
});
viewPage = undefined;
}
if (process.argv.indexOf("--use-os-windows") !== -1)
exports.mainWindow.webContents.executeJavaScript(`
document.querySelectorAll(".windowButton").forEach(function(elem){
elem.remove();
});`);
}
if (!defined) {
php.setApp(this2);
require("./main/updater.js").checkForUpdates(php, function() {
// Defining php
php.snackbar("Looking for php...");
php.define(function() {
// Checking for servers;
php.snackbar("Looking for servers...");
var servers = fs.readdirSync(exports.serverFolder);
servers.forEach(function(folder) {
exports.servers[folder] = new server.Server(folder, php, exports);
if (tray.trayMenu) {
tray.trayMenu.items[0].submenu.append(new MenuItem({
label: folder,
type: "normal",
id: `stopped${folder}`,
click: function() {
exports.servers[folder].start();
tray.removeStopServer(folder);
}
}));
tray.trayMenu.items[2].submenu.append(new MenuItem({
label: folder,
type: "normal",
id: `view${folder}`,
click: function() {
if (!exports.mainWindow) createWindow();
exports.mainWindow.webContents.executeJavaScript(`document.getElementById('frame').contentWindow.location = 'serverInfos.html#${folder}'`, function() {
if (exports.mainWindow.isMinimized()) exports.mainWindow.restore();
exports.mainWindow.focus();
});
}
}));
}
}, this);
if (tray.tray) tray.tray.setContextMenu(tray.trayMenu);
// Setting app clock (1 second based)
setInterval(function() {
// IPC Refreshing
ipcMain.main = this;
// Servers refreshing
var name;
Object.keys(exports.servers).forEach(function(name) {
var server = exports.servers[name];
if (server.changed) {
server.changed = false;
var serv = new server.ServerExportable();
serv.import(server);
event.sender.send("sendServer", serv);
}
})
Object.keys(exports.servers).forEach(function(key) {
var serv = exports.servers[key];
serv.refresh();
});
}, 2000);
php.snackbar("Done !");
// Redirects user to the main page
if (exports.mainWindow) exports.mainWindow.webContents.loadURL(url.format({
pathname: path.join(__dirname, 'assets', 'index.html'),
protocol: 'file:',
slashes: true
}));
});
});
defined = true;
}
}