-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLibraryHandler.java
More file actions
82 lines (68 loc) · 3.29 KB
/
LibraryHandler.java
File metadata and controls
82 lines (68 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package io.github.fabriccompatibilitylayers.modremappingapi.impl.context;
import fr.catcore.modremapperapi.utils.Constants;
import io.github.fabriccompatibilitylayers.modremappingapi.api.v2.CacheHandler;
import io.github.fabriccompatibilitylayers.modremappingapi.api.v2.RemapLibrary;
import io.github.fabriccompatibilitylayers.modremappingapi.impl.utils.FileUtils;
import net.fabricmc.tinyremapper.TinyRemapper;
import org.jetbrains.annotations.ApiStatus;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.stream.Collectors;
@ApiStatus.Internal
public class LibraryHandler {
private Map<RemapLibrary, Path> remapLibraries = new HashMap<>();
private String sourceNamespace;
private CacheHandler cacheHandler;
public LibraryHandler() {}
public void init(String sourceNamespace, CacheHandler cacheHandler) {
this.sourceNamespace = sourceNamespace;
this.cacheHandler = cacheHandler;
Path sourceLibraryPath = this.cacheHandler.resolveLibrary(this.sourceNamespace);
if (!Files.exists(sourceLibraryPath)) {
try {
Files.createDirectories(sourceLibraryPath);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
private Map<RemapLibrary, Path> computeExtraLibraryPaths(Collection<RemapLibrary> sourcePaths, String target) {
return sourcePaths.stream()
.collect(Collectors.toMap(p -> p,
p -> this.cacheHandler.resolveLibrary(target).resolve(p.getFileName())));
}
public void cacheLibraries(List<RemapLibrary> libraries) {
remapLibraries = computeExtraLibraryPaths(libraries, sourceNamespace);
try {
for (Map.Entry<RemapLibrary, Path> entry : remapLibraries.entrySet()) {
RemapLibrary library = entry.getKey();
Path path = entry.getValue();
if (Files.exists(path)) continue;
if (library.getURL() != null && !library.getURL().isEmpty()) {
Constants.MAIN_LOGGER.info("Downloading remapping library '" + library.getFileName() + "' from url '" + library.getURL() + "'");
FileUtils.downloadFile(library.getURL(), path);
FileUtils.removeEntriesFromZip(path, library.getToExclude());
Constants.MAIN_LOGGER.info("Remapping library ready for use.");
} else if (library.getPath() != null) {
Constants.MAIN_LOGGER.info("Extracting remapping library '" + library.getFileName() + "' from mod jar.");
FileUtils.copyZipFile(library.getPath(), path);
Constants.MAIN_LOGGER.info("Remapping library ready for use.");
}
}
} catch (IOException | URISyntaxException e) {
throw new RuntimeException(e);
}
}
public void addLibrariesToRemapClasspath(TinyRemapper remapper) {
for (Path libPath : remapLibraries.values()) {
if (Files.exists(libPath)) {
remapper.readClassPathAsync(libPath);
} else {
Constants.MAIN_LOGGER.info("Library " + libPath + " does not exist.");
}
}
}
}