-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
373 lines (304 loc) · 10.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
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
const {app, protocol, BrowserWindow, ipcMain, dialog} = require('electron')
const installing = require('electron-squirrel-startup');
const path = require('path')
const fs = require('fs');
const jszip = require('jszip')
const APP_ROOT_PATH = app.getPath('userData')
const { DiscordInterface } = require('./DiscordInterface.js')
var config;
var discordInterface;
if (installing) {
app.quit()
}
if (handleSquirrelEvent()) {
app.quit()
}
function handleSquirrelEvent() {
if (process.argv.length === 1) {
return false;
}
const ChildProcess = require('child_process');
const path = require('path');
const appFolder = path.resolve(process.execPath, '..');
const rootAtomFolder = path.resolve(appFolder, '..');
const updateDotExe = path.resolve(path.join(rootAtomFolder, 'Update.exe'));
const exeName = path.basename(process.execPath);
const spawn = function(command, args) {
let spawnedProcess, error;
try {
spawnedProcess = ChildProcess.spawn(command, args, {detached: true});
} catch (error) {}
return spawnedProcess;
};
const spawnUpdate = function(args) {
return spawn(updateDotExe, args);
};
const squirrelEvent = process.argv[1];
switch (squirrelEvent) {
case '--squirrel-install':
case '--squirrel-updated':
// Optionally do things such as:
// - Add your .exe to the PATH
// - Write to the registry for things like file associations and
// explorer context menus
// Install desktop and start menu shortcuts
spawnUpdate(['--createShortcut', exeName]);
setTimeout(app.quit, 1000);
return true;
case '--squirrel-uninstall':
// Undo anything you did in the --squirrel-install and
// --squirrel-updated handlers
// Remove desktop and start menu shortcuts
spawnUpdate(['--removeShortcut', exeName]);
setTimeout(app.quit, 1000);
return true;
case '--squirrel-obsolete':
// This is called on the outgoing version of your app before
// we update to the new version - it's the opposite of
// --squirrel-updated
app.quit();
return true;
}
};
function init(){
let configPath = path.join(APP_ROOT_PATH, 'config.json')
if (!fs.existsSync(configPath)){
config = {
"defaultCharacterPath": {
"value": path.join(__dirname, "defaultCharacter.json"),
"UIType": "path"
},
"forcePowerTreesPath": {
"value": path.join(APP_ROOT_PATH, "force_power_trees"),
"UIType": "path"
},
"talentTreesPath": {
"value": path.join(APP_ROOT_PATH, "talent_trees"),
"UIType": "path"
},
"characterSavedPath":{
"value": path.join(APP_ROOT_PATH, "saved_characters"),
"UIType": "path"
},
"discordToken": {
"value": "",
"UIType": "string"
}
}
writeJSONToDisk(configPath, config)
}
else {
config = filenameToJson(configPath)
}
if (!fs.existsSync(config["characterSavedPath"]["value"])){
fs.mkdirSync(config["characterSavedPath"]["value"]);
}
if (!fs.existsSync(config["forcePowerTreesPath"]["value"])){
fs.mkdirSync(config["forcePowerTreesPath"]["value"]);
}
if (!fs.existsSync(config["talentTreesPath"]["value"])){
fs.mkdirSync(config["talentTreesPath"]["value"]);
}
discordInterface = new DiscordInterface(config['discordToken']["value"])
}
function createWindow () {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
},
icon: path.join(__dirname, "images/EOTEAppIcon.ico")
})
mainWindow.setMenuBarVisibility(false)
mainWindow.maximize();
mainWindow.loadFile(path.join(__dirname, 'template/main.html'))
mainWindow.on("ready-to-show", () => {
mainWindow.show();
})
}
app.whenReady().then(() => {
protocol.interceptFileProtocol('static-image', (request, callback) => {
const url = request.url.substr(15) /* all urls start with 'file://' */
callback({ path: path.normalize(`${__dirname}/${url}`)})
}, (err) => {
if (err) console.error('Failed to register protocol')
})
init()
createWindow()
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
ipcMain.handle("getAllData", async (event) => {
return {
"characters": await getCharactersFromDisk(),
"defaultCharacter": await getDefaultCharacterFromDisk(),
"forcePowerTrees": await getForcePowerTreesFromDisk(),
"talentTrees": await getTalentTreesFromDisk(),
}
})
ipcMain.handle("saveCharacter", (event, character) => {
return writeCharacterToDisk(character)
})
ipcMain.handle("deleteCharacter", (event, character) => {
return deleteCharacterFromDisk(character)
})
ipcMain.handle("getGlobalSettings", (event) => {
return getGlobalSettingsFromDisk()
})
ipcMain.handle("setGlobalSettings", (event, globalSettings) => {
if (discordInterface.getToken() != globalSettings["discordToken"]){
discordInterface.logOut()
discordInterface = new DiscordInterface(globalSettings["discordToken"]["value"])
}
return writeGlobalSettingstoDisk(globalSettings)
})
ipcMain.handle("sendMessageDiscord", (event, channel, message) => {
if (!discordInterface.discordConnected){
throw new Error("Discord is not connected.")
}
let promise = discordInterface.sendMessageToChannel(channel, message);
return promise.then(() => true).catch((err) => {
throw new Error(err.message)
})
})
ipcMain.handle("chooseImage", (event) => {
let result = dialog.showOpenDialogSync({
properties: ["openFile"],
filters: [{ name: "Images", extensions: ["png","jpg","jpeg"] }]
});
const base64 = fs.readFileSync(result[0]).toString('base64');
let extname = path.extname(result[0]).substring(1)
return `data:image/${extname};base64,${base64}`
})
ipcMain.handle("importCharacter", (event, contents) => {
let result = chooseFilePath([
{ name: "json", extensions: ["json"] }
])
if (!result){
throw "File Not Specified."
}
return filenameToJson(result)
})
ipcMain.handle("chooseFilePath", (event, contents) => {
let result = chooseFilePath([
{ name: "json", extensions: ["json"] }
])
return result
})
ipcMain.handle("exportCharacter", (event, contents) => {
let result = dialog.showSaveDialogSync({
defaultPath: `~/${contents["meta"]["filename"]}.json`,
filters: [{ name: "json", extensions: ["json"] }]
});
contents["meta"]["filename"] = result.replace(/^.*[\\\/]/, '')
let pos = contents["meta"]["filename"].lastIndexOf('.');
contents["meta"]["filename"] = contents["meta"]["filename"].substring(0,pos);
writeJSONToDisk(result, contents)
})
ipcMain.handle("doesCharacterFileNameExist", (event, character) => {
return fs.existsSync(path.join(config["characterSavedPath"]["value"],`${character["meta"]["filename"]}.json`))
})
ipcMain.handle("addTalentTrees", async (event, character) => {
await chooseTreeFilePath(config["talentTreesPath"]["value"])
return getTalentTreesFromDisk()
})
ipcMain.handle("addForceTrees", async (event, character) => {
await chooseTreeFilePath(config["forcePowerTreesPath"]["value"])
return getForcePowerTreesFromDisk()
})
async function chooseTreeFilePath(writeFolderPath){
let result = chooseFilePath([
{ name: "json", extensions: ["json"] },
{ name: "zip", extensions: ["zip"] }
])
let file_obj = path.parse(result);
let extension = file_obj.ext
if ( extension == '.json' ) {
let filejson = filenameToJson(result)
writeJSONToDisk(path.join(writeFolderPath, file_obj.base), filejson)
}
else if ( extension == '.zip' ) {
const zipfilecontent = fs.readFileSync(result)
const zipout = await jszip.loadAsync(zipfilecontent)
for (let key of Object.keys(zipout.files)){
const item = zipout.files[key]
if (!item.dir){
const filepath = path.join(writeFolderPath, item.name)
fs.writeFileSync(filepath, Buffer.from(await item.async('arraybuffer')))
}
}
}
}
function chooseFilePath(filters) {
let result = dialog.showOpenDialogSync({
properties: ["openFile"],
filters: filters
});
return result ? result[0] : result
}
function getDefaultCharacterFromDisk() {
if (!fs.existsSync(config["defaultCharacterPath"]["value"])){
return null
}
return filenameToJson(config["defaultCharacterPath"]["value"])
}
function getCharactersFromDisk() {
let characterFilenames = getFolderJSONFilenames(config["characterSavedPath"]["value"]);
let characters = filenamesToJson(characterFilenames)
return characters
}
function getForcePowerTreesFromDisk() {
let forcePowerTreeFilenames = getFolderJSONFilenames(config["forcePowerTreesPath"]["value"]);
let forcePowerTrees = filenamesToJson(forcePowerTreeFilenames)
return forcePowerTrees
}
function getTalentTreesFromDisk() {
let talentTreeFilenames = getFolderJSONFilenames(config["talentTreesPath"]["value"]);
let talentTrees = filenamesToJson(talentTreeFilenames)
return talentTrees
}
function writeCharacterToDisk(character) {
writeJSONToDisk(path.join(config["characterSavedPath"]["value"],`${character["meta"]["filename"]}.json`), character)
}
function deleteCharacterFromDisk(character) {
let deletePath = path.join(config["characterSavedPath"]["value"],`${character["meta"]["filename"]}.json`)
if( fs.existsSync(deletePath) ){
fs.unlinkSync(deletePath)
}
}
function getGlobalSettingsFromDisk() {
return filenameToJson(path.join(APP_ROOT_PATH, 'config.json'))
}
function writeGlobalSettingstoDisk(globalSettings) {
config = globalSettings
writeJSONToDisk(path.join(APP_ROOT_PATH, 'config.json'), globalSettings)
}
function getFolderJSONFilenames(folder){
if (!fs.existsSync(folder)){
return []
}
let files = fs.readdirSync(folder)
.map(file => folder + "/" + file)
.filter(file => fs.lstatSync(file).isFile() && path.extname(file) == '.json');
return files
}
function filenamesToJson(filenames){
let jsonArr = filenames.map(file => {
return filenameToJson(file);
})
return jsonArr;
}
function filenameToJson(filename) {
let jsonData = fs.readFileSync(filename);
return JSON.parse(jsonData);
}
function writeJSONToDisk(filename, jsonObj) {
var characterstring = JSON.stringify(jsonObj)
fs.writeFileSync(filename, characterstring, 'utf8');
}