GrassClipper/resources/js/helpers.js

200 lines
4.8 KiB
JavaScript
Raw Normal View History

/**
* Get configuration
*
* @returns {Promise<string>}
*/
2022-05-03 05:44:57 +00:00
async function getCfg() {
2022-05-03 05:48:03 +00:00
const defaultConf = {
gameexe: '',
serverFolder: '',
lastConnect: '',
enableKillswitch: false,
serverLaunchPanel: false,
language: 'en',
useHttps: true,
2022-05-08 03:49:45 +00:00
debug: true,
2022-05-03 05:48:03 +00:00
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:48:03 +00:00
Neutralino.storage.setData('config', JSON.stringify(defaultConf))
2022-05-03 05:48:03 +00:00
// Show the first time notice if there is no config
document.querySelector('#firstTimeNotice').style.display = 'block'
2022-05-08 03:49:45 +00:00
debug.warn('First time opening')
2022-05-03 05:48:03 +00:00
})
2022-05-03 05:48:03 +00:00
const config = cfgStr ? JSON.parse(cfgStr) : defaultConf
2022-05-03 05:48:03 +00:00
return config
}
/**
* Get the list of favorite IPs
*
* @returns {Promise<string[]>}
*/
2022-05-03 05:44:57 +00:00
async function getFavIps() {
2022-05-03 05:48:03 +00:00
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:48:03 +00:00
Neutralino.storage.setData('favorites', JSON.stringify([]))
2022-05-08 03:49:45 +00:00
debug.warn('No favorites set')
2022-05-03 05:48:03 +00:00
})
2022-05-08 03:49:45 +00:00
debug.log('Favorites:', ipStr)
2022-05-03 05:48:03 +00:00
const ipArr = ipStr ? JSON.parse(ipStr) : []
2022-05-03 05:48:03 +00:00
return ipArr
}
async function proxyIsInstalled() {
2022-05-03 05:48:03 +00:00
// Check if the proxy server is installed
const curDirList = await filesystem.readDirectory(NL_CWD)
2022-05-03 05:48:03 +00:00
if (curDirList.find(f => f.entry === 'ext')) {
const extFiles = await filesystem.readDirectory(NL_CWD + '/ext')
2022-05-08 03:49:45 +00:00
debug.log('ext/ folder exists')
2022-05-03 05:48:03 +00:00
if (extFiles.find(f => f.entry === 'mitmdump.exe')) {
2022-05-08 03:49:45 +00:00
debug.log('mitmdump exists')
2022-05-03 05:48:03 +00:00
return true
}
2022-05-03 05:48:03 +00:00
}
2022-05-08 03:49:45 +00:00
debug.log('No proxy installed')
2022-05-03 05:48:03 +00:00
return false
}
2022-04-30 08:56:00 +00:00
async function checkForUpdates() {
2022-05-03 05:48:03 +00:00
const url = 'https://api.github.com/repos/Grasscutters/GrassClipper/releases/latest'
2022-04-30 08:56:00 +00:00
2022-05-08 03:49:45 +00:00
const { data } = await axios.get(url).catch(e => debug.error('Error getting latest release'))
2022-05-03 05:48:03 +00:00
const latest = data.tag_name
2022-04-30 08:56:00 +00:00
2022-05-08 03:49:45 +00:00
debug.log('Latest release:', latest)
2022-05-03 05:48:03 +00:00
return latest
2022-04-30 08:56:00 +00:00
}
async function displayUpdate() {
2022-05-03 05:48:03 +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-08 03:49:45 +00:00
debug.log('Comparing versions: ' + latest + ' vs v' + NL_APPVERSION)
2022-05-03 05:48:03 +00:00
if (latest === `v${NL_APPVERSION}`) return
2022-04-30 08:56:00 +00:00
2022-05-08 03:49:45 +00:00
debug.log('New version available')
2022-05-03 05:48:03 +00:00
versionDisplay.innerText = latest
2022-04-30 08:56:00 +00:00
2022-05-03 05:48:03 +00:00
notif.classList.add('displayed')
2022-04-30 08:56:00 +00:00
2022-05-03 05:48:03 +00:00
setTimeout(() => {
notif.classList.remove('displayed')
}, 5000)
2022-04-30 08:56:00 +00:00
}
async function openLatestDownload() {
2022-05-03 05:48:03 +00:00
const downloadLink = 'https://github.com/Grasscutters/GrassClipper/releases/latest/'
2022-04-30 08:56:00 +00:00
2022-05-08 03:49:45 +00:00
debug.log('Opening download link: ', downloadLink)
2022-05-03 05:48:03 +00:00
Neutralino.os.open(downloadLink)
2022-04-30 08:56:00 +00:00
}
async function openGameFolder() {
2022-05-03 05:48:03 +00:00
const config = await getCfg()
const folder = config.gameexe.match(/.*\\|.*\//g, '')
2022-05-08 03:49:45 +00:00
debug.log('Opening game folder: ', folder)
2022-05-04 04:14:02 +00:00
2022-05-06 00:29:23 +00:00
if (folder.length > 0) openInExplorer(folder[0].replace(/\//g, '\\'))
}
async function openGrasscutterFolder() {
2022-05-03 05:48:03 +00:00
const config = await getCfg()
const folder = config.serverFolder.match(/.*\\|.*\//g, '')
2022-05-08 03:49:45 +00:00
debug.log('Opening grasscutter folder: ', folder)
2022-05-06 00:29:23 +00:00
if (folder.length > 0) openInExplorer(folder[0].replace(/\//g, '\\'))
}
2022-05-04 04:06:01 +00:00
// https://www.jimzhao.us/2015/09/javascript-detect-chinese-character.html
2022-05-08 02:56:02 +00:00
function hasForeignChars(str) {
let re1 = /^[\x00-\x7F]+$/g
2022-05-04 04:06:01 +00:00
str = str.replace(/\s/g, '')
2022-05-08 03:49:45 +00:00
debug.log('Checking for foreign chars in path: ', str)
debug.log('Path includes foreign chars? ', re1.test(str))
2022-05-08 02:56:02 +00:00
return !re1.test(str)
2022-05-06 01:16:15 +00:00
}
function openDialog(title, message, negBtn = false, affirmBtn = closeDialog) {
const dialog = document.getElementById('miscDialog')
const titleElm = document.getElementById('dialogTitle')
const contents = document.getElementById('dialogContent')
const noBtn = document.getElementById('dialogButtonNeg')
const yesBtn = document.getElementById('dialogButtonAffirm')
2022-05-08 03:49:45 +00:00
debug.log('Opening dialog: ', title, message)
2022-05-06 03:25:39 +00:00
if (!negBtn) {
2022-05-06 01:16:15 +00:00
noBtn.style.display = 'none'
2022-05-08 03:49:45 +00:00
debug.log('No "no" button')
2022-05-06 01:16:15 +00:00
} else {
noBtn.style.removeProperty('display')
noBtn.onclick = () => closeDialog()
}
2022-05-06 03:25:39 +00:00
yesBtn.innerText = localeObj.dialogYes || 'OK'
noBtn.innerText = localeObj.dialogNo || 'NO'
2022-05-06 01:16:15 +00:00
yesBtn.onclick = () => {
2022-05-08 03:49:45 +00:00
debug.log('Affirmative button clicked')
2022-05-06 01:16:15 +00:00
affirmBtn()
closeDialog()
}
// Set title and message
titleElm.innerText = title
contents.innerText = message
// Show the dialog
dialog.style.display = 'block'
}
function closeDialog() {
const dialog = document.getElementById('miscDialog')
2022-05-08 03:49:45 +00:00
debug.log('Closing dialog')
2022-05-06 01:16:15 +00:00
dialog.style.display = 'none'
}
2022-05-04 04:06:01 +00:00
/**
* Minimize the window
*/
2022-05-03 05:44:57 +00:00
function minimizeWin() {
2022-05-08 03:49:45 +00:00
debug.log('Minimizing window')
2022-05-03 05:48:03 +00:00
Neutralino.window.minimize()
}
/**
* Close the window
*/
function closeWin() {
2022-05-08 03:49:45 +00:00
debug.log('Closing window')
2022-05-03 05:48:03 +00:00
Neutralino.app.exit()
2022-05-03 05:48:03 +00:00
window.close()
}