1
1
package io .github .fabriccompatibiltylayers .modremappingapi .impl ;
2
2
3
3
import fr .catcore .modremapperapi .utils .Constants ;
4
- import io .github .fabriccompatibiltylayers .modremappingapi .api .v1 .ModRemapper ;
5
- import io .github .fabriccompatibiltylayers .modremappingapi .api .v1 .RemapLibrary ;
6
- import io .github .fabriccompatibiltylayers .modremappingapi .impl .utils .CacheUtils ;
4
+ import io .github .fabriccompatibilitylayers .modremappingapi .api .v2 .CacheHandler ;
5
+ import io .github .fabriccompatibilitylayers .modremappingapi .api .v2 .RemapLibrary ;
7
6
import io .github .fabriccompatibiltylayers .modremappingapi .impl .utils .FileUtils ;
8
- import net .fabricmc .loader .api .FabricLoader ;
9
7
import net .fabricmc .tinyremapper .TinyRemapper ;
10
8
11
9
import java .io .IOException ;
12
10
import java .net .URISyntaxException ;
13
11
import java .nio .file .Files ;
14
12
import java .nio .file .Path ;
15
- import java .util .ArrayList ;
16
- import java .util .HashMap ;
17
- import java .util .List ;
18
- import java .util .Map ;
13
+ import java .util .*;
14
+ import java .util .stream .Collectors ;
19
15
20
16
public class LibraryHandler {
21
- private final Map <RemapLibrary , Path > remapLibraries = new HashMap <>();
17
+ private Map <RemapLibrary , Path > remapLibraries = new HashMap <>();
22
18
23
19
private String sourceNamespace ;
20
+ private CacheHandler cacheHandler ;
24
21
25
22
public LibraryHandler () {}
26
23
27
- public void init (String sourceNamespace ) {
24
+ public void init (String sourceNamespace , CacheHandler cacheHandler ) {
28
25
this .sourceNamespace = sourceNamespace ;
26
+ this .cacheHandler = cacheHandler ;
29
27
30
- Path sourceLibraryPath = CacheUtils . getLibraryPath (this .sourceNamespace );
28
+ Path sourceLibraryPath = this . cacheHandler . resolveLibrary (this .sourceNamespace );
31
29
32
30
if (!Files .exists (sourceLibraryPath )) {
33
31
try {
@@ -38,34 +36,32 @@ public void init(String sourceNamespace) {
38
36
}
39
37
}
40
38
41
- public void gatherRemapLibraries (List <ModRemapper > remappers ) {
42
- try {
43
- for (ModRemapper remapper : remappers ) {
44
- List <RemapLibrary > libraries = new ArrayList <>();
45
-
46
- remapper .addRemapLibraries (libraries , FabricLoader .getInstance ().getEnvironmentType ());
47
-
48
- Map <RemapLibrary , Path > temp = CacheUtils .computeExtraLibraryPaths (libraries , sourceNamespace );
49
-
50
- for (Map .Entry <RemapLibrary , Path > entry : temp .entrySet ()) {
51
- RemapLibrary library = entry .getKey ();
52
- Path path = entry .getValue ();
39
+ private Map <RemapLibrary , Path > computeExtraLibraryPaths (Collection <RemapLibrary > sourcePaths , String target ) {
40
+ return sourcePaths .stream ()
41
+ .collect (Collectors .toMap (p -> p ,
42
+ p -> this .cacheHandler .resolveLibrary (target ).resolve (p .getFileName ())));
43
+ }
53
44
54
- if (Files .exists (path )) continue ;
45
+ public void cacheLibraries (List <RemapLibrary > libraries ) {
46
+ remapLibraries = computeExtraLibraryPaths (libraries , sourceNamespace );
55
47
56
- if (!library .url .isEmpty ()) {
57
- Constants .MAIN_LOGGER .info ("Downloading remapping library '" + library .fileName + "' from url '" + library .url + "'" );
58
- FileUtils .downloadFile (library .url , path );
59
- FileUtils .removeEntriesFromZip (path , library .toExclude );
60
- Constants .MAIN_LOGGER .info ("Remapping library ready for use." );
61
- } else if (library .path != null ) {
62
- Constants .MAIN_LOGGER .info ("Extracting remapping library '" + library .fileName + "' from mod jar." );
63
- FileUtils .copyZipFile (library .path , path );
64
- Constants .MAIN_LOGGER .info ("Remapping library ready for use." );
65
- }
48
+ try {
49
+ for (Map .Entry <RemapLibrary , Path > entry : remapLibraries .entrySet ()) {
50
+ RemapLibrary library = entry .getKey ();
51
+ Path path = entry .getValue ();
52
+
53
+ if (Files .exists (path )) continue ;
54
+
55
+ if (library .getURL () != null && !library .getURL ().isEmpty ()) {
56
+ Constants .MAIN_LOGGER .info ("Downloading remapping library '" + library .getFileName () + "' from url '" + library .getURL () + "'" );
57
+ FileUtils .downloadFile (library .getURL (), path );
58
+ FileUtils .removeEntriesFromZip (path , library .getToExclude ());
59
+ Constants .MAIN_LOGGER .info ("Remapping library ready for use." );
60
+ } else if (library .getPath () != null ) {
61
+ Constants .MAIN_LOGGER .info ("Extracting remapping library '" + library .getFileName () + "' from mod jar." );
62
+ FileUtils .copyZipFile (library .getPath (), path );
63
+ Constants .MAIN_LOGGER .info ("Remapping library ready for use." );
66
64
}
67
-
68
- remapLibraries .putAll (temp );
69
65
}
70
66
} catch (IOException | URISyntaxException e ) {
71
67
throw new RuntimeException (e );
0 commit comments