1
1
import { PACKAGE } from '../../constants'
2
2
import { getCurrentVersion , getLatestVersion } from './versionManager'
3
3
4
- import EVENTS from '@events'
5
- import download from 'download '
4
+ import EVENTS from '@aj/util/ events'
5
+ import { app } from 'electron '
6
6
import { type Unzipped } from 'fflate'
7
7
import {
8
8
showOfflineError ,
@@ -15,20 +15,48 @@ const ASSET_OVERRIDES: Record<string, string> = {}
15
15
async function downloadJar ( url : string , savePath : string ) {
16
16
updateLoadingProgressLabel ( 'Downloading Minecraft Assets...' )
17
17
18
- const data = await download ( url , { retry : { retries : 3 } } )
19
- . on ( 'downloadProgress' , progress => {
20
- updateLoadingProgress ( progress . percent * 100 )
21
- } )
22
- . catch ( ( error : any ) => {
23
- console . error ( 'Failed to download Minecraft client:' , error )
24
- } )
25
-
26
- if ( ! data ) {
18
+ let attempts = 3
19
+ let failed = false
20
+ let sink : Uint8Array
21
+ do {
22
+ try {
23
+ const res = await fetch ( url )
24
+ if ( ! res ) throw new Error ( 'Failed to fetch Minecraft client.' )
25
+ const reader = res . body ?. getReader ( )
26
+ if ( ! reader ) throw new Error ( 'Failed to get reader from Minecraft client response.' )
27
+ const length = parseInt ( res . headers . get ( 'Content-Length' ) ?? '0' )
28
+ sink = new Uint8Array ( length )
29
+ let offset = 0
30
+ while ( true ) {
31
+ const { done, value } = await reader . read ( )
32
+ if ( done ) break
33
+ if ( ! value ) throw new Error ( 'Failed to read Minecraft client response.' )
34
+ sink . set ( value , offset )
35
+ offset += value . length
36
+ updateLoadingProgress ( ( offset / length ) * 100 )
37
+ }
38
+ failed = false
39
+ } catch ( e ) {
40
+ console . error ( 'Failed to download Minecraft client:' , e )
41
+ failed = true
42
+ } finally {
43
+ attempts --
44
+ }
45
+ } while ( attempts > 0 )
46
+ // const data = await download(url, { retry: { retries: 3 } })
47
+ // .on('downloadProgress', progress => {
48
+ // updateLoadingProgress(progress.percent * 100)
49
+ // })
50
+ // .catch((error: any) => {
51
+ // console.error('Failed to download Minecraft client:', error)
52
+ // })
53
+
54
+ if ( failed ) {
27
55
showOfflineError ( )
28
56
throw new Error ( 'Failed to download Minecraft client after 3 retries.' )
29
57
}
30
58
31
- await fs . promises . writeFile ( savePath , data )
59
+ await fs . promises . writeFile ( savePath , sink ! )
32
60
}
33
61
34
62
export async function getLatestVersionClientDownloadUrl ( ) {
@@ -55,7 +83,7 @@ export async function getLatestVersionClientDownloadUrl() {
55
83
}
56
84
57
85
function getCachedJarFilePath ( ) {
58
- const userDataPath = electron . app . getPath ( 'userData' )
86
+ const userDataPath = ( electron ? .app ?? app ) . getPath ( 'userData' )
59
87
return PathModule . join ( userDataPath , `${ PACKAGE . name } /latest.jar` )
60
88
}
61
89
0 commit comments