GrassClipper/resources/js/gcdownloader.js

61 lines
2.4 KiB
JavaScript
Raw Normal View History

2022-05-02 02:09:10 +00:00
async function clearGCInstallation() {
Neutralino.os.execCommand(`del /s /q "./gc"`)
}
async function downloadGC(branch) {
const config = await getCfg()
// If we are pulling from a new branch, delete the old installation
if (config.grasscutterBranch !== branch) await clearGCInstallation()
2022-05-02 05:19:32 +00:00
// Set current installation in config
config.grasscutterBranch = branch
// Set gc path for people with launcher enabled
config.serverFolder = `${NL_CWD}/gc-${branch}/grasscutter.jar`
Neutralino.storage.setData('config', JSON.stringify(config))
2022-05-02 02:09:10 +00:00
// Keystore for branch (since they can differ)
const keystoreUrl = `https://github.com/Grasscutters/Grasscutter/raw/${branch}/keystore.p12`
// External service that allows un-authed artifact downloading
const artiUrl = `https://nightly.link/Grasscutters/Grasscutter/workflows/build/${branch}/Grasscutter.zip`
2022-05-02 05:19:32 +00:00
// For data files
const dataFiles = await axios.get(`https://api.github.com/repos/Grasscutters/Grasscutter/contents/data?ref=${branch}`)
const dataList = dataFiles.data
.map(file => ({ path: file.path, filename: file.name }))
.map(o => ({ url: `https://raw.githubusercontent.com/Grasscutters/Grasscutter/${branch}/${o.path}`, filename: o.filename }))
// For key files
const keyFiles = await axios.get(`https://api.github.com/repos/Grasscutters/Grasscutter/contents/keys?ref=${branch}`)
const keyList = keyFiles.data
.map(file => ({ path: file.path, filename: file.name }))
.map(o => ({ url: `https://raw.githubusercontent.com/Grasscutters/Grasscutter/${branch}/${o.path}`, filename: o.filename }))
const serverFolderFixed = config.serverFolder.match(/.*\\|.*\//g, '')[0].replace(/\//g, '\\')
2022-05-02 05:23:33 +00:00
// Ensure data and key folders exist
await Neutralino.os.execCommand(`mkdir ${serverFolderFixed}\\data`)
await Neutralino.os.execCommand(`mkdir ${serverFolderFixed}\\keys`)
2022-05-02 05:19:32 +00:00
// Download data files
for (const o of dataList) {
const folder = 'data'
2022-05-02 05:23:33 +00:00
await Neutralino.os.execCommand(`powershell Invoke-WebRequest -Uri ${o.url} -OutFile "${serverFolderFixed}\\${folder}\\${o.filename}"`)
2022-05-02 05:19:32 +00:00
}
2022-05-02 05:23:33 +00:00
// Download key files
2022-05-02 05:19:32 +00:00
for (const o of keyList) {
const folder = 'keys'
2022-05-02 05:23:33 +00:00
await Neutralino.os.execCommand(`powershell Invoke-WebRequest -Uri ${o.url} -OutFile "${serverFolderFixed}\\${folder}\\${o.filename}"`)
2022-05-02 05:19:32 +00:00
}
2022-05-02 02:09:10 +00:00
// Run installer
2022-05-02 04:05:24 +00:00
createCmdWindow(`.\\scripts\\gc_download.cmd ${artiUrl} ${keystoreUrl} ${branch}`)
2022-05-02 02:09:10 +00:00
2022-05-02 02:39:47 +00:00
// Display folder after saving config
displayServerFolder()
2022-05-02 02:09:10 +00:00
}