Skip to content

Commit

Permalink
Update 2 Final Stage
Browse files Browse the repository at this point in the history
  • Loading branch information
Ipmake committed Oct 9, 2023
1 parent 6886d72 commit 6e76f66
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 125 deletions.
200 changes: 96 additions & 104 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,15 @@ function createWindow(): void {
})

async function updateServerList(): Promise<void> {
serverList = (
await axios.get(`http://localhost:23501/stats/servers`, {
timeout: 5000
})
).data
try {
serverList = (
await axios.get(`https://api.kocity.xyz/stats/servers`, {
timeout: 5000
})
).data
} catch (error) {
console.log(error)
}
}
setInterval(updateServerList, 1000 * 60 * 3)
updateServerList()
Expand Down Expand Up @@ -378,9 +382,11 @@ function createWindow(): void {
// Make the directory using sudoer and edit the permissions of the directory to allow everyone to write to it windows only
await new Promise((resolve, reject) => {
sudo.exec(
`mkdir "${arg.path}" && icacls "${arg.path}" /grant "${
os.userInfo().username
}":(OI)(CI)F /T`,
os.platform() === 'win32'
? `mkdir "${arg.path}" && icacls "${arg.path}" /grant "${
os.userInfo().username
}":(OI)(CI)F /T`
: `mkdir "${arg.path}" && chown -R ${os.userInfo().username} "${arg.path}"`,
{ name: 'Knockout City Launcher' },
(error) => {
if (error) reject(new Error(error.message)), console.log(error)
Expand All @@ -400,7 +406,9 @@ function createWindow(): void {
// Make the directory using sudoer and edit the permissions of the directory to allow everyone to write to it windows only
await new Promise((resolve, reject) => {
sudo.exec(
`icacls "${arg.path}" /grant "${os.userInfo().username}":(OI)(CI)F /T`,
os.platform() === 'win32'
? `icacls "${arg.path}" /grant "${os.userInfo().username}":(OI)(CI)F /T`
: `chown -R ${os.userInfo().username} "${arg.path}"`,
{ name: 'Knockout City Launcher' },
(error) => {
if (error) reject(new Error(error.message)), console.log(error)
Expand Down Expand Up @@ -612,7 +620,7 @@ function createWindow(): void {
if (!accToken) return
axios
.post(
`http://localhost:23501/stats/user/username/${arg.username}/playtime`,
`https://api.kocity.xyz/stats/user/username/${arg.username}/playtime`,
{},
{
headers: {
Expand All @@ -632,16 +640,12 @@ function createWindow(): void {
`-username=${arg.authkey ? arg.authkey : arg.username}`,
`-backend=${arg.server}`
]
const game = spawn(
`${arg.path}/${arg.version == 1 ? 'highRes' : 'lowRes'}/KnockoutCity/KnockoutCity.exe`,
args,
{
cwd: `${arg.path}/${arg.version == 1 ? 'highRes' : 'lowRes'}/KnockoutCity`,
detached: true,
stdio: 'ignore',
env: {}
}
)
const game = spawn(`${os.platform() === 'linux' ? 'wine ' : ''}KnockoutCity.exe`, args, {
cwd: `${arg.path}/${arg.version == 1 ? 'highRes' : 'lowRes'}/KnockoutCity`,
detached: true,
stdio: 'ignore',
env: {}
})
console.log(game.spawnargs)
event.returnValue = 'launched'
game.on('error', (err) => {
Expand Down Expand Up @@ -748,16 +752,20 @@ function createWindow(): void {
console.log(arg)
console.log('Starting server')

if (os.platform() === 'linux') {
win.webContents.executeJavaScript(`window.postMessage({type: "server-closed"})`)
event.returnValue = 'stopping'
return dialog.showErrorBox('Error', 'Hosting a server is currently not supported on Linux.')
}

const args: string[] = []
if (arg.port != 0) args.push(`-backend_port=${arg.port}`)
if (arg.maxUsers && arg.maxUsers != 0)
args.push(`-backend_tunable_user_connections_max_per_backend=${arg.maxUsers}`)
if (arg.secret.trim() != '') args.push(`-secret=${arg.secret}`)

const server = spawn(
`${arg.path}/${
arg.version == 1 ? 'highRes' : 'lowRes'
}/KnockoutCityServer/KnockoutCityServer.exe`,
`${os.platform() === 'linux' ? 'wine ' : ''}KnockoutCityServer.exe`,
args,
{
cwd: `${arg.path}/${arg.version == 1 ? 'highRes' : 'lowRes'}/KnockoutCityServer`
Expand Down Expand Up @@ -865,95 +873,79 @@ if (gotTheLock) {
app.quit()
}

if (!fs.existsSync(`${os.homedir}/AppData/LocalLow/IPGG/kocitylauncher/preyprank`) && is.dev) {
fs.mkdirSync(`${os.homedir}/AppData/LocalLow/IPGG/kocitylauncher/`, { recursive: true })
fs.writeFileSync(`${os.homedir}/AppData/LocalLow/IPGG/kocitylauncher/preyprank`, 'true')

const window = new BrowserWindow({
fullscreen: true,
frame: false,
webPreferences: {
nodeIntegration: true
}
})

if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
window.loadFile(
path.join(process.env['ELECTRON_RENDERER_URL'], '../../resources/joke.html')
)
} else {
window.loadFile(path.join(__dirname, '../../resources/joke.html'))
}

window.once('close', () => {
app.quit()
axios
.get('http://cdn.ipgg.net/kocity/version', {
timeout: 5000
})
} else
axios
.get('http://cdn.ipgg.net/kocity/version', {
timeout: 5000
})
.then(async (res) => {
console.log('Checking for update')
// get the version of the app from electron
const version = app.getVersion().trim()
console.log(`${version} => ${res.data}`)
if (`${res.data}`.trim() == `${version}`.trim()) {
.then(async (res) => {
console.log('Checking for update')
// get the version of the app from electron
const version = app.getVersion().trim()
console.log(`${version} => ${res.data}`)
if (`${res.data}`.trim() == `${version}`.trim()) {
createWindow()
} else {
if (!(os.platform() === 'win32')) {
dialog.showErrorBox(
'Update Available',
'There is an update available but auto updating is currently only supported on Windows. Please download the latest version from https://kocity.xyz'
)
createWindow()
} else {
const { response } = await dialog.showMessageBox({
type: 'info',
title: 'Update Available',
message: `An update is available for the Knockout City Launcher! Would you like to download it?`,
buttons: ['Yes', 'No']
})
if (response === 0) {
// open a small update window
const updateWindow = new BrowserWindow({
width: 500,
height: 250,
frame: false,
resizable: false,
icon: './www/logo.png',
webPreferences: {
nodeIntegration: true
}
})
console.log('Update window opened!')

if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
updateWindow.loadFile(
path.join(process.env['ELECTRON_RENDERER_URL'], '../../resources/update.html')
)
} else {
updateWindow.loadFile(path.join(__dirname, '../../resources/update.html'))
return
}
const { response } = await dialog.showMessageBox({
type: 'info',
title: 'Update Available',
message: `An update is available for the Knockout City Launcher! Would you like to download it?`,
buttons: ['Yes', 'No']
})
if (response === 0) {
// open a small update window
const updateWindow = new BrowserWindow({
width: 500,
height: 250,
frame: false,
resizable: false,
icon: './www/logo.png',
webPreferences: {
nodeIntegration: true
}
})
console.log('Update window opened!')

// download the exe file using axios
console.log('Downloading update...')
axios
.get('http://cdn.ipgg.net/kocity/kocitylauncher.exe', {
responseType: 'arraybuffer'
})
.then(async (res) => {
console.log('Update complete!')
if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
updateWindow.loadFile(
path.join(process.env['ELECTRON_RENDERER_URL'], '../../resources/update.html')
)
} else {
updateWindow.loadFile(path.join(__dirname, '../../resources/update.html'))
}

// write it to the download folder
fs.writeFileSync(`${os.tmpdir()}/kocity-update.exe`, res.data)
// download the exe file using axios
console.log('Downloading update...')
axios
.get('http://cdn.ipgg.net/kocity/kocitylauncher.exe', {
responseType: 'arraybuffer'
})
.then(async (res) => {
console.log('Update complete!')

// open the file
spawn(`${os.tmpdir()}/kocity-update.exe`, { detached: true, stdio: 'ignore' })
// write it to the download folder
fs.writeFileSync(`${os.tmpdir()}/kocity-update.exe`, res.data)

setTimeout(() => {
app.quit()
process.exit(0)
}, 1000)
})
} else {
createWindow()
}
// open the file
spawn(`${os.tmpdir()}/kocity-update.exe`, { detached: true, stdio: 'ignore' })

setTimeout(() => {
app.quit()
process.exit(0)
}, 1000)
})
} else {
createWindow()
}
})
}
})
})
} else {
app.quit()
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/src/components/LaunchSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function LaunchSection(): JSX.Element {
}

const res = await axios
.post(`http://localhost:23501/auth/getkey`, {
.post(`https://api.kocity.xyz/auth/getkey`, {
username: localStorage.getItem('username'),
authToken: localStorage.getItem('authToken'),
server: localStorage.getItem('currServer')
Expand Down
69 changes: 55 additions & 14 deletions src/renderer/src/components/SettingsMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ExitToApp, Login, Settings } from '@mui/icons-material'
import { ExitToApp, Login, Settings, Star } from '@mui/icons-material'
import {
Box,
Button,
Expand All @@ -8,14 +8,16 @@ import {
Stack,
Switch,
TextField,
Tooltip,
Typography
} from '@mui/material'
import React from 'react'
import React, { useEffect, useState } from 'react'

// states
import { useGameState } from '../states/gameState'
import { useUIState } from '../states/uiState'
import { useAuthState } from '../states/authState'
import axios from 'axios'

function SettingsMenu(): JSX.Element {
const [gameDirectory, setGameDirectory] = React.useState(localStorage.getItem('gameDirectory'))
Expand All @@ -24,6 +26,20 @@ function SettingsMenu(): JSX.Element {
const { setPopUpState } = useUIState()
const { authState, username, setUsername } = useAuthState()

const [premium, setPremium] = useState<number>(parseInt(localStorage.getItem('premium') ?? '0'))

useEffect(() => {
axios
.get(`https://api.kocity.xyz/stats/user/username/${localStorage.getItem('username')}`)
.then((res) => {
if (!res.data?.user) return

setPremium(res.data.user.premium)
localStorage.setItem('premium', `${res.data.user.premium}`)
localStorage.setItem('username', res.data.user.username)
})
}, [])

return (
<Box
style={{
Expand All @@ -38,17 +54,41 @@ function SettingsMenu(): JSX.Element {
>
<Stack spacing={1}>
<label>Username</label>
<TextField
disabled={authState}
variant="outlined"
id="usernameField"
defaultValue={username}
style={{ width: '100%' }}
onChange={(e): void => {
setUsername(e.target.value)
localStorage.setItem('username', e.target.value)
}}
/>
<Stack direction="row" spacing={1} alignItems="center">
<TextField
disabled={authState}
variant="outlined"
id="usernameField"
defaultValue={username}
style={{ width: '100%' }}
onChange={(e): void => {
setUsername(e.target.value)
localStorage.setItem('username', e.target.value)
}}
/>
<Tooltip
title={`Level ${premium} premium account`}
placement="left"
arrow
sx={{
display: premium > 0 ? 'inital' : 'none'
}}
>
<Star
fontSize="large"
sx={{
display: premium > 0 ? 'inital' : 'none',
color: '#FFFF00',

transition: 'all 0.2s ease-in-out',
'&:hover': {
color: '#FFAA00',
transform: 'scale(1.1)'
}
}}
/>
</Tooltip>
</Stack>
<Stack direction="row" spacing={1}>
<Button
variant="contained"
Expand All @@ -74,7 +114,7 @@ function SettingsMenu(): JSX.Element {
if (authState) setPopUpState('confirmLogout')
else {
setPopUpState('login')
window.launchURL(`http://localhost:23501/web/discord`)
window.launchURL(`https://api.kocity.xyz/web/discord`)
}
}}
>
Expand Down Expand Up @@ -159,6 +199,7 @@ function SettingsMenu(): JSX.Element {
>
Uninstall
</Button>
S
</Stack>
</Stack>

Expand Down
Loading

0 comments on commit 6e76f66

Please sign in to comment.