Skip to content

Commit 2980fd2

Browse files
committed
Cache remapped mc jars + more refactoring
1 parent d328058 commit 2980fd2

File tree

4 files changed

+145
-138
lines changed

4 files changed

+145
-138
lines changed

src/main/java/fr/catcore/modremapperapi/remapping/RemapUtil.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
import fr.catcore.modremapperapi.utils.Constants;
44
import fr.catcore.modremapperapi.utils.FileUtils;
5-
import fr.catcore.modremapperapi.utils.MappingsUtils;
6-
import io.github.fabriccompatibiltylayers.modremappingapi.api.MappingUtils;
5+
import io.github.fabriccompatibiltylayers.modremappingapi.api.v1.MappingUtils;
76
import io.github.fabriccompatibiltylayers.modremappingapi.api.v1.ModRemapper;
87
import io.github.fabriccompatibiltylayers.modremappingapi.api.v1.RemapLibrary;
98
import io.github.fabriccompatibiltylayers.modremappingapi.impl.MappingBuilderImpl;
109
import io.github.fabriccompatibiltylayers.modremappingapi.impl.MappingsUtilsImpl;
1110
import io.github.fabriccompatibiltylayers.modremappingapi.impl.VisitorInfosImpl;
11+
import io.github.fabriccompatibiltylayers.modremappingapi.impl.remapper.minecraft.MinecraftRemapper;
1212
import io.github.fabriccompatibiltylayers.modremappingapi.impl.remapper.resource.RefmapRemapper;
1313
import io.github.fabriccompatibiltylayers.modremappingapi.impl.remapper.visitor.MRAApplyVisitor;
1414
import io.github.fabriccompatibiltylayers.modremappingapi.impl.remapper.visitor.MixinPostApplyVisitor;
@@ -39,7 +39,8 @@ public class RemapUtil {
3939
private static MappingTree MINECRAFT_TREE;
4040
private static MemoryMappingTree MODS_TREE;
4141

42-
protected static final Map<String, List<String>> MIXINED = new HashMap<>();
42+
@ApiStatus.Internal
43+
public static final Map<String, List<String>> MIXINED = new HashMap<>();
4344

4445
private static String defaultPackage = "";
4546

@@ -526,7 +527,7 @@ private static TinyRemapper makeRemapper(MappingTree... trees) {
526527
}
527528

528529
for (MappingTree tree : trees) {
529-
builder.withMappings(MappingsUtilsImpl.createProvider(tree, MappingsUtilsImpl.getSourceNamespace(), MappingsUtils.getTargetNamespace()));
530+
builder.withMappings(MappingsUtilsImpl.createProvider(tree, MappingsUtilsImpl.getSourceNamespace(), MappingsUtilsImpl.getTargetNamespace()));
530531
}
531532

532533
MRAApplyVisitor preApplyVisitor = new MRAApplyVisitor();
@@ -553,7 +554,7 @@ private static TinyRemapper makeRemapper(MappingTree... trees) {
553554
TinyRemapper remapper = builder.build();
554555

555556
try {
556-
MappingsUtils.addMinecraftJar(remapper);
557+
MinecraftRemapper.addMinecraftJar(remapper);
557558
} catch (IOException e) {
558559
throw new RuntimeException(e);
559560
}
@@ -588,7 +589,7 @@ private static void remapFiles(TinyRemapper remapper, Map<Path, Path> paths) {
588589
List<OutputConsumerPath.ResourceRemapper> resourceRemappers = new ArrayList<>(NonClassCopyMode.FIX_META_INF.remappers);
589590
resourceRemappers.add(new RefmapRemapper());
590591

591-
applyRemapper(remapper, paths, outputConsumerPaths, resourceRemappers, true, MappingsUtilsImpl.getSourceNamespace(), MappingsUtils.getTargetNamespace());
592+
applyRemapper(remapper, paths, outputConsumerPaths, resourceRemappers, true, MappingsUtilsImpl.getSourceNamespace(), MappingsUtilsImpl.getTargetNamespace());
592593
}
593594

594595
@ApiStatus.Internal
@@ -662,6 +663,6 @@ public static EnvType getEnvironment() {
662663

663664
@Deprecated
664665
public static String getNativeNamespace() {
665-
return MappingsUtils.getNativeNamespace();
666+
return MappingsUtilsImpl.getNativeNamespace();
666667
}
667668
}
Lines changed: 4 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,21 @@
11
package fr.catcore.modremapperapi.utils;
22

3-
import fr.catcore.modremapperapi.ModRemappingAPI;
4-
import fr.catcore.modremapperapi.remapping.RemapUtil;
53
import io.github.fabriccompatibiltylayers.modremappingapi.impl.MappingsUtilsImpl;
6-
import io.github.fabriccompatibiltylayers.modremappingapi.impl.RemapUtils;
7-
import io.github.fabriccompatibiltylayers.modremappingapi.impl.utils.CacheUtils;
8-
import net.fabricmc.api.EnvType;
9-
import net.fabricmc.loader.api.*;
10-
import net.fabricmc.mappingio.MappingReader;
11-
import net.fabricmc.mappingio.MappingVisitor;
124
import net.fabricmc.mappingio.tree.MappingTree;
13-
import net.fabricmc.mappingio.tree.MemoryMappingTree;
145
import net.fabricmc.tinyremapper.*;
15-
import org.jetbrains.annotations.ApiStatus;
16-
17-
import java.io.*;
18-
import java.nio.file.Files;
19-
import java.nio.file.Path;
20-
import java.util.*;
21-
22-
import static io.github.fabriccompatibiltylayers.modremappingapi.impl.RemapUtils.getRemapClasspath;
236

7+
@Deprecated
248
public class MappingsUtils {
259
@Deprecated
2610
public static String getNativeNamespace() {
27-
if (ModRemappingAPI.BABRIC) {
28-
return FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT ? "client" : "server";
29-
}
30-
31-
return "official";
11+
return MappingsUtilsImpl.getNativeNamespace();
3212
}
3313

14+
@Deprecated
3415
public static String getTargetNamespace() {
35-
return FabricLoader.getInstance().getMappingResolver().getCurrentRuntimeNamespace();
16+
return MappingsUtilsImpl.getTargetNamespace();
3617
}
3718

38-
public static MappingTree loadMappings(Reader reader) {
39-
MemoryMappingTree tree = new MemoryMappingTree();
40-
try {
41-
loadMappings(reader, tree);
42-
} catch (IOException e) {
43-
e.printStackTrace();
44-
}
45-
return tree;
46-
}
47-
48-
public static void loadMappings(Reader reader, MappingVisitor tree) throws IOException {
49-
MappingReader.read(reader, tree);
50-
}
51-
52-
/**
53-
* @deprecated Use {@link MappingsUtilsImpl#getVanillaMappings()} instead for the same behavior.
54-
*/
5519
@Deprecated
5620
public static MappingTree getMinecraftMappings() {
5721
return MappingsUtilsImpl.getVanillaMappings();
@@ -61,91 +25,4 @@ public static MappingTree getMinecraftMappings() {
6125
public static IMappingProvider createProvider(MappingTree mappings) {
6226
return MappingsUtilsImpl.createProvider(mappings, getNativeNamespace(), getTargetNamespace());
6327
}
64-
65-
private static Path[] getMinecraftJar(List<Path> sourcePaths, String src, String target) throws IOException {
66-
Path[] originalClassPath = sourcePaths.toArray(new Path[0]);
67-
68-
Map<Path, Path> paths = new HashMap<>();
69-
70-
for (Path path : originalClassPath) {
71-
Constants.MAIN_LOGGER.debug(path.toString());
72-
73-
Path computedPath = CacheUtils.getLibraryPath(target)
74-
.resolve(path.toFile().getName());
75-
76-
paths.put(path, computedPath);
77-
Files.delete(computedPath);
78-
}
79-
80-
TinyRemapper.Builder builder = TinyRemapper
81-
.newRemapper()
82-
.renameInvalidLocals(true)
83-
.ignoreFieldDesc(false)
84-
.propagatePrivate(true)
85-
.ignoreConflicts(true)
86-
.fixPackageAccess(true)
87-
.withMappings(
88-
MappingsUtilsImpl.createProvider(MappingsUtilsImpl.getMinecraftMappings(), src, target)
89-
);
90-
91-
TinyRemapper remapper = builder.build();
92-
93-
Constants.MAIN_LOGGER.info("Remapping minecraft jar from " + src + " to " + target + "!");
94-
95-
List<OutputConsumerPath> outputConsumerPaths = new ArrayList<>();
96-
97-
List<OutputConsumerPath.ResourceRemapper> resourceRemappers = new ArrayList<>(NonClassCopyMode.FIX_META_INF.remappers);
98-
99-
RemapUtil.applyRemapper(remapper, paths, outputConsumerPaths, resourceRemappers, true, src, target);
100-
101-
Constants.MAIN_LOGGER.info("MC jar remapped successfully!");
102-
103-
return paths.values().toArray(new Path[0]);
104-
}
105-
106-
@ApiStatus.Internal
107-
public static void addMinecraftJar(TinyRemapper remapper) throws IOException {
108-
if (FabricLoader.getInstance().isDevelopmentEnvironment()) {
109-
try {
110-
Path[] classPath = getMinecraftJar(
111-
Arrays.asList(
112-
getMinecraftJar(
113-
getRemapClasspath(),
114-
getTargetNamespace(),
115-
"intermediary"
116-
)
117-
),
118-
"intermediary",
119-
"official"
120-
);
121-
122-
if (!MappingsUtilsImpl.isSourceNamespaceObf()) {
123-
classPath = getMinecraftJar(
124-
Arrays.asList(
125-
classPath
126-
),
127-
"official",
128-
MappingsUtilsImpl.getSourceNamespace()
129-
);
130-
}
131-
132-
remapper.readClassPathAsync(classPath);
133-
} catch (IOException e) {
134-
throw new RuntimeException("Failed to populate default remap classpath", e);
135-
}
136-
} else {
137-
List<Path> list = RemapUtils.getClassPathFromObjectShare();
138-
139-
Path[] classPath = list.toArray(new Path[0]);
140-
141-
if (!MappingsUtilsImpl.isSourceNamespaceObf()) {
142-
classPath = getMinecraftJar(list, "official", MappingsUtilsImpl.getSourceNamespace());
143-
}
144-
145-
for (Path path : classPath) {
146-
Constants.MAIN_LOGGER.debug("Appending '%s' to remapper classpath", path);
147-
remapper.readClassPathAsync(path);
148-
}
149-
}
150-
}
15128
}

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package io.github.fabriccompatibiltylayers.modremappingapi.impl;
22

3+
import fr.catcore.modremapperapi.ModRemappingAPI;
34
import fr.catcore.modremapperapi.utils.Constants;
4-
import fr.catcore.modremapperapi.utils.MappingsUtils;
55
import fr.catcore.wfvaio.WhichFabricVariantAmIOn;
66
import io.github.fabriccompatibiltylayers.modremappingapi.api.MappingUtils;
77
import io.github.fabriccompatibiltylayers.modremappingapi.impl.mappings.MappingTreeHelper;
88
import io.github.fabriccompatibiltylayers.modremappingapi.impl.utils.VersionHelper;
9+
import net.fabricmc.api.EnvType;
910
import net.fabricmc.loader.api.FabricLoader;
1011
import net.fabricmc.loader.impl.launch.MappingConfiguration;
1112
import net.fabricmc.loader.impl.util.log.Log;
@@ -41,8 +42,6 @@
4142
import java.util.stream.Collectors;
4243
import java.util.zip.ZipError;
4344

44-
import static fr.catcore.modremapperapi.utils.MappingsUtils.getTargetNamespace;
45-
4645
@ApiStatus.Internal
4746
public class MappingsUtilsImpl {
4847
private static boolean initialized = false;
@@ -91,6 +90,18 @@ public static boolean isSourceNamespaceObf() {
9190
return Objects.equals(sourceNamespace, "official");
9291
}
9392

93+
public static String getTargetNamespace() {
94+
return FabricLoader.getInstance().getMappingResolver().getCurrentRuntimeNamespace();
95+
}
96+
97+
public static String getNativeNamespace() {
98+
if (ModRemappingAPI.BABRIC) {
99+
return FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT ? "client" : "server";
100+
}
101+
102+
return "official";
103+
}
104+
94105
@ApiStatus.Internal
95106
public static MemoryMappingTree loadMappings(InputStream stream) throws IOException {
96107
try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) {
@@ -226,7 +237,7 @@ public static IMappingProvider createProvider(MappingTree mappings, String from,
226237

227238
@ApiStatus.Internal
228239
public static void initializeMappingTree(MappingVisitor mappingVisitor) throws IOException {
229-
initializeMappingTree(mappingVisitor, getSourceNamespace(), MappingsUtils.getTargetNamespace());
240+
initializeMappingTree(mappingVisitor, getSourceNamespace(), getTargetNamespace());
230241
}
231242

232243
@ApiStatus.Internal
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package io.github.fabriccompatibiltylayers.modremappingapi.impl.remapper.minecraft;
2+
3+
import fr.catcore.modremapperapi.remapping.RemapUtil;
4+
import fr.catcore.modremapperapi.utils.Constants;
5+
import io.github.fabriccompatibiltylayers.modremappingapi.impl.MappingsUtilsImpl;
6+
import io.github.fabriccompatibiltylayers.modremappingapi.impl.RemapUtils;
7+
import io.github.fabriccompatibiltylayers.modremappingapi.impl.utils.CacheUtils;
8+
import net.fabricmc.loader.api.FabricLoader;
9+
import net.fabricmc.tinyremapper.NonClassCopyMode;
10+
import net.fabricmc.tinyremapper.OutputConsumerPath;
11+
import net.fabricmc.tinyremapper.TinyRemapper;
12+
import org.jetbrains.annotations.ApiStatus;
13+
14+
import java.io.IOException;
15+
import java.nio.file.Files;
16+
import java.nio.file.Path;
17+
import java.util.*;
18+
import java.util.stream.Collectors;
19+
20+
@ApiStatus.Internal
21+
public class MinecraftRemapper {
22+
private static Collection<Path> getMinecraftJar(Collection<Path> sourcePaths, String src, String target) throws IOException {
23+
Path targetFolder = CacheUtils.getLibraryPath(target);
24+
25+
if (!Files.exists(targetFolder)) {
26+
Files.createDirectories(targetFolder);
27+
}
28+
29+
Map<Path, Path> paths = computeLibraryPaths(sourcePaths, target);
30+
31+
if (exist(paths.values())) return paths.values();
32+
33+
delete(paths.values());
34+
35+
TinyRemapper.Builder builder = TinyRemapper
36+
.newRemapper()
37+
.renameInvalidLocals(true)
38+
.ignoreFieldDesc(false)
39+
.propagatePrivate(true)
40+
.ignoreConflicts(true)
41+
.fixPackageAccess(true)
42+
.withMappings(
43+
MappingsUtilsImpl.createProvider(MappingsUtilsImpl.getMinecraftMappings(), src, target)
44+
);
45+
46+
TinyRemapper remapper = builder.build();
47+
48+
Constants.MAIN_LOGGER.info("Remapping minecraft jar from " + src + " to " + target + "!");
49+
50+
List<OutputConsumerPath> outputConsumerPaths = new ArrayList<>();
51+
52+
List<OutputConsumerPath.ResourceRemapper> resourceRemappers = new ArrayList<>(NonClassCopyMode.FIX_META_INF.remappers);
53+
54+
RemapUtil.applyRemapper(remapper, paths, outputConsumerPaths, resourceRemappers, true, src, target);
55+
56+
Constants.MAIN_LOGGER.info("MC jar remapped successfully!");
57+
58+
return paths.values();
59+
}
60+
61+
@ApiStatus.Internal
62+
public static Map<Path, Path> computeLibraryPaths(Collection<Path> sourcePaths, String target) {
63+
return sourcePaths.stream().collect(Collectors.toMap(p -> p,
64+
p -> CacheUtils.getLibraryPath(target).resolve(p.toFile().getName())));
65+
}
66+
67+
@ApiStatus.Internal
68+
public static boolean exist(Collection<Path> paths) {
69+
for (Path path : paths) {
70+
if (!Files.exists(path)) return false;
71+
}
72+
73+
return true;
74+
}
75+
76+
@ApiStatus.Internal
77+
public static void delete(Collection<Path> paths) {
78+
for (Path path : paths) {
79+
try {
80+
Files.deleteIfExists(path);
81+
} catch (IOException e) {
82+
e.printStackTrace();
83+
}
84+
}
85+
}
86+
87+
@ApiStatus.Internal
88+
public static void addMinecraftJar(TinyRemapper remapper) throws IOException {
89+
Collection<Path> classPath;
90+
91+
if (FabricLoader.getInstance().isDevelopmentEnvironment()) {
92+
try {
93+
classPath = getMinecraftJar(
94+
getMinecraftJar(RemapUtils.getRemapClasspath(), MappingsUtilsImpl.getTargetNamespace(), "intermediary"),
95+
"intermediary",
96+
"official"
97+
);
98+
99+
if (!MappingsUtilsImpl.isSourceNamespaceObf()) {
100+
classPath = getMinecraftJar(classPath, "official", MappingsUtilsImpl.getSourceNamespace());
101+
}
102+
} catch (IOException e) {
103+
throw new RuntimeException("Failed to populate default remap classpath", e);
104+
}
105+
} else {
106+
classPath = RemapUtils.getClassPathFromObjectShare();
107+
108+
if (!MappingsUtilsImpl.isSourceNamespaceObf()) {
109+
classPath = getMinecraftJar(classPath, "official", MappingsUtilsImpl.getSourceNamespace());
110+
}
111+
}
112+
113+
for (Path path : classPath) {
114+
Constants.MAIN_LOGGER.debug("Appending '%s' to remapper classpath", path);
115+
remapper.readClassPathAsync(path);
116+
}
117+
}
118+
}

0 commit comments

Comments
 (0)