diff options
author | Linnea Gräf <nea@nea.moe> | 2024-03-06 00:28:18 +0100 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2024-03-06 00:28:18 +0100 |
commit | 24553fcf7129e94559627a2edbf02777dc987f64 (patch) | |
tree | 45153ff5b673cbf008f11a2d7be65fcd5e81dfbc | |
parent | 73c606bf8ea782ffd2fb35597a5991add8c78af7 (diff) | |
download | zwirn-24553fcf7129e94559627a2edbf02777dc987f64.tar.gz zwirn-24553fcf7129e94559627a2edbf02777dc987f64.tar.bz2 zwirn-24553fcf7129e94559627a2edbf02777dc987f64.zip |
Add renaming/reordering
-rw-r--r-- | build.gradle.kts | 9 | ||||
-rw-r--r-- | src/main/java/moe/nea/zwirn/RenameTask.java | 84 | ||||
-rw-r--r-- | src/main/java/moe/nea/zwirn/Zwirn.java | 18 |
3 files changed, 111 insertions, 0 deletions
diff --git a/build.gradle.kts b/build.gradle.kts index 0596e52..50f3fe5 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,5 +1,6 @@ plugins { java + `maven-publish` } group = "moe.nea" @@ -27,4 +28,12 @@ java { tasks.test { useJUnitPlatform() +} + +publishing { + publications { + create<MavenPublication>("maven") { + from(components["java"]) + } + } }
\ No newline at end of file diff --git a/src/main/java/moe/nea/zwirn/RenameTask.java b/src/main/java/moe/nea/zwirn/RenameTask.java new file mode 100644 index 0000000..89c15c4 --- /dev/null +++ b/src/main/java/moe/nea/zwirn/RenameTask.java @@ -0,0 +1,84 @@ +package moe.nea.zwirn; + +import net.fabricmc.stitch.commands.tinyv2.*; +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +public class RenameTask { + private final TinyFile tinyFile; + private final List<Zwirn.RenameCommand> newNamespaceOrder; + private final int[] namespaceRemapOrder; + + public RenameTask(@NotNull TinyFile tinyFile, @NotNull List<Zwirn.RenameCommand> newNamespaceOrder) { + this.tinyFile = tinyFile; + this.newNamespaceOrder = newNamespaceOrder; + namespaceRemapOrder = newNamespaceOrder.stream().mapToInt( + it -> tinyFile.getHeader().getNamespaces().indexOf(it.oldNamespaceName()) + ).toArray(); + } + + + private List<String> rename(List<String> strings) { + List<String> newNames = new ArrayList<>(namespaceRemapOrder.length); + for (int j : namespaceRemapOrder) { + newNames.add(strings.get(j)); + } + return newNames; + } + + public TinyFile rename() { + return new TinyFile( + new TinyHeader(newNamespaceOrder.stream().map(it -> it.newNamespaceName()).collect(Collectors.toList()), 2, 0, tinyFile.getHeader().getProperties()), + tinyFile.getClassEntries().stream().map(this::renameClass).collect(Collectors.toList()) + ); + } + + private TinyClass renameClass(TinyClass tinyClass) { + return new TinyClass( + rename(tinyClass.getClassNames()), + tinyClass.getMethods().stream().map(this::renameMethod).collect(Collectors.toList()), + tinyClass.getFields().stream().map(this::renameField).collect(Collectors.toList()), + tinyClass.getComments() + ); + } + + private TinyField renameField(TinyField tinyField) { + var names = rename(tinyField.getFieldNames()); + return new TinyField( + names.get(0), + names, + tinyField.getComments() + ); + } + + private TinyMethod renameMethod(TinyMethod tinyMethod) { + var names = rename(tinyMethod.getMethodNames()); + return new TinyMethod( + names.get(0), + names, + tinyMethod.getParameters().stream().map(this::renameMethodParameter).collect(Collectors.toList()), + tinyMethod.getLocalVariables().stream().map(this::renameVariable).collect(Collectors.toList()), + tinyMethod.getComments() + ); + } + + private TinyMethodParameter renameMethodParameter(TinyMethodParameter tinyMethodParameter) { + return new TinyMethodParameter( + tinyMethodParameter.getLvIndex(), + rename(tinyMethodParameter.getParameterNames()), + tinyMethodParameter.getComments() + ); + } + + private TinyLocalVariable renameVariable(TinyLocalVariable tinyLocalVariable) { + return new TinyLocalVariable( + tinyLocalVariable.getLvIndex(), tinyLocalVariable.getLvStartOffset(), + tinyLocalVariable.getLvTableIndex(), + rename(tinyLocalVariable.getLocalVariableNames()), + tinyLocalVariable.getComments() + ); + } +} diff --git a/src/main/java/moe/nea/zwirn/Zwirn.java b/src/main/java/moe/nea/zwirn/Zwirn.java index 3612007..b3a4956 100644 --- a/src/main/java/moe/nea/zwirn/Zwirn.java +++ b/src/main/java/moe/nea/zwirn/Zwirn.java @@ -21,6 +21,24 @@ public class Zwirn { return new TinyMerger(base, overlay, sharedNamespace).merge(); } + + public record RenameCommand( + String oldNamespaceName, + String newNamespaceName + ) { + } + + public static @NotNull TinyFile renameNamespaces( + @NotNull TinyFile tinyFile, + @NotNull List<@NotNull RenameCommand> newNamespaceOrder + ) { + for (var renameCommand : newNamespaceOrder) { + if (!tinyFile.getHeader().getNamespaces().contains(renameCommand.oldNamespaceName())) + throw new IllegalArgumentException("Old namespace " + renameCommand.oldNamespaceName() + " not found"); + } + return new RenameTask(tinyFile, newNamespaceOrder).rename(); + } + public static @NotNull TinyFile enrichSeargeWithMCP(@NotNull TinyFile searge, @NotNull Path mcpArchiveRoot) throws IOException { if (!searge.getHeader().getNamespaces().equals(Arrays.asList("left", "right"))) throw new IllegalArgumentException("Searge namespaces need to be left and right"); |