Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support package mappings #115

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/main/java/net/fabricmc/mappingio/FlatMappingVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ default boolean visitContent() throws IOException {
return true;
}

// TODO: Un-"default" in the next breaking release
default boolean visitPackage(String srcName, @Nullable String[] dstNames) throws IOException {
return false;
}

// TODO: Un-"default" in the next breaking release
default void visitPackageComment(String srcName, @Nullable String[] dstNames, String comment) throws IOException { }

boolean visitClass(String srcName, @Nullable String[] dstNames) throws IOException;
void visitClassComment(String srcName, @Nullable String[] dstNames, String comment) throws IOException;

Expand Down Expand Up @@ -149,6 +157,17 @@ default boolean visitMethodVar(String srcClsName, String srcMethodName, @Nullabl
}

// convenience / potentially higher efficiency visit methods for only one dst name
default boolean visitPackage(String srcName, String dstName) throws IOException {
return visitPackage(srcName, toArray(dstName));
}

default void visitPackageComment(String srcName, String comment) throws IOException {
visitPackageComment(srcName, (String) null, comment);
}

default void visitPackageComment(String srcName, @Nullable String dstName, String comment) throws IOException {
visitPackageComment(srcName, toArray(dstName), comment);
}

default boolean visitClass(String srcName, String dstName) throws IOException {
return visitClass(srcName, toArray(dstName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* A kind of element that can be mapped.
*/
public enum MappedElementKind {
PACKAGE(0),
CLASS(0),
FIELD(1),
METHOD(1),
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/net/fabricmc/mappingio/MappingVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
* <p>The visitation order is as follows (omitting visit prefixes for brevity, lowercase for cross references):
* <ul><li>overall: header -> content -> End -> overall
* <li>header: Header -> Namespaces [-> Metadata]*
* <li>content: Content [-> class|Metadata]*
* <li>content: Content [-> package|class|Metadata]*
* <li>package: Package [-> DstName]* -> ElementContent*
* <li>class: Class [-> DstName]* -> ElementContent [-> field|method|Comment]*
* <li>field: Field [-> DstName|DstDesc]* -> ElementContent [-> Comment]
* <li>method: Method [-> DstName|DstDesc]* -> ElementContent [-> arg|var|Comment]*
Expand Down Expand Up @@ -85,6 +86,18 @@ default boolean visitContent() throws IOException {
return true;
}

/**
* Visit a package.
*
* @param srcName The package path, with slashes instead of dots, and no trailing slash.
* An empty string represents the default package.
* @return Whether or not the package's content should be visited too.
*/
// TODO: Un-"default" in the next breaking release
default boolean visitPackage(String srcName) throws IOException {
return false;
}

/**
* Visit a class.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@ public void visitNamespaces(String srcNamespace, List<String> dstNamespaces) thr
Set<MappingFlag> flags = next.getFlags();

if (flags.contains(MappingFlag.NEEDS_ELEMENT_UNIQUENESS)) {
dstPackageNames = new String[count];
dstClassNames = new String[count];
dstMemberNames = new String[count];
} else {
dstClassNames = dstMemberNames = null;
dstPackageNames = dstClassNames = dstMemberNames = null;
}

dstMemberDescs = flags.contains(MappingFlag.NEEDS_DST_FIELD_DESC) || flags.contains(MappingFlag.NEEDS_DST_METHOD_DESC) ? new String[count] : null;
Expand All @@ -83,6 +84,16 @@ public boolean visitContent() throws IOException {
return next.visitContent();
}

@Override
public boolean visitPackage(String srcName) throws IOException {
this.srcPkgName = srcName;

Arrays.fill(dstNames, null);
if (dstPackageNames != null) Arrays.fill(dstPackageNames, null);

return true;
}

@Override
public boolean visitClass(String srcName) {
this.srcClsName = srcName;
Expand Down Expand Up @@ -161,6 +172,10 @@ public boolean visitElementContent(MappedElementKind targetKind) throws IOExcept
boolean relay;

switch (targetKind) {
case PACKAGE:
relay = next.visitPackage(srcPkgName, dstNames);
if (relay && dstPackageNames != null) System.arraycopy(dstNames, 0, dstPackageNames, 0, dstNames.length);
break;
case CLASS:
relay = next.visitClass(srcClsName, dstNames);
if (relay && dstClassNames != null) System.arraycopy(dstNames, 0, dstClassNames, 0, dstNames.length);
Expand Down Expand Up @@ -193,6 +208,9 @@ public boolean visitElementContent(MappedElementKind targetKind) throws IOExcept
@Override
public void visitComment(MappedElementKind targetKind, String comment) throws IOException {
switch (targetKind) {
case PACKAGE:
next.visitPackageComment(srcPkgName, dstNames, comment);
break;
case CLASS:
next.visitClassComment(srcClsName, dstClassNames, comment);
break;
Expand All @@ -217,12 +235,14 @@ public void visitComment(MappedElementKind targetKind, String comment) throws IO

private final FlatMappingVisitor next;

private String srcPkgName;
private String srcClsName;
private String srcMemberName;
private String srcMemberDesc;
private String srcMemberSubName;
private int argIdx, lvIndex, startOpIdx, endOpIdx;
private String[] dstNames;
private String[] dstPackageNames;
private String[] dstClassNames;
private String[] dstMemberNames;
private String[] dstMemberDescs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public boolean visitContent() throws IOException {
return next.visitContent();
}

@Override
public boolean visitPackage(String srcName) throws IOException {
return next.visitPackage(srcName);
}

@Override
public boolean visitClass(String srcName) throws IOException {
return next.visitClass(srcName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ public boolean visitContent() throws IOException {
return next.visitContent();
}

@Override
public boolean visitPackage(String srcName) throws IOException {
this.srcName = srcName;

return next.visitPackage(srcName);
}

@Override
public boolean visitClass(String srcName) throws IOException {
this.srcName = srcName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,15 @@ public boolean visitContent() throws IOException {
return next.visitContent();
}

@Override
public boolean visitPackage(String srcName) throws IOException {
if (passThrough) return next.visitPackage(srcName);

this.srcName = srcName;

return true;
}

@Override
public boolean visitClass(String srcName) throws IOException {
if (passThrough) return next.visitClass(srcName);
Expand Down Expand Up @@ -256,6 +265,9 @@ public boolean visitElementContent(MappedElementKind targetKind) throws IOExcept
boolean relay;

switch (targetKind) {
case PACKAGE:
relay = next.visitPackage(dstName);
break;
case CLASS:
relay = next.visitClass(dstName);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,38 @@ public boolean visitContent() throws IOException {
}

@Override
public boolean visitClass(String srcName, String[] dstNames) throws IOException {
public boolean visitPackage(String srcName, @Nullable String[] dstNames) throws IOException {
return visitPackage(srcName, dstNames, null);
}

@Override
public boolean visitPackage(String srcName, String dstName) throws IOException {
return visitPackage(srcName, null, dstName);
}

private boolean visitPackage(String srcName, @Nullable String[] dstNames, @Nullable String dstName) throws IOException {
if (!srcName.equals(lastPackage)) {
lastPackage = srcName;
relayLastPackage = next.visitPackage(srcName) && visitDstNames(MappedElementKind.PACKAGE, dstNames, dstName);
}

return relayLastPackage;
}

@Override
public void visitPackageComment(String srcName, @Nullable String[] dstNames, String comment) throws IOException {
if (!visitPackage(srcName, dstNames, null)) return;
next.visitComment(MappedElementKind.PACKAGE, comment);
}

@Override
public void visitPackageComment(String srcName, @Nullable String dstName, String comment) throws IOException {
if (!visitPackage(srcName, null, dstName)) return;
next.visitComment(MappedElementKind.PACKAGE, comment);
}

@Override
public boolean visitClass(String srcName, @Nullable String[] dstNames) throws IOException {
return visitClass(srcName, dstNames, null);
}

Expand Down Expand Up @@ -367,6 +398,8 @@ private boolean visitDstNamesDescs(MappedElementKind targetKind, @Nullable Strin
private boolean relayDstFieldDescs;
private boolean relayDstMethodDescs;

private String lastPackage;
private boolean relayLastPackage;
private String lastClass;
private boolean relayLastClass;
private String lastMemberName, lastMemberDesc;
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/net/fabricmc/mappingio/format/FeatureSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@ public interface FeatureSet {
boolean hasFileComments();

default boolean supportsPackages() {
return packages().srcNames() != FeaturePresence.ABSENT
|| packages().dstNames() != FeaturePresence.ABSENT;
return packages().srcNames() != FeaturePresence.ABSENT;
}

default boolean supportsClasses() {
return classes().srcNames() != FeaturePresence.ABSENT
|| classes().dstNames() != FeaturePresence.ABSENT;
return classes().srcNames() != FeaturePresence.ABSENT;
}

default boolean supportsFields() {
Expand Down
18 changes: 0 additions & 18 deletions src/main/java/net/fabricmc/mappingio/format/MappingFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,6 @@ public enum MappingFormat {

/**
* The {@code SRG} ("Searge RetroGuard") mapping format, as specified <a href="https://github.com/MinecraftForge/SrgUtils/blob/67f30647ece29f18256ca89a23cda6216d6bd21e/src/main/java/net/minecraftforge/srgutils/InternalUtils.java#L69-L81">here</a>.
*
* <h2>Implementation notes</h2>
* Package mappings are currently not supported.
*/
SRG_FILE("SRG file", "srg", true, FeatureSetBuilder.create()
.withPackages(p -> p
Expand All @@ -141,9 +138,6 @@ public enum MappingFormat {
* The {@code XSRG} ("Extended SRG") mapping format, as specified <a href="https://github.com/MinecraftForge/SrgUtils/blob/67f30647ece29f18256ca89a23cda6216d6bd21e/src/main/java/net/minecraftforge/srgutils/InternalUtils.java#L69-L84">here</a>.
*
* <p>Same as SRG, but with field descriptors.
*
* <h2>Implementation notes</h2>
* Package mappings are currently not supported.
*/
XSRG_FILE("XSRG file", "xsrg", true, FeatureSetBuilder.createFrom(SRG_FILE.features)
.withFields(f -> f
Expand All @@ -168,9 +162,6 @@ public enum MappingFormat {

/**
* The {@code CSRG} ("Compact SRG", since it saves disk space over SRG) mapping format, as specified <a href="https://github.com/MinecraftForge/SrgUtils/blob/67f30647ece29f18256ca89a23cda6216d6bd21e/src/main/java/net/minecraftforge/srgutils/InternalUtils.java#L196-L207">here</a>.
*
* <h2>Implementation notes</h2>
* Package mappings are currently not supported.
*/
CSRG_FILE("CSRG file", "csrg", true, FeatureSetBuilder.createFrom(SRG_FILE.features)
.withMethods(m -> m
Expand All @@ -180,17 +171,11 @@ public enum MappingFormat {
* The {@code TSRG} ("Tiny SRG", since it saves disk space over SRG) mapping format, as specified <a href="https://github.com/MinecraftForge/SrgUtils/blob/67f30647ece29f18256ca89a23cda6216d6bd21e/src/main/java/net/minecraftforge/srgutils/InternalUtils.java#L196-L213">here</a>.
*
* <p>Same as CSRG, but hierarchical instead of flat.
*
* <h2>Implementation notes</h2>
* Package mappings are currently not supported.
*/
TSRG_FILE("TSRG file", "tsrg", true, FeatureSetBuilder.createFrom(CSRG_FILE.features)),

/**
* The {@code TSRG v2} mapping format, as specified <a href="https://github.com/MinecraftForge/SrgUtils/blob/67f30647ece29f18256ca89a23cda6216d6bd21e/src/main/java/net/minecraftforge/srgutils/InternalUtils.java#L262-L285">here</a>.
*
* <h2>Implementation notes</h2>
* Package mappings and static markers for methods are currently not supported.
*/
TSRG_2_FILE("TSRG v2 file", "tsrg", true, FeatureSetBuilder.createFrom(TSRG_FILE.features)
.withNamespaces(true)
Expand Down Expand Up @@ -255,9 +240,6 @@ public enum MappingFormat {

/**
* The {@code JOBF} mapping format, as specified <a href="https://github.com/skylot/jadx/blob/2d5c0fda4a0c5d16207a5f48edb72e6efa7d5bbd/jadx-core/src/main/java/jadx/core/deobf/DeobfPresets.java">here</a>.
*
* <h2>Implementation notes</h2>
* Package mappings are currently not supported.
*/
JOBF_FILE("JOBF file", "jobf", true, FeatureSetBuilder.create()
.withPackages(c -> c
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,22 +122,29 @@ private static void read0(BufferedReader reader, String sourceNs, String targetN
break;
case "entry":
String type = xmlReader.getAttributeValue(null, "type");
boolean isPackage;

if (type == null || type.isEmpty()) throw new IOException("missing/empty type attribute at line "+xmlReader.getLocation().getLineNumber());
if (type.equals("package")) continue; // TODO: support packages
if (!type.equals("class")) throw new IOException("unexpected entry type "+type+" at line "+xmlReader.getLocation().getLineNumber());

if (!(isPackage = type.equals("package")) && !type.equals("class")) {
throw new IOException("unexpected entry type "+type+" at line "+xmlReader.getLocation().getLineNumber());
}

String srcName = xmlReader.getAttributeValue(null, "oldName");
String dstName = xmlReader.getAttributeValue(null, "newName");
// String recursive = xmlReader.getAttributeValue(null, "recursive"); // only used for packages
// String recursive = xmlReader.getAttributeValue(null, "recursive"); // only used for packages, indicates if subpackages should also be renamed

if (srcName == null || srcName.isEmpty()) throw new IOException("missing/empty oldName attribute at line "+xmlReader.getLocation().getLineNumber());
if (dstName == null || dstName.isEmpty()) throw new IOException("missing/empty newName attribute at line "+xmlReader.getLocation().getLineNumber());

srcName = srcName.replace('.', '/');
dstName = dstName.replace('.', '/');

if (visitor.visitClass(srcName)) {
if (isPackage && visitor.visitPackage(srcName)) {
visitor.visitDstName(MappedElementKind.PACKAGE, 0, dstName);
visitor.visitElementContent(MappedElementKind.PACKAGE);
// TODO: visit "recursive" attribute as element metadata once https://github.com/FabricMC/mapping-io/pull/41 is merged
} else if (!isPackage && visitor.visitClass(srcName)) {
visitor.visitDstName(MappedElementKind.CLASS, 0, dstName);
visitor.visitElementContent(MappedElementKind.CLASS);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,20 @@ public void visitMetadata(String key, @Nullable String value) throws IOException
}
}

@Override
public boolean visitPackage(String srcName) throws IOException {
this.srcName = srcName;
this.dstName = null;
this.isPackage = true;

return true;
}

@Override
public boolean visitClass(String srcName) throws IOException {
this.srcName = srcName;
this.dstName = null;
this.isPackage = false;

return true;
}
Expand Down Expand Up @@ -161,7 +171,11 @@ public boolean visitElementContent(MappedElementKind targetKind) throws IOExcept
xmlWriter.writeEmptyElement("entry");
xmlWriter.writeAttribute("oldName", srcName.replace('/', '.'));
xmlWriter.writeAttribute("newName", dstName.replace('/', '.'));
xmlWriter.writeAttribute("type", "class");
xmlWriter.writeAttribute("type", isPackage ? "package" : "class");

if (isPackage) {
xmlWriter.writeAttribute("recursive", "false"); // TODO: true or false?
}
} catch (XMLStreamException e) {
throw new IOException(e);
}
Expand All @@ -180,6 +194,7 @@ public void visitComment(MappedElementKind targetKind, String comment) throws IO
private XMLStreamWriter xmlWriter;
private boolean wroteName;
private boolean wroteOrder;
private boolean isPackage;
private String srcName;
private String dstName;
}
Loading
Loading