-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
194 lines (168 loc) · 4.4 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
const electron = require('electron')
const app = electron.app
const BrowserWindow = electron.BrowserWindow
const ipcMain = electron.ipcMain;
const path = require('path')
const url = require('url')
const glob = require("glob");
const fs = require("fs");
const exec = require("child_process").execFile;
let mainWindow
let steamConfigPath = process.env.APPDATA + '/SoulExplorer/steamGames.config'
let getSteamGames = function (src, callback) {
glob(src + '/steamapps/common/*', callback);
}
let checkGame = function (src, callback) {
glob(src + '/*.exe', callback);
}
function createWindow () {
initSave();
mainWindow = new BrowserWindow({
width: 1600,
height: 1200,
frame: false,
webPreferences: {
nodeIntegration: true
}
})
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'dist/SoulExplorer/index.html'),
protocol: 'file:',
slashes: true
}))
mainWindow.webContents.openDevTools()
mainWindow.on('closed', function () {
mainWindow = null
})
}
app.on('ready', createWindow)
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', function () {
if (mainWindow === null) {
createWindow()
}
})
function initSave() {
let dir = process.env.APPDATA + '/SoulExplorer';
try {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
console.log('Save Directory created')
} else {
console.log('Save Directory already exists')
}
} catch (err) {
console.log(err);
}
}
ipcMain.on('exit', (event, args) => {
mainWindow.close();
})
ipcMain.on('minimize', (event, args) => {
mainWindow.minimize();
})
ipcMain.on('maximize', (event, args) => {
if (mainWindow.isMaximized()) {
mainWindow.unmaximize();
} else {
mainWindow.maximize();
}
})
ipcMain.on('openNewLibrary', (event, args) => {
let folderPath = electron.dialog.showOpenDialogSync({
properties: ['openDirectory']
});
event.sender.send('addSteamLibrary', {folderPath: folderPath, folderNumber: args});
})
ipcMain.on('scanSteamLibraries', (event, args) => {
let libs = args;
let promises = [];
for(let lib of libs) {
promises.push(scanSteamLibrary(lib));
}
Promise.all(promises).then(
function (value) {
console.log('Scanned Steam Games');
event.sender.send('scannedSteamLibraries', value);
}
);
})
ipcMain.on('saveSteamLibrary', (event, args) => {
/*if(!fs.existsSync(steamConfigPath)) {
fs.writeFileSync(steamConfigPath, [])
}*/
let libraries = JSON.stringify(args);
fs.writeFileSync(steamConfigPath, libraries);
})
ipcMain.on('loadSteamLibraries', (event, args) => {
if (fs.existsSync(steamConfigPath)) {
let configFile = fs.readFileSync(steamConfigPath);
let libraries = JSON.parse(configFile);
event.sender.send('setSteamLibraries', libraries);
}
})
ipcMain.on('startExe', (event, args) => {
console.log('Executing: ' + args);
let child = exec(args, function (err, data) {
console.log(err);
console.log(data.toString());
})
})
function checkSteamGame(game) {
return new Promise((res, rej) => {
checkGame(game, (err, resp) => {
if (err) {
rej(err)
} else {
res(resp)
}
})
})
}
function getAllSteamGames(library) {
return new Promise((res, rej) => {
getSteamGames(library.filePath, (err, resp) => {
if (err) {
rej(err)
} else {
res(resp);
}
})
})
}
function scanSteamLibrary(library) {
return new Promise((res, rej) => {
library.games = [];
getAllSteamGames(library).then(
function (lib) {
let formatPath = library.filePath[0].replace(/\\/g, '/');
let promises = [];
for (let game of lib) {
promises.push(
checkSteamGame(game).then(
function (value) {
let exes = [];
let name = game.replace(formatPath + '/steamapps/common/', '');
for (let exe of value) {
let exeName = exe.replace(formatPath + '/steamapps/common/' + name + '/', '');
exes.push({filePath: exe, label: exeName})
}
library.games.push({name: name, folderPath: game, exes: exes});
}
)
)
}
Promise.all(promises).then(
function (value) {
library.scanned = true;
res(library);
}
)
}
)
})
}