@@ -83,14 +83,17 @@ export default class BetterThunderstoreDownloader extends ThunderstoreDownloader
83
83
} ) ;
84
84
}
85
85
86
- public async download ( profile : ImmutableProfile , mod : ThunderstoreMod , modVersion : ThunderstoreVersion ,
87
- ignoreCache : boolean ,
88
- callback : ( progress : number , modName : string , status : number , err : R2Error | null ) => void ,
89
- completedCallback : ( modList : ThunderstoreCombo [ ] ) => void ) {
86
+ public async download (
87
+ profile : ImmutableProfile , mod : ThunderstoreMod , modVersion : ThunderstoreVersion ,
88
+ ignoreCache : boolean ,
89
+ callback : ( progress : number , modName : string , status : number , err : R2Error | null ) => void
90
+ ) : Promise < ThunderstoreCombo [ ] > {
91
+
90
92
91
93
const modList = await ProfileModList . getModList ( profile ) ;
92
94
if ( modList instanceof R2Error ) {
93
- return callback ( 0 , mod . getName ( ) , StatusEnum . FAILURE , modList ) ;
95
+ callback ( 0 , mod . getName ( ) , StatusEnum . FAILURE , modList ) ;
96
+ throw modList ;
94
97
}
95
98
96
99
let dependencies : ThunderstoreCombo [ ] = [ ] ;
@@ -101,40 +104,41 @@ export default class BetterThunderstoreDownloader extends ThunderstoreDownloader
101
104
combo . setVersion ( modVersion ) ;
102
105
103
106
dependencies = await this . determineDependencyVersions ( dependencies , combo , modVersion , modList ) ;
104
- let downloadableDependencySize = this . calculateInitialDownloadSize ( dependencies ) ;
107
+ const allModsToDownload = [ ... dependencies , combo ] ;
105
108
106
109
let downloadCount = 0 ;
107
- await this . downloadAndSave ( combo , ignoreCache , async ( progress : number , status : number , err : R2Error | null ) => {
110
+ const singleModProgressCallback = ( progress : number , status : number , err : R2Error | null ) => {
108
111
if ( status === StatusEnum . FAILURE ) {
109
- callback ( 0 , mod . getName ( ) , status , err ) ;
110
- } else if ( status === StatusEnum . PENDING ) {
111
- callback ( this . generateProgressPercentage ( progress , downloadCount , downloadableDependencySize + 1 ) , mod . getName ( ) , status , err ) ;
112
+ throw err ;
113
+ }
114
+
115
+ let totalProgress : number ;
116
+ if ( status === StatusEnum . PENDING ) {
117
+ totalProgress = this . generateProgressPercentage ( progress , downloadCount , allModsToDownload . length ) ;
112
118
} else if ( status === StatusEnum . SUCCESS ) {
119
+ totalProgress = this . generateProgressPercentage ( 100 , downloadCount , allModsToDownload . length ) ;
113
120
downloadCount += 1 ;
114
- // If no dependencies, end here.
115
- if ( dependencies . length === 0 ) {
116
- callback ( 100 , mod . getName ( ) , StatusEnum . PENDING , err ) ;
117
- completedCallback ( [ combo ] ) ;
118
- return ;
119
- }
121
+ } else {
122
+ console . error ( `Ignore unknown status code " ${ status } "` ) ;
123
+ return ;
124
+ }
125
+ callback ( totalProgress , mod . getName ( ) , status , err ) ;
126
+ }
120
127
121
- // If dependencies, queue and download.
122
- await this . queueDownloadDependencies ( dependencies . entries ( ) , ignoreCache , ( progress : number , modName : string , status : number , err : R2Error | null ) => {
123
- if ( status === StatusEnum . FAILURE ) {
124
- callback ( 0 , modName , status , err ) ;
125
- } else if ( status === StatusEnum . PENDING ) {
126
- callback ( this . generateProgressPercentage ( progress , downloadCount , downloadableDependencySize + 1 ) , modName , status , err ) ;
127
- } else if ( status === StatusEnum . SUCCESS ) {
128
- callback ( this . generateProgressPercentage ( progress , downloadCount , downloadableDependencySize + 1 ) , modName , StatusEnum . PENDING , err ) ;
129
- downloadCount += 1 ;
130
- if ( downloadCount >= dependencies . length + 1 ) {
131
- callback ( 100 , modName , StatusEnum . PENDING , err ) ;
132
- completedCallback ( [ ...dependencies , combo ] ) ;
133
- }
134
- }
135
- } ) ;
128
+ for ( const combo of allModsToDownload ) {
129
+ if ( ! ignoreCache && await this . isVersionAlreadyDownloaded ( combo ) ) {
130
+ callback ( 100 , combo . getMod ( ) . getName ( ) , StatusEnum . SUCCESS , null ) ;
131
+ continue ;
136
132
}
137
- } )
133
+
134
+ try {
135
+ const response = await this . _downloadCombo ( combo , singleModProgressCallback ) ;
136
+ await this . _saveDownloadResponse ( response , combo , singleModProgressCallback ) ;
137
+ } catch ( e ) {
138
+ throw R2Error . fromThrownValue ( e , `Failed to download mod ${ combo . getVersion ( ) . getFullName ( ) } ` ) ;
139
+ }
140
+ }
141
+ return allModsToDownload ;
138
142
}
139
143
140
144
// If combo is a modpack, use the modpack's dependency versions. If it isn't, get the latest versions.
@@ -185,7 +189,7 @@ export default class BetterThunderstoreDownloader extends ThunderstoreDownloader
185
189
186
190
try {
187
191
const response = await this . _downloadCombo ( combo , singleModProgressCallback ) ;
188
- await this . _saveDownloadResponse ( response , combo , singleModProgressCallback ) ;
192
+ await this . _saveDownloadResponse ( response , combo , singleModProgressCallback ) ; //TODO: track installation separately
189
193
} catch ( e ) {
190
194
throw R2Error . fromThrownValue ( e , `Failed to download mod ${ combo . getVersion ( ) . getFullName ( ) } ` ) ;
191
195
}
0 commit comments