Skip to content

Commit

Permalink
Only minimize patches if --minimize is passed on the command-line
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaming32 committed Nov 4, 2024
1 parent 3772884 commit c80cd2e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public static void main(String[] args) throws IOException {
OptionSpec<String> prefixO = parser.accepts("prefix").withRequiredArg();
OptionSpec<Void> packO = parser.accepts("pack200");
OptionSpec<Void> legacyO = parser.accepts("legacy", "Uses the legacy patch header format, also implies --pack200. NOT RECOMENDED.");
OptionSpec<Void> minimizeO = parser.accepts("minimize");

// Create arguments
OptionSpec<File> createO = parser.acceptsAll(Arrays.asList("dirty", "create")).withRequiredArg().ofType(File.class);
Expand All @@ -44,6 +45,7 @@ public static void main(String[] args) throws IOException {
File output = options.valueOf(outputO).getAbsoluteFile();
boolean legacy = options.has(legacyO);
boolean pack200 = legacy || options.has(packO);
boolean minimizePatches = options.has(minimizeO);

if (output.exists() && !output.delete())
err("Could not delete output file: " + output);
Expand All @@ -66,8 +68,9 @@ public static void main(String[] args) throws IOException {
log(" Output: " + output);
log(" Pack200: " + pack200);
log(" Legacy: " + legacy);
log(" Minimize patches: " + minimizePatches);

Generator gen = new Generator(output).pack200(pack200).legacy(legacy);
Generator gen = new Generator(output).pack200(pack200).legacy(legacy).minimizePatches(minimizePatches);

if (clean.size() > 1 || dirty.size() > 1 || prefixes.size() > 1) {
if (clean.size() != dirty.size() || dirty.size() != prefixes.size()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class Generator {
private final List<PatchSet> sets = new ArrayList<>();
private boolean pack200 = false;
private boolean legacy = false;
private boolean minimizePatches = false;

public Generator(File output) {
this.output = output;
Expand Down Expand Up @@ -85,6 +86,15 @@ public Generator legacy(boolean value) {
return this;
}

public Generator minimizePatches() {
return this.minimizePatches(true);
}

public Generator minimizePatches(boolean value) {
this.minimizePatches = value;
return this;
}

public void loadMappings(File srg) throws IOException {
IMappingFile map = IMappingFile.load(srg);
map.getClasses().forEach(cls -> {
Expand Down Expand Up @@ -250,7 +260,7 @@ private byte[] process(String obf, String srg, byte[] clean, byte[] dirty) throw
else
log(" Processing " + srg + "(" + obf + ")");

Patch patch = Patch.from(obf, srg, clean, dirty);
Patch patch = Patch.from(obf, srg, clean, dirty, this.minimizePatches);
log(" Clean: " + Integer.toHexString(patch.checksum(clean)) + " Dirty: " + Integer.toHexString(patch.checksum(dirty)));
return patch.toBytes(this.legacy);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ private Patch(String obf, String srg, boolean exists, int checksum, byte[] data)
this.data = data;
}

public static Patch from(String obf, String srg, byte[] clean, byte[] dirty) throws IOException {
byte[] diff = dirty.length == 0 ? EMPTY_DATA : DELTA.compute(clean, shrinkDirtyForPatch(clean, dirty));
public static Patch from(String obf, String srg, byte[] clean, byte[] dirty, boolean minimizePatch) throws IOException {
if (minimizePatch) {
dirty = shrinkDirtyForPatch(clean, dirty);
}
byte[] diff = dirty.length == 0 ? EMPTY_DATA : DELTA.compute(clean, dirty);
int checksum = clean.length == 0 ? 0 : adlerHash(clean);
return new Patch(obf, srg, clean.length != 0, checksum, diff);
}
Expand Down

0 comments on commit c80cd2e

Please sign in to comment.