Skip to content

Commit

Permalink
Why does tiny remapper suck
Browse files Browse the repository at this point in the history
  • Loading branch information
hYdos committed Apr 23, 2022
1 parent 4e69a40 commit 27efc8d
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 91 deletions.
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ repositories {
}

dependencies {
implementation("net.fabricmc:tiny-remapper:0.8.1")
implementation("net.fabricmc:tiny-remapper:0.8.2")
implementation("net.fabricmc:mapping-io:0.3.0")
implementation ("net.minecraftforge:installertools:1.2.10")
implementation ("net.minecraftforge:ForgeAutoRenamingTool:0.1.22")

implementation("org.ow2.asm:asm:9.3")
implementation("org.ow2.asm:asm-analysis:9.3")
Expand Down
33 changes: 16 additions & 17 deletions src/main/java/me/hydos/focu/ForgePatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import me.hydos.focu.mald.logic.MalderLoader;
import me.hydos.focu.mald.logic.MalderPatcher;
import me.hydos.focu.mald.logic.read.InjectTarget;
import me.hydos.focu.providers.ForgeProvider;
import me.hydos.focu.util.FileSystemUtil;
import me.hydos.focu.util.MappingUtils;
import net.fabricmc.tinyremapper.Main;
import org.objectweb.asm.Opcodes;

import java.io.IOException;
import java.nio.file.Files;
Expand All @@ -16,15 +18,14 @@

@SuppressWarnings("ClassCanBeRecord")
public class ForgePatcher {
public static final int ASM_VERSION = Opcodes.ASM9;
public static final Path WORK_DIR = Paths.get(".work");
public final Path minecraftDir;
public final ForgeProvider forge;
public final MalderLoader malderLoader;
private final String forgeVersion;

public ForgePatcher(Path minecraftDir, MalderLoader malderLoader, String forgeVersion) {
this.minecraftDir = minecraftDir;
public ForgePatcher(ForgeProvider forge, MalderLoader malderLoader) {
this.forge = forge;
this.malderLoader = malderLoader;
this.forgeVersion = forgeVersion;

try {
Files.createDirectories(WORK_DIR);
Expand All @@ -34,12 +35,10 @@ public ForgePatcher(Path minecraftDir, MalderLoader malderLoader, String forgeVe
}

public void remapToIntermediary() throws IOException {
Path librariesDir = this.minecraftDir.resolve("libraries");
Path forgeDir = librariesDir.resolve("net/minecraftforge/forge/" + forgeVersion);
Path clientDir = librariesDir.resolve("net/minecraft/client/1.18.2-20220404.173914/");

backupAndRemapJar(forgeDir.resolve("forge-" + forgeVersion + "-universal.jar"));
backupAndRemapJar(clientDir.resolve("client-1.18.2-20220404.173914-srg.jar"));
for (Path target : this.forge.targets()) {
System.out.println("Remapping " + target.getFileName().toString());
backupAndRemapJar(target);
}
}

public void apply() {
Expand All @@ -48,7 +47,7 @@ public void apply() {
} catch (IOException e) {
throw new RuntimeException(e);
}

/*
Path librariesDir = this.minecraftDir.resolve("libraries");
Path forgePatches = librariesDir.resolve("net/minecraftforge/forge/1.18.2-40.0.52/forge-1.18.2-40.0.52-client.jar");
Path forgeIntermediary = forgePatches.getParent().resolve("forge-intermediary.jar");
Expand Down Expand Up @@ -85,10 +84,11 @@ public void apply() {
}
} catch (IOException e) {
throw new RuntimeException("Failed to create output jar!", e);
}
}*/
}

private void backupAndRemapJar(Path inJar, Path outJar) throws IOException {
Path srgToIntermediaryMappings = MappingUtils.getFullMappings();
Path srgToIntermediaryMappings = MappingUtils.getFullMappings(this.forge.minecraftSrg());
Path backupPath = inJar.getParent().resolve(inJar.getFileName().toString().replace(".jar", ".intermediary-backup.jar"));

if (!Files.exists(backupPath)) {
Expand All @@ -100,8 +100,7 @@ private void backupAndRemapJar(Path inJar, Path outJar) throws IOException {
outJar.toAbsolutePath().toString(),
srgToIntermediaryMappings.toAbsolutePath().toString(),
"srg",
"intermediary",
"--fixPackageAccess"
"intermediary"
});

try (FileSystemUtil.Delegate fs = FileSystemUtil.getJarFileSystem(outJar); Stream<Path> files = Files.walk(fs.get().getPath("/"))) {
Expand All @@ -116,7 +115,7 @@ private void backupAndRemapJar(Path inJar, Path outJar) throws IOException {
// Hope it's an AccessTransformer
AccessTransformer at = AccessTransformer.read(path);
System.out.println("Remapping Access Widener");
at.remap(MappingUtils.getFullMappings());
at.remap(srgToIntermediaryMappings);
System.out.println("Overwriting old Access Widener");
Files.delete(path);
at.write(path);
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/me/hydos/focu/Main.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package me.hydos.focu;

import me.hydos.focu.mald.logic.MalderLoader;
import me.hydos.focu.providers.ForgeProvider;

import java.nio.file.Paths;
import java.nio.file.Path;

public class Main {

public static void main(String[] args) {
MalderLoader malderLoader = new MalderLoader()
.loadMalder(Paths.get("malders/TitleScreen.class"));
ForgeProvider forge = ForgeProvider.createFromClient(
Path.of("C:/Users/hayde/AppData/Roaming/.minecraft"),
"1.18.2",
"40.1.0"
);

ForgePatcher patcher = new ForgePatcher(Paths.get("C:/Users/hayde/AppData/Roaming/.minecraft"), malderLoader, "1.18.2-40.0.52");
MalderLoader malderLoader = new MalderLoader();
//.loadMalder(Paths.get("malders/TitleScreen.class"));

ForgePatcher patcher = new ForgePatcher(forge, malderLoader);
patcher.apply();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void remap(Path srgMappingsFile) throws IOException {
this.fieldTransformers.put(fieldTransform, access);
} else {
MappingTree.FieldMapping fieldMapping = getField(classMapping, fieldTransform.field());
this.fieldTransformers.put(new FieldTransform(classMapping.getDstName(intermediary), fieldMapping.getSrcName(), fieldTransform.removeFinal()), access);
this.fieldTransformers.put(new FieldTransform(classMapping.getDstName(intermediary), fieldMapping.getDstName(intermediary), fieldTransform.removeFinal()), access);
}
});

Expand All @@ -118,15 +118,15 @@ public void remap(Path srgMappingsFile) throws IOException {
this.methodTransformers.put(methodTransform, access);
} else {
MappingTree.MethodMapping methodMapping = getMethod(classMapping, methodTransform.methodName());
this.methodTransformers.put(new MethodTransform(classMapping.getDstName(intermediary), methodMapping.getDstDesc(intermediary), remapMethodDesc(srgMappings, methodTransform.methodSig()), methodTransform.removeFinal()), access);
this.methodTransformers.put(new MethodTransform(classMapping.getDstName(intermediary), methodMapping.getDstName(intermediary), methodMapping.getDstDesc(intermediary), methodTransform.removeFinal()), access);
}
});
}
}

private MappingTree.FieldMapping getField(MappingTree.ClassMapping classMapping, String field) {
for (MappingTree.FieldMapping fieldMapping : classMapping.getFields()) {
if (field.equals(fieldMapping.getSrcDesc())) {
if (field.equals(fieldMapping.getDstName(1))) {
return fieldMapping;
}
}
Expand All @@ -135,7 +135,7 @@ private MappingTree.FieldMapping getField(MappingTree.ClassMapping classMapping,

private MappingTree.MethodMapping getMethod(MappingTree.ClassMapping classMapping, String methodName) {
for (MappingTree.MethodMapping methodMapping : classMapping.getMethods()) {
if (methodName.equals(methodMapping.getSrcDesc())) {
if (methodName.equals(methodMapping.getDstName(1))) {
return methodMapping;
}
}
Expand Down
17 changes: 0 additions & 17 deletions src/main/java/me/hydos/focu/mald/logic/MalderMarker.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.hydos.focu.mald.logic;

import me.hydos.focu.ForgePatcher;
import me.hydos.focu.mald.logic.read.InjectTarget;
import me.hydos.focu.mald.logic.read.Malder;
import org.objectweb.asm.*;
Expand All @@ -12,7 +13,7 @@ public class MalderWriterVisitor extends ClassVisitor {
private final InjectTarget target;

public MalderWriterVisitor(ClassVisitor parent, ClassNode malderClass, Malder malder, InjectTarget target) {
super(Opcodes.ASM9, parent);
super(ForgePatcher.ASM_VERSION, parent);
this.malderClass = malderClass;
this.malder = malder;
this.target = target;
Expand Down Expand Up @@ -41,7 +42,7 @@ private static class MethodWriterVisitor extends MethodVisitor {
private final InjectTarget target;

protected MethodWriterVisitor(MethodVisitor methodVisitor, MethodNode method, InjectTarget target) {
super(Opcodes.ASM9, methodVisitor);
super(ForgePatcher.ASM_VERSION, methodVisitor);
this.method = method;
this.target = target;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.hydos.focu.mald.logic.read;

import me.hydos.focu.ForgePatcher;
import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.MethodVisitor;
Expand All @@ -11,7 +12,7 @@ public class MalderReaderVisitor extends ClassVisitor {
private final MethodAnnotationVisitor methodAnnotationVisitor;

public MalderReaderVisitor() {
super(Opcodes.ASM9);
super(ForgePatcher.ASM_VERSION);
this.annotationVisitor = new TargetClassAnnotationVisitor();
this.methodAnnotationVisitor = new MethodAnnotationVisitor(this.annotationVisitor);
}
Expand All @@ -35,7 +36,7 @@ private static class MethodAnnotationVisitor extends MethodVisitor {
private final AnnotationVisitor annotationVisitor;

protected MethodAnnotationVisitor(AnnotationVisitor visitor) {
super(Opcodes.ASM9);
super(ForgePatcher.ASM_VERSION);
this.annotationVisitor = visitor;
}

Expand All @@ -50,7 +51,7 @@ private static class TargetClassAnnotationVisitor extends AnnotationVisitor {
public Malder malder = new Malder();

protected TargetClassAnnotationVisitor() {
super(Opcodes.ASM9);
super(ForgePatcher.ASM_VERSION);
}

// FIXME: assumptions bad. Please fix them soon
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/me/hydos/focu/providers/ForgeProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package me.hydos.focu.providers;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Stream;

public record ForgeProvider(
List<Path> targets,
Path mojmap,
Path minecraftSrg
) {

public static ForgeProvider createFromClient(Path dotMinecraftDir, String minecraftVersion, String forgeVersion) {
String fullForgeVersion = minecraftVersion + "-" + forgeVersion;
String fullMinecraftVersion = detectFullMinecraftVersion(minecraftVersion, dotMinecraftDir);
Path librariesFolder = dotMinecraftDir.resolve("libraries");
Path forgeOrgFolder = librariesFolder.resolve("net/minecraftforge/forge/" + fullForgeVersion);
Path minecraftOrgFolder = librariesFolder.resolve("net/minecraft/client/" + fullMinecraftVersion);

Path mcSrg = minecraftOrgFolder.resolve("client-" + fullMinecraftVersion + "-srg.jar");
return new ForgeProvider(List.of(
forgeOrgFolder.resolve("forge-" + fullForgeVersion + "-client.jar"),
forgeOrgFolder.resolve("forge-" + fullForgeVersion + "-universal.jar"),
mcSrg
), minecraftOrgFolder, mcSrg);
}

private static String detectFullMinecraftVersion(String minecraftVersion, Path dotMinecraftDir) {
try {
try (Stream<Path> list = Files.list(dotMinecraftDir.resolve("libraries/net/minecraft/client"))) {
List<Path> results = list.filter(Files::isDirectory).filter(path -> path.toString().contains(minecraftVersion)).toList();
if (results.isEmpty()) {
throw new RuntimeException("Forge Installer has not been run!");
} else if (results.size() > 1) {
System.out.println("More than one version of " + minecraftVersion + ". Patcher may not work!");
}

return minecraftVersion + results.get(0).getFileName().toString().substring(minecraftVersion.length());
}
} catch (IOException e) {
throw new RuntimeException("Failed to detect full minecraft version", e);
}
}
}
Loading

0 comments on commit 27efc8d

Please sign in to comment.