Skip to content

Commit

Permalink
work
Browse files Browse the repository at this point in the history
  • Loading branch information
ix0rai committed Dec 8, 2024
1 parent eca067b commit c90b8a7
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 34 deletions.
2 changes: 2 additions & 0 deletions src/main/java/org/quiltmc/enigma_plugin/Arguments.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ public class Arguments {
public static final String DISABLE_MAP_NON_HASHED = "disable_map_non_hashed";
public static final String DISABLE_DELEGATE_PARAMS = "disable_delegate_params";
public static final String DISABLE_CONFLICT_FIXER = "disable_conflict_fixer";
public static final String DISABLE_MOJMAP = "disable_mojmap";
public static final String CUSTOM_CODECS = "custom_codecs";
public static final String SIMPLE_TYPE_FIELD_NAMES_PATH = "simple_type_field_names_path";
public static final String MOJMAP_PATH = "mojmap_path";

public static <T extends EnigmaService> boolean getBoolean(EnigmaServiceContext<T> context, String arg) {
return getBoolean(context, arg, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,22 @@
import org.quiltmc.enigma.api.service.ObfuscationTestService;
import org.quiltmc.enigma_plugin.index.JarIndexer;
import org.quiltmc.enigma_plugin.obfuscation.NameObfuscationTestService;
import org.quiltmc.enigma_plugin.proposal.NameProposerService;
import org.quiltmc.enigma_plugin.proposal.DefaultProposalService;
import org.quiltmc.enigma_plugin.proposal.FallbackProposalService;

public class QuiltEnigmaPlugin implements EnigmaPlugin {
public static final String SERVICE_ID_PREFIX = "quiltmc:";
public static final String INDEX_SERVICE_ID = SERVICE_ID_PREFIX + "jar_index";
public static final String NAME_PROPOSAL_SERVICE_ID = SERVICE_ID_PREFIX + "name_proposal";
public static final String FALLBACK_NAME_PROPOSAL_SERVICE_ID = SERVICE_ID_PREFIX + "name_proposal/fallback";
public static final String OBFUSCATION_SERVICE_ID = SERVICE_ID_PREFIX + "obfuscation_test";

@Override
public void init(EnigmaPluginContext ctx) {
var indexer = new JarIndexer();
ctx.registerService(JarIndexerService.TYPE, indexer::withContext);
ctx.registerService(NameProposalService.TYPE, ctx1 -> new NameProposerService(indexer, ctx1));
ctx.registerService(NameProposalService.TYPE, ctx1 -> new DefaultProposalService(indexer, ctx1));
ctx.registerService(NameProposalService.TYPE, ctx1 -> new FallbackProposalService(indexer, ctx1));
ctx.registerService(ObfuscationTestService.TYPE, NameObfuscationTestService::new);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.quiltmc.enigma_plugin.proposal;

import org.quiltmc.enigma.api.service.EnigmaServiceContext;
import org.quiltmc.enigma.api.service.NameProposalService;
import org.quiltmc.enigma_plugin.Arguments;
import org.quiltmc.enigma_plugin.QuiltEnigmaPlugin;
import org.quiltmc.enigma_plugin.index.JarIndexer;
import org.quiltmc.enigma_plugin.index.simple_type_single.SimpleTypeSingleIndex;

public class DefaultProposalService extends NameProposerService {
public DefaultProposalService(JarIndexer indexer, EnigmaServiceContext<NameProposalService> context) {
super();
this.addIfEnabled(context, indexer, Arguments.DISABLE_RECORDS, RecordComponentNameProposer::new);
this.addIfEnabled(context, indexer, Arguments.DISABLE_CONSTANT_FIELDS, ConstantFieldNameProposer::new);
this.addIfEnabled(context, Arguments.DISABLE_EQUALS, EqualsNameProposer::new);
this.addIfEnabled(context, indexer, Arguments.DISABLE_LOGGER, LoggerNameProposer::new);
this.addIfEnabled(context, indexer, Arguments.DISABLE_CODECS, CodecNameProposer::new);
this.addIfNotDisabled(context, Arguments.DISABLE_MAP_NON_HASHED, MojangNameProposer::new);

if (indexer.getIndex(SimpleTypeSingleIndex.class).isEnabled()) {
this.add(indexer, SimpleTypeFieldNameProposer::new);
}

this.addIfEnabled(context, indexer, Arguments.DISABLE_CONSTRUCTOR_PARAMS, ConstructorParamsNameProposer::new);
this.addIfEnabled(context, indexer, Arguments.DISABLE_GETTER_SETTER, GetterSetterNameProposer::new);
this.addIfEnabled(context, indexer, Arguments.DISABLE_DELEGATE_PARAMS, DelegateParametersNameProposer::new);

// conflict fixer must be last in order to get context from other dynamic proposers
this.addIfEnabled(context, indexer, Arguments.DISABLE_CONFLICT_FIXER, ConflictFixProposer::new);
}

@Override
public String getId() {
return QuiltEnigmaPlugin.NAME_PROPOSAL_SERVICE_ID;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.quiltmc.enigma_plugin.proposal;

import org.quiltmc.enigma.api.service.EnigmaServiceContext;
import org.quiltmc.enigma.api.service.NameProposalService;
import org.quiltmc.enigma_plugin.Arguments;
import org.quiltmc.enigma_plugin.QuiltEnigmaPlugin;
import org.quiltmc.enigma_plugin.index.JarIndexer;

public class FallbackProposalService extends NameProposerService {
public FallbackProposalService(JarIndexer indexer, EnigmaServiceContext<NameProposalService> context) {
super();
this.addIfEnabled(context, Arguments.DISABLE_MOJMAP, () -> new MojmapNameProposer(context.getSingleArgument(Arguments.MOJMAP_PATH)));
}

@Override
public String getId() {
return QuiltEnigmaPlugin.FALLBACK_NAME_PROPOSAL_SERVICE_ID;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.quiltmc.enigma_plugin.proposal;

import org.quiltmc.enigma.api.analysis.index.jar.JarIndex;
import org.quiltmc.enigma.api.translation.mapping.EntryMapping;
import org.quiltmc.enigma.api.translation.mapping.EntryRemapper;
import org.quiltmc.enigma.api.translation.mapping.tree.EntryTree;
import org.quiltmc.enigma.api.translation.representation.entry.Entry;
import org.tinylog.Logger;

import java.util.Map;
import java.util.Optional;

public class MojmapNameProposer extends NameProposer {
public static final String ID = "mojmap";

private final String mojmapPath;
private EntryTree<EntryMapping> mojmaps;

@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
public MojmapNameProposer(Optional<String> path) {
super(ID);

if (path.isPresent()) {
this.mojmapPath = path.get();
} else {
Logger.error("No mojmap path provided, disabling " + this.getSourcePluginId());
this.mojmapPath = null;
}
}

@Override
public void insertProposedNames(JarIndex index, Map<Entry<?>, EntryMapping> mappings) {
// todo read mojmaps
// needs more context (enigma change)
}

@Override
public void proposeDynamicNames(EntryRemapper remapper, Entry<?> obfEntry, EntryMapping oldMapping, EntryMapping newMapping, Map<Entry<?>, EntryMapping> mappings) {
// todo dynamically fix class names to match their moj package
// todo definitions file
}

@Override
public boolean fallback() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ private void insertProposal(Map<Entry<?>, EntryMapping> mappings, Entry<?> entry
}
}

public boolean fallback() {
return false;
}

public boolean hasJarProposal(EntryRemapper remapper, Entry<?> entry) {
return remapper.getJarProposedMappings().contains(entry);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.quiltmc.enigma_plugin.Arguments;
import org.quiltmc.enigma_plugin.QuiltEnigmaPlugin;
import org.quiltmc.enigma_plugin.index.JarIndexer;
import org.quiltmc.enigma_plugin.index.simple_type_single.SimpleTypeSingleIndex;

import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -34,49 +33,33 @@
import java.util.function.Function;
import java.util.function.Supplier;

public class NameProposerService implements NameProposalService {
public abstract class NameProposerService implements NameProposalService {
private final List<NameProposer> nameProposers = new ArrayList<>();

public NameProposerService(JarIndexer indexer, EnigmaServiceContext<NameProposalService> context) {
this.addIfEnabled(context, indexer, Arguments.DISABLE_RECORDS, RecordComponentNameProposer::new);
this.addIfEnabled(context, indexer, Arguments.DISABLE_CONSTANT_FIELDS, ConstantFieldNameProposer::new);
this.addIfEnabled(context, Arguments.DISABLE_EQUALS, EqualsNameProposer::new);
this.addIfEnabled(context, indexer, Arguments.DISABLE_LOGGER, LoggerNameProposer::new);
this.addIfEnabled(context, indexer, Arguments.DISABLE_CODECS, CodecNameProposer::new);
this.addIfNotDisabled(context, Arguments.DISABLE_MAP_NON_HASHED, MojangNameProposer::new);

if (indexer.getIndex(SimpleTypeSingleIndex.class).isEnabled()) {
this.nameProposers.add(new SimpleTypeFieldNameProposer(indexer));
}

this.addIfEnabled(context, indexer, Arguments.DISABLE_CONSTRUCTOR_PARAMS, ConstructorParamsNameProposer::new);
this.addIfEnabled(context, indexer, Arguments.DISABLE_GETTER_SETTER, GetterSetterNameProposer::new);
this.addIfEnabled(context, indexer, Arguments.DISABLE_DELEGATE_PARAMS, DelegateParametersNameProposer::new);

// conflict fixer must be last in order to get context from other dynamic proposers
this.addIfEnabled(context, indexer, Arguments.DISABLE_CONFLICT_FIXER, ConflictFixProposer::new);
}

private void addIfEnabled(EnigmaServiceContext<NameProposalService> context, String name, Supplier<NameProposer> factory) {
protected void addIfEnabled(EnigmaServiceContext<NameProposalService> context, String name, Supplier<NameProposer> factory) {
this.addIfEnabled(context, null, name, indexer -> factory.get());
}

private void addIfEnabled(EnigmaServiceContext<NameProposalService> context, JarIndexer indexer, String name, Function<JarIndexer, NameProposer> factory) {
protected void addIfEnabled(EnigmaServiceContext<NameProposalService> context, JarIndexer indexer, String name, Function<JarIndexer, NameProposer> factory) {
if (!Arguments.getBoolean(context, name)) {
this.nameProposers.add(factory.apply(indexer));
}
}

private void addIfNotDisabled(EnigmaServiceContext<NameProposalService> context, String name, Supplier<NameProposer> factory) {
protected void addIfNotDisabled(EnigmaServiceContext<NameProposalService> context, String name, Supplier<NameProposer> factory) {
this.addIfNotDisabled(context, null, name, indexer -> factory.get());
}

private void addIfNotDisabled(EnigmaServiceContext<NameProposalService> context, JarIndexer indexer, String name, Function<JarIndexer, NameProposer> factory) {
protected void addIfNotDisabled(EnigmaServiceContext<NameProposalService> context, JarIndexer indexer, String name, Function<JarIndexer, NameProposer> factory) {
if (!Arguments.getBoolean(context, name, true)) {
this.nameProposers.add(factory.apply(indexer));
}
}

protected void add(JarIndexer indexer, Function<JarIndexer, NameProposer> factory) {
this.nameProposers.add(factory.apply(indexer));
}

@Override
public Map<Entry<?>, EntryMapping> getProposedNames(JarIndex index) {
HashMap<Entry<?>, EntryMapping> proposedNames = new HashMap<>();
Expand All @@ -98,9 +81,4 @@ public Map<Entry<?>, EntryMapping> getDynamicProposedNames(EntryRemapper remappe

return proposedNames;
}

@Override
public String getId() {
return QuiltEnigmaPlugin.NAME_PROPOSAL_SERVICE_ID;
}
}
5 changes: 4 additions & 1 deletion src/test/java/org/quiltmc/enigma_plugin/MojmapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ void setupEnigma() throws IOException, MappingParseException {
"services": {
"name_proposal": [
{
"id": "quiltmc:mojang_name_proposal"
"id": "quiltmc:mojang_name_proposal",
"args": {
"mojmap_path": "./mojmap_cache/server-mappings.txt"
}
}
]
}
Expand Down

0 comments on commit c90b8a7

Please sign in to comment.