Skip to content

Commit

Permalink
added settings to disable auto-update
Browse files Browse the repository at this point in the history
  • Loading branch information
Kensaa committed Apr 30, 2023
1 parent 157da8f commit 50564a3
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 108 deletions.
236 changes: 128 additions & 108 deletions launcher/electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ const defaultConfig = {
primaryServer,
cdnServer: '',
jrePath: '',
closeLauncher: true
closeLauncher: true,
disableAutoUpdate: false
}

let loginInfo: msmc.result | null
let config: Record<string, any> | null
let config: Record<string, any>

async function createWindow() {
console.log('createWindow')
Expand Down Expand Up @@ -94,6 +95,17 @@ async function createWindow() {
config = JSON.parse(
fs.readFileSync(path.join(configFolder, 'config.json'), 'utf-8')
)
// checking if config is missing field
if (Object.keys(config).length !== Object.keys(defaultConfig).length) {
console.log(
'config seems to be missing some fields, using default config'
)
config = defaultConfig
fs.writeFileSync(
path.join(configFolder, 'config.json'),
JSON.stringify(config, null, 4)
)
}
console.log('parsed existing config:')
console.log(config)
}
Expand Down Expand Up @@ -268,70 +280,88 @@ ipcMain.handle('start-game', async (event, args: Profile) => {
}
forgeArgs = forgePath
}

if (args.gameFolder) {
console.log('a forced game folder is detected, downloading it...')
const localPath = path.join(
config.rootDir,
'profiles',
args.gameFolder
)
checkExist(localPath)

const hashTree = (await JSONFetch(
urlJoin(primaryServer, '/hashes')
)) as any
const remoteTree = hashTree['gameFolders'][args.gameFolder]
const fileCount = (
(await JSONFetch(
urlJoin(primaryServer, '/fileCount', args.gameFolder)
)) as { count: number }
).count

console.log('remote tree fetched')
let localTree = await folderTree(localPath)
console.log('local tree created')
function getFolders(tree: any) {
return Object.keys(tree).filter(
key => typeof tree[key] !== 'string'
if (!config.disableAutoUpdate) {
if (args.gameFolder) {
console.log(
'a forced game folder is detected, downloading it...'
)
}
const remoteFolders = getFolders(remoteTree)
const localFolders = getFolders(localTree)
const localPath = path.join(
config.rootDir,
'profiles',
args.gameFolder
)
checkExist(localPath)

const hashTree = (await JSONFetch(
urlJoin(primaryServer, '/hashes')
)) as any
const remoteTree = hashTree['gameFolders'][args.gameFolder]
const fileCount = (
(await JSONFetch(
urlJoin(primaryServer, '/fileCount', args.gameFolder)
)) as { count: number }
).count

console.log('remote tree fetched')
let localTree = await folderTree(localPath)
console.log('local tree created')
function getFolders(tree: any) {
return Object.keys(tree).filter(
key => typeof tree[key] !== 'string'
)
}
const remoteFolders = getFolders(remoteTree)
const localFolders = getFolders(localTree)

console.log('starting update procedure')
for (const folder of remoteFolders) {
if (!localFolders.includes(folder)) {
fs.mkdirSync(path.join(localPath, folder))
console.log('starting update procedure')
for (const folder of remoteFolders) {
if (!localFolders.includes(folder)) {
fs.mkdirSync(path.join(localPath, folder))
}
}
}
localTree = await folderTree(localPath)

let count = 0
for (const folder of remoteFolders) {
//start recursive function which will download all files for all the folders
await downloadFolder(
remoteTree[folder],
localTree[folder],
path.join(args.gameFolder, folder),
path.join(localPath, folder)
)
}
async function downloadFolder(
remoteFolder,
localFolder,
gameFolder: string,
folderPath: string,
pathA: string[] = []
) {
for (const element of Object.keys(remoteFolder)) {
if (typeof remoteFolder[element] === 'string') {
if (localFolder[element] !== undefined) {
if (
(await getHash(
path.join(folderPath, ...pathA, element)
)) !== remoteFolder[element]
) {
localTree = await folderTree(localPath)

let count = 0
for (const folder of remoteFolders) {
//start recursive function which will download all files for all the folders
await downloadFolder(
remoteTree[folder],
localTree[folder],
path.join(args.gameFolder, folder),
path.join(localPath, folder)
)
}
async function downloadFolder(
remoteFolder,
localFolder,
gameFolder: string,
folderPath: string,
pathA: string[] = []
) {
for (const element of Object.keys(remoteFolder)) {
if (typeof remoteFolder[element] === 'string') {
if (localFolder[element] !== undefined) {
if (
(await getHash(
path.join(folderPath, ...pathA, element)
)) !== remoteFolder[element]
) {
await download(
urlJoin(
downloadServer,
'/static/gameFolders',
gameFolder,
...pathA,
element
),
path.join(folderPath, ...pathA, element)
)
count++
startProgress = Math.round(
(count / fileCount) * 100
)
}
} else {
await download(
urlJoin(
downloadServer,
Expand All @@ -348,57 +378,47 @@ ipcMain.handle('start-game', async (event, args: Profile) => {
)
}
} else {
await download(
urlJoin(
downloadServer,
'/static/gameFolders',
if (localFolder[element]) {
await downloadFolder(
remoteFolder[element],
localFolder[element],
gameFolder,
...pathA,
element
),
path.join(folderPath, ...pathA, element)
)
count++
startProgress = Math.round(
(count / fileCount) * 100
)
}
} else {
if (localFolder[element]) {
await downloadFolder(
remoteFolder[element],
localFolder[element],
gameFolder,
folderPath,
pathA.concat(element)
)
} else {
fs.mkdirSync(
path.join(folderPath, ...pathA, element)
)
await downloadFolder(
remoteFolder[element],
{},
gameFolder,
folderPath,
pathA.concat(element)
)
folderPath,
pathA.concat(element)
)
} else {
fs.mkdirSync(
path.join(folderPath, ...pathA, element)
)
await downloadFolder(
remoteFolder[element],
{},
gameFolder,
folderPath,
pathA.concat(element)
)
}
}
}
const onlyLocalFile = Object.keys(localFolder)
.filter(key => typeof localFolder[key] === 'string')
.filter(key => !Object.keys(remoteFolder).includes(key))
for (const file of onlyLocalFile) {
console.log('deleting file : ' + file)
fs.rmSync(path.join(folderPath, ...pathA, file), {
recursive: true
})
}
}
const onlyLocalFile = Object.keys(localFolder)
.filter(key => typeof localFolder[key] === 'string')
.filter(key => !Object.keys(remoteFolder).includes(key))
for (const file of onlyLocalFile) {
fs.rmSync(path.join(folderPath, ...pathA, file), {
recursive: true
})
}
} else {
console.log(
'no forced game folder detected, creating an empty one...'
)
args.gameFolder = args.name
.replace(/[^a-zA-Z0-9]/g, '_')
.toLowerCase()
}
} else {
console.log(
'no forced game folder detected, creating an empty one...'
)
args.gameFolder = args.name
.replace(/[^a-zA-Z0-9]/g, '_')
.toLowerCase()
Expand Down
15 changes: 15 additions & 0 deletions launcher/src/pages/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,21 @@ export default function Home({
{error}
</Alert>
)}
{config.disableAutoUpdate && (
<Alert
className=' position-absolute'
style={{
zIndex: 1,
margin: '100px',
textAlign: 'center'
}}
variant='info'
onClose={() => setError('')}
>
you disabled auto-update, the game WILL NOT be updated while
this option is enabled
</Alert>
)}
<HomeHeader
{...{
setOverlay,
Expand Down
9 changes: 9 additions & 0 deletions launcher/src/pages/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ export default function Settings({ hide }: SettingsProps) {
const [cdnServer, setCdnServer] = useState(config.cdnServer)
const [jrePath, setJrePath] = useState(config.jrePath)
const [closeLauncher, setCloseLauncher] = useState(config.closeLauncher)
const [disableAutoUpdate, setDisableAutoUpdate] = useState(
config.disableAutoUpdate
)

const [validated, setValidated] = useState(false)

Expand All @@ -31,6 +34,7 @@ export default function Settings({ hide }: SettingsProps) {
config.setCdnServer(cdnServer)
config.setJrePath(jrePath)
config.setCloseLauncher(closeLauncher)
config.setDisableAutoUpdate(disableAutoUpdate)

setValidated(true)
hide()
Expand Down Expand Up @@ -76,6 +80,11 @@ export default function Settings({ hide }: SettingsProps) {
value={closeLauncher}
setter={setCloseLauncher as Setter}
/>
<BooleanInput
label='Disable auto-update'
value={disableAutoUpdate}
setter={setDisableAutoUpdate as Setter}
/>
</div>
<Button type='submit'>Save</Button>
</Form>
Expand Down
10 changes: 10 additions & 0 deletions launcher/src/stores/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ interface configStore {
cdnServer: string
jrePath: string
closeLauncher: boolean
disableAutoUpdate: boolean
setRootDir: (dir: string) => void
setRam: (ram: number) => void
setPrimaryServer: (primaryServer: string) => void
setCdnServer: (cdnServer: string) => void
setJrePath: (jrePath: string) => void
setCloseLauncher: (closeLauncher: boolean) => void
setDisableAutoUpdate: (disableAutoUpdate: boolean) => void
}

export default create<configStore>(set => {
Expand All @@ -26,6 +28,7 @@ export default create<configStore>(set => {
cdnServer: config.cdnServer,
jrePath: config.jrePath,
closeLauncher: config.closeLauncher,
disableAutoUpdate: config.disableAutoUpdate,
setRootDir: (rootDir: string) => {
set({ rootDir })
ipcRenderer.send('set-config', JSON.stringify({ rootDir }))
Expand All @@ -52,6 +55,13 @@ export default create<configStore>(set => {
setCloseLauncher: (closeLauncher: boolean) => {
set({ closeLauncher })
ipcRenderer.send('set-config', JSON.stringify({ closeLauncher }))
},
setDisableAutoUpdate: (disableAutoUpdate: boolean) => {
set({ disableAutoUpdate })
ipcRenderer.send(
'set-config',
JSON.stringify({ disableAutoUpdate })
)
}
}
})

0 comments on commit 50564a3

Please sign in to comment.