Skip to content

Commit

Permalink
add enigma instance as name proposal context
Browse files Browse the repository at this point in the history
  • Loading branch information
ix0rai committed Dec 8, 2024
1 parent a4b6965 commit 18cfe7b
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public static void run(Path inJar, Path source, Path output, Enigma enigma, @Nul
@SuppressWarnings("unused")
public static EntryTree<EntryMapping> exec(NameProposalService[] nameProposalServices, EnigmaProject project) {
for (NameProposalService service : nameProposalServices) {
Map<Entry<?>, EntryMapping> jarMappings = service.getProposedNames(project.getJarIndex());
Map<Entry<?>, EntryMapping> jarMappings = service.getProposedNames(project.getEnigma(), project.getJarIndex());
Map<Entry<?>, EntryMapping> dynamicMappings = service.getDynamicProposedNames(project.getRemapper(), null, null, null);

insertMappings(jarMappings, project);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ public static void main(String[] args) {

EntryRemapper mappings;
if (!Files.exists(mappingsFile)) {
mappings = EntryRemapper.mapped(project.getJarIndex(), project.getMappingsIndex(), project.getRemapper().getJarProposedMappings(), new HashEntryTree<>(), enigma.getNameProposalServices());
mappings = EntryRemapper.mapped(project.getEnigma(), project.getJarIndex(), project.getMappingsIndex(), project.getRemapper().getJarProposedMappings(), new HashEntryTree<>(), enigma.getNameProposalServices());
} else {
Logger.info("Reading mappings...");
mappings = EntryRemapper.mapped(project.getJarIndex(), project.getMappingsIndex(), project.getRemapper().getJarProposedMappings(), readWriteService.get().read(mappingsFile), enigma.getNameProposalServices());
mappings = EntryRemapper.mapped(project.getEnigma(), project.getJarIndex(), project.getMappingsIndex(), project.getRemapper().getJarProposedMappings(), readWriteService.get().read(mappingsFile), enigma.getNameProposalServices());
}

PrintWriter log = new PrintWriter(Files.newBufferedWriter(logFile));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ public void createClient(String username, String ip, int port, char[] password)
}

public void createServer(String username, int port, char[] password) throws IOException {
this.server = new IntegratedEnigmaServer(this.project.getJarChecksum(), password, EntryRemapper.mapped(this.project.getJarIndex(), this.project.getMappingsIndex(), new HashEntryTree<>(this.project.getRemapper().getJarProposedMappings()), new HashEntryTree<>(this.project.getRemapper().getDeobfMappings()), this.project.getEnigma().getNameProposalServices()), port);
this.server = new IntegratedEnigmaServer(this.project.getJarChecksum(), password, EntryRemapper.mapped(this.project.getEnigma(), this.project.getJarIndex(), this.project.getMappingsIndex(), new HashEntryTree<>(this.project.getRemapper().getJarProposedMappings()), new HashEntryTree<>(this.project.getRemapper().getDeobfMappings()), this.project.getEnigma().getNameProposalServices()), port);
this.server.start();
this.client = new IntegratedEnigmaClient(this, "127.0.0.1", port);
this.client.connect();
Expand Down
2 changes: 1 addition & 1 deletion enigma/src/main/java/org/quiltmc/enigma/api/Enigma.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public EnigmaProject openJar(Path path, ClassProvider libraryClassProvider, Prog
int j = 1;
for (var service : nameProposalServices) {
progress.step(j++, I18n.translateFormatted("progress.jar.name_proposal.proposer", service.getId()));
Map<Entry<?>, EntryMapping> proposed = service.getProposedNames(index);
Map<Entry<?>, EntryMapping> proposed = service.getProposedNames(this, index);

if (proposed != null) {
for (var entry : proposed.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public EnigmaProject(Enigma enigma, Path jarPath, ClassProvider classProvider, J
this.jarChecksum = jarChecksum;

this.mappingsIndex = mappingsIndex;
this.remapper = EntryRemapper.mapped(jarIndex, this.mappingsIndex, proposedNames, new HashEntryTree<>(), this.enigma.getNameProposalServices());
this.remapper = EntryRemapper.mapped(enigma, jarIndex, this.mappingsIndex, proposedNames, new HashEntryTree<>(), this.enigma.getNameProposalServices());
}

/**
Expand All @@ -91,12 +91,12 @@ public void setMappings(@Nullable EntryTree<EntryMapping> mappings, ProgressList
EntryTree<EntryMapping> mergedTree = EntryTreeUtil.merge(jarProposedMappings, mappings);

this.mappingsIndex.indexMappings(mergedTree, progress);
this.remapper = EntryRemapper.mapped(this.jarIndex, this.mappingsIndex, jarProposedMappings, mappings, this.enigma.getNameProposalServices());
this.remapper = EntryRemapper.mapped(this.enigma, this.jarIndex, this.mappingsIndex, jarProposedMappings, mappings, this.enigma.getNameProposalServices());
} else if (!jarProposedMappings.isEmpty()) {
this.mappingsIndex.indexMappings(jarProposedMappings, progress);
this.remapper = EntryRemapper.mapped(this.jarIndex, this.mappingsIndex, jarProposedMappings, new HashEntryTree<>(), this.enigma.getNameProposalServices());
this.remapper = EntryRemapper.mapped(this.enigma, this.jarIndex, this.mappingsIndex, jarProposedMappings, new HashEntryTree<>(), this.enigma.getNameProposalServices());
} else {
this.remapper = EntryRemapper.empty(this.jarIndex, this.enigma.getNameProposalServices());
this.remapper = EntryRemapper.empty(this.enigma, this.jarIndex, this.enigma.getNameProposalServices());
}

// update dynamically proposed names
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.quiltmc.enigma.api.service;

import org.quiltmc.enigma.api.Enigma;
import org.quiltmc.enigma.api.analysis.index.jar.JarIndex;
import org.quiltmc.enigma.api.source.TokenType;
import org.quiltmc.enigma.api.translation.mapping.EntryMapping;
Expand All @@ -21,11 +22,12 @@ public interface NameProposalService extends EnigmaService {
* Runs when a new JAR file is opened. Note that at this point, no mapping context will exist in the remapper.
* All mappings proposed should have a token type of {@link TokenType#JAR_PROPOSED} and a non-null source plugin ID.
*
* @param enigma an enigma instance to use as context
* @param index an index of the jar, to use as context
* @return a map of obfuscated entries to their proposed names
*/
@Nullable
Map<Entry<?>, EntryMapping> getProposedNames(JarIndex index);
Map<Entry<?>, EntryMapping> getProposedNames(Enigma enigma, JarIndex index);

/**
* Runs when an entry is renamed, for updating proposed names that use other mappings as context.
Expand All @@ -49,7 +51,7 @@ default boolean isFallback() {
/**
* Disables validation of proposed mappings from this service.
* This allows you to return any kind of mapping you want from {@link #getDynamicProposedNames(EntryRemapper, Entry, EntryMapping, EntryMapping)}
* and {@link #getProposedNames(JarIndex)}, but should be used sparingly as it will allow creating mappings that can't be linked back to this proposer.
* and {@link #getProposedNames(Enigma, JarIndex)}, but should be used sparingly as it will allow creating mappings that can't be linked back to this proposer.
* Do not use this unless you're sure there's no other way to accomplish what you're looking to do!
*
* @return whether validation should be bypassed
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.quiltmc.enigma.api.translation.mapping;

import org.quiltmc.enigma.api.Enigma;
import org.quiltmc.enigma.api.analysis.index.jar.InheritanceIndex;
import org.quiltmc.enigma.api.analysis.index.jar.JarIndex;
import org.quiltmc.enigma.api.analysis.index.mapping.MappingsIndex;
Expand Down Expand Up @@ -33,6 +34,7 @@ public class EntryRemapper {
private final EntryTree<EntryMapping> proposedMappings;
private final DeltaTrackingTree<EntryMapping> mappings;

private final Enigma enigma;
private final EntryResolver obfResolver;
private final Translator deobfuscator;
private final JarIndex jarIndex;
Expand All @@ -41,13 +43,14 @@ public class EntryRemapper {
private final MappingValidator validator;
private final List<NameProposalService> proposalServices;

private EntryRemapper(JarIndex jarIndex, MappingsIndex mappingsIndex, EntryTree<EntryMapping> jarProposedMappings, EntryTree<EntryMapping> deobfMappings, List<NameProposalService> proposalServices) {
private EntryRemapper(Enigma enigma, JarIndex jarIndex, MappingsIndex mappingsIndex, EntryTree<EntryMapping> jarProposedMappings, EntryTree<EntryMapping> deobfMappings, List<NameProposalService> proposalServices) {
this.deobfMappings = deobfMappings;
this.jarProposedMappings = jarProposedMappings;
this.proposedMappings = new HashEntryTree<>(jarProposedMappings);
this.mappings = new DeltaTrackingTree<>(new MergedEntryMappingTree(deobfMappings, this.proposedMappings));

this.obfResolver = jarIndex.getEntryResolver();
this.enigma = enigma;

this.deobfuscator = new MappingTranslator(this.mappings, this.obfResolver);
this.jarIndex = jarIndex;
Expand All @@ -57,12 +60,12 @@ private EntryRemapper(JarIndex jarIndex, MappingsIndex mappingsIndex, EntryTree<
this.proposalServices = proposalServices;
}

public static EntryRemapper mapped(JarIndex jarIndex, MappingsIndex mappingsIndex, EntryTree<EntryMapping> proposedMappings, EntryTree<EntryMapping> deobfMappings, List<NameProposalService> proposalServices) {
return new EntryRemapper(jarIndex, mappingsIndex, proposedMappings, deobfMappings, proposalServices);
public static EntryRemapper mapped(Enigma enigma, JarIndex jarIndex, MappingsIndex mappingsIndex, EntryTree<EntryMapping> proposedMappings, EntryTree<EntryMapping> deobfMappings, List<NameProposalService> proposalServices) {
return new EntryRemapper(enigma, jarIndex, mappingsIndex, proposedMappings, deobfMappings, proposalServices);
}

public static EntryRemapper empty(JarIndex index, List<NameProposalService> proposalServices) {
return new EntryRemapper(index, MappingsIndex.empty(), new HashEntryTree<>(), new HashEntryTree<>(), proposalServices);
public static EntryRemapper empty(Enigma enigma, JarIndex index, List<NameProposalService> proposalServices) {
return new EntryRemapper(enigma, index, MappingsIndex.empty(), new HashEntryTree<>(), new HashEntryTree<>(), proposalServices);
}

public void validatePutMapping(ValidationContext vc, Entry<?> obfuscatedEntry, @Nonnull EntryMapping deobfMapping) {
Expand Down Expand Up @@ -165,6 +168,13 @@ public Translator getDeobfuscator() {
return this.deobfuscator;
}

/**
* {@return the enigma instance used to create this remapper}
*/
public Enigma getEnigma() {
return this.enigma;
}

public Stream<Entry<?>> getObfEntries() {
return this.mappings.getAllEntries();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.quiltmc.enigma.impl.plugin;

import org.quiltmc.enigma.api.Enigma;
import org.quiltmc.enigma.api.analysis.index.jar.BridgeMethodIndex;
import org.quiltmc.enigma.api.EnigmaPlugin;
import org.quiltmc.enigma.api.EnigmaPluginContext;
Expand Down Expand Up @@ -38,7 +39,7 @@ private static void registerEnumNamingService(EnigmaPluginContext ctx) {

ctx.registerService(NameProposalService.TYPE, ctx1 -> new NameProposalService() {
@Override
public Map<Entry<?>, EntryMapping> getProposedNames(JarIndex index) {
public Map<Entry<?>, EntryMapping> getProposedNames(Enigma enigma, JarIndex index) {
Map<Entry<?>, EntryMapping> mappings = new HashMap<>();

index.getIndex(EntryIndex.class).getFields().forEach(field -> {
Expand Down Expand Up @@ -73,7 +74,7 @@ private static void registerRecordNamingService(EnigmaPluginContext ctx) {
private static void registerSpecializedMethodNamingService(EnigmaPluginContext ctx) {
ctx.registerService(NameProposalService.TYPE, ctx1 -> new NameProposalService() {
@Override
public Map<Entry<?>, EntryMapping> getProposedNames(JarIndex index) {
public Map<Entry<?>, EntryMapping> getProposedNames(Enigma enigma, JarIndex index) {
BridgeMethodIndex bridgeMethodIndex = index.getIndex(BridgeMethodIndex.class);
Map<Entry<?>, EntryMapping> mappings = new HashMap<>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.quiltmc.enigma.impl.plugin;

import org.quiltmc.enigma.api.Enigma;
import org.quiltmc.enigma.api.analysis.index.jar.EntryIndex;
import org.quiltmc.enigma.api.analysis.index.jar.JarIndex;
import org.quiltmc.enigma.api.service.NameProposalService;
Expand All @@ -20,7 +21,7 @@
public record RecordComponentProposalService(Map<FieldEntry, MethodEntry> fieldToGetter) implements NameProposalService {
@Nullable
@Override
public Map<Entry<?>, EntryMapping> getProposedNames(JarIndex index) {
public Map<Entry<?>, EntryMapping> getProposedNames(Enigma enigma, JarIndex index) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public void init(EnigmaPluginContext ctx) {

private static class TestFieldProposerNoFallback implements NameProposalService {
@Override
public Map<Entry<?>, EntryMapping> getProposedNames(JarIndex index) {
public Map<Entry<?>, EntryMapping> getProposedNames(Enigma enigma, JarIndex index) {
Map<Entry<?>, EntryMapping> mappings = new HashMap<>();
AtomicInteger i = new AtomicInteger();

Expand All @@ -142,7 +142,7 @@ public String getId() {

private static class TestMethodProposerWithFallback implements NameProposalService {
@Override
public Map<Entry<?>, EntryMapping> getProposedNames(JarIndex index) {
public Map<Entry<?>, EntryMapping> getProposedNames(Enigma enigma, JarIndex index) {
Map<Entry<?>, EntryMapping> mappings = new HashMap<>();
AtomicInteger i = new AtomicInteger();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ private static void nameAllFields(EnigmaPluginContext ctx, String prefix) {

private record TestJarNameProposer(String prefix, String id) implements NameProposalService {
@Override
public Map<Entry<?>, EntryMapping> getProposedNames(JarIndex index) {
public Map<Entry<?>, EntryMapping> getProposedNames(Enigma enigma, JarIndex index) {
Map<Entry<?>, EntryMapping> mappings = new HashMap<>();
AtomicInteger i = new AtomicInteger();

Expand All @@ -154,7 +154,7 @@ public String getId() {

private record TestDynamicNameProposer(String id) implements NameProposalService {
@Override
public Map<Entry<?>, EntryMapping> getProposedNames(JarIndex index) {
public Map<Entry<?>, EntryMapping> getProposedNames(Enigma enigma, JarIndex index) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public void init(EnigmaPluginContext ctx) {

private static class TestFieldProposerNormal implements NameProposalService {
@Override
public Map<Entry<?>, EntryMapping> getProposedNames(JarIndex index) {
public Map<Entry<?>, EntryMapping> getProposedNames(Enigma enigma, JarIndex index) {
Map<Entry<?>, EntryMapping> mappings = new HashMap<>();
AtomicInteger i = new AtomicInteger();

Expand All @@ -149,7 +149,7 @@ public String getId() {

private static class TestMethodProposerWithBypass implements NameProposalService {
@Override
public Map<Entry<?>, EntryMapping> getProposedNames(JarIndex index) {
public Map<Entry<?>, EntryMapping> getProposedNames(Enigma enigma, JarIndex index) {
Map<Entry<?>, EntryMapping> mappings = new HashMap<>();
AtomicInteger i = new AtomicInteger();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.quiltmc.enigma.test.plugin;

import org.quiltmc.enigma.api.Enigma;
import org.quiltmc.enigma.api.EnigmaPlugin;
import org.quiltmc.enigma.api.EnigmaPluginContext;
import org.quiltmc.enigma.api.analysis.index.jar.EntryIndex;
Expand Down Expand Up @@ -36,7 +37,7 @@ public String getId() {
}

@Override
public Map<Entry<?>, EntryMapping> getProposedNames(JarIndex index) {
public Map<Entry<?>, EntryMapping> getProposedNames(Enigma enigma, JarIndex index) {
EntryIndex entryIndex = index.getIndex(EntryIndex.class);
Map<Entry<?>, EntryMapping> names = new HashMap<>();

Expand Down Expand Up @@ -71,7 +72,7 @@ public String getId() {
}

@Override
public Map<Entry<?>, EntryMapping> getProposedNames(JarIndex index) {
public Map<Entry<?>, EntryMapping> getProposedNames(Enigma enigma, JarIndex index) {
EntryIndex entryIndex = index.getIndex(EntryIndex.class);
Map<Entry<?>, EntryMapping> names = new HashMap<>();
for (var method : entryIndex.getMethods()) {
Expand Down

0 comments on commit 18cfe7b

Please sign in to comment.