@@ -9,42 +9,53 @@ import FsProvider from '../../providers/generic/file/FsProvider';
9
9
import FileWriteError from '../../model/errors/FileWriteError' ;
10
10
import ThunderstoreDownloaderProvider from '../../providers/ror2/downloading/ThunderstoreDownloaderProvider' ;
11
11
import ManagerInformation from '../../_managerinf/ManagerInformation' ;
12
- import { generateProgressPercentage } from '../../utils/DownloadUtils ' ;
12
+ import ManagerSettings from '../manager/ManagerSettings ' ;
13
13
14
14
export default class BetterThunderstoreDownloader extends ThunderstoreDownloaderProvider {
15
15
16
16
public async download (
17
17
combos : ThunderstoreCombo [ ] ,
18
18
ignoreCache : boolean ,
19
- totalProgressCallback : ( progress : number , modName : string , status : number , err : R2Error | null ) => void
19
+ settings : ManagerSettings ,
20
+ totalProgressCallback : ( progress : number , downloadedSize : number , totalDownloadSize : number , modName : string , status : number , err : R2Error | null ) => void
20
21
) : Promise < void > {
21
22
let modInProgressName = combos [ 0 ] . getMod ( ) . getName ( ) ;
22
23
let downloadCount = 0 ;
24
+ let finishedModsDownloadedSize = 0 ;
25
+ let modInProgressSizeProgress = 0 ;
23
26
24
- // Mark the mod 80% processed when the download completes, save the remaining 20% for extracting.
25
- const singleModProgressCallback = ( downloadProgress : number , status : number , err : R2Error | null ) => {
27
+ const totalDownloadSize = await this . getTotalDownloadSizeInBytes ( combos , settings ) ;
28
+
29
+ const singleModProgressCallback = ( downloadProgress : number , modInProgressSize : number , status : number , err : R2Error | null ) => {
26
30
if ( status === StatusEnum . FAILURE ) {
27
31
throw err ;
28
32
}
29
33
30
- let totalDownloadProgress : number ;
31
34
if ( status === StatusEnum . PENDING ) {
32
- totalDownloadProgress = generateProgressPercentage ( downloadProgress * 0.8 , downloadCount , combos . length ) ;
35
+ modInProgressSizeProgress = ( downloadProgress / 100 ) * modInProgressSize ;
33
36
} else if ( status === StatusEnum . SUCCESS ) {
34
- totalDownloadProgress = generateProgressPercentage ( 100 , downloadCount , combos . length ) ;
37
+ finishedModsDownloadedSize += modInProgressSize ;
38
+ modInProgressSizeProgress = 0 ;
35
39
downloadCount += 1 ;
36
40
} else {
37
41
console . error ( `Ignore unknown status code "${ status } "` ) ;
38
42
return ;
39
43
}
40
- totalProgressCallback ( Math . round ( totalDownloadProgress ) , modInProgressName , status , err ) ;
44
+ totalProgressCallback (
45
+ Math . round ( finishedModsDownloadedSize / totalDownloadSize * 100 ) ,
46
+ finishedModsDownloadedSize + modInProgressSizeProgress ,
47
+ totalDownloadSize ,
48
+ modInProgressName ,
49
+ status ,
50
+ err
51
+ ) ;
41
52
}
42
53
43
54
for ( const comboInProgress of combos ) {
44
55
modInProgressName = comboInProgress . getMod ( ) . getName ( ) ;
45
56
46
57
if ( ! ignoreCache && await this . isVersionAlreadyDownloaded ( comboInProgress ) ) {
47
- singleModProgressCallback ( 100 , StatusEnum . SUCCESS , null ) ;
58
+ singleModProgressCallback ( 100 , 0 , StatusEnum . SUCCESS , null ) ;
48
59
continue ;
49
60
}
50
61
@@ -57,10 +68,15 @@ export default class BetterThunderstoreDownloader extends ThunderstoreDownloader
57
68
}
58
69
}
59
70
60
- private async _downloadCombo ( combo : ThunderstoreCombo , callback : ( progress : number , status : number , err : R2Error | null ) => void ) : Promise < AxiosResponse > {
71
+ private async _downloadCombo ( combo : ThunderstoreCombo , callback : ( progress : number , downloadedSize : number , status : number , err : R2Error | null ) => void ) : Promise < AxiosResponse > {
61
72
return axios . get ( combo . getVersion ( ) . getDownloadUrl ( ) , {
62
73
onDownloadProgress : progress => {
63
- callback ( ( progress . loaded / progress . total ) * 100 , StatusEnum . PENDING , null ) ;
74
+ callback (
75
+ ( progress . loaded / progress . total ) * 100 ,
76
+ combo . getVersion ( ) . getFileSize ( ) ,
77
+ StatusEnum . PENDING ,
78
+ null
79
+ ) ;
64
80
} ,
65
81
responseType : 'arraybuffer' ,
66
82
headers : {
@@ -70,14 +86,14 @@ export default class BetterThunderstoreDownloader extends ThunderstoreDownloader
70
86
} ) ;
71
87
}
72
88
73
- private async _saveDownloadResponse ( response : AxiosResponse , combo : ThunderstoreCombo , callback : ( progress : number , status : number , err : R2Error | null ) => void ) : Promise < void > {
89
+ private async _saveDownloadResponse ( response : AxiosResponse , combo : ThunderstoreCombo , callback : ( progress : number , downloadedSize : number , status : number , err : R2Error | null ) => void ) : Promise < void > {
74
90
const buf : Buffer = Buffer . from ( response . data )
75
- callback ( 100 , StatusEnum . PENDING , null ) ;
91
+ callback ( 100 , combo . getVersion ( ) . getFileSize ( ) , StatusEnum . PENDING , null ) ;
76
92
await this . saveToFile ( buf , combo , ( success : boolean , error ?: R2Error ) => {
77
93
if ( success ) {
78
- callback ( 100 , StatusEnum . SUCCESS , error || null ) ;
94
+ callback ( 100 , combo . getVersion ( ) . getFileSize ( ) , StatusEnum . SUCCESS , null ) ;
79
95
} else {
80
- callback ( 100 , StatusEnum . FAILURE , error || null ) ;
96
+ callback ( 100 , combo . getVersion ( ) . getFileSize ( ) , StatusEnum . FAILURE , error || null ) ;
81
97
}
82
98
} ) ;
83
99
}
@@ -120,4 +136,14 @@ export default class BetterThunderstoreDownloader extends ThunderstoreDownloader
120
136
}
121
137
}
122
138
139
+ public async getTotalDownloadSizeInBytes ( combos : ThunderstoreCombo [ ] , settings : ManagerSettings ) : Promise < number > {
140
+ const filteredList = combos . filter ( async value => ! ( await this . isVersionAlreadyDownloaded ( value ) ) || settings . getContext ( ) . global . ignoreCache )
141
+ . map ( value => value . getVersion ( ) . getFileSize ( ) ) ;
142
+ if ( filteredList . length > 0 ) {
143
+ return filteredList . reduce ( ( previousValue , currentValue ) => previousValue + currentValue ) || 0 ;
144
+ } else {
145
+ return 0 ;
146
+ }
147
+ }
148
+
123
149
}
0 commit comments