Skip to content

Commit

Permalink
Arg relationship adjustment
Browse files Browse the repository at this point in the history
Mapping, Component, PairedMapping tests
PairedMapping validation
  • Loading branch information
XiaoPangxie732 committed Feb 3, 2025
1 parent 815a1ab commit 816dd48
Show file tree
Hide file tree
Showing 11 changed files with 322 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ public static void main(String[] args) throws Throwable {
"decompile. Values are \"CLIENT\" and \"SERVER\". With this option, you must specify --version option.")
.withRequiredArg().ofType(SideType.class).defaultsTo(SideType.CLIENT);
ArgumentAcceptingOptionSpec<Path> inputO = parser.acceptsAll(of("i", "input"), "Input jar.")
.requiredUnless(sideTypeO).withRequiredArg().withValuesConvertedBy(new PathConverter(PathProperties.FILE_EXISTING));
.withRequiredArg().withValuesConvertedBy(new PathConverter(PathProperties.FILE_EXISTING));
ArgumentAcceptingOptionSpec<String> mappingPathO = parser.acceptsAll(of("m", "map", "mapping-path"), "Mapping file that " +
"is used to deobfuscate.").requiredUnless(sideTypeO).withRequiredArg();
"is used to deobfuscate.").withRequiredArg();
ArgumentAcceptingOptionSpec<String> versionO = parser.acceptsAll(of("v", "ver", "version"), "Version to " +
"deobfuscate/decompile. Only works on Proguard mappings or downloading libraries for the decompiler.")
.requiredUnless(inputO, mappingPathO).requiredIf(sideTypeO).withRequiredArg();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* @implNote This class should only be extended, so it is abstract
*/
public abstract class Mapping implements NameGetter {
private final Object2ObjectOpenHashMap<Class<? extends Component>, Component> components = new Object2ObjectOpenHashMap<>();
protected final Object2ObjectOpenHashMap<Class<? extends Component>, Component> components = new Object2ObjectOpenHashMap<>();

/**
* Constructor
Expand Down Expand Up @@ -70,7 +70,7 @@ public <C extends Component> C getComponent(@NotNull Class<? extends C> componen
* @return The component if exists, or the newly created component
*/
@SuppressWarnings("unchecked")
public <C extends Component> @NotNull C getOrCreateComponent(@NotNull Class<? extends C> component, Supplier<? extends C> factory) {
public <C extends Component> @NotNull C getOrCreateComponent(@NotNull Class<? extends C> component, @NotNull Supplier<? extends C> factory) {
var value = components.get(component);
if (value == null) {
value = Objects.requireNonNull(factory.get());
Expand Down Expand Up @@ -138,11 +138,11 @@ public void removeComponent(@NotNull Class<? extends Component> component) {
}

/**
* Validates all the components of this mapping
* Validates this mapping
*
* @throws IllegalStateException If any of the component fails validation
*/
public void validateComponents() throws IllegalStateException {
public void validate() throws IllegalStateException {
for (Component value : components.values()) {
value.validate();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.jetbrains.annotations.NotNull;

/**
* Intended to be a universal interface for getting names
* Intends to be a universal interface for getting names
*/
public interface NameGetter {
String getUnmappedName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import cn.maxpixel.mcdecompiler.common.util.Utils;
import cn.maxpixel.mcdecompiler.mapping.component.Component;
import cn.maxpixel.mcdecompiler.mapping.component.LocalVariableTable;
import cn.maxpixel.mcdecompiler.mapping.component.Owned;
import cn.maxpixel.mcdecompiler.mapping.util.DescriptorRemapper;
import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap;
Expand Down Expand Up @@ -293,8 +292,9 @@ public NamespacedMapping setUnmappedNamespace(@NotNull String namespace) {
@Override
public void setMappedNamespace(@NotNull String namespace) {
this.mappedNamespace = Objects.requireNonNull(namespace);
LocalVariableTable.Namespaced n = getComponent(LocalVariableTable.Namespaced.class);
if (n != null) n.setMappedNamespace(namespace);
for (Component component : getComponents()) {
if (component instanceof NameGetter.Namespace n) n.setMappedNamespace(namespace);
}
}

@Override
Expand All @@ -305,8 +305,9 @@ public String getFallbackNamespace() {
@Override
public void setFallbackNamespace(@NotNull String namespace) {
this.fallbackNamespace = Objects.requireNonNull(namespace);
LocalVariableTable.Namespaced n = getComponent(LocalVariableTable.Namespaced.class);
if (n != null) n.setFallbackNamespace(namespace);
for (Component component : getComponents()) {
if (component instanceof NameGetter.Namespace n) n.setFallbackNamespace(namespace);
}
}

/* Auto-generated equals, hashCode and toString methods */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import cn.maxpixel.mcdecompiler.mapping.component.Component;
import cn.maxpixel.mcdecompiler.mapping.component.Owned;
import cn.maxpixel.mcdecompiler.mapping.util.Validation;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;

Expand Down Expand Up @@ -115,23 +116,6 @@ public PairedMapping reverse() {
String temp = unmappedName;
unmappedName = mappedName;
mappedName = temp;
// boolean supportDesc = hasComponent(Descriptor.Unmapped.class);// TODO: abstract as a general-purpose interface
// boolean supportDescMapped = hasComponent(Descriptor.Mapped.class);
// if (supportDesc) {
// Descriptor.Unmapped unmapped = getComponent(Descriptor.Unmapped.class);
// if (supportDescMapped) {
// Descriptor.Mapped mapped = getComponent(Descriptor.Mapped.class);
// String desc = unmapped.descriptor;
// unmapped.descriptor = mapped.descriptor;
// mapped.descriptor = desc;
// } else {
// addComponent(new Descriptor.Mapped(unmapped.descriptor));
// removeComponent(Descriptor.Unmapped.class);
// }
// } else if (supportDescMapped) {
// addComponent(new Descriptor.Unmapped(getComponent(Descriptor.Mapped.class).descriptor));
// removeComponent(Descriptor.Mapped.class);
// }// TODO: Remove this after passing the tests
ObjectOpenHashSet<Class<? extends Component>> skipped = new ObjectOpenHashSet<>();
Object2ObjectOpenHashMap<Class<? extends Component>, Component> toAdd = new Object2ObjectOpenHashMap<>();
var it = getComponents().iterator();
Expand All @@ -152,9 +136,7 @@ public PairedMapping reverse() {
}
}
}
for (Component value : toAdd.values()) {
addComponent(value);
}
components.putAll(toAdd);
return this;
}

Expand All @@ -176,6 +158,13 @@ public void setMappedName(String mappedName) {
this.mappedName = mappedName;
}

@Override
public void validate() throws IllegalStateException {
Validation.requireNonNull(unmappedName, "unmappedName");
Validation.requireNonNull(mappedName, "mappedName");
super.validate();
}

/* Auto-generated equals, hashCode and toString methods */
@Override
public boolean equals(Object o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import cn.maxpixel.mcdecompiler.common.Constants;
import cn.maxpixel.mcdecompiler.common.annotation.MethodOrFieldDesc;
import cn.maxpixel.mcdecompiler.mapping.util.DescriptorRemapper;
import cn.maxpixel.mcdecompiler.mapping.util.Validation;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;
Expand Down Expand Up @@ -50,7 +51,7 @@ public void setDescriptor(@NotNull @MethodOrFieldDesc String desc) {

@Override
public void validate() throws IllegalStateException {
if (descriptor == null) throw new IllegalStateException("Descriptor must not be null");
Validation.requireNonNull(descriptor, "descriptor");
if (!MATCHERS.get().reset(descriptor).matches()) {
throw new IllegalStateException("Invalid descriptor: " + descriptor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import cn.maxpixel.mcdecompiler.mapping.NamespacedMapping;
import cn.maxpixel.mcdecompiler.mapping.PairedMapping;
import cn.maxpixel.mcdecompiler.mapping.util.DescriptorRemapper;
import cn.maxpixel.mcdecompiler.mapping.util.Validation;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.ints.IntSet;
import org.jetbrains.annotations.NotNull;
Expand All @@ -39,7 +40,7 @@
* will be treated as the actual lvt index - 1, which means that index 0 in {@link LocalVariableTable} represents
* the index 1 in the actual lvt(omitting {@code this}).
*/
public abstract class LocalVariableTable<T extends Mapping> {
public abstract class LocalVariableTable<T extends Mapping> implements Component {
protected final @NotNull Int2ObjectOpenHashMap<T> lvt = new Int2ObjectOpenHashMap<>();

public T getLocalVariable(@Range(from = 0, to = 255) int index) {
Expand All @@ -62,6 +63,14 @@ public boolean isEmpty() {
return lvt.isEmpty();
}

@Override
public void validate() throws IllegalStateException {
lvt.int2ObjectEntrySet().fastForEach(entry -> {
if (entry.getIntKey() < 0 || entry.getIntKey() > 255) throw new IllegalStateException("Illegal LVT index");
entry.getValue().validate();
});
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand All @@ -82,14 +91,6 @@ public String toString() {
}

public static class Paired extends LocalVariableTable<PairedMapping> implements Component, Component.Reversible {
@Override
public void validate() throws IllegalStateException {
lvt.int2ObjectEntrySet().fastForEach(entry -> {
if (entry.getIntKey() < 0 || entry.getIntKey() > 255) throw new IllegalStateException("Illegal LVT index");
entry.getValue().validateComponents();
});
}

@Override
public void reverse() {
lvt.values().forEach(PairedMapping::reverse);
Expand Down Expand Up @@ -157,12 +158,8 @@ public void setFallbackNamespace(@NotNull String namespace) {

@Override
public void validate() throws IllegalStateException {
if (unmappedNamespace == null) throw new IllegalStateException();
lvt.int2ObjectEntrySet().fastForEach(entry -> {
if (entry.getIntKey() < 0 || entry.getIntKey() > 255)
throw new IllegalStateException("Illegal LVT index");
entry.getValue().validateComponents();
});
Validation.requireNonNull(unmappedNamespace, "unmappedNamespace");
super.validate();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import cn.maxpixel.mcdecompiler.mapping.Mapping;
import cn.maxpixel.mcdecompiler.mapping.collection.ClassMapping;
import cn.maxpixel.mcdecompiler.mapping.util.Validation;

import java.util.Objects;

Expand All @@ -42,7 +43,7 @@ public void setOwner(ClassMapping<T> owner) {

@Override
public void validate() throws IllegalStateException {
if (owner == null) throw new IllegalStateException("Owner is null");
Validation.requireNonNull(owner, "owner");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* MinecraftDecompiler. A tool/library to deobfuscate and decompile jars.
* Copyright (C) 2019-2025 MaxPixelStudios(XiaoPangxie732)
*
* 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 <https://www.gnu.org/licenses/>.
*/

package cn.maxpixel.mcdecompiler.mapping.util;

import java.util.Objects;

public class Validation {
public static void requireNonNull(Object o, String name) {
if (o == null) throw new IllegalStateException(Objects.requireNonNull(name) + " cannot be null");
}
}
Loading

0 comments on commit 816dd48

Please sign in to comment.