GrassClipper/resources/js/gcdownloader.js

120 lines
4.0 KiB
JavaScript
Raw Normal View History

2022-05-02 06:32:51 +00:00
async function setDownloadButtonsToLoading() {
2022-05-03 05:48:03 +00:00
const stableBtn = document.querySelector('#stableInstall')
const devBtn = document.querySelector('#devInstall')
2022-05-02 06:32:51 +00:00
2022-05-03 05:48:03 +00:00
stableBtn.innerText = localeObj.gcScriptRunning || 'Running...'
2022-05-02 06:32:51 +00:00
2022-05-03 05:48:03 +00:00
devBtn.innerText = localeObj.gcScriptRunning || 'Running...'
2022-05-02 06:32:51 +00:00
2022-05-03 05:48:03 +00:00
// Set btns to disabled
stableBtn.disabled = true
stableBtn.classList.add('disabled')
2022-05-02 06:32:51 +00:00
2022-05-03 05:48:03 +00:00
devBtn.disabled = true
devBtn.classList.add('disabled')
2022-05-02 06:32:51 +00:00
}
async function resetDownloadButtons() {
2022-05-03 05:48:03 +00:00
const stableBtn = document.querySelector('#stableInstall')
const devBtn = document.querySelector('#devInstall')
2022-05-02 06:32:51 +00:00
2022-05-03 05:48:03 +00:00
stableBtn.innerText = localeObj.stableInstall || 'Download'
devBtn.innerText = localeObj.devInstall || 'Download'
2022-05-02 06:32:51 +00:00
2022-05-03 05:48:03 +00:00
// Set btns to enabled
stableBtn.disabled = false
stableBtn.classList.remove('disabled')
2022-05-02 06:32:51 +00:00
2022-05-03 05:48:03 +00:00
devBtn.disabled = false
devBtn.classList.remove('disabled')
2022-05-02 06:32:51 +00:00
}
2022-05-08 03:05:01 +00:00
async function downloadDataFiles(branch) {
2022-05-03 05:48:03 +00:00
const config = await getCfg()
2022-05-02 02:09:10 +00:00
2022-05-03 05:48:03 +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 }))
2022-05-02 05:19:32 +00:00
2022-05-03 05:48:03 +00:00
// 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 }))
2022-05-02 05:19:32 +00:00
2022-05-03 05:48:03 +00:00
const serverFolderFixed = config.serverFolder.match(/.*\\|.*\//g, '')[0].replace(/\//g, '\\')
2022-05-02 05:19:32 +00:00
2022-05-03 05:48:03 +00:00
// Ensure data and key folders exist
2022-05-06 04:56:08 +00:00
console.log(config.serverFolder)
console.log(serverFolderFixed)
2022-05-08 03:05:01 +00:00
2022-05-03 05:48:03 +00:00
await Neutralino.os.execCommand(`mkdir ${serverFolderFixed}\\data`)
await Neutralino.os.execCommand(`mkdir ${serverFolderFixed}\\keys`)
2022-05-08 03:05:01 +00:00
2022-05-03 05:48:03 +00:00
// Download data files
for (const o of dataList) {
const folder = 'data'
2022-05-08 03:05:01 +00:00
const e = await Neutralino.os.execCommand(`powershell Invoke-WebRequest -Uri ${o.url} -OutFile "${serverFolderFixed}\\${folder}\\${o.filename}"`)
console.log(e)
2022-05-03 05:48:03 +00:00
}
2022-05-08 03:05:01 +00:00
2022-05-03 05:48:03 +00:00
// Download key files
for (const o of keyList) {
const folder = 'keys'
2022-05-08 03:05:01 +00:00
const e = await Neutralino.os.execCommand(`powershell Invoke-WebRequest -Uri ${o.url} -OutFile "${serverFolderFixed}\\${folder}\\${o.filename}"`)
console.log(e)
}
}
async function downloadGC(branch) {
const config = await getCfg()
// Set current installation in config
config.grasscutterBranch = branch
// Set gc path for people with launcher enabled
config.serverFolder = `${NL_CWD}\\gc-${branch}\\grasscutter.jar`
// Enable server launcher
config.serverLaunchPanel = true
Neutralino.storage.setData('config', JSON.stringify(config))
setDownloadButtonsToLoading()
// Download data files
downloadDataFiles(branch)
// External service that allows un-authed artifact downloading
let artiUrl = `https://nightly.link/Grasscutters/Grasscutter/workflows/build/${branch}/Grasscutter.zip`
await axios.get(artiUrl).catch(e => {
// Fallback link if artifacts are not being uploaded
artiUrl = 'https://nightly.link/Grasscutters/Grasscutter/actions/runs/2284467925/Grasscutter.zip'
})
// Keystore for branch (since they can differ)
const keystoreUrl = `https://github.com/Grasscutters/Grasscutter/raw/${branch}/keystore.p12`
2022-05-02 05:19:32 +00:00
2022-05-03 05:48:03 +00:00
// Run installer
createCmdWindow(`.\\scripts\\gc_download.cmd ${artiUrl} ${keystoreUrl} ${branch}`)
2022-05-02 02:09:10 +00:00
2022-05-03 05:48:03 +00:00
// Fix buttons
resetDownloadButtons()
2022-05-02 06:32:51 +00:00
2022-05-03 05:48:03 +00:00
// Display folder after saving config
displayServerFolder()
enableServerButton()
displayServerLaunchSection()
2022-05-08 00:15:54 +00:00
}
async function downloadResources() {
const config = await getCfg()
const serverFolderFixed = config.serverFolder.match(/.*\\|.*\//g, '')[0].replace(/\//g, '\\')
// Dont bother with data or keys, just want straight resources
createCmdWindow(`.\\scripts\\resources_download.cmd "${serverFolderFixed}"`)
2022-05-02 02:09:10 +00:00
}