Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -48,6 +49,7 @@
import net.fabricmc.tinyremapper.extension.mixin.common.data.Message;
import net.fabricmc.tinyremapper.extension.mixin.common.data.Pair;
import net.fabricmc.tinyremapper.extension.mixin.soft.data.MemberInfo;
import net.fabricmc.tinyremapper.extension.mixin.soft.util.RegexMatcher;

/**
* If the {@code method} element does not contain a name, then do not remap it; If the
Expand Down Expand Up @@ -96,6 +98,21 @@ public AnnotationVisitor visitArray(String name) {
public void visit(String name, Object value) {
String string = Objects.requireNonNull((String) value);

// ending slash -> regex target
if (string.endsWith("/")) {
List<String> resolved = resolveAndRemapMixinRegex(string);

if (resolved == null) {
super.visit(name, value);
} else {
for (String remappedSelector : resolved) {
super.visit(name, remappedSelector);
}
}

return;
}

MemberInfo info = MemberInfo.parse(string.replaceAll("\\s", ""));

if (info == null) {
Expand Down Expand Up @@ -155,6 +172,42 @@ public AnnotationVisitor visitAnnotation(String name, String descriptor) {
return av;
}

private List<String> resolveAndRemapMixinRegex(String input) {
RegexMatcher matcher;

try {
matcher = RegexMatcher.parse(input);
} catch (RegexMatcher.ParsingException e) {
data.getLogger().warn("Error parsing mixin regex %s: %s", input, e.toString());
return null;
}

List<? extends TrMethod> matchedMethods = Objects.requireNonNull(targets).stream()
.map(data.resolver::resolveClass)
.filter(Optional::isPresent)
.map(Optional::get)
.flatMap(trClass -> trClass.getMethods().stream())
.filter(trMethod -> matcher.matches(trMethod.getOwner().getName(), trMethod.getName(), trMethod.getDesc()))
.sorted(
Comparator
.<TrMethod, String>comparing(trMethod -> trMethod.getOwner().getName())
.thenComparing(TrMember::getName)
.thenComparing(TrMember::getDesc)
)
.collect(Collectors.toList());

List<String> result = new ArrayList<>();

for (TrMethod matchedMethod : matchedMethods) {
String mappedOwner = data.mapper.mapName(matchedMethod.getOwner());
String mappedName = data.mapper.mapName(matchedMethod);
String mappedDesc = data.mapper.mapDesc(matchedMethod);
result.add(String.format("L%s;%s%s", mappedOwner, mappedName, mappedDesc));
}

return result;
}

private static class InjectMethodMappable implements IMappable<List<MemberInfo>> {
private final CommonData data;
private final MemberInfo info;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright (c) 2016, 2018, Player, asie
* Copyright (c) 2026, FabricMC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package net.fabricmc.tinyremapper.extension.mixin.soft.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class RegexMatcher {
private static final Pattern PATTERN = Pattern.compile("((owner|name|desc)\\s*=\\s*)?/(.*?)(?<!\\\\)/");

private final Pattern owner;
private final Pattern name;
private final Pattern desc;

private RegexMatcher(Pattern owner, Pattern name, Pattern desc) {
this.owner = owner;
this.name = name;
this.desc = desc;
}

public static RegexMatcher parse(String input) throws ParsingException {
Matcher matcher = PATTERN.matcher(input);

Pattern owner = null;
Pattern name = null;
Pattern desc = null;

while (matcher.find()) {
Pattern pattern;

try {
pattern = Pattern.compile(matcher.group(3));
} catch (PatternSyntaxException e) {
throw new ParsingException(String.format("Error parsing pattern %s", matcher.group(3)), e);
}

String key = matcher.group(2);

if (key != null && key.equals("owner")) {
if (owner != null) {
throw new ParsingException(String.format("Duplicate owner field, old=/%s/, new=/%s/", owner.pattern(), pattern.pattern()));
} else {
owner = pattern;
}
} else if (key != null && key.equals("desc")) {
if (desc != null) {
throw new ParsingException(String.format("Duplicate desc field, old=/%s/, new=/%s/", desc.pattern(), pattern.pattern()));
} else {
desc = pattern;
}
} else {
if (name != null) {
throw new ParsingException(String.format("Duplicate name field, old=/%s/, new=/%s/", name.pattern(), pattern.pattern()));
} else {
name = pattern;
}
}
}

return new RegexMatcher(owner, name, desc);
}

public boolean matches(String owner, String name, String desc) {
return matches0(owner, this.owner) && matches0(name, this.name) && matches0(desc, this.desc);
}

private boolean matches0(String input, Pattern pattern) {
return pattern == null || input == null || pattern.matcher(input).find();
}

public static class ParsingException extends Exception {
public ParsingException(String message) {
super(message);
}

public ParsingException(String message, Throwable cause) {
super(message, cause);
}

public ParsingException(Throwable cause) {
super(cause);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@
import net.fabricmc.tinyremapper.extension.mixin.integration.mixins.DescAtMixin;
import net.fabricmc.tinyremapper.extension.mixin.integration.mixins.LvtRemapTargetMixin;
import net.fabricmc.tinyremapper.extension.mixin.integration.mixins.NonObfuscatedOverrideMixin;
import net.fabricmc.tinyremapper.extension.mixin.integration.mixins.RegexMethodTargetMixin;
import net.fabricmc.tinyremapper.extension.mixin.integration.mixins.SeparateRemappedNameMixin;
import net.fabricmc.tinyremapper.extension.mixin.integration.mixins.WildcardTargetMixin;
import net.fabricmc.tinyremapper.extension.mixin.integration.targets.AmbiguousRemappedNameTarget;
import net.fabricmc.tinyremapper.extension.mixin.integration.targets.DescAtTarget;
import net.fabricmc.tinyremapper.extension.mixin.integration.targets.LvtRemapTarget;
import net.fabricmc.tinyremapper.extension.mixin.integration.targets.NonObfuscatedOverrideTarget;
import net.fabricmc.tinyremapper.extension.mixin.integration.targets.RegexMethodTarget;
import net.fabricmc.tinyremapper.extension.mixin.integration.targets.SeparateRemappedNameTarget;
import net.fabricmc.tinyremapper.extension.mixin.integration.targets.WildcardTarget;

Expand Down Expand Up @@ -155,6 +157,22 @@ public void remapDescAt() throws IOException {
assertTrue(remapped.contains("@Lorg/spongepowered/asm/mixin/injection/Desc;(args={java.lang.String.class}, ret=int.class, value=\"at\""));
}

@Test
public void remapRegexMethodTarget() throws IOException {
String remapped = remap(RegexMethodTarget.class, RegexMethodTargetMixin.class, out -> {
String fqn = "net/fabricmc/tinyremapper/extension/mixin/integration/targets/RegexMethodTarget";
out.acceptClass(fqn, "com/example/Remapped");
out.acceptMethod(new IMappingProvider.Member(fqn, "target0", "()Ljava/lang/String;"), "t0");
out.acceptMethod(new IMappingProvider.Member(fqn, "target0", "(Ljava/lang/String;)Ljava/lang/String;"), "t00");
out.acceptMethod(new IMappingProvider.Member(fqn, "target1", "()Ljava/lang/String;"), "t1");
out.acceptMethod(new IMappingProvider.Member(fqn, "target2", "(Ljava/util/List;)Ljava/lang/String;"), "t2");
out.acceptMethod(new IMappingProvider.Member(fqn, "target3", "(Ljava/lang/String;)V"), "t3");
out.acceptMethod(new IMappingProvider.Member(fqn, "thing4", "()Ljava/lang/String;"), "thing");
});

assertTrue(remapped.contains("method={\"Lcom/example/Remapped;t0()Ljava/lang/String;\", \"Lcom/example/Remapped;t00(Ljava/lang/String;)Ljava/lang/String;\", \"Lcom/example/Remapped;t1()Ljava/lang/String;\", \"Lcom/example/Remapped;t2(Ljava/util/List;)Ljava/lang/String;\"}"));
}

private String remap(Class<?> target, Class<?> mixin, IMappingProvider mappings) throws IOException {
Path classpath = createJar(target);
Path input = createJar(mixin);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2016, 2018, Player, asie
* Copyright (c) 2026, FabricMC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package net.fabricmc.tinyremapper.extension.mixin.integration.mixins;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import net.fabricmc.tinyremapper.extension.mixin.integration.targets.RegexMethodTarget;

@Mixin(RegexMethodTarget.class)
public class RegexMethodTargetMixin {
@Inject(method = "/^target/ desc=/String;$/", at = @At("RETURN"))
private void onTargets(CallbackInfoReturnable<String> cir) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2016, 2018, Player, asie
* Copyright (c) 2026, FabricMC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package net.fabricmc.tinyremapper.extension.mixin.integration.targets;

import java.util.List;

public class RegexMethodTarget {
private String target0() {
return "target0";
}

private String target0(String input) {
return input;
}

private String target1() {
return "target1";
}

private String target2(List<String> input) {
return input.toString();
}

private void target3(String input) {
}

private String thing4() {
return "thing4";
}
}
Loading