Skip to content

Commit

Permalink
qol: improve UI and add better logging
Browse files Browse the repository at this point in the history
  • Loading branch information
terminalsin committed Nov 13, 2024
1 parent 0169a74 commit fd27d70
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static void printLogo() {
" │ " + topMemory + " │",
" └───────────────────────────────────────────┘",
"",
" Author: Ghast Version: 2.0.10 Today: "
" Author: Ghast Version: 2.0.11 Today: "
+ DateFormat.getDateTimeInstance().format(new Date(Instant.now().toEpochMilli())),
""
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import dev.skidfuscator.obfuscator.dependency.matcher.DependencyMatcher;
import dev.skidfuscator.obfuscator.directory.SkiddedDirectory;
import dev.skidfuscator.obfuscator.event.EventBus;
import dev.skidfuscator.obfuscator.event.impl.TransformEvent;
import dev.skidfuscator.obfuscator.event.impl.transform.ClassTransformEvent;
import dev.skidfuscator.obfuscator.event.impl.transform.GroupTransformEvent;
import dev.skidfuscator.obfuscator.event.impl.transform.MethodTransformEvent;
Expand Down Expand Up @@ -52,6 +53,7 @@
import dev.skidfuscator.obfuscator.transform.impl.string.StringEncryptionType;
import dev.skidfuscator.obfuscator.transform.impl.string.StringTransformer;
import dev.skidfuscator.obfuscator.transform.impl.string.StringTransformerV2;
import dev.skidfuscator.obfuscator.util.ConsoleColors;
import dev.skidfuscator.obfuscator.util.MapleJarUtil;
import dev.skidfuscator.obfuscator.util.MiscUtil;
import dev.skidfuscator.obfuscator.util.ProgressUtil;
Expand Down Expand Up @@ -850,12 +852,14 @@ interface Caller {
}

private void run(final String phaseName, final Caller caller) {
final List<String> issues = new ArrayList<>();
final SkidTransformEvent event = caller.callBase();
EventBus.call(event);
issues.addAll(event.getIssues());

try (ProgressWrapper progressBar = ProgressUtil.progressCheck(
hierarchy.getClasses().size(),
pad("Running phase [" + phaseName + "] on " + hierarchy.getClasses().size() + " classes", 63) + "│",
pad("Running phase [" + phaseName + "] on " + hierarchy.getClasses().size() + " classes", 62) + "│",
"│ "
)){
for (ClassNode ccls : hierarchy.getClasses()) {
Expand All @@ -866,8 +870,9 @@ private void run(final String phaseName, final Caller caller) {
continue;
}

final TransformEvent classEvent = caller.callClass(classNode);
EventBus.call(
caller.callClass(classNode),
classEvent,
el -> {
final Class<?> listener = el.getListener().getClass();
return !Transformer.class.isAssignableFrom(listener)
Expand All @@ -877,13 +882,15 @@ private void run(final String phaseName, final Caller caller) {
);
}
);
issues.addAll(classEvent.getIssues());
progressBar.tick();
}
}


try (ProgressWrapper progressBar = ProgressUtil.progressCheck(
hierarchy.getGroups().size(),
pad("Running phase [" + phaseName + "] on " + hierarchy.getGroups().size() + " method groups", 63) + "│",
pad("Running phase [" + phaseName + "] on " + hierarchy.getGroups().size() + " method groups", 62) + "│",
"│ "
)){
for (SkidGroup group : hierarchy.getGroups()) {
Expand All @@ -892,8 +899,9 @@ private void run(final String phaseName, final Caller caller) {
continue;
}

final TransformEvent groupEvent = caller.callGroup(group);
EventBus.call(
caller.callGroup(group),
groupEvent,
el -> {
final Class<?> listenerClazz = el.getListener().getClass();
return !Transformer.class.isAssignableFrom(listenerClazz)
Expand All @@ -911,6 +919,7 @@ private void run(final String phaseName, final Caller caller) {
);
}
);
issues.addAll(groupEvent.getIssues());
progressBar.tick();
}
}
Expand All @@ -919,7 +928,7 @@ private void run(final String phaseName, final Caller caller) {

try (ProgressWrapper progressBar = ProgressUtil.progressCheck(
size,
pad("Running phase [" + phaseName + "] on " + size + " methods", 63) + "│",
pad("Running phase [" + phaseName + "] on " + size + " methods", 62) + "│",
"│ "
)){
for (ClassNode ccls : hierarchy.getClasses()) {
Expand Down Expand Up @@ -949,8 +958,9 @@ private void run(final String phaseName, final Caller caller) {
continue;
}

final TransformEvent methodEvent = caller.callMethod(methodNode);
EventBus.call(
caller.callMethod(methodNode),
methodEvent,
el -> {
final Class<?> listenerClazz = el.getListener().getClass();
return !Transformer.class.isAssignableFrom(listenerClazz)
Expand All @@ -960,11 +970,27 @@ private void run(final String phaseName, final Caller caller) {
);
}
);
issues.addAll(methodEvent.getIssues());
methodNode.getCfg().recomputeEdges();
progressBar.tick();
}
}
}

System.out.println(ansi().cursorUpLine()
.append("│ ")
.append(pad(String.format(
"Found " + ConsoleColors.RED + "%d " + ConsoleColors.RESET + "issues | " + " Modified " + ConsoleColors.YELLOW + "%d" + ConsoleColors.RESET,
issues.size(), event.getChanged()
), 87))
.append("│"));
issues.forEach(e -> System.out.println("│ --> " + pad(e, 61) + "│"));

if (!phaseName.equals("Finalize")) {
System.out.println("│───────────────────────────────────────────────────────────────────│\n");
} else {
System.out.println("");
}
}

private static String getBaseName(String fn) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1608,7 +1608,7 @@ private void dumpRange(ExceptionRange<BasicBlock> er) {
int rangeIdx = -1, orderIdx;
do {
if (++rangeIdx == range.size()) {
System.err.println("[warn] range is absent: " + m);
//System.err.println("[warn] range is absent: " + m);
return;
}
BasicBlock b = range.get(rangeIdx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

import dev.skidfuscator.obfuscator.Skidfuscator;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

public abstract class TransformEvent extends Event {
private int changed;
private final List<String> issues = new ArrayList<>();

public TransformEvent(Skidfuscator skidfuscator) {
super(skidfuscator);
Expand All @@ -12,4 +18,16 @@ public TransformEvent(Skidfuscator skidfuscator) {
public void tick() {
changed++;
}

public int getChanged() {
return changed;
}

public void warn(String issue) {
issues.add(issue);
}

public List<String> getIssues() {
return Collections.unmodifiableList(issues);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public Exclusion renderExclusion(final String pattern) {
else {
if (c == '}') {
final String matcher = padded.toString();
if (matcher.isEmpty()) {
throw new IllegalArgumentException("Empty matcher! What is going on? PATTERN: " + pattern);
}
final String[] split = matcher.contains(" ")
? matcher.split(" ")
: new String[]{matcher};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ public void cache() {
return skidfuscator.getClassSource().isApplicationClass(e.getName());
})
.filter(e -> !skidfuscator.getExemptAnalysis().isExempt(e))
/*.filter(e -> {
System.out.println("Caching " + e.getName());
return true;
})*/
.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public int dumpClass(JarOutputStream out, JarClassData classData) throws IOExcep
ClassTree tree = source.getClassTree();
for(MethodNode m : cn.getMethods()) {
if(m.node.instructions.size() > 10000) {
System.out.println("large method: " + m + " @" + m.node.instructions.size());
//System.out.println("large method: " + m + " @" + m.node.instructions.size());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,12 @@ void handle(final PostMethodTransformEvent event) {
final BasicBlock entryPoint = methodNode.getEntryBlock();
final SkidBlock seedEntry = (SkidBlock) entryPoint;
cfg.recomputeEdges();
cfg.verify();

try {
cfg.verify();
} catch (Exception e) {
event.warn("Failed to verify CFG for method " + methodNode.getName());
}

/*
* ____ __
Expand Down Expand Up @@ -867,7 +872,11 @@ void handle(final PostMethodTransformEvent event) {
System.out.println(cfg.toString());
}
cfg.recomputeEdges();
cfg.verify();
try {
cfg.verify();
} catch (Exception e) {
event.warn("Failed to verify post-CFG for method " + methodNode.getName());
}

return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ public void verify() {
final File output = new File("skidfuscator-error-" + RandomUtil.randomAlphabeticalString(3) + ".txt");
try {
Files.write(output.toPath(), this.toString().getBytes(StandardCharsets.UTF_8));
Skidfuscator.LOGGER.warn( "-----------------------------------------------------\n"
/*Skidfuscator.LOGGER.warn( "-----------------------------------------------------\n"
+ "/!\\ Skidfuscator failed to verify an obfuscated method!\n"
+ "Please use the following debug information and send it to Ghast#0001\n"
+ "\n"
+ "File " + output.getAbsolutePath()
+ "\n"
);
);*/
} catch (IOException ex) {
Skidfuscator.LOGGER.warn( "-----------------------------------------------------\n"
+ "/!\\ Skidfuscator failed to verify an obfuscated method!\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ public void visitPost(SkidClassNode node) {

));

System.out.println(String.format(
/*System.out.println(String.format(
"Generated buffer with %d chars and %d index in %s",
buffer.length(),
bufferIndex,
node.getName()
));
));*/
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ public int dumpClass(JarOutputStream out, JarClassData classData) throws IOExcep
}


for (MethodNode m : cn.getMethods()) {
/*for (MethodNode m : cn.getMethods()) {
if (m.node.instructions.size() > 10000) {
Skidfuscator.LOGGER.warn("large method: " + m + " @" + m.node.instructions.size() + "\n");
}
}
}*/

try {
final String name = skidfuscator.getClassRemapper()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public ProgressWrapper progressCheck(final int count, String success) {
}

public ProgressWrapper progressCheck(final int count, String success, String prefix) {
return progress(count, prefix + AnsiColors.green("✔") + " " + success);
return progress(count, prefix + AnsiColors.green("✔") + " " + success);
}

private Boolean isRunningTest = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ private void dumpRange(ExceptionRange<BasicBlock> er) {
int rangeIdx = -1, orderIdx;
do {
if (++rangeIdx == range.size()) {
System.err.println("[warn] range is absent: " + m);
//System.err.println("[warn] range is absent: " + m);
return;
}
BasicBlock b = range.get(rangeIdx);
Expand Down

0 comments on commit fd27d70

Please sign in to comment.