-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
151 lines (134 loc) · 4.06 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
const { app, BrowserWindow, ipcMain, shell, dialog} = require('electron')
const path = require('path')
const http = require('http')
const fs = require('fs-extra')
const isDevelopment = process.env.NODE_ENV === 'development'
function pollServer(url) {
return new Promise(resolve => {
const request = http.get(url, (res) => {
resolve(true);
request.abort(); // We don't need to read the response, just knowing the server is up is enough
});
request.on('error', (err) => {
setTimeout(() => {
console.log(`Polling for dev server:`, url);
resolve(pollServer(url)); // Retry after a delay
}, 1000);
});
});
}
/**
* Create and return the {@link BrowserWindow} instance.
* @returns {Promise<Electron.CrossProcessExports.BrowserWindow>}
*/
const createWindow = async () => {
const win = new BrowserWindow({
show: false,
width: 720,
height: 420,
minWidth: 720,
minHeight: 420,
backgroundColor: '#184a73',
webPreferences: {
contextIsolation: true,
sandbox: false,
preload: isDevelopment
? path.join(__dirname, 'src', 'main', 'preload.js')
: path.join(__dirname, 'preload.js')
}
})
const startUrl =
isDevelopment
? `http://127.0.0.1:3000`
: `file://${path.join(__dirname, 'index.html')}`
win.removeMenu()
win.once('ready-to-show', () => {
win.show()
})
if (isDevelopment) {
await pollServer(startUrl)
}
await win.loadURL(startUrl)
return win
}
/**
* Wait for app ready and initialize the window.
* @returns {Promise<void>}
*/
const start = async () => {
await app.whenReady()
let window = await createWindow()
let activating = false
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', async () => {
if (!activating && window.isDestroyed()) {
activating = true
window = await createWindow()
activating = false
}
})
const {exportSaveBlock} = (await import('./src/main/utils/export.mjs'))
ipcMain.on('load-save', async (event, file) => {
const inputFileExists = await fs.pathExists(file.path)
if (!inputFileExists) {
event.reply('convert-error', `Input file does not exist: ${file.path}`)
return
}
try {
// support loading .sav or extracting save from .sta
const saveBlock = file.name.endsWith('.sta')
? await exportSaveBlock(file.path)
: await fs.readFile(file.path)
try {
const {Gen3Save} = (await import('./src/main/pkmn/gen3/Gen3Save.mjs'))
const gen3Save = new Gen3Save({buffer: saveBlock})
event.reply('loaded-save', gen3Save)
// @todo remove this eventually
if (process.env.NODE_ENV === 'development') {
await fs.writeJson('save.json', gen3Save)
}
} catch (err) {
event.reply('error', `Failed to decode save`)
}
} catch (err) {
event.reply('error', `Error loading save: ${err.message}`)
console.error(err)
}
})
ipcMain.on('export-save', async (event, inputFile) => {
const {filePath} = await dialog.showSaveDialog({
title: 'Export Pokemon .sav file',
defaultPath: inputFile.path.replace('.sta', '.sav')
})
if (!filePath) {
console.log('Canceled file save')
return
}
try {
const inputFileExists = await fs.pathExists(inputFile.path)
if (!inputFileExists) {
event.reply('error', `Input file does not exist: ${inputFile.path}`)
return
}
const outputFilePath = path.resolve(filePath)
try {
console.log('Exporting save:', outputFilePath)
await exportSaveBlock(inputFile.path, {outputFilePath})
shell.showItemInFolder(outputFilePath)
event.reply('convert-success', outputFilePath)
} catch (err) {
event.reply('error', `Error exporting save block: ${err.message}`)
console.error(err)
}
} catch (err) {
event.reply('error', `Unknown error: ${err.message}`)
console.error('Error converting:', err)
}
})
}
// run the start func
start()