Skip to content

Commit 3496c57

Browse files
committed
Mappings and Libraries refactoring
1 parent 0fc5da2 commit 3496c57

File tree

4 files changed

+107
-102
lines changed

4 files changed

+107
-102
lines changed

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

Lines changed: 7 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
import fr.catcore.modremapperapi.utils.Constants;
44
import io.github.fabriccompatibiltylayers.modremappingapi.api.v1.MappingUtils;
55
import io.github.fabriccompatibiltylayers.modremappingapi.api.v1.ModRemapper;
6-
import io.github.fabriccompatibiltylayers.modremappingapi.api.v1.RemapLibrary;
7-
import io.github.fabriccompatibiltylayers.modremappingapi.impl.MappingBuilderImpl;
6+
import io.github.fabriccompatibiltylayers.modremappingapi.impl.LibraryHandler;
87
import io.github.fabriccompatibiltylayers.modremappingapi.impl.MappingsUtilsImpl;
98
import io.github.fabriccompatibiltylayers.modremappingapi.impl.VisitorInfosImpl;
109
import io.github.fabriccompatibiltylayers.modremappingapi.impl.mappings.MappingTreeHelper;
@@ -15,35 +14,31 @@
1514
import io.github.fabriccompatibiltylayers.modremappingapi.impl.remapper.visitor.MRAApplyVisitor;
1615
import io.github.fabriccompatibiltylayers.modremappingapi.impl.remapper.visitor.MixinPostApplyVisitor;
1716
import io.github.fabriccompatibiltylayers.modremappingapi.impl.utils.CacheUtils;
18-
import io.github.fabriccompatibiltylayers.modremappingapi.impl.utils.FileUtils;
1917
import net.fabricmc.api.EnvType;
2018
import net.fabricmc.loader.api.FabricLoader;
2119
import net.fabricmc.mappingio.MappingVisitor;
2220
import net.fabricmc.mappingio.tree.MappingTree;
23-
import net.fabricmc.mappingio.tree.MemoryMappingTree;
2421
import net.fabricmc.tinyremapper.*;
2522
import net.fabricmc.tinyremapper.extension.mixin.MixinExtension;
2623
import org.jetbrains.annotations.ApiStatus;
2724

2825
import java.io.*;
29-
import java.net.URISyntaxException;
3026
import java.nio.file.Files;
3127
import java.nio.file.Path;
3228
import java.util.*;
3329
import java.util.function.Supplier;
3430

3531
public class RemapUtil {
3632
private static List<ModRemapper> remappers;
37-
private static MappingTree LOADER_TREE;
3833

3934
@ApiStatus.Internal
4035
public static final Map<String, List<String>> MIXINED = new HashMap<>();
4136

4237
@ApiStatus.Internal
4338
public static String defaultPackage = "";
4439

45-
@ApiStatus.Internal
46-
public static final List<String> MC_CLASS_NAMES = new ArrayList<>();
40+
@Deprecated
41+
public static final List<String> MC_CLASS_NAMES = MappingsRegistry.VANILLA_CLASS_LIST;
4742

4843
@ApiStatus.Internal
4944
public static void init(List<io.github.fabriccompatibiltylayers.modremappingapi.api.v1.ModRemapper> modRemappers) {
@@ -87,73 +82,23 @@ public static void init(List<io.github.fabriccompatibiltylayers.modremappingapi.
8782
}
8883
}
8984

90-
downloadRemappingLibs();
91-
92-
writeMcMappings();
93-
94-
LOADER_TREE = generateMappings();
95-
MappingsUtilsImpl.addMappingsToContext(LOADER_TREE);
96-
97-
for (MappingTree.ClassMapping classView : MappingsRegistry.FORMATTED.getClasses()) {
98-
String className = classView.getName(MappingsUtilsImpl.getSourceNamespace());
85+
LibraryHandler.gatherRemapLibraries(remappers);
9986

100-
if (className != null) {
101-
MC_CLASS_NAMES.add("/" + className + ".class");
102-
}
103-
}
104-
}
105-
106-
private static void downloadRemappingLibs() {
107-
try {
108-
for (ModRemapper remapper : remappers) {
109-
List<RemapLibrary> libraries = new ArrayList<>();
110-
111-
remapper.addRemapLibraries(libraries, FabricLoader.getInstance().getEnvironmentType());
112-
113-
Map<RemapLibrary, Path> libraryPaths = CacheUtils.computeExtraLibraryPaths(libraries, MappingsUtilsImpl.getSourceNamespace());
114-
115-
for (Map.Entry<RemapLibrary, Path> entry : libraryPaths.entrySet()) {
116-
RemapLibrary library = entry.getKey();
117-
Path path = entry.getValue();
118-
119-
if (!library.url.isEmpty()) {
120-
Constants.MAIN_LOGGER.info("Downloading remapping library '" + library.fileName + "' from url '" + library.url + "'");
121-
FileUtils.downloadFile(library.url, path);
122-
FileUtils.removeEntriesFromZip(path, library.toExclude);
123-
Constants.MAIN_LOGGER.info("Remapping library ready for use.");
124-
} else if (library.path != null) {
125-
Constants.MAIN_LOGGER.info("Extracting remapping library '" + library.fileName + "' from mod jar.");
126-
FileUtils.copyZipFile(library.path, path);
127-
Constants.MAIN_LOGGER.info("Remapping library ready for use.");
128-
}
129-
}
130-
}
131-
} catch (IOException | URISyntaxException e) {
132-
throw new RuntimeException(e);
133-
}
87+
MappingsRegistry.registerAdditionalMappings(remappers);
13488
}
13589

13690
@ApiStatus.Internal
13791
public static void remapMods(Map<Path, Path> pathMap) {
13892
Constants.MAIN_LOGGER.debug("Starting jar remapping!");
13993
SoftLockFixer.preloadClasses();
140-
TinyRemapper remapper = makeRemapper(MappingsRegistry.FORMATTED, LOADER_TREE, MappingsRegistry.MODS);
94+
TinyRemapper remapper = makeRemapper(MappingsRegistry.FORMATTED, MappingsRegistry.ADDITIONAL, MappingsRegistry.MODS);
14195
Constants.MAIN_LOGGER.debug("Remapper created!");
14296
remapFiles(remapper, pathMap);
14397
Constants.MAIN_LOGGER.debug("Jar remapping done!");
14498

14599
MappingsUtilsImpl.writeFullMappings();
146100
}
147101

148-
@ApiStatus.Internal
149-
public static void writeMcMappings() {
150-
try {
151-
MappingTreeHelper.exportMappings(MappingsRegistry.FORMATTED, Constants.MC_MAPPINGS_FILE.toPath());
152-
} catch (IOException e) {
153-
throw new RuntimeException(e);
154-
}
155-
}
156-
157102
@Deprecated
158103
public static class MappingList extends ArrayList<MappingBuilder> {
159104
public MappingList() {
@@ -180,28 +125,6 @@ public void accept(MappingVisitor visitor) throws IOException {
180125
}
181126
}
182127

183-
private static MappingTree generateMappings() {
184-
MemoryMappingTree mappingTree;
185-
186-
try {
187-
mappingTree = MappingTreeHelper.createMappingTree();
188-
189-
io.github.fabriccompatibiltylayers.modremappingapi.api.v1.MappingBuilder builder = new MappingBuilderImpl(mappingTree);
190-
191-
for (ModRemapper remapper : remappers) {
192-
remapper.registerMappings(builder);
193-
}
194-
195-
mappingTree.visitEnd();
196-
197-
MappingTreeHelper.exportMappings(mappingTree, Constants.EXTRA_MAPPINGS_FILE.toPath());
198-
} catch (IOException e) {
199-
throw new RuntimeException("Error while generating remappers mappings", e);
200-
}
201-
202-
return mappingTree;
203-
}
204-
205128
/**
206129
* Will create remapper with specified trees.
207130
*/
@@ -250,21 +173,7 @@ private static TinyRemapper makeRemapper(MappingTree... trees) {
250173
throw new RuntimeException(e);
251174
}
252175

253-
for (ModRemapper modRemapper : remappers) {
254-
List<RemapLibrary> libraries = new ArrayList<>();
255-
256-
modRemapper.addRemapLibraries(libraries, FabricLoader.getInstance().getEnvironmentType());
257-
258-
for (RemapLibrary library : libraries) {
259-
Path libPath = CacheUtils.getLibraryPath(MappingsUtilsImpl.getSourceNamespace()).resolve(library.fileName);
260-
261-
if (Files.exists(libPath)) {
262-
remapper.readClassPathAsync(libPath);
263-
} else {
264-
Constants.MAIN_LOGGER.info("Library " + libPath + " does not exist.");
265-
}
266-
}
267-
}
176+
LibraryHandler.addLibrariesToRemapClasspath(remapper);
268177

269178
return remapper;
270179
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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.api.v1.RemapLibrary;
6+
import io.github.fabriccompatibiltylayers.modremappingapi.impl.utils.CacheUtils;
7+
import io.github.fabriccompatibiltylayers.modremappingapi.impl.utils.FileUtils;
8+
import net.fabricmc.loader.api.FabricLoader;
9+
import net.fabricmc.tinyremapper.TinyRemapper;
10+
11+
import java.io.IOException;
12+
import java.net.URISyntaxException;
13+
import java.nio.file.Files;
14+
import java.nio.file.Path;
15+
import java.util.ArrayList;
16+
import java.util.List;
17+
import java.util.Map;
18+
19+
public class LibraryHandler {
20+
private static Map<RemapLibrary, Path> remapLibraries;
21+
22+
public static void gatherRemapLibraries(List<ModRemapper> remappers) {
23+
try {
24+
for (ModRemapper remapper : remappers) {
25+
List<RemapLibrary> libraries = new ArrayList<>();
26+
27+
remapper.addRemapLibraries(libraries, FabricLoader.getInstance().getEnvironmentType());
28+
29+
remapLibraries = CacheUtils.computeExtraLibraryPaths(libraries, MappingsUtilsImpl.getSourceNamespace());
30+
31+
for (Map.Entry<RemapLibrary, Path> entry : remapLibraries.entrySet()) {
32+
RemapLibrary library = entry.getKey();
33+
Path path = entry.getValue();
34+
35+
if (!library.url.isEmpty()) {
36+
Constants.MAIN_LOGGER.info("Downloading remapping library '" + library.fileName + "' from url '" + library.url + "'");
37+
FileUtils.downloadFile(library.url, path);
38+
FileUtils.removeEntriesFromZip(path, library.toExclude);
39+
Constants.MAIN_LOGGER.info("Remapping library ready for use.");
40+
} else if (library.path != null) {
41+
Constants.MAIN_LOGGER.info("Extracting remapping library '" + library.fileName + "' from mod jar.");
42+
FileUtils.copyZipFile(library.path, path);
43+
Constants.MAIN_LOGGER.info("Remapping library ready for use.");
44+
}
45+
}
46+
}
47+
} catch (IOException | URISyntaxException e) {
48+
throw new RuntimeException(e);
49+
}
50+
}
51+
52+
public static void addLibrariesToRemapClasspath(TinyRemapper remapper) {
53+
for (Path libPath : remapLibraries.values()) {
54+
if (Files.exists(libPath)) {
55+
remapper.readClassPathAsync(libPath);
56+
} else {
57+
Constants.MAIN_LOGGER.info("Library " + libPath + " does not exist.");
58+
}
59+
}
60+
}
61+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ private static Map<Path, Path> excludeClassEdits(Map<Path, Path> modPaths, Path
114114
Files.copy(entry.getKey(), entry.getValue(), StandardCopyOption.REPLACE_EXISTING);
115115
}
116116

117-
FileUtils.removeEntriesFromZip(entry.getValue(), RemapUtil.MC_CLASS_NAMES);
117+
FileUtils.removeEntriesFromZip(entry.getValue(), MappingsRegistry.VANILLA_CLASS_LIST);
118118
} catch (IOException | URISyntaxException e) {
119119
e.printStackTrace();
120120
errored.add(entry.getValue());

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

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import fr.catcore.modremapperapi.utils.Constants;
44
import fr.catcore.wfvaio.WhichFabricVariantAmIOn;
55
import io.github.fabriccompatibiltylayers.modremappingapi.api.v1.MappingBuilder;
6+
import io.github.fabriccompatibiltylayers.modremappingapi.api.v1.ModRemapper;
67
import io.github.fabriccompatibiltylayers.modremappingapi.impl.MappingBuilderImpl;
78
import io.github.fabriccompatibiltylayers.modremappingapi.impl.MappingsUtilsImpl;
89
import io.github.fabriccompatibiltylayers.modremappingapi.impl.utils.FileUtils;
@@ -20,24 +21,26 @@
2021
import java.net.URL;
2122
import java.net.URLConnection;
2223
import java.nio.file.Path;
23-
import java.util.HashMap;
24-
import java.util.Locale;
25-
import java.util.Map;
24+
import java.util.*;
2625
import java.util.zip.ZipError;
2726

2827
import static fr.catcore.modremapperapi.remapping.RemapUtil.defaultPackage;
2928

3029
@ApiStatus.Internal
3130
public class MappingsRegistry {
31+
public static List<String> VANILLA_CLASS_LIST = new ArrayList<>();
32+
3233
public static final MemoryMappingTree VANILLA;
3334
public static MemoryMappingTree FORMATTED = new MemoryMappingTree();
3435
public static boolean generated = false;
3536

3637
public static MemoryMappingTree MODS;
38+
public static MemoryMappingTree ADDITIONAL;
3739

3840
static {
3941
try {
4042
MODS = MappingTreeHelper.createMappingTree();
43+
ADDITIONAL = MappingTreeHelper.createMappingTree();
4144
} catch (IOException e) {
4245
throw new RuntimeException(e);
4346
}
@@ -105,6 +108,20 @@ public static void generateFormattedMappings(@Nullable InputStream extraStream)
105108
}
106109

107110
FORMATTED.accept(FULL);
111+
112+
for (MappingTree.ClassMapping classView : FORMATTED.getClasses()) {
113+
String className = classView.getName(MappingsUtilsImpl.getSourceNamespace());
114+
115+
if (className != null) {
116+
VANILLA_CLASS_LIST.add("/" + className + ".class");
117+
}
118+
}
119+
120+
try {
121+
MappingTreeHelper.exportMappings(MappingsRegistry.FORMATTED, Constants.MC_MAPPINGS_FILE.toPath());
122+
} catch (IOException e) {
123+
throw new RuntimeException("Error while writing formatted mappings", e);
124+
}
108125
}
109126

110127
public static void addModMappings(Path path) {
@@ -132,4 +149,22 @@ public static void generateModMappings() {
132149

133150
MappingsUtilsImpl.addMappingsToContext(MODS);
134151
}
152+
153+
public static void registerAdditionalMappings(List<ModRemapper> remappers) {
154+
MappingBuilder builder = new MappingBuilderImpl(ADDITIONAL);
155+
156+
for (ModRemapper remapper : remappers) {
157+
remapper.registerMappings(builder);
158+
}
159+
160+
ADDITIONAL.visitEnd();
161+
162+
try {
163+
MappingTreeHelper.exportMappings(ADDITIONAL, Constants.EXTRA_MAPPINGS_FILE.toPath());
164+
} catch (IOException e) {
165+
throw new RuntimeException("Error while generating remappers mappings", e);
166+
}
167+
168+
MappingsUtilsImpl.addMappingsToContext(ADDITIONAL);
169+
}
135170
}

0 commit comments

Comments
 (0)