-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.js
349 lines (309 loc) · 12.4 KB
/
app.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
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
// Modules to control application life and create native browser window
const {
app,
BrowserWindow,
ipcMain,
shell,
remote,
} = require('electron');
const fs = require('fs');
const { spawn } = require('cross-spawn');
const dialog = require('dialog');
const settings = require('electron-settings');
const homeDir = require('os').homedir();
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;
let promptWindow;
let addonJSON;
let isWin = process.platform === "win32";
var dtObj = new Date();
var hours = dtObj.getHours();
var minutes = dtObj.getMinutes();
var finalTime = hours + ":" + minutes;
var ADDON_IDS = [];
// Changes extensions to appropriate endings for Linux or Windows
ext = (isWin) ? ".ico" : ".png";
gmpublishFile = (isWin) ? "gmpublish.exe" : "gmpublish_linux";
gmadFile = (isWin) ? "gmad.exe" : "gmad_linux";
logLocation = (isWin) ? "\\AppData\\Roaming\\gmod-addon-tool\\GMATLog.txt" : "/.gmod-addon-tool/GMATLog.txt";
console.log('\n');
//################//
// Electron stuff //
//################//
function createWindow() {
// Called before anything else
if (settings.get('gmodDirectory')) {
fs.stat(settings.get('gmodDirectory') + "/bin/" + gmadFile, (err, stat) => {
if (err) {
dialog.err("It seems like your Garry's Mod directory has changed.\nResetting user settings so you can set your directory again.", "Gmad.exe not found", () => {
// Clear all settings
settings.deleteAll();
// Relaunch app so user can show where the directory has moved
setTimeout(() => {
app.relaunch(); app.exit();
}, 500);
});
}
});
}
app.allowRendererProcessReuse = true;
// Create the browser window.
mainWindow = new BrowserWindow({
width: 500,
height: 225,
resizable: false,
fullscreenable: false,
backgroundColor: (settings.get("darkMode") ? "#202020" : "#048CEC"),
titleBarStyle: "hidden",
frame: false,
icon: __dirname + "/src/img/icon" + ext,
webPreferences: {
nodeIntegration: true
}
});
// and load the index.html of the app.
mainWindow.loadFile('index.html');
// Open the DevTools.
// mainWindow.webContents.openDevTools();
// Emitted when the window is closed.
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.
mainWindow = null
});
// When an anchor tab w/ a target attribute with a value of "_blank", this will stop the default behavior and open the link in the user's default browser.
mainWindow.webContents.on('new-window', function(e, url) {
e.preventDefault();
require('electron').shell.openExternal(url);
});
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow);
//#################//
// Settings & logs //
//#################//
// Create log file if it doesn't exist
if (isWin) {
fs.stat(homeDir + logLocation, function(err, stats) {
if (err) {
if (err != null) { console.log(err); }
fs.access(homeDir + logLocation, fs.constants.F_OK, (err) => {
console.log("Created log.txt");
fs.appendFile(homeDir + logLocation, "--- Beginning of log --- \n", 'utf8', (err) => {
});
});
} else {
fs.appendFile(homeDir + logLocation, "\n-----------------------", 'utf8', (err) => { if (err != null) { console.log(err) } });
}
});
} else {
// Create directory for GMAT logs, if it doesn't exist
if (!fs.existsSync(homeDir + "/.gmod-addon-tool")) {
fs.mkdirSync(homeDir + "/.gmod-addon-tool");
}
fs.stat(homeDir + logLocation, function(err, stats) {
if (err) {
if (err != null) { console.log(err); }
fs.access(homeDir + logLocation, fs.constants.F_OK, (err) => {
console.log("Created log.txt");
fs.appendFile(homeDir + logLocation, "--- Beginning of log --- \n", 'utf8', (err) => {
});
});
}
});
}
function openSettings(callback) {
promptWindow = new BrowserWindow({
width: 250,
height: 275,
parent: mainWindow,
show: false,
modal: true,
title : "Settings",
autoHideMenuBar: true,
resizable: false,
fullscreenable: false,
backgroundColor: (settings.get("darkMode") ? "#202020" : "#048CEC"),
titleBarStyle: "hidden",
frame: false,
webPreferences: {
nodeIntegration: true,
}
});
promptWindow.on('closed', () => {
promptWindow = null
});
promptWindow.webContents.on('new-window', function(e, url) {
e.preventDefault();
require('electron').shell.openExternal(url);
});
// Load the HTML dialog box
promptWindow.loadFile("settings.html");
promptWindow.once('ready-to-show', () => { promptWindow.show();});
}
ipcMain.on("openSettings", (e) => {
console.log(__dirname);
openSettings();
});
// Writes info to GMATLog.txt
function sendConsoleData(dataArray) {
dataArray.forEach(data => {
fs.appendFile(homeDir + logLocation, "\n[" + finalTime + "]" + data, 'utf8', (err) => {});
});
}
// Also let client tell us about errors
ipcMain.on("logError", (e, error) => {
sendConsoleData(error);
});
//#####################//
// Addon related logic //
//#####################//
// addon.json related //
// Check if user's addon folder already has addon.json, let client know
function checkIfAddonJSONExist(file) {
console.log("Checking for addon.json in " + file);
fs.stat(file + "/addon.json", function(err, stats) {
console.log((err) ? "User does not have addon.json" : "User already has addon.json");
if (err) {
mainWindow.webContents.send('addonJSONCheck', false);
} else {
fs.readFile(file + '/addon.json', function read(err, data) {
if (err) {
throw err;
}
const content = data.toString("utf-8");
mainWindow.webContents.send('addonJSONCheck', true, content);
});
}
});
}
// This creates our addon.json
ipcMain.on('createJsonFile', (event, json, dir) => {
fs.writeFileSync(dir + "/addon.json", JSON.stringify(json), 'utf8', (err) => {
if (err != null) {
mainWindow.webContents.send('errorNote', "An error occured while writing JSON object to file.\n", false, true);
}
});
});
// User's current addons //
// This will send the client the IDs of their addons
ipcMain.on('getAddonInfo', () => {
console.log('Trying to get addon info...');
sendClientAddonInfo();
console.log("User's Gmod Directory:" + settings.get('gmodDirectory'));
});
// We use this to get the addon IDs from gmpublish.exe
function sendClientAddonInfo() {
const bat = spawn(settings.get('gmodDirectory') + '/bin/' + gmpublishFile, ['list']);
bat.stdout.on('data', (data) => {
sendConsoleData(data.toString().split("\n"));
var arrayOfOutput = data.toString().split('\n');
//fixedArray should be a list of addon IDs
var fixedArray = arrayOfOutput.slice(5, arrayOfOutput.length - 3);
if (data.includes("Couldn't initialize Steam!")) {
mainWindow.webContents.send('errorNote', "Steam doesn't seem to be open!", true, false);
}
if (data.includes("Done.")) {
for (var i = 0; i < fixedArray.length; i++) {
fixedArray[i] = fixedArray[i].replace('/r', '');
ADDON_IDS.push([fixedArray[i].substr(0, 11).replace(/\s/g, '').toString()]);
}
console.log("Addon IDs", ADDON_IDS);
mainWindow.webContents.send('addonInfo', ADDON_IDS);
}
}, (err) => {console.log(err)});
bat.on('uncaughtException', err => {
console.error('There was an uncaught error', err);
alert("bruh")
process.exit(1); // mandatory (as per the Node.js docs)
});
}
// Addon creation and uploading //
// This is ran once the client requests to create an addon
ipcMain.on('createGMAFile', (event, addonDir) => {
console.log("Addon's Directory: " + addonDir.toString());
sendConsoleData(["Addon's directory: " + addonDir.toString()]);
const gmad = spawn(settings.get('gmodDirectory') + '/bin/' + gmadFile, ['create', '-folder', addonDir]);
gmad.stdout.on('data', (data) => {
var arrayOfOutput = data.toString().split('\n');
if (data.includes('File list verification failed')) {
mainWindow.webContents.send("errorNote", "File list verification failed - check your addon for unallowed files.", false, true);
}
if (data.includes("Successfully")) {
var fixedArray = arrayOfOutput.slice(arrayOfOutput.length - 2, arrayOfOutput.length - 1);
fixedArray = fixedArray[0].match(/(?:"[^"]*"|^[^"]*$)/)[0].replace(/"/g, "");
var addonGMADir = fixedArray;
mainWindow.webContents.send('addonGMALocation', addonGMADir);
console.log("GMA location: " + addonGMADir);
sendConsoleData(arrayOfOutput);
}
}), (err) => { console.log(err) };
gmad.on("error", (err) => {sendConsoleData([err])})
});
// This block will upload the GMA file to the Steam Workshop
ipcMain.on('uploadToWorkshop', (event, gmaDir, iconDir, addonId) => {
if (addonId != null) {
const gmpublish = spawn(settings.get('gmodDirectory') + '/bin/' + gmpublishFile, ['update', '-id', addonId, '-icon', iconDir, '-addon', gmaDir]);
gmpublish.stdout.on('data', (data) => {
var arrayOfOutput = data.toString().split('\n');
sendConsoleData(arrayOfOutput);
if (data.includes('512x512')) {
mainWindow.webContents.send("errorNote", "Image must be a 512x512 baseline jpeg! Trying exporting with Paint.", false, false);
}
if (data.includes("Success!")) {
var fixedArray = arrayOfOutput.slice(arrayOfOutput.length - 2, arrayOfOutput.length - 1);
fixedArray = fixedArray[0].replace(/\D/, '');
fixedArray = fixedArray.substr(5, fixedArray.length);
mainWindow.webContents.send('currentAddonID', fixedArray);
}
});
} else {
// Passes all the info needed to publish a Garry's Mod addon
const gmpublish = spawn(settings.get('gmodDirectory') + '/bin/' + gmpublishFile, ['create', '-icon', iconDir, '-addon', gmaDir]);
gmpublish.stdout.on('data', (data) => {
var arrayOfOutput = data.toString().split('\n');
sendConsoleData(arrayOfOutput);
// If gmpublish output contains "512x512" in data stream, user did not provide a correctly sized image
if (data.includes('512x512')) {
mainWindow.webContents.send("errorNote", "Image must be a 512x512 baseline jpeg! Trying exporting with Paint.", false, false);
}
if (data.includes("Addon creation finished")) {
var fixedArray = arrayOfOutput.slice(arrayOfOutput.length - 2, arrayOfOutput.length - 1);
fixedArray = fixedArray[0].replace(/\D/, '');
fixedArray = fixedArray.substr(5, fixedArray.length);
var stringArray = fixedArray.toString();
var addonURLIndex = stringArray.indexOf("?id=");
var addonURL = stringArray.slice(addonURLIndex + 4, addonURLIndex + 14)
mainWindow.webContents.send('currentAddonID', addonURL);
}
});
};
});
// Other things & stuff //
// This will extract a GMA file into the same directory
ipcMain.on("extractAddon", (e, path) => {
const gmad = spawn(settings.get('gmodDirectory') + '/bin/' + gmadFile, ['extract', '-file', path]);
gmad.stdout.on("data", (DATA) => sendConsoleData([DATA]));
gmad.on("error", (err) => sendConsoleData([err]));
mainWindow.webContents.send("finishExtraction");
});
// Checks to see if the directory the user chooses is writeable
ipcMain.on('checkIfDirectoryExists', (event, file, jsonCheck) => {
fs.access(file, fs.constants.R_OK, (err) => {
console.log(`${file} ${err ? 'is not readable' : 'is readable'}`);
});
if (jsonCheck) { checkIfAddonJSONExist(file) }
});
// This will only run the first time the user launches the app or resets settings
if (settings.get("firstRun") == null) {
settings.set("firstRun", false);
settings.set("darkMode", false);
// Check to see if user has Garry's Mod installed on local drive so we can skip getting the directory
fs.stat((isWin) ? "C:/Program Files (x86)/Steam/steamapps/common/GarrysMod/bin/gmad.exe" : homeDir + "/.local/share/Steam/steamapps/common/GarrysMod", (err, stat) => {
if (!err) { settings.set("gmodDirectory", (isWin) ? "C:/Program Files (x86)/Steam/steamapps/common/GarrysMod" : homeDir + "/.local/share/Steam/steamapps/common/GarrysMod") }
});
}