Skip to content

Commit

Permalink
Working Dev environment for 1.3+, client and server separated but not…
Browse files Browse the repository at this point in the history
… pre 1.3 client-server-merged
  • Loading branch information
thecatcore committed Oct 30, 2023
1 parent ffd51b1 commit 221d326
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 34 deletions.
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,9 @@ publishing {
modrinth {
token = System.getenv("MODRINTH_TOKEN")
projectId = "mod-remapping-api"
changelog = """Move out annotations to an included library (CursedMixinExtensions).\
Update bundled MixinExtras version to 0.2.0-rc.4.
changelog = """
Working Dev environment for 1.3+, client and server separated but not pre 1.3 client-server-merged.
Update bundled MixinExtras version to 0.2.0.
"""
uploadFile = remapJar

Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ org.gradle.jvmargs=-Xmx1G
# Fabric Properties
minecraft_version = 1.8.9
yarn_build = 458
loader_version = 0.14.22
loader_version = 0.14.24
fabric_version = 1.9.1+1.8.9
mixin_extras_version=0.2.0-rc.4
mixin_extras_version=0.2.0
spasm_version=0.2

# Mod Properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public MappingBuilder method(String name, String description) {
public List<String> build() {
List<String> list = new ArrayList<>();
if (ModRemappingAPI.BABRIC) {
list.add(toString("CLASS", this.intermediary, this.intermediary, this.obfucated, this.obfucated));
list.add(toString("CLASS", this.intermediary, this.intermediary, this.obfucated, this.obfucated, this.intermediary));
} else {
list.add(toString("CLASS", this.obfucated, this.intermediary, this.intermediary));
}
Expand All @@ -80,7 +80,7 @@ public Entry(String obfuscated, String intermediary, String description, Type ty

public String toString(String className) {
if (ModRemappingAPI.BABRIC) {
return MappingBuilder.toString(this.type.name(), className, this.description, this.intermediary, this.intermediary, this.obfuscated, this.obfuscated);
return MappingBuilder.toString(this.type.name(), className, this.description, this.intermediary, this.intermediary, this.obfuscated, this.obfuscated, this.intermediary);
} else {
return MappingBuilder.toString(this.type.name(), className, this.description, this.obfuscated, this.intermediary, this.intermediary);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
import java.util.ArrayList;
import java.util.List;

import static fr.catcore.modremapperapi.utils.MappingsUtils.getNativeNamespace;
import static fr.catcore.modremapperapi.utils.MappingsUtils.getTargetNamespace;

public class MixinPostApplyVisitor implements TinyRemapper.ApplyVisitorProvider{
private final List<MappingTree.ClassMapping> classDefs = new ArrayList<>();
public MixinPostApplyVisitor(MappingTree[] trees) {
Expand Down
22 changes: 4 additions & 18 deletions src/main/java/fr/catcore/modremapperapi/remapping/RemapUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import fr.catcore.modremapperapi.utils.MappingsUtils;
import net.fabricmc.api.EnvType;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.impl.launch.FabricLauncherBase;
import net.fabricmc.mappingio.tree.MappingTree;
import net.fabricmc.mappingio.tree.MemoryMappingTree;
import net.fabricmc.tinyremapper.*;
Expand Down Expand Up @@ -171,7 +170,7 @@ public static void generateModMappings() {
List<String> lines = new ArrayList<>();

if (ModRemappingAPI.BABRIC) {
lines.add(toString("v1", "intermediary", "glue", "server", "client"));
lines.add(toString("v1", "intermediary", "glue", "server", "client", "named"));
} else {
lines.add(toString("v1", "official", "intermediary", "named"));
}
Expand Down Expand Up @@ -226,7 +225,7 @@ private static void generateMappings() {
List<String> lines = new ArrayList<>();

if (ModRemappingAPI.BABRIC) {
lines.add(toString("v1", "intermediary", "glue", "server", "client"));
lines.add(toString("v1", "intermediary", "glue", "server", "client", "named"));
} else {
lines.add(toString("v1", "official", "intermediary", "named"));
}
Expand Down Expand Up @@ -319,20 +318,7 @@ private static TinyRemapper makeRemapper(MappingTree... trees) {

TinyRemapper remapper = builder.build();

if (FabricLoader.getInstance().isDevelopmentEnvironment()) {
try {
remapper.readClassPathAsync(getRemapClasspath().toArray(new Path[0]));
} catch (IOException e) {
throw new RuntimeException("Failed to populate default remap classpath", e);
}
} else {
remapper.readClassPathAsync((Path) FabricLoader.getInstance().getObjectShare().get("fabric-loader:inputGameJar"));

for (Path path : FabricLauncherBase.getLauncher().getClassPath()) {
Constants.MAIN_LOGGER.debug("Appending '%s' to remapper classpath", path);
remapper.readClassPathAsync(path);
}
}
MappingsUtils.addMinecraftJar(remapper);

for (ModRemapper modRemapper : ModRemappingAPI.MOD_REMAPPERS) {
for (RemapLibrary library : modRemapper.getRemapLibraries()) {
Expand Down Expand Up @@ -469,7 +455,7 @@ public static EnvType getEnvironment() {
return FabricLoader.getInstance().getEnvironmentType();
}

private static List<Path> getRemapClasspath() throws IOException {
public static List<Path> getRemapClasspath() throws IOException {
String remapClasspathFile = System.getProperty("fabric.remapClasspathFile");

if (remapClasspathFile == null) {
Expand Down
175 changes: 169 additions & 6 deletions src/main/java/fr/catcore/modremapperapi/utils/MappingsUtils.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package fr.catcore.modremapperapi.utils;

import fr.catcore.modremapperapi.ModRemappingAPI;
import fr.catcore.modremapperapi.remapping.RefmapRemapper;
import net.fabricmc.api.EnvType;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.impl.launch.FabricLauncherBase;
import net.fabricmc.loader.impl.launch.MappingConfiguration;
import net.fabricmc.loader.impl.util.ManifestUtil;
import net.fabricmc.loader.impl.util.log.Log;
Expand All @@ -15,19 +17,23 @@
import net.fabricmc.mappingio.format.Tiny2Reader;
import net.fabricmc.mappingio.tree.MappingTree;
import net.fabricmc.mappingio.tree.MemoryMappingTree;
import net.fabricmc.tinyremapper.IMappingProvider;
import net.fabricmc.tinyremapper.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.*;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
import java.util.zip.ZipError;

import static fr.catcore.modremapperapi.remapping.RemapUtil.getRemapClasspath;

public class MappingsUtils {
private static MemoryMappingTree MINECRAFT_MAPPINGS;
private static boolean initialized = false;
Expand All @@ -41,7 +47,7 @@ public static String getNativeNamespace() {
}

public static String getTargetNamespace() {
return "intermediary";
return !FabricLoader.getInstance().isDevelopmentEnvironment() ? "intermediary" : FabricLoader.getInstance().getMappingResolver().getCurrentRuntimeNamespace();
}

private static void initialize() {
Expand Down Expand Up @@ -179,7 +185,164 @@ public static IMappingProvider createProvider(MappingTree mappings) {
};
}

private static IMappingProvider createBackwardProvider(MappingTree mappings) {
return (acceptor) -> {
final int fromId = mappings.getNamespaceId(getTargetNamespace());
final int toId = mappings.getNamespaceId(getNativeNamespace());

for (MappingTree.ClassMapping classDef : mappings.getClasses()) {
String className = classDef.getName(fromId);
String dstName = classDef.getName(toId);

if (ModRemappingAPI.BABRIC && dstName == null) {
if (className == null) continue;
dstName = className;
}

if (className == null) {
className = dstName;
}

acceptor.acceptClass(className, dstName);

for (MappingTree.FieldMapping field : classDef.getFields()) {
String fieldName = field.getName(fromId);
String dstFieldName = field.getName(toId);
String fieldDesc = field.getDesc(fromId);
String dstFieldDesc = field.getDesc(toId);

if (ModRemappingAPI.BABRIC && dstFieldName == null) {
if (fieldName == null) continue;
dstFieldName = fieldName;
}

if (ModRemappingAPI.BABRIC && dstFieldDesc == null) {
if (fieldDesc == null) continue;
dstFieldDesc = fieldDesc;
}

acceptor.acceptField(memberOf(className, fieldName, fieldDesc), dstFieldName);
}

for (MappingTree.MethodMapping method : classDef.getMethods()) {
String methodName = method.getName(fromId);
String dstMethodName = method.getName(toId);
String methodDesc = method.getDesc(fromId);
String dstMethodDesc = method.getDesc(toId);

if (ModRemappingAPI.BABRIC && dstMethodName == null) {
if (methodName == null) continue;
dstMethodName = methodName;
}

if (ModRemappingAPI.BABRIC && dstMethodDesc == null) {
if (methodDesc == null) continue;
dstMethodDesc = methodDesc;
}

IMappingProvider.Member methodIdentifier = memberOf(className, methodName, methodDesc);
acceptor.acceptMethod(methodIdentifier, dstMethodName);
}
}
};
}

private static IMappingProvider.Member memberOf(String className, String memberName, String descriptor) {
return new IMappingProvider.Member(className, memberName, descriptor);
}

private static Path[] getMinecraftJar() throws IOException {
Path[] originalClassPath = getRemapClasspath().toArray(new Path[0]);

Map<Path, Path> paths = new HashMap<>();

for (Path path :
originalClassPath) {
Constants.MAIN_LOGGER.info(path.toString());
paths.put(path, new File(Constants.LIB_FOLDER, path.toFile().getName()).toPath());
paths.get(path).toFile().delete();
}

TinyRemapper.Builder builder = TinyRemapper
.newRemapper()
.renameInvalidLocals(true)
.ignoreFieldDesc(false)
.propagatePrivate(true)
.ignoreConflicts(true)
.fixPackageAccess(true)
.withMappings(createBackwardProvider(getMinecraftMappings()));

TinyRemapper remapper = builder.build();

Constants.MAIN_LOGGER.info("Remapping minecraft jar back to obfuscated!");

List<OutputConsumerPath> outputConsumerPaths = new ArrayList<>();

List<OutputConsumerPath.ResourceRemapper> resourceRemappers = new ArrayList<>(NonClassCopyMode.FIX_META_INF.remappers);

try {
Map<Path, InputTag> tagMap = new HashMap<>();

Constants.MAIN_LOGGER.debug("Creating InputTags!");
for (Path input : paths.keySet()) {
InputTag tag = remapper.createInputTag();
tagMap.put(input, tag);
remapper.readInputsAsync(tag, input);
}

Constants.MAIN_LOGGER.debug("Initializing remapping!");
for (Map.Entry<Path, Path> entry : paths.entrySet()) {
Constants.MAIN_LOGGER.debug("Starting remapping " + entry.getKey().toString() + " to " + entry.getValue().toString());
OutputConsumerPath outputConsumer = new OutputConsumerPath.Builder(entry.getValue()).build();

outputConsumerPaths.add(outputConsumer);

Constants.MAIN_LOGGER.debug("Apply remapper!");
remapper.apply(outputConsumer, tagMap.get(entry.getKey()));

Constants.MAIN_LOGGER.debug("Add input as non class file!");
outputConsumer.addNonClassFiles(entry.getKey(), remapper, resourceRemappers);

Constants.MAIN_LOGGER.debug("Done 1!");
}
} catch (Exception e) {
remapper.finish();
outputConsumerPaths.forEach(o -> {
try {
o.close();
} catch (IOException e2) {
e2.printStackTrace();
}
});
throw new RuntimeException("Failed to remap jar", e);
} finally {
remapper.finish();
outputConsumerPaths.forEach(o -> {
try {
o.close();
} catch (IOException e) {
e.printStackTrace();
}
});
}

return paths.values().toArray(new Path[0]);
}

public static void addMinecraftJar(TinyRemapper remapper) {
if (FabricLoader.getInstance().isDevelopmentEnvironment()) {
try {
remapper.readClassPathAsync(getMinecraftJar());
} catch (IOException e) {
throw new RuntimeException("Failed to populate default remap classpath", e);
}
} else {
remapper.readClassPathAsync((Path) FabricLoader.getInstance().getObjectShare().get("fabric-loader:inputGameJar"));

for (Path path : FabricLauncherBase.getLauncher().getClassPath()) {
Constants.MAIN_LOGGER.debug("Appending '%s' to remapper classpath", path);
remapper.readClassPathAsync(path);
}
}
}
}
2 changes: 1 addition & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"depends": {
"fabricloader": ">=0.14.18",
"spasm": ">=0.2.0",
"com_github_llamalad7_mixinextras": ">=0.2.0-rc.4"
"com_github_llamalad7_mixinextras": ">=0.2.0"
},
"mixins": [
"mixins.mod-remapping-api.json"
Expand Down

0 comments on commit 221d326

Please sign in to comment.