Skip to content

Commit 6e06dd3

Browse files
committed
Fully migrate path handling to CacheHandler and add RemapLibrary to v2 api
1 parent 4078f4f commit 6e06dd3

File tree

13 files changed

+209
-80
lines changed

13 files changed

+209
-80
lines changed

src/main/java/io/github/fabriccompatibilitylayers/modremappingapi/api/v2/ModRemapper.java

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.github.fabriccompatibilitylayers.modremappingapi.api.v2;
22

3+
import net.fabricmc.api.EnvType;
4+
35
import java.util.List;
46

57
public interface ModRemapper {
@@ -12,4 +14,7 @@ public interface ModRemapper {
1214
MappingsConfig getMappingsConfig();
1315
List<RemappingFlags> getRemappingFlags();
1416
void afterRemapping();
17+
void afterAllRemappings();
18+
19+
void addRemappingLibraries(List<RemapLibrary> libraries, EnvType environment);
1520
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package io.github.fabriccompatibilitylayers.modremappingapi.api.v2;
2+
3+
import io.github.fabriccompatibilitylayers.modremappingapi.impl.DefaultRemapLibrary;
4+
import org.jetbrains.annotations.Nullable;
5+
6+
import java.nio.file.Path;
7+
import java.util.List;
8+
9+
public interface RemapLibrary {
10+
@Nullable String getURL();
11+
@Nullable Path getPath();
12+
String getFileName();
13+
List<String> getToExclude();
14+
15+
static RemapLibrary of(Path path, String fileName) {
16+
return new DefaultRemapLibrary(path, fileName);
17+
}
18+
19+
static RemapLibrary of(Path path, String fileName, List<String> toExclude) {
20+
return new DefaultRemapLibrary(path, fileName, toExclude);
21+
}
22+
23+
static RemapLibrary of(String url, String fileName, List<String> toExclude) {
24+
return new DefaultRemapLibrary(url, fileName, toExclude);
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package io.github.fabriccompatibilitylayers.modremappingapi.impl;
2+
3+
import io.github.fabriccompatibilitylayers.modremappingapi.api.v2.RemapLibrary;
4+
import org.jetbrains.annotations.Nullable;
5+
6+
import java.nio.file.Path;
7+
import java.util.ArrayList;
8+
import java.util.Collections;
9+
import java.util.List;
10+
11+
public class DefaultRemapLibrary implements RemapLibrary {
12+
private final @Nullable String url;
13+
private final @Nullable Path path;
14+
private final String fileName;
15+
private final List<String> toExclude;
16+
17+
public DefaultRemapLibrary(@Nullable String url, String fileName, List<String> toExclude) {
18+
this.url = url;
19+
this.path = null;
20+
this.fileName = fileName;
21+
this.toExclude = toExclude;
22+
}
23+
24+
public DefaultRemapLibrary(@Nullable Path path, String fileName, List<String> toExclude) {
25+
this.url = null;
26+
this.path = path;
27+
this.fileName = fileName;
28+
this.toExclude = toExclude;
29+
}
30+
31+
public DefaultRemapLibrary(@Nullable Path path, String fileName) {
32+
this(path, fileName, Collections.emptyList());
33+
}
34+
35+
@Override
36+
public @Nullable String getURL() {
37+
return this.url;
38+
}
39+
40+
@Override
41+
public @Nullable Path getPath() {
42+
return this.path;
43+
}
44+
45+
@Override
46+
public String getFileName() {
47+
return this.fileName;
48+
}
49+
50+
@Override
51+
public List<String> getToExclude() {
52+
return this.toExclude;
53+
}
54+
}

src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/api/v1/RemapLibrary.java

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package io.github.fabriccompatibiltylayers.modremappingapi.api.v1;
22

33
import org.jetbrains.annotations.ApiStatus;
4+
import org.jetbrains.annotations.Nullable;
45

56
import java.nio.file.Path;
67
import java.util.ArrayList;
78
import java.util.List;
89

9-
public class RemapLibrary {
10+
public class RemapLibrary implements io.github.fabriccompatibilitylayers.modremappingapi.api.v2.RemapLibrary {
1011
public final String url;
1112
public final Path path;
1213
public final List<String> toExclude;
@@ -40,4 +41,24 @@ public RemapLibrary(String url, Path path, List<String> toExclude, String fileNa
4041
this.toExclude = toExclude;
4142
this.fileName = fileName;
4243
}
44+
45+
@Override
46+
public @Nullable String getURL() {
47+
return this.url;
48+
}
49+
50+
@Override
51+
public @Nullable Path getPath() {
52+
return this.path;
53+
}
54+
55+
@Override
56+
public String getFileName() {
57+
return this.fileName;
58+
}
59+
60+
@Override
61+
public List<String> getToExclude() {
62+
return this.toExclude;
63+
}
4364
}

src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/LibraryHandler.java

+32-36
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
11
package io.github.fabriccompatibiltylayers.modremappingapi.impl;
22

33
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;
76
import io.github.fabriccompatibiltylayers.modremappingapi.impl.utils.FileUtils;
8-
import net.fabricmc.loader.api.FabricLoader;
97
import net.fabricmc.tinyremapper.TinyRemapper;
108

119
import java.io.IOException;
1210
import java.net.URISyntaxException;
1311
import java.nio.file.Files;
1412
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;
1915

2016
public class LibraryHandler {
21-
private final Map<RemapLibrary, Path> remapLibraries = new HashMap<>();
17+
private Map<RemapLibrary, Path> remapLibraries = new HashMap<>();
2218

2319
private String sourceNamespace;
20+
private CacheHandler cacheHandler;
2421

2522
public LibraryHandler() {}
2623

27-
public void init(String sourceNamespace) {
24+
public void init(String sourceNamespace, CacheHandler cacheHandler) {
2825
this.sourceNamespace = sourceNamespace;
26+
this.cacheHandler = cacheHandler;
2927

30-
Path sourceLibraryPath = CacheUtils.getLibraryPath(this.sourceNamespace);
28+
Path sourceLibraryPath = this.cacheHandler.resolveLibrary(this.sourceNamespace);
3129

3230
if (!Files.exists(sourceLibraryPath)) {
3331
try {
@@ -38,34 +36,32 @@ public void init(String sourceNamespace) {
3836
}
3937
}
4038

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+
}
5344

54-
if (Files.exists(path)) continue;
45+
public void cacheLibraries(List<RemapLibrary> libraries) {
46+
remapLibraries = computeExtraLibraryPaths(libraries, sourceNamespace);
5547

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.");
6664
}
67-
68-
remapLibraries.putAll(temp);
6965
}
7066
} catch (IOException | URISyntaxException e) {
7167
throw new RuntimeException(e);

src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/ModRemappingAPIImpl.java

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ public static void init() {
6161
context.afterRemap();
6262
}
6363

64+
v2Remappers.values().forEach(l -> l.forEach(ModRemapper::afterAllRemappings));
65+
6466
initializing = false;
6567
init = true;
6668
}

src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/context/ModRemapperContext.java

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.github.fabriccompatibiltylayers.modremappingapi.impl.context;
22

3+
import io.github.fabriccompatibilitylayers.modremappingapi.api.v2.CacheHandler;
34
import io.github.fabriccompatibilitylayers.modremappingapi.api.v2.ModCandidate;
45
import io.github.fabriccompatibiltylayers.modremappingapi.impl.LibraryHandler;
56
import io.github.fabriccompatibiltylayers.modremappingapi.impl.mappings.MappingsRegistry;
@@ -23,4 +24,6 @@ public interface ModRemapperContext<T> {
2324
LibraryHandler getLibraryHandler();
2425
String getId();
2526
MixinData getMixinData();
27+
void gatherLibraries();
28+
CacheHandler getCacheHandler();
2629
}

src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/context/v1/ModRemapperV1Context.java

+21-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package io.github.fabriccompatibiltylayers.modremappingapi.impl.context.v1;
22

33
import fr.catcore.modremapperapi.utils.Constants;
4+
import io.github.fabriccompatibilitylayers.modremappingapi.api.v2.CacheHandler;
45
import io.github.fabriccompatibilitylayers.modremappingapi.api.v2.ModCandidate;
56
import io.github.fabriccompatibilitylayers.modremappingapi.api.v2.ModDiscovererConfig;
67
import io.github.fabriccompatibilitylayers.modremappingapi.impl.DefaultModCandidate;
78
import io.github.fabriccompatibilitylayers.modremappingapi.impl.InternalCacheHandler;
89
import io.github.fabriccompatibiltylayers.modremappingapi.api.v1.MappingBuilder;
910
import io.github.fabriccompatibiltylayers.modremappingapi.api.v1.ModRemapper;
11+
import io.github.fabriccompatibiltylayers.modremappingapi.api.v1.RemapLibrary;
1012
import io.github.fabriccompatibiltylayers.modremappingapi.impl.*;
1113
import io.github.fabriccompatibiltylayers.modremappingapi.impl.compatibility.V0ModRemapper;
1214
import io.github.fabriccompatibiltylayers.modremappingapi.impl.context.BaseModRemapperContext;
@@ -78,9 +80,7 @@ public void init() {
7880
throw new RuntimeException(e);
7981
}
8082

81-
libraryHandler.init(this.mappingsRegistry.getSourceNamespace());
82-
83-
libraryHandler.gatherRemapLibraries(remappers);
83+
this.gatherLibraries();
8484

8585
this.registerAdditionalMappings();
8686
this.mappingsRegistry.generateAdditionalMappings();
@@ -266,4 +266,22 @@ public LibraryHandler getLibraryHandler() {
266266
public MixinData getMixinData() {
267267
return mixinData;
268268
}
269+
270+
@Override
271+
public void gatherLibraries() {
272+
libraryHandler.init(this.mappingsRegistry.getSourceNamespace(), this.cacheHandler);
273+
274+
List<RemapLibrary> libraries = new ArrayList<>();
275+
276+
for (ModRemapper remapper : remappers) {
277+
remapper.addRemapLibraries(libraries, FabricLoader.getInstance().getEnvironmentType());
278+
}
279+
280+
libraryHandler.cacheLibraries(new ArrayList<>(libraries));
281+
}
282+
283+
@Override
284+
public CacheHandler getCacheHandler() {
285+
return this.cacheHandler;
286+
}
269287
}

src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/context/v1/V1CacheHandler.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
public class V1CacheHandler implements InternalCacheHandler {
1111
private final Path contextDirectory = CacheUtils.MAIN_FOLDER;
1212
private final Path tempDirectory = contextDirectory.resolve("temp");
13-
private final Path libsDirectory = contextDirectory.resolve("libs");
13+
private final Path libsDirectory = CacheUtils.LIBRARY_FOLDER;
1414

1515
public V1CacheHandler() {
1616
for (Path p : new Path[] {tempDirectory, libsDirectory}) {

src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/context/v2/ModRemmaperV2Context.java

+20-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import io.github.fabriccompatibiltylayers.modremappingapi.impl.remapper.ModTrRemapper;
1212
import io.github.fabriccompatibiltylayers.modremappingapi.impl.remapper.SoftLockFixer;
1313
import io.github.fabriccompatibiltylayers.modremappingapi.impl.utils.CacheUtils;
14+
import net.fabricmc.loader.api.FabricLoader;
1415
import net.fabricmc.tinyremapper.TinyRemapper;
1516

1617
import java.io.ByteArrayInputStream;
@@ -63,7 +64,7 @@ public void init() {
6364
throw new RuntimeException(e);
6465
}
6566

66-
libraryHandler.init(this.mappingsRegistry.getSourceNamespace());
67+
this.gatherLibraries();
6768

6869
this.mappingsRegistry.generateAdditionalMappings();
6970
}
@@ -162,4 +163,22 @@ public List<ModRemapper> getRemappers() {
162163
public MixinData getMixinData() {
163164
return mixinData;
164165
}
166+
167+
@Override
168+
public void gatherLibraries() {
169+
libraryHandler.init(this.mappingsRegistry.getSourceNamespace(), this.cacheHandler);
170+
171+
List<RemapLibrary> libraries = new ArrayList<>();
172+
173+
for (ModRemapper remapper : remappers) {
174+
remapper.addRemappingLibraries(libraries, FabricLoader.getInstance().getEnvironmentType());
175+
}
176+
177+
libraryHandler.cacheLibraries(libraries);
178+
}
179+
180+
@Override
181+
public CacheHandler getCacheHandler() {
182+
return this.cacheHandler;
183+
}
165184
}

0 commit comments

Comments
 (0)