Skip to content

Commit

Permalink
add generateAccessWidenerFromTransformer task
Browse files Browse the repository at this point in the history
  • Loading branch information
Nolij committed Sep 29, 2023
1 parent 8fed9aa commit 3fc77c2
Show file tree
Hide file tree
Showing 7 changed files with 607 additions and 4 deletions.
2 changes: 1 addition & 1 deletion felt-spindle/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ loom_version=1.3.8
asm_version=9.5
mapping_io_version=0.4.2

base_version=0.1
base_version=0.2
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package net.feltmc.spindle;

import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Optional;

public abstract class SpindleExtension {

@Optional
public abstract RegularFileProperty getAccessTransformerPath();

@Optional
public abstract Property<Boolean> getOverwriteAccessWidener();

// @Optional
// public abstract Property<Boolean> getAutoConvertATToAW();
//
// @Optional
// public abstract Property<Boolean> getApplyATInDev();

}
25 changes: 23 additions & 2 deletions felt-spindle/src/main/java/net/feltmc/spindle/SpindlePlugin.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
package net.feltmc.spindle;

import net.fabricmc.loom.LoomGradleExtension;
import net.fabricmc.loom.api.LoomGradleExtensionAPI;
import net.feltmc.spindle.processors.ClassOverlayProcessor;
import net.feltmc.spindle.task.GenerateAccessWidenerFromTransformerTask;
import org.gradle.api.Plugin;
import org.gradle.api.Project;

public class SpindlePlugin implements Plugin<Project> {

public void apply(Project project) {
LoomGradleExtensionAPI loomExtension = project.getExtensions().findByType(LoomGradleExtensionAPI.class);
loomExtension.addMinecraftJarProcessor(ClassOverlayProcessor.class, "felt-spindle:overlays");
final LoomGradleExtensionAPI loom = project.getExtensions().findByType(LoomGradleExtensionAPI.class);
if (loom == null)
throw new AssertionError("Fabric Loom not found!");

loom.addMinecraftJarProcessor(ClassOverlayProcessor.class, "felt-spindle:overlays");

var config = project.getExtensions().create("spindle", SpindleExtension.class);

project.getTasks().register("generateAccessWidenerFromTransformer", GenerateAccessWidenerFromTransformerTask.class, task -> {
task.getAccessWidenerPath().set(loom.getAccessWidenerPath());
task.getAccessTransformerPath().set(config.getAccessTransformerPath());
task.getProjectMappingsFile().set(loom::getMappingsFile);
//noinspection UnstableApiUsage
task.getMinecraftVersion().set(loom.getIntermediateMappingsProvider().getMinecraftVersion());
//noinspection UnstableApiUsage
task.getMinecraftVersionMeta().set(((LoomGradleExtension) loom).getMinecraftProvider().getVersionInfo());
task.getOverwriteAccessWidener().set(config.getOverwriteAccessWidener());
});
}

}
129 changes: 129 additions & 0 deletions felt-spindle/src/main/java/net/feltmc/spindle/mapping/Mappings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package net.feltmc.spindle.mapping;

import net.fabricmc.loom.configuration.providers.minecraft.MinecraftVersionMeta;
import net.fabricmc.mappingio.MappingVisitor;
import net.fabricmc.mappingio.adapter.MappingSourceNsSwitch;
import net.fabricmc.mappingio.format.ProGuardReader;
import net.fabricmc.mappingio.format.Tiny2Reader;
import net.fabricmc.mappingio.format.TsrgReader;
import net.fabricmc.mappingio.tree.MappingTree;
import net.fabricmc.mappingio.tree.MemoryMappingTree;
import net.feltmc.spindle.util.LazyMap;

import java.io.*;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Mappings {

private static final String SRG_URL_TEMPLATE = "https://raw.githubusercontent.com/MinecraftForge/MCPConfig/master/versions/release/%s/joined.tsrg";
private static final String MOJMAP_CLIENT_MAPPINGS = "client_mappings";
private static final String MOJMAP_SERVER_MAPPINGS = "server_mappings";

public enum Namespace {
OBF("obf"),
SRG("srg"),
MOJMAP("mojmap"),
INTERMEDIARY("intermediary"),
NAMED("named"),
MERGED("merged"),
;

public final String name;

Namespace(String name) {
this.name = name;
}
}

public final MappingTree tree;
public final Map<Namespace, Map<String, MappingTree.ClassMapping>> map;

public Mappings(final File projectMappingsFile, final String mcVersion, final MinecraftVersionMeta mcVersionMeta) throws IOException {
final MemoryMappingTree projectMappingsTree = new MemoryMappingTree();
projectMappingsTree.visitHeader();
Tiny2Reader.read(new FileReader(projectMappingsFile), projectMappingsTree);
projectMappingsTree.visitEnd();

final String srgURL = String.format(SRG_URL_TEMPLATE, mcVersion);
final MemoryMappingTree srgMappingsTree = new MemoryMappingTree();
srgMappingsTree.visitHeader();
//noinspection deprecation
TsrgReader.read(new InputStreamReader(new URL(srgURL).openStream()), srgMappingsTree);
srgMappingsTree.visitEnd();

final String mojMapClientUrl = mcVersionMeta.download(MOJMAP_CLIENT_MAPPINGS).url();
final String mojMapServerUrl = mcVersionMeta.download(MOJMAP_SERVER_MAPPINGS).url();
final MemoryMappingTree mojMapTree = new MemoryMappingTree();
final MappingVisitor mojMapInverter = new MappingSourceNsSwitch(mojMapTree, Namespace.OBF.name);
mojMapTree.visitHeader();
//noinspection deprecation
ProGuardReader.read(new InputStreamReader(new URL(mojMapClientUrl).openStream()), Namespace.MOJMAP.name, Namespace.OBF.name, mojMapInverter);
//noinspection deprecation
ProGuardReader.read(new InputStreamReader(new URL(mojMapServerUrl).openStream()), Namespace.MOJMAP.name, Namespace.OBF.name, mojMapInverter);
mojMapTree.visitEnd();

tree = MergingMappingVisitor.merge(
Namespace.OBF.name, List.of(projectMappingsTree, srgMappingsTree, mojMapTree),
Namespace.MERGED.name, Stream.of(Namespace.NAMED, Namespace.MOJMAP, Namespace.INTERMEDIARY).map(x -> x.name).toList());;
map = new LazyMap<>(
namespace ->
tree
.getClasses()
.stream()
.collect(Collectors.toMap(classMapping -> classMapping.getName(namespace.name), x -> x)));
}

public String mapSignature(String signature, Namespace from, Namespace to) {
var builder = new StringBuilder();

for (int i = 0; i < signature.length(); i++) {
var c = signature.charAt(i);
builder.append(c);

if (c == 'L') {
for (int j = ++i; j < signature.length(); j++) {
if (signature.charAt(j) == ';') {
var fromType = signature.substring(i, j);
var mapping = map.get(from).get(fromType);
if (mapping != null)
builder.append(mapping.getName(to.name));
else
builder.append(fromType);

i = j - 1;
break;
}
}
}
}

return builder.toString();
}

public MappingTree.ClassMapping findClass(String className, Namespace namespace) {
return map.get(namespace).getOrDefault(className, null);
}

public MappingTree.FieldMapping findField(MappingTree.ClassMapping classMapping, String fieldName, String fieldDesc, Namespace namespace) {
return classMapping.getField(fieldName, fieldDesc, tree.getNamespaceId(namespace.name));
}

public MappingTree.FieldMapping findField(MappingTree.ClassMapping classMapping, String fieldName, Namespace namespace) {
return classMapping
.getFields()
.stream()
.filter(x ->
x.getName(namespace.name).equals(fieldName))
.findFirst()
.orElse(null);
}

public MappingTree.MethodMapping findMethod(MappingTree.ClassMapping classMapping, String methodName, String methodDesc, Namespace namespace) {
return classMapping.getMethod(methodName, methodDesc, tree.getNamespaceId(namespace.name));
}

}
Loading

0 comments on commit 3fc77c2

Please sign in to comment.