Skip to content

Commit

Permalink
Optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
XiaoPangxie732 committed Aug 2, 2024
1 parent eaf3d31 commit 1d2217e
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,7 @@ public static boolean verify(@NotNull Path path, @NotNull String hash, long size
try (FileChannel fc = FileChannel.open(path, READ)) {
MessageDigest md = MessageDigest.getInstance("SHA-1");
ByteBuffer buf = ByteBuffer.allocateDirect(65536);
while (fc.read(buf) != -1) {
md.update(buf.flip());
buf.clear();
}
while (fc.read(buf.clear()) != -1) md.update(buf.flip());
return (size < 0 || fc.size() == size) && hash.contentEquals(Utils.createHashString(md));
} catch (IOException e) {
LOGGER.fatal("Error reading files", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,19 @@ public class IOUtil {

public static byte[] readAllBytes(@NotNull Path file) throws IOException {
if (ZIP_FILESYSTEM != file.getFileSystem().getClass()) throw new IllegalArgumentException(); // Ensure the filesystem is zipfs
InputStream is = Files.newInputStream(file); // Caller will close this stream
byte[] bytes = new byte[is.available()];
if (is instanceof InflaterInputStream) {
if (bytes.length > 65536) for (int len = 0; len != bytes.length; len += is.read(bytes, len, bytes.length - len));
else is.read(bytes);
return bytes;
try (InputStream is = Files.newInputStream(file)) {
byte[] bytes = new byte[is.available()];
if (is instanceof InflaterInputStream) {
if (bytes.length > 65536) for (int len = 0; len != bytes.length; len += is.read(bytes, len, bytes.length - len));
else is.read(bytes);
return bytes;
}
if (is.getClass() == ENTRY_INPUT_STREAM) {
is.read(bytes);
return bytes;
}
throw new UnsupportedOperationException();
}
if (is.getClass() == ENTRY_INPUT_STREAM) {
is.read(bytes);
return bytes;
}
throw new UnsupportedOperationException();
}

public static BufferedReader asBufferedReader(@NotNull Reader reader) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectCollection;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;
import java.util.Optional;
Expand All @@ -41,7 +40,7 @@ public abstract class Mapping implements NameGetter {
* @param components Components add to this mapping
*/
protected Mapping(@NotNull Component @NotNull ... components) {
for(@NotNull Component component : components) {
for (@NotNull Component component : components) {
this.components.put(component.getClass(), component);
}
}
Expand All @@ -59,7 +58,7 @@ protected Mapping() {}
* @return The component if exists, or {@code null}
*/
@SuppressWarnings("unchecked")
public final <C extends Component> @Nullable C getComponent(@NotNull Class<? extends C> component) {
public final <C extends Component> C getComponent(@NotNull Class<? extends C> component) {
return (C) components.get(component);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ public Deobfuscator<T> deobfuscate(Path source, Path target) throws IOException
boolean extraClassesNotEmpty = !extraClasses.isEmpty();
ExtraClassesInformation info = new ExtraClassesInformation(options.refMap, FileUtil.iterateFiles(fs.getPath(""))
.filter(p -> {
String k = NamingUtil.file2Native(p.toString());
return (deobfAll && p.toString().endsWith(".class")) || remapper.hasClassMapping(k) ||
String ps = p.toString();
String k = NamingUtil.file2Native(ps);
return (deobfAll && ps.endsWith(".class")) || remapper.hasClassMapping(k) ||
(extraClassesNotEmpty && extraClasses.stream().anyMatch(k::startsWith));
}), true);
options.extraJars.forEach(jar -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,20 @@

package cn.maxpixel.mcdecompiler.test;

import cn.maxpixel.mcdecompiler.mapping.NamespacedMapping;
import cn.maxpixel.mcdecompiler.mapping.PairedMapping;
import cn.maxpixel.mcdecompiler.mapping.collection.ClassMapping;
import cn.maxpixel.mcdecompiler.mapping.collection.ClassifiedMapping;
import cn.maxpixel.mcdecompiler.mapping.component.Descriptor;
import cn.maxpixel.mcdecompiler.mapping.format.MappingFormats;
import cn.maxpixel.mcdecompiler.mapping.remapper.ClassifiedMappingRemapper;
import cn.maxpixel.mcdecompiler.mapping.util.MappingUtil;
import cn.maxpixel.rewh.logging.LogManager;
import cn.maxpixel.rewh.logging.Logger;
import org.jetbrains.annotations.NotNull;

import java.io.FileReader;
import java.nio.file.Files;
import java.io.File;
import java.nio.channels.FileChannel;
import java.nio.file.Path;

import static java.nio.file.StandardOpenOption.CREATE;
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;

public class FunctionTest {
private static final Logger LOGGER = LogManager.getLogger();

Expand Down Expand Up @@ -93,38 +88,68 @@ public class FunctionTest {
// }

public static void main(String[] args) throws Throwable {
// ClassifiedMapping<NamespacedMapping> mcpconfig = MappingFormats.TSRG_V2.read(new FileInputStream("downloads/1.19.3/joined.tsrg"));
// ClassifiedMapping<PairedMapping> official = MappingFormats.PROGUARD.read(new FileInputStream("downloads/1.19.3/client_mappings.txt"));
// var mappings = ClassifiedMappingRemapper.genMappingsByUnmappedNameMap(official.classes);
// for (ClassMapping<NamespacedMapping> cm : mcpconfig.classes) {
// NamespacedMapping mapping = cm.mapping;
// mapping.setName("srg", mappings.get(mapping.getName("obf")).mapping.mappedName);
FileChannel.open(Path.of("")).size();
new File("").length();
// ClassifiedMapping<NamespacedMapping> obf2srg = MappingFormats.TSRG_V2.read(new FileInputStream("downloads/1.19.2/joined.tsrg"));
// obf2srg.getTrait(NamespacedTrait.class).setMappedNamespace("srg");
// obf2srg.updateCollection();
// ClassifiedMapping<PairedMapping> official = MappingFormats.PROGUARD.read(new FileInputStream("downloads/1.19.2/client_mappings.txt"));
// var obf2off = new ClassifiedMappingRemapper(official);
// var out = new ClassifiedMapping<PairedMapping>();
// for (ClassMapping<NamespacedMapping> cm : obf2srg.classes) {
// String obfClassName = cm.mapping.getName("obf");
// ClassMapping<PairedMapping> ncm = new ClassMapping<>(new PairedMapping(obfClassName, obf2off.mapClass(obfClassName)));
// for (NamespacedMapping field : cm.getFields()) {
// ncm.addField(MappingUtil.Paired.o(field.getUnmappedName(), field.getMappedName()));
// }
// for (NamespacedMapping method : cm.getMethods()) {
// ncm.addMethod(MappingUtil.Paired.duo(method.getUnmappedName(), method.getMappedName(), method.getComponent(Descriptor.Namespaced.class).unmappedDescriptor));
// }
// out.classes.add(ncm);
// }
// try (var writer = Files.newBufferedWriter(FileUtil.ensureFileExist(Path.of("downloads/1.19.2/obf2srg.tsrg")))) {
// MappingFormats.TSRG_V1.write(out, writer);
// }
// for (ClassMapping<NamespacedMapping> cm : obf2srg.classes) {
// String obfClassName = cm.mapping.getUnmappedName();
// ClassMapping<PairedMapping> ncm = new ClassMapping<>(new PairedMapping(obf2off.mapClass(obfClassName), obf2off.mapClass(obfClassName)));
// for (NamespacedMapping field : cm.getFields()) {
// ncm.addField(MappingUtil.Paired.o(field.getMappedName(), obf2off.mapField(obfClassName, field.getUnmappedName())));
// }
// for (NamespacedMapping method : cm.getMethods()) {
// String obfDesc = method.getComponent(Descriptor.Namespaced.class).unmappedDescriptor;
// ncm.addMethod(MappingUtil.Paired.duo(method.getMappedName(), obf2off.mapMethod(obfClassName, method.getUnmappedName(), obfDesc), obf2off.mapMethodDesc(obfDesc)));
// }
// out.classes.add(ncm);
// }
// MappingFormats.TSRG_V2.write(mcpconfig, Files.newBufferedWriter(FileUtil.ensureFileExist(Path.of("downloads/1.19.3/obf2srg.tsrg"))));
// try (var writer = Files.newBufferedWriter(FileUtil.ensureFileExist(Path.of("downloads/1.19.2/srg2moj.tsrg")))) {
// MappingFormats.TSRG_V1.write(out, writer);
// }

// srg2mcp();
// moj2srg();

ClassifiedMapping<PairedMapping> srcMixin = MappingFormats.TSRG_V1.read(new FileReader("downloads/CSL/mixin.tsrg"));// mcp -> int
ClassifiedMapping<PairedMapping> srcFabric = MappingFormats.TSRG_V1.read(new FileReader("downloads/CSL/Fabric.tsrg"));
ClassifiedMapping<PairedMapping> mMoj2srg = MappingFormats.TSRG_V1.read(new FileReader("output/1.16.5-moj2srg.tsrg"));
ClassifiedMapping<PairedMapping> mObf2srg = MappingFormats.TSRG_V1.read(new FileReader("downloads/1.16.5/old_srg.tsrg"));
ClassifiedMapping<NamespacedMapping> srcIntermediary = MappingFormats.TINY_V1.read(new FileReader("downloads/1.16.5/intermediary.tiny"));
ClassifiedMappingRemapper int2obf = new ClassifiedMappingRemapper(srcIntermediary, "intermediary", true);
ClassifiedMappingRemapper obf2srg = new ClassifiedMappingRemapper(mObf2srg);
ClassifiedMappingRemapper srg2moj = new ClassifiedMappingRemapper(mMoj2srg, true);
ClassifiedMapping<PairedMapping> outMixin = new ClassifiedMapping<>();// moj -> srg
ClassifiedMapping<PairedMapping> outFabric = new ClassifiedMapping<>();
process(srcMixin, int2obf, obf2srg, srg2moj, outMixin, "mixin.tsrg");
process(srcFabric, int2obf, obf2srg, srg2moj, outFabric, "Fabric.tsrg");
try (var osMixin = Files.newOutputStream(Path.of("output/mixin-moj2srg.tsrg"), CREATE, TRUNCATE_EXISTING);
var osFabric = Files.newOutputStream(Path.of("output/Fabric-moj2srg.tsrg"), CREATE, TRUNCATE_EXISTING)) {
MappingFormats.TSRG_V1.write(outMixin, osMixin);
MappingFormats.TSRG_V1.write(outFabric, osFabric);
}
try (var in = Files.newBufferedReader(Path.of("in"));
var out = Files.newBufferedWriter(Path.of("out"))) {
MappingFormats.SRG.write(MappingFormats.TSRG_V1.read(in), out);
}
// ClassifiedMapping<PairedMapping> srcMixin = MappingFormats.TSRG_V1.read(new FileReader("downloads/CSL/mixin.tsrg"));// mcp -> int
// ClassifiedMapping<PairedMapping> srcFabric = MappingFormats.TSRG_V1.read(new FileReader("downloads/CSL/Fabric.tsrg"));
// ClassifiedMapping<PairedMapping> mMoj2srg = MappingFormats.TSRG_V1.read(new FileReader("output/1.16.5-moj2srg.tsrg"));
// ClassifiedMapping<PairedMapping> mObf2srg = MappingFormats.TSRG_V1.read(new FileReader("downloads/1.16.5/old_srg.tsrg"));
// ClassifiedMapping<NamespacedMapping> srcIntermediary = MappingFormats.TINY_V1.read(new FileReader("downloads/1.16.5/intermediary.tiny"));
// ClassifiedMappingRemapper int2obf = new ClassifiedMappingRemapper(srcIntermediary, "intermediary", true);
// ClassifiedMappingRemapper obf2srg = new ClassifiedMappingRemapper(mObf2srg);
// ClassifiedMappingRemapper srg2moj = new ClassifiedMappingRemapper(mMoj2srg, true);
// ClassifiedMapping<PairedMapping> outMixin = new ClassifiedMapping<>();// moj -> srg
// ClassifiedMapping<PairedMapping> outFabric = new ClassifiedMapping<>();
// process(srcMixin, int2obf, obf2srg, srg2moj, outMixin, "mixin.tsrg");
// process(srcFabric, int2obf, obf2srg, srg2moj, outFabric, "Fabric.tsrg");
// try (var osMixin = Files.newOutputStream(Path.of("output/mixin-moj2srg.tsrg"), CREATE, TRUNCATE_EXISTING);
// var osFabric = Files.newOutputStream(Path.of("output/Fabric-moj2srg.tsrg"), CREATE, TRUNCATE_EXISTING)) {
// MappingFormats.TSRG_V1.write(outMixin, osMixin);
// MappingFormats.TSRG_V1.write(outFabric, osFabric);
// }
// try (var in = Files.newBufferedReader(Path.of("in"));
// var out = Files.newBufferedWriter(Path.of("out"))) {
// MappingFormats.SRG.write(MappingFormats.TSRG_V1.read(in), out);
// }
}

private static void process(ClassifiedMapping<PairedMapping> src, ClassifiedMappingRemapper int2obf, ClassifiedMappingRemapper obf2srg,
Expand Down

0 comments on commit 1d2217e

Please sign in to comment.