-
Notifications
You must be signed in to change notification settings - Fork 0
/
preload.js
103 lines (86 loc) · 2.98 KB
/
preload.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
const {contextBridge, ipcRenderer} = require('electron')
const fs = require('fs')
const path = require('path')
const {promisify} = require('util')
var parser = require('osu-parser');
contextBridge.exposeInMainWorld('electronAPI', {
handleUndo: (handler) => ipcRenderer.on('undo', handler), handleRedo: (handler) => ipcRenderer.on('redo', handler),
async fileExists(file) {
return promisify(fs.exists)(file)
},
async loadBeatmapFiles(dir) {
const ret = {
images: [],
beatmaps: []
}
async function loadDirectory(directory) {
try {
const files = await promisify(fs.readdir)(directory)
await Promise.all(files.map(async filename => {
const currentPath = path.join(directory, filename)
if (fs.lstatSync(currentPath).isDirectory()) {
await loadDirectory(currentPath)
} else if (filename.endsWith('.png') || filename.endsWith('.jpg')) {
const data = promisify(fs.readFile)(currentPath)
ret.images.push({filename: path.relative(dir, currentPath), data})
} else if (filename.endsWith('.osu')) {
const beatmap = await promisify(parser.parseFile)(currentPath)
ret.beatmaps.push(beatmap)
}
}))
} catch (e) {
console.log(e)
}
}
await loadDirectory(dir)
return ret
},
writeFile(file, contents) {
return new Promise((resolve, reject) => {
fs.writeFile(file, contents, (err) => {
if (err) reject(err)
else resolve()
})
})
},
readTextFile(file) {
return new Promise((resolve, reject) => {
fs.readFile(file, {encoding: 'utf-8'}, (err, data) => {
if (err) reject(err)
else resolve(data)
})
})
},
readFile(file) {
return new Promise((resolve, reject) => {
fs.readFile(file, (err, data) => {
if (err) reject(err)
else resolve(data)
})
})
},
isDir(file) {
return promisify(fs.lstat)(file).then(it => it.isDirectory())
},
readDir(dir) {
return promisify(fs.readdir)(dir)
},
async selectDirectory(defaultPath) {
return ipcRenderer.invoke('select-directory', defaultPath)
},
async saveFileDialog(opts) {
return ipcRenderer.invoke('save-file-dialog', opts || {})
},
async openFileDialog(opts) {
return ipcRenderer.invoke('open-file-dialog', opts || {})
},
getOsuSongsDirectory() {
switch (process.platform) {
case 'win32':
return path.join(process.env.LOCALAPPDATA, 'osu!', 'Songs');
case 'darwin':
return '';
}
return undefined;
}
})