GrassClipper/resources/js/index.js

410 lines
12 KiB
JavaScript
Raw Normal View History

2022-04-20 01:12:56 +00:00
Neutralino.init();
let localeObj;
2022-04-22 04:58:09 +00:00
const filesystem = Neutralino.filesystem
2022-04-21 08:20:41 +00:00
/**
* Enable play buttons
*/
async function enableButtons() {
const offBtn = document.querySelector('#playOfficial')
const privBtn = document.querySelector('#playPrivate')
offBtn.classList.remove('disabled')
offBtn.disabled = false
2022-04-22 04:58:09 +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
*/
async function enableServerButton() {
const serverBtn = document.querySelector('#serverLaunch')
serverBtn.classList.remove('disabled')
serverBtn.disabled = false
}
2022-04-21 08:20:41 +00:00
/**
* Disable buttons when the game folder is not set
*/
async function handleGameNotSet() {
// Set buttons to greyed out and disable
document.querySelector('#gamePath').innerHTML = localeObj.folderNotSet
// Set official server background to default
2022-04-22 22:34:24 +00:00
document.querySelector('#firstPanel').style.backgroundImage = `url("../bg/private/default.png")`
const offBtn = document.querySelector('#playOfficial')
const privBtn = document.querySelector('#playPrivate')
offBtn.classList.add('disabled')
offBtn.disabled = true
privBtn.classList.add('disabled')
privBtn.disabled = true
// 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() {
// Set buttons to greyed out and disable
document.querySelector('#serverPath').innerHTML = localeObj.folderNotSet
2022-04-23 02:43:50 +00:00
// Set official server background to default
// document.querySelector('#firstPanel').style.backgroundImage = `url("../bg/private/default.png")`
const privBtn = document.querySelector('#serverLaunch')
privBtn.classList.add('disabled')
privBtn.disabled = true
}
2022-04-21 08:20:41 +00:00
/**
* Show the game folder under the select button
*/
async function displayGameFolder() {
const elm = document.querySelector('#gamePath')
2022-04-20 01:12:56 +00:00
const config = await getCfg()
elm.innerHTML = config.gamefolder
2022-04-20 01:12:56 +00:00
}
2022-04-23 02:43:50 +00:00
/**
* Show the server folder under the select button
*/
async function displayServerFolder() {
const elm = document.querySelector('#serverPath')
const config = await getCfg()
elm.innerHTML = config.serverFolder
}
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() {
const config = await getCfg()
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
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
// Set default image, it will change if the bg folder exists
2022-04-22 22:34:24 +00:00
document.querySelector('#firstPanel').style.backgroundImage = `url("https://webstatic.hoyoverse.com/upload/event/2020/11/04/7fd661b5184e1734f91f628b6f89a31f_7367318474207189623.png")`
2022-04-22 19:32:04 +00:00
// Set the private background image
2022-04-22 22:34:24 +00:00
document.querySelector('#secondPanel').style.backgroundImage = `url("../bg/private/${privImage}")`
2022-04-22 22:58:47 +00:00
// Set the server background image
document.querySelector('#thirdPanel').style.backgroundImage = `url("../bg/server/${servImage}")`
2022-04-22 19:32:04 +00:00
return
2022-04-20 01:40:05 +00:00
// Check if resources folder exists
2022-04-22 04:58:09 +00:00
const mainDir = await filesystem.readDirectory(NL_CWD)
2022-04-20 01:40:05 +00:00
if (!mainDir.find(dir => dir.entry === 'resources')) {
2022-04-22 04:58:09 +00:00
await filesystem.createDirectory(NL_CWD + '/resources')
2022-04-20 01:40:05 +00:00
}
// Ensure bg folder exists
2022-04-22 04:58:09 +00:00
const bgDir = await filesystem.readDirectory(NL_CWD + '/resources')
2022-04-20 01:40:05 +00:00
if (!bgDir.find(dir => dir.entry === 'bg')) {
2022-04-22 04:58:09 +00:00
await filesystem.createDirectory(NL_CWD + '/resources/bg')
}
// Ensure official folder exists
2022-04-22 04:58:09 +00:00
const officialDir = await filesystem.readDirectory(NL_CWD + '/resources/bg')
if (!officialDir.find(dir => dir.entry === 'official')) {
2022-04-22 04:58:09 +00:00
await filesystem.createDirectory(NL_CWD + '/resources/bg/official')
2022-04-20 01:40:05 +00:00
}
if (config.gamefolder) {
2022-04-22 03:02:57 +00:00
// See if bg folder exists in parent dir
const parentDir = await filesystem.readDirectory(config.gamefolder + '/..')
2022-04-21 05:06:56 +00:00
2022-04-22 03:02:57 +00:00
if (parentDir.find(dir => dir.entry === 'bg')) {
2022-04-21 05:06:56 +00:00
const officialImages = (await filesystem.readDirectory(config.gamefolder + '/../bg')).filter(file => file.type === 'FILE')
2022-04-21 05:06:56 +00:00
2022-04-22 03:02:57 +00:00
if (officialImages.length > 0) {
for (const bg of officialImages) {
const path = config.gamefolder.replace('\\', '/') + '/../bg/' + bg.entry
2022-04-22 03:02:57 +00:00
// See if the file exists already
2022-04-22 04:58:09 +00:00
const currentBgs = (await filesystem.readDirectory(NL_CWD + '/resources/bg/official/')).filter(file => file.type === 'FILE')
2022-04-22 03:02:57 +00:00
if (!currentBgs.find(file => file.entry === bg.entry)) {
2022-04-22 04:58:09 +00:00
await filesystem.copyFile(path, NL_CWD + '/resources/bg/official/' + bg.entry).catch(e => {
2022-04-22 03:02:57 +00:00
// TODO: Handle error
})
}
2022-04-21 05:06:56 +00:00
}
2022-04-22 03:02:57 +00:00
// Pick one of the images
2022-04-22 04:58:09 +00:00
const localImg = (await filesystem.readDirectory(NL_CWD + '/resources/bg/official')).filter(file => file.type === 'FILE')
2022-04-22 03:02:57 +00:00
const image = localImg[Math.floor(Math.random() * localImg.length)].entry
// Set background image
2022-04-22 22:34:24 +00:00
document.querySelector('#firstPanel').style.backgroundImage = `url("../bg/official/${image}")`
}
2022-04-21 05:06:56 +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-04-21 05:43:32 +00:00
const ip = document.querySelector('#ip').value
2022-04-24 23:06:25 +00:00
const port = document.querySelector('#port').value
2022-04-21 05:43:32 +00:00
const ipArr = await getFavIps()
2022-04-24 23:06:25 +00:00
const addr = `${ip}:${port}`
if (!ip || !ipArr.includes(addr)) {
2022-04-21 05:43:32 +00:00
document.querySelector('#star').src = 'icons/star_empty.svg'
} else {
document.querySelector('#star').src = 'icons/star_filled.svg'
}
}
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) {
const ipInput = document.querySelector('#ip')
2022-04-24 23:06:25 +00:00
const portInput = document.querySelector('#port')
const parseIp = ip.split(':')[0]
const parsePort = ip.split(':')[1]
2022-04-21 06:12:15 +00:00
// Set star
if (ip) {
document.querySelector('#star').src = 'icons/star_filled.svg'
}
2022-04-24 23:06:25 +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() {
const ipArr = await getFavIps()
const ipList = document.querySelector('#ipList')
if (ipList.style.display === 'none') {
ipList.innerHTML = ''
const list = ipList.appendChild(
document.createElement('ul')
)
2022-04-21 06:17:50 +00:00
if (ipArr.length < 1) {
const listItem = list.appendChild(
document.createElement('li')
)
listItem.innerHTML = localeObj.noFavorites
2022-04-21 06:17:50 +00:00
}
2022-04-21 06:04:20 +00:00
for (const ip of ipArr) {
const elm = document.createElement('li')
elm.innerHTML = ip
2022-04-21 06:12:15 +00:00
elm.addEventListener('click', () => setIp(ip))
2022-04-21 06:04:20 +00:00
list.appendChild(elm)
}
ipList.style.display = 'block'
const transform = window.getComputedStyle(document.querySelector('#ipList')).transform
const xy = [ transform.split(',')[4], transform.split(',')[5] ]
let newY = parseInt(xy[1].replace(')', '')) - (27 * ipArr.length)
if (ipArr.length === 0) newY -= 27
ipList.style.transform = `translate(${xy[0]}px, ${newY}px)`
2022-04-21 06:04:20 +00:00
}
}
2022-04-21 21:53:53 +00:00
async function openSettings() {
const settings = document.querySelector('#settingsPanel')
2022-04-21 22:15:52 +00:00
const config = await getCfg()
2022-04-21 21:53:53 +00:00
if (settings.style.display === 'none') {
settings.style.removeProperty('display')
}
2022-04-21 22:15:52 +00:00
// Fill setting options with what is currently set in config
const killSwitch = document.querySelector('#killswitchOption')
2022-04-23 03:01:57 +00:00
const serverLaunch = document.querySelector('#serverLaunchOption')
2022-04-24 23:17:23 +00:00
const httpsCheckbox = document.querySelector('#httpsOption')
2022-04-21 22:15:52 +00:00
killSwitch.checked = config.enableKillswitch
2022-04-23 03:01:57 +00:00
serverLaunch.checked = config.serverLaunchPanel
2022-04-24 23:17:23 +00:00
httpsCheckbox.checked = config.useHttps
2022-04-23 03:01:57 +00:00
// Load languages
getLanguages()
2022-04-22 21:42:16 +00:00
// Check for updates
2022-04-22 22:02:50 +00:00
//checkForUpdatesAndShow()
2022-04-21 22:15:52 +00:00
}
2022-04-21 22:33:14 +00:00
async function closeSettings() {
const settings = document.querySelector('#settingsPanel')
2022-04-23 02:43:50 +00:00
const config = await getCfg()
2022-04-21 22:33:14 +00:00
settings.style.display = 'none'
2022-04-22 04:58:09 +00:00
// In case we installed the proxy server
if (await proxyIsInstalled() && config.gamefolder) {
2022-04-22 04:58:09 +00:00
const playPriv = document.querySelector('#playPrivate')
playPriv.classList.remove('disabled')
playPriv.disabled = false
}
2022-04-21 22:33:14 +00:00
}
async function closeFirstTimePopup() {
const firstTimePopup = document.querySelector('#firstTimeNotice')
firstTimePopup.style.display = 'none'
}
async function runInstallScript() {
Neutralino.os.execCommand(`${NL_CWD}/scripts/install.cmd "${NL_CWD}"`)
2022-04-24 20:25:39 +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)
closeFirstTimePopup()
}
2022-04-22 21:42:16 +00:00
async function updateResources() {
}
async function checkForUpdatesAndShow() {
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-23 03:01:57 +00:00
async function displayServerLaunchSection() {
2022-04-23 02:14:40 +00:00
const serverPanel = document.querySelector('#thirdPanel')
const bottomBtnSection = document.querySelector('#serverPath').parentElement
2022-04-23 02:14:40 +00:00
if (serverPanel.style.display === 'none') {
serverPanel.style.removeProperty('display')
bottomBtnSection.style.removeProperty('display')
2022-04-23 02:14:40 +00:00
} else {
serverPanel.style.display = 'none'
bottomBtnSection.style.display = 'none'
2022-04-23 02:14:40 +00:00
}
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
*/
async function setGameFolder() {
const folder = await Neutralino.os.showFolderDialog(localeObj.gameFolderDialog)
2022-04-20 01:12:56 +00:00
// Set the folder in our configuration
const config = await getCfg()
// See if the actual game folder is inside this one
2022-04-22 04:58:09 +00:00
const folderList = await filesystem.readDirectory(folder)
const gameFolder = folderList.filter(file => file.entry.includes('Genshin Impact Game'))
if (gameFolder.length > 0) {
config.gamefolder = folder + '\\Genshin Impact Game'
Neutralino.storage.setData('config', JSON.stringify(config))
} else {
config.gamefolder = folder
}
2022-04-20 01:12:56 +00:00
Neutralino.storage.setData('config', JSON.stringify(config))
2022-04-20 01:40:05 +00:00
2022-04-20 05:31:23 +00:00
// Refresh background and path
2022-04-20 01:40:05 +00:00
setBackgroundImage()
displayGameFolder()
enableButtons()
2022-04-20 01:12:56 +00:00
}
2022-04-20 01:21:29 +00:00
async function setGrasscutterFolder() {
const folder = await Neutralino.os.showOpenDialog(localeObj.grasscutterFileDialog, {
2022-04-23 03:01:57 +00:00
filters: [
{ name: 'Jar files', extensions: ['jar'] }
]
})
2022-04-23 02:43:50 +00:00
// Set the folder in our configuration
const config = await getCfg()
config.serverFolder = folder
Neutralino.storage.setData('config', JSON.stringify(config))
displayServerFolder()
enableServerButton()
}
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() {
const config = await getCfg()
Neutralino.os.execCommand(config.gamefolder + '/' + await getGameExecName())
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-04-20 03:33:34 +00:00
const ip = document.getElementById('ip').value || 'localhost'
2022-04-24 07:32:17 +00:00
const port = document.getElementById('port').value || '443'
2022-04-20 01:40:05 +00:00
const config = await getCfg()
2022-04-20 03:33:34 +00:00
2022-04-24 07:32:17 +00:00
console.log('connecting to ' + ip + ':' + port)
2022-04-21 01:43:19 +00:00
// Set the last connect
config.lastConnect = ip
Neutralino.storage.setData('config', JSON.stringify(config))
2022-04-20 03:02:04 +00:00
// Pass IP and game folder to the private server launcher
2022-04-24 23:22:23 +00:00
Neutralino.os.execCommand(`${NL_CWD}/scripts/private_server_launch.cmd ${ip} ${port} ${config.useHttps} "${config.gamefolder}/${await getGameExecName()}" "${NL_CWD}" ${config.enableKillswitch}`).catch(e => console.log(e))
2022-04-21 00:44:17 +00:00
}
2022-04-23 03:01:57 +00:00
async function launchLocalServer() {
const config = await getCfg()
Neutralino.os.execCommand(`${NL_CWD}/scripts/local_server_launch.cmd "${config.serverFolder}"`).catch(e => console.log(e))
}