Skip to content

Commit

Permalink
feat: add support for json mapping pull + bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
terminalsin committed Nov 13, 2024
1 parent 1fb9e5b commit 456f240
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public class Skidfuscator {

private final SkidfuscatorSession session;

protected Set<CommonDependency> installedDependencies = new HashSet<>();
protected SkidApplicationClassSource classSource;
private LibraryClassSource jvmClassSource;
protected JarContents jarContents;
Expand Down Expand Up @@ -714,8 +715,16 @@ private void _verify() {
} catch (Exception ex) {
final List<String> missingClasses = classSource.getClassTree().getMissingClasses();

LOGGER.warn("Attempting to auto-resolve missing classes...");
final Set<CommonDependency> commonDependencies = Arrays.stream(CommonDependency.values()).filter(f -> f.getMatcher().test(missingClasses)).collect(Collectors.toSet());
LOGGER.warn(
"Attempting to auto-resolve missing classes..."
+ "\n"
+ "List of missing classes:\n"
+ missingClasses.stream().map(f -> " --> " + f + "\n").collect(Collectors.joining())
);
final Set<CommonDependency> commonDependencies = Arrays.stream(CommonDependency.values())
.filter(f -> f.getMatcher().test(missingClasses))
.filter(f -> !installedDependencies.contains(f))
.collect(Collectors.toSet());

if (commonDependencies.isEmpty()) {
LOGGER.warn("\n" +
Expand Down Expand Up @@ -751,6 +760,7 @@ private void _verify() {
"Resolved %d common dependencies... retrying verification...\n",
commonDependencies.size()
));
installedDependencies.addAll(commonDependencies);
_verify();
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,30 @@ public void download(final CommonDependency dependency) {
return;
}

Path zipFilePath = mappingsDir.resolve(dependency.name().toLowerCase() + "/download.zip");
Files.createDirectories(zipFilePath.getParent());
Path resolvedMappingPath = mappingsDir.resolve(dependency.name().toLowerCase() + "/download.mappings");
Files.createDirectories(resolvedMappingPath.getParent());

// Download the zip file
Skidfuscator.LOGGER.style(String.format("Downloading dependency %s from %s\n", dependency.name(), url));
try (BufferedInputStream in = new BufferedInputStream(new URL(url).openStream());
FileOutputStream fileOutputStream = new FileOutputStream(zipFilePath.toFile())) {
FileOutputStream fileOutputStream = new FileOutputStream(resolvedMappingPath.toFile())) {
byte[] dataBuffer = new byte[1024];
int bytesRead;
while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) {
fileOutputStream.write(dataBuffer, 0, bytesRead);
}
}

if (!Files.exists(zipFilePath)) {
throw new IOException("Failed to download the file: " + zipFilePath);
if (!Files.exists(resolvedMappingPath)) {
throw new IOException("Failed to download the file: " + resolvedMappingPath);
}

Skidfuscator.LOGGER.style(String.format("Downloaded dependency %s to %s\n", dependency.name(), zipFilePath));
Skidfuscator.LOGGER.style(String.format("Downloaded dependency %s to %s\n", dependency.name(), resolvedMappingPath));

if (url.endsWith(".jar")) {
throw new IllegalArgumentException("Invalid dependency type");
} else if (url.endsWith(".zip")) {
try (ZipInputStream zipInputStream = new ZipInputStream(Files.newInputStream(zipFilePath))) {
try (ZipInputStream zipInputStream = new ZipInputStream(Files.newInputStream(resolvedMappingPath))) {
ZipEntry entry;
while ((entry = zipInputStream.getNextEntry()) != null) {
Path filePath = mappingsDir.resolve(entry.getName());
Expand All @@ -71,10 +71,15 @@ public void download(final CommonDependency dependency) {
zipInputStream.closeEntry();
}
}
Files.delete(zipFilePath);
Skidfuscator.LOGGER.style(String.format("Extracted dependency %s to %s\n", dependency.name(), mappingsDir.toFile().getAbsolutePath()));
} else {
} else if (url.endsWith(".json")) {
// Copy the file to the mappings directory
Files.copy(resolvedMappingPath, mappingsDir.resolve(dependency.name().toLowerCase() + ".json"));
Skidfuscator.LOGGER.style(String.format("Extracted JSON particular dependency %s to %s\n", dependency.name(), mappingsDir.toFile().getAbsolutePath()));
}
else {
Skidfuscator.LOGGER.style(String.format("Unsupported file type for %s\n", url));
}
Files.delete(resolvedMappingPath);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,6 @@ private void setupInvoke() {
.filter(e -> e instanceof Invokable)
.map(e -> (Invocation) e)
.forEach(invocation -> {

if (invocation.getName().equals("handle")) {
System.out.println("Invoking " + invocation.getOwner() + "#"
+ invocation.getName() + invocation.getDesc());
}

final ClassMethodHash target;

if (invocation instanceof DynamicInvocationExpr) {
Expand Down

0 comments on commit 456f240

Please sign in to comment.