GrassClipper/resources/js/helpers.js

123 lines
2.9 KiB
JavaScript
Raw Normal View History

/**
* Get configuration
*
* @returns {Promise<string>}
*/
2022-05-03 05:44:57 +00:00
async function getCfg() {
const defaultConf = {
gameexe: '',
serverFolder: '',
lastConnect: '',
enableKillswitch: false,
serverLaunchPanel: false,
language: 'en',
useHttps: true,
grasscutterBranch: '',
}
const cfgStr = await Neutralino.storage.getData('config').catch(e => {
// The data isn't set, so this is our first time opening
2022-05-03 05:44:57 +00:00
Neutralino.storage.setData('config', JSON.stringify(defaultConf))
2022-05-03 05:44:57 +00:00
// Show the first time notice if there is no config
document.querySelector('#firstTimeNotice').style.display = 'block'
})
2022-05-03 05:44:57 +00:00
const config = cfgStr ? JSON.parse(cfgStr) : defaultConf
2022-05-03 05:44:57 +00:00
return config
}
/**
* Get the list of favorite IPs
*
* @returns {Promise<string[]>}
*/
2022-05-03 05:44:57 +00:00
async function getFavIps() {
const ipStr = await Neutralino.storage.getData('favorites').catch(e => {
// The data isn't set, so this is our first time opening
2022-05-03 05:44:57 +00:00
Neutralino.storage.setData('favorites', JSON.stringify([]))
})
2022-05-03 05:44:57 +00:00
const ipArr = ipStr ? JSON.parse(ipStr) : []
2022-05-03 05:44:57 +00:00
return ipArr
}
async function proxyIsInstalled() {
2022-05-03 05:44:57 +00:00
// Check if the proxy server is installed
const curDirList = await filesystem.readDirectory(NL_CWD)
2022-05-03 05:44:57 +00:00
if (curDirList.find(f => f.entry === 'ext')) {
const extFiles = await filesystem.readDirectory(NL_CWD + '/ext')
2022-05-03 05:44:57 +00:00
if (extFiles.find(f => f.entry === 'mitmdump.exe')) {
return true
}
}
2022-05-03 05:44:57 +00:00
return false
}
2022-04-30 08:56:00 +00:00
async function checkForUpdates() {
2022-05-03 05:44:57 +00:00
const url = 'https://api.github.com/repos/Grasscutters/GrassClipper/releases/latest'
2022-04-30 08:56:00 +00:00
2022-05-03 05:44:57 +00:00
const { data } = await axios.get(url)
const latest = data.tag_name
2022-04-30 08:56:00 +00:00
2022-05-03 05:44:57 +00:00
return latest
2022-04-30 08:56:00 +00:00
}
async function displayUpdate() {
2022-05-03 05:44:57 +00:00
const latest = await checkForUpdates()
const versionDisplay = document.querySelector('#newestVersion')
const notif = document.querySelector('#downloadNotif')
2022-04-30 08:56:00 +00:00
2022-05-03 05:44:57 +00:00
if (latest === `v${NL_APPVERSION}`) return
2022-04-30 08:56:00 +00:00
2022-05-03 05:44:57 +00:00
versionDisplay.innerText = latest
2022-04-30 08:56:00 +00:00
2022-05-03 05:44:57 +00:00
notif.classList.add('displayed')
2022-04-30 08:56:00 +00:00
2022-05-03 05:44:57 +00:00
setTimeout(() => {
notif.classList.remove('displayed')
}, 5000)
2022-04-30 08:56:00 +00:00
}
async function openLatestDownload() {
2022-05-03 05:44:57 +00:00
const downloadLink = 'https://github.com/Grasscutters/GrassClipper/releases/latest/'
2022-04-30 08:56:00 +00:00
2022-05-03 05:44:57 +00:00
Neutralino.os.open(downloadLink)
2022-04-30 08:56:00 +00:00
}
async function openGameFolder() {
2022-05-03 05:44:57 +00:00
const config = await getCfg()
const folder = config.gameexe.match(/.*\\/g, '')[0]
2022-05-03 05:44:57 +00:00
openInExplorer(folder)
}
async function openGrasscutterFolder() {
2022-05-03 05:44:57 +00:00
const config = await getCfg()
const folder = config.serverFolder.match(/.*\\|.*\//g, '')[0]
2022-05-03 05:44:57 +00:00
openInExplorer(folder)
}
/**
* Minimize the window
*/
2022-05-03 05:44:57 +00:00
function minimizeWin() {
console.log('min')
Neutralino.window.minimize()
}
/**
* Close the window
*/
function closeWin() {
2022-05-03 05:44:57 +00:00
console.log('close')
Neutralino.app.exit()
2022-05-03 05:44:57 +00:00
window.close()
}