Skip to content

Commit ef00298

Browse files
committed
Finish cleaning up RemapUtil.
1 parent 3496c57 commit ef00298

File tree

11 files changed

+268
-213
lines changed

11 files changed

+268
-213
lines changed

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

+1-202
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,21 @@
11
package fr.catcore.modremapperapi.remapping;
22

3-
import fr.catcore.modremapperapi.utils.Constants;
43
import io.github.fabriccompatibiltylayers.modremappingapi.api.v1.MappingUtils;
5-
import io.github.fabriccompatibiltylayers.modremappingapi.api.v1.ModRemapper;
6-
import io.github.fabriccompatibiltylayers.modremappingapi.impl.LibraryHandler;
74
import io.github.fabriccompatibiltylayers.modremappingapi.impl.MappingsUtilsImpl;
8-
import io.github.fabriccompatibiltylayers.modremappingapi.impl.VisitorInfosImpl;
9-
import io.github.fabriccompatibiltylayers.modremappingapi.impl.mappings.MappingTreeHelper;
105
import io.github.fabriccompatibiltylayers.modremappingapi.impl.mappings.MappingsRegistry;
11-
import io.github.fabriccompatibiltylayers.modremappingapi.impl.remapper.SoftLockFixer;
12-
import io.github.fabriccompatibiltylayers.modremappingapi.impl.remapper.minecraft.MinecraftRemapper;
13-
import io.github.fabriccompatibiltylayers.modremappingapi.impl.remapper.resource.RefmapRemapper;
14-
import io.github.fabriccompatibiltylayers.modremappingapi.impl.remapper.visitor.MRAApplyVisitor;
15-
import io.github.fabriccompatibiltylayers.modremappingapi.impl.remapper.visitor.MixinPostApplyVisitor;
16-
import io.github.fabriccompatibiltylayers.modremappingapi.impl.utils.CacheUtils;
176
import net.fabricmc.api.EnvType;
187
import net.fabricmc.loader.api.FabricLoader;
198
import net.fabricmc.mappingio.MappingVisitor;
20-
import net.fabricmc.mappingio.tree.MappingTree;
21-
import net.fabricmc.tinyremapper.*;
22-
import net.fabricmc.tinyremapper.extension.mixin.MixinExtension;
239
import org.jetbrains.annotations.ApiStatus;
2410

2511
import java.io.*;
26-
import java.nio.file.Files;
27-
import java.nio.file.Path;
2812
import java.util.*;
29-
import java.util.function.Supplier;
3013

14+
@Deprecated
3115
public class RemapUtil {
32-
private static List<ModRemapper> remappers;
33-
34-
@ApiStatus.Internal
35-
public static final Map<String, List<String>> MIXINED = new HashMap<>();
36-
37-
@ApiStatus.Internal
38-
public static String defaultPackage = "";
39-
4016
@Deprecated
4117
public static final List<String> MC_CLASS_NAMES = MappingsRegistry.VANILLA_CLASS_LIST;
4218

43-
@ApiStatus.Internal
44-
public static void init(List<io.github.fabriccompatibiltylayers.modremappingapi.api.v1.ModRemapper> modRemappers) {
45-
remappers = modRemappers;
46-
47-
for (ModRemapper remapper : remappers) {
48-
Optional<String> pkg = remapper.getDefaultPackage();
49-
50-
pkg.ifPresent(s -> defaultPackage = s);
51-
52-
Optional<String> sourceNamespace = remapper.getSourceNamespace();
53-
54-
sourceNamespace.ifPresent(MappingsUtilsImpl::setSourceNamespace);
55-
56-
Optional<Supplier<InputStream>> mappings = remapper.getExtraMapping();
57-
58-
mappings.ifPresent(inputStreamSupplier -> {
59-
try {
60-
MappingsRegistry.generateFormattedMappings(inputStreamSupplier.get());
61-
} catch (IOException e) {
62-
throw new RuntimeException(e);
63-
}
64-
});
65-
}
66-
67-
if (!MappingsRegistry.generated) {
68-
try {
69-
MappingsRegistry.generateFormattedMappings(null);
70-
} catch (IOException e) {
71-
throw new RuntimeException(e);
72-
}
73-
}
74-
75-
Path sourceLibraryPath = CacheUtils.getLibraryPath(MappingsUtilsImpl.getSourceNamespace());
76-
77-
if (!Files.exists(sourceLibraryPath)) {
78-
try {
79-
Files.createDirectories(sourceLibraryPath);
80-
} catch (IOException e) {
81-
throw new RuntimeException(e);
82-
}
83-
}
84-
85-
LibraryHandler.gatherRemapLibraries(remappers);
86-
87-
MappingsRegistry.registerAdditionalMappings(remappers);
88-
}
89-
90-
@ApiStatus.Internal
91-
public static void remapMods(Map<Path, Path> pathMap) {
92-
Constants.MAIN_LOGGER.debug("Starting jar remapping!");
93-
SoftLockFixer.preloadClasses();
94-
TinyRemapper remapper = makeRemapper(MappingsRegistry.FORMATTED, MappingsRegistry.ADDITIONAL, MappingsRegistry.MODS);
95-
Constants.MAIN_LOGGER.debug("Remapper created!");
96-
remapFiles(remapper, pathMap);
97-
Constants.MAIN_LOGGER.debug("Jar remapping done!");
98-
99-
MappingsUtilsImpl.writeFullMappings();
100-
}
101-
10219
@Deprecated
10320
public static class MappingList extends ArrayList<MappingBuilder> {
10421
public MappingList() {
@@ -125,124 +42,6 @@ public void accept(MappingVisitor visitor) throws IOException {
12542
}
12643
}
12744

128-
/**
129-
* Will create remapper with specified trees.
130-
*/
131-
private static TinyRemapper makeRemapper(MappingTree... trees) {
132-
TinyRemapper.Builder builder = TinyRemapper
133-
.newRemapper()
134-
.renameInvalidLocals(true)
135-
.ignoreFieldDesc(false)
136-
.propagatePrivate(true)
137-
.ignoreConflicts(true);
138-
139-
if (FabricLoader.getInstance().isDevelopmentEnvironment()) {
140-
builder.fixPackageAccess(true);
141-
}
142-
143-
for (MappingTree tree : trees) {
144-
builder.withMappings(MappingTreeHelper.createMappingProvider(tree, MappingsUtilsImpl.getSourceNamespace(), MappingsUtilsImpl.getTargetNamespace()));
145-
}
146-
147-
MRAApplyVisitor preApplyVisitor = new MRAApplyVisitor();
148-
MRAApplyVisitor postApplyVisitor = new MRAApplyVisitor();
149-
MixinPostApplyVisitor mixinPostApplyVisitor = new MixinPostApplyVisitor();
150-
151-
VisitorInfosImpl preInfos = new VisitorInfosImpl();
152-
VisitorInfosImpl postInfos = new VisitorInfosImpl();
153-
154-
for (ModRemapper modRemapper : remappers) {
155-
modRemapper.registerPreVisitors(preInfos);
156-
modRemapper.registerPostVisitors(postInfos);
157-
}
158-
159-
preApplyVisitor.setInfos(preInfos);
160-
postApplyVisitor.setInfos(postInfos);
161-
162-
builder.extraPreApplyVisitor(preApplyVisitor);
163-
builder.extraPostApplyVisitor(postApplyVisitor);
164-
builder.extraPostApplyVisitor(mixinPostApplyVisitor);
165-
166-
builder.extension(new MixinExtension(EnumSet.of(MixinExtension.AnnotationTarget.HARD)));
167-
168-
TinyRemapper remapper = builder.build();
169-
170-
try {
171-
MinecraftRemapper.addMinecraftJar(remapper);
172-
} catch (IOException e) {
173-
throw new RuntimeException(e);
174-
}
175-
176-
LibraryHandler.addLibrariesToRemapClasspath(remapper);
177-
178-
return remapper;
179-
}
180-
181-
/**
182-
* Will remap file with specified remapper and store it into output.
183-
*
184-
* @param remapper {@link TinyRemapper} to remap with.
185-
*/
186-
private static void remapFiles(TinyRemapper remapper, Map<Path, Path> paths) {
187-
List<OutputConsumerPath> outputConsumerPaths = new ArrayList<>();
188-
189-
List<OutputConsumerPath.ResourceRemapper> resourceRemappers = new ArrayList<>(NonClassCopyMode.FIX_META_INF.remappers);
190-
resourceRemappers.add(new RefmapRemapper());
191-
192-
applyRemapper(remapper, paths, outputConsumerPaths, resourceRemappers, true, MappingsUtilsImpl.getSourceNamespace(), MappingsUtilsImpl.getTargetNamespace());
193-
}
194-
195-
@ApiStatus.Internal
196-
public static void applyRemapper(TinyRemapper remapper, Map<Path, Path> paths, List<OutputConsumerPath> outputConsumerPaths, List<OutputConsumerPath.ResourceRemapper> resourceRemappers, boolean analyzeMapping, String srcNamespace, String targetNamespace) {
197-
try {
198-
Map<Path, InputTag> tagMap = new HashMap<>();
199-
200-
Constants.MAIN_LOGGER.debug("Creating InputTags!");
201-
for (Path input : paths.keySet()) {
202-
InputTag tag = remapper.createInputTag();
203-
tagMap.put(input, tag);
204-
remapper.readInputsAsync(tag, input);
205-
}
206-
207-
Constants.MAIN_LOGGER.debug("Initializing remapping!");
208-
for (Map.Entry<Path, Path> entry : paths.entrySet()) {
209-
Constants.MAIN_LOGGER.debug("Starting remapping " + entry.getKey().toString() + " to " + entry.getValue().toString());
210-
OutputConsumerPath outputConsumer = new OutputConsumerPath.Builder(entry.getValue()).build();
211-
212-
outputConsumerPaths.add(outputConsumer);
213-
214-
Constants.MAIN_LOGGER.debug("Apply remapper!");
215-
remapper.apply(outputConsumer, tagMap.get(entry.getKey()));
216-
217-
Constants.MAIN_LOGGER.debug("Add input as non class file!");
218-
outputConsumer.addNonClassFiles(entry.getKey(), remapper, resourceRemappers);
219-
220-
Constants.MAIN_LOGGER.debug("Done 1!");
221-
}
222-
223-
if (analyzeMapping) MappingsUtilsImpl.completeMappingsFromTr(remapper.getEnvironment(), srcNamespace);
224-
} catch (Exception e) {
225-
remapper.finish();
226-
outputConsumerPaths.forEach(o -> {
227-
try {
228-
o.close();
229-
} catch (IOException e2) {
230-
e2.printStackTrace();
231-
}
232-
});
233-
throw new RuntimeException("Failed to remap jar", e);
234-
} finally {
235-
remapper.finish();
236-
outputConsumerPaths.forEach(o -> {
237-
try {
238-
o.close();
239-
} catch (IOException e) {
240-
e.printStackTrace();
241-
}
242-
});
243-
}
244-
}
245-
24645
@Deprecated
24746
public static String getRemappedFieldName(Class<?> owner, String fieldName) {
24847
return MappingUtils.mapField(owner, fieldName).name;

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

+9
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
@ApiStatus.Internal
2424
public class MappingsUtilsImpl {
2525
private static String sourceNamespace = "official";
26+
private static String defaultPackage = "";
2627

2728
@ApiStatus.Internal
2829
public static String getSourceNamespace() {
@@ -34,6 +35,14 @@ public static void setSourceNamespace(String sourceNamespace) {
3435
MappingsUtilsImpl.sourceNamespace = sourceNamespace;
3536
}
3637

38+
public static String getDefaultPackage() {
39+
return defaultPackage;
40+
}
41+
42+
public static void setDefaultPackage(String defaultPackage) {
43+
MappingsUtilsImpl.defaultPackage = defaultPackage;
44+
}
45+
3746
public static boolean isSourceNamespaceObf() {
3847
return Objects.equals(sourceNamespace, "official");
3948
}

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import fr.catcore.modremapperapi.utils.Constants;
44
import io.github.fabriccompatibiltylayers.modremappingapi.api.v1.ModRemapper;
5-
import fr.catcore.modremapperapi.remapping.RemapUtil;
65
import io.github.fabriccompatibiltylayers.modremappingapi.impl.compatibility.V0ModRemapper;
76
import io.github.fabriccompatibiltylayers.modremappingapi.impl.mappings.MappingsRegistry;
87
import io.github.fabriccompatibiltylayers.modremappingapi.impl.utils.CacheUtils;
@@ -23,7 +22,9 @@ public class ModDiscoverer {
2322
private static final Map<String, List<String>> EXCLUDED = new HashMap<>();
2423

2524
protected static void init(List<ModRemapper> modRemappers, boolean remapClassEdits) {
26-
RemapUtil.init(modRemappers);
25+
ModRemapperContext context = new ModRemapperContext(modRemappers);
26+
27+
context.init();
2728

2829
List<ModEntry> mods = new ArrayList<>();
2930

@@ -78,7 +79,7 @@ protected static void init(List<ModRemapper> modRemappers, boolean remapClassEdi
7879

7980
MappingsRegistry.generateModMappings();
8081

81-
RemapUtil.remapMods(modPaths);
82+
context.remapMods(modPaths);
8283

8384
modPaths.values().forEach(FabricLauncherBase.getLauncher()::addToClassPath);
8485
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package io.github.fabriccompatibiltylayers.modremappingapi.impl;
2+
3+
import fr.catcore.modremapperapi.utils.Constants;
4+
import io.github.fabriccompatibiltylayers.modremappingapi.api.v1.ModRemapper;
5+
import io.github.fabriccompatibiltylayers.modremappingapi.impl.mappings.MappingsRegistry;
6+
import io.github.fabriccompatibiltylayers.modremappingapi.impl.remapper.ModTrRemapper;
7+
import io.github.fabriccompatibiltylayers.modremappingapi.impl.remapper.SoftLockFixer;
8+
import io.github.fabriccompatibiltylayers.modremappingapi.impl.utils.CacheUtils;
9+
import net.fabricmc.tinyremapper.TinyRemapper;
10+
11+
import java.io.IOException;
12+
import java.io.InputStream;
13+
import java.nio.file.Files;
14+
import java.nio.file.Path;
15+
import java.util.List;
16+
import java.util.Map;
17+
import java.util.Optional;
18+
import java.util.function.Supplier;
19+
20+
public class ModRemapperContext {
21+
private final List<ModRemapper> remappers;
22+
23+
public ModRemapperContext(List<ModRemapper> remappers) {
24+
this.remappers = remappers;
25+
}
26+
27+
public void init() {
28+
for (ModRemapper remapper : remappers) {
29+
Optional<String> pkg = remapper.getDefaultPackage();
30+
31+
pkg.ifPresent(MappingsUtilsImpl::setDefaultPackage);
32+
33+
Optional<String> sourceNamespace = remapper.getSourceNamespace();
34+
35+
sourceNamespace.ifPresent(MappingsUtilsImpl::setSourceNamespace);
36+
37+
Optional<Supplier<InputStream>> mappings = remapper.getExtraMapping();
38+
39+
mappings.ifPresent(inputStreamSupplier -> {
40+
try {
41+
MappingsRegistry.generateFormattedMappings(inputStreamSupplier.get());
42+
} catch (IOException e) {
43+
throw new RuntimeException(e);
44+
}
45+
});
46+
}
47+
48+
if (!MappingsRegistry.generated) {
49+
try {
50+
MappingsRegistry.generateFormattedMappings(null);
51+
} catch (IOException e) {
52+
throw new RuntimeException(e);
53+
}
54+
}
55+
56+
Path sourceLibraryPath = CacheUtils.getLibraryPath(MappingsUtilsImpl.getSourceNamespace());
57+
58+
if (!Files.exists(sourceLibraryPath)) {
59+
try {
60+
Files.createDirectories(sourceLibraryPath);
61+
} catch (IOException e) {
62+
throw new RuntimeException(e);
63+
}
64+
}
65+
66+
LibraryHandler.gatherRemapLibraries(remappers);
67+
68+
MappingsRegistry.registerAdditionalMappings(remappers);
69+
}
70+
71+
public void remapMods(Map<Path, Path> pathMap) {
72+
Constants.MAIN_LOGGER.debug("Starting jar remapping!");
73+
SoftLockFixer.preloadClasses();
74+
TinyRemapper remapper = ModTrRemapper.makeRemapper(remappers);
75+
Constants.MAIN_LOGGER.debug("Remapper created!");
76+
ModTrRemapper.remapMods(remapper, pathMap);
77+
Constants.MAIN_LOGGER.debug("Jar remapping done!");
78+
79+
MappingsUtilsImpl.writeFullMappings();
80+
}
81+
}

src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/mappings/MappingsRegistry.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
import java.util.*;
2525
import java.util.zip.ZipError;
2626

27-
import static fr.catcore.modremapperapi.remapping.RemapUtil.defaultPackage;
28-
2927
@ApiStatus.Internal
3028
public class MappingsRegistry {
3129
public static List<String> VANILLA_CLASS_LIST = new ArrayList<>();
@@ -132,7 +130,7 @@ public static void addModMappings(Path path) {
132130
.stream()
133131
.filter(file -> file.endsWith(".class"))
134132
.map(file -> file.replace(".class", ""))
135-
.forEach(cl -> mappingBuilder.addMapping(cl, (cl.contains("/") ? "" : defaultPackage) + cl));
133+
.forEach(cl -> mappingBuilder.addMapping(cl, (cl.contains("/") ? "" : MappingsUtilsImpl.getDefaultPackage()) + cl));
136134
} catch (IOException e) {
137135
throw new RuntimeException(e);
138136
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.github.fabriccompatibiltylayers.modremappingapi.impl.remapper;
2+
3+
import org.jetbrains.annotations.ApiStatus;
4+
5+
import java.util.HashMap;
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
@ApiStatus.Internal
10+
public class MixinRemappingHelper {
11+
public static final Map<String, List<String>> MIXIN2TARGETMAP = new HashMap<>();
12+
}

0 commit comments

Comments
 (0)