GrassClipper/resources/js/index.js

487 lines
14 KiB
JavaScript
Raw Normal View History

2022-05-03 05:44:57 +00:00
Neutralino.init()
2022-04-20 01:12:56 +00:00
2022-05-03 05:44:57 +00:00
let localeObj
2022-04-22 04:58:09 +00:00
const filesystem = Neutralino.filesystem
2022-04-26 01:37:41 +00:00
const createCmdWindow = async (command) => {
2022-05-03 05:48:03 +00:00
Neutralino.os.execCommand(`cmd.exe /c start "" ${command}`, { background: true })
2022-04-26 01:37:41 +00:00
}
2022-04-22 04:58:09 +00:00
2022-04-26 09:26:53 +00:00
const openInExplorer = async (path) => {
2022-05-03 05:48:03 +00:00
createCmdWindow(`explorer.exe "${path}"`)
2022-04-26 09:26:53 +00:00
}
2022-04-21 08:20:41 +00:00
/**
* Enable play buttons
*/
async function enableButtons() {
2022-05-03 05:48:03 +00:00
const offBtn = document.querySelector('#playOfficial')
const privBtn = document.querySelector('#playPrivate')
2022-05-03 05:48:03 +00:00
offBtn.classList.remove('disabled')
offBtn.disabled = false
2022-05-03 05:48:03 +00:00
// Check if the proxy server is installed
if (await proxyIsInstalled()) {
privBtn.classList.remove('disabled')
privBtn.disabled = false
}
}
2022-04-23 02:43:50 +00:00
/**
* Enable server launch button
*/
2022-05-03 05:44:57 +00:00
async function enableServerButton() {
2022-05-03 05:48:03 +00:00
const serverBtn = document.querySelector('#serverLaunch')
2022-04-23 02:43:50 +00:00
2022-05-03 05:48:03 +00:00
serverBtn.classList.remove('disabled')
serverBtn.disabled = false
2022-04-23 02:43:50 +00:00
}
2022-04-21 08:20:41 +00:00
/**
* Disable buttons when the game folder is not set
*/
async function handleGameNotSet() {
2022-05-03 05:48:03 +00:00
// Set buttons to greyed out and disable
document.querySelector('#gamePath').innerHTML = localeObj.folderNotSet
2022-05-03 05:48:03 +00:00
// Set official server background to default
document.querySelector('#firstPanel').style.backgroundImage = 'url("../bg/private/default.png")'
2022-05-03 05:48:03 +00:00
const offBtn = document.querySelector('#playOfficial')
const privBtn = document.querySelector('#playPrivate')
2022-05-03 05:48:03 +00:00
offBtn.classList.add('disabled')
offBtn.disabled = true
2022-05-03 05:48:03 +00:00
privBtn.classList.add('disabled')
privBtn.disabled = true
2022-05-03 05:48:03 +00:00
// TODO show a dialog of sorts
2022-04-20 01:12:56 +00:00
}
2022-04-23 02:43:50 +00:00
async function handleServerNotSet() {
2022-05-03 05:48:03 +00:00
// Set buttons to greyed out and disable
document.querySelector('#serverPath').innerHTML = localeObj.folderNotSet
2022-04-23 02:43:50 +00:00
2022-05-03 05:48:03 +00:00
// Set official server background to default
// document.querySelector('#firstPanel').style.backgroundImage = `url("../bg/private/default.png")`
2022-04-23 02:43:50 +00:00
2022-05-03 05:48:03 +00:00
const privBtn = document.querySelector('#serverLaunch')
2022-04-23 02:43:50 +00:00
2022-05-03 05:48:03 +00:00
privBtn.classList.add('disabled')
privBtn.disabled = true
2022-04-23 02:43:50 +00:00
}
2022-04-21 08:20:41 +00:00
/**
* Show the game folder under the select button
*/
async function displayGameFolder() {
2022-05-03 05:48:03 +00:00
const elm = document.querySelector('#gamePath')
const config = await getCfg()
2022-04-20 01:12:56 +00:00
2022-05-03 05:48:03 +00:00
elm.innerHTML = config.gameexe
2022-04-20 01:12:56 +00:00
}
2022-04-23 02:43:50 +00:00
/**
* Show the server folder under the select button
*/
2022-05-03 05:44:57 +00:00
async function displayServerFolder() {
2022-05-03 05:48:03 +00:00
const elm = document.querySelector('#serverPath')
const config = await getCfg()
2022-04-23 02:43:50 +00:00
2022-05-03 05:48:03 +00:00
elm.innerHTML = config.serverFolder
2022-04-23 02:43:50 +00:00
}
2022-04-21 08:20:41 +00:00
/**
* Set the background images of both the private and public sections
*/
2022-04-20 01:12:56 +00:00
async function setBackgroundImage() {
2022-05-03 05:48:03 +00:00
const config = await getCfg()
2022-04-20 01:12:56 +00:00
2022-05-03 05:48:03 +00:00
const privImages = (await filesystem.readDirectory(NL_CWD + '/resources/bg/private')).filter(file => file.type === 'FILE' && !file.entry.includes('default'))
const privImage = privImages[Math.floor(Math.random() * privImages.length)].entry
2022-04-22 22:58:47 +00:00
2022-05-03 05:48:03 +00:00
const servImages = (await filesystem.readDirectory(NL_CWD + '/resources/bg/server')).filter(file => file.type === 'FILE' && !file.entry.includes('default'))
const servImage = servImages[Math.floor(Math.random() * servImages.length)].entry
2022-05-03 05:48:03 +00:00
// Set default image, it will change if the bg folder exists
document.querySelector('#firstPanel').style.backgroundImage = 'url("https://webstatic.hoyoverse.com/upload/event/2020/11/04/7fd661b5184e1734f91f628b6f89a31f_7367318474207189623.png")'
2022-05-03 05:48:03 +00:00
// Set the private background image
document.querySelector('#secondPanel').style.backgroundImage = `url("../bg/private/${privImage}")`
2022-04-22 22:58:47 +00:00
2022-05-03 05:48:03 +00:00
// Set the server background image
document.querySelector('#thirdPanel').style.backgroundImage = `url("../bg/server/${servImage}")`
return
// Check if resources folder exists
const mainDir = await filesystem.readDirectory(NL_CWD)
if (!mainDir.find(dir => dir.entry === 'resources')) {
await filesystem.createDirectory(NL_CWD + '/resources')
}
// Ensure bg folder exists
const bgDir = await filesystem.readDirectory(NL_CWD + '/resources')
if (!bgDir.find(dir => dir.entry === 'bg')) {
await filesystem.createDirectory(NL_CWD + '/resources/bg')
}
// Ensure official folder exists
const officialDir = await filesystem.readDirectory(NL_CWD + '/resources/bg')
if (!officialDir.find(dir => dir.entry === 'official')) {
await filesystem.createDirectory(NL_CWD + '/resources/bg/official')
}
if (config.gameexe) {
2022-04-22 03:02:57 +00:00
// See if bg folder exists in parent dir
2022-05-03 05:48:03 +00:00
const parentDir = await filesystem.readDirectory(config.gameexe + '/..')
2022-04-21 05:06:56 +00:00
2022-05-03 05:48:03 +00:00
if (parentDir.find(dir => dir.entry === 'bg')) {
2022-04-21 05:06:56 +00:00
2022-05-03 05:48:03 +00:00
const officialImages = (await filesystem.readDirectory(config.gameexe + '/../bg')).filter(file => file.type === 'FILE')
2022-04-21 05:06:56 +00:00
2022-05-03 05:48:03 +00:00
if (officialImages.length > 0) {
for (const bg of officialImages) {
const path = config.gameexe.replace('\\', '/') + '/../bg/' + bg.entry
2022-04-22 03:02:57 +00:00
2022-05-03 05:48:03 +00:00
// See if the file exists already
const currentBgs = (await filesystem.readDirectory(NL_CWD + '/resources/bg/official/')).filter(file => file.type === 'FILE')
2022-04-22 03:02:57 +00:00
2022-05-03 05:48:03 +00:00
if (!currentBgs.find(file => file.entry === bg.entry)) {
await filesystem.copyFile(path, NL_CWD + '/resources/bg/official/' + bg.entry).catch(e => {
// TODO: Handle error
})
}
}
2022-04-22 03:02:57 +00:00
2022-05-03 05:48:03 +00:00
// Pick one of the images
const localImg = (await filesystem.readDirectory(NL_CWD + '/resources/bg/official')).filter(file => file.type === 'FILE')
const image = localImg[Math.floor(Math.random() * localImg.length)].entry
2022-04-22 03:02:57 +00:00
2022-05-03 05:48:03 +00:00
// Set background image
document.querySelector('#firstPanel').style.backgroundImage = `url("../bg/official/${image}")`
}
2022-04-21 05:06:56 +00:00
}
2022-05-03 05:48:03 +00:00
}
2022-04-20 01:12:56 +00:00
}
2022-04-21 08:20:41 +00:00
/**
* When an IP is being input, check if it is part of the favorites
*/
2022-04-21 05:26:25 +00:00
async function handleFavoriteInput() {
2022-05-03 05:48:03 +00:00
const ip = document.querySelector('#ip').value
const port = document.querySelector('#port').value || '443'
const ipArr = await getFavIps()
2022-04-21 05:43:32 +00:00
2022-05-03 05:48:03 +00:00
const addr = `${ip}:${port}`
2022-04-24 23:06:25 +00:00
2022-05-03 05:48:03 +00:00
if (!ip || !ipArr.includes(addr)) {
document.querySelector('#star').src = 'icons/star_empty.svg'
} else {
document.querySelector('#star').src = 'icons/star_filled.svg'
}
2022-04-21 05:43:32 +00:00
}
2022-04-21 08:20:41 +00:00
/**
* Set the IP input value
*
* @param {String} ip
*/
2022-04-21 06:12:15 +00:00
async function setIp(ip) {
2022-05-03 05:48:03 +00:00
const ipInput = document.querySelector('#ip')
const portInput = document.querySelector('#port')
2022-04-24 23:06:25 +00:00
2022-05-03 05:48:03 +00:00
const parseIp = ip.split(':')[0]
const parsePort = ip.split(':')[1]
2022-04-21 06:12:15 +00:00
2022-05-03 05:48:03 +00:00
// Set star
if (ip) {
document.querySelector('#star').src = 'icons/star_filled.svg'
}
2022-05-03 05:48:03 +00:00
ipInput.value = parseIp
portInput.value = parsePort
2022-04-21 06:12:15 +00:00
}
2022-04-21 08:20:41 +00:00
/**
* Create/hide the favorites list
*/
2022-04-21 06:04:20 +00:00
async function handleFavoriteList() {
2022-05-03 05:48:03 +00:00
const ipArr = await getFavIps()
const ipList = document.querySelector('#ipList')
2022-04-21 06:04:20 +00:00
2022-05-03 05:48:03 +00:00
if (ipList.style.display === 'none') {
ipList.innerHTML = ''
2022-04-21 06:04:20 +00:00
2022-05-03 05:48:03 +00:00
const list = ipList.appendChild(
document.createElement('ul')
)
2022-04-21 06:04:20 +00:00
2022-05-03 05:48:03 +00:00
if (ipArr.length < 1) {
const listItem = list.appendChild(
document.createElement('li')
)
2022-04-21 06:17:50 +00:00
2022-05-03 05:48:03 +00:00
listItem.innerHTML = localeObj.noFavorites
}
2022-04-21 06:17:50 +00:00
2022-05-03 05:48:03 +00:00
for (const ip of ipArr) {
const elm = document.createElement('li')
elm.innerHTML = ip
elm.addEventListener('click', () => setIp(ip))
list.appendChild(elm)
}
2022-04-21 06:04:20 +00:00
2022-05-03 05:48:03 +00:00
ipList.style.display = 'block'
2022-05-03 05:48:03 +00:00
const transform = window.getComputedStyle(document.querySelector('#ipList')).transform
const xy = [ transform.split(',')[4], transform.split(',')[5] ]
let newY = (27 * ipArr.length) * window.devicePixelRatio
2022-05-03 05:48:03 +00:00
if (ipArr.length === 0 || ipArr.length === 1) newY = 0
2022-05-03 05:48:03 +00:00
ipList.style.transform = `translate(${xy[0]}px, calc(56vh - ${newY}px)`
}
2022-04-21 06:04:20 +00:00
}
2022-05-02 05:56:15 +00:00
async function openDownloads() {
2022-05-03 05:48:03 +00:00
const downloads = document.querySelector('#downloadPanel')
const config = await getCfg()
if (downloads.style.display === 'none') {
downloads.style.removeProperty('display')
}
// Disable the resource download button if a serverFolder path is not set
if (!config.serverFolder) {
document.querySelector('#resourceInstall').disabled = true
document.querySelector('#resourceInstall').classList.add('disabled')
} else {
document.querySelector('#resourceInstall').disabled = false
document.querySelector('#resourceInstall').classList.remove('disabled')
}
2022-05-02 05:56:15 +00:00
}
async function closeDownloads() {
2022-05-03 05:48:03 +00:00
const downloads = document.querySelector('#downloadPanel')
2022-05-02 05:56:15 +00:00
2022-05-03 05:48:03 +00:00
downloads.style.display = 'none'
2022-05-02 05:56:15 +00:00
}
2022-04-21 21:53:53 +00:00
async function openSettings() {
2022-05-03 05:48:03 +00:00
const settings = document.querySelector('#settingsPanel')
const config = await getCfg()
2022-04-21 21:53:53 +00:00
2022-05-03 05:48:03 +00:00
if (settings.style.display === 'none') {
settings.style.removeProperty('display')
}
2022-04-21 22:15:52 +00:00
2022-05-03 05:48:03 +00:00
// Fill setting options with what is currently set in config
const killSwitch = document.querySelector('#killswitchOption')
const serverLaunch = document.querySelector('#serverLaunchOption')
const httpsCheckbox = document.querySelector('#httpsOption')
2022-04-21 22:15:52 +00:00
2022-05-03 05:48:03 +00:00
killSwitch.checked = config.enableKillswitch
serverLaunch.checked = config.serverLaunchPanel
httpsCheckbox.checked = config.useHttps
2022-04-23 03:01:57 +00:00
2022-05-03 05:48:03 +00:00
// Load languages
getLanguages()
2022-05-03 05:48:03 +00:00
// Check for updates
//checkForUpdatesAndShow()
2022-04-21 22:15:52 +00:00
}
2022-04-21 22:33:14 +00:00
async function closeSettings() {
2022-05-03 05:48:03 +00:00
const settings = document.querySelector('#settingsPanel')
const config = await getCfg()
2022-04-21 22:33:14 +00:00
2022-05-03 05:48:03 +00:00
settings.style.display = 'none'
2022-04-22 04:58:09 +00:00
2022-05-03 05:48:03 +00:00
// In case we installed the proxy server
if (await proxyIsInstalled() && config.gameexe) {
const playPriv = document.querySelector('#playPrivate')
2022-04-22 04:58:09 +00:00
2022-05-03 05:48:03 +00:00
playPriv.classList.remove('disabled')
playPriv.disabled = false
}
2022-04-21 22:33:14 +00:00
}
2022-04-27 01:34:20 +00:00
async function openLogin() {
2022-05-03 05:48:03 +00:00
const login = document.querySelector('#loginPanel')
const ip = document.querySelector('#ip').value
const port = document.querySelector('#port').value
const loginIpDisplay = document.querySelector('#loginPopupServer')
const registerIpDisplay = document.querySelector('#registerPopupServer')
2022-05-03 05:48:03 +00:00
const config = await getCfg()
const useHttps = config.useHttps
const url = `${useHttps ? 'https' : 'http'}://${ip}:${port}`
2022-05-03 05:48:03 +00:00
// Check if we even need to authenticate
try {
const { data } = await axios.get(url + '/authentication/type')
2022-05-03 05:48:03 +00:00
if (!data.includes('GCAuthAuthenticationHandler')) {
launchPrivate()
return
2022-04-28 04:17:22 +00:00
}
2022-05-03 05:48:03 +00:00
} catch(e) {
launchPrivate()
return
}
2022-04-27 01:34:20 +00:00
2022-05-03 05:48:03 +00:00
loginIpDisplay.innerText = ip
registerIpDisplay.innerText = ip
2022-04-27 01:34:20 +00:00
2022-05-03 05:48:03 +00:00
if (login.style.display === 'none') {
login.style.removeProperty('display')
}
2022-04-27 01:34:20 +00:00
}
async function closeLogin() {
2022-05-03 05:48:03 +00:00
const login = document.querySelector('#loginPanel')
2022-04-27 01:34:20 +00:00
2022-05-03 05:48:03 +00:00
login.style.display = 'none'
2022-04-27 04:26:21 +00:00
2022-05-03 05:48:03 +00:00
setLoginSection()
2022-04-27 01:34:20 +00:00
}
async function closeFirstTimePopup() {
2022-05-03 05:48:03 +00:00
const firstTimePopup = document.querySelector('#firstTimeNotice')
firstTimePopup.style.display = 'none'
}
async function runInstallScript() {
2022-05-03 05:48:03 +00:00
createCmdWindow(`.\\scripts\\install.cmd "${NL_CWD}" true`)
2022-05-03 05:48:03 +00:00
// Create an interval that will check for the proxy server installation finish
const interval = setInterval(async () => {
if (await proxyIsInstalled()) {
clearInterval(interval)
enableButtons()
}
}, 1000)
2022-04-24 20:25:39 +00:00
2022-05-03 05:48:03 +00:00
closeFirstTimePopup()
}
2022-04-22 21:42:16 +00:00
async function updateResources() {
}
async function checkForUpdatesAndShow() {
2022-05-03 05:48:03 +00:00
const updateBtn = document.querySelector('#updateBtn')
const subtitle = document.querySelector('#updateSubtitle')
const url = 'https://github.com/Grasscutters/GrassClipper/releases/latest/download/'
const manifest = await Neutralino.updater.checkForUpdates(url)
// Version mismatch? Update!
if (manifest?.version !== NL_APPVERSION) {
subtitle.innerHTML = 'New update available!'
updateBtn.classList.remove('disabled')
} else {
subtitle.innerHTML = 'You are on the latest version! :)'
updateBtn.classList.add('disabled')
}
2022-04-22 21:42:16 +00:00
}
2022-04-23 03:01:57 +00:00
async function displayServerLaunchSection() {
2022-05-03 05:48:03 +00:00
const serverPanel = document.querySelector('#thirdPanel')
const bottomBtnSection = document.querySelector('#serverPath').parentElement
if (serverPanel.style.display === 'none') {
serverPanel.style.removeProperty('display')
bottomBtnSection.style.removeProperty('display')
} else {
serverPanel.style.display = 'none'
bottomBtnSection.style.display = 'none'
}
2022-04-23 03:01:57 +00:00
}
2022-04-21 08:20:41 +00:00
/**
* Set the game folder by opening a folder picker
*/
2022-04-26 05:22:54 +00:00
async function setGameExe() {
2022-05-03 05:48:03 +00:00
const gameExe = await Neutralino.os.showOpenDialog(localeObj.gameFolderDialog, {
filters: [
{ name: 'Executable files', extensions: ['exe'] }
]
})
2022-04-20 01:12:56 +00:00
2022-05-03 05:48:03 +00:00
if (!gameExe[0]) return
2022-05-04 04:14:02 +00:00
if (hasChineseChars(gameExe[0])) displayAlert(localeObj.chineseCharacterAlert)
2022-05-02 06:17:23 +00:00
2022-05-03 05:48:03 +00:00
// Set the folder in our configuration
const config = await getCfg()
2022-05-03 05:48:03 +00:00
// It's an array of selections, so only get the first one
config.gameexe = gameExe[0].replace(/\//g, '\\')
2022-05-03 05:48:03 +00:00
Neutralino.storage.setData('config', JSON.stringify(config))
2022-04-20 01:40:05 +00:00
2022-05-03 05:48:03 +00:00
// Refresh background and path
setBackgroundImage()
displayGameFolder()
enableButtons()
2022-04-20 01:12:56 +00:00
}
2022-04-20 01:21:29 +00:00
async function setGrasscutterFolder() {
2022-05-03 05:48:03 +00:00
const folder = await Neutralino.os.showOpenDialog(localeObj.grasscutterFileDialog, {
filters: [
{ name: 'Jar files', extensions: ['jar'] }
]
})
2022-04-23 02:43:50 +00:00
2022-05-03 05:48:03 +00:00
if (!folder[0]) return
2022-05-04 04:14:02 +00:00
if (hasChineseChars(folder[0])) displayAlert(localeObj.chineseCharacterAlert)
2022-05-02 06:17:23 +00:00
2022-05-03 05:48:03 +00:00
// Set the folder in our configuration
const config = await getCfg()
2022-04-23 02:43:50 +00:00
2022-05-03 05:48:03 +00:00
config.serverFolder = folder[0]
Neutralino.storage.setData('config', JSON.stringify(config))
2022-04-23 02:43:50 +00:00
2022-05-03 05:48:03 +00:00
displayServerFolder()
enableServerButton()
2022-04-23 02:43:50 +00:00
}
2022-04-21 08:20:41 +00:00
/**
* Launch the game with no modifications nor proxy
*/
2022-04-20 01:21:29 +00:00
async function launchOfficial() {
2022-05-03 05:48:03 +00:00
const config = await getCfg()
2022-04-20 01:21:29 +00:00
2022-05-03 05:48:03 +00:00
Neutralino.os.execCommand(`"${config.gameexe}"`)
2022-04-20 01:40:05 +00:00
}
2022-04-21 08:20:41 +00:00
/**
* Launch the game with a proxy
*/
2022-04-20 01:40:05 +00:00
async function launchPrivate() {
2022-05-03 05:48:03 +00:00
const ip = document.getElementById('ip').value || '127.0.0.1'
const port = document.getElementById('port').value || '443'
2022-04-20 01:40:05 +00:00
2022-05-03 05:48:03 +00:00
const config = await getCfg()
2022-04-20 03:33:34 +00:00
2022-05-03 05:48:03 +00:00
console.log('connecting to ' + ip + ':' + port)
2022-05-03 05:48:03 +00:00
// Set the last connect
config.lastConnect = ip
Neutralino.storage.setData('config', JSON.stringify(config))
2022-04-21 01:43:19 +00:00
2022-05-03 05:48:03 +00:00
// Pass IP and game folder to the private server launcher
2022-05-06 03:11:41 +00:00
Neutralino.os.execCommand(
`.\\scripts\\private_server_launch.cmd ${ip} ${port} ${config.useHttps} "${config.gameexe}" "${NL_CWD}" ${config.enableKillswitch} true`, {
background: true
}
).catch(e => console.log(e))
2022-04-21 00:44:17 +00:00
}
2022-04-23 03:01:57 +00:00
async function launchLocalServer() {
2022-05-03 05:48:03 +00:00
const config = await getCfg()
2022-04-23 03:01:57 +00:00
2022-05-03 05:48:03 +00:00
createCmdWindow(`.\\scripts\\local_server_launch.cmd "${config.serverFolder}"`).catch(e => console.log(e))
2022-04-23 03:01:57 +00:00
}