GrassClipper/resources/js/gcdownloader.js

149 lines
4.9 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...'
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-08 03:49:45 +00:00
debug.log('Set download buttons to loading')
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-08 03:49:45 +00:00
debug.log('Reset download buttons')
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-08 03:49:45 +00:00
if (!branch) {
debug.warn('Branch not specified')
branch = config.grasscutterBranch || 'development'
}
debug.log('Using branch: ' + branch)
2022-05-08 03:08:55 +00:00
setDownloadButtonsToLoading()
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-08 03:49:45 +00:00
debug.log('Downloaded data files')
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-08 03:49:45 +00:00
debug.log('Downloaded key files')
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-08 03:49:45 +00:00
debug.log('Server folder fixed: ' + 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:49:45 +00:00
debug.log('Created data and keys folders')
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}"`)
debug.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}"`)
debug.log(e)
2022-05-08 03:08:55 +00:00
}
// Fix buttons
resetDownloadButtons()
2022-05-08 03:05:01 +00:00
}
async function downloadGC(branch) {
const config = await getCfg()
// Set current installation in config
config.grasscutterBranch = branch
2022-05-08 03:49:45 +00:00
debug.log('Branch set to: ' + branch)
2022-05-08 03:05:01 +00:00
// Set gc path for people with launcher enabled
config.serverFolder = `${NL_CWD}\\gc-${branch}\\grasscutter.jar`
2022-05-08 03:49:45 +00:00
debug.log('Server folder automatically set to: ' + config.serverFolder)
2022-05-08 03:05:01 +00:00
// Enable server launcher
config.serverLaunchPanel = true
Neutralino.storage.setData('config', JSON.stringify(config))
// 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
2022-05-08 03:49:45 +00:00
debug.warn('Artifacts not available for latest, falling back...')
2022-05-08 03:05:01 +00:00
artiUrl = 'https://nightly.link/Grasscutters/Grasscutter/actions/runs/2284467925/Grasscutter.zip'
})
2022-05-08 03:49:45 +00:00
debug.log('Artifact URL: ' + artiUrl)
2022-05-08 03:05:01 +00:00
// Keystore for branch (since they can differ)
const keystoreUrl = `https://github.com/Grasscutters/Grasscutter/raw/${branch}/keystore.p12`
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-08 03:49:45 +00:00
debug.log('Created installer window')
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()
2022-05-08 05:51:17 +00:00
let serverFolderFixed = config.serverFolder.match(/.*\\|.*\//g, '')[0].replace(/\//g, '\\')
// Remove trailing slash, it's important here
serverFolderFixed = serverFolderFixed.endsWith('\\') ? serverFolderFixed.slice(0, -1) : serverFolderFixed
2022-05-08 00:15:54 +00:00
2022-05-08 03:49:45 +00:00
debug.log('Server folder fixed: ' + serverFolderFixed)
2022-05-08 00:15:54 +00:00
// Dont bother with data or keys, just want straight resources
createCmdWindow(`.\\scripts\\resources_download.cmd "${serverFolderFixed}"`)
2022-05-08 03:49:45 +00:00
debug.log('Created resources window')
2022-05-02 02:09:10 +00:00
}