23
23
import me .itzg .helpers .errors .GenericException ;
24
24
import me .itzg .helpers .errors .InvalidParameterException ;
25
25
import me .itzg .helpers .files .Manifests ;
26
+ import me .itzg .helpers .http .Fetch ;
26
27
import me .itzg .helpers .http .SharedFetchArgs ;
27
- import me .itzg .helpers .http .Uris ;
28
28
import me .itzg .helpers .json .ObjectMappers ;
29
29
import me .itzg .helpers .modrinth .model .DependencyType ;
30
30
import me .itzg .helpers .modrinth .model .Project ;
@@ -109,6 +109,7 @@ private List<Path> processProjects(List<String> projects) {
109
109
return
110
110
modrinthApiClient .bulkGetProjects (
111
111
projects .stream ()
112
+ .filter (s -> !s .trim ().isEmpty ())
112
113
.map (ProjectRef ::parse )
113
114
)
114
115
.defaultIfEmpty (Collections .emptyList ())
@@ -148,31 +149,28 @@ private Stream<Version> expandDependencies(ModrinthApiClient modrinthApiClient,
148
149
.filter (dep -> projectsProcessed .add (dep .getProjectId ()))
149
150
.flatMap (dep -> {
150
151
projectsProcessed .add (dep .getProjectId ());
151
- try {
152
- final Version depVersion ;
153
- if (dep .getVersionId () == null ) {
154
- log .debug ("Fetching versions of dep={} and picking" , dep );
155
- depVersion = pickVersion (
156
- getVersionsForProject (modrinthApiClient , dep .getProjectId ())
157
- );
158
- }
159
- else {
160
- log .debug ("Fetching version for dep={}" , dep );
161
- depVersion = getVersion (dep .getVersionId ());
162
- }
163
- if (depVersion != null ) {
164
- log .debug ("Resolved version={} for dep={}" , depVersion .getVersionNumber (), dep );
165
- return Stream .concat (
166
- Stream .of (depVersion ),
167
- expandDependencies (modrinthApiClient , depVersion )
168
- )
169
- .peek (expandedVer -> log .debug ("Expanded dependency={} into version={}" , dep , expandedVer ));
170
- }
171
- else {
172
- return Stream .empty ();
173
- }
174
- } catch (IOException e ) {
175
- throw new RuntimeException (e );
152
+ final Version depVersion ;
153
+ if (dep .getVersionId () == null ) {
154
+ log .debug ("Fetching versions of dep={} and picking" , dep );
155
+ depVersion = pickVersion (
156
+ getVersionsForProject (modrinthApiClient , dep .getProjectId ())
157
+ );
158
+ }
159
+ else {
160
+ log .debug ("Fetching version for dep={}" , dep );
161
+ depVersion = modrinthApiClient .getVersionFromId (dep .getVersionId ())
162
+ .block ();
163
+ }
164
+ if (depVersion != null ) {
165
+ log .debug ("Resolved version={} for dep={}" , depVersion .getVersionNumber (), dep );
166
+ return Stream .concat (
167
+ Stream .of (depVersion ),
168
+ expandDependencies (modrinthApiClient , depVersion )
169
+ )
170
+ .peek (expandedVer -> log .debug ("Expanded dependency={} into version={}" , dep , expandedVer ));
171
+ }
172
+ else {
173
+ return Stream .empty ();
176
174
}
177
175
});
178
176
@@ -190,15 +188,6 @@ private boolean filterDependency(VersionDependency dep) {
190
188
);
191
189
}
192
190
193
- private Version getVersion (String versionId ) throws IOException {
194
- return fetch (Uris .populateToUri (
195
- baseUrl + "/v2/version/{id}" , versionId
196
- ))
197
- .userAgentCommand ("modrinth" )
198
- .toObject (Version .class )
199
- .execute ();
200
- }
201
-
202
191
private Version pickVersion (List <Version > versions ) {
203
192
return this .pickVersion (versions , defaultVersionType );
204
193
}
@@ -213,13 +202,6 @@ private Version pickVersion(List<Version> versions, VersionType versionType) {
213
202
}
214
203
215
204
private Path download (ProjectType projectType , VersionFile versionFile ) {
216
- if (log .isDebugEnabled ()) {
217
- log .debug ("Downloading {}" , versionFile );
218
- }
219
- else {
220
- log .info ("Downloading {}" , versionFile .getFilename ());
221
- }
222
-
223
205
if (projectType != ProjectType .mod ) {
224
206
throw new InvalidParameterException ("Only mod project types can be downloaded for now" );
225
207
}
@@ -236,6 +218,7 @@ private Path download(ProjectType projectType, VersionFile versionFile) {
236
218
.userAgentCommand ("modrinth" )
237
219
.toFile (outPath )
238
220
.skipUpToDate (true )
221
+ .handleStatus (Fetch .loggingDownloadStatusHandler (log ))
239
222
.execute ();
240
223
} catch (IOException e ) {
241
224
throw new RuntimeException ("Downloading mod file" , e );
@@ -253,22 +236,20 @@ private List<Version> getVersionsForProject(ModrinthApiClient modrinthApiClient,
253
236
return versions ;
254
237
}
255
238
256
- private Version getVersionFromId (String versionId ) {
257
- return fetch (Uris .populateToUri (
258
- baseUrl + "/v2/version/{id}" ,
259
- versionId
260
- ))
261
- .userAgentCommand ("modrinth" )
262
- .toObject (Version .class )
263
- .execute ();
264
- }
265
-
266
239
267
240
private Stream <Path > processProject (ModrinthApiClient modrinthApiClient , ProjectRef projectRef , Project project ) {
268
241
log .debug ("Starting with projectRef={}" , projectRef );
269
242
270
243
if (projectsProcessed .add (project .getId ())) {
271
- final Version version = resolveProjectVersion (modrinthApiClient , project , projectRef );
244
+ final Version version ;
245
+ try {
246
+ version = modrinthApiClient .resolveProjectVersion (
247
+ project , projectRef , loader , gameVersion , defaultVersionType
248
+ )
249
+ .block ();
250
+ } catch (NoApplicableVersionsException | NoFilesAvailableException e ) {
251
+ throw new InvalidParameterException (e .getMessage (), e );
252
+ }
272
253
273
254
if (version != null ) {
274
255
if (version .getFiles ().isEmpty ()) {
@@ -287,29 +268,10 @@ private Stream<Path> processProject(ModrinthApiClient modrinthApiClient, Project
287
268
throw new InvalidParameterException (
288
269
String .format ("Project %s does not have any matching versions for loader %s, game version %s" ,
289
270
projectRef , loader , gameVersion
290
- ));
291
- }
292
- }
293
- return Stream .empty ();
294
- }
295
-
296
- private Version resolveProjectVersion (ModrinthApiClient modrinthApiClient , Project project , ProjectRef projectRef ) {
297
- if (projectRef .hasVersionType ()) {
298
- return pickVersion (getVersionsForProject (modrinthApiClient , project .getId ()), projectRef .getVersionType ());
299
- }
300
- else if (projectRef .hasVersionId ()) {
301
- return getVersionFromId (projectRef .getVersionId ());
302
- }
303
- else {
304
- final List <Version > versions = getVersionsForProject (modrinthApiClient , project .getId ());
305
- if (versions .isEmpty ()) {
306
- throw new InvalidParameterException (
307
- String .format ("No files are available for the project '%s' (%s) for %s loader and Minecraft version %s" ,
308
- project .getTitle (), project .getSlug (), loader , gameVersion
309
271
));
310
272
}
311
- return pickVersion (versions );
312
273
}
274
+ return Stream .empty ();
313
275
}
314
276
315
277
/**
0 commit comments