GrassClipper/resources/js/index.js

173 lines
5.4 KiB
JavaScript
Raw Normal View History

2022-04-20 01:12:56 +00:00
Neutralino.init();
document.addEventListener('DOMContentLoaded', async () => {
setBackgroundImage();
displayGenshinFolder();
const config = await getCfg()
if (!config.genshinImpactFolder) {
handleGenshinFolderNotSet()
}
2022-04-21 01:43:19 +00:00
// Set last connect
document.querySelector('#ip').value = config.lastConnect
2022-04-20 01:12:56 +00:00
})
async function getCfg() {
2022-04-20 04:45:34 +00:00
const cfgStr = await Neutralino.storage.getData('config').catch(e => {
2022-04-20 01:12:56 +00:00
// The data isn't set, so this is our first time opening
Neutralino.storage.setData('config', JSON.stringify({
genshinImpactFolder: '',
lastConnect: ''
}))
2022-04-20 04:45:34 +00:00
})
const config = cfgStr ? JSON.parse(cfgStr) : {
2022-04-20 04:45:34 +00:00
genshinImpactFolder: '',
lastConnect: ''
}
return config
}
async function enableButtons() {
const offBtn = document.querySelector('#playOfficial')
const privBtn = document.querySelector('#playPrivate')
offBtn.classList.remove('disabled')
offBtn.disabled = false
privBtn.classList.remove('disabled')
privBtn.disabled = false
}
async function handleGenshinFolderNotSet() {
// Set buttons to greyed out and disable
document.querySelector('#genshinPath').innerHTML = 'Not set'
// Set official server background to default
document.querySelector('#firstHalf').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
}
async function displayGenshinFolder() {
const elm = document.querySelector('#genshinPath')
const config = await getCfg()
elm.innerHTML = config.genshinImpactFolder
}
async function setBackgroundImage() {
const config = await getCfg()
2022-04-20 01:40:05 +00:00
// Check if resources folder exists
const mainDir = await Neutralino.filesystem.readDirectory(NL_CWD)
if (!mainDir.find(dir => dir.entry === 'resources')) {
await Neutralino.filesystem.createDirectory(NL_CWD + '/resources')
}
// Ensure bg folder exists
const bgDir = await Neutralino.filesystem.readDirectory(NL_CWD + '/resources')
if (!bgDir.find(dir => dir.entry === 'bg')) {
await Neutralino.filesystem.createDirectory(NL_CWD + '/resources/bg')
}
// Ensure official folder exists
const officialDir = await Neutralino.filesystem.readDirectory(NL_CWD + '/resources/bg')
if (!officialDir.find(dir => dir.entry === 'official')) {
2022-04-20 23:27:40 +00:00
await Neutralino.filesystem.createDirectory(NL_CWD + '/resources/bg/official')
2022-04-20 01:40:05 +00:00
}
if (config.genshinImpactFolder) {
const officialImages = (await Neutralino.filesystem.readDirectory(config.genshinImpactFolder + '/bg')).filter(file => file.type === 'FILE')
// Pick one of the images
const image = officialImages[Math.floor(Math.random() * officialImages.length)].entry
const path = config.genshinImpactFolder.replace('\\', '/') + '/bg/' + image
// Copy to backgrounds folder
const officialBgs = (await Neutralino.filesystem.readDirectory(NL_CWD + '/resources/bg/official/')).filter(file => file.type === 'FILE')
if (!officialBgs.find(file => file.entry === image)) {
await Neutralino.filesystem.copyFile(path, NL_CWD + '/resources/bg/official/' + image).catch(e => {
// TODO: Handle error
})
}
// Set background image
document.querySelector('#firstHalf').style.backgroundImage = `url("../bg/official/${image}")`
2022-04-20 01:12:56 +00:00
}
const privImages = (await Neutralino.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-20 01:12:56 +00:00
// Set the background image
document.querySelector('#secondHalf').style.backgroundImage = `url("../bg/private/${privImage}")`
2022-04-20 01:12:56 +00:00
}
async function setGenshinImpactFolder() {
const folder = await Neutralino.os.showFolderDialog('Select Genshin Impact folder')
// Set the folder in our configuration
const config = await getCfg()
2022-04-20 01:12:56 +00:00
config.genshinImpactFolder = folder
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()
2022-04-20 05:31:23 +00:00
displayGenshinFolder()
enableButtons()
2022-04-20 01:12:56 +00:00
}
2022-04-20 01:21:29 +00:00
2022-04-20 04:55:25 +00:00
async function getGenshinExecName() {
// Scan genshin dir
const config = await getCfg()
const genshinDir = await Neutralino.filesystem.readDirectory(config.genshinImpactFolder + '/Genshin Impact Game')
// Find the executable
const genshinExec = genshinDir.find(file => file.entry.endsWith('.exe'))
return genshinExec.entry
}
2022-04-20 01:21:29 +00:00
async function launchOfficial() {
const config = await getCfg()
2022-04-20 04:55:25 +00:00
Neutralino.os.execCommand(config.genshinImpactFolder + '/Genshin Impact Game/' + await getGenshinExecName())
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-20 01:40:05 +00:00
const config = await getCfg()
2022-04-20 03:33:34 +00:00
console.log('connecting to ' + ip)
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-20 05:16:56 +00:00
Neutralino.os.execCommand(`${NL_CWD}/scripts/private_server_launch.cmd ${ip} "${config.genshinImpactFolder}/Genshin Impact Game/${await getGenshinExecName()}"`).catch(e => console.log(e))
2022-04-21 00:44:17 +00:00
}
function minimizeWin() {
2022-04-21 01:16:36 +00:00
console.log('min')
2022-04-21 00:44:17 +00:00
Neutralino.window.minimize()
}
function closeWin() {
2022-04-21 01:16:36 +00:00
console.log('close')
2022-04-21 00:44:17 +00:00
Neutralino.app.exit()
2022-04-20 01:21:29 +00:00
}