-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
120 lines (103 loc) · 2.88 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
const { app, BrowserWindow, ipcMain } = require('electron')
const Store = require('electron-store')
const path = require('path')
let win
const { validate } = require('./helpers/authentication.js')
const { launch } = require('./helpers/launch.js')
async function createWindow () {
win = new BrowserWindow({
minWidth: 1280,
minHeight: 720,
webPreferences: {
nodeIntegration: true,
preload: path.resolve(__dirname, 'app', 'renderer', 'scripts', 'preload.js')
},
frame: false
})
win.setAspectRatio(16/9)
//win.setMenu(null)
await stayLoggedIn() ? win.loadFile(path.join(__dirname, 'app', 'renderer', 'views', 'main.html')) : win.loadFile(path.join(__dirname, 'app', 'renderer', 'views', 'index.html'))
}
app.whenReady().then(() => {
createWindow()
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
const credentialData = new Store({
name: 'authentication',
schema: {
clientToken: {
type: 'string'
},
accessToken: {
type: 'string'
},
username: {
type: 'string'
},
selectedProfile: {
type: 'object'
}
},
encryptionKey: 'encryption1234'
})
const userPreferences = new Store({
name: 'user-preferences',
schema: {
stayLoggedIn: {
type: 'boolean',
default: false
},
minRam: {
type: 'number',
default: 2048
},
maxRam: {
type: 'number',
default: 4096
},
forgeVersion: {
type: 'string'
},
minecraftVersion: {
type: 'string'
}
}
})
const storages = [credentialData, userPreferences]
ipcMain.handle('set', async (e, key, value, option) => {
storages[option].set(key, value)
})
ipcMain.handle('get', async (e, key, option) => {
const data = await storages[option].get(key)
return data
})
ipcMain.handle('minimize', async() => {
win.minimize()
})
ipcMain.handle('maximize', async() => {
win.isMaximized() ? win.restore() : win.maximize()
})
ipcMain.handle('launch', async() => {
const newAccessToken = await launch(credentialData.get('clientToken'), credentialData.get('accessToken'), credentialData.get('selectedProfile'), userPreferences.get('maxRam'), userPreferences.get('minRam'))
credentialData.set('accessToken', newAccessToken) // apply new accessToken
})
async function stayLoggedIn() {
const persist = await userPreferences.get('stayLoggedIn')
const clientToken = await credentialData.get('clientToken')
const accessToken = await credentialData.get('accessToken')
if (!clientToken || !accessToken || !persist) return false
const login = await validate(accessToken, clientToken)
if (!login) {
return false
}
return true
}